72da4ef7aa9a3ae1f1188c173ba00a7652237bc5
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2016 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "gdb.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "block.h"
51 #include "solib.h"
52 #include "solist.h"
53 #include "observer.h"
54 #include "memattr.h"
55 #include "ada-lang.h"
56 #include "top.h"
57 #include "valprint.h"
58 #include "jit.h"
59 #include "parser-defs.h"
60 #include "gdb_regex.h"
61 #include "probe.h"
62 #include "cli/cli-utils.h"
63 #include "continuations.h"
64 #include "stack.h"
65 #include "skip.h"
66 #include "ax-gdb.h"
67 #include "dummy-frame.h"
68 #include "interps.h"
69 #include "format.h"
70 #include "location.h"
71 #include "thread-fsm.h"
72 #include "tid-parse.h"
73
74 /* readline include files */
75 #include "readline/readline.h"
76 #include "readline/history.h"
77
78 /* readline defines this. */
79 #undef savestring
80
81 #include "mi/mi-common.h"
82 #include "extension.h"
83
84 /* Enums for exception-handling support. */
85 enum exception_event_kind
86 {
87 EX_EVENT_THROW,
88 EX_EVENT_RETHROW,
89 EX_EVENT_CATCH
90 };
91
92 /* Prototypes for local functions. */
93
94 static void enable_delete_command (char *, int);
95
96 static void enable_once_command (char *, int);
97
98 static void enable_count_command (char *, int);
99
100 static void disable_command (char *, int);
101
102 static void enable_command (char *, int);
103
104 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
105 void *),
106 void *);
107
108 static void ignore_command (char *, int);
109
110 static int breakpoint_re_set_one (void *);
111
112 static void breakpoint_re_set_default (struct breakpoint *);
113
114 static void
115 create_sals_from_location_default (const struct event_location *location,
116 struct linespec_result *canonical,
117 enum bptype type_wanted);
118
119 static void create_breakpoints_sal_default (struct gdbarch *,
120 struct linespec_result *,
121 char *, char *, enum bptype,
122 enum bpdisp, int, int,
123 int,
124 const struct breakpoint_ops *,
125 int, int, int, unsigned);
126
127 static void decode_location_default (struct breakpoint *b,
128 const struct event_location *location,
129 struct symtabs_and_lines *sals);
130
131 static void clear_command (char *, int);
132
133 static void catch_command (char *, int);
134
135 static int can_use_hardware_watchpoint (struct value *);
136
137 static void break_command_1 (char *, int, int);
138
139 static void mention (struct breakpoint *);
140
141 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
142 enum bptype,
143 const struct breakpoint_ops *);
144 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
145 const struct symtab_and_line *);
146
147 /* This function is used in gdbtk sources and thus can not be made
148 static. */
149 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
150 struct symtab_and_line,
151 enum bptype,
152 const struct breakpoint_ops *);
153
154 static struct breakpoint *
155 momentary_breakpoint_from_master (struct breakpoint *orig,
156 enum bptype type,
157 const struct breakpoint_ops *ops,
158 int loc_enabled);
159
160 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
161
162 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
163 CORE_ADDR bpaddr,
164 enum bptype bptype);
165
166 static void describe_other_breakpoints (struct gdbarch *,
167 struct program_space *, CORE_ADDR,
168 struct obj_section *, int);
169
170 static int watchpoint_locations_match (struct bp_location *loc1,
171 struct bp_location *loc2);
172
173 static int breakpoint_location_address_match (struct bp_location *bl,
174 struct address_space *aspace,
175 CORE_ADDR addr);
176
177 static int breakpoint_location_address_range_overlap (struct bp_location *,
178 struct address_space *,
179 CORE_ADDR, int);
180
181 static void breakpoints_info (char *, int);
182
183 static void watchpoints_info (char *, int);
184
185 static int breakpoint_1 (char *, int,
186 int (*) (const struct breakpoint *));
187
188 static int breakpoint_cond_eval (void *);
189
190 static void cleanup_executing_breakpoints (void *);
191
192 static void commands_command (char *, int);
193
194 static void condition_command (char *, int);
195
196 typedef enum
197 {
198 mark_inserted,
199 mark_uninserted
200 }
201 insertion_state_t;
202
203 static int remove_breakpoint (struct bp_location *, insertion_state_t);
204 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
205
206 static enum print_stop_action print_bp_stop_message (bpstat bs);
207
208 static int watchpoint_check (void *);
209
210 static void maintenance_info_breakpoints (char *, int);
211
212 static int hw_breakpoint_used_count (void);
213
214 static int hw_watchpoint_use_count (struct breakpoint *);
215
216 static int hw_watchpoint_used_count_others (struct breakpoint *except,
217 enum bptype type,
218 int *other_type_used);
219
220 static void hbreak_command (char *, int);
221
222 static void thbreak_command (char *, int);
223
224 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
225 int count);
226
227 static void stop_command (char *arg, int from_tty);
228
229 static void stopin_command (char *arg, int from_tty);
230
231 static void stopat_command (char *arg, int from_tty);
232
233 static void tcatch_command (char *arg, int from_tty);
234
235 static void free_bp_location (struct bp_location *loc);
236 static void incref_bp_location (struct bp_location *loc);
237 static void decref_bp_location (struct bp_location **loc);
238
239 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
240
241 /* update_global_location_list's modes of operation wrt to whether to
242 insert locations now. */
243 enum ugll_insert_mode
244 {
245 /* Don't insert any breakpoint locations into the inferior, only
246 remove already-inserted locations that no longer should be
247 inserted. Functions that delete a breakpoint or breakpoints
248 should specify this mode, so that deleting a breakpoint doesn't
249 have the side effect of inserting the locations of other
250 breakpoints that are marked not-inserted, but should_be_inserted
251 returns true on them.
252
253 This behavior is useful is situations close to tear-down -- e.g.,
254 after an exec, while the target still has execution, but
255 breakpoint shadows of the previous executable image should *NOT*
256 be restored to the new image; or before detaching, where the
257 target still has execution and wants to delete breakpoints from
258 GDB's lists, and all breakpoints had already been removed from
259 the inferior. */
260 UGLL_DONT_INSERT,
261
262 /* May insert breakpoints iff breakpoints_should_be_inserted_now
263 claims breakpoints should be inserted now. */
264 UGLL_MAY_INSERT,
265
266 /* Insert locations now, irrespective of
267 breakpoints_should_be_inserted_now. E.g., say all threads are
268 stopped right now, and the user did "continue". We need to
269 insert breakpoints _before_ resuming the target, but
270 UGLL_MAY_INSERT wouldn't insert them, because
271 breakpoints_should_be_inserted_now returns false at that point,
272 as no thread is running yet. */
273 UGLL_INSERT
274 };
275
276 static void update_global_location_list (enum ugll_insert_mode);
277
278 static void update_global_location_list_nothrow (enum ugll_insert_mode);
279
280 static int is_hardware_watchpoint (const struct breakpoint *bpt);
281
282 static void insert_breakpoint_locations (void);
283
284 static void tracepoints_info (char *, int);
285
286 static void delete_trace_command (char *, int);
287
288 static void enable_trace_command (char *, int);
289
290 static void disable_trace_command (char *, int);
291
292 static void trace_pass_command (char *, int);
293
294 static void set_tracepoint_count (int num);
295
296 static int is_masked_watchpoint (const struct breakpoint *b);
297
298 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
299
300 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
301 otherwise. */
302
303 static int strace_marker_p (struct breakpoint *b);
304
305 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
306 that are implemented on top of software or hardware breakpoints
307 (user breakpoints, internal and momentary breakpoints, etc.). */
308 static struct breakpoint_ops bkpt_base_breakpoint_ops;
309
310 /* Internal breakpoints class type. */
311 static struct breakpoint_ops internal_breakpoint_ops;
312
313 /* Momentary breakpoints class type. */
314 static struct breakpoint_ops momentary_breakpoint_ops;
315
316 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
317 static struct breakpoint_ops longjmp_breakpoint_ops;
318
319 /* The breakpoint_ops structure to be used in regular user created
320 breakpoints. */
321 struct breakpoint_ops bkpt_breakpoint_ops;
322
323 /* Breakpoints set on probes. */
324 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
325
326 /* Dynamic printf class type. */
327 struct breakpoint_ops dprintf_breakpoint_ops;
328
329 /* The style in which to perform a dynamic printf. This is a user
330 option because different output options have different tradeoffs;
331 if GDB does the printing, there is better error handling if there
332 is a problem with any of the arguments, but using an inferior
333 function lets you have special-purpose printers and sending of
334 output to the same place as compiled-in print functions. */
335
336 static const char dprintf_style_gdb[] = "gdb";
337 static const char dprintf_style_call[] = "call";
338 static const char dprintf_style_agent[] = "agent";
339 static const char *const dprintf_style_enums[] = {
340 dprintf_style_gdb,
341 dprintf_style_call,
342 dprintf_style_agent,
343 NULL
344 };
345 static const char *dprintf_style = dprintf_style_gdb;
346
347 /* The function to use for dynamic printf if the preferred style is to
348 call into the inferior. The value is simply a string that is
349 copied into the command, so it can be anything that GDB can
350 evaluate to a callable address, not necessarily a function name. */
351
352 static char *dprintf_function = "";
353
354 /* The channel to use for dynamic printf if the preferred style is to
355 call into the inferior; if a nonempty string, it will be passed to
356 the call as the first argument, with the format string as the
357 second. As with the dprintf function, this can be anything that
358 GDB knows how to evaluate, so in addition to common choices like
359 "stderr", this could be an app-specific expression like
360 "mystreams[curlogger]". */
361
362 static char *dprintf_channel = "";
363
364 /* True if dprintf commands should continue to operate even if GDB
365 has disconnected. */
366 static int disconnected_dprintf = 1;
367
368 /* A reference-counted struct command_line. This lets multiple
369 breakpoints share a single command list. */
370 struct counted_command_line
371 {
372 /* The reference count. */
373 int refc;
374
375 /* The command list. */
376 struct command_line *commands;
377 };
378
379 struct command_line *
380 breakpoint_commands (struct breakpoint *b)
381 {
382 return b->commands ? b->commands->commands : NULL;
383 }
384
385 /* Flag indicating that a command has proceeded the inferior past the
386 current breakpoint. */
387
388 static int breakpoint_proceeded;
389
390 const char *
391 bpdisp_text (enum bpdisp disp)
392 {
393 /* NOTE: the following values are a part of MI protocol and
394 represent values of 'disp' field returned when inferior stops at
395 a breakpoint. */
396 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
397
398 return bpdisps[(int) disp];
399 }
400
401 /* Prototypes for exported functions. */
402 /* If FALSE, gdb will not use hardware support for watchpoints, even
403 if such is available. */
404 static int can_use_hw_watchpoints;
405
406 static void
407 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
408 struct cmd_list_element *c,
409 const char *value)
410 {
411 fprintf_filtered (file,
412 _("Debugger's willingness to use "
413 "watchpoint hardware is %s.\n"),
414 value);
415 }
416
417 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
418 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
419 for unrecognized breakpoint locations.
420 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
421 static enum auto_boolean pending_break_support;
422 static void
423 show_pending_break_support (struct ui_file *file, int from_tty,
424 struct cmd_list_element *c,
425 const char *value)
426 {
427 fprintf_filtered (file,
428 _("Debugger's behavior regarding "
429 "pending breakpoints is %s.\n"),
430 value);
431 }
432
433 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
434 set with "break" but falling in read-only memory.
435 If 0, gdb will warn about such breakpoints, but won't automatically
436 use hardware breakpoints. */
437 static int automatic_hardware_breakpoints;
438 static void
439 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
440 struct cmd_list_element *c,
441 const char *value)
442 {
443 fprintf_filtered (file,
444 _("Automatic usage of hardware breakpoints is %s.\n"),
445 value);
446 }
447
448 /* If on, GDB keeps breakpoints inserted even if the inferior is
449 stopped, and immediately inserts any new breakpoints as soon as
450 they're created. If off (default), GDB keeps breakpoints off of
451 the target as long as possible. That is, it delays inserting
452 breakpoints until the next resume, and removes them again when the
453 target fully stops. This is a bit safer in case GDB crashes while
454 processing user input. */
455 static int always_inserted_mode = 0;
456
457 static void
458 show_always_inserted_mode (struct ui_file *file, int from_tty,
459 struct cmd_list_element *c, const char *value)
460 {
461 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
462 value);
463 }
464
465 /* See breakpoint.h. */
466
467 int
468 breakpoints_should_be_inserted_now (void)
469 {
470 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
471 {
472 /* If breakpoints are global, they should be inserted even if no
473 thread under gdb's control is running, or even if there are
474 no threads under GDB's control yet. */
475 return 1;
476 }
477 else if (target_has_execution)
478 {
479 struct thread_info *tp;
480
481 if (always_inserted_mode)
482 {
483 /* The user wants breakpoints inserted even if all threads
484 are stopped. */
485 return 1;
486 }
487
488 if (threads_are_executing ())
489 return 1;
490
491 /* Don't remove breakpoints yet if, even though all threads are
492 stopped, we still have events to process. */
493 ALL_NON_EXITED_THREADS (tp)
494 if (tp->resumed
495 && tp->suspend.waitstatus_pending_p)
496 return 1;
497 }
498 return 0;
499 }
500
501 static const char condition_evaluation_both[] = "host or target";
502
503 /* Modes for breakpoint condition evaluation. */
504 static const char condition_evaluation_auto[] = "auto";
505 static const char condition_evaluation_host[] = "host";
506 static const char condition_evaluation_target[] = "target";
507 static const char *const condition_evaluation_enums[] = {
508 condition_evaluation_auto,
509 condition_evaluation_host,
510 condition_evaluation_target,
511 NULL
512 };
513
514 /* Global that holds the current mode for breakpoint condition evaluation. */
515 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
516
517 /* Global that we use to display information to the user (gets its value from
518 condition_evaluation_mode_1. */
519 static const char *condition_evaluation_mode = condition_evaluation_auto;
520
521 /* Translate a condition evaluation mode MODE into either "host"
522 or "target". This is used mostly to translate from "auto" to the
523 real setting that is being used. It returns the translated
524 evaluation mode. */
525
526 static const char *
527 translate_condition_evaluation_mode (const char *mode)
528 {
529 if (mode == condition_evaluation_auto)
530 {
531 if (target_supports_evaluation_of_breakpoint_conditions ())
532 return condition_evaluation_target;
533 else
534 return condition_evaluation_host;
535 }
536 else
537 return mode;
538 }
539
540 /* Discovers what condition_evaluation_auto translates to. */
541
542 static const char *
543 breakpoint_condition_evaluation_mode (void)
544 {
545 return translate_condition_evaluation_mode (condition_evaluation_mode);
546 }
547
548 /* Return true if GDB should evaluate breakpoint conditions or false
549 otherwise. */
550
551 static int
552 gdb_evaluates_breakpoint_condition_p (void)
553 {
554 const char *mode = breakpoint_condition_evaluation_mode ();
555
556 return (mode == condition_evaluation_host);
557 }
558
559 void _initialize_breakpoint (void);
560
561 /* Are we executing breakpoint commands? */
562 static int executing_breakpoint_commands;
563
564 /* Are overlay event breakpoints enabled? */
565 static int overlay_events_enabled;
566
567 /* See description in breakpoint.h. */
568 int target_exact_watchpoints = 0;
569
570 /* Walk the following statement or block through all breakpoints.
571 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
572 current breakpoint. */
573
574 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
575
576 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
577 for (B = breakpoint_chain; \
578 B ? (TMP=B->next, 1): 0; \
579 B = TMP)
580
581 /* Similar iterator for the low-level breakpoints. SAFE variant is
582 not provided so update_global_location_list must not be called
583 while executing the block of ALL_BP_LOCATIONS. */
584
585 #define ALL_BP_LOCATIONS(B,BP_TMP) \
586 for (BP_TMP = bp_location; \
587 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
588 BP_TMP++)
589
590 /* Iterates through locations with address ADDRESS for the currently selected
591 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
592 to where the loop should start from.
593 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
594 appropriate location to start with. */
595
596 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
597 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
598 BP_LOCP_TMP = BP_LOCP_START; \
599 BP_LOCP_START \
600 && (BP_LOCP_TMP < bp_location + bp_location_count \
601 && (*BP_LOCP_TMP)->address == ADDRESS); \
602 BP_LOCP_TMP++)
603
604 /* Iterator for tracepoints only. */
605
606 #define ALL_TRACEPOINTS(B) \
607 for (B = breakpoint_chain; B; B = B->next) \
608 if (is_tracepoint (B))
609
610 /* Chains of all breakpoints defined. */
611
612 struct breakpoint *breakpoint_chain;
613
614 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
615
616 static struct bp_location **bp_location;
617
618 /* Number of elements of BP_LOCATION. */
619
620 static unsigned bp_location_count;
621
622 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
623 ADDRESS for the current elements of BP_LOCATION which get a valid
624 result from bp_location_has_shadow. You can use it for roughly
625 limiting the subrange of BP_LOCATION to scan for shadow bytes for
626 an address you need to read. */
627
628 static CORE_ADDR bp_location_placed_address_before_address_max;
629
630 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
631 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
632 BP_LOCATION which get a valid result from bp_location_has_shadow.
633 You can use it for roughly limiting the subrange of BP_LOCATION to
634 scan for shadow bytes for an address you need to read. */
635
636 static CORE_ADDR bp_location_shadow_len_after_address_max;
637
638 /* The locations that no longer correspond to any breakpoint, unlinked
639 from bp_location array, but for which a hit may still be reported
640 by a target. */
641 VEC(bp_location_p) *moribund_locations = NULL;
642
643 /* Number of last breakpoint made. */
644
645 static int breakpoint_count;
646
647 /* The value of `breakpoint_count' before the last command that
648 created breakpoints. If the last (break-like) command created more
649 than one breakpoint, then the difference between BREAKPOINT_COUNT
650 and PREV_BREAKPOINT_COUNT is more than one. */
651 static int prev_breakpoint_count;
652
653 /* Number of last tracepoint made. */
654
655 static int tracepoint_count;
656
657 static struct cmd_list_element *breakpoint_set_cmdlist;
658 static struct cmd_list_element *breakpoint_show_cmdlist;
659 struct cmd_list_element *save_cmdlist;
660
661 /* See declaration at breakpoint.h. */
662
663 struct breakpoint *
664 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
665 void *user_data)
666 {
667 struct breakpoint *b = NULL;
668
669 ALL_BREAKPOINTS (b)
670 {
671 if (func (b, user_data) != 0)
672 break;
673 }
674
675 return b;
676 }
677
678 /* Return whether a breakpoint is an active enabled breakpoint. */
679 static int
680 breakpoint_enabled (struct breakpoint *b)
681 {
682 return (b->enable_state == bp_enabled);
683 }
684
685 /* Set breakpoint count to NUM. */
686
687 static void
688 set_breakpoint_count (int num)
689 {
690 prev_breakpoint_count = breakpoint_count;
691 breakpoint_count = num;
692 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
693 }
694
695 /* Used by `start_rbreak_breakpoints' below, to record the current
696 breakpoint count before "rbreak" creates any breakpoint. */
697 static int rbreak_start_breakpoint_count;
698
699 /* Called at the start an "rbreak" command to record the first
700 breakpoint made. */
701
702 void
703 start_rbreak_breakpoints (void)
704 {
705 rbreak_start_breakpoint_count = breakpoint_count;
706 }
707
708 /* Called at the end of an "rbreak" command to record the last
709 breakpoint made. */
710
711 void
712 end_rbreak_breakpoints (void)
713 {
714 prev_breakpoint_count = rbreak_start_breakpoint_count;
715 }
716
717 /* Used in run_command to zero the hit count when a new run starts. */
718
719 void
720 clear_breakpoint_hit_counts (void)
721 {
722 struct breakpoint *b;
723
724 ALL_BREAKPOINTS (b)
725 b->hit_count = 0;
726 }
727
728 /* Allocate a new counted_command_line with reference count of 1.
729 The new structure owns COMMANDS. */
730
731 static struct counted_command_line *
732 alloc_counted_command_line (struct command_line *commands)
733 {
734 struct counted_command_line *result = XNEW (struct counted_command_line);
735
736 result->refc = 1;
737 result->commands = commands;
738
739 return result;
740 }
741
742 /* Increment reference count. This does nothing if CMD is NULL. */
743
744 static void
745 incref_counted_command_line (struct counted_command_line *cmd)
746 {
747 if (cmd)
748 ++cmd->refc;
749 }
750
751 /* Decrement reference count. If the reference count reaches 0,
752 destroy the counted_command_line. Sets *CMDP to NULL. This does
753 nothing if *CMDP is NULL. */
754
755 static void
756 decref_counted_command_line (struct counted_command_line **cmdp)
757 {
758 if (*cmdp)
759 {
760 if (--(*cmdp)->refc == 0)
761 {
762 free_command_lines (&(*cmdp)->commands);
763 xfree (*cmdp);
764 }
765 *cmdp = NULL;
766 }
767 }
768
769 /* A cleanup function that calls decref_counted_command_line. */
770
771 static void
772 do_cleanup_counted_command_line (void *arg)
773 {
774 decref_counted_command_line ((struct counted_command_line **) arg);
775 }
776
777 /* Create a cleanup that calls decref_counted_command_line on the
778 argument. */
779
780 static struct cleanup *
781 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
782 {
783 return make_cleanup (do_cleanup_counted_command_line, cmdp);
784 }
785
786 \f
787 /* Return the breakpoint with the specified number, or NULL
788 if the number does not refer to an existing breakpoint. */
789
790 struct breakpoint *
791 get_breakpoint (int num)
792 {
793 struct breakpoint *b;
794
795 ALL_BREAKPOINTS (b)
796 if (b->number == num)
797 return b;
798
799 return NULL;
800 }
801
802 \f
803
804 /* Mark locations as "conditions have changed" in case the target supports
805 evaluating conditions on its side. */
806
807 static void
808 mark_breakpoint_modified (struct breakpoint *b)
809 {
810 struct bp_location *loc;
811
812 /* This is only meaningful if the target is
813 evaluating conditions and if the user has
814 opted for condition evaluation on the target's
815 side. */
816 if (gdb_evaluates_breakpoint_condition_p ()
817 || !target_supports_evaluation_of_breakpoint_conditions ())
818 return;
819
820 if (!is_breakpoint (b))
821 return;
822
823 for (loc = b->loc; loc; loc = loc->next)
824 loc->condition_changed = condition_modified;
825 }
826
827 /* Mark location as "conditions have changed" in case the target supports
828 evaluating conditions on its side. */
829
830 static void
831 mark_breakpoint_location_modified (struct bp_location *loc)
832 {
833 /* This is only meaningful if the target is
834 evaluating conditions and if the user has
835 opted for condition evaluation on the target's
836 side. */
837 if (gdb_evaluates_breakpoint_condition_p ()
838 || !target_supports_evaluation_of_breakpoint_conditions ())
839
840 return;
841
842 if (!is_breakpoint (loc->owner))
843 return;
844
845 loc->condition_changed = condition_modified;
846 }
847
848 /* Sets the condition-evaluation mode using the static global
849 condition_evaluation_mode. */
850
851 static void
852 set_condition_evaluation_mode (char *args, int from_tty,
853 struct cmd_list_element *c)
854 {
855 const char *old_mode, *new_mode;
856
857 if ((condition_evaluation_mode_1 == condition_evaluation_target)
858 && !target_supports_evaluation_of_breakpoint_conditions ())
859 {
860 condition_evaluation_mode_1 = condition_evaluation_mode;
861 warning (_("Target does not support breakpoint condition evaluation.\n"
862 "Using host evaluation mode instead."));
863 return;
864 }
865
866 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
867 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
868
869 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
870 settings was "auto". */
871 condition_evaluation_mode = condition_evaluation_mode_1;
872
873 /* Only update the mode if the user picked a different one. */
874 if (new_mode != old_mode)
875 {
876 struct bp_location *loc, **loc_tmp;
877 /* If the user switched to a different evaluation mode, we
878 need to synch the changes with the target as follows:
879
880 "host" -> "target": Send all (valid) conditions to the target.
881 "target" -> "host": Remove all the conditions from the target.
882 */
883
884 if (new_mode == condition_evaluation_target)
885 {
886 /* Mark everything modified and synch conditions with the
887 target. */
888 ALL_BP_LOCATIONS (loc, loc_tmp)
889 mark_breakpoint_location_modified (loc);
890 }
891 else
892 {
893 /* Manually mark non-duplicate locations to synch conditions
894 with the target. We do this to remove all the conditions the
895 target knows about. */
896 ALL_BP_LOCATIONS (loc, loc_tmp)
897 if (is_breakpoint (loc->owner) && loc->inserted)
898 loc->needs_update = 1;
899 }
900
901 /* Do the update. */
902 update_global_location_list (UGLL_MAY_INSERT);
903 }
904
905 return;
906 }
907
908 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
909 what "auto" is translating to. */
910
911 static void
912 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
913 struct cmd_list_element *c, const char *value)
914 {
915 if (condition_evaluation_mode == condition_evaluation_auto)
916 fprintf_filtered (file,
917 _("Breakpoint condition evaluation "
918 "mode is %s (currently %s).\n"),
919 value,
920 breakpoint_condition_evaluation_mode ());
921 else
922 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
923 value);
924 }
925
926 /* A comparison function for bp_location AP and BP that is used by
927 bsearch. This comparison function only cares about addresses, unlike
928 the more general bp_location_compare function. */
929
930 static int
931 bp_location_compare_addrs (const void *ap, const void *bp)
932 {
933 const struct bp_location *a = *(const struct bp_location **) ap;
934 const struct bp_location *b = *(const struct bp_location **) bp;
935
936 if (a->address == b->address)
937 return 0;
938 else
939 return ((a->address > b->address) - (a->address < b->address));
940 }
941
942 /* Helper function to skip all bp_locations with addresses
943 less than ADDRESS. It returns the first bp_location that
944 is greater than or equal to ADDRESS. If none is found, just
945 return NULL. */
946
947 static struct bp_location **
948 get_first_locp_gte_addr (CORE_ADDR address)
949 {
950 struct bp_location dummy_loc;
951 struct bp_location *dummy_locp = &dummy_loc;
952 struct bp_location **locp_found = NULL;
953
954 /* Initialize the dummy location's address field. */
955 memset (&dummy_loc, 0, sizeof (struct bp_location));
956 dummy_loc.address = address;
957
958 /* Find a close match to the first location at ADDRESS. */
959 locp_found = ((struct bp_location **)
960 bsearch (&dummy_locp, bp_location, bp_location_count,
961 sizeof (struct bp_location **),
962 bp_location_compare_addrs));
963
964 /* Nothing was found, nothing left to do. */
965 if (locp_found == NULL)
966 return NULL;
967
968 /* We may have found a location that is at ADDRESS but is not the first in the
969 location's list. Go backwards (if possible) and locate the first one. */
970 while ((locp_found - 1) >= bp_location
971 && (*(locp_found - 1))->address == address)
972 locp_found--;
973
974 return locp_found;
975 }
976
977 void
978 set_breakpoint_condition (struct breakpoint *b, const char *exp,
979 int from_tty)
980 {
981 xfree (b->cond_string);
982 b->cond_string = NULL;
983
984 if (is_watchpoint (b))
985 {
986 struct watchpoint *w = (struct watchpoint *) b;
987
988 xfree (w->cond_exp);
989 w->cond_exp = NULL;
990 }
991 else
992 {
993 struct bp_location *loc;
994
995 for (loc = b->loc; loc; loc = loc->next)
996 {
997 xfree (loc->cond);
998 loc->cond = NULL;
999
1000 /* No need to free the condition agent expression
1001 bytecode (if we have one). We will handle this
1002 when we go through update_global_location_list. */
1003 }
1004 }
1005
1006 if (*exp == 0)
1007 {
1008 if (from_tty)
1009 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1010 }
1011 else
1012 {
1013 const char *arg = exp;
1014
1015 /* I don't know if it matters whether this is the string the user
1016 typed in or the decompiled expression. */
1017 b->cond_string = xstrdup (arg);
1018 b->condition_not_parsed = 0;
1019
1020 if (is_watchpoint (b))
1021 {
1022 struct watchpoint *w = (struct watchpoint *) b;
1023
1024 innermost_block = NULL;
1025 arg = exp;
1026 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1027 if (*arg)
1028 error (_("Junk at end of expression"));
1029 w->cond_exp_valid_block = innermost_block;
1030 }
1031 else
1032 {
1033 struct bp_location *loc;
1034
1035 for (loc = b->loc; loc; loc = loc->next)
1036 {
1037 arg = exp;
1038 loc->cond =
1039 parse_exp_1 (&arg, loc->address,
1040 block_for_pc (loc->address), 0);
1041 if (*arg)
1042 error (_("Junk at end of expression"));
1043 }
1044 }
1045 }
1046 mark_breakpoint_modified (b);
1047
1048 observer_notify_breakpoint_modified (b);
1049 }
1050
1051 /* Completion for the "condition" command. */
1052
1053 static VEC (char_ptr) *
1054 condition_completer (struct cmd_list_element *cmd,
1055 const char *text, const char *word)
1056 {
1057 const char *space;
1058
1059 text = skip_spaces_const (text);
1060 space = skip_to_space_const (text);
1061 if (*space == '\0')
1062 {
1063 int len;
1064 struct breakpoint *b;
1065 VEC (char_ptr) *result = NULL;
1066
1067 if (text[0] == '$')
1068 {
1069 /* We don't support completion of history indices. */
1070 if (isdigit (text[1]))
1071 return NULL;
1072 return complete_internalvar (&text[1]);
1073 }
1074
1075 /* We're completing the breakpoint number. */
1076 len = strlen (text);
1077
1078 ALL_BREAKPOINTS (b)
1079 {
1080 char number[50];
1081
1082 xsnprintf (number, sizeof (number), "%d", b->number);
1083
1084 if (strncmp (number, text, len) == 0)
1085 VEC_safe_push (char_ptr, result, xstrdup (number));
1086 }
1087
1088 return result;
1089 }
1090
1091 /* We're completing the expression part. */
1092 text = skip_spaces_const (space);
1093 return expression_completer (cmd, text, word);
1094 }
1095
1096 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1097
1098 static void
1099 condition_command (char *arg, int from_tty)
1100 {
1101 struct breakpoint *b;
1102 char *p;
1103 int bnum;
1104
1105 if (arg == 0)
1106 error_no_arg (_("breakpoint number"));
1107
1108 p = arg;
1109 bnum = get_number (&p);
1110 if (bnum == 0)
1111 error (_("Bad breakpoint argument: '%s'"), arg);
1112
1113 ALL_BREAKPOINTS (b)
1114 if (b->number == bnum)
1115 {
1116 /* Check if this breakpoint has a "stop" method implemented in an
1117 extension language. This method and conditions entered into GDB
1118 from the CLI are mutually exclusive. */
1119 const struct extension_language_defn *extlang
1120 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1121
1122 if (extlang != NULL)
1123 {
1124 error (_("Only one stop condition allowed. There is currently"
1125 " a %s stop condition defined for this breakpoint."),
1126 ext_lang_capitalized_name (extlang));
1127 }
1128 set_breakpoint_condition (b, p, from_tty);
1129
1130 if (is_breakpoint (b))
1131 update_global_location_list (UGLL_MAY_INSERT);
1132
1133 return;
1134 }
1135
1136 error (_("No breakpoint number %d."), bnum);
1137 }
1138
1139 /* Check that COMMAND do not contain commands that are suitable
1140 only for tracepoints and not suitable for ordinary breakpoints.
1141 Throw if any such commands is found. */
1142
1143 static void
1144 check_no_tracepoint_commands (struct command_line *commands)
1145 {
1146 struct command_line *c;
1147
1148 for (c = commands; c; c = c->next)
1149 {
1150 int i;
1151
1152 if (c->control_type == while_stepping_control)
1153 error (_("The 'while-stepping' command can "
1154 "only be used for tracepoints"));
1155
1156 for (i = 0; i < c->body_count; ++i)
1157 check_no_tracepoint_commands ((c->body_list)[i]);
1158
1159 /* Not that command parsing removes leading whitespace and comment
1160 lines and also empty lines. So, we only need to check for
1161 command directly. */
1162 if (strstr (c->line, "collect ") == c->line)
1163 error (_("The 'collect' command can only be used for tracepoints"));
1164
1165 if (strstr (c->line, "teval ") == c->line)
1166 error (_("The 'teval' command can only be used for tracepoints"));
1167 }
1168 }
1169
1170 /* Encapsulate tests for different types of tracepoints. */
1171
1172 static int
1173 is_tracepoint_type (enum bptype type)
1174 {
1175 return (type == bp_tracepoint
1176 || type == bp_fast_tracepoint
1177 || type == bp_static_tracepoint);
1178 }
1179
1180 int
1181 is_tracepoint (const struct breakpoint *b)
1182 {
1183 return is_tracepoint_type (b->type);
1184 }
1185
1186 /* A helper function that validates that COMMANDS are valid for a
1187 breakpoint. This function will throw an exception if a problem is
1188 found. */
1189
1190 static void
1191 validate_commands_for_breakpoint (struct breakpoint *b,
1192 struct command_line *commands)
1193 {
1194 if (is_tracepoint (b))
1195 {
1196 struct tracepoint *t = (struct tracepoint *) b;
1197 struct command_line *c;
1198 struct command_line *while_stepping = 0;
1199
1200 /* Reset the while-stepping step count. The previous commands
1201 might have included a while-stepping action, while the new
1202 ones might not. */
1203 t->step_count = 0;
1204
1205 /* We need to verify that each top-level element of commands is
1206 valid for tracepoints, that there's at most one
1207 while-stepping element, and that the while-stepping's body
1208 has valid tracing commands excluding nested while-stepping.
1209 We also need to validate the tracepoint action line in the
1210 context of the tracepoint --- validate_actionline actually
1211 has side effects, like setting the tracepoint's
1212 while-stepping STEP_COUNT, in addition to checking if the
1213 collect/teval actions parse and make sense in the
1214 tracepoint's context. */
1215 for (c = commands; c; c = c->next)
1216 {
1217 if (c->control_type == while_stepping_control)
1218 {
1219 if (b->type == bp_fast_tracepoint)
1220 error (_("The 'while-stepping' command "
1221 "cannot be used for fast tracepoint"));
1222 else if (b->type == bp_static_tracepoint)
1223 error (_("The 'while-stepping' command "
1224 "cannot be used for static tracepoint"));
1225
1226 if (while_stepping)
1227 error (_("The 'while-stepping' command "
1228 "can be used only once"));
1229 else
1230 while_stepping = c;
1231 }
1232
1233 validate_actionline (c->line, b);
1234 }
1235 if (while_stepping)
1236 {
1237 struct command_line *c2;
1238
1239 gdb_assert (while_stepping->body_count == 1);
1240 c2 = while_stepping->body_list[0];
1241 for (; c2; c2 = c2->next)
1242 {
1243 if (c2->control_type == while_stepping_control)
1244 error (_("The 'while-stepping' command cannot be nested"));
1245 }
1246 }
1247 }
1248 else
1249 {
1250 check_no_tracepoint_commands (commands);
1251 }
1252 }
1253
1254 /* Return a vector of all the static tracepoints set at ADDR. The
1255 caller is responsible for releasing the vector. */
1256
1257 VEC(breakpoint_p) *
1258 static_tracepoints_here (CORE_ADDR addr)
1259 {
1260 struct breakpoint *b;
1261 VEC(breakpoint_p) *found = 0;
1262 struct bp_location *loc;
1263
1264 ALL_BREAKPOINTS (b)
1265 if (b->type == bp_static_tracepoint)
1266 {
1267 for (loc = b->loc; loc; loc = loc->next)
1268 if (loc->address == addr)
1269 VEC_safe_push(breakpoint_p, found, b);
1270 }
1271
1272 return found;
1273 }
1274
1275 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1276 validate that only allowed commands are included. */
1277
1278 void
1279 breakpoint_set_commands (struct breakpoint *b,
1280 struct command_line *commands)
1281 {
1282 validate_commands_for_breakpoint (b, commands);
1283
1284 decref_counted_command_line (&b->commands);
1285 b->commands = alloc_counted_command_line (commands);
1286 observer_notify_breakpoint_modified (b);
1287 }
1288
1289 /* Set the internal `silent' flag on the breakpoint. Note that this
1290 is not the same as the "silent" that may appear in the breakpoint's
1291 commands. */
1292
1293 void
1294 breakpoint_set_silent (struct breakpoint *b, int silent)
1295 {
1296 int old_silent = b->silent;
1297
1298 b->silent = silent;
1299 if (old_silent != silent)
1300 observer_notify_breakpoint_modified (b);
1301 }
1302
1303 /* Set the thread for this breakpoint. If THREAD is -1, make the
1304 breakpoint work for any thread. */
1305
1306 void
1307 breakpoint_set_thread (struct breakpoint *b, int thread)
1308 {
1309 int old_thread = b->thread;
1310
1311 b->thread = thread;
1312 if (old_thread != thread)
1313 observer_notify_breakpoint_modified (b);
1314 }
1315
1316 /* Set the task for this breakpoint. If TASK is 0, make the
1317 breakpoint work for any task. */
1318
1319 void
1320 breakpoint_set_task (struct breakpoint *b, int task)
1321 {
1322 int old_task = b->task;
1323
1324 b->task = task;
1325 if (old_task != task)
1326 observer_notify_breakpoint_modified (b);
1327 }
1328
1329 void
1330 check_tracepoint_command (char *line, void *closure)
1331 {
1332 struct breakpoint *b = (struct breakpoint *) closure;
1333
1334 validate_actionline (line, b);
1335 }
1336
1337 /* A structure used to pass information through
1338 map_breakpoint_numbers. */
1339
1340 struct commands_info
1341 {
1342 /* True if the command was typed at a tty. */
1343 int from_tty;
1344
1345 /* The breakpoint range spec. */
1346 char *arg;
1347
1348 /* Non-NULL if the body of the commands are being read from this
1349 already-parsed command. */
1350 struct command_line *control;
1351
1352 /* The command lines read from the user, or NULL if they have not
1353 yet been read. */
1354 struct counted_command_line *cmd;
1355 };
1356
1357 /* A callback for map_breakpoint_numbers that sets the commands for
1358 commands_command. */
1359
1360 static void
1361 do_map_commands_command (struct breakpoint *b, void *data)
1362 {
1363 struct commands_info *info = (struct commands_info *) data;
1364
1365 if (info->cmd == NULL)
1366 {
1367 struct command_line *l;
1368
1369 if (info->control != NULL)
1370 l = copy_command_lines (info->control->body_list[0]);
1371 else
1372 {
1373 struct cleanup *old_chain;
1374 char *str;
1375
1376 str = xstrprintf (_("Type commands for breakpoint(s) "
1377 "%s, one per line."),
1378 info->arg);
1379
1380 old_chain = make_cleanup (xfree, str);
1381
1382 l = read_command_lines (str,
1383 info->from_tty, 1,
1384 (is_tracepoint (b)
1385 ? check_tracepoint_command : 0),
1386 b);
1387
1388 do_cleanups (old_chain);
1389 }
1390
1391 info->cmd = alloc_counted_command_line (l);
1392 }
1393
1394 /* If a breakpoint was on the list more than once, we don't need to
1395 do anything. */
1396 if (b->commands != info->cmd)
1397 {
1398 validate_commands_for_breakpoint (b, info->cmd->commands);
1399 incref_counted_command_line (info->cmd);
1400 decref_counted_command_line (&b->commands);
1401 b->commands = info->cmd;
1402 observer_notify_breakpoint_modified (b);
1403 }
1404 }
1405
1406 static void
1407 commands_command_1 (char *arg, int from_tty,
1408 struct command_line *control)
1409 {
1410 struct cleanup *cleanups;
1411 struct commands_info info;
1412
1413 info.from_tty = from_tty;
1414 info.control = control;
1415 info.cmd = NULL;
1416 /* If we read command lines from the user, then `info' will hold an
1417 extra reference to the commands that we must clean up. */
1418 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1419
1420 if (arg == NULL || !*arg)
1421 {
1422 if (breakpoint_count - prev_breakpoint_count > 1)
1423 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1424 breakpoint_count);
1425 else if (breakpoint_count > 0)
1426 arg = xstrprintf ("%d", breakpoint_count);
1427 else
1428 {
1429 /* So that we don't try to free the incoming non-NULL
1430 argument in the cleanup below. Mapping breakpoint
1431 numbers will fail in this case. */
1432 arg = NULL;
1433 }
1434 }
1435 else
1436 /* The command loop has some static state, so we need to preserve
1437 our argument. */
1438 arg = xstrdup (arg);
1439
1440 if (arg != NULL)
1441 make_cleanup (xfree, arg);
1442
1443 info.arg = arg;
1444
1445 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1446
1447 if (info.cmd == NULL)
1448 error (_("No breakpoints specified."));
1449
1450 do_cleanups (cleanups);
1451 }
1452
1453 static void
1454 commands_command (char *arg, int from_tty)
1455 {
1456 commands_command_1 (arg, from_tty, NULL);
1457 }
1458
1459 /* Like commands_command, but instead of reading the commands from
1460 input stream, takes them from an already parsed command structure.
1461
1462 This is used by cli-script.c to DTRT with breakpoint commands
1463 that are part of if and while bodies. */
1464 enum command_control_type
1465 commands_from_control_command (char *arg, struct command_line *cmd)
1466 {
1467 commands_command_1 (arg, 0, cmd);
1468 return simple_control;
1469 }
1470
1471 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1472
1473 static int
1474 bp_location_has_shadow (struct bp_location *bl)
1475 {
1476 if (bl->loc_type != bp_loc_software_breakpoint)
1477 return 0;
1478 if (!bl->inserted)
1479 return 0;
1480 if (bl->target_info.shadow_len == 0)
1481 /* BL isn't valid, or doesn't shadow memory. */
1482 return 0;
1483 return 1;
1484 }
1485
1486 /* Update BUF, which is LEN bytes read from the target address
1487 MEMADDR, by replacing a memory breakpoint with its shadowed
1488 contents.
1489
1490 If READBUF is not NULL, this buffer must not overlap with the of
1491 the breakpoint location's shadow_contents buffer. Otherwise, a
1492 failed assertion internal error will be raised. */
1493
1494 static void
1495 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1496 const gdb_byte *writebuf_org,
1497 ULONGEST memaddr, LONGEST len,
1498 struct bp_target_info *target_info,
1499 struct gdbarch *gdbarch)
1500 {
1501 /* Now do full processing of the found relevant range of elements. */
1502 CORE_ADDR bp_addr = 0;
1503 int bp_size = 0;
1504 int bptoffset = 0;
1505
1506 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1507 current_program_space->aspace, 0))
1508 {
1509 /* The breakpoint is inserted in a different address space. */
1510 return;
1511 }
1512
1513 /* Addresses and length of the part of the breakpoint that
1514 we need to copy. */
1515 bp_addr = target_info->placed_address;
1516 bp_size = target_info->shadow_len;
1517
1518 if (bp_addr + bp_size <= memaddr)
1519 {
1520 /* The breakpoint is entirely before the chunk of memory we are
1521 reading. */
1522 return;
1523 }
1524
1525 if (bp_addr >= memaddr + len)
1526 {
1527 /* The breakpoint is entirely after the chunk of memory we are
1528 reading. */
1529 return;
1530 }
1531
1532 /* Offset within shadow_contents. */
1533 if (bp_addr < memaddr)
1534 {
1535 /* Only copy the second part of the breakpoint. */
1536 bp_size -= memaddr - bp_addr;
1537 bptoffset = memaddr - bp_addr;
1538 bp_addr = memaddr;
1539 }
1540
1541 if (bp_addr + bp_size > memaddr + len)
1542 {
1543 /* Only copy the first part of the breakpoint. */
1544 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1545 }
1546
1547 if (readbuf != NULL)
1548 {
1549 /* Verify that the readbuf buffer does not overlap with the
1550 shadow_contents buffer. */
1551 gdb_assert (target_info->shadow_contents >= readbuf + len
1552 || readbuf >= (target_info->shadow_contents
1553 + target_info->shadow_len));
1554
1555 /* Update the read buffer with this inserted breakpoint's
1556 shadow. */
1557 memcpy (readbuf + bp_addr - memaddr,
1558 target_info->shadow_contents + bptoffset, bp_size);
1559 }
1560 else
1561 {
1562 const unsigned char *bp;
1563 CORE_ADDR addr = target_info->reqstd_address;
1564 int placed_size;
1565
1566 /* Update the shadow with what we want to write to memory. */
1567 memcpy (target_info->shadow_contents + bptoffset,
1568 writebuf_org + bp_addr - memaddr, bp_size);
1569
1570 /* Determine appropriate breakpoint contents and size for this
1571 address. */
1572 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1573
1574 /* Update the final write buffer with this inserted
1575 breakpoint's INSN. */
1576 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1577 }
1578 }
1579
1580 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1581 by replacing any memory breakpoints with their shadowed contents.
1582
1583 If READBUF is not NULL, this buffer must not overlap with any of
1584 the breakpoint location's shadow_contents buffers. Otherwise,
1585 a failed assertion internal error will be raised.
1586
1587 The range of shadowed area by each bp_location is:
1588 bl->address - bp_location_placed_address_before_address_max
1589 up to bl->address + bp_location_shadow_len_after_address_max
1590 The range we were requested to resolve shadows for is:
1591 memaddr ... memaddr + len
1592 Thus the safe cutoff boundaries for performance optimization are
1593 memaddr + len <= (bl->address
1594 - bp_location_placed_address_before_address_max)
1595 and:
1596 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1597
1598 void
1599 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1600 const gdb_byte *writebuf_org,
1601 ULONGEST memaddr, LONGEST len)
1602 {
1603 /* Left boundary, right boundary and median element of our binary
1604 search. */
1605 unsigned bc_l, bc_r, bc;
1606 size_t i;
1607
1608 /* Find BC_L which is a leftmost element which may affect BUF
1609 content. It is safe to report lower value but a failure to
1610 report higher one. */
1611
1612 bc_l = 0;
1613 bc_r = bp_location_count;
1614 while (bc_l + 1 < bc_r)
1615 {
1616 struct bp_location *bl;
1617
1618 bc = (bc_l + bc_r) / 2;
1619 bl = bp_location[bc];
1620
1621 /* Check first BL->ADDRESS will not overflow due to the added
1622 constant. Then advance the left boundary only if we are sure
1623 the BC element can in no way affect the BUF content (MEMADDR
1624 to MEMADDR + LEN range).
1625
1626 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1627 offset so that we cannot miss a breakpoint with its shadow
1628 range tail still reaching MEMADDR. */
1629
1630 if ((bl->address + bp_location_shadow_len_after_address_max
1631 >= bl->address)
1632 && (bl->address + bp_location_shadow_len_after_address_max
1633 <= memaddr))
1634 bc_l = bc;
1635 else
1636 bc_r = bc;
1637 }
1638
1639 /* Due to the binary search above, we need to make sure we pick the
1640 first location that's at BC_L's address. E.g., if there are
1641 multiple locations at the same address, BC_L may end up pointing
1642 at a duplicate location, and miss the "master"/"inserted"
1643 location. Say, given locations L1, L2 and L3 at addresses A and
1644 B:
1645
1646 L1@A, L2@A, L3@B, ...
1647
1648 BC_L could end up pointing at location L2, while the "master"
1649 location could be L1. Since the `loc->inserted' flag is only set
1650 on "master" locations, we'd forget to restore the shadow of L1
1651 and L2. */
1652 while (bc_l > 0
1653 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1654 bc_l--;
1655
1656 /* Now do full processing of the found relevant range of elements. */
1657
1658 for (bc = bc_l; bc < bp_location_count; bc++)
1659 {
1660 struct bp_location *bl = bp_location[bc];
1661 CORE_ADDR bp_addr = 0;
1662 int bp_size = 0;
1663 int bptoffset = 0;
1664
1665 /* bp_location array has BL->OWNER always non-NULL. */
1666 if (bl->owner->type == bp_none)
1667 warning (_("reading through apparently deleted breakpoint #%d?"),
1668 bl->owner->number);
1669
1670 /* Performance optimization: any further element can no longer affect BUF
1671 content. */
1672
1673 if (bl->address >= bp_location_placed_address_before_address_max
1674 && memaddr + len <= (bl->address
1675 - bp_location_placed_address_before_address_max))
1676 break;
1677
1678 if (!bp_location_has_shadow (bl))
1679 continue;
1680
1681 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1682 memaddr, len, &bl->target_info, bl->gdbarch);
1683 }
1684 }
1685
1686 \f
1687
1688 /* Return true if BPT is either a software breakpoint or a hardware
1689 breakpoint. */
1690
1691 int
1692 is_breakpoint (const struct breakpoint *bpt)
1693 {
1694 return (bpt->type == bp_breakpoint
1695 || bpt->type == bp_hardware_breakpoint
1696 || bpt->type == bp_dprintf);
1697 }
1698
1699 /* Return true if BPT is of any hardware watchpoint kind. */
1700
1701 static int
1702 is_hardware_watchpoint (const struct breakpoint *bpt)
1703 {
1704 return (bpt->type == bp_hardware_watchpoint
1705 || bpt->type == bp_read_watchpoint
1706 || bpt->type == bp_access_watchpoint);
1707 }
1708
1709 /* Return true if BPT is of any watchpoint kind, hardware or
1710 software. */
1711
1712 int
1713 is_watchpoint (const struct breakpoint *bpt)
1714 {
1715 return (is_hardware_watchpoint (bpt)
1716 || bpt->type == bp_watchpoint);
1717 }
1718
1719 /* Returns true if the current thread and its running state are safe
1720 to evaluate or update watchpoint B. Watchpoints on local
1721 expressions need to be evaluated in the context of the thread that
1722 was current when the watchpoint was created, and, that thread needs
1723 to be stopped to be able to select the correct frame context.
1724 Watchpoints on global expressions can be evaluated on any thread,
1725 and in any state. It is presently left to the target allowing
1726 memory accesses when threads are running. */
1727
1728 static int
1729 watchpoint_in_thread_scope (struct watchpoint *b)
1730 {
1731 return (b->base.pspace == current_program_space
1732 && (ptid_equal (b->watchpoint_thread, null_ptid)
1733 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1734 && !is_executing (inferior_ptid))));
1735 }
1736
1737 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1738 associated bp_watchpoint_scope breakpoint. */
1739
1740 static void
1741 watchpoint_del_at_next_stop (struct watchpoint *w)
1742 {
1743 struct breakpoint *b = &w->base;
1744
1745 if (b->related_breakpoint != b)
1746 {
1747 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1748 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1749 b->related_breakpoint->disposition = disp_del_at_next_stop;
1750 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1751 b->related_breakpoint = b;
1752 }
1753 b->disposition = disp_del_at_next_stop;
1754 }
1755
1756 /* Extract a bitfield value from value VAL using the bit parameters contained in
1757 watchpoint W. */
1758
1759 static struct value *
1760 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1761 {
1762 struct value *bit_val;
1763
1764 if (val == NULL)
1765 return NULL;
1766
1767 bit_val = allocate_value (value_type (val));
1768
1769 unpack_value_bitfield (bit_val,
1770 w->val_bitpos,
1771 w->val_bitsize,
1772 value_contents_for_printing (val),
1773 value_offset (val),
1774 val);
1775
1776 return bit_val;
1777 }
1778
1779 /* Allocate a dummy location and add it to B, which must be a software
1780 watchpoint. This is required because even if a software watchpoint
1781 is not watching any memory, bpstat_stop_status requires a location
1782 to be able to report stops. */
1783
1784 static void
1785 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1786 struct program_space *pspace)
1787 {
1788 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1789
1790 b->loc = allocate_bp_location (b);
1791 b->loc->pspace = pspace;
1792 b->loc->address = -1;
1793 b->loc->length = -1;
1794 }
1795
1796 /* Returns true if B is a software watchpoint that is not watching any
1797 memory (e.g., "watch $pc"). */
1798
1799 static int
1800 is_no_memory_software_watchpoint (struct breakpoint *b)
1801 {
1802 return (b->type == bp_watchpoint
1803 && b->loc != NULL
1804 && b->loc->next == NULL
1805 && b->loc->address == -1
1806 && b->loc->length == -1);
1807 }
1808
1809 /* Assuming that B is a watchpoint:
1810 - Reparse watchpoint expression, if REPARSE is non-zero
1811 - Evaluate expression and store the result in B->val
1812 - Evaluate the condition if there is one, and store the result
1813 in b->loc->cond.
1814 - Update the list of values that must be watched in B->loc.
1815
1816 If the watchpoint disposition is disp_del_at_next_stop, then do
1817 nothing. If this is local watchpoint that is out of scope, delete
1818 it.
1819
1820 Even with `set breakpoint always-inserted on' the watchpoints are
1821 removed + inserted on each stop here. Normal breakpoints must
1822 never be removed because they might be missed by a running thread
1823 when debugging in non-stop mode. On the other hand, hardware
1824 watchpoints (is_hardware_watchpoint; processed here) are specific
1825 to each LWP since they are stored in each LWP's hardware debug
1826 registers. Therefore, such LWP must be stopped first in order to
1827 be able to modify its hardware watchpoints.
1828
1829 Hardware watchpoints must be reset exactly once after being
1830 presented to the user. It cannot be done sooner, because it would
1831 reset the data used to present the watchpoint hit to the user. And
1832 it must not be done later because it could display the same single
1833 watchpoint hit during multiple GDB stops. Note that the latter is
1834 relevant only to the hardware watchpoint types bp_read_watchpoint
1835 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1836 not user-visible - its hit is suppressed if the memory content has
1837 not changed.
1838
1839 The following constraints influence the location where we can reset
1840 hardware watchpoints:
1841
1842 * target_stopped_by_watchpoint and target_stopped_data_address are
1843 called several times when GDB stops.
1844
1845 [linux]
1846 * Multiple hardware watchpoints can be hit at the same time,
1847 causing GDB to stop. GDB only presents one hardware watchpoint
1848 hit at a time as the reason for stopping, and all the other hits
1849 are presented later, one after the other, each time the user
1850 requests the execution to be resumed. Execution is not resumed
1851 for the threads still having pending hit event stored in
1852 LWP_INFO->STATUS. While the watchpoint is already removed from
1853 the inferior on the first stop the thread hit event is kept being
1854 reported from its cached value by linux_nat_stopped_data_address
1855 until the real thread resume happens after the watchpoint gets
1856 presented and thus its LWP_INFO->STATUS gets reset.
1857
1858 Therefore the hardware watchpoint hit can get safely reset on the
1859 watchpoint removal from inferior. */
1860
1861 static void
1862 update_watchpoint (struct watchpoint *b, int reparse)
1863 {
1864 int within_current_scope;
1865 struct frame_id saved_frame_id;
1866 int frame_saved;
1867
1868 /* If this is a local watchpoint, we only want to check if the
1869 watchpoint frame is in scope if the current thread is the thread
1870 that was used to create the watchpoint. */
1871 if (!watchpoint_in_thread_scope (b))
1872 return;
1873
1874 if (b->base.disposition == disp_del_at_next_stop)
1875 return;
1876
1877 frame_saved = 0;
1878
1879 /* Determine if the watchpoint is within scope. */
1880 if (b->exp_valid_block == NULL)
1881 within_current_scope = 1;
1882 else
1883 {
1884 struct frame_info *fi = get_current_frame ();
1885 struct gdbarch *frame_arch = get_frame_arch (fi);
1886 CORE_ADDR frame_pc = get_frame_pc (fi);
1887
1888 /* If we're at a point where the stack has been destroyed
1889 (e.g. in a function epilogue), unwinding may not work
1890 properly. Do not attempt to recreate locations at this
1891 point. See similar comments in watchpoint_check. */
1892 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1893 return;
1894
1895 /* Save the current frame's ID so we can restore it after
1896 evaluating the watchpoint expression on its own frame. */
1897 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1898 took a frame parameter, so that we didn't have to change the
1899 selected frame. */
1900 frame_saved = 1;
1901 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1902
1903 fi = frame_find_by_id (b->watchpoint_frame);
1904 within_current_scope = (fi != NULL);
1905 if (within_current_scope)
1906 select_frame (fi);
1907 }
1908
1909 /* We don't free locations. They are stored in the bp_location array
1910 and update_global_location_list will eventually delete them and
1911 remove breakpoints if needed. */
1912 b->base.loc = NULL;
1913
1914 if (within_current_scope && reparse)
1915 {
1916 const char *s;
1917
1918 if (b->exp)
1919 {
1920 xfree (b->exp);
1921 b->exp = NULL;
1922 }
1923 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1924 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1925 /* If the meaning of expression itself changed, the old value is
1926 no longer relevant. We don't want to report a watchpoint hit
1927 to the user when the old value and the new value may actually
1928 be completely different objects. */
1929 value_free (b->val);
1930 b->val = NULL;
1931 b->val_valid = 0;
1932
1933 /* Note that unlike with breakpoints, the watchpoint's condition
1934 expression is stored in the breakpoint object, not in the
1935 locations (re)created below. */
1936 if (b->base.cond_string != NULL)
1937 {
1938 if (b->cond_exp != NULL)
1939 {
1940 xfree (b->cond_exp);
1941 b->cond_exp = NULL;
1942 }
1943
1944 s = b->base.cond_string;
1945 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1946 }
1947 }
1948
1949 /* If we failed to parse the expression, for example because
1950 it refers to a global variable in a not-yet-loaded shared library,
1951 don't try to insert watchpoint. We don't automatically delete
1952 such watchpoint, though, since failure to parse expression
1953 is different from out-of-scope watchpoint. */
1954 if (!target_has_execution)
1955 {
1956 /* Without execution, memory can't change. No use to try and
1957 set watchpoint locations. The watchpoint will be reset when
1958 the target gains execution, through breakpoint_re_set. */
1959 if (!can_use_hw_watchpoints)
1960 {
1961 if (b->base.ops->works_in_software_mode (&b->base))
1962 b->base.type = bp_watchpoint;
1963 else
1964 error (_("Can't set read/access watchpoint when "
1965 "hardware watchpoints are disabled."));
1966 }
1967 }
1968 else if (within_current_scope && b->exp)
1969 {
1970 int pc = 0;
1971 struct value *val_chain, *v, *result, *next;
1972 struct program_space *frame_pspace;
1973
1974 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1975
1976 /* Avoid setting b->val if it's already set. The meaning of
1977 b->val is 'the last value' user saw, and we should update
1978 it only if we reported that last value to user. As it
1979 happens, the code that reports it updates b->val directly.
1980 We don't keep track of the memory value for masked
1981 watchpoints. */
1982 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1983 {
1984 if (b->val_bitsize != 0)
1985 {
1986 v = extract_bitfield_from_watchpoint_value (b, v);
1987 if (v != NULL)
1988 release_value (v);
1989 }
1990 b->val = v;
1991 b->val_valid = 1;
1992 }
1993
1994 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1995
1996 /* Look at each value on the value chain. */
1997 for (v = val_chain; v; v = value_next (v))
1998 {
1999 /* If it's a memory location, and GDB actually needed
2000 its contents to evaluate the expression, then we
2001 must watch it. If the first value returned is
2002 still lazy, that means an error occurred reading it;
2003 watch it anyway in case it becomes readable. */
2004 if (VALUE_LVAL (v) == lval_memory
2005 && (v == val_chain || ! value_lazy (v)))
2006 {
2007 struct type *vtype = check_typedef (value_type (v));
2008
2009 /* We only watch structs and arrays if user asked
2010 for it explicitly, never if they just happen to
2011 appear in the middle of some value chain. */
2012 if (v == result
2013 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2014 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2015 {
2016 CORE_ADDR addr;
2017 enum target_hw_bp_type type;
2018 struct bp_location *loc, **tmp;
2019 int bitpos = 0, bitsize = 0;
2020
2021 if (value_bitsize (v) != 0)
2022 {
2023 /* Extract the bit parameters out from the bitfield
2024 sub-expression. */
2025 bitpos = value_bitpos (v);
2026 bitsize = value_bitsize (v);
2027 }
2028 else if (v == result && b->val_bitsize != 0)
2029 {
2030 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2031 lvalue whose bit parameters are saved in the fields
2032 VAL_BITPOS and VAL_BITSIZE. */
2033 bitpos = b->val_bitpos;
2034 bitsize = b->val_bitsize;
2035 }
2036
2037 addr = value_address (v);
2038 if (bitsize != 0)
2039 {
2040 /* Skip the bytes that don't contain the bitfield. */
2041 addr += bitpos / 8;
2042 }
2043
2044 type = hw_write;
2045 if (b->base.type == bp_read_watchpoint)
2046 type = hw_read;
2047 else if (b->base.type == bp_access_watchpoint)
2048 type = hw_access;
2049
2050 loc = allocate_bp_location (&b->base);
2051 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2052 ;
2053 *tmp = loc;
2054 loc->gdbarch = get_type_arch (value_type (v));
2055
2056 loc->pspace = frame_pspace;
2057 loc->address = addr;
2058
2059 if (bitsize != 0)
2060 {
2061 /* Just cover the bytes that make up the bitfield. */
2062 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2063 }
2064 else
2065 loc->length = TYPE_LENGTH (value_type (v));
2066
2067 loc->watchpoint_type = type;
2068 }
2069 }
2070 }
2071
2072 /* Change the type of breakpoint between hardware assisted or
2073 an ordinary watchpoint depending on the hardware support
2074 and free hardware slots. REPARSE is set when the inferior
2075 is started. */
2076 if (reparse)
2077 {
2078 int reg_cnt;
2079 enum bp_loc_type loc_type;
2080 struct bp_location *bl;
2081
2082 reg_cnt = can_use_hardware_watchpoint (val_chain);
2083
2084 if (reg_cnt)
2085 {
2086 int i, target_resources_ok, other_type_used;
2087 enum bptype type;
2088
2089 /* Use an exact watchpoint when there's only one memory region to be
2090 watched, and only one debug register is needed to watch it. */
2091 b->exact = target_exact_watchpoints && reg_cnt == 1;
2092
2093 /* We need to determine how many resources are already
2094 used for all other hardware watchpoints plus this one
2095 to see if we still have enough resources to also fit
2096 this watchpoint in as well. */
2097
2098 /* If this is a software watchpoint, we try to turn it
2099 to a hardware one -- count resources as if B was of
2100 hardware watchpoint type. */
2101 type = b->base.type;
2102 if (type == bp_watchpoint)
2103 type = bp_hardware_watchpoint;
2104
2105 /* This watchpoint may or may not have been placed on
2106 the list yet at this point (it won't be in the list
2107 if we're trying to create it for the first time,
2108 through watch_command), so always account for it
2109 manually. */
2110
2111 /* Count resources used by all watchpoints except B. */
2112 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2113
2114 /* Add in the resources needed for B. */
2115 i += hw_watchpoint_use_count (&b->base);
2116
2117 target_resources_ok
2118 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2119 if (target_resources_ok <= 0)
2120 {
2121 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2122
2123 if (target_resources_ok == 0 && !sw_mode)
2124 error (_("Target does not support this type of "
2125 "hardware watchpoint."));
2126 else if (target_resources_ok < 0 && !sw_mode)
2127 error (_("There are not enough available hardware "
2128 "resources for this watchpoint."));
2129
2130 /* Downgrade to software watchpoint. */
2131 b->base.type = bp_watchpoint;
2132 }
2133 else
2134 {
2135 /* If this was a software watchpoint, we've just
2136 found we have enough resources to turn it to a
2137 hardware watchpoint. Otherwise, this is a
2138 nop. */
2139 b->base.type = type;
2140 }
2141 }
2142 else if (!b->base.ops->works_in_software_mode (&b->base))
2143 {
2144 if (!can_use_hw_watchpoints)
2145 error (_("Can't set read/access watchpoint when "
2146 "hardware watchpoints are disabled."));
2147 else
2148 error (_("Expression cannot be implemented with "
2149 "read/access watchpoint."));
2150 }
2151 else
2152 b->base.type = bp_watchpoint;
2153
2154 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2155 : bp_loc_hardware_watchpoint);
2156 for (bl = b->base.loc; bl; bl = bl->next)
2157 bl->loc_type = loc_type;
2158 }
2159
2160 for (v = val_chain; v; v = next)
2161 {
2162 next = value_next (v);
2163 if (v != b->val)
2164 value_free (v);
2165 }
2166
2167 /* If a software watchpoint is not watching any memory, then the
2168 above left it without any location set up. But,
2169 bpstat_stop_status requires a location to be able to report
2170 stops, so make sure there's at least a dummy one. */
2171 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2172 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
2173 }
2174 else if (!within_current_scope)
2175 {
2176 printf_filtered (_("\
2177 Watchpoint %d deleted because the program has left the block\n\
2178 in which its expression is valid.\n"),
2179 b->base.number);
2180 watchpoint_del_at_next_stop (b);
2181 }
2182
2183 /* Restore the selected frame. */
2184 if (frame_saved)
2185 select_frame (frame_find_by_id (saved_frame_id));
2186 }
2187
2188
2189 /* Returns 1 iff breakpoint location should be
2190 inserted in the inferior. We don't differentiate the type of BL's owner
2191 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2192 breakpoint_ops is not defined, because in insert_bp_location,
2193 tracepoint's insert_location will not be called. */
2194 static int
2195 should_be_inserted (struct bp_location *bl)
2196 {
2197 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2198 return 0;
2199
2200 if (bl->owner->disposition == disp_del_at_next_stop)
2201 return 0;
2202
2203 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2204 return 0;
2205
2206 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2207 return 0;
2208
2209 /* This is set for example, when we're attached to the parent of a
2210 vfork, and have detached from the child. The child is running
2211 free, and we expect it to do an exec or exit, at which point the
2212 OS makes the parent schedulable again (and the target reports
2213 that the vfork is done). Until the child is done with the shared
2214 memory region, do not insert breakpoints in the parent, otherwise
2215 the child could still trip on the parent's breakpoints. Since
2216 the parent is blocked anyway, it won't miss any breakpoint. */
2217 if (bl->pspace->breakpoints_not_allowed)
2218 return 0;
2219
2220 /* Don't insert a breakpoint if we're trying to step past its
2221 location. */
2222 if ((bl->loc_type == bp_loc_software_breakpoint
2223 || bl->loc_type == bp_loc_hardware_breakpoint)
2224 && stepping_past_instruction_at (bl->pspace->aspace,
2225 bl->address))
2226 {
2227 if (debug_infrun)
2228 {
2229 fprintf_unfiltered (gdb_stdlog,
2230 "infrun: skipping breakpoint: "
2231 "stepping past insn at: %s\n",
2232 paddress (bl->gdbarch, bl->address));
2233 }
2234 return 0;
2235 }
2236
2237 /* Don't insert watchpoints if we're trying to step past the
2238 instruction that triggered one. */
2239 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2240 && stepping_past_nonsteppable_watchpoint ())
2241 {
2242 if (debug_infrun)
2243 {
2244 fprintf_unfiltered (gdb_stdlog,
2245 "infrun: stepping past non-steppable watchpoint. "
2246 "skipping watchpoint at %s:%d\n",
2247 paddress (bl->gdbarch, bl->address),
2248 bl->length);
2249 }
2250 return 0;
2251 }
2252
2253 return 1;
2254 }
2255
2256 /* Same as should_be_inserted but does the check assuming
2257 that the location is not duplicated. */
2258
2259 static int
2260 unduplicated_should_be_inserted (struct bp_location *bl)
2261 {
2262 int result;
2263 const int save_duplicate = bl->duplicate;
2264
2265 bl->duplicate = 0;
2266 result = should_be_inserted (bl);
2267 bl->duplicate = save_duplicate;
2268 return result;
2269 }
2270
2271 /* Parses a conditional described by an expression COND into an
2272 agent expression bytecode suitable for evaluation
2273 by the bytecode interpreter. Return NULL if there was
2274 any error during parsing. */
2275
2276 static struct agent_expr *
2277 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2278 {
2279 struct agent_expr *aexpr = NULL;
2280
2281 if (!cond)
2282 return NULL;
2283
2284 /* We don't want to stop processing, so catch any errors
2285 that may show up. */
2286 TRY
2287 {
2288 aexpr = gen_eval_for_expr (scope, cond);
2289 }
2290
2291 CATCH (ex, RETURN_MASK_ERROR)
2292 {
2293 /* If we got here, it means the condition could not be parsed to a valid
2294 bytecode expression and thus can't be evaluated on the target's side.
2295 It's no use iterating through the conditions. */
2296 return NULL;
2297 }
2298 END_CATCH
2299
2300 /* We have a valid agent expression. */
2301 return aexpr;
2302 }
2303
2304 /* Based on location BL, create a list of breakpoint conditions to be
2305 passed on to the target. If we have duplicated locations with different
2306 conditions, we will add such conditions to the list. The idea is that the
2307 target will evaluate the list of conditions and will only notify GDB when
2308 one of them is true. */
2309
2310 static void
2311 build_target_condition_list (struct bp_location *bl)
2312 {
2313 struct bp_location **locp = NULL, **loc2p;
2314 int null_condition_or_parse_error = 0;
2315 int modified = bl->needs_update;
2316 struct bp_location *loc;
2317
2318 /* Release conditions left over from a previous insert. */
2319 VEC_free (agent_expr_p, bl->target_info.conditions);
2320
2321 /* This is only meaningful if the target is
2322 evaluating conditions and if the user has
2323 opted for condition evaluation on the target's
2324 side. */
2325 if (gdb_evaluates_breakpoint_condition_p ()
2326 || !target_supports_evaluation_of_breakpoint_conditions ())
2327 return;
2328
2329 /* Do a first pass to check for locations with no assigned
2330 conditions or conditions that fail to parse to a valid agent expression
2331 bytecode. If any of these happen, then it's no use to send conditions
2332 to the target since this location will always trigger and generate a
2333 response back to GDB. */
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 if (modified)
2340 {
2341 struct agent_expr *aexpr;
2342
2343 /* Re-parse the conditions since something changed. In that
2344 case we already freed the condition bytecodes (see
2345 force_breakpoint_reinsertion). We just
2346 need to parse the condition to bytecodes again. */
2347 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2348 loc->cond_bytecode = aexpr;
2349
2350 /* Check if we managed to parse the conditional expression
2351 correctly. If not, we will not send this condition
2352 to the target. */
2353 if (aexpr)
2354 continue;
2355 }
2356
2357 /* If we have a NULL bytecode expression, it means something
2358 went wrong or we have a null condition expression. */
2359 if (!loc->cond_bytecode)
2360 {
2361 null_condition_or_parse_error = 1;
2362 break;
2363 }
2364 }
2365 }
2366
2367 /* If any of these happened, it means we will have to evaluate the conditions
2368 for the location's address on gdb's side. It is no use keeping bytecodes
2369 for all the other duplicate locations, thus we free all of them here.
2370
2371 This is so we have a finer control over which locations' conditions are
2372 being evaluated by GDB or the remote stub. */
2373 if (null_condition_or_parse_error)
2374 {
2375 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2376 {
2377 loc = (*loc2p);
2378 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2379 {
2380 /* Only go as far as the first NULL bytecode is
2381 located. */
2382 if (!loc->cond_bytecode)
2383 return;
2384
2385 free_agent_expr (loc->cond_bytecode);
2386 loc->cond_bytecode = NULL;
2387 }
2388 }
2389 }
2390
2391 /* No NULL conditions or failed bytecode generation. Build a condition list
2392 for this location's address. */
2393 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2394 {
2395 loc = (*loc2p);
2396 if (loc->cond
2397 && is_breakpoint (loc->owner)
2398 && loc->pspace->num == bl->pspace->num
2399 && loc->owner->enable_state == bp_enabled
2400 && loc->enabled)
2401 /* Add the condition to the vector. This will be used later to send the
2402 conditions to the target. */
2403 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2404 loc->cond_bytecode);
2405 }
2406
2407 return;
2408 }
2409
2410 /* Parses a command described by string CMD into an agent expression
2411 bytecode suitable for evaluation by the bytecode interpreter.
2412 Return NULL if there was any error during parsing. */
2413
2414 static struct agent_expr *
2415 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2416 {
2417 struct cleanup *old_cleanups = 0;
2418 struct expression *expr, **argvec;
2419 struct agent_expr *aexpr = NULL;
2420 const char *cmdrest;
2421 const char *format_start, *format_end;
2422 struct format_piece *fpieces;
2423 int nargs;
2424 struct gdbarch *gdbarch = get_current_arch ();
2425
2426 if (!cmd)
2427 return NULL;
2428
2429 cmdrest = cmd;
2430
2431 if (*cmdrest == ',')
2432 ++cmdrest;
2433 cmdrest = skip_spaces_const (cmdrest);
2434
2435 if (*cmdrest++ != '"')
2436 error (_("No format string following the location"));
2437
2438 format_start = cmdrest;
2439
2440 fpieces = parse_format_string (&cmdrest);
2441
2442 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2443
2444 format_end = cmdrest;
2445
2446 if (*cmdrest++ != '"')
2447 error (_("Bad format string, non-terminated '\"'."));
2448
2449 cmdrest = skip_spaces_const (cmdrest);
2450
2451 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2452 error (_("Invalid argument syntax"));
2453
2454 if (*cmdrest == ',')
2455 cmdrest++;
2456 cmdrest = skip_spaces_const (cmdrest);
2457
2458 /* For each argument, make an expression. */
2459
2460 argvec = (struct expression **) alloca (strlen (cmd)
2461 * sizeof (struct expression *));
2462
2463 nargs = 0;
2464 while (*cmdrest != '\0')
2465 {
2466 const char *cmd1;
2467
2468 cmd1 = cmdrest;
2469 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2470 argvec[nargs++] = expr;
2471 cmdrest = cmd1;
2472 if (*cmdrest == ',')
2473 ++cmdrest;
2474 }
2475
2476 /* We don't want to stop processing, so catch any errors
2477 that may show up. */
2478 TRY
2479 {
2480 aexpr = gen_printf (scope, gdbarch, 0, 0,
2481 format_start, format_end - format_start,
2482 fpieces, nargs, argvec);
2483 }
2484 CATCH (ex, RETURN_MASK_ERROR)
2485 {
2486 /* If we got here, it means the command could not be parsed to a valid
2487 bytecode expression and thus can't be evaluated on the target's side.
2488 It's no use iterating through the other commands. */
2489 aexpr = NULL;
2490 }
2491 END_CATCH
2492
2493 do_cleanups (old_cleanups);
2494
2495 /* We have a valid agent expression, return it. */
2496 return aexpr;
2497 }
2498
2499 /* Based on location BL, create a list of breakpoint commands to be
2500 passed on to the target. If we have duplicated locations with
2501 different commands, we will add any such to the list. */
2502
2503 static void
2504 build_target_command_list (struct bp_location *bl)
2505 {
2506 struct bp_location **locp = NULL, **loc2p;
2507 int null_command_or_parse_error = 0;
2508 int modified = bl->needs_update;
2509 struct bp_location *loc;
2510
2511 /* Release commands left over from a previous insert. */
2512 VEC_free (agent_expr_p, bl->target_info.tcommands);
2513
2514 if (!target_can_run_breakpoint_commands ())
2515 return;
2516
2517 /* For now, limit to agent-style dprintf breakpoints. */
2518 if (dprintf_style != dprintf_style_agent)
2519 return;
2520
2521 /* For now, if we have any duplicate location that isn't a dprintf,
2522 don't install the target-side commands, as that would make the
2523 breakpoint not be reported to the core, and we'd lose
2524 control. */
2525 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2526 {
2527 loc = (*loc2p);
2528 if (is_breakpoint (loc->owner)
2529 && loc->pspace->num == bl->pspace->num
2530 && loc->owner->type != bp_dprintf)
2531 return;
2532 }
2533
2534 /* Do a first pass to check for locations with no assigned
2535 conditions or conditions that fail to parse to a valid agent expression
2536 bytecode. If any of these happen, then it's no use to send conditions
2537 to the target since this location will always trigger and generate a
2538 response back to GDB. */
2539 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2540 {
2541 loc = (*loc2p);
2542 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2543 {
2544 if (modified)
2545 {
2546 struct agent_expr *aexpr;
2547
2548 /* Re-parse the commands since something changed. In that
2549 case we already freed the command bytecodes (see
2550 force_breakpoint_reinsertion). We just
2551 need to parse the command to bytecodes again. */
2552 aexpr = parse_cmd_to_aexpr (bl->address,
2553 loc->owner->extra_string);
2554 loc->cmd_bytecode = aexpr;
2555
2556 if (!aexpr)
2557 continue;
2558 }
2559
2560 /* If we have a NULL bytecode expression, it means something
2561 went wrong or we have a null command expression. */
2562 if (!loc->cmd_bytecode)
2563 {
2564 null_command_or_parse_error = 1;
2565 break;
2566 }
2567 }
2568 }
2569
2570 /* If anything failed, then we're not doing target-side commands,
2571 and so clean up. */
2572 if (null_command_or_parse_error)
2573 {
2574 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2575 {
2576 loc = (*loc2p);
2577 if (is_breakpoint (loc->owner)
2578 && loc->pspace->num == bl->pspace->num)
2579 {
2580 /* Only go as far as the first NULL bytecode is
2581 located. */
2582 if (loc->cmd_bytecode == NULL)
2583 return;
2584
2585 free_agent_expr (loc->cmd_bytecode);
2586 loc->cmd_bytecode = NULL;
2587 }
2588 }
2589 }
2590
2591 /* No NULL commands or failed bytecode generation. Build a command list
2592 for this location's address. */
2593 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2594 {
2595 loc = (*loc2p);
2596 if (loc->owner->extra_string
2597 && is_breakpoint (loc->owner)
2598 && loc->pspace->num == bl->pspace->num
2599 && loc->owner->enable_state == bp_enabled
2600 && loc->enabled)
2601 /* Add the command to the vector. This will be used later
2602 to send the commands to the target. */
2603 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2604 loc->cmd_bytecode);
2605 }
2606
2607 bl->target_info.persist = 0;
2608 /* Maybe flag this location as persistent. */
2609 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2610 bl->target_info.persist = 1;
2611 }
2612
2613 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2614 location. Any error messages are printed to TMP_ERROR_STREAM; and
2615 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2616 Returns 0 for success, 1 if the bp_location type is not supported or
2617 -1 for failure.
2618
2619 NOTE drow/2003-09-09: This routine could be broken down to an
2620 object-style method for each breakpoint or catchpoint type. */
2621 static int
2622 insert_bp_location (struct bp_location *bl,
2623 struct ui_file *tmp_error_stream,
2624 int *disabled_breaks,
2625 int *hw_breakpoint_error,
2626 int *hw_bp_error_explained_already)
2627 {
2628 enum errors bp_err = GDB_NO_ERROR;
2629 const char *bp_err_message = NULL;
2630
2631 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2632 return 0;
2633
2634 /* Note we don't initialize bl->target_info, as that wipes out
2635 the breakpoint location's shadow_contents if the breakpoint
2636 is still inserted at that location. This in turn breaks
2637 target_read_memory which depends on these buffers when
2638 a memory read is requested at the breakpoint location:
2639 Once the target_info has been wiped, we fail to see that
2640 we have a breakpoint inserted at that address and thus
2641 read the breakpoint instead of returning the data saved in
2642 the breakpoint location's shadow contents. */
2643 bl->target_info.reqstd_address = bl->address;
2644 bl->target_info.placed_address_space = bl->pspace->aspace;
2645 bl->target_info.length = bl->length;
2646
2647 /* When working with target-side conditions, we must pass all the conditions
2648 for the same breakpoint address down to the target since GDB will not
2649 insert those locations. With a list of breakpoint conditions, the target
2650 can decide when to stop and notify GDB. */
2651
2652 if (is_breakpoint (bl->owner))
2653 {
2654 build_target_condition_list (bl);
2655 build_target_command_list (bl);
2656 /* Reset the modification marker. */
2657 bl->needs_update = 0;
2658 }
2659
2660 if (bl->loc_type == bp_loc_software_breakpoint
2661 || bl->loc_type == bp_loc_hardware_breakpoint)
2662 {
2663 if (bl->owner->type != bp_hardware_breakpoint)
2664 {
2665 /* If the explicitly specified breakpoint type
2666 is not hardware breakpoint, check the memory map to see
2667 if the breakpoint address is in read only memory or not.
2668
2669 Two important cases are:
2670 - location type is not hardware breakpoint, memory
2671 is readonly. We change the type of the location to
2672 hardware breakpoint.
2673 - location type is hardware breakpoint, memory is
2674 read-write. This means we've previously made the
2675 location hardware one, but then the memory map changed,
2676 so we undo.
2677
2678 When breakpoints are removed, remove_breakpoints will use
2679 location types we've just set here, the only possible
2680 problem is that memory map has changed during running
2681 program, but it's not going to work anyway with current
2682 gdb. */
2683 struct mem_region *mr
2684 = lookup_mem_region (bl->target_info.reqstd_address);
2685
2686 if (mr)
2687 {
2688 if (automatic_hardware_breakpoints)
2689 {
2690 enum bp_loc_type new_type;
2691
2692 if (mr->attrib.mode != MEM_RW)
2693 new_type = bp_loc_hardware_breakpoint;
2694 else
2695 new_type = bp_loc_software_breakpoint;
2696
2697 if (new_type != bl->loc_type)
2698 {
2699 static int said = 0;
2700
2701 bl->loc_type = new_type;
2702 if (!said)
2703 {
2704 fprintf_filtered (gdb_stdout,
2705 _("Note: automatically using "
2706 "hardware breakpoints for "
2707 "read-only addresses.\n"));
2708 said = 1;
2709 }
2710 }
2711 }
2712 else if (bl->loc_type == bp_loc_software_breakpoint
2713 && mr->attrib.mode != MEM_RW)
2714 {
2715 fprintf_unfiltered (tmp_error_stream,
2716 _("Cannot insert breakpoint %d.\n"
2717 "Cannot set software breakpoint "
2718 "at read-only address %s\n"),
2719 bl->owner->number,
2720 paddress (bl->gdbarch, bl->address));
2721 return 1;
2722 }
2723 }
2724 }
2725
2726 /* First check to see if we have to handle an overlay. */
2727 if (overlay_debugging == ovly_off
2728 || bl->section == NULL
2729 || !(section_is_overlay (bl->section)))
2730 {
2731 /* No overlay handling: just set the breakpoint. */
2732 TRY
2733 {
2734 int val;
2735
2736 val = bl->owner->ops->insert_location (bl);
2737 if (val)
2738 bp_err = GENERIC_ERROR;
2739 }
2740 CATCH (e, RETURN_MASK_ALL)
2741 {
2742 bp_err = e.error;
2743 bp_err_message = e.message;
2744 }
2745 END_CATCH
2746 }
2747 else
2748 {
2749 /* This breakpoint is in an overlay section.
2750 Shall we set a breakpoint at the LMA? */
2751 if (!overlay_events_enabled)
2752 {
2753 /* Yes -- overlay event support is not active,
2754 so we must try to set a breakpoint at the LMA.
2755 This will not work for a hardware breakpoint. */
2756 if (bl->loc_type == bp_loc_hardware_breakpoint)
2757 warning (_("hardware breakpoint %d not supported in overlay!"),
2758 bl->owner->number);
2759 else
2760 {
2761 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2762 bl->section);
2763 /* Set a software (trap) breakpoint at the LMA. */
2764 bl->overlay_target_info = bl->target_info;
2765 bl->overlay_target_info.reqstd_address = addr;
2766
2767 /* No overlay handling: just set the breakpoint. */
2768 TRY
2769 {
2770 int val;
2771
2772 val = target_insert_breakpoint (bl->gdbarch,
2773 &bl->overlay_target_info);
2774 if (val)
2775 bp_err = GENERIC_ERROR;
2776 }
2777 CATCH (e, RETURN_MASK_ALL)
2778 {
2779 bp_err = e.error;
2780 bp_err_message = e.message;
2781 }
2782 END_CATCH
2783
2784 if (bp_err != GDB_NO_ERROR)
2785 fprintf_unfiltered (tmp_error_stream,
2786 "Overlay breakpoint %d "
2787 "failed: in ROM?\n",
2788 bl->owner->number);
2789 }
2790 }
2791 /* Shall we set a breakpoint at the VMA? */
2792 if (section_is_mapped (bl->section))
2793 {
2794 /* Yes. This overlay section is mapped into memory. */
2795 TRY
2796 {
2797 int val;
2798
2799 val = bl->owner->ops->insert_location (bl);
2800 if (val)
2801 bp_err = GENERIC_ERROR;
2802 }
2803 CATCH (e, RETURN_MASK_ALL)
2804 {
2805 bp_err = e.error;
2806 bp_err_message = e.message;
2807 }
2808 END_CATCH
2809 }
2810 else
2811 {
2812 /* No. This breakpoint will not be inserted.
2813 No error, but do not mark the bp as 'inserted'. */
2814 return 0;
2815 }
2816 }
2817
2818 if (bp_err != GDB_NO_ERROR)
2819 {
2820 /* Can't set the breakpoint. */
2821
2822 /* In some cases, we might not be able to insert a
2823 breakpoint in a shared library that has already been
2824 removed, but we have not yet processed the shlib unload
2825 event. Unfortunately, some targets that implement
2826 breakpoint insertion themselves can't tell why the
2827 breakpoint insertion failed (e.g., the remote target
2828 doesn't define error codes), so we must treat generic
2829 errors as memory errors. */
2830 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2831 && bl->loc_type == bp_loc_software_breakpoint
2832 && (solib_name_from_address (bl->pspace, bl->address)
2833 || shared_objfile_contains_address_p (bl->pspace,
2834 bl->address)))
2835 {
2836 /* See also: disable_breakpoints_in_shlibs. */
2837 bl->shlib_disabled = 1;
2838 observer_notify_breakpoint_modified (bl->owner);
2839 if (!*disabled_breaks)
2840 {
2841 fprintf_unfiltered (tmp_error_stream,
2842 "Cannot insert breakpoint %d.\n",
2843 bl->owner->number);
2844 fprintf_unfiltered (tmp_error_stream,
2845 "Temporarily disabling shared "
2846 "library breakpoints:\n");
2847 }
2848 *disabled_breaks = 1;
2849 fprintf_unfiltered (tmp_error_stream,
2850 "breakpoint #%d\n", bl->owner->number);
2851 return 0;
2852 }
2853 else
2854 {
2855 if (bl->loc_type == bp_loc_hardware_breakpoint)
2856 {
2857 *hw_breakpoint_error = 1;
2858 *hw_bp_error_explained_already = bp_err_message != NULL;
2859 fprintf_unfiltered (tmp_error_stream,
2860 "Cannot insert hardware breakpoint %d%s",
2861 bl->owner->number, bp_err_message ? ":" : ".\n");
2862 if (bp_err_message != NULL)
2863 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2864 }
2865 else
2866 {
2867 if (bp_err_message == NULL)
2868 {
2869 char *message
2870 = memory_error_message (TARGET_XFER_E_IO,
2871 bl->gdbarch, bl->address);
2872 struct cleanup *old_chain = make_cleanup (xfree, message);
2873
2874 fprintf_unfiltered (tmp_error_stream,
2875 "Cannot insert breakpoint %d.\n"
2876 "%s\n",
2877 bl->owner->number, message);
2878 do_cleanups (old_chain);
2879 }
2880 else
2881 {
2882 fprintf_unfiltered (tmp_error_stream,
2883 "Cannot insert breakpoint %d: %s\n",
2884 bl->owner->number,
2885 bp_err_message);
2886 }
2887 }
2888 return 1;
2889
2890 }
2891 }
2892 else
2893 bl->inserted = 1;
2894
2895 return 0;
2896 }
2897
2898 else if (bl->loc_type == bp_loc_hardware_watchpoint
2899 /* NOTE drow/2003-09-08: This state only exists for removing
2900 watchpoints. It's not clear that it's necessary... */
2901 && bl->owner->disposition != disp_del_at_next_stop)
2902 {
2903 int val;
2904
2905 gdb_assert (bl->owner->ops != NULL
2906 && bl->owner->ops->insert_location != NULL);
2907
2908 val = bl->owner->ops->insert_location (bl);
2909
2910 /* If trying to set a read-watchpoint, and it turns out it's not
2911 supported, try emulating one with an access watchpoint. */
2912 if (val == 1 && bl->watchpoint_type == hw_read)
2913 {
2914 struct bp_location *loc, **loc_temp;
2915
2916 /* But don't try to insert it, if there's already another
2917 hw_access location that would be considered a duplicate
2918 of this one. */
2919 ALL_BP_LOCATIONS (loc, loc_temp)
2920 if (loc != bl
2921 && loc->watchpoint_type == hw_access
2922 && watchpoint_locations_match (bl, loc))
2923 {
2924 bl->duplicate = 1;
2925 bl->inserted = 1;
2926 bl->target_info = loc->target_info;
2927 bl->watchpoint_type = hw_access;
2928 val = 0;
2929 break;
2930 }
2931
2932 if (val == 1)
2933 {
2934 bl->watchpoint_type = hw_access;
2935 val = bl->owner->ops->insert_location (bl);
2936
2937 if (val)
2938 /* Back to the original value. */
2939 bl->watchpoint_type = hw_read;
2940 }
2941 }
2942
2943 bl->inserted = (val == 0);
2944 }
2945
2946 else if (bl->owner->type == bp_catchpoint)
2947 {
2948 int val;
2949
2950 gdb_assert (bl->owner->ops != NULL
2951 && bl->owner->ops->insert_location != NULL);
2952
2953 val = bl->owner->ops->insert_location (bl);
2954 if (val)
2955 {
2956 bl->owner->enable_state = bp_disabled;
2957
2958 if (val == 1)
2959 warning (_("\
2960 Error inserting catchpoint %d: Your system does not support this type\n\
2961 of catchpoint."), bl->owner->number);
2962 else
2963 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2964 }
2965
2966 bl->inserted = (val == 0);
2967
2968 /* We've already printed an error message if there was a problem
2969 inserting this catchpoint, and we've disabled the catchpoint,
2970 so just return success. */
2971 return 0;
2972 }
2973
2974 return 0;
2975 }
2976
2977 /* This function is called when program space PSPACE is about to be
2978 deleted. It takes care of updating breakpoints to not reference
2979 PSPACE anymore. */
2980
2981 void
2982 breakpoint_program_space_exit (struct program_space *pspace)
2983 {
2984 struct breakpoint *b, *b_temp;
2985 struct bp_location *loc, **loc_temp;
2986
2987 /* Remove any breakpoint that was set through this program space. */
2988 ALL_BREAKPOINTS_SAFE (b, b_temp)
2989 {
2990 if (b->pspace == pspace)
2991 delete_breakpoint (b);
2992 }
2993
2994 /* Breakpoints set through other program spaces could have locations
2995 bound to PSPACE as well. Remove those. */
2996 ALL_BP_LOCATIONS (loc, loc_temp)
2997 {
2998 struct bp_location *tmp;
2999
3000 if (loc->pspace == pspace)
3001 {
3002 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3003 if (loc->owner->loc == loc)
3004 loc->owner->loc = loc->next;
3005 else
3006 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3007 if (tmp->next == loc)
3008 {
3009 tmp->next = loc->next;
3010 break;
3011 }
3012 }
3013 }
3014
3015 /* Now update the global location list to permanently delete the
3016 removed locations above. */
3017 update_global_location_list (UGLL_DONT_INSERT);
3018 }
3019
3020 /* Make sure all breakpoints are inserted in inferior.
3021 Throws exception on any error.
3022 A breakpoint that is already inserted won't be inserted
3023 again, so calling this function twice is safe. */
3024 void
3025 insert_breakpoints (void)
3026 {
3027 struct breakpoint *bpt;
3028
3029 ALL_BREAKPOINTS (bpt)
3030 if (is_hardware_watchpoint (bpt))
3031 {
3032 struct watchpoint *w = (struct watchpoint *) bpt;
3033
3034 update_watchpoint (w, 0 /* don't reparse. */);
3035 }
3036
3037 /* Updating watchpoints creates new locations, so update the global
3038 location list. Explicitly tell ugll to insert locations and
3039 ignore breakpoints_always_inserted_mode. */
3040 update_global_location_list (UGLL_INSERT);
3041 }
3042
3043 /* Invoke CALLBACK for each of bp_location. */
3044
3045 void
3046 iterate_over_bp_locations (walk_bp_location_callback callback)
3047 {
3048 struct bp_location *loc, **loc_tmp;
3049
3050 ALL_BP_LOCATIONS (loc, loc_tmp)
3051 {
3052 callback (loc, NULL);
3053 }
3054 }
3055
3056 /* This is used when we need to synch breakpoint conditions between GDB and the
3057 target. It is the case with deleting and disabling of breakpoints when using
3058 always-inserted mode. */
3059
3060 static void
3061 update_inserted_breakpoint_locations (void)
3062 {
3063 struct bp_location *bl, **blp_tmp;
3064 int error_flag = 0;
3065 int val = 0;
3066 int disabled_breaks = 0;
3067 int hw_breakpoint_error = 0;
3068 int hw_bp_details_reported = 0;
3069
3070 struct ui_file *tmp_error_stream = mem_fileopen ();
3071 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3072
3073 /* Explicitly mark the warning -- this will only be printed if
3074 there was an error. */
3075 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3076
3077 save_current_space_and_thread ();
3078
3079 ALL_BP_LOCATIONS (bl, blp_tmp)
3080 {
3081 /* We only want to update software breakpoints and hardware
3082 breakpoints. */
3083 if (!is_breakpoint (bl->owner))
3084 continue;
3085
3086 /* We only want to update locations that are already inserted
3087 and need updating. This is to avoid unwanted insertion during
3088 deletion of breakpoints. */
3089 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3090 continue;
3091
3092 switch_to_program_space_and_thread (bl->pspace);
3093
3094 /* For targets that support global breakpoints, there's no need
3095 to select an inferior to insert breakpoint to. In fact, even
3096 if we aren't attached to any process yet, we should still
3097 insert breakpoints. */
3098 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3099 && ptid_equal (inferior_ptid, null_ptid))
3100 continue;
3101
3102 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3103 &hw_breakpoint_error, &hw_bp_details_reported);
3104 if (val)
3105 error_flag = val;
3106 }
3107
3108 if (error_flag)
3109 {
3110 target_terminal_ours_for_output ();
3111 error_stream (tmp_error_stream);
3112 }
3113
3114 do_cleanups (cleanups);
3115 }
3116
3117 /* Used when starting or continuing the program. */
3118
3119 static void
3120 insert_breakpoint_locations (void)
3121 {
3122 struct breakpoint *bpt;
3123 struct bp_location *bl, **blp_tmp;
3124 int error_flag = 0;
3125 int val = 0;
3126 int disabled_breaks = 0;
3127 int hw_breakpoint_error = 0;
3128 int hw_bp_error_explained_already = 0;
3129
3130 struct ui_file *tmp_error_stream = mem_fileopen ();
3131 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3132
3133 /* Explicitly mark the warning -- this will only be printed if
3134 there was an error. */
3135 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3136
3137 save_current_space_and_thread ();
3138
3139 ALL_BP_LOCATIONS (bl, blp_tmp)
3140 {
3141 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3142 continue;
3143
3144 /* There is no point inserting thread-specific breakpoints if
3145 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3146 has BL->OWNER always non-NULL. */
3147 if (bl->owner->thread != -1
3148 && !valid_global_thread_id (bl->owner->thread))
3149 continue;
3150
3151 switch_to_program_space_and_thread (bl->pspace);
3152
3153 /* For targets that support global breakpoints, there's no need
3154 to select an inferior to insert breakpoint to. In fact, even
3155 if we aren't attached to any process yet, we should still
3156 insert breakpoints. */
3157 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3158 && ptid_equal (inferior_ptid, null_ptid))
3159 continue;
3160
3161 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3162 &hw_breakpoint_error, &hw_bp_error_explained_already);
3163 if (val)
3164 error_flag = val;
3165 }
3166
3167 /* If we failed to insert all locations of a watchpoint, remove
3168 them, as half-inserted watchpoint is of limited use. */
3169 ALL_BREAKPOINTS (bpt)
3170 {
3171 int some_failed = 0;
3172 struct bp_location *loc;
3173
3174 if (!is_hardware_watchpoint (bpt))
3175 continue;
3176
3177 if (!breakpoint_enabled (bpt))
3178 continue;
3179
3180 if (bpt->disposition == disp_del_at_next_stop)
3181 continue;
3182
3183 for (loc = bpt->loc; loc; loc = loc->next)
3184 if (!loc->inserted && should_be_inserted (loc))
3185 {
3186 some_failed = 1;
3187 break;
3188 }
3189 if (some_failed)
3190 {
3191 for (loc = bpt->loc; loc; loc = loc->next)
3192 if (loc->inserted)
3193 remove_breakpoint (loc, mark_uninserted);
3194
3195 hw_breakpoint_error = 1;
3196 fprintf_unfiltered (tmp_error_stream,
3197 "Could not insert hardware watchpoint %d.\n",
3198 bpt->number);
3199 error_flag = -1;
3200 }
3201 }
3202
3203 if (error_flag)
3204 {
3205 /* If a hardware breakpoint or watchpoint was inserted, add a
3206 message about possibly exhausted resources. */
3207 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3208 {
3209 fprintf_unfiltered (tmp_error_stream,
3210 "Could not insert hardware breakpoints:\n\
3211 You may have requested too many hardware breakpoints/watchpoints.\n");
3212 }
3213 target_terminal_ours_for_output ();
3214 error_stream (tmp_error_stream);
3215 }
3216
3217 do_cleanups (cleanups);
3218 }
3219
3220 /* Used when the program stops.
3221 Returns zero if successful, or non-zero if there was a problem
3222 removing a breakpoint location. */
3223
3224 int
3225 remove_breakpoints (void)
3226 {
3227 struct bp_location *bl, **blp_tmp;
3228 int val = 0;
3229
3230 ALL_BP_LOCATIONS (bl, blp_tmp)
3231 {
3232 if (bl->inserted && !is_tracepoint (bl->owner))
3233 val |= remove_breakpoint (bl, mark_uninserted);
3234 }
3235 return val;
3236 }
3237
3238 /* When a thread exits, remove breakpoints that are related to
3239 that thread. */
3240
3241 static void
3242 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3243 {
3244 struct breakpoint *b, *b_tmp;
3245
3246 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3247 {
3248 if (b->thread == tp->global_num && user_breakpoint_p (b))
3249 {
3250 b->disposition = disp_del_at_next_stop;
3251
3252 printf_filtered (_("\
3253 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3254 b->number, print_thread_id (tp));
3255
3256 /* Hide it from the user. */
3257 b->number = 0;
3258 }
3259 }
3260 }
3261
3262 /* Remove breakpoints of process PID. */
3263
3264 int
3265 remove_breakpoints_pid (int pid)
3266 {
3267 struct bp_location *bl, **blp_tmp;
3268 int val;
3269 struct inferior *inf = find_inferior_pid (pid);
3270
3271 ALL_BP_LOCATIONS (bl, blp_tmp)
3272 {
3273 if (bl->pspace != inf->pspace)
3274 continue;
3275
3276 if (bl->inserted && !bl->target_info.persist)
3277 {
3278 val = remove_breakpoint (bl, mark_uninserted);
3279 if (val != 0)
3280 return val;
3281 }
3282 }
3283 return 0;
3284 }
3285
3286 int
3287 reattach_breakpoints (int pid)
3288 {
3289 struct cleanup *old_chain;
3290 struct bp_location *bl, **blp_tmp;
3291 int val;
3292 struct ui_file *tmp_error_stream;
3293 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3294 struct inferior *inf;
3295 struct thread_info *tp;
3296
3297 tp = any_live_thread_of_process (pid);
3298 if (tp == NULL)
3299 return 1;
3300
3301 inf = find_inferior_pid (pid);
3302 old_chain = save_inferior_ptid ();
3303
3304 inferior_ptid = tp->ptid;
3305
3306 tmp_error_stream = mem_fileopen ();
3307 make_cleanup_ui_file_delete (tmp_error_stream);
3308
3309 ALL_BP_LOCATIONS (bl, blp_tmp)
3310 {
3311 if (bl->pspace != inf->pspace)
3312 continue;
3313
3314 if (bl->inserted)
3315 {
3316 bl->inserted = 0;
3317 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3318 if (val != 0)
3319 {
3320 do_cleanups (old_chain);
3321 return val;
3322 }
3323 }
3324 }
3325 do_cleanups (old_chain);
3326 return 0;
3327 }
3328
3329 static int internal_breakpoint_number = -1;
3330
3331 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3332 If INTERNAL is non-zero, the breakpoint number will be populated
3333 from internal_breakpoint_number and that variable decremented.
3334 Otherwise the breakpoint number will be populated from
3335 breakpoint_count and that value incremented. Internal breakpoints
3336 do not set the internal var bpnum. */
3337 static void
3338 set_breakpoint_number (int internal, struct breakpoint *b)
3339 {
3340 if (internal)
3341 b->number = internal_breakpoint_number--;
3342 else
3343 {
3344 set_breakpoint_count (breakpoint_count + 1);
3345 b->number = breakpoint_count;
3346 }
3347 }
3348
3349 static struct breakpoint *
3350 create_internal_breakpoint (struct gdbarch *gdbarch,
3351 CORE_ADDR address, enum bptype type,
3352 const struct breakpoint_ops *ops)
3353 {
3354 struct symtab_and_line sal;
3355 struct breakpoint *b;
3356
3357 init_sal (&sal); /* Initialize to zeroes. */
3358
3359 sal.pc = address;
3360 sal.section = find_pc_overlay (sal.pc);
3361 sal.pspace = current_program_space;
3362
3363 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3364 b->number = internal_breakpoint_number--;
3365 b->disposition = disp_donttouch;
3366
3367 return b;
3368 }
3369
3370 static const char *const longjmp_names[] =
3371 {
3372 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3373 };
3374 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3375
3376 /* Per-objfile data private to breakpoint.c. */
3377 struct breakpoint_objfile_data
3378 {
3379 /* Minimal symbol for "_ovly_debug_event" (if any). */
3380 struct bound_minimal_symbol overlay_msym;
3381
3382 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3383 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3384
3385 /* True if we have looked for longjmp probes. */
3386 int longjmp_searched;
3387
3388 /* SystemTap probe points for longjmp (if any). */
3389 VEC (probe_p) *longjmp_probes;
3390
3391 /* Minimal symbol for "std::terminate()" (if any). */
3392 struct bound_minimal_symbol terminate_msym;
3393
3394 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3395 struct bound_minimal_symbol exception_msym;
3396
3397 /* True if we have looked for exception probes. */
3398 int exception_searched;
3399
3400 /* SystemTap probe points for unwinding (if any). */
3401 VEC (probe_p) *exception_probes;
3402 };
3403
3404 static const struct objfile_data *breakpoint_objfile_key;
3405
3406 /* Minimal symbol not found sentinel. */
3407 static struct minimal_symbol msym_not_found;
3408
3409 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3410
3411 static int
3412 msym_not_found_p (const struct minimal_symbol *msym)
3413 {
3414 return msym == &msym_not_found;
3415 }
3416
3417 /* Return per-objfile data needed by breakpoint.c.
3418 Allocate the data if necessary. */
3419
3420 static struct breakpoint_objfile_data *
3421 get_breakpoint_objfile_data (struct objfile *objfile)
3422 {
3423 struct breakpoint_objfile_data *bp_objfile_data;
3424
3425 bp_objfile_data = ((struct breakpoint_objfile_data *)
3426 objfile_data (objfile, breakpoint_objfile_key));
3427 if (bp_objfile_data == NULL)
3428 {
3429 bp_objfile_data =
3430 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3431
3432 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3433 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3434 }
3435 return bp_objfile_data;
3436 }
3437
3438 static void
3439 free_breakpoint_probes (struct objfile *obj, void *data)
3440 {
3441 struct breakpoint_objfile_data *bp_objfile_data
3442 = (struct breakpoint_objfile_data *) data;
3443
3444 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3445 VEC_free (probe_p, bp_objfile_data->exception_probes);
3446 }
3447
3448 static void
3449 create_overlay_event_breakpoint (void)
3450 {
3451 struct objfile *objfile;
3452 const char *const func_name = "_ovly_debug_event";
3453
3454 ALL_OBJFILES (objfile)
3455 {
3456 struct breakpoint *b;
3457 struct breakpoint_objfile_data *bp_objfile_data;
3458 CORE_ADDR addr;
3459 struct explicit_location explicit_loc;
3460
3461 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3462
3463 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3464 continue;
3465
3466 if (bp_objfile_data->overlay_msym.minsym == NULL)
3467 {
3468 struct bound_minimal_symbol m;
3469
3470 m = lookup_minimal_symbol_text (func_name, objfile);
3471 if (m.minsym == NULL)
3472 {
3473 /* Avoid future lookups in this objfile. */
3474 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3475 continue;
3476 }
3477 bp_objfile_data->overlay_msym = m;
3478 }
3479
3480 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3481 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3482 bp_overlay_event,
3483 &internal_breakpoint_ops);
3484 initialize_explicit_location (&explicit_loc);
3485 explicit_loc.function_name = ASTRDUP (func_name);
3486 b->location = new_explicit_location (&explicit_loc);
3487
3488 if (overlay_debugging == ovly_auto)
3489 {
3490 b->enable_state = bp_enabled;
3491 overlay_events_enabled = 1;
3492 }
3493 else
3494 {
3495 b->enable_state = bp_disabled;
3496 overlay_events_enabled = 0;
3497 }
3498 }
3499 update_global_location_list (UGLL_MAY_INSERT);
3500 }
3501
3502 static void
3503 create_longjmp_master_breakpoint (void)
3504 {
3505 struct program_space *pspace;
3506 struct cleanup *old_chain;
3507
3508 old_chain = save_current_program_space ();
3509
3510 ALL_PSPACES (pspace)
3511 {
3512 struct objfile *objfile;
3513
3514 set_current_program_space (pspace);
3515
3516 ALL_OBJFILES (objfile)
3517 {
3518 int i;
3519 struct gdbarch *gdbarch;
3520 struct breakpoint_objfile_data *bp_objfile_data;
3521
3522 gdbarch = get_objfile_arch (objfile);
3523
3524 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3525
3526 if (!bp_objfile_data->longjmp_searched)
3527 {
3528 VEC (probe_p) *ret;
3529
3530 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3531 if (ret != NULL)
3532 {
3533 /* We are only interested in checking one element. */
3534 struct probe *p = VEC_index (probe_p, ret, 0);
3535
3536 if (!can_evaluate_probe_arguments (p))
3537 {
3538 /* We cannot use the probe interface here, because it does
3539 not know how to evaluate arguments. */
3540 VEC_free (probe_p, ret);
3541 ret = NULL;
3542 }
3543 }
3544 bp_objfile_data->longjmp_probes = ret;
3545 bp_objfile_data->longjmp_searched = 1;
3546 }
3547
3548 if (bp_objfile_data->longjmp_probes != NULL)
3549 {
3550 int i;
3551 struct probe *probe;
3552 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3553
3554 for (i = 0;
3555 VEC_iterate (probe_p,
3556 bp_objfile_data->longjmp_probes,
3557 i, probe);
3558 ++i)
3559 {
3560 struct breakpoint *b;
3561
3562 b = create_internal_breakpoint (gdbarch,
3563 get_probe_address (probe,
3564 objfile),
3565 bp_longjmp_master,
3566 &internal_breakpoint_ops);
3567 b->location
3568 = new_probe_location ("-probe-stap libc:longjmp");
3569 b->enable_state = bp_disabled;
3570 }
3571
3572 continue;
3573 }
3574
3575 if (!gdbarch_get_longjmp_target_p (gdbarch))
3576 continue;
3577
3578 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3579 {
3580 struct breakpoint *b;
3581 const char *func_name;
3582 CORE_ADDR addr;
3583 struct explicit_location explicit_loc;
3584
3585 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3586 continue;
3587
3588 func_name = longjmp_names[i];
3589 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3590 {
3591 struct bound_minimal_symbol m;
3592
3593 m = lookup_minimal_symbol_text (func_name, objfile);
3594 if (m.minsym == NULL)
3595 {
3596 /* Prevent future lookups in this objfile. */
3597 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3598 continue;
3599 }
3600 bp_objfile_data->longjmp_msym[i] = m;
3601 }
3602
3603 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3604 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3605 &internal_breakpoint_ops);
3606 initialize_explicit_location (&explicit_loc);
3607 explicit_loc.function_name = ASTRDUP (func_name);
3608 b->location = new_explicit_location (&explicit_loc);
3609 b->enable_state = bp_disabled;
3610 }
3611 }
3612 }
3613 update_global_location_list (UGLL_MAY_INSERT);
3614
3615 do_cleanups (old_chain);
3616 }
3617
3618 /* Create a master std::terminate breakpoint. */
3619 static void
3620 create_std_terminate_master_breakpoint (void)
3621 {
3622 struct program_space *pspace;
3623 struct cleanup *old_chain;
3624 const char *const func_name = "std::terminate()";
3625
3626 old_chain = save_current_program_space ();
3627
3628 ALL_PSPACES (pspace)
3629 {
3630 struct objfile *objfile;
3631 CORE_ADDR addr;
3632
3633 set_current_program_space (pspace);
3634
3635 ALL_OBJFILES (objfile)
3636 {
3637 struct breakpoint *b;
3638 struct breakpoint_objfile_data *bp_objfile_data;
3639 struct explicit_location explicit_loc;
3640
3641 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3642
3643 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3644 continue;
3645
3646 if (bp_objfile_data->terminate_msym.minsym == NULL)
3647 {
3648 struct bound_minimal_symbol m;
3649
3650 m = lookup_minimal_symbol (func_name, NULL, objfile);
3651 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3652 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3653 {
3654 /* Prevent future lookups in this objfile. */
3655 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3656 continue;
3657 }
3658 bp_objfile_data->terminate_msym = m;
3659 }
3660
3661 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3662 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3663 bp_std_terminate_master,
3664 &internal_breakpoint_ops);
3665 initialize_explicit_location (&explicit_loc);
3666 explicit_loc.function_name = ASTRDUP (func_name);
3667 b->location = new_explicit_location (&explicit_loc);
3668 b->enable_state = bp_disabled;
3669 }
3670 }
3671
3672 update_global_location_list (UGLL_MAY_INSERT);
3673
3674 do_cleanups (old_chain);
3675 }
3676
3677 /* Install a master breakpoint on the unwinder's debug hook. */
3678
3679 static void
3680 create_exception_master_breakpoint (void)
3681 {
3682 struct objfile *objfile;
3683 const char *const func_name = "_Unwind_DebugHook";
3684
3685 ALL_OBJFILES (objfile)
3686 {
3687 struct breakpoint *b;
3688 struct gdbarch *gdbarch;
3689 struct breakpoint_objfile_data *bp_objfile_data;
3690 CORE_ADDR addr;
3691 struct explicit_location explicit_loc;
3692
3693 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3694
3695 /* We prefer the SystemTap probe point if it exists. */
3696 if (!bp_objfile_data->exception_searched)
3697 {
3698 VEC (probe_p) *ret;
3699
3700 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3701
3702 if (ret != NULL)
3703 {
3704 /* We are only interested in checking one element. */
3705 struct probe *p = VEC_index (probe_p, ret, 0);
3706
3707 if (!can_evaluate_probe_arguments (p))
3708 {
3709 /* We cannot use the probe interface here, because it does
3710 not know how to evaluate arguments. */
3711 VEC_free (probe_p, ret);
3712 ret = NULL;
3713 }
3714 }
3715 bp_objfile_data->exception_probes = ret;
3716 bp_objfile_data->exception_searched = 1;
3717 }
3718
3719 if (bp_objfile_data->exception_probes != NULL)
3720 {
3721 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3722 int i;
3723 struct probe *probe;
3724
3725 for (i = 0;
3726 VEC_iterate (probe_p,
3727 bp_objfile_data->exception_probes,
3728 i, probe);
3729 ++i)
3730 {
3731 struct breakpoint *b;
3732
3733 b = create_internal_breakpoint (gdbarch,
3734 get_probe_address (probe,
3735 objfile),
3736 bp_exception_master,
3737 &internal_breakpoint_ops);
3738 b->location
3739 = new_probe_location ("-probe-stap libgcc:unwind");
3740 b->enable_state = bp_disabled;
3741 }
3742
3743 continue;
3744 }
3745
3746 /* Otherwise, try the hook function. */
3747
3748 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3749 continue;
3750
3751 gdbarch = get_objfile_arch (objfile);
3752
3753 if (bp_objfile_data->exception_msym.minsym == NULL)
3754 {
3755 struct bound_minimal_symbol debug_hook;
3756
3757 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3758 if (debug_hook.minsym == NULL)
3759 {
3760 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3761 continue;
3762 }
3763
3764 bp_objfile_data->exception_msym = debug_hook;
3765 }
3766
3767 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3768 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3769 &current_target);
3770 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3771 &internal_breakpoint_ops);
3772 initialize_explicit_location (&explicit_loc);
3773 explicit_loc.function_name = ASTRDUP (func_name);
3774 b->location = new_explicit_location (&explicit_loc);
3775 b->enable_state = bp_disabled;
3776 }
3777
3778 update_global_location_list (UGLL_MAY_INSERT);
3779 }
3780
3781 void
3782 update_breakpoints_after_exec (void)
3783 {
3784 struct breakpoint *b, *b_tmp;
3785 struct bp_location *bploc, **bplocp_tmp;
3786
3787 /* We're about to delete breakpoints from GDB's lists. If the
3788 INSERTED flag is true, GDB will try to lift the breakpoints by
3789 writing the breakpoints' "shadow contents" back into memory. The
3790 "shadow contents" are NOT valid after an exec, so GDB should not
3791 do that. Instead, the target is responsible from marking
3792 breakpoints out as soon as it detects an exec. We don't do that
3793 here instead, because there may be other attempts to delete
3794 breakpoints after detecting an exec and before reaching here. */
3795 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3796 if (bploc->pspace == current_program_space)
3797 gdb_assert (!bploc->inserted);
3798
3799 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3800 {
3801 if (b->pspace != current_program_space)
3802 continue;
3803
3804 /* Solib breakpoints must be explicitly reset after an exec(). */
3805 if (b->type == bp_shlib_event)
3806 {
3807 delete_breakpoint (b);
3808 continue;
3809 }
3810
3811 /* JIT breakpoints must be explicitly reset after an exec(). */
3812 if (b->type == bp_jit_event)
3813 {
3814 delete_breakpoint (b);
3815 continue;
3816 }
3817
3818 /* Thread event breakpoints must be set anew after an exec(),
3819 as must overlay event and longjmp master breakpoints. */
3820 if (b->type == bp_thread_event || b->type == bp_overlay_event
3821 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3822 || b->type == bp_exception_master)
3823 {
3824 delete_breakpoint (b);
3825 continue;
3826 }
3827
3828 /* Step-resume breakpoints are meaningless after an exec(). */
3829 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3830 {
3831 delete_breakpoint (b);
3832 continue;
3833 }
3834
3835 /* Just like single-step breakpoints. */
3836 if (b->type == bp_single_step)
3837 {
3838 delete_breakpoint (b);
3839 continue;
3840 }
3841
3842 /* Longjmp and longjmp-resume breakpoints are also meaningless
3843 after an exec. */
3844 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3845 || b->type == bp_longjmp_call_dummy
3846 || b->type == bp_exception || b->type == bp_exception_resume)
3847 {
3848 delete_breakpoint (b);
3849 continue;
3850 }
3851
3852 if (b->type == bp_catchpoint)
3853 {
3854 /* For now, none of the bp_catchpoint breakpoints need to
3855 do anything at this point. In the future, if some of
3856 the catchpoints need to something, we will need to add
3857 a new method, and call this method from here. */
3858 continue;
3859 }
3860
3861 /* bp_finish is a special case. The only way we ought to be able
3862 to see one of these when an exec() has happened, is if the user
3863 caught a vfork, and then said "finish". Ordinarily a finish just
3864 carries them to the call-site of the current callee, by setting
3865 a temporary bp there and resuming. But in this case, the finish
3866 will carry them entirely through the vfork & exec.
3867
3868 We don't want to allow a bp_finish to remain inserted now. But
3869 we can't safely delete it, 'cause finish_command has a handle to
3870 the bp on a bpstat, and will later want to delete it. There's a
3871 chance (and I've seen it happen) that if we delete the bp_finish
3872 here, that its storage will get reused by the time finish_command
3873 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3874 We really must allow finish_command to delete a bp_finish.
3875
3876 In the absence of a general solution for the "how do we know
3877 it's safe to delete something others may have handles to?"
3878 problem, what we'll do here is just uninsert the bp_finish, and
3879 let finish_command delete it.
3880
3881 (We know the bp_finish is "doomed" in the sense that it's
3882 momentary, and will be deleted as soon as finish_command sees
3883 the inferior stopped. So it doesn't matter that the bp's
3884 address is probably bogus in the new a.out, unlike e.g., the
3885 solib breakpoints.) */
3886
3887 if (b->type == bp_finish)
3888 {
3889 continue;
3890 }
3891
3892 /* Without a symbolic address, we have little hope of the
3893 pre-exec() address meaning the same thing in the post-exec()
3894 a.out. */
3895 if (event_location_empty_p (b->location))
3896 {
3897 delete_breakpoint (b);
3898 continue;
3899 }
3900 }
3901 }
3902
3903 int
3904 detach_breakpoints (ptid_t ptid)
3905 {
3906 struct bp_location *bl, **blp_tmp;
3907 int val = 0;
3908 struct cleanup *old_chain = save_inferior_ptid ();
3909 struct inferior *inf = current_inferior ();
3910
3911 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3912 error (_("Cannot detach breakpoints of inferior_ptid"));
3913
3914 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3915 inferior_ptid = ptid;
3916 ALL_BP_LOCATIONS (bl, blp_tmp)
3917 {
3918 if (bl->pspace != inf->pspace)
3919 continue;
3920
3921 /* This function must physically remove breakpoints locations
3922 from the specified ptid, without modifying the breakpoint
3923 package's state. Locations of type bp_loc_other are only
3924 maintained at GDB side. So, there is no need to remove
3925 these bp_loc_other locations. Moreover, removing these
3926 would modify the breakpoint package's state. */
3927 if (bl->loc_type == bp_loc_other)
3928 continue;
3929
3930 if (bl->inserted)
3931 val |= remove_breakpoint_1 (bl, mark_inserted);
3932 }
3933
3934 do_cleanups (old_chain);
3935 return val;
3936 }
3937
3938 /* Remove the breakpoint location BL from the current address space.
3939 Note that this is used to detach breakpoints from a child fork.
3940 When we get here, the child isn't in the inferior list, and neither
3941 do we have objects to represent its address space --- we should
3942 *not* look at bl->pspace->aspace here. */
3943
3944 static int
3945 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3946 {
3947 int val;
3948
3949 /* BL is never in moribund_locations by our callers. */
3950 gdb_assert (bl->owner != NULL);
3951
3952 /* The type of none suggests that owner is actually deleted.
3953 This should not ever happen. */
3954 gdb_assert (bl->owner->type != bp_none);
3955
3956 if (bl->loc_type == bp_loc_software_breakpoint
3957 || bl->loc_type == bp_loc_hardware_breakpoint)
3958 {
3959 /* "Normal" instruction breakpoint: either the standard
3960 trap-instruction bp (bp_breakpoint), or a
3961 bp_hardware_breakpoint. */
3962
3963 /* First check to see if we have to handle an overlay. */
3964 if (overlay_debugging == ovly_off
3965 || bl->section == NULL
3966 || !(section_is_overlay (bl->section)))
3967 {
3968 /* No overlay handling: just remove the breakpoint. */
3969
3970 /* If we're trying to uninsert a memory breakpoint that we
3971 know is set in a dynamic object that is marked
3972 shlib_disabled, then either the dynamic object was
3973 removed with "remove-symbol-file" or with
3974 "nosharedlibrary". In the former case, we don't know
3975 whether another dynamic object might have loaded over the
3976 breakpoint's address -- the user might well let us know
3977 about it next with add-symbol-file (the whole point of
3978 add-symbol-file is letting the user manually maintain a
3979 list of dynamically loaded objects). If we have the
3980 breakpoint's shadow memory, that is, this is a software
3981 breakpoint managed by GDB, check whether the breakpoint
3982 is still inserted in memory, to avoid overwriting wrong
3983 code with stale saved shadow contents. Note that HW
3984 breakpoints don't have shadow memory, as they're
3985 implemented using a mechanism that is not dependent on
3986 being able to modify the target's memory, and as such
3987 they should always be removed. */
3988 if (bl->shlib_disabled
3989 && bl->target_info.shadow_len != 0
3990 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3991 val = 0;
3992 else
3993 val = bl->owner->ops->remove_location (bl);
3994 }
3995 else
3996 {
3997 /* This breakpoint is in an overlay section.
3998 Did we set a breakpoint at the LMA? */
3999 if (!overlay_events_enabled)
4000 {
4001 /* Yes -- overlay event support is not active, so we
4002 should have set a breakpoint at the LMA. Remove it.
4003 */
4004 /* Ignore any failures: if the LMA is in ROM, we will
4005 have already warned when we failed to insert it. */
4006 if (bl->loc_type == bp_loc_hardware_breakpoint)
4007 target_remove_hw_breakpoint (bl->gdbarch,
4008 &bl->overlay_target_info);
4009 else
4010 target_remove_breakpoint (bl->gdbarch,
4011 &bl->overlay_target_info);
4012 }
4013 /* Did we set a breakpoint at the VMA?
4014 If so, we will have marked the breakpoint 'inserted'. */
4015 if (bl->inserted)
4016 {
4017 /* Yes -- remove it. Previously we did not bother to
4018 remove the breakpoint if the section had been
4019 unmapped, but let's not rely on that being safe. We
4020 don't know what the overlay manager might do. */
4021
4022 /* However, we should remove *software* breakpoints only
4023 if the section is still mapped, or else we overwrite
4024 wrong code with the saved shadow contents. */
4025 if (bl->loc_type == bp_loc_hardware_breakpoint
4026 || section_is_mapped (bl->section))
4027 val = bl->owner->ops->remove_location (bl);
4028 else
4029 val = 0;
4030 }
4031 else
4032 {
4033 /* No -- not inserted, so no need to remove. No error. */
4034 val = 0;
4035 }
4036 }
4037
4038 /* In some cases, we might not be able to remove a breakpoint in
4039 a shared library that has already been removed, but we have
4040 not yet processed the shlib unload event. Similarly for an
4041 unloaded add-symbol-file object - the user might not yet have
4042 had the chance to remove-symbol-file it. shlib_disabled will
4043 be set if the library/object has already been removed, but
4044 the breakpoint hasn't been uninserted yet, e.g., after
4045 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4046 always-inserted mode. */
4047 if (val
4048 && (bl->loc_type == bp_loc_software_breakpoint
4049 && (bl->shlib_disabled
4050 || solib_name_from_address (bl->pspace, bl->address)
4051 || shared_objfile_contains_address_p (bl->pspace,
4052 bl->address))))
4053 val = 0;
4054
4055 if (val)
4056 return val;
4057 bl->inserted = (is == mark_inserted);
4058 }
4059 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4060 {
4061 gdb_assert (bl->owner->ops != NULL
4062 && bl->owner->ops->remove_location != NULL);
4063
4064 bl->inserted = (is == mark_inserted);
4065 bl->owner->ops->remove_location (bl);
4066
4067 /* Failure to remove any of the hardware watchpoints comes here. */
4068 if ((is == mark_uninserted) && (bl->inserted))
4069 warning (_("Could not remove hardware watchpoint %d."),
4070 bl->owner->number);
4071 }
4072 else if (bl->owner->type == bp_catchpoint
4073 && breakpoint_enabled (bl->owner)
4074 && !bl->duplicate)
4075 {
4076 gdb_assert (bl->owner->ops != NULL
4077 && bl->owner->ops->remove_location != NULL);
4078
4079 val = bl->owner->ops->remove_location (bl);
4080 if (val)
4081 return val;
4082
4083 bl->inserted = (is == mark_inserted);
4084 }
4085
4086 return 0;
4087 }
4088
4089 static int
4090 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
4091 {
4092 int ret;
4093 struct cleanup *old_chain;
4094
4095 /* BL is never in moribund_locations by our callers. */
4096 gdb_assert (bl->owner != NULL);
4097
4098 /* The type of none suggests that owner is actually deleted.
4099 This should not ever happen. */
4100 gdb_assert (bl->owner->type != bp_none);
4101
4102 old_chain = save_current_space_and_thread ();
4103
4104 switch_to_program_space_and_thread (bl->pspace);
4105
4106 ret = remove_breakpoint_1 (bl, is);
4107
4108 do_cleanups (old_chain);
4109 return ret;
4110 }
4111
4112 /* Clear the "inserted" flag in all breakpoints. */
4113
4114 void
4115 mark_breakpoints_out (void)
4116 {
4117 struct bp_location *bl, **blp_tmp;
4118
4119 ALL_BP_LOCATIONS (bl, blp_tmp)
4120 if (bl->pspace == current_program_space)
4121 bl->inserted = 0;
4122 }
4123
4124 /* Clear the "inserted" flag in all breakpoints and delete any
4125 breakpoints which should go away between runs of the program.
4126
4127 Plus other such housekeeping that has to be done for breakpoints
4128 between runs.
4129
4130 Note: this function gets called at the end of a run (by
4131 generic_mourn_inferior) and when a run begins (by
4132 init_wait_for_inferior). */
4133
4134
4135
4136 void
4137 breakpoint_init_inferior (enum inf_context context)
4138 {
4139 struct breakpoint *b, *b_tmp;
4140 struct bp_location *bl, **blp_tmp;
4141 int ix;
4142 struct program_space *pspace = current_program_space;
4143
4144 /* If breakpoint locations are shared across processes, then there's
4145 nothing to do. */
4146 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4147 return;
4148
4149 mark_breakpoints_out ();
4150
4151 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4152 {
4153 if (b->loc && b->loc->pspace != pspace)
4154 continue;
4155
4156 switch (b->type)
4157 {
4158 case bp_call_dummy:
4159 case bp_longjmp_call_dummy:
4160
4161 /* If the call dummy breakpoint is at the entry point it will
4162 cause problems when the inferior is rerun, so we better get
4163 rid of it. */
4164
4165 case bp_watchpoint_scope:
4166
4167 /* Also get rid of scope breakpoints. */
4168
4169 case bp_shlib_event:
4170
4171 /* Also remove solib event breakpoints. Their addresses may
4172 have changed since the last time we ran the program.
4173 Actually we may now be debugging against different target;
4174 and so the solib backend that installed this breakpoint may
4175 not be used in by the target. E.g.,
4176
4177 (gdb) file prog-linux
4178 (gdb) run # native linux target
4179 ...
4180 (gdb) kill
4181 (gdb) file prog-win.exe
4182 (gdb) tar rem :9999 # remote Windows gdbserver.
4183 */
4184
4185 case bp_step_resume:
4186
4187 /* Also remove step-resume breakpoints. */
4188
4189 case bp_single_step:
4190
4191 /* Also remove single-step breakpoints. */
4192
4193 delete_breakpoint (b);
4194 break;
4195
4196 case bp_watchpoint:
4197 case bp_hardware_watchpoint:
4198 case bp_read_watchpoint:
4199 case bp_access_watchpoint:
4200 {
4201 struct watchpoint *w = (struct watchpoint *) b;
4202
4203 /* Likewise for watchpoints on local expressions. */
4204 if (w->exp_valid_block != NULL)
4205 delete_breakpoint (b);
4206 else if (context == inf_starting)
4207 {
4208 /* Reset val field to force reread of starting value in
4209 insert_breakpoints. */
4210 if (w->val)
4211 value_free (w->val);
4212 w->val = NULL;
4213 w->val_valid = 0;
4214 }
4215 }
4216 break;
4217 default:
4218 break;
4219 }
4220 }
4221
4222 /* Get rid of the moribund locations. */
4223 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4224 decref_bp_location (&bl);
4225 VEC_free (bp_location_p, moribund_locations);
4226 }
4227
4228 /* These functions concern about actual breakpoints inserted in the
4229 target --- to e.g. check if we need to do decr_pc adjustment or if
4230 we need to hop over the bkpt --- so we check for address space
4231 match, not program space. */
4232
4233 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4234 exists at PC. It returns ordinary_breakpoint_here if it's an
4235 ordinary breakpoint, or permanent_breakpoint_here if it's a
4236 permanent breakpoint.
4237 - When continuing from a location with an ordinary breakpoint, we
4238 actually single step once before calling insert_breakpoints.
4239 - When continuing from a location with a permanent breakpoint, we
4240 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4241 the target, to advance the PC past the breakpoint. */
4242
4243 enum breakpoint_here
4244 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4245 {
4246 struct bp_location *bl, **blp_tmp;
4247 int any_breakpoint_here = 0;
4248
4249 ALL_BP_LOCATIONS (bl, blp_tmp)
4250 {
4251 if (bl->loc_type != bp_loc_software_breakpoint
4252 && bl->loc_type != bp_loc_hardware_breakpoint)
4253 continue;
4254
4255 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4256 if ((breakpoint_enabled (bl->owner)
4257 || bl->permanent)
4258 && breakpoint_location_address_match (bl, aspace, pc))
4259 {
4260 if (overlay_debugging
4261 && section_is_overlay (bl->section)
4262 && !section_is_mapped (bl->section))
4263 continue; /* unmapped overlay -- can't be a match */
4264 else if (bl->permanent)
4265 return permanent_breakpoint_here;
4266 else
4267 any_breakpoint_here = 1;
4268 }
4269 }
4270
4271 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4272 }
4273
4274 /* See breakpoint.h. */
4275
4276 int
4277 breakpoint_in_range_p (struct address_space *aspace,
4278 CORE_ADDR addr, ULONGEST len)
4279 {
4280 struct bp_location *bl, **blp_tmp;
4281
4282 ALL_BP_LOCATIONS (bl, blp_tmp)
4283 {
4284 if (bl->loc_type != bp_loc_software_breakpoint
4285 && bl->loc_type != bp_loc_hardware_breakpoint)
4286 continue;
4287
4288 if ((breakpoint_enabled (bl->owner)
4289 || bl->permanent)
4290 && breakpoint_location_address_range_overlap (bl, aspace,
4291 addr, len))
4292 {
4293 if (overlay_debugging
4294 && section_is_overlay (bl->section)
4295 && !section_is_mapped (bl->section))
4296 {
4297 /* Unmapped overlay -- can't be a match. */
4298 continue;
4299 }
4300
4301 return 1;
4302 }
4303 }
4304
4305 return 0;
4306 }
4307
4308 /* Return true if there's a moribund breakpoint at PC. */
4309
4310 int
4311 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4312 {
4313 struct bp_location *loc;
4314 int ix;
4315
4316 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4317 if (breakpoint_location_address_match (loc, aspace, pc))
4318 return 1;
4319
4320 return 0;
4321 }
4322
4323 /* Returns non-zero iff BL is inserted at PC, in address space
4324 ASPACE. */
4325
4326 static int
4327 bp_location_inserted_here_p (struct bp_location *bl,
4328 struct address_space *aspace, CORE_ADDR pc)
4329 {
4330 if (bl->inserted
4331 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4332 aspace, pc))
4333 {
4334 if (overlay_debugging
4335 && section_is_overlay (bl->section)
4336 && !section_is_mapped (bl->section))
4337 return 0; /* unmapped overlay -- can't be a match */
4338 else
4339 return 1;
4340 }
4341 return 0;
4342 }
4343
4344 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4345
4346 int
4347 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4348 {
4349 struct bp_location **blp, **blp_tmp = NULL;
4350 struct bp_location *bl;
4351
4352 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4353 {
4354 struct bp_location *bl = *blp;
4355
4356 if (bl->loc_type != bp_loc_software_breakpoint
4357 && bl->loc_type != bp_loc_hardware_breakpoint)
4358 continue;
4359
4360 if (bp_location_inserted_here_p (bl, aspace, pc))
4361 return 1;
4362 }
4363 return 0;
4364 }
4365
4366 /* This function returns non-zero iff there is a software breakpoint
4367 inserted at PC. */
4368
4369 int
4370 software_breakpoint_inserted_here_p (struct address_space *aspace,
4371 CORE_ADDR pc)
4372 {
4373 struct bp_location **blp, **blp_tmp = NULL;
4374 struct bp_location *bl;
4375
4376 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4377 {
4378 struct bp_location *bl = *blp;
4379
4380 if (bl->loc_type != bp_loc_software_breakpoint)
4381 continue;
4382
4383 if (bp_location_inserted_here_p (bl, aspace, pc))
4384 return 1;
4385 }
4386
4387 return 0;
4388 }
4389
4390 /* See breakpoint.h. */
4391
4392 int
4393 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4394 CORE_ADDR pc)
4395 {
4396 struct bp_location **blp, **blp_tmp = NULL;
4397 struct bp_location *bl;
4398
4399 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4400 {
4401 struct bp_location *bl = *blp;
4402
4403 if (bl->loc_type != bp_loc_hardware_breakpoint)
4404 continue;
4405
4406 if (bp_location_inserted_here_p (bl, aspace, pc))
4407 return 1;
4408 }
4409
4410 return 0;
4411 }
4412
4413 int
4414 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4415 CORE_ADDR addr, ULONGEST len)
4416 {
4417 struct breakpoint *bpt;
4418
4419 ALL_BREAKPOINTS (bpt)
4420 {
4421 struct bp_location *loc;
4422
4423 if (bpt->type != bp_hardware_watchpoint
4424 && bpt->type != bp_access_watchpoint)
4425 continue;
4426
4427 if (!breakpoint_enabled (bpt))
4428 continue;
4429
4430 for (loc = bpt->loc; loc; loc = loc->next)
4431 if (loc->pspace->aspace == aspace && loc->inserted)
4432 {
4433 CORE_ADDR l, h;
4434
4435 /* Check for intersection. */
4436 l = max (loc->address, addr);
4437 h = min (loc->address + loc->length, addr + len);
4438 if (l < h)
4439 return 1;
4440 }
4441 }
4442 return 0;
4443 }
4444 \f
4445
4446 /* bpstat stuff. External routines' interfaces are documented
4447 in breakpoint.h. */
4448
4449 int
4450 is_catchpoint (struct breakpoint *ep)
4451 {
4452 return (ep->type == bp_catchpoint);
4453 }
4454
4455 /* Frees any storage that is part of a bpstat. Does not walk the
4456 'next' chain. */
4457
4458 static void
4459 bpstat_free (bpstat bs)
4460 {
4461 if (bs->old_val != NULL)
4462 value_free (bs->old_val);
4463 decref_counted_command_line (&bs->commands);
4464 decref_bp_location (&bs->bp_location_at);
4465 xfree (bs);
4466 }
4467
4468 /* Clear a bpstat so that it says we are not at any breakpoint.
4469 Also free any storage that is part of a bpstat. */
4470
4471 void
4472 bpstat_clear (bpstat *bsp)
4473 {
4474 bpstat p;
4475 bpstat q;
4476
4477 if (bsp == 0)
4478 return;
4479 p = *bsp;
4480 while (p != NULL)
4481 {
4482 q = p->next;
4483 bpstat_free (p);
4484 p = q;
4485 }
4486 *bsp = NULL;
4487 }
4488
4489 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4490 is part of the bpstat is copied as well. */
4491
4492 bpstat
4493 bpstat_copy (bpstat bs)
4494 {
4495 bpstat p = NULL;
4496 bpstat tmp;
4497 bpstat retval = NULL;
4498
4499 if (bs == NULL)
4500 return bs;
4501
4502 for (; bs != NULL; bs = bs->next)
4503 {
4504 tmp = (bpstat) xmalloc (sizeof (*tmp));
4505 memcpy (tmp, bs, sizeof (*tmp));
4506 incref_counted_command_line (tmp->commands);
4507 incref_bp_location (tmp->bp_location_at);
4508 if (bs->old_val != NULL)
4509 {
4510 tmp->old_val = value_copy (bs->old_val);
4511 release_value (tmp->old_val);
4512 }
4513
4514 if (p == NULL)
4515 /* This is the first thing in the chain. */
4516 retval = tmp;
4517 else
4518 p->next = tmp;
4519 p = tmp;
4520 }
4521 p->next = NULL;
4522 return retval;
4523 }
4524
4525 /* Find the bpstat associated with this breakpoint. */
4526
4527 bpstat
4528 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4529 {
4530 if (bsp == NULL)
4531 return NULL;
4532
4533 for (; bsp != NULL; bsp = bsp->next)
4534 {
4535 if (bsp->breakpoint_at == breakpoint)
4536 return bsp;
4537 }
4538 return NULL;
4539 }
4540
4541 /* See breakpoint.h. */
4542
4543 int
4544 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4545 {
4546 for (; bsp != NULL; bsp = bsp->next)
4547 {
4548 if (bsp->breakpoint_at == NULL)
4549 {
4550 /* A moribund location can never explain a signal other than
4551 GDB_SIGNAL_TRAP. */
4552 if (sig == GDB_SIGNAL_TRAP)
4553 return 1;
4554 }
4555 else
4556 {
4557 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4558 sig))
4559 return 1;
4560 }
4561 }
4562
4563 return 0;
4564 }
4565
4566 /* Put in *NUM the breakpoint number of the first breakpoint we are
4567 stopped at. *BSP upon return is a bpstat which points to the
4568 remaining breakpoints stopped at (but which is not guaranteed to be
4569 good for anything but further calls to bpstat_num).
4570
4571 Return 0 if passed a bpstat which does not indicate any breakpoints.
4572 Return -1 if stopped at a breakpoint that has been deleted since
4573 we set it.
4574 Return 1 otherwise. */
4575
4576 int
4577 bpstat_num (bpstat *bsp, int *num)
4578 {
4579 struct breakpoint *b;
4580
4581 if ((*bsp) == NULL)
4582 return 0; /* No more breakpoint values */
4583
4584 /* We assume we'll never have several bpstats that correspond to a
4585 single breakpoint -- otherwise, this function might return the
4586 same number more than once and this will look ugly. */
4587 b = (*bsp)->breakpoint_at;
4588 *bsp = (*bsp)->next;
4589 if (b == NULL)
4590 return -1; /* breakpoint that's been deleted since */
4591
4592 *num = b->number; /* We have its number */
4593 return 1;
4594 }
4595
4596 /* See breakpoint.h. */
4597
4598 void
4599 bpstat_clear_actions (void)
4600 {
4601 struct thread_info *tp;
4602 bpstat bs;
4603
4604 if (ptid_equal (inferior_ptid, null_ptid))
4605 return;
4606
4607 tp = find_thread_ptid (inferior_ptid);
4608 if (tp == NULL)
4609 return;
4610
4611 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4612 {
4613 decref_counted_command_line (&bs->commands);
4614
4615 if (bs->old_val != NULL)
4616 {
4617 value_free (bs->old_val);
4618 bs->old_val = NULL;
4619 }
4620 }
4621 }
4622
4623 /* Called when a command is about to proceed the inferior. */
4624
4625 static void
4626 breakpoint_about_to_proceed (void)
4627 {
4628 if (!ptid_equal (inferior_ptid, null_ptid))
4629 {
4630 struct thread_info *tp = inferior_thread ();
4631
4632 /* Allow inferior function calls in breakpoint commands to not
4633 interrupt the command list. When the call finishes
4634 successfully, the inferior will be standing at the same
4635 breakpoint as if nothing happened. */
4636 if (tp->control.in_infcall)
4637 return;
4638 }
4639
4640 breakpoint_proceeded = 1;
4641 }
4642
4643 /* Stub for cleaning up our state if we error-out of a breakpoint
4644 command. */
4645 static void
4646 cleanup_executing_breakpoints (void *ignore)
4647 {
4648 executing_breakpoint_commands = 0;
4649 }
4650
4651 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4652 or its equivalent. */
4653
4654 static int
4655 command_line_is_silent (struct command_line *cmd)
4656 {
4657 return cmd && (strcmp ("silent", cmd->line) == 0);
4658 }
4659
4660 /* Execute all the commands associated with all the breakpoints at
4661 this location. Any of these commands could cause the process to
4662 proceed beyond this point, etc. We look out for such changes by
4663 checking the global "breakpoint_proceeded" after each command.
4664
4665 Returns true if a breakpoint command resumed the inferior. In that
4666 case, it is the caller's responsibility to recall it again with the
4667 bpstat of the current thread. */
4668
4669 static int
4670 bpstat_do_actions_1 (bpstat *bsp)
4671 {
4672 bpstat bs;
4673 struct cleanup *old_chain;
4674 int again = 0;
4675
4676 /* Avoid endless recursion if a `source' command is contained
4677 in bs->commands. */
4678 if (executing_breakpoint_commands)
4679 return 0;
4680
4681 executing_breakpoint_commands = 1;
4682 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4683
4684 prevent_dont_repeat ();
4685
4686 /* This pointer will iterate over the list of bpstat's. */
4687 bs = *bsp;
4688
4689 breakpoint_proceeded = 0;
4690 for (; bs != NULL; bs = bs->next)
4691 {
4692 struct counted_command_line *ccmd;
4693 struct command_line *cmd;
4694 struct cleanup *this_cmd_tree_chain;
4695
4696 /* Take ownership of the BSP's command tree, if it has one.
4697
4698 The command tree could legitimately contain commands like
4699 'step' and 'next', which call clear_proceed_status, which
4700 frees stop_bpstat's command tree. To make sure this doesn't
4701 free the tree we're executing out from under us, we need to
4702 take ownership of the tree ourselves. Since a given bpstat's
4703 commands are only executed once, we don't need to copy it; we
4704 can clear the pointer in the bpstat, and make sure we free
4705 the tree when we're done. */
4706 ccmd = bs->commands;
4707 bs->commands = NULL;
4708 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4709 cmd = ccmd ? ccmd->commands : NULL;
4710 if (command_line_is_silent (cmd))
4711 {
4712 /* The action has been already done by bpstat_stop_status. */
4713 cmd = cmd->next;
4714 }
4715
4716 while (cmd != NULL)
4717 {
4718 execute_control_command (cmd);
4719
4720 if (breakpoint_proceeded)
4721 break;
4722 else
4723 cmd = cmd->next;
4724 }
4725
4726 /* We can free this command tree now. */
4727 do_cleanups (this_cmd_tree_chain);
4728
4729 if (breakpoint_proceeded)
4730 {
4731 if (interpreter_async)
4732 /* If we are in async mode, then the target might be still
4733 running, not stopped at any breakpoint, so nothing for
4734 us to do here -- just return to the event loop. */
4735 ;
4736 else
4737 /* In sync mode, when execute_control_command returns
4738 we're already standing on the next breakpoint.
4739 Breakpoint commands for that stop were not run, since
4740 execute_command does not run breakpoint commands --
4741 only command_line_handler does, but that one is not
4742 involved in execution of breakpoint commands. So, we
4743 can now execute breakpoint commands. It should be
4744 noted that making execute_command do bpstat actions is
4745 not an option -- in this case we'll have recursive
4746 invocation of bpstat for each breakpoint with a
4747 command, and can easily blow up GDB stack. Instead, we
4748 return true, which will trigger the caller to recall us
4749 with the new stop_bpstat. */
4750 again = 1;
4751 break;
4752 }
4753 }
4754 do_cleanups (old_chain);
4755 return again;
4756 }
4757
4758 void
4759 bpstat_do_actions (void)
4760 {
4761 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4762
4763 /* Do any commands attached to breakpoint we are stopped at. */
4764 while (!ptid_equal (inferior_ptid, null_ptid)
4765 && target_has_execution
4766 && !is_exited (inferior_ptid)
4767 && !is_executing (inferior_ptid))
4768 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4769 and only return when it is stopped at the next breakpoint, we
4770 keep doing breakpoint actions until it returns false to
4771 indicate the inferior was not resumed. */
4772 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4773 break;
4774
4775 discard_cleanups (cleanup_if_error);
4776 }
4777
4778 /* Print out the (old or new) value associated with a watchpoint. */
4779
4780 static void
4781 watchpoint_value_print (struct value *val, struct ui_file *stream)
4782 {
4783 if (val == NULL)
4784 fprintf_unfiltered (stream, _("<unreadable>"));
4785 else
4786 {
4787 struct value_print_options opts;
4788 get_user_print_options (&opts);
4789 value_print (val, stream, &opts);
4790 }
4791 }
4792
4793 /* Generic routine for printing messages indicating why we
4794 stopped. The behavior of this function depends on the value
4795 'print_it' in the bpstat structure. Under some circumstances we
4796 may decide not to print anything here and delegate the task to
4797 normal_stop(). */
4798
4799 static enum print_stop_action
4800 print_bp_stop_message (bpstat bs)
4801 {
4802 switch (bs->print_it)
4803 {
4804 case print_it_noop:
4805 /* Nothing should be printed for this bpstat entry. */
4806 return PRINT_UNKNOWN;
4807 break;
4808
4809 case print_it_done:
4810 /* We still want to print the frame, but we already printed the
4811 relevant messages. */
4812 return PRINT_SRC_AND_LOC;
4813 break;
4814
4815 case print_it_normal:
4816 {
4817 struct breakpoint *b = bs->breakpoint_at;
4818
4819 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4820 which has since been deleted. */
4821 if (b == NULL)
4822 return PRINT_UNKNOWN;
4823
4824 /* Normal case. Call the breakpoint's print_it method. */
4825 return b->ops->print_it (bs);
4826 }
4827 break;
4828
4829 default:
4830 internal_error (__FILE__, __LINE__,
4831 _("print_bp_stop_message: unrecognized enum value"));
4832 break;
4833 }
4834 }
4835
4836 /* A helper function that prints a shared library stopped event. */
4837
4838 static void
4839 print_solib_event (int is_catchpoint)
4840 {
4841 int any_deleted
4842 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4843 int any_added
4844 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4845
4846 if (!is_catchpoint)
4847 {
4848 if (any_added || any_deleted)
4849 ui_out_text (current_uiout,
4850 _("Stopped due to shared library event:\n"));
4851 else
4852 ui_out_text (current_uiout,
4853 _("Stopped due to shared library event (no "
4854 "libraries added or removed)\n"));
4855 }
4856
4857 if (ui_out_is_mi_like_p (current_uiout))
4858 ui_out_field_string (current_uiout, "reason",
4859 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4860
4861 if (any_deleted)
4862 {
4863 struct cleanup *cleanup;
4864 char *name;
4865 int ix;
4866
4867 ui_out_text (current_uiout, _(" Inferior unloaded "));
4868 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4869 "removed");
4870 for (ix = 0;
4871 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4872 ix, name);
4873 ++ix)
4874 {
4875 if (ix > 0)
4876 ui_out_text (current_uiout, " ");
4877 ui_out_field_string (current_uiout, "library", name);
4878 ui_out_text (current_uiout, "\n");
4879 }
4880
4881 do_cleanups (cleanup);
4882 }
4883
4884 if (any_added)
4885 {
4886 struct so_list *iter;
4887 int ix;
4888 struct cleanup *cleanup;
4889
4890 ui_out_text (current_uiout, _(" Inferior loaded "));
4891 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4892 "added");
4893 for (ix = 0;
4894 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4895 ix, iter);
4896 ++ix)
4897 {
4898 if (ix > 0)
4899 ui_out_text (current_uiout, " ");
4900 ui_out_field_string (current_uiout, "library", iter->so_name);
4901 ui_out_text (current_uiout, "\n");
4902 }
4903
4904 do_cleanups (cleanup);
4905 }
4906 }
4907
4908 /* Print a message indicating what happened. This is called from
4909 normal_stop(). The input to this routine is the head of the bpstat
4910 list - a list of the eventpoints that caused this stop. KIND is
4911 the target_waitkind for the stopping event. This
4912 routine calls the generic print routine for printing a message
4913 about reasons for stopping. This will print (for example) the
4914 "Breakpoint n," part of the output. The return value of this
4915 routine is one of:
4916
4917 PRINT_UNKNOWN: Means we printed nothing.
4918 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4919 code to print the location. An example is
4920 "Breakpoint 1, " which should be followed by
4921 the location.
4922 PRINT_SRC_ONLY: Means we printed something, but there is no need
4923 to also print the location part of the message.
4924 An example is the catch/throw messages, which
4925 don't require a location appended to the end.
4926 PRINT_NOTHING: We have done some printing and we don't need any
4927 further info to be printed. */
4928
4929 enum print_stop_action
4930 bpstat_print (bpstat bs, int kind)
4931 {
4932 enum print_stop_action val;
4933
4934 /* Maybe another breakpoint in the chain caused us to stop.
4935 (Currently all watchpoints go on the bpstat whether hit or not.
4936 That probably could (should) be changed, provided care is taken
4937 with respect to bpstat_explains_signal). */
4938 for (; bs; bs = bs->next)
4939 {
4940 val = print_bp_stop_message (bs);
4941 if (val == PRINT_SRC_ONLY
4942 || val == PRINT_SRC_AND_LOC
4943 || val == PRINT_NOTHING)
4944 return val;
4945 }
4946
4947 /* If we had hit a shared library event breakpoint,
4948 print_bp_stop_message would print out this message. If we hit an
4949 OS-level shared library event, do the same thing. */
4950 if (kind == TARGET_WAITKIND_LOADED)
4951 {
4952 print_solib_event (0);
4953 return PRINT_NOTHING;
4954 }
4955
4956 /* We reached the end of the chain, or we got a null BS to start
4957 with and nothing was printed. */
4958 return PRINT_UNKNOWN;
4959 }
4960
4961 /* Evaluate the expression EXP and return 1 if value is zero.
4962 This returns the inverse of the condition because it is called
4963 from catch_errors which returns 0 if an exception happened, and if an
4964 exception happens we want execution to stop.
4965 The argument is a "struct expression *" that has been cast to a
4966 "void *" to make it pass through catch_errors. */
4967
4968 static int
4969 breakpoint_cond_eval (void *exp)
4970 {
4971 struct value *mark = value_mark ();
4972 int i = !value_true (evaluate_expression ((struct expression *) exp));
4973
4974 value_free_to_mark (mark);
4975 return i;
4976 }
4977
4978 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4979
4980 static bpstat
4981 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4982 {
4983 bpstat bs;
4984
4985 bs = (bpstat) xmalloc (sizeof (*bs));
4986 bs->next = NULL;
4987 **bs_link_pointer = bs;
4988 *bs_link_pointer = &bs->next;
4989 bs->breakpoint_at = bl->owner;
4990 bs->bp_location_at = bl;
4991 incref_bp_location (bl);
4992 /* If the condition is false, etc., don't do the commands. */
4993 bs->commands = NULL;
4994 bs->old_val = NULL;
4995 bs->print_it = print_it_normal;
4996 return bs;
4997 }
4998 \f
4999 /* The target has stopped with waitstatus WS. Check if any hardware
5000 watchpoints have triggered, according to the target. */
5001
5002 int
5003 watchpoints_triggered (struct target_waitstatus *ws)
5004 {
5005 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5006 CORE_ADDR addr;
5007 struct breakpoint *b;
5008
5009 if (!stopped_by_watchpoint)
5010 {
5011 /* We were not stopped by a watchpoint. Mark all watchpoints
5012 as not triggered. */
5013 ALL_BREAKPOINTS (b)
5014 if (is_hardware_watchpoint (b))
5015 {
5016 struct watchpoint *w = (struct watchpoint *) b;
5017
5018 w->watchpoint_triggered = watch_triggered_no;
5019 }
5020
5021 return 0;
5022 }
5023
5024 if (!target_stopped_data_address (&current_target, &addr))
5025 {
5026 /* We were stopped by a watchpoint, but we don't know where.
5027 Mark all watchpoints as unknown. */
5028 ALL_BREAKPOINTS (b)
5029 if (is_hardware_watchpoint (b))
5030 {
5031 struct watchpoint *w = (struct watchpoint *) b;
5032
5033 w->watchpoint_triggered = watch_triggered_unknown;
5034 }
5035
5036 return 1;
5037 }
5038
5039 /* The target could report the data address. Mark watchpoints
5040 affected by this data address as triggered, and all others as not
5041 triggered. */
5042
5043 ALL_BREAKPOINTS (b)
5044 if (is_hardware_watchpoint (b))
5045 {
5046 struct watchpoint *w = (struct watchpoint *) b;
5047 struct bp_location *loc;
5048
5049 w->watchpoint_triggered = watch_triggered_no;
5050 for (loc = b->loc; loc; loc = loc->next)
5051 {
5052 if (is_masked_watchpoint (b))
5053 {
5054 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5055 CORE_ADDR start = loc->address & w->hw_wp_mask;
5056
5057 if (newaddr == start)
5058 {
5059 w->watchpoint_triggered = watch_triggered_yes;
5060 break;
5061 }
5062 }
5063 /* Exact match not required. Within range is sufficient. */
5064 else if (target_watchpoint_addr_within_range (&current_target,
5065 addr, loc->address,
5066 loc->length))
5067 {
5068 w->watchpoint_triggered = watch_triggered_yes;
5069 break;
5070 }
5071 }
5072 }
5073
5074 return 1;
5075 }
5076
5077 /* Possible return values for watchpoint_check (this can't be an enum
5078 because of check_errors). */
5079 /* The watchpoint has been deleted. */
5080 #define WP_DELETED 1
5081 /* The value has changed. */
5082 #define WP_VALUE_CHANGED 2
5083 /* The value has not changed. */
5084 #define WP_VALUE_NOT_CHANGED 3
5085 /* Ignore this watchpoint, no matter if the value changed or not. */
5086 #define WP_IGNORE 4
5087
5088 #define BP_TEMPFLAG 1
5089 #define BP_HARDWAREFLAG 2
5090
5091 /* Evaluate watchpoint condition expression and check if its value
5092 changed.
5093
5094 P should be a pointer to struct bpstat, but is defined as a void *
5095 in order for this function to be usable with catch_errors. */
5096
5097 static int
5098 watchpoint_check (void *p)
5099 {
5100 bpstat bs = (bpstat) p;
5101 struct watchpoint *b;
5102 struct frame_info *fr;
5103 int within_current_scope;
5104
5105 /* BS is built from an existing struct breakpoint. */
5106 gdb_assert (bs->breakpoint_at != NULL);
5107 b = (struct watchpoint *) bs->breakpoint_at;
5108
5109 /* If this is a local watchpoint, we only want to check if the
5110 watchpoint frame is in scope if the current thread is the thread
5111 that was used to create the watchpoint. */
5112 if (!watchpoint_in_thread_scope (b))
5113 return WP_IGNORE;
5114
5115 if (b->exp_valid_block == NULL)
5116 within_current_scope = 1;
5117 else
5118 {
5119 struct frame_info *frame = get_current_frame ();
5120 struct gdbarch *frame_arch = get_frame_arch (frame);
5121 CORE_ADDR frame_pc = get_frame_pc (frame);
5122
5123 /* stack_frame_destroyed_p() returns a non-zero value if we're
5124 still in the function but the stack frame has already been
5125 invalidated. Since we can't rely on the values of local
5126 variables after the stack has been destroyed, we are treating
5127 the watchpoint in that state as `not changed' without further
5128 checking. Don't mark watchpoints as changed if the current
5129 frame is in an epilogue - even if they are in some other
5130 frame, our view of the stack is likely to be wrong and
5131 frame_find_by_id could error out. */
5132 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5133 return WP_IGNORE;
5134
5135 fr = frame_find_by_id (b->watchpoint_frame);
5136 within_current_scope = (fr != NULL);
5137
5138 /* If we've gotten confused in the unwinder, we might have
5139 returned a frame that can't describe this variable. */
5140 if (within_current_scope)
5141 {
5142 struct symbol *function;
5143
5144 function = get_frame_function (fr);
5145 if (function == NULL
5146 || !contained_in (b->exp_valid_block,
5147 SYMBOL_BLOCK_VALUE (function)))
5148 within_current_scope = 0;
5149 }
5150
5151 if (within_current_scope)
5152 /* If we end up stopping, the current frame will get selected
5153 in normal_stop. So this call to select_frame won't affect
5154 the user. */
5155 select_frame (fr);
5156 }
5157
5158 if (within_current_scope)
5159 {
5160 /* We use value_{,free_to_}mark because it could be a *long*
5161 time before we return to the command level and call
5162 free_all_values. We can't call free_all_values because we
5163 might be in the middle of evaluating a function call. */
5164
5165 int pc = 0;
5166 struct value *mark;
5167 struct value *new_val;
5168
5169 if (is_masked_watchpoint (&b->base))
5170 /* Since we don't know the exact trigger address (from
5171 stopped_data_address), just tell the user we've triggered
5172 a mask watchpoint. */
5173 return WP_VALUE_CHANGED;
5174
5175 mark = value_mark ();
5176 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
5177
5178 if (b->val_bitsize != 0)
5179 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5180
5181 /* We use value_equal_contents instead of value_equal because
5182 the latter coerces an array to a pointer, thus comparing just
5183 the address of the array instead of its contents. This is
5184 not what we want. */
5185 if ((b->val != NULL) != (new_val != NULL)
5186 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5187 {
5188 if (new_val != NULL)
5189 {
5190 release_value (new_val);
5191 value_free_to_mark (mark);
5192 }
5193 bs->old_val = b->val;
5194 b->val = new_val;
5195 b->val_valid = 1;
5196 return WP_VALUE_CHANGED;
5197 }
5198 else
5199 {
5200 /* Nothing changed. */
5201 value_free_to_mark (mark);
5202 return WP_VALUE_NOT_CHANGED;
5203 }
5204 }
5205 else
5206 {
5207 struct ui_out *uiout = current_uiout;
5208
5209 /* This seems like the only logical thing to do because
5210 if we temporarily ignored the watchpoint, then when
5211 we reenter the block in which it is valid it contains
5212 garbage (in the case of a function, it may have two
5213 garbage values, one before and one after the prologue).
5214 So we can't even detect the first assignment to it and
5215 watch after that (since the garbage may or may not equal
5216 the first value assigned). */
5217 /* We print all the stop information in
5218 breakpoint_ops->print_it, but in this case, by the time we
5219 call breakpoint_ops->print_it this bp will be deleted
5220 already. So we have no choice but print the information
5221 here. */
5222 if (ui_out_is_mi_like_p (uiout))
5223 ui_out_field_string
5224 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5225 ui_out_text (uiout, "\nWatchpoint ");
5226 ui_out_field_int (uiout, "wpnum", b->base.number);
5227 ui_out_text (uiout,
5228 " deleted because the program has left the block in\n\
5229 which its expression is valid.\n");
5230
5231 /* Make sure the watchpoint's commands aren't executed. */
5232 decref_counted_command_line (&b->base.commands);
5233 watchpoint_del_at_next_stop (b);
5234
5235 return WP_DELETED;
5236 }
5237 }
5238
5239 /* Return true if it looks like target has stopped due to hitting
5240 breakpoint location BL. This function does not check if we should
5241 stop, only if BL explains the stop. */
5242
5243 static int
5244 bpstat_check_location (const struct bp_location *bl,
5245 struct address_space *aspace, CORE_ADDR bp_addr,
5246 const struct target_waitstatus *ws)
5247 {
5248 struct breakpoint *b = bl->owner;
5249
5250 /* BL is from an existing breakpoint. */
5251 gdb_assert (b != NULL);
5252
5253 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5254 }
5255
5256 /* Determine if the watched values have actually changed, and we
5257 should stop. If not, set BS->stop to 0. */
5258
5259 static void
5260 bpstat_check_watchpoint (bpstat bs)
5261 {
5262 const struct bp_location *bl;
5263 struct watchpoint *b;
5264
5265 /* BS is built for existing struct breakpoint. */
5266 bl = bs->bp_location_at;
5267 gdb_assert (bl != NULL);
5268 b = (struct watchpoint *) bs->breakpoint_at;
5269 gdb_assert (b != NULL);
5270
5271 {
5272 int must_check_value = 0;
5273
5274 if (b->base.type == bp_watchpoint)
5275 /* For a software watchpoint, we must always check the
5276 watched value. */
5277 must_check_value = 1;
5278 else if (b->watchpoint_triggered == watch_triggered_yes)
5279 /* We have a hardware watchpoint (read, write, or access)
5280 and the target earlier reported an address watched by
5281 this watchpoint. */
5282 must_check_value = 1;
5283 else if (b->watchpoint_triggered == watch_triggered_unknown
5284 && b->base.type == bp_hardware_watchpoint)
5285 /* We were stopped by a hardware watchpoint, but the target could
5286 not report the data address. We must check the watchpoint's
5287 value. Access and read watchpoints are out of luck; without
5288 a data address, we can't figure it out. */
5289 must_check_value = 1;
5290
5291 if (must_check_value)
5292 {
5293 char *message
5294 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5295 b->base.number);
5296 struct cleanup *cleanups = make_cleanup (xfree, message);
5297 int e = catch_errors (watchpoint_check, bs, message,
5298 RETURN_MASK_ALL);
5299 do_cleanups (cleanups);
5300 switch (e)
5301 {
5302 case WP_DELETED:
5303 /* We've already printed what needs to be printed. */
5304 bs->print_it = print_it_done;
5305 /* Stop. */
5306 break;
5307 case WP_IGNORE:
5308 bs->print_it = print_it_noop;
5309 bs->stop = 0;
5310 break;
5311 case WP_VALUE_CHANGED:
5312 if (b->base.type == bp_read_watchpoint)
5313 {
5314 /* There are two cases to consider here:
5315
5316 1. We're watching the triggered memory for reads.
5317 In that case, trust the target, and always report
5318 the watchpoint hit to the user. Even though
5319 reads don't cause value changes, the value may
5320 have changed since the last time it was read, and
5321 since we're not trapping writes, we will not see
5322 those, and as such we should ignore our notion of
5323 old value.
5324
5325 2. We're watching the triggered memory for both
5326 reads and writes. There are two ways this may
5327 happen:
5328
5329 2.1. This is a target that can't break on data
5330 reads only, but can break on accesses (reads or
5331 writes), such as e.g., x86. We detect this case
5332 at the time we try to insert read watchpoints.
5333
5334 2.2. Otherwise, the target supports read
5335 watchpoints, but, the user set an access or write
5336 watchpoint watching the same memory as this read
5337 watchpoint.
5338
5339 If we're watching memory writes as well as reads,
5340 ignore watchpoint hits when we find that the
5341 value hasn't changed, as reads don't cause
5342 changes. This still gives false positives when
5343 the program writes the same value to memory as
5344 what there was already in memory (we will confuse
5345 it for a read), but it's much better than
5346 nothing. */
5347
5348 int other_write_watchpoint = 0;
5349
5350 if (bl->watchpoint_type == hw_read)
5351 {
5352 struct breakpoint *other_b;
5353
5354 ALL_BREAKPOINTS (other_b)
5355 if (other_b->type == bp_hardware_watchpoint
5356 || other_b->type == bp_access_watchpoint)
5357 {
5358 struct watchpoint *other_w =
5359 (struct watchpoint *) other_b;
5360
5361 if (other_w->watchpoint_triggered
5362 == watch_triggered_yes)
5363 {
5364 other_write_watchpoint = 1;
5365 break;
5366 }
5367 }
5368 }
5369
5370 if (other_write_watchpoint
5371 || bl->watchpoint_type == hw_access)
5372 {
5373 /* We're watching the same memory for writes,
5374 and the value changed since the last time we
5375 updated it, so this trap must be for a write.
5376 Ignore it. */
5377 bs->print_it = print_it_noop;
5378 bs->stop = 0;
5379 }
5380 }
5381 break;
5382 case WP_VALUE_NOT_CHANGED:
5383 if (b->base.type == bp_hardware_watchpoint
5384 || b->base.type == bp_watchpoint)
5385 {
5386 /* Don't stop: write watchpoints shouldn't fire if
5387 the value hasn't changed. */
5388 bs->print_it = print_it_noop;
5389 bs->stop = 0;
5390 }
5391 /* Stop. */
5392 break;
5393 default:
5394 /* Can't happen. */
5395 case 0:
5396 /* Error from catch_errors. */
5397 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5398 watchpoint_del_at_next_stop (b);
5399 /* We've already printed what needs to be printed. */
5400 bs->print_it = print_it_done;
5401 break;
5402 }
5403 }
5404 else /* must_check_value == 0 */
5405 {
5406 /* This is a case where some watchpoint(s) triggered, but
5407 not at the address of this watchpoint, or else no
5408 watchpoint triggered after all. So don't print
5409 anything for this watchpoint. */
5410 bs->print_it = print_it_noop;
5411 bs->stop = 0;
5412 }
5413 }
5414 }
5415
5416 /* For breakpoints that are currently marked as telling gdb to stop,
5417 check conditions (condition proper, frame, thread and ignore count)
5418 of breakpoint referred to by BS. If we should not stop for this
5419 breakpoint, set BS->stop to 0. */
5420
5421 static void
5422 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5423 {
5424 const struct bp_location *bl;
5425 struct breakpoint *b;
5426 int value_is_zero = 0;
5427 struct expression *cond;
5428
5429 gdb_assert (bs->stop);
5430
5431 /* BS is built for existing struct breakpoint. */
5432 bl = bs->bp_location_at;
5433 gdb_assert (bl != NULL);
5434 b = bs->breakpoint_at;
5435 gdb_assert (b != NULL);
5436
5437 /* Even if the target evaluated the condition on its end and notified GDB, we
5438 need to do so again since GDB does not know if we stopped due to a
5439 breakpoint or a single step breakpoint. */
5440
5441 if (frame_id_p (b->frame_id)
5442 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5443 {
5444 bs->stop = 0;
5445 return;
5446 }
5447
5448 /* If this is a thread/task-specific breakpoint, don't waste cpu
5449 evaluating the condition if this isn't the specified
5450 thread/task. */
5451 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5452 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5453
5454 {
5455 bs->stop = 0;
5456 return;
5457 }
5458
5459 /* Evaluate extension language breakpoints that have a "stop" method
5460 implemented. */
5461 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5462
5463 if (is_watchpoint (b))
5464 {
5465 struct watchpoint *w = (struct watchpoint *) b;
5466
5467 cond = w->cond_exp;
5468 }
5469 else
5470 cond = bl->cond;
5471
5472 if (cond && b->disposition != disp_del_at_next_stop)
5473 {
5474 int within_current_scope = 1;
5475 struct watchpoint * w;
5476
5477 /* We use value_mark and value_free_to_mark because it could
5478 be a long time before we return to the command level and
5479 call free_all_values. We can't call free_all_values
5480 because we might be in the middle of evaluating a
5481 function call. */
5482 struct value *mark = value_mark ();
5483
5484 if (is_watchpoint (b))
5485 w = (struct watchpoint *) b;
5486 else
5487 w = NULL;
5488
5489 /* Need to select the frame, with all that implies so that
5490 the conditions will have the right context. Because we
5491 use the frame, we will not see an inlined function's
5492 variables when we arrive at a breakpoint at the start
5493 of the inlined function; the current frame will be the
5494 call site. */
5495 if (w == NULL || w->cond_exp_valid_block == NULL)
5496 select_frame (get_current_frame ());
5497 else
5498 {
5499 struct frame_info *frame;
5500
5501 /* For local watchpoint expressions, which particular
5502 instance of a local is being watched matters, so we
5503 keep track of the frame to evaluate the expression
5504 in. To evaluate the condition however, it doesn't
5505 really matter which instantiation of the function
5506 where the condition makes sense triggers the
5507 watchpoint. This allows an expression like "watch
5508 global if q > 10" set in `func', catch writes to
5509 global on all threads that call `func', or catch
5510 writes on all recursive calls of `func' by a single
5511 thread. We simply always evaluate the condition in
5512 the innermost frame that's executing where it makes
5513 sense to evaluate the condition. It seems
5514 intuitive. */
5515 frame = block_innermost_frame (w->cond_exp_valid_block);
5516 if (frame != NULL)
5517 select_frame (frame);
5518 else
5519 within_current_scope = 0;
5520 }
5521 if (within_current_scope)
5522 value_is_zero
5523 = catch_errors (breakpoint_cond_eval, cond,
5524 "Error in testing breakpoint condition:\n",
5525 RETURN_MASK_ALL);
5526 else
5527 {
5528 warning (_("Watchpoint condition cannot be tested "
5529 "in the current scope"));
5530 /* If we failed to set the right context for this
5531 watchpoint, unconditionally report it. */
5532 value_is_zero = 0;
5533 }
5534 /* FIXME-someday, should give breakpoint #. */
5535 value_free_to_mark (mark);
5536 }
5537
5538 if (cond && value_is_zero)
5539 {
5540 bs->stop = 0;
5541 }
5542 else if (b->ignore_count > 0)
5543 {
5544 b->ignore_count--;
5545 bs->stop = 0;
5546 /* Increase the hit count even though we don't stop. */
5547 ++(b->hit_count);
5548 observer_notify_breakpoint_modified (b);
5549 }
5550 }
5551
5552 /* Returns true if we need to track moribund locations of LOC's type
5553 on the current target. */
5554
5555 static int
5556 need_moribund_for_location_type (struct bp_location *loc)
5557 {
5558 return ((loc->loc_type == bp_loc_software_breakpoint
5559 && !target_supports_stopped_by_sw_breakpoint ())
5560 || (loc->loc_type == bp_loc_hardware_breakpoint
5561 && !target_supports_stopped_by_hw_breakpoint ()));
5562 }
5563
5564
5565 /* Get a bpstat associated with having just stopped at address
5566 BP_ADDR in thread PTID.
5567
5568 Determine whether we stopped at a breakpoint, etc, or whether we
5569 don't understand this stop. Result is a chain of bpstat's such
5570 that:
5571
5572 if we don't understand the stop, the result is a null pointer.
5573
5574 if we understand why we stopped, the result is not null.
5575
5576 Each element of the chain refers to a particular breakpoint or
5577 watchpoint at which we have stopped. (We may have stopped for
5578 several reasons concurrently.)
5579
5580 Each element of the chain has valid next, breakpoint_at,
5581 commands, FIXME??? fields. */
5582
5583 bpstat
5584 bpstat_stop_status (struct address_space *aspace,
5585 CORE_ADDR bp_addr, ptid_t ptid,
5586 const struct target_waitstatus *ws)
5587 {
5588 struct breakpoint *b = NULL;
5589 struct bp_location *bl;
5590 struct bp_location *loc;
5591 /* First item of allocated bpstat's. */
5592 bpstat bs_head = NULL, *bs_link = &bs_head;
5593 /* Pointer to the last thing in the chain currently. */
5594 bpstat bs;
5595 int ix;
5596 int need_remove_insert;
5597 int removed_any;
5598
5599 /* First, build the bpstat chain with locations that explain a
5600 target stop, while being careful to not set the target running,
5601 as that may invalidate locations (in particular watchpoint
5602 locations are recreated). Resuming will happen here with
5603 breakpoint conditions or watchpoint expressions that include
5604 inferior function calls. */
5605
5606 ALL_BREAKPOINTS (b)
5607 {
5608 if (!breakpoint_enabled (b))
5609 continue;
5610
5611 for (bl = b->loc; bl != NULL; bl = bl->next)
5612 {
5613 /* For hardware watchpoints, we look only at the first
5614 location. The watchpoint_check function will work on the
5615 entire expression, not the individual locations. For
5616 read watchpoints, the watchpoints_triggered function has
5617 checked all locations already. */
5618 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5619 break;
5620
5621 if (!bl->enabled || bl->shlib_disabled)
5622 continue;
5623
5624 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5625 continue;
5626
5627 /* Come here if it's a watchpoint, or if the break address
5628 matches. */
5629
5630 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5631 explain stop. */
5632
5633 /* Assume we stop. Should we find a watchpoint that is not
5634 actually triggered, or if the condition of the breakpoint
5635 evaluates as false, we'll reset 'stop' to 0. */
5636 bs->stop = 1;
5637 bs->print = 1;
5638
5639 /* If this is a scope breakpoint, mark the associated
5640 watchpoint as triggered so that we will handle the
5641 out-of-scope event. We'll get to the watchpoint next
5642 iteration. */
5643 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5644 {
5645 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5646
5647 w->watchpoint_triggered = watch_triggered_yes;
5648 }
5649 }
5650 }
5651
5652 /* Check if a moribund breakpoint explains the stop. */
5653 if (!target_supports_stopped_by_sw_breakpoint ()
5654 || !target_supports_stopped_by_hw_breakpoint ())
5655 {
5656 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5657 {
5658 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5659 && need_moribund_for_location_type (loc))
5660 {
5661 bs = bpstat_alloc (loc, &bs_link);
5662 /* For hits of moribund locations, we should just proceed. */
5663 bs->stop = 0;
5664 bs->print = 0;
5665 bs->print_it = print_it_noop;
5666 }
5667 }
5668 }
5669
5670 /* A bit of special processing for shlib breakpoints. We need to
5671 process solib loading here, so that the lists of loaded and
5672 unloaded libraries are correct before we handle "catch load" and
5673 "catch unload". */
5674 for (bs = bs_head; bs != NULL; bs = bs->next)
5675 {
5676 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5677 {
5678 handle_solib_event ();
5679 break;
5680 }
5681 }
5682
5683 /* Now go through the locations that caused the target to stop, and
5684 check whether we're interested in reporting this stop to higher
5685 layers, or whether we should resume the target transparently. */
5686
5687 removed_any = 0;
5688
5689 for (bs = bs_head; bs != NULL; bs = bs->next)
5690 {
5691 if (!bs->stop)
5692 continue;
5693
5694 b = bs->breakpoint_at;
5695 b->ops->check_status (bs);
5696 if (bs->stop)
5697 {
5698 bpstat_check_breakpoint_conditions (bs, ptid);
5699
5700 if (bs->stop)
5701 {
5702 ++(b->hit_count);
5703 observer_notify_breakpoint_modified (b);
5704
5705 /* We will stop here. */
5706 if (b->disposition == disp_disable)
5707 {
5708 --(b->enable_count);
5709 if (b->enable_count <= 0)
5710 b->enable_state = bp_disabled;
5711 removed_any = 1;
5712 }
5713 if (b->silent)
5714 bs->print = 0;
5715 bs->commands = b->commands;
5716 incref_counted_command_line (bs->commands);
5717 if (command_line_is_silent (bs->commands
5718 ? bs->commands->commands : NULL))
5719 bs->print = 0;
5720
5721 b->ops->after_condition_true (bs);
5722 }
5723
5724 }
5725
5726 /* Print nothing for this entry if we don't stop or don't
5727 print. */
5728 if (!bs->stop || !bs->print)
5729 bs->print_it = print_it_noop;
5730 }
5731
5732 /* If we aren't stopping, the value of some hardware watchpoint may
5733 not have changed, but the intermediate memory locations we are
5734 watching may have. Don't bother if we're stopping; this will get
5735 done later. */
5736 need_remove_insert = 0;
5737 if (! bpstat_causes_stop (bs_head))
5738 for (bs = bs_head; bs != NULL; bs = bs->next)
5739 if (!bs->stop
5740 && bs->breakpoint_at
5741 && is_hardware_watchpoint (bs->breakpoint_at))
5742 {
5743 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5744
5745 update_watchpoint (w, 0 /* don't reparse. */);
5746 need_remove_insert = 1;
5747 }
5748
5749 if (need_remove_insert)
5750 update_global_location_list (UGLL_MAY_INSERT);
5751 else if (removed_any)
5752 update_global_location_list (UGLL_DONT_INSERT);
5753
5754 return bs_head;
5755 }
5756
5757 static void
5758 handle_jit_event (void)
5759 {
5760 struct frame_info *frame;
5761 struct gdbarch *gdbarch;
5762
5763 if (debug_infrun)
5764 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5765
5766 /* Switch terminal for any messages produced by
5767 breakpoint_re_set. */
5768 target_terminal_ours_for_output ();
5769
5770 frame = get_current_frame ();
5771 gdbarch = get_frame_arch (frame);
5772
5773 jit_event_handler (gdbarch);
5774
5775 target_terminal_inferior ();
5776 }
5777
5778 /* Prepare WHAT final decision for infrun. */
5779
5780 /* Decide what infrun needs to do with this bpstat. */
5781
5782 struct bpstat_what
5783 bpstat_what (bpstat bs_head)
5784 {
5785 struct bpstat_what retval;
5786 int jit_event = 0;
5787 bpstat bs;
5788
5789 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5790 retval.call_dummy = STOP_NONE;
5791 retval.is_longjmp = 0;
5792
5793 for (bs = bs_head; bs != NULL; bs = bs->next)
5794 {
5795 /* Extract this BS's action. After processing each BS, we check
5796 if its action overrides all we've seem so far. */
5797 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5798 enum bptype bptype;
5799
5800 if (bs->breakpoint_at == NULL)
5801 {
5802 /* I suspect this can happen if it was a momentary
5803 breakpoint which has since been deleted. */
5804 bptype = bp_none;
5805 }
5806 else
5807 bptype = bs->breakpoint_at->type;
5808
5809 switch (bptype)
5810 {
5811 case bp_none:
5812 break;
5813 case bp_breakpoint:
5814 case bp_hardware_breakpoint:
5815 case bp_single_step:
5816 case bp_until:
5817 case bp_finish:
5818 case bp_shlib_event:
5819 if (bs->stop)
5820 {
5821 if (bs->print)
5822 this_action = BPSTAT_WHAT_STOP_NOISY;
5823 else
5824 this_action = BPSTAT_WHAT_STOP_SILENT;
5825 }
5826 else
5827 this_action = BPSTAT_WHAT_SINGLE;
5828 break;
5829 case bp_watchpoint:
5830 case bp_hardware_watchpoint:
5831 case bp_read_watchpoint:
5832 case bp_access_watchpoint:
5833 if (bs->stop)
5834 {
5835 if (bs->print)
5836 this_action = BPSTAT_WHAT_STOP_NOISY;
5837 else
5838 this_action = BPSTAT_WHAT_STOP_SILENT;
5839 }
5840 else
5841 {
5842 /* There was a watchpoint, but we're not stopping.
5843 This requires no further action. */
5844 }
5845 break;
5846 case bp_longjmp:
5847 case bp_longjmp_call_dummy:
5848 case bp_exception:
5849 if (bs->stop)
5850 {
5851 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5852 retval.is_longjmp = bptype != bp_exception;
5853 }
5854 else
5855 this_action = BPSTAT_WHAT_SINGLE;
5856 break;
5857 case bp_longjmp_resume:
5858 case bp_exception_resume:
5859 if (bs->stop)
5860 {
5861 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5862 retval.is_longjmp = bptype == bp_longjmp_resume;
5863 }
5864 else
5865 this_action = BPSTAT_WHAT_SINGLE;
5866 break;
5867 case bp_step_resume:
5868 if (bs->stop)
5869 this_action = BPSTAT_WHAT_STEP_RESUME;
5870 else
5871 {
5872 /* It is for the wrong frame. */
5873 this_action = BPSTAT_WHAT_SINGLE;
5874 }
5875 break;
5876 case bp_hp_step_resume:
5877 if (bs->stop)
5878 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5879 else
5880 {
5881 /* It is for the wrong frame. */
5882 this_action = BPSTAT_WHAT_SINGLE;
5883 }
5884 break;
5885 case bp_watchpoint_scope:
5886 case bp_thread_event:
5887 case bp_overlay_event:
5888 case bp_longjmp_master:
5889 case bp_std_terminate_master:
5890 case bp_exception_master:
5891 this_action = BPSTAT_WHAT_SINGLE;
5892 break;
5893 case bp_catchpoint:
5894 if (bs->stop)
5895 {
5896 if (bs->print)
5897 this_action = BPSTAT_WHAT_STOP_NOISY;
5898 else
5899 this_action = BPSTAT_WHAT_STOP_SILENT;
5900 }
5901 else
5902 {
5903 /* There was a catchpoint, but we're not stopping.
5904 This requires no further action. */
5905 }
5906 break;
5907 case bp_jit_event:
5908 jit_event = 1;
5909 this_action = BPSTAT_WHAT_SINGLE;
5910 break;
5911 case bp_call_dummy:
5912 /* Make sure the action is stop (silent or noisy),
5913 so infrun.c pops the dummy frame. */
5914 retval.call_dummy = STOP_STACK_DUMMY;
5915 this_action = BPSTAT_WHAT_STOP_SILENT;
5916 break;
5917 case bp_std_terminate:
5918 /* Make sure the action is stop (silent or noisy),
5919 so infrun.c pops the dummy frame. */
5920 retval.call_dummy = STOP_STD_TERMINATE;
5921 this_action = BPSTAT_WHAT_STOP_SILENT;
5922 break;
5923 case bp_tracepoint:
5924 case bp_fast_tracepoint:
5925 case bp_static_tracepoint:
5926 /* Tracepoint hits should not be reported back to GDB, and
5927 if one got through somehow, it should have been filtered
5928 out already. */
5929 internal_error (__FILE__, __LINE__,
5930 _("bpstat_what: tracepoint encountered"));
5931 break;
5932 case bp_gnu_ifunc_resolver:
5933 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5934 this_action = BPSTAT_WHAT_SINGLE;
5935 break;
5936 case bp_gnu_ifunc_resolver_return:
5937 /* The breakpoint will be removed, execution will restart from the
5938 PC of the former breakpoint. */
5939 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5940 break;
5941
5942 case bp_dprintf:
5943 if (bs->stop)
5944 this_action = BPSTAT_WHAT_STOP_SILENT;
5945 else
5946 this_action = BPSTAT_WHAT_SINGLE;
5947 break;
5948
5949 default:
5950 internal_error (__FILE__, __LINE__,
5951 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5952 }
5953
5954 retval.main_action = max (retval.main_action, this_action);
5955 }
5956
5957 return retval;
5958 }
5959
5960 void
5961 bpstat_run_callbacks (bpstat bs_head)
5962 {
5963 bpstat bs;
5964
5965 for (bs = bs_head; bs != NULL; bs = bs->next)
5966 {
5967 struct breakpoint *b = bs->breakpoint_at;
5968
5969 if (b == NULL)
5970 continue;
5971 switch (b->type)
5972 {
5973 case bp_jit_event:
5974 handle_jit_event ();
5975 break;
5976 case bp_gnu_ifunc_resolver:
5977 gnu_ifunc_resolver_stop (b);
5978 break;
5979 case bp_gnu_ifunc_resolver_return:
5980 gnu_ifunc_resolver_return_stop (b);
5981 break;
5982 }
5983 }
5984 }
5985
5986 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5987 without hardware support). This isn't related to a specific bpstat,
5988 just to things like whether watchpoints are set. */
5989
5990 int
5991 bpstat_should_step (void)
5992 {
5993 struct breakpoint *b;
5994
5995 ALL_BREAKPOINTS (b)
5996 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5997 return 1;
5998 return 0;
5999 }
6000
6001 int
6002 bpstat_causes_stop (bpstat bs)
6003 {
6004 for (; bs != NULL; bs = bs->next)
6005 if (bs->stop)
6006 return 1;
6007
6008 return 0;
6009 }
6010
6011 \f
6012
6013 /* Compute a string of spaces suitable to indent the next line
6014 so it starts at the position corresponding to the table column
6015 named COL_NAME in the currently active table of UIOUT. */
6016
6017 static char *
6018 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6019 {
6020 static char wrap_indent[80];
6021 int i, total_width, width, align;
6022 char *text;
6023
6024 total_width = 0;
6025 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6026 {
6027 if (strcmp (text, col_name) == 0)
6028 {
6029 gdb_assert (total_width < sizeof wrap_indent);
6030 memset (wrap_indent, ' ', total_width);
6031 wrap_indent[total_width] = 0;
6032
6033 return wrap_indent;
6034 }
6035
6036 total_width += width + 1;
6037 }
6038
6039 return NULL;
6040 }
6041
6042 /* Determine if the locations of this breakpoint will have their conditions
6043 evaluated by the target, host or a mix of both. Returns the following:
6044
6045 "host": Host evals condition.
6046 "host or target": Host or Target evals condition.
6047 "target": Target evals condition.
6048 */
6049
6050 static const char *
6051 bp_condition_evaluator (struct breakpoint *b)
6052 {
6053 struct bp_location *bl;
6054 char host_evals = 0;
6055 char target_evals = 0;
6056
6057 if (!b)
6058 return NULL;
6059
6060 if (!is_breakpoint (b))
6061 return NULL;
6062
6063 if (gdb_evaluates_breakpoint_condition_p ()
6064 || !target_supports_evaluation_of_breakpoint_conditions ())
6065 return condition_evaluation_host;
6066
6067 for (bl = b->loc; bl; bl = bl->next)
6068 {
6069 if (bl->cond_bytecode)
6070 target_evals++;
6071 else
6072 host_evals++;
6073 }
6074
6075 if (host_evals && target_evals)
6076 return condition_evaluation_both;
6077 else if (target_evals)
6078 return condition_evaluation_target;
6079 else
6080 return condition_evaluation_host;
6081 }
6082
6083 /* Determine the breakpoint location's condition evaluator. This is
6084 similar to bp_condition_evaluator, but for locations. */
6085
6086 static const char *
6087 bp_location_condition_evaluator (struct bp_location *bl)
6088 {
6089 if (bl && !is_breakpoint (bl->owner))
6090 return NULL;
6091
6092 if (gdb_evaluates_breakpoint_condition_p ()
6093 || !target_supports_evaluation_of_breakpoint_conditions ())
6094 return condition_evaluation_host;
6095
6096 if (bl && bl->cond_bytecode)
6097 return condition_evaluation_target;
6098 else
6099 return condition_evaluation_host;
6100 }
6101
6102 /* Print the LOC location out of the list of B->LOC locations. */
6103
6104 static void
6105 print_breakpoint_location (struct breakpoint *b,
6106 struct bp_location *loc)
6107 {
6108 struct ui_out *uiout = current_uiout;
6109 struct cleanup *old_chain = save_current_program_space ();
6110
6111 if (loc != NULL && loc->shlib_disabled)
6112 loc = NULL;
6113
6114 if (loc != NULL)
6115 set_current_program_space (loc->pspace);
6116
6117 if (b->display_canonical)
6118 ui_out_field_string (uiout, "what",
6119 event_location_to_string (b->location));
6120 else if (loc && loc->symtab)
6121 {
6122 struct symbol *sym
6123 = find_pc_sect_function (loc->address, loc->section);
6124 if (sym)
6125 {
6126 ui_out_text (uiout, "in ");
6127 ui_out_field_string (uiout, "func",
6128 SYMBOL_PRINT_NAME (sym));
6129 ui_out_text (uiout, " ");
6130 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6131 ui_out_text (uiout, "at ");
6132 }
6133 ui_out_field_string (uiout, "file",
6134 symtab_to_filename_for_display (loc->symtab));
6135 ui_out_text (uiout, ":");
6136
6137 if (ui_out_is_mi_like_p (uiout))
6138 ui_out_field_string (uiout, "fullname",
6139 symtab_to_fullname (loc->symtab));
6140
6141 ui_out_field_int (uiout, "line", loc->line_number);
6142 }
6143 else if (loc)
6144 {
6145 struct ui_file *stb = mem_fileopen ();
6146 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6147
6148 print_address_symbolic (loc->gdbarch, loc->address, stb,
6149 demangle, "");
6150 ui_out_field_stream (uiout, "at", stb);
6151
6152 do_cleanups (stb_chain);
6153 }
6154 else
6155 {
6156 ui_out_field_string (uiout, "pending",
6157 event_location_to_string (b->location));
6158 /* If extra_string is available, it could be holding a condition
6159 or dprintf arguments. In either case, make sure it is printed,
6160 too, but only for non-MI streams. */
6161 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6162 {
6163 if (b->type == bp_dprintf)
6164 ui_out_text (uiout, ",");
6165 else
6166 ui_out_text (uiout, " ");
6167 ui_out_text (uiout, b->extra_string);
6168 }
6169 }
6170
6171 if (loc && is_breakpoint (b)
6172 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6173 && bp_condition_evaluator (b) == condition_evaluation_both)
6174 {
6175 ui_out_text (uiout, " (");
6176 ui_out_field_string (uiout, "evaluated-by",
6177 bp_location_condition_evaluator (loc));
6178 ui_out_text (uiout, ")");
6179 }
6180
6181 do_cleanups (old_chain);
6182 }
6183
6184 static const char *
6185 bptype_string (enum bptype type)
6186 {
6187 struct ep_type_description
6188 {
6189 enum bptype type;
6190 char *description;
6191 };
6192 static struct ep_type_description bptypes[] =
6193 {
6194 {bp_none, "?deleted?"},
6195 {bp_breakpoint, "breakpoint"},
6196 {bp_hardware_breakpoint, "hw breakpoint"},
6197 {bp_single_step, "sw single-step"},
6198 {bp_until, "until"},
6199 {bp_finish, "finish"},
6200 {bp_watchpoint, "watchpoint"},
6201 {bp_hardware_watchpoint, "hw watchpoint"},
6202 {bp_read_watchpoint, "read watchpoint"},
6203 {bp_access_watchpoint, "acc watchpoint"},
6204 {bp_longjmp, "longjmp"},
6205 {bp_longjmp_resume, "longjmp resume"},
6206 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6207 {bp_exception, "exception"},
6208 {bp_exception_resume, "exception resume"},
6209 {bp_step_resume, "step resume"},
6210 {bp_hp_step_resume, "high-priority step resume"},
6211 {bp_watchpoint_scope, "watchpoint scope"},
6212 {bp_call_dummy, "call dummy"},
6213 {bp_std_terminate, "std::terminate"},
6214 {bp_shlib_event, "shlib events"},
6215 {bp_thread_event, "thread events"},
6216 {bp_overlay_event, "overlay events"},
6217 {bp_longjmp_master, "longjmp master"},
6218 {bp_std_terminate_master, "std::terminate master"},
6219 {bp_exception_master, "exception master"},
6220 {bp_catchpoint, "catchpoint"},
6221 {bp_tracepoint, "tracepoint"},
6222 {bp_fast_tracepoint, "fast tracepoint"},
6223 {bp_static_tracepoint, "static tracepoint"},
6224 {bp_dprintf, "dprintf"},
6225 {bp_jit_event, "jit events"},
6226 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6227 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6228 };
6229
6230 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6231 || ((int) type != bptypes[(int) type].type))
6232 internal_error (__FILE__, __LINE__,
6233 _("bptypes table does not describe type #%d."),
6234 (int) type);
6235
6236 return bptypes[(int) type].description;
6237 }
6238
6239 /* For MI, output a field named 'thread-groups' with a list as the value.
6240 For CLI, prefix the list with the string 'inf'. */
6241
6242 static void
6243 output_thread_groups (struct ui_out *uiout,
6244 const char *field_name,
6245 VEC(int) *inf_num,
6246 int mi_only)
6247 {
6248 struct cleanup *back_to;
6249 int is_mi = ui_out_is_mi_like_p (uiout);
6250 int inf;
6251 int i;
6252
6253 /* For backward compatibility, don't display inferiors in CLI unless
6254 there are several. Always display them for MI. */
6255 if (!is_mi && mi_only)
6256 return;
6257
6258 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6259
6260 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6261 {
6262 if (is_mi)
6263 {
6264 char mi_group[10];
6265
6266 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6267 ui_out_field_string (uiout, NULL, mi_group);
6268 }
6269 else
6270 {
6271 if (i == 0)
6272 ui_out_text (uiout, " inf ");
6273 else
6274 ui_out_text (uiout, ", ");
6275
6276 ui_out_text (uiout, plongest (inf));
6277 }
6278 }
6279
6280 do_cleanups (back_to);
6281 }
6282
6283 /* Print B to gdb_stdout. */
6284
6285 static void
6286 print_one_breakpoint_location (struct breakpoint *b,
6287 struct bp_location *loc,
6288 int loc_number,
6289 struct bp_location **last_loc,
6290 int allflag)
6291 {
6292 struct command_line *l;
6293 static char bpenables[] = "nynny";
6294
6295 struct ui_out *uiout = current_uiout;
6296 int header_of_multiple = 0;
6297 int part_of_multiple = (loc != NULL);
6298 struct value_print_options opts;
6299
6300 get_user_print_options (&opts);
6301
6302 gdb_assert (!loc || loc_number != 0);
6303 /* See comment in print_one_breakpoint concerning treatment of
6304 breakpoints with single disabled location. */
6305 if (loc == NULL
6306 && (b->loc != NULL
6307 && (b->loc->next != NULL || !b->loc->enabled)))
6308 header_of_multiple = 1;
6309 if (loc == NULL)
6310 loc = b->loc;
6311
6312 annotate_record ();
6313
6314 /* 1 */
6315 annotate_field (0);
6316 if (part_of_multiple)
6317 {
6318 char *formatted;
6319 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6320 ui_out_field_string (uiout, "number", formatted);
6321 xfree (formatted);
6322 }
6323 else
6324 {
6325 ui_out_field_int (uiout, "number", b->number);
6326 }
6327
6328 /* 2 */
6329 annotate_field (1);
6330 if (part_of_multiple)
6331 ui_out_field_skip (uiout, "type");
6332 else
6333 ui_out_field_string (uiout, "type", bptype_string (b->type));
6334
6335 /* 3 */
6336 annotate_field (2);
6337 if (part_of_multiple)
6338 ui_out_field_skip (uiout, "disp");
6339 else
6340 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6341
6342
6343 /* 4 */
6344 annotate_field (3);
6345 if (part_of_multiple)
6346 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6347 else
6348 ui_out_field_fmt (uiout, "enabled", "%c",
6349 bpenables[(int) b->enable_state]);
6350 ui_out_spaces (uiout, 2);
6351
6352
6353 /* 5 and 6 */
6354 if (b->ops != NULL && b->ops->print_one != NULL)
6355 {
6356 /* Although the print_one can possibly print all locations,
6357 calling it here is not likely to get any nice result. So,
6358 make sure there's just one location. */
6359 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6360 b->ops->print_one (b, last_loc);
6361 }
6362 else
6363 switch (b->type)
6364 {
6365 case bp_none:
6366 internal_error (__FILE__, __LINE__,
6367 _("print_one_breakpoint: bp_none encountered\n"));
6368 break;
6369
6370 case bp_watchpoint:
6371 case bp_hardware_watchpoint:
6372 case bp_read_watchpoint:
6373 case bp_access_watchpoint:
6374 {
6375 struct watchpoint *w = (struct watchpoint *) b;
6376
6377 /* Field 4, the address, is omitted (which makes the columns
6378 not line up too nicely with the headers, but the effect
6379 is relatively readable). */
6380 if (opts.addressprint)
6381 ui_out_field_skip (uiout, "addr");
6382 annotate_field (5);
6383 ui_out_field_string (uiout, "what", w->exp_string);
6384 }
6385 break;
6386
6387 case bp_breakpoint:
6388 case bp_hardware_breakpoint:
6389 case bp_single_step:
6390 case bp_until:
6391 case bp_finish:
6392 case bp_longjmp:
6393 case bp_longjmp_resume:
6394 case bp_longjmp_call_dummy:
6395 case bp_exception:
6396 case bp_exception_resume:
6397 case bp_step_resume:
6398 case bp_hp_step_resume:
6399 case bp_watchpoint_scope:
6400 case bp_call_dummy:
6401 case bp_std_terminate:
6402 case bp_shlib_event:
6403 case bp_thread_event:
6404 case bp_overlay_event:
6405 case bp_longjmp_master:
6406 case bp_std_terminate_master:
6407 case bp_exception_master:
6408 case bp_tracepoint:
6409 case bp_fast_tracepoint:
6410 case bp_static_tracepoint:
6411 case bp_dprintf:
6412 case bp_jit_event:
6413 case bp_gnu_ifunc_resolver:
6414 case bp_gnu_ifunc_resolver_return:
6415 if (opts.addressprint)
6416 {
6417 annotate_field (4);
6418 if (header_of_multiple)
6419 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6420 else if (b->loc == NULL || loc->shlib_disabled)
6421 ui_out_field_string (uiout, "addr", "<PENDING>");
6422 else
6423 ui_out_field_core_addr (uiout, "addr",
6424 loc->gdbarch, loc->address);
6425 }
6426 annotate_field (5);
6427 if (!header_of_multiple)
6428 print_breakpoint_location (b, loc);
6429 if (b->loc)
6430 *last_loc = b->loc;
6431 break;
6432 }
6433
6434
6435 if (loc != NULL && !header_of_multiple)
6436 {
6437 struct inferior *inf;
6438 VEC(int) *inf_num = NULL;
6439 int mi_only = 1;
6440
6441 ALL_INFERIORS (inf)
6442 {
6443 if (inf->pspace == loc->pspace)
6444 VEC_safe_push (int, inf_num, inf->num);
6445 }
6446
6447 /* For backward compatibility, don't display inferiors in CLI unless
6448 there are several. Always display for MI. */
6449 if (allflag
6450 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6451 && (number_of_program_spaces () > 1
6452 || number_of_inferiors () > 1)
6453 /* LOC is for existing B, it cannot be in
6454 moribund_locations and thus having NULL OWNER. */
6455 && loc->owner->type != bp_catchpoint))
6456 mi_only = 0;
6457 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6458 VEC_free (int, inf_num);
6459 }
6460
6461 if (!part_of_multiple)
6462 {
6463 if (b->thread != -1)
6464 {
6465 /* FIXME: This seems to be redundant and lost here; see the
6466 "stop only in" line a little further down. */
6467 ui_out_text (uiout, " thread ");
6468 ui_out_field_int (uiout, "thread", b->thread);
6469 }
6470 else if (b->task != 0)
6471 {
6472 ui_out_text (uiout, " task ");
6473 ui_out_field_int (uiout, "task", b->task);
6474 }
6475 }
6476
6477 ui_out_text (uiout, "\n");
6478
6479 if (!part_of_multiple)
6480 b->ops->print_one_detail (b, uiout);
6481
6482 if (part_of_multiple && frame_id_p (b->frame_id))
6483 {
6484 annotate_field (6);
6485 ui_out_text (uiout, "\tstop only in stack frame at ");
6486 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6487 the frame ID. */
6488 ui_out_field_core_addr (uiout, "frame",
6489 b->gdbarch, b->frame_id.stack_addr);
6490 ui_out_text (uiout, "\n");
6491 }
6492
6493 if (!part_of_multiple && b->cond_string)
6494 {
6495 annotate_field (7);
6496 if (is_tracepoint (b))
6497 ui_out_text (uiout, "\ttrace only if ");
6498 else
6499 ui_out_text (uiout, "\tstop only if ");
6500 ui_out_field_string (uiout, "cond", b->cond_string);
6501
6502 /* Print whether the target is doing the breakpoint's condition
6503 evaluation. If GDB is doing the evaluation, don't print anything. */
6504 if (is_breakpoint (b)
6505 && breakpoint_condition_evaluation_mode ()
6506 == condition_evaluation_target)
6507 {
6508 ui_out_text (uiout, " (");
6509 ui_out_field_string (uiout, "evaluated-by",
6510 bp_condition_evaluator (b));
6511 ui_out_text (uiout, " evals)");
6512 }
6513 ui_out_text (uiout, "\n");
6514 }
6515
6516 if (!part_of_multiple && b->thread != -1)
6517 {
6518 /* FIXME should make an annotation for this. */
6519 ui_out_text (uiout, "\tstop only in thread ");
6520 if (ui_out_is_mi_like_p (uiout))
6521 ui_out_field_int (uiout, "thread", b->thread);
6522 else
6523 {
6524 struct thread_info *thr = find_thread_global_id (b->thread);
6525
6526 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6527 }
6528 ui_out_text (uiout, "\n");
6529 }
6530
6531 if (!part_of_multiple)
6532 {
6533 if (b->hit_count)
6534 {
6535 /* FIXME should make an annotation for this. */
6536 if (is_catchpoint (b))
6537 ui_out_text (uiout, "\tcatchpoint");
6538 else if (is_tracepoint (b))
6539 ui_out_text (uiout, "\ttracepoint");
6540 else
6541 ui_out_text (uiout, "\tbreakpoint");
6542 ui_out_text (uiout, " already hit ");
6543 ui_out_field_int (uiout, "times", b->hit_count);
6544 if (b->hit_count == 1)
6545 ui_out_text (uiout, " time\n");
6546 else
6547 ui_out_text (uiout, " times\n");
6548 }
6549 else
6550 {
6551 /* Output the count also if it is zero, but only if this is mi. */
6552 if (ui_out_is_mi_like_p (uiout))
6553 ui_out_field_int (uiout, "times", b->hit_count);
6554 }
6555 }
6556
6557 if (!part_of_multiple && b->ignore_count)
6558 {
6559 annotate_field (8);
6560 ui_out_text (uiout, "\tignore next ");
6561 ui_out_field_int (uiout, "ignore", b->ignore_count);
6562 ui_out_text (uiout, " hits\n");
6563 }
6564
6565 /* Note that an enable count of 1 corresponds to "enable once"
6566 behavior, which is reported by the combination of enablement and
6567 disposition, so we don't need to mention it here. */
6568 if (!part_of_multiple && b->enable_count > 1)
6569 {
6570 annotate_field (8);
6571 ui_out_text (uiout, "\tdisable after ");
6572 /* Tweak the wording to clarify that ignore and enable counts
6573 are distinct, and have additive effect. */
6574 if (b->ignore_count)
6575 ui_out_text (uiout, "additional ");
6576 else
6577 ui_out_text (uiout, "next ");
6578 ui_out_field_int (uiout, "enable", b->enable_count);
6579 ui_out_text (uiout, " hits\n");
6580 }
6581
6582 if (!part_of_multiple && is_tracepoint (b))
6583 {
6584 struct tracepoint *tp = (struct tracepoint *) b;
6585
6586 if (tp->traceframe_usage)
6587 {
6588 ui_out_text (uiout, "\ttrace buffer usage ");
6589 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6590 ui_out_text (uiout, " bytes\n");
6591 }
6592 }
6593
6594 l = b->commands ? b->commands->commands : NULL;
6595 if (!part_of_multiple && l)
6596 {
6597 struct cleanup *script_chain;
6598
6599 annotate_field (9);
6600 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6601 print_command_lines (uiout, l, 4);
6602 do_cleanups (script_chain);
6603 }
6604
6605 if (is_tracepoint (b))
6606 {
6607 struct tracepoint *t = (struct tracepoint *) b;
6608
6609 if (!part_of_multiple && t->pass_count)
6610 {
6611 annotate_field (10);
6612 ui_out_text (uiout, "\tpass count ");
6613 ui_out_field_int (uiout, "pass", t->pass_count);
6614 ui_out_text (uiout, " \n");
6615 }
6616
6617 /* Don't display it when tracepoint or tracepoint location is
6618 pending. */
6619 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6620 {
6621 annotate_field (11);
6622
6623 if (ui_out_is_mi_like_p (uiout))
6624 ui_out_field_string (uiout, "installed",
6625 loc->inserted ? "y" : "n");
6626 else
6627 {
6628 if (loc->inserted)
6629 ui_out_text (uiout, "\t");
6630 else
6631 ui_out_text (uiout, "\tnot ");
6632 ui_out_text (uiout, "installed on target\n");
6633 }
6634 }
6635 }
6636
6637 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6638 {
6639 if (is_watchpoint (b))
6640 {
6641 struct watchpoint *w = (struct watchpoint *) b;
6642
6643 ui_out_field_string (uiout, "original-location", w->exp_string);
6644 }
6645 else if (b->location != NULL
6646 && event_location_to_string (b->location) != NULL)
6647 ui_out_field_string (uiout, "original-location",
6648 event_location_to_string (b->location));
6649 }
6650 }
6651
6652 static void
6653 print_one_breakpoint (struct breakpoint *b,
6654 struct bp_location **last_loc,
6655 int allflag)
6656 {
6657 struct cleanup *bkpt_chain;
6658 struct ui_out *uiout = current_uiout;
6659
6660 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6661
6662 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6663 do_cleanups (bkpt_chain);
6664
6665 /* If this breakpoint has custom print function,
6666 it's already printed. Otherwise, print individual
6667 locations, if any. */
6668 if (b->ops == NULL || b->ops->print_one == NULL)
6669 {
6670 /* If breakpoint has a single location that is disabled, we
6671 print it as if it had several locations, since otherwise it's
6672 hard to represent "breakpoint enabled, location disabled"
6673 situation.
6674
6675 Note that while hardware watchpoints have several locations
6676 internally, that's not a property exposed to user. */
6677 if (b->loc
6678 && !is_hardware_watchpoint (b)
6679 && (b->loc->next || !b->loc->enabled))
6680 {
6681 struct bp_location *loc;
6682 int n = 1;
6683
6684 for (loc = b->loc; loc; loc = loc->next, ++n)
6685 {
6686 struct cleanup *inner2 =
6687 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6688 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6689 do_cleanups (inner2);
6690 }
6691 }
6692 }
6693 }
6694
6695 static int
6696 breakpoint_address_bits (struct breakpoint *b)
6697 {
6698 int print_address_bits = 0;
6699 struct bp_location *loc;
6700
6701 /* Software watchpoints that aren't watching memory don't have an
6702 address to print. */
6703 if (is_no_memory_software_watchpoint (b))
6704 return 0;
6705
6706 for (loc = b->loc; loc; loc = loc->next)
6707 {
6708 int addr_bit;
6709
6710 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6711 if (addr_bit > print_address_bits)
6712 print_address_bits = addr_bit;
6713 }
6714
6715 return print_address_bits;
6716 }
6717
6718 struct captured_breakpoint_query_args
6719 {
6720 int bnum;
6721 };
6722
6723 static int
6724 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6725 {
6726 struct captured_breakpoint_query_args *args
6727 = (struct captured_breakpoint_query_args *) data;
6728 struct breakpoint *b;
6729 struct bp_location *dummy_loc = NULL;
6730
6731 ALL_BREAKPOINTS (b)
6732 {
6733 if (args->bnum == b->number)
6734 {
6735 print_one_breakpoint (b, &dummy_loc, 0);
6736 return GDB_RC_OK;
6737 }
6738 }
6739 return GDB_RC_NONE;
6740 }
6741
6742 enum gdb_rc
6743 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6744 char **error_message)
6745 {
6746 struct captured_breakpoint_query_args args;
6747
6748 args.bnum = bnum;
6749 /* For the moment we don't trust print_one_breakpoint() to not throw
6750 an error. */
6751 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6752 error_message, RETURN_MASK_ALL) < 0)
6753 return GDB_RC_FAIL;
6754 else
6755 return GDB_RC_OK;
6756 }
6757
6758 /* Return true if this breakpoint was set by the user, false if it is
6759 internal or momentary. */
6760
6761 int
6762 user_breakpoint_p (struct breakpoint *b)
6763 {
6764 return b->number > 0;
6765 }
6766
6767 /* Print information on user settable breakpoint (watchpoint, etc)
6768 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6769 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6770 FILTER is non-NULL, call it on each breakpoint and only include the
6771 ones for which it returns non-zero. Return the total number of
6772 breakpoints listed. */
6773
6774 static int
6775 breakpoint_1 (char *args, int allflag,
6776 int (*filter) (const struct breakpoint *))
6777 {
6778 struct breakpoint *b;
6779 struct bp_location *last_loc = NULL;
6780 int nr_printable_breakpoints;
6781 struct cleanup *bkpttbl_chain;
6782 struct value_print_options opts;
6783 int print_address_bits = 0;
6784 int print_type_col_width = 14;
6785 struct ui_out *uiout = current_uiout;
6786
6787 get_user_print_options (&opts);
6788
6789 /* Compute the number of rows in the table, as well as the size
6790 required for address fields. */
6791 nr_printable_breakpoints = 0;
6792 ALL_BREAKPOINTS (b)
6793 {
6794 /* If we have a filter, only list the breakpoints it accepts. */
6795 if (filter && !filter (b))
6796 continue;
6797
6798 /* If we have an "args" string, it is a list of breakpoints to
6799 accept. Skip the others. */
6800 if (args != NULL && *args != '\0')
6801 {
6802 if (allflag && parse_and_eval_long (args) != b->number)
6803 continue;
6804 if (!allflag && !number_is_in_list (args, b->number))
6805 continue;
6806 }
6807
6808 if (allflag || user_breakpoint_p (b))
6809 {
6810 int addr_bit, type_len;
6811
6812 addr_bit = breakpoint_address_bits (b);
6813 if (addr_bit > print_address_bits)
6814 print_address_bits = addr_bit;
6815
6816 type_len = strlen (bptype_string (b->type));
6817 if (type_len > print_type_col_width)
6818 print_type_col_width = type_len;
6819
6820 nr_printable_breakpoints++;
6821 }
6822 }
6823
6824 if (opts.addressprint)
6825 bkpttbl_chain
6826 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6827 nr_printable_breakpoints,
6828 "BreakpointTable");
6829 else
6830 bkpttbl_chain
6831 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6832 nr_printable_breakpoints,
6833 "BreakpointTable");
6834
6835 if (nr_printable_breakpoints > 0)
6836 annotate_breakpoints_headers ();
6837 if (nr_printable_breakpoints > 0)
6838 annotate_field (0);
6839 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6840 if (nr_printable_breakpoints > 0)
6841 annotate_field (1);
6842 ui_out_table_header (uiout, print_type_col_width, ui_left,
6843 "type", "Type"); /* 2 */
6844 if (nr_printable_breakpoints > 0)
6845 annotate_field (2);
6846 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6847 if (nr_printable_breakpoints > 0)
6848 annotate_field (3);
6849 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6850 if (opts.addressprint)
6851 {
6852 if (nr_printable_breakpoints > 0)
6853 annotate_field (4);
6854 if (print_address_bits <= 32)
6855 ui_out_table_header (uiout, 10, ui_left,
6856 "addr", "Address"); /* 5 */
6857 else
6858 ui_out_table_header (uiout, 18, ui_left,
6859 "addr", "Address"); /* 5 */
6860 }
6861 if (nr_printable_breakpoints > 0)
6862 annotate_field (5);
6863 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6864 ui_out_table_body (uiout);
6865 if (nr_printable_breakpoints > 0)
6866 annotate_breakpoints_table ();
6867
6868 ALL_BREAKPOINTS (b)
6869 {
6870 QUIT;
6871 /* If we have a filter, only list the breakpoints it accepts. */
6872 if (filter && !filter (b))
6873 continue;
6874
6875 /* If we have an "args" string, it is a list of breakpoints to
6876 accept. Skip the others. */
6877
6878 if (args != NULL && *args != '\0')
6879 {
6880 if (allflag) /* maintenance info breakpoint */
6881 {
6882 if (parse_and_eval_long (args) != b->number)
6883 continue;
6884 }
6885 else /* all others */
6886 {
6887 if (!number_is_in_list (args, b->number))
6888 continue;
6889 }
6890 }
6891 /* We only print out user settable breakpoints unless the
6892 allflag is set. */
6893 if (allflag || user_breakpoint_p (b))
6894 print_one_breakpoint (b, &last_loc, allflag);
6895 }
6896
6897 do_cleanups (bkpttbl_chain);
6898
6899 if (nr_printable_breakpoints == 0)
6900 {
6901 /* If there's a filter, let the caller decide how to report
6902 empty list. */
6903 if (!filter)
6904 {
6905 if (args == NULL || *args == '\0')
6906 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6907 else
6908 ui_out_message (uiout, 0,
6909 "No breakpoint or watchpoint matching '%s'.\n",
6910 args);
6911 }
6912 }
6913 else
6914 {
6915 if (last_loc && !server_command)
6916 set_next_address (last_loc->gdbarch, last_loc->address);
6917 }
6918
6919 /* FIXME? Should this be moved up so that it is only called when
6920 there have been breakpoints? */
6921 annotate_breakpoints_table_end ();
6922
6923 return nr_printable_breakpoints;
6924 }
6925
6926 /* Display the value of default-collect in a way that is generally
6927 compatible with the breakpoint list. */
6928
6929 static void
6930 default_collect_info (void)
6931 {
6932 struct ui_out *uiout = current_uiout;
6933
6934 /* If it has no value (which is frequently the case), say nothing; a
6935 message like "No default-collect." gets in user's face when it's
6936 not wanted. */
6937 if (!*default_collect)
6938 return;
6939
6940 /* The following phrase lines up nicely with per-tracepoint collect
6941 actions. */
6942 ui_out_text (uiout, "default collect ");
6943 ui_out_field_string (uiout, "default-collect", default_collect);
6944 ui_out_text (uiout, " \n");
6945 }
6946
6947 static void
6948 breakpoints_info (char *args, int from_tty)
6949 {
6950 breakpoint_1 (args, 0, NULL);
6951
6952 default_collect_info ();
6953 }
6954
6955 static void
6956 watchpoints_info (char *args, int from_tty)
6957 {
6958 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6959 struct ui_out *uiout = current_uiout;
6960
6961 if (num_printed == 0)
6962 {
6963 if (args == NULL || *args == '\0')
6964 ui_out_message (uiout, 0, "No watchpoints.\n");
6965 else
6966 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6967 }
6968 }
6969
6970 static void
6971 maintenance_info_breakpoints (char *args, int from_tty)
6972 {
6973 breakpoint_1 (args, 1, NULL);
6974
6975 default_collect_info ();
6976 }
6977
6978 static int
6979 breakpoint_has_pc (struct breakpoint *b,
6980 struct program_space *pspace,
6981 CORE_ADDR pc, struct obj_section *section)
6982 {
6983 struct bp_location *bl = b->loc;
6984
6985 for (; bl; bl = bl->next)
6986 {
6987 if (bl->pspace == pspace
6988 && bl->address == pc
6989 && (!overlay_debugging || bl->section == section))
6990 return 1;
6991 }
6992 return 0;
6993 }
6994
6995 /* Print a message describing any user-breakpoints set at PC. This
6996 concerns with logical breakpoints, so we match program spaces, not
6997 address spaces. */
6998
6999 static void
7000 describe_other_breakpoints (struct gdbarch *gdbarch,
7001 struct program_space *pspace, CORE_ADDR pc,
7002 struct obj_section *section, int thread)
7003 {
7004 int others = 0;
7005 struct breakpoint *b;
7006
7007 ALL_BREAKPOINTS (b)
7008 others += (user_breakpoint_p (b)
7009 && breakpoint_has_pc (b, pspace, pc, section));
7010 if (others > 0)
7011 {
7012 if (others == 1)
7013 printf_filtered (_("Note: breakpoint "));
7014 else /* if (others == ???) */
7015 printf_filtered (_("Note: breakpoints "));
7016 ALL_BREAKPOINTS (b)
7017 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7018 {
7019 others--;
7020 printf_filtered ("%d", b->number);
7021 if (b->thread == -1 && thread != -1)
7022 printf_filtered (" (all threads)");
7023 else if (b->thread != -1)
7024 printf_filtered (" (thread %d)", b->thread);
7025 printf_filtered ("%s%s ",
7026 ((b->enable_state == bp_disabled
7027 || b->enable_state == bp_call_disabled)
7028 ? " (disabled)"
7029 : ""),
7030 (others > 1) ? ","
7031 : ((others == 1) ? " and" : ""));
7032 }
7033 printf_filtered (_("also set at pc "));
7034 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7035 printf_filtered (".\n");
7036 }
7037 }
7038 \f
7039
7040 /* Return true iff it is meaningful to use the address member of
7041 BPT locations. For some breakpoint types, the locations' address members
7042 are irrelevant and it makes no sense to attempt to compare them to other
7043 addresses (or use them for any other purpose either).
7044
7045 More specifically, each of the following breakpoint types will
7046 always have a zero valued location address and we don't want to mark
7047 breakpoints of any of these types to be a duplicate of an actual
7048 breakpoint location at address zero:
7049
7050 bp_watchpoint
7051 bp_catchpoint
7052
7053 */
7054
7055 static int
7056 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7057 {
7058 enum bptype type = bpt->type;
7059
7060 return (type != bp_watchpoint && type != bp_catchpoint);
7061 }
7062
7063 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7064 true if LOC1 and LOC2 represent the same watchpoint location. */
7065
7066 static int
7067 watchpoint_locations_match (struct bp_location *loc1,
7068 struct bp_location *loc2)
7069 {
7070 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7071 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7072
7073 /* Both of them must exist. */
7074 gdb_assert (w1 != NULL);
7075 gdb_assert (w2 != NULL);
7076
7077 /* If the target can evaluate the condition expression in hardware,
7078 then we we need to insert both watchpoints even if they are at
7079 the same place. Otherwise the watchpoint will only trigger when
7080 the condition of whichever watchpoint was inserted evaluates to
7081 true, not giving a chance for GDB to check the condition of the
7082 other watchpoint. */
7083 if ((w1->cond_exp
7084 && target_can_accel_watchpoint_condition (loc1->address,
7085 loc1->length,
7086 loc1->watchpoint_type,
7087 w1->cond_exp))
7088 || (w2->cond_exp
7089 && target_can_accel_watchpoint_condition (loc2->address,
7090 loc2->length,
7091 loc2->watchpoint_type,
7092 w2->cond_exp)))
7093 return 0;
7094
7095 /* Note that this checks the owner's type, not the location's. In
7096 case the target does not support read watchpoints, but does
7097 support access watchpoints, we'll have bp_read_watchpoint
7098 watchpoints with hw_access locations. Those should be considered
7099 duplicates of hw_read locations. The hw_read locations will
7100 become hw_access locations later. */
7101 return (loc1->owner->type == loc2->owner->type
7102 && loc1->pspace->aspace == loc2->pspace->aspace
7103 && loc1->address == loc2->address
7104 && loc1->length == loc2->length);
7105 }
7106
7107 /* See breakpoint.h. */
7108
7109 int
7110 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7111 struct address_space *aspace2, CORE_ADDR addr2)
7112 {
7113 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7114 || aspace1 == aspace2)
7115 && addr1 == addr2);
7116 }
7117
7118 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7119 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7120 matches ASPACE2. On targets that have global breakpoints, the address
7121 space doesn't really matter. */
7122
7123 static int
7124 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7125 int len1, struct address_space *aspace2,
7126 CORE_ADDR addr2)
7127 {
7128 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7129 || aspace1 == aspace2)
7130 && addr2 >= addr1 && addr2 < addr1 + len1);
7131 }
7132
7133 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7134 a ranged breakpoint. In most targets, a match happens only if ASPACE
7135 matches the breakpoint's address space. On targets that have global
7136 breakpoints, the address space doesn't really matter. */
7137
7138 static int
7139 breakpoint_location_address_match (struct bp_location *bl,
7140 struct address_space *aspace,
7141 CORE_ADDR addr)
7142 {
7143 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7144 aspace, addr)
7145 || (bl->length
7146 && breakpoint_address_match_range (bl->pspace->aspace,
7147 bl->address, bl->length,
7148 aspace, addr)));
7149 }
7150
7151 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7152 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7153 match happens only if ASPACE matches the breakpoint's address
7154 space. On targets that have global breakpoints, the address space
7155 doesn't really matter. */
7156
7157 static int
7158 breakpoint_location_address_range_overlap (struct bp_location *bl,
7159 struct address_space *aspace,
7160 CORE_ADDR addr, int len)
7161 {
7162 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7163 || bl->pspace->aspace == aspace)
7164 {
7165 int bl_len = bl->length != 0 ? bl->length : 1;
7166
7167 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7168 return 1;
7169 }
7170 return 0;
7171 }
7172
7173 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7174 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7175 true, otherwise returns false. */
7176
7177 static int
7178 tracepoint_locations_match (struct bp_location *loc1,
7179 struct bp_location *loc2)
7180 {
7181 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7182 /* Since tracepoint locations are never duplicated with others', tracepoint
7183 locations at the same address of different tracepoints are regarded as
7184 different locations. */
7185 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7186 else
7187 return 0;
7188 }
7189
7190 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7191 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7192 represent the same location. */
7193
7194 static int
7195 breakpoint_locations_match (struct bp_location *loc1,
7196 struct bp_location *loc2)
7197 {
7198 int hw_point1, hw_point2;
7199
7200 /* Both of them must not be in moribund_locations. */
7201 gdb_assert (loc1->owner != NULL);
7202 gdb_assert (loc2->owner != NULL);
7203
7204 hw_point1 = is_hardware_watchpoint (loc1->owner);
7205 hw_point2 = is_hardware_watchpoint (loc2->owner);
7206
7207 if (hw_point1 != hw_point2)
7208 return 0;
7209 else if (hw_point1)
7210 return watchpoint_locations_match (loc1, loc2);
7211 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7212 return tracepoint_locations_match (loc1, loc2);
7213 else
7214 /* We compare bp_location.length in order to cover ranged breakpoints. */
7215 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7216 loc2->pspace->aspace, loc2->address)
7217 && loc1->length == loc2->length);
7218 }
7219
7220 static void
7221 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7222 int bnum, int have_bnum)
7223 {
7224 /* The longest string possibly returned by hex_string_custom
7225 is 50 chars. These must be at least that big for safety. */
7226 char astr1[64];
7227 char astr2[64];
7228
7229 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7230 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7231 if (have_bnum)
7232 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7233 bnum, astr1, astr2);
7234 else
7235 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7236 }
7237
7238 /* Adjust a breakpoint's address to account for architectural
7239 constraints on breakpoint placement. Return the adjusted address.
7240 Note: Very few targets require this kind of adjustment. For most
7241 targets, this function is simply the identity function. */
7242
7243 static CORE_ADDR
7244 adjust_breakpoint_address (struct gdbarch *gdbarch,
7245 CORE_ADDR bpaddr, enum bptype bptype)
7246 {
7247 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7248 {
7249 /* Very few targets need any kind of breakpoint adjustment. */
7250 return bpaddr;
7251 }
7252 else if (bptype == bp_watchpoint
7253 || bptype == bp_hardware_watchpoint
7254 || bptype == bp_read_watchpoint
7255 || bptype == bp_access_watchpoint
7256 || bptype == bp_catchpoint)
7257 {
7258 /* Watchpoints and the various bp_catch_* eventpoints should not
7259 have their addresses modified. */
7260 return bpaddr;
7261 }
7262 else if (bptype == bp_single_step)
7263 {
7264 /* Single-step breakpoints should not have their addresses
7265 modified. If there's any architectural constrain that
7266 applies to this address, then it should have already been
7267 taken into account when the breakpoint was created in the
7268 first place. If we didn't do this, stepping through e.g.,
7269 Thumb-2 IT blocks would break. */
7270 return bpaddr;
7271 }
7272 else
7273 {
7274 CORE_ADDR adjusted_bpaddr;
7275
7276 /* Some targets have architectural constraints on the placement
7277 of breakpoint instructions. Obtain the adjusted address. */
7278 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7279
7280 /* An adjusted breakpoint address can significantly alter
7281 a user's expectations. Print a warning if an adjustment
7282 is required. */
7283 if (adjusted_bpaddr != bpaddr)
7284 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7285
7286 return adjusted_bpaddr;
7287 }
7288 }
7289
7290 void
7291 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7292 struct breakpoint *owner)
7293 {
7294 memset (loc, 0, sizeof (*loc));
7295
7296 gdb_assert (ops != NULL);
7297
7298 loc->ops = ops;
7299 loc->owner = owner;
7300 loc->cond = NULL;
7301 loc->cond_bytecode = NULL;
7302 loc->shlib_disabled = 0;
7303 loc->enabled = 1;
7304
7305 switch (owner->type)
7306 {
7307 case bp_breakpoint:
7308 case bp_single_step:
7309 case bp_until:
7310 case bp_finish:
7311 case bp_longjmp:
7312 case bp_longjmp_resume:
7313 case bp_longjmp_call_dummy:
7314 case bp_exception:
7315 case bp_exception_resume:
7316 case bp_step_resume:
7317 case bp_hp_step_resume:
7318 case bp_watchpoint_scope:
7319 case bp_call_dummy:
7320 case bp_std_terminate:
7321 case bp_shlib_event:
7322 case bp_thread_event:
7323 case bp_overlay_event:
7324 case bp_jit_event:
7325 case bp_longjmp_master:
7326 case bp_std_terminate_master:
7327 case bp_exception_master:
7328 case bp_gnu_ifunc_resolver:
7329 case bp_gnu_ifunc_resolver_return:
7330 case bp_dprintf:
7331 loc->loc_type = bp_loc_software_breakpoint;
7332 mark_breakpoint_location_modified (loc);
7333 break;
7334 case bp_hardware_breakpoint:
7335 loc->loc_type = bp_loc_hardware_breakpoint;
7336 mark_breakpoint_location_modified (loc);
7337 break;
7338 case bp_hardware_watchpoint:
7339 case bp_read_watchpoint:
7340 case bp_access_watchpoint:
7341 loc->loc_type = bp_loc_hardware_watchpoint;
7342 break;
7343 case bp_watchpoint:
7344 case bp_catchpoint:
7345 case bp_tracepoint:
7346 case bp_fast_tracepoint:
7347 case bp_static_tracepoint:
7348 loc->loc_type = bp_loc_other;
7349 break;
7350 default:
7351 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7352 }
7353
7354 loc->refc = 1;
7355 }
7356
7357 /* Allocate a struct bp_location. */
7358
7359 static struct bp_location *
7360 allocate_bp_location (struct breakpoint *bpt)
7361 {
7362 return bpt->ops->allocate_location (bpt);
7363 }
7364
7365 static void
7366 free_bp_location (struct bp_location *loc)
7367 {
7368 loc->ops->dtor (loc);
7369 xfree (loc);
7370 }
7371
7372 /* Increment reference count. */
7373
7374 static void
7375 incref_bp_location (struct bp_location *bl)
7376 {
7377 ++bl->refc;
7378 }
7379
7380 /* Decrement reference count. If the reference count reaches 0,
7381 destroy the bp_location. Sets *BLP to NULL. */
7382
7383 static void
7384 decref_bp_location (struct bp_location **blp)
7385 {
7386 gdb_assert ((*blp)->refc > 0);
7387
7388 if (--(*blp)->refc == 0)
7389 free_bp_location (*blp);
7390 *blp = NULL;
7391 }
7392
7393 /* Add breakpoint B at the end of the global breakpoint chain. */
7394
7395 static void
7396 add_to_breakpoint_chain (struct breakpoint *b)
7397 {
7398 struct breakpoint *b1;
7399
7400 /* Add this breakpoint to the end of the chain so that a list of
7401 breakpoints will come out in order of increasing numbers. */
7402
7403 b1 = breakpoint_chain;
7404 if (b1 == 0)
7405 breakpoint_chain = b;
7406 else
7407 {
7408 while (b1->next)
7409 b1 = b1->next;
7410 b1->next = b;
7411 }
7412 }
7413
7414 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7415
7416 static void
7417 init_raw_breakpoint_without_location (struct breakpoint *b,
7418 struct gdbarch *gdbarch,
7419 enum bptype bptype,
7420 const struct breakpoint_ops *ops)
7421 {
7422 memset (b, 0, sizeof (*b));
7423
7424 gdb_assert (ops != NULL);
7425
7426 b->ops = ops;
7427 b->type = bptype;
7428 b->gdbarch = gdbarch;
7429 b->language = current_language->la_language;
7430 b->input_radix = input_radix;
7431 b->thread = -1;
7432 b->enable_state = bp_enabled;
7433 b->next = 0;
7434 b->silent = 0;
7435 b->ignore_count = 0;
7436 b->commands = NULL;
7437 b->frame_id = null_frame_id;
7438 b->condition_not_parsed = 0;
7439 b->py_bp_object = NULL;
7440 b->related_breakpoint = b;
7441 b->location = NULL;
7442 }
7443
7444 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7445 that has type BPTYPE and has no locations as yet. */
7446
7447 static struct breakpoint *
7448 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7449 enum bptype bptype,
7450 const struct breakpoint_ops *ops)
7451 {
7452 struct breakpoint *b = XNEW (struct breakpoint);
7453
7454 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7455 add_to_breakpoint_chain (b);
7456 return b;
7457 }
7458
7459 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7460 resolutions should be made as the user specified the location explicitly
7461 enough. */
7462
7463 static void
7464 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7465 {
7466 gdb_assert (loc->owner != NULL);
7467
7468 if (loc->owner->type == bp_breakpoint
7469 || loc->owner->type == bp_hardware_breakpoint
7470 || is_tracepoint (loc->owner))
7471 {
7472 int is_gnu_ifunc;
7473 const char *function_name;
7474 CORE_ADDR func_addr;
7475
7476 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7477 &func_addr, NULL, &is_gnu_ifunc);
7478
7479 if (is_gnu_ifunc && !explicit_loc)
7480 {
7481 struct breakpoint *b = loc->owner;
7482
7483 gdb_assert (loc->pspace == current_program_space);
7484 if (gnu_ifunc_resolve_name (function_name,
7485 &loc->requested_address))
7486 {
7487 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7488 loc->address = adjust_breakpoint_address (loc->gdbarch,
7489 loc->requested_address,
7490 b->type);
7491 }
7492 else if (b->type == bp_breakpoint && b->loc == loc
7493 && loc->next == NULL && b->related_breakpoint == b)
7494 {
7495 /* Create only the whole new breakpoint of this type but do not
7496 mess more complicated breakpoints with multiple locations. */
7497 b->type = bp_gnu_ifunc_resolver;
7498 /* Remember the resolver's address for use by the return
7499 breakpoint. */
7500 loc->related_address = func_addr;
7501 }
7502 }
7503
7504 if (function_name)
7505 loc->function_name = xstrdup (function_name);
7506 }
7507 }
7508
7509 /* Attempt to determine architecture of location identified by SAL. */
7510 struct gdbarch *
7511 get_sal_arch (struct symtab_and_line sal)
7512 {
7513 if (sal.section)
7514 return get_objfile_arch (sal.section->objfile);
7515 if (sal.symtab)
7516 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7517
7518 return NULL;
7519 }
7520
7521 /* Low level routine for partially initializing a breakpoint of type
7522 BPTYPE. The newly created breakpoint's address, section, source
7523 file name, and line number are provided by SAL.
7524
7525 It is expected that the caller will complete the initialization of
7526 the newly created breakpoint struct as well as output any status
7527 information regarding the creation of a new breakpoint. */
7528
7529 static void
7530 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7531 struct symtab_and_line sal, enum bptype bptype,
7532 const struct breakpoint_ops *ops)
7533 {
7534 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7535
7536 add_location_to_breakpoint (b, &sal);
7537
7538 if (bptype != bp_catchpoint)
7539 gdb_assert (sal.pspace != NULL);
7540
7541 /* Store the program space that was used to set the breakpoint,
7542 except for ordinary breakpoints, which are independent of the
7543 program space. */
7544 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7545 b->pspace = sal.pspace;
7546 }
7547
7548 /* set_raw_breakpoint is a low level routine for allocating and
7549 partially initializing a breakpoint of type BPTYPE. The newly
7550 created breakpoint's address, section, source file name, and line
7551 number are provided by SAL. The newly created and partially
7552 initialized breakpoint is added to the breakpoint chain and
7553 is also returned as the value of this function.
7554
7555 It is expected that the caller will complete the initialization of
7556 the newly created breakpoint struct as well as output any status
7557 information regarding the creation of a new breakpoint. In
7558 particular, set_raw_breakpoint does NOT set the breakpoint
7559 number! Care should be taken to not allow an error to occur
7560 prior to completing the initialization of the breakpoint. If this
7561 should happen, a bogus breakpoint will be left on the chain. */
7562
7563 struct breakpoint *
7564 set_raw_breakpoint (struct gdbarch *gdbarch,
7565 struct symtab_and_line sal, enum bptype bptype,
7566 const struct breakpoint_ops *ops)
7567 {
7568 struct breakpoint *b = XNEW (struct breakpoint);
7569
7570 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7571 add_to_breakpoint_chain (b);
7572 return b;
7573 }
7574
7575 /* Call this routine when stepping and nexting to enable a breakpoint
7576 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7577 initiated the operation. */
7578
7579 void
7580 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7581 {
7582 struct breakpoint *b, *b_tmp;
7583 int thread = tp->global_num;
7584
7585 /* To avoid having to rescan all objfile symbols at every step,
7586 we maintain a list of continually-inserted but always disabled
7587 longjmp "master" breakpoints. Here, we simply create momentary
7588 clones of those and enable them for the requested thread. */
7589 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7590 if (b->pspace == current_program_space
7591 && (b->type == bp_longjmp_master
7592 || b->type == bp_exception_master))
7593 {
7594 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7595 struct breakpoint *clone;
7596
7597 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7598 after their removal. */
7599 clone = momentary_breakpoint_from_master (b, type,
7600 &longjmp_breakpoint_ops, 1);
7601 clone->thread = thread;
7602 }
7603
7604 tp->initiating_frame = frame;
7605 }
7606
7607 /* Delete all longjmp breakpoints from THREAD. */
7608 void
7609 delete_longjmp_breakpoint (int thread)
7610 {
7611 struct breakpoint *b, *b_tmp;
7612
7613 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7614 if (b->type == bp_longjmp || b->type == bp_exception)
7615 {
7616 if (b->thread == thread)
7617 delete_breakpoint (b);
7618 }
7619 }
7620
7621 void
7622 delete_longjmp_breakpoint_at_next_stop (int thread)
7623 {
7624 struct breakpoint *b, *b_tmp;
7625
7626 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7627 if (b->type == bp_longjmp || b->type == bp_exception)
7628 {
7629 if (b->thread == thread)
7630 b->disposition = disp_del_at_next_stop;
7631 }
7632 }
7633
7634 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7635 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7636 pointer to any of them. Return NULL if this system cannot place longjmp
7637 breakpoints. */
7638
7639 struct breakpoint *
7640 set_longjmp_breakpoint_for_call_dummy (void)
7641 {
7642 struct breakpoint *b, *retval = NULL;
7643
7644 ALL_BREAKPOINTS (b)
7645 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7646 {
7647 struct breakpoint *new_b;
7648
7649 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7650 &momentary_breakpoint_ops,
7651 1);
7652 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7653
7654 /* Link NEW_B into the chain of RETVAL breakpoints. */
7655
7656 gdb_assert (new_b->related_breakpoint == new_b);
7657 if (retval == NULL)
7658 retval = new_b;
7659 new_b->related_breakpoint = retval;
7660 while (retval->related_breakpoint != new_b->related_breakpoint)
7661 retval = retval->related_breakpoint;
7662 retval->related_breakpoint = new_b;
7663 }
7664
7665 return retval;
7666 }
7667
7668 /* Verify all existing dummy frames and their associated breakpoints for
7669 TP. Remove those which can no longer be found in the current frame
7670 stack.
7671
7672 You should call this function only at places where it is safe to currently
7673 unwind the whole stack. Failed stack unwind would discard live dummy
7674 frames. */
7675
7676 void
7677 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7678 {
7679 struct breakpoint *b, *b_tmp;
7680
7681 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7682 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7683 {
7684 struct breakpoint *dummy_b = b->related_breakpoint;
7685
7686 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7687 dummy_b = dummy_b->related_breakpoint;
7688 if (dummy_b->type != bp_call_dummy
7689 || frame_find_by_id (dummy_b->frame_id) != NULL)
7690 continue;
7691
7692 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7693
7694 while (b->related_breakpoint != b)
7695 {
7696 if (b_tmp == b->related_breakpoint)
7697 b_tmp = b->related_breakpoint->next;
7698 delete_breakpoint (b->related_breakpoint);
7699 }
7700 delete_breakpoint (b);
7701 }
7702 }
7703
7704 void
7705 enable_overlay_breakpoints (void)
7706 {
7707 struct breakpoint *b;
7708
7709 ALL_BREAKPOINTS (b)
7710 if (b->type == bp_overlay_event)
7711 {
7712 b->enable_state = bp_enabled;
7713 update_global_location_list (UGLL_MAY_INSERT);
7714 overlay_events_enabled = 1;
7715 }
7716 }
7717
7718 void
7719 disable_overlay_breakpoints (void)
7720 {
7721 struct breakpoint *b;
7722
7723 ALL_BREAKPOINTS (b)
7724 if (b->type == bp_overlay_event)
7725 {
7726 b->enable_state = bp_disabled;
7727 update_global_location_list (UGLL_DONT_INSERT);
7728 overlay_events_enabled = 0;
7729 }
7730 }
7731
7732 /* Set an active std::terminate breakpoint for each std::terminate
7733 master breakpoint. */
7734 void
7735 set_std_terminate_breakpoint (void)
7736 {
7737 struct breakpoint *b, *b_tmp;
7738
7739 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7740 if (b->pspace == current_program_space
7741 && b->type == bp_std_terminate_master)
7742 {
7743 momentary_breakpoint_from_master (b, bp_std_terminate,
7744 &momentary_breakpoint_ops, 1);
7745 }
7746 }
7747
7748 /* Delete all the std::terminate breakpoints. */
7749 void
7750 delete_std_terminate_breakpoint (void)
7751 {
7752 struct breakpoint *b, *b_tmp;
7753
7754 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7755 if (b->type == bp_std_terminate)
7756 delete_breakpoint (b);
7757 }
7758
7759 struct breakpoint *
7760 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7761 {
7762 struct breakpoint *b;
7763
7764 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7765 &internal_breakpoint_ops);
7766
7767 b->enable_state = bp_enabled;
7768 /* location has to be used or breakpoint_re_set will delete me. */
7769 b->location = new_address_location (b->loc->address);
7770
7771 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7772
7773 return b;
7774 }
7775
7776 struct lang_and_radix
7777 {
7778 enum language lang;
7779 int radix;
7780 };
7781
7782 /* Create a breakpoint for JIT code registration and unregistration. */
7783
7784 struct breakpoint *
7785 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7786 {
7787 struct breakpoint *b;
7788
7789 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7790 &internal_breakpoint_ops);
7791 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7792 return b;
7793 }
7794
7795 /* Remove JIT code registration and unregistration breakpoint(s). */
7796
7797 void
7798 remove_jit_event_breakpoints (void)
7799 {
7800 struct breakpoint *b, *b_tmp;
7801
7802 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7803 if (b->type == bp_jit_event
7804 && b->loc->pspace == current_program_space)
7805 delete_breakpoint (b);
7806 }
7807
7808 void
7809 remove_solib_event_breakpoints (void)
7810 {
7811 struct breakpoint *b, *b_tmp;
7812
7813 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7814 if (b->type == bp_shlib_event
7815 && b->loc->pspace == current_program_space)
7816 delete_breakpoint (b);
7817 }
7818
7819 /* See breakpoint.h. */
7820
7821 void
7822 remove_solib_event_breakpoints_at_next_stop (void)
7823 {
7824 struct breakpoint *b, *b_tmp;
7825
7826 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7827 if (b->type == bp_shlib_event
7828 && b->loc->pspace == current_program_space)
7829 b->disposition = disp_del_at_next_stop;
7830 }
7831
7832 /* Helper for create_solib_event_breakpoint /
7833 create_and_insert_solib_event_breakpoint. Allows specifying which
7834 INSERT_MODE to pass through to update_global_location_list. */
7835
7836 static struct breakpoint *
7837 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7838 enum ugll_insert_mode insert_mode)
7839 {
7840 struct breakpoint *b;
7841
7842 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7843 &internal_breakpoint_ops);
7844 update_global_location_list_nothrow (insert_mode);
7845 return b;
7846 }
7847
7848 struct breakpoint *
7849 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7850 {
7851 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7852 }
7853
7854 /* See breakpoint.h. */
7855
7856 struct breakpoint *
7857 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7858 {
7859 struct breakpoint *b;
7860
7861 /* Explicitly tell update_global_location_list to insert
7862 locations. */
7863 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7864 if (!b->loc->inserted)
7865 {
7866 delete_breakpoint (b);
7867 return NULL;
7868 }
7869 return b;
7870 }
7871
7872 /* Disable any breakpoints that are on code in shared libraries. Only
7873 apply to enabled breakpoints, disabled ones can just stay disabled. */
7874
7875 void
7876 disable_breakpoints_in_shlibs (void)
7877 {
7878 struct bp_location *loc, **locp_tmp;
7879
7880 ALL_BP_LOCATIONS (loc, locp_tmp)
7881 {
7882 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7883 struct breakpoint *b = loc->owner;
7884
7885 /* We apply the check to all breakpoints, including disabled for
7886 those with loc->duplicate set. This is so that when breakpoint
7887 becomes enabled, or the duplicate is removed, gdb will try to
7888 insert all breakpoints. If we don't set shlib_disabled here,
7889 we'll try to insert those breakpoints and fail. */
7890 if (((b->type == bp_breakpoint)
7891 || (b->type == bp_jit_event)
7892 || (b->type == bp_hardware_breakpoint)
7893 || (is_tracepoint (b)))
7894 && loc->pspace == current_program_space
7895 && !loc->shlib_disabled
7896 && solib_name_from_address (loc->pspace, loc->address)
7897 )
7898 {
7899 loc->shlib_disabled = 1;
7900 }
7901 }
7902 }
7903
7904 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7905 notification of unloaded_shlib. Only apply to enabled breakpoints,
7906 disabled ones can just stay disabled. */
7907
7908 static void
7909 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7910 {
7911 struct bp_location *loc, **locp_tmp;
7912 int disabled_shlib_breaks = 0;
7913
7914 /* SunOS a.out shared libraries are always mapped, so do not
7915 disable breakpoints; they will only be reported as unloaded
7916 through clear_solib when GDB discards its shared library
7917 list. See clear_solib for more information. */
7918 if (exec_bfd != NULL
7919 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7920 return;
7921
7922 ALL_BP_LOCATIONS (loc, locp_tmp)
7923 {
7924 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7925 struct breakpoint *b = loc->owner;
7926
7927 if (solib->pspace == loc->pspace
7928 && !loc->shlib_disabled
7929 && (((b->type == bp_breakpoint
7930 || b->type == bp_jit_event
7931 || b->type == bp_hardware_breakpoint)
7932 && (loc->loc_type == bp_loc_hardware_breakpoint
7933 || loc->loc_type == bp_loc_software_breakpoint))
7934 || is_tracepoint (b))
7935 && solib_contains_address_p (solib, loc->address))
7936 {
7937 loc->shlib_disabled = 1;
7938 /* At this point, we cannot rely on remove_breakpoint
7939 succeeding so we must mark the breakpoint as not inserted
7940 to prevent future errors occurring in remove_breakpoints. */
7941 loc->inserted = 0;
7942
7943 /* This may cause duplicate notifications for the same breakpoint. */
7944 observer_notify_breakpoint_modified (b);
7945
7946 if (!disabled_shlib_breaks)
7947 {
7948 target_terminal_ours_for_output ();
7949 warning (_("Temporarily disabling breakpoints "
7950 "for unloaded shared library \"%s\""),
7951 solib->so_name);
7952 }
7953 disabled_shlib_breaks = 1;
7954 }
7955 }
7956 }
7957
7958 /* Disable any breakpoints and tracepoints in OBJFILE upon
7959 notification of free_objfile. Only apply to enabled breakpoints,
7960 disabled ones can just stay disabled. */
7961
7962 static void
7963 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7964 {
7965 struct breakpoint *b;
7966
7967 if (objfile == NULL)
7968 return;
7969
7970 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7971 managed by the user with add-symbol-file/remove-symbol-file.
7972 Similarly to how breakpoints in shared libraries are handled in
7973 response to "nosharedlibrary", mark breakpoints in such modules
7974 shlib_disabled so they end up uninserted on the next global
7975 location list update. Shared libraries not loaded by the user
7976 aren't handled here -- they're already handled in
7977 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7978 solib_unloaded observer. We skip objfiles that are not
7979 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7980 main objfile). */
7981 if ((objfile->flags & OBJF_SHARED) == 0
7982 || (objfile->flags & OBJF_USERLOADED) == 0)
7983 return;
7984
7985 ALL_BREAKPOINTS (b)
7986 {
7987 struct bp_location *loc;
7988 int bp_modified = 0;
7989
7990 if (!is_breakpoint (b) && !is_tracepoint (b))
7991 continue;
7992
7993 for (loc = b->loc; loc != NULL; loc = loc->next)
7994 {
7995 CORE_ADDR loc_addr = loc->address;
7996
7997 if (loc->loc_type != bp_loc_hardware_breakpoint
7998 && loc->loc_type != bp_loc_software_breakpoint)
7999 continue;
8000
8001 if (loc->shlib_disabled != 0)
8002 continue;
8003
8004 if (objfile->pspace != loc->pspace)
8005 continue;
8006
8007 if (loc->loc_type != bp_loc_hardware_breakpoint
8008 && loc->loc_type != bp_loc_software_breakpoint)
8009 continue;
8010
8011 if (is_addr_in_objfile (loc_addr, objfile))
8012 {
8013 loc->shlib_disabled = 1;
8014 /* At this point, we don't know whether the object was
8015 unmapped from the inferior or not, so leave the
8016 inserted flag alone. We'll handle failure to
8017 uninsert quietly, in case the object was indeed
8018 unmapped. */
8019
8020 mark_breakpoint_location_modified (loc);
8021
8022 bp_modified = 1;
8023 }
8024 }
8025
8026 if (bp_modified)
8027 observer_notify_breakpoint_modified (b);
8028 }
8029 }
8030
8031 /* FORK & VFORK catchpoints. */
8032
8033 /* An instance of this type is used to represent a fork or vfork
8034 catchpoint. It includes a "struct breakpoint" as a kind of base
8035 class; users downcast to "struct breakpoint *" when needed. A
8036 breakpoint is really of this type iff its ops pointer points to
8037 CATCH_FORK_BREAKPOINT_OPS. */
8038
8039 struct fork_catchpoint
8040 {
8041 /* The base class. */
8042 struct breakpoint base;
8043
8044 /* Process id of a child process whose forking triggered this
8045 catchpoint. This field is only valid immediately after this
8046 catchpoint has triggered. */
8047 ptid_t forked_inferior_pid;
8048 };
8049
8050 /* Implement the "insert" breakpoint_ops method for fork
8051 catchpoints. */
8052
8053 static int
8054 insert_catch_fork (struct bp_location *bl)
8055 {
8056 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8057 }
8058
8059 /* Implement the "remove" breakpoint_ops method for fork
8060 catchpoints. */
8061
8062 static int
8063 remove_catch_fork (struct bp_location *bl)
8064 {
8065 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8066 }
8067
8068 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8069 catchpoints. */
8070
8071 static int
8072 breakpoint_hit_catch_fork (const struct bp_location *bl,
8073 struct address_space *aspace, CORE_ADDR bp_addr,
8074 const struct target_waitstatus *ws)
8075 {
8076 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8077
8078 if (ws->kind != TARGET_WAITKIND_FORKED)
8079 return 0;
8080
8081 c->forked_inferior_pid = ws->value.related_pid;
8082 return 1;
8083 }
8084
8085 /* Implement the "print_it" breakpoint_ops method for fork
8086 catchpoints. */
8087
8088 static enum print_stop_action
8089 print_it_catch_fork (bpstat bs)
8090 {
8091 struct ui_out *uiout = current_uiout;
8092 struct breakpoint *b = bs->breakpoint_at;
8093 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8094
8095 annotate_catchpoint (b->number);
8096 if (b->disposition == disp_del)
8097 ui_out_text (uiout, "\nTemporary catchpoint ");
8098 else
8099 ui_out_text (uiout, "\nCatchpoint ");
8100 if (ui_out_is_mi_like_p (uiout))
8101 {
8102 ui_out_field_string (uiout, "reason",
8103 async_reason_lookup (EXEC_ASYNC_FORK));
8104 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8105 }
8106 ui_out_field_int (uiout, "bkptno", b->number);
8107 ui_out_text (uiout, " (forked process ");
8108 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8109 ui_out_text (uiout, "), ");
8110 return PRINT_SRC_AND_LOC;
8111 }
8112
8113 /* Implement the "print_one" breakpoint_ops method for fork
8114 catchpoints. */
8115
8116 static void
8117 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8118 {
8119 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8120 struct value_print_options opts;
8121 struct ui_out *uiout = current_uiout;
8122
8123 get_user_print_options (&opts);
8124
8125 /* Field 4, the address, is omitted (which makes the columns not
8126 line up too nicely with the headers, but the effect is relatively
8127 readable). */
8128 if (opts.addressprint)
8129 ui_out_field_skip (uiout, "addr");
8130 annotate_field (5);
8131 ui_out_text (uiout, "fork");
8132 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8133 {
8134 ui_out_text (uiout, ", process ");
8135 ui_out_field_int (uiout, "what",
8136 ptid_get_pid (c->forked_inferior_pid));
8137 ui_out_spaces (uiout, 1);
8138 }
8139
8140 if (ui_out_is_mi_like_p (uiout))
8141 ui_out_field_string (uiout, "catch-type", "fork");
8142 }
8143
8144 /* Implement the "print_mention" breakpoint_ops method for fork
8145 catchpoints. */
8146
8147 static void
8148 print_mention_catch_fork (struct breakpoint *b)
8149 {
8150 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8151 }
8152
8153 /* Implement the "print_recreate" breakpoint_ops method for fork
8154 catchpoints. */
8155
8156 static void
8157 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8158 {
8159 fprintf_unfiltered (fp, "catch fork");
8160 print_recreate_thread (b, fp);
8161 }
8162
8163 /* The breakpoint_ops structure to be used in fork catchpoints. */
8164
8165 static struct breakpoint_ops catch_fork_breakpoint_ops;
8166
8167 /* Implement the "insert" breakpoint_ops method for vfork
8168 catchpoints. */
8169
8170 static int
8171 insert_catch_vfork (struct bp_location *bl)
8172 {
8173 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8174 }
8175
8176 /* Implement the "remove" breakpoint_ops method for vfork
8177 catchpoints. */
8178
8179 static int
8180 remove_catch_vfork (struct bp_location *bl)
8181 {
8182 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8183 }
8184
8185 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8186 catchpoints. */
8187
8188 static int
8189 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8190 struct address_space *aspace, CORE_ADDR bp_addr,
8191 const struct target_waitstatus *ws)
8192 {
8193 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8194
8195 if (ws->kind != TARGET_WAITKIND_VFORKED)
8196 return 0;
8197
8198 c->forked_inferior_pid = ws->value.related_pid;
8199 return 1;
8200 }
8201
8202 /* Implement the "print_it" breakpoint_ops method for vfork
8203 catchpoints. */
8204
8205 static enum print_stop_action
8206 print_it_catch_vfork (bpstat bs)
8207 {
8208 struct ui_out *uiout = current_uiout;
8209 struct breakpoint *b = bs->breakpoint_at;
8210 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8211
8212 annotate_catchpoint (b->number);
8213 if (b->disposition == disp_del)
8214 ui_out_text (uiout, "\nTemporary catchpoint ");
8215 else
8216 ui_out_text (uiout, "\nCatchpoint ");
8217 if (ui_out_is_mi_like_p (uiout))
8218 {
8219 ui_out_field_string (uiout, "reason",
8220 async_reason_lookup (EXEC_ASYNC_VFORK));
8221 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8222 }
8223 ui_out_field_int (uiout, "bkptno", b->number);
8224 ui_out_text (uiout, " (vforked process ");
8225 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8226 ui_out_text (uiout, "), ");
8227 return PRINT_SRC_AND_LOC;
8228 }
8229
8230 /* Implement the "print_one" breakpoint_ops method for vfork
8231 catchpoints. */
8232
8233 static void
8234 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8235 {
8236 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8237 struct value_print_options opts;
8238 struct ui_out *uiout = current_uiout;
8239
8240 get_user_print_options (&opts);
8241 /* Field 4, the address, is omitted (which makes the columns not
8242 line up too nicely with the headers, but the effect is relatively
8243 readable). */
8244 if (opts.addressprint)
8245 ui_out_field_skip (uiout, "addr");
8246 annotate_field (5);
8247 ui_out_text (uiout, "vfork");
8248 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8249 {
8250 ui_out_text (uiout, ", process ");
8251 ui_out_field_int (uiout, "what",
8252 ptid_get_pid (c->forked_inferior_pid));
8253 ui_out_spaces (uiout, 1);
8254 }
8255
8256 if (ui_out_is_mi_like_p (uiout))
8257 ui_out_field_string (uiout, "catch-type", "vfork");
8258 }
8259
8260 /* Implement the "print_mention" breakpoint_ops method for vfork
8261 catchpoints. */
8262
8263 static void
8264 print_mention_catch_vfork (struct breakpoint *b)
8265 {
8266 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8267 }
8268
8269 /* Implement the "print_recreate" breakpoint_ops method for vfork
8270 catchpoints. */
8271
8272 static void
8273 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8274 {
8275 fprintf_unfiltered (fp, "catch vfork");
8276 print_recreate_thread (b, fp);
8277 }
8278
8279 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8280
8281 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8282
8283 /* An instance of this type is used to represent an solib catchpoint.
8284 It includes a "struct breakpoint" as a kind of base class; users
8285 downcast to "struct breakpoint *" when needed. A breakpoint is
8286 really of this type iff its ops pointer points to
8287 CATCH_SOLIB_BREAKPOINT_OPS. */
8288
8289 struct solib_catchpoint
8290 {
8291 /* The base class. */
8292 struct breakpoint base;
8293
8294 /* True for "catch load", false for "catch unload". */
8295 unsigned char is_load;
8296
8297 /* Regular expression to match, if any. COMPILED is only valid when
8298 REGEX is non-NULL. */
8299 char *regex;
8300 regex_t compiled;
8301 };
8302
8303 static void
8304 dtor_catch_solib (struct breakpoint *b)
8305 {
8306 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8307
8308 if (self->regex)
8309 regfree (&self->compiled);
8310 xfree (self->regex);
8311
8312 base_breakpoint_ops.dtor (b);
8313 }
8314
8315 static int
8316 insert_catch_solib (struct bp_location *ignore)
8317 {
8318 return 0;
8319 }
8320
8321 static int
8322 remove_catch_solib (struct bp_location *ignore)
8323 {
8324 return 0;
8325 }
8326
8327 static int
8328 breakpoint_hit_catch_solib (const struct bp_location *bl,
8329 struct address_space *aspace,
8330 CORE_ADDR bp_addr,
8331 const struct target_waitstatus *ws)
8332 {
8333 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8334 struct breakpoint *other;
8335
8336 if (ws->kind == TARGET_WAITKIND_LOADED)
8337 return 1;
8338
8339 ALL_BREAKPOINTS (other)
8340 {
8341 struct bp_location *other_bl;
8342
8343 if (other == bl->owner)
8344 continue;
8345
8346 if (other->type != bp_shlib_event)
8347 continue;
8348
8349 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8350 continue;
8351
8352 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8353 {
8354 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8355 return 1;
8356 }
8357 }
8358
8359 return 0;
8360 }
8361
8362 static void
8363 check_status_catch_solib (struct bpstats *bs)
8364 {
8365 struct solib_catchpoint *self
8366 = (struct solib_catchpoint *) bs->breakpoint_at;
8367 int ix;
8368
8369 if (self->is_load)
8370 {
8371 struct so_list *iter;
8372
8373 for (ix = 0;
8374 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8375 ix, iter);
8376 ++ix)
8377 {
8378 if (!self->regex
8379 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8380 return;
8381 }
8382 }
8383 else
8384 {
8385 char *iter;
8386
8387 for (ix = 0;
8388 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8389 ix, iter);
8390 ++ix)
8391 {
8392 if (!self->regex
8393 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8394 return;
8395 }
8396 }
8397
8398 bs->stop = 0;
8399 bs->print_it = print_it_noop;
8400 }
8401
8402 static enum print_stop_action
8403 print_it_catch_solib (bpstat bs)
8404 {
8405 struct breakpoint *b = bs->breakpoint_at;
8406 struct ui_out *uiout = current_uiout;
8407
8408 annotate_catchpoint (b->number);
8409 if (b->disposition == disp_del)
8410 ui_out_text (uiout, "\nTemporary catchpoint ");
8411 else
8412 ui_out_text (uiout, "\nCatchpoint ");
8413 ui_out_field_int (uiout, "bkptno", b->number);
8414 ui_out_text (uiout, "\n");
8415 if (ui_out_is_mi_like_p (uiout))
8416 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8417 print_solib_event (1);
8418 return PRINT_SRC_AND_LOC;
8419 }
8420
8421 static void
8422 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8423 {
8424 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8425 struct value_print_options opts;
8426 struct ui_out *uiout = current_uiout;
8427 char *msg;
8428
8429 get_user_print_options (&opts);
8430 /* Field 4, the address, is omitted (which makes the columns not
8431 line up too nicely with the headers, but the effect is relatively
8432 readable). */
8433 if (opts.addressprint)
8434 {
8435 annotate_field (4);
8436 ui_out_field_skip (uiout, "addr");
8437 }
8438
8439 annotate_field (5);
8440 if (self->is_load)
8441 {
8442 if (self->regex)
8443 msg = xstrprintf (_("load of library matching %s"), self->regex);
8444 else
8445 msg = xstrdup (_("load of library"));
8446 }
8447 else
8448 {
8449 if (self->regex)
8450 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8451 else
8452 msg = xstrdup (_("unload of library"));
8453 }
8454 ui_out_field_string (uiout, "what", msg);
8455 xfree (msg);
8456
8457 if (ui_out_is_mi_like_p (uiout))
8458 ui_out_field_string (uiout, "catch-type",
8459 self->is_load ? "load" : "unload");
8460 }
8461
8462 static void
8463 print_mention_catch_solib (struct breakpoint *b)
8464 {
8465 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8466
8467 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8468 self->is_load ? "load" : "unload");
8469 }
8470
8471 static void
8472 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8473 {
8474 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8475
8476 fprintf_unfiltered (fp, "%s %s",
8477 b->disposition == disp_del ? "tcatch" : "catch",
8478 self->is_load ? "load" : "unload");
8479 if (self->regex)
8480 fprintf_unfiltered (fp, " %s", self->regex);
8481 fprintf_unfiltered (fp, "\n");
8482 }
8483
8484 static struct breakpoint_ops catch_solib_breakpoint_ops;
8485
8486 /* Shared helper function (MI and CLI) for creating and installing
8487 a shared object event catchpoint. If IS_LOAD is non-zero then
8488 the events to be caught are load events, otherwise they are
8489 unload events. If IS_TEMP is non-zero the catchpoint is a
8490 temporary one. If ENABLED is non-zero the catchpoint is
8491 created in an enabled state. */
8492
8493 void
8494 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8495 {
8496 struct solib_catchpoint *c;
8497 struct gdbarch *gdbarch = get_current_arch ();
8498 struct cleanup *cleanup;
8499
8500 if (!arg)
8501 arg = "";
8502 arg = skip_spaces (arg);
8503
8504 c = XCNEW (struct solib_catchpoint);
8505 cleanup = make_cleanup (xfree, c);
8506
8507 if (*arg != '\0')
8508 {
8509 int errcode;
8510
8511 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8512 if (errcode != 0)
8513 {
8514 char *err = get_regcomp_error (errcode, &c->compiled);
8515
8516 make_cleanup (xfree, err);
8517 error (_("Invalid regexp (%s): %s"), err, arg);
8518 }
8519 c->regex = xstrdup (arg);
8520 }
8521
8522 c->is_load = is_load;
8523 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8524 &catch_solib_breakpoint_ops);
8525
8526 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8527
8528 discard_cleanups (cleanup);
8529 install_breakpoint (0, &c->base, 1);
8530 }
8531
8532 /* A helper function that does all the work for "catch load" and
8533 "catch unload". */
8534
8535 static void
8536 catch_load_or_unload (char *arg, int from_tty, int is_load,
8537 struct cmd_list_element *command)
8538 {
8539 int tempflag;
8540 const int enabled = 1;
8541
8542 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8543
8544 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8545 }
8546
8547 static void
8548 catch_load_command_1 (char *arg, int from_tty,
8549 struct cmd_list_element *command)
8550 {
8551 catch_load_or_unload (arg, from_tty, 1, command);
8552 }
8553
8554 static void
8555 catch_unload_command_1 (char *arg, int from_tty,
8556 struct cmd_list_element *command)
8557 {
8558 catch_load_or_unload (arg, from_tty, 0, command);
8559 }
8560
8561 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8562 is non-zero, then make the breakpoint temporary. If COND_STRING is
8563 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8564 the breakpoint_ops structure associated to the catchpoint. */
8565
8566 void
8567 init_catchpoint (struct breakpoint *b,
8568 struct gdbarch *gdbarch, int tempflag,
8569 char *cond_string,
8570 const struct breakpoint_ops *ops)
8571 {
8572 struct symtab_and_line sal;
8573
8574 init_sal (&sal);
8575 sal.pspace = current_program_space;
8576
8577 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8578
8579 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8580 b->disposition = tempflag ? disp_del : disp_donttouch;
8581 }
8582
8583 void
8584 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8585 {
8586 add_to_breakpoint_chain (b);
8587 set_breakpoint_number (internal, b);
8588 if (is_tracepoint (b))
8589 set_tracepoint_count (breakpoint_count);
8590 if (!internal)
8591 mention (b);
8592 observer_notify_breakpoint_created (b);
8593
8594 if (update_gll)
8595 update_global_location_list (UGLL_MAY_INSERT);
8596 }
8597
8598 static void
8599 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8600 int tempflag, char *cond_string,
8601 const struct breakpoint_ops *ops)
8602 {
8603 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8604
8605 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8606
8607 c->forked_inferior_pid = null_ptid;
8608
8609 install_breakpoint (0, &c->base, 1);
8610 }
8611
8612 /* Exec catchpoints. */
8613
8614 /* An instance of this type is used to represent an exec catchpoint.
8615 It includes a "struct breakpoint" as a kind of base class; users
8616 downcast to "struct breakpoint *" when needed. A breakpoint is
8617 really of this type iff its ops pointer points to
8618 CATCH_EXEC_BREAKPOINT_OPS. */
8619
8620 struct exec_catchpoint
8621 {
8622 /* The base class. */
8623 struct breakpoint base;
8624
8625 /* Filename of a program whose exec triggered this catchpoint.
8626 This field is only valid immediately after this catchpoint has
8627 triggered. */
8628 char *exec_pathname;
8629 };
8630
8631 /* Implement the "dtor" breakpoint_ops method for exec
8632 catchpoints. */
8633
8634 static void
8635 dtor_catch_exec (struct breakpoint *b)
8636 {
8637 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8638
8639 xfree (c->exec_pathname);
8640
8641 base_breakpoint_ops.dtor (b);
8642 }
8643
8644 static int
8645 insert_catch_exec (struct bp_location *bl)
8646 {
8647 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8648 }
8649
8650 static int
8651 remove_catch_exec (struct bp_location *bl)
8652 {
8653 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8654 }
8655
8656 static int
8657 breakpoint_hit_catch_exec (const struct bp_location *bl,
8658 struct address_space *aspace, CORE_ADDR bp_addr,
8659 const struct target_waitstatus *ws)
8660 {
8661 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8662
8663 if (ws->kind != TARGET_WAITKIND_EXECD)
8664 return 0;
8665
8666 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8667 return 1;
8668 }
8669
8670 static enum print_stop_action
8671 print_it_catch_exec (bpstat bs)
8672 {
8673 struct ui_out *uiout = current_uiout;
8674 struct breakpoint *b = bs->breakpoint_at;
8675 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8676
8677 annotate_catchpoint (b->number);
8678 if (b->disposition == disp_del)
8679 ui_out_text (uiout, "\nTemporary catchpoint ");
8680 else
8681 ui_out_text (uiout, "\nCatchpoint ");
8682 if (ui_out_is_mi_like_p (uiout))
8683 {
8684 ui_out_field_string (uiout, "reason",
8685 async_reason_lookup (EXEC_ASYNC_EXEC));
8686 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8687 }
8688 ui_out_field_int (uiout, "bkptno", b->number);
8689 ui_out_text (uiout, " (exec'd ");
8690 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8691 ui_out_text (uiout, "), ");
8692
8693 return PRINT_SRC_AND_LOC;
8694 }
8695
8696 static void
8697 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8698 {
8699 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8700 struct value_print_options opts;
8701 struct ui_out *uiout = current_uiout;
8702
8703 get_user_print_options (&opts);
8704
8705 /* Field 4, the address, is omitted (which makes the columns
8706 not line up too nicely with the headers, but the effect
8707 is relatively readable). */
8708 if (opts.addressprint)
8709 ui_out_field_skip (uiout, "addr");
8710 annotate_field (5);
8711 ui_out_text (uiout, "exec");
8712 if (c->exec_pathname != NULL)
8713 {
8714 ui_out_text (uiout, ", program \"");
8715 ui_out_field_string (uiout, "what", c->exec_pathname);
8716 ui_out_text (uiout, "\" ");
8717 }
8718
8719 if (ui_out_is_mi_like_p (uiout))
8720 ui_out_field_string (uiout, "catch-type", "exec");
8721 }
8722
8723 static void
8724 print_mention_catch_exec (struct breakpoint *b)
8725 {
8726 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8727 }
8728
8729 /* Implement the "print_recreate" breakpoint_ops method for exec
8730 catchpoints. */
8731
8732 static void
8733 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8734 {
8735 fprintf_unfiltered (fp, "catch exec");
8736 print_recreate_thread (b, fp);
8737 }
8738
8739 static struct breakpoint_ops catch_exec_breakpoint_ops;
8740
8741 static int
8742 hw_breakpoint_used_count (void)
8743 {
8744 int i = 0;
8745 struct breakpoint *b;
8746 struct bp_location *bl;
8747
8748 ALL_BREAKPOINTS (b)
8749 {
8750 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8751 for (bl = b->loc; bl; bl = bl->next)
8752 {
8753 /* Special types of hardware breakpoints may use more than
8754 one register. */
8755 i += b->ops->resources_needed (bl);
8756 }
8757 }
8758
8759 return i;
8760 }
8761
8762 /* Returns the resources B would use if it were a hardware
8763 watchpoint. */
8764
8765 static int
8766 hw_watchpoint_use_count (struct breakpoint *b)
8767 {
8768 int i = 0;
8769 struct bp_location *bl;
8770
8771 if (!breakpoint_enabled (b))
8772 return 0;
8773
8774 for (bl = b->loc; bl; bl = bl->next)
8775 {
8776 /* Special types of hardware watchpoints may use more than
8777 one register. */
8778 i += b->ops->resources_needed (bl);
8779 }
8780
8781 return i;
8782 }
8783
8784 /* Returns the sum the used resources of all hardware watchpoints of
8785 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8786 the sum of the used resources of all hardware watchpoints of other
8787 types _not_ TYPE. */
8788
8789 static int
8790 hw_watchpoint_used_count_others (struct breakpoint *except,
8791 enum bptype type, int *other_type_used)
8792 {
8793 int i = 0;
8794 struct breakpoint *b;
8795
8796 *other_type_used = 0;
8797 ALL_BREAKPOINTS (b)
8798 {
8799 if (b == except)
8800 continue;
8801 if (!breakpoint_enabled (b))
8802 continue;
8803
8804 if (b->type == type)
8805 i += hw_watchpoint_use_count (b);
8806 else if (is_hardware_watchpoint (b))
8807 *other_type_used = 1;
8808 }
8809
8810 return i;
8811 }
8812
8813 void
8814 disable_watchpoints_before_interactive_call_start (void)
8815 {
8816 struct breakpoint *b;
8817
8818 ALL_BREAKPOINTS (b)
8819 {
8820 if (is_watchpoint (b) && breakpoint_enabled (b))
8821 {
8822 b->enable_state = bp_call_disabled;
8823 update_global_location_list (UGLL_DONT_INSERT);
8824 }
8825 }
8826 }
8827
8828 void
8829 enable_watchpoints_after_interactive_call_stop (void)
8830 {
8831 struct breakpoint *b;
8832
8833 ALL_BREAKPOINTS (b)
8834 {
8835 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8836 {
8837 b->enable_state = bp_enabled;
8838 update_global_location_list (UGLL_MAY_INSERT);
8839 }
8840 }
8841 }
8842
8843 void
8844 disable_breakpoints_before_startup (void)
8845 {
8846 current_program_space->executing_startup = 1;
8847 update_global_location_list (UGLL_DONT_INSERT);
8848 }
8849
8850 void
8851 enable_breakpoints_after_startup (void)
8852 {
8853 current_program_space->executing_startup = 0;
8854 breakpoint_re_set ();
8855 }
8856
8857 /* Create a new single-step breakpoint for thread THREAD, with no
8858 locations. */
8859
8860 static struct breakpoint *
8861 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8862 {
8863 struct breakpoint *b = XNEW (struct breakpoint);
8864
8865 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8866 &momentary_breakpoint_ops);
8867
8868 b->disposition = disp_donttouch;
8869 b->frame_id = null_frame_id;
8870
8871 b->thread = thread;
8872 gdb_assert (b->thread != 0);
8873
8874 add_to_breakpoint_chain (b);
8875
8876 return b;
8877 }
8878
8879 /* Set a momentary breakpoint of type TYPE at address specified by
8880 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8881 frame. */
8882
8883 struct breakpoint *
8884 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8885 struct frame_id frame_id, enum bptype type)
8886 {
8887 struct breakpoint *b;
8888
8889 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8890 tail-called one. */
8891 gdb_assert (!frame_id_artificial_p (frame_id));
8892
8893 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8894 b->enable_state = bp_enabled;
8895 b->disposition = disp_donttouch;
8896 b->frame_id = frame_id;
8897
8898 /* If we're debugging a multi-threaded program, then we want
8899 momentary breakpoints to be active in only a single thread of
8900 control. */
8901 if (in_thread_list (inferior_ptid))
8902 b->thread = ptid_to_global_thread_id (inferior_ptid);
8903
8904 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8905
8906 return b;
8907 }
8908
8909 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8910 The new breakpoint will have type TYPE, use OPS as its
8911 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8912
8913 static struct breakpoint *
8914 momentary_breakpoint_from_master (struct breakpoint *orig,
8915 enum bptype type,
8916 const struct breakpoint_ops *ops,
8917 int loc_enabled)
8918 {
8919 struct breakpoint *copy;
8920
8921 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8922 copy->loc = allocate_bp_location (copy);
8923 set_breakpoint_location_function (copy->loc, 1);
8924
8925 copy->loc->gdbarch = orig->loc->gdbarch;
8926 copy->loc->requested_address = orig->loc->requested_address;
8927 copy->loc->address = orig->loc->address;
8928 copy->loc->section = orig->loc->section;
8929 copy->loc->pspace = orig->loc->pspace;
8930 copy->loc->probe = orig->loc->probe;
8931 copy->loc->line_number = orig->loc->line_number;
8932 copy->loc->symtab = orig->loc->symtab;
8933 copy->loc->enabled = loc_enabled;
8934 copy->frame_id = orig->frame_id;
8935 copy->thread = orig->thread;
8936 copy->pspace = orig->pspace;
8937
8938 copy->enable_state = bp_enabled;
8939 copy->disposition = disp_donttouch;
8940 copy->number = internal_breakpoint_number--;
8941
8942 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8943 return copy;
8944 }
8945
8946 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8947 ORIG is NULL. */
8948
8949 struct breakpoint *
8950 clone_momentary_breakpoint (struct breakpoint *orig)
8951 {
8952 /* If there's nothing to clone, then return nothing. */
8953 if (orig == NULL)
8954 return NULL;
8955
8956 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8957 }
8958
8959 struct breakpoint *
8960 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8961 enum bptype type)
8962 {
8963 struct symtab_and_line sal;
8964
8965 sal = find_pc_line (pc, 0);
8966 sal.pc = pc;
8967 sal.section = find_pc_overlay (pc);
8968 sal.explicit_pc = 1;
8969
8970 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8971 }
8972 \f
8973
8974 /* Tell the user we have just set a breakpoint B. */
8975
8976 static void
8977 mention (struct breakpoint *b)
8978 {
8979 b->ops->print_mention (b);
8980 if (ui_out_is_mi_like_p (current_uiout))
8981 return;
8982 printf_filtered ("\n");
8983 }
8984 \f
8985
8986 static int bp_loc_is_permanent (struct bp_location *loc);
8987
8988 static struct bp_location *
8989 add_location_to_breakpoint (struct breakpoint *b,
8990 const struct symtab_and_line *sal)
8991 {
8992 struct bp_location *loc, **tmp;
8993 CORE_ADDR adjusted_address;
8994 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8995
8996 if (loc_gdbarch == NULL)
8997 loc_gdbarch = b->gdbarch;
8998
8999 /* Adjust the breakpoint's address prior to allocating a location.
9000 Once we call allocate_bp_location(), that mostly uninitialized
9001 location will be placed on the location chain. Adjustment of the
9002 breakpoint may cause target_read_memory() to be called and we do
9003 not want its scan of the location chain to find a breakpoint and
9004 location that's only been partially initialized. */
9005 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9006 sal->pc, b->type);
9007
9008 /* Sort the locations by their ADDRESS. */
9009 loc = allocate_bp_location (b);
9010 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9011 tmp = &((*tmp)->next))
9012 ;
9013 loc->next = *tmp;
9014 *tmp = loc;
9015
9016 loc->requested_address = sal->pc;
9017 loc->address = adjusted_address;
9018 loc->pspace = sal->pspace;
9019 loc->probe.probe = sal->probe;
9020 loc->probe.objfile = sal->objfile;
9021 gdb_assert (loc->pspace != NULL);
9022 loc->section = sal->section;
9023 loc->gdbarch = loc_gdbarch;
9024 loc->line_number = sal->line;
9025 loc->symtab = sal->symtab;
9026
9027 set_breakpoint_location_function (loc,
9028 sal->explicit_pc || sal->explicit_line);
9029
9030 /* While by definition, permanent breakpoints are already present in the
9031 code, we don't mark the location as inserted. Normally one would expect
9032 that GDB could rely on that breakpoint instruction to stop the program,
9033 thus removing the need to insert its own breakpoint, except that executing
9034 the breakpoint instruction can kill the target instead of reporting a
9035 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9036 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9037 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9038 breakpoint be inserted normally results in QEMU knowing about the GDB
9039 breakpoint, and thus trap before the breakpoint instruction is executed.
9040 (If GDB later needs to continue execution past the permanent breakpoint,
9041 it manually increments the PC, thus avoiding executing the breakpoint
9042 instruction.) */
9043 if (bp_loc_is_permanent (loc))
9044 loc->permanent = 1;
9045
9046 return loc;
9047 }
9048 \f
9049
9050 /* See breakpoint.h. */
9051
9052 int
9053 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9054 {
9055 int len;
9056 CORE_ADDR addr;
9057 const gdb_byte *bpoint;
9058 gdb_byte *target_mem;
9059 struct cleanup *cleanup;
9060 int retval = 0;
9061
9062 addr = address;
9063 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9064
9065 /* Software breakpoints unsupported? */
9066 if (bpoint == NULL)
9067 return 0;
9068
9069 target_mem = (gdb_byte *) alloca (len);
9070
9071 /* Enable the automatic memory restoration from breakpoints while
9072 we read the memory. Otherwise we could say about our temporary
9073 breakpoints they are permanent. */
9074 cleanup = make_show_memory_breakpoints_cleanup (0);
9075
9076 if (target_read_memory (address, target_mem, len) == 0
9077 && memcmp (target_mem, bpoint, len) == 0)
9078 retval = 1;
9079
9080 do_cleanups (cleanup);
9081
9082 return retval;
9083 }
9084
9085 /* Return 1 if LOC is pointing to a permanent breakpoint,
9086 return 0 otherwise. */
9087
9088 static int
9089 bp_loc_is_permanent (struct bp_location *loc)
9090 {
9091 struct cleanup *cleanup;
9092 int retval;
9093
9094 gdb_assert (loc != NULL);
9095
9096 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9097 attempt to read from the addresses the locations of these breakpoint types
9098 point to. program_breakpoint_here_p, below, will attempt to read
9099 memory. */
9100 if (!breakpoint_address_is_meaningful (loc->owner))
9101 return 0;
9102
9103 cleanup = save_current_space_and_thread ();
9104 switch_to_program_space_and_thread (loc->pspace);
9105
9106 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9107
9108 do_cleanups (cleanup);
9109
9110 return retval;
9111 }
9112
9113 /* Build a command list for the dprintf corresponding to the current
9114 settings of the dprintf style options. */
9115
9116 static void
9117 update_dprintf_command_list (struct breakpoint *b)
9118 {
9119 char *dprintf_args = b->extra_string;
9120 char *printf_line = NULL;
9121
9122 if (!dprintf_args)
9123 return;
9124
9125 dprintf_args = skip_spaces (dprintf_args);
9126
9127 /* Allow a comma, as it may have terminated a location, but don't
9128 insist on it. */
9129 if (*dprintf_args == ',')
9130 ++dprintf_args;
9131 dprintf_args = skip_spaces (dprintf_args);
9132
9133 if (*dprintf_args != '"')
9134 error (_("Bad format string, missing '\"'."));
9135
9136 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9137 printf_line = xstrprintf ("printf %s", dprintf_args);
9138 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9139 {
9140 if (!dprintf_function)
9141 error (_("No function supplied for dprintf call"));
9142
9143 if (dprintf_channel && strlen (dprintf_channel) > 0)
9144 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9145 dprintf_function,
9146 dprintf_channel,
9147 dprintf_args);
9148 else
9149 printf_line = xstrprintf ("call (void) %s (%s)",
9150 dprintf_function,
9151 dprintf_args);
9152 }
9153 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9154 {
9155 if (target_can_run_breakpoint_commands ())
9156 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9157 else
9158 {
9159 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9160 printf_line = xstrprintf ("printf %s", dprintf_args);
9161 }
9162 }
9163 else
9164 internal_error (__FILE__, __LINE__,
9165 _("Invalid dprintf style."));
9166
9167 gdb_assert (printf_line != NULL);
9168 /* Manufacture a printf sequence. */
9169 {
9170 struct command_line *printf_cmd_line = XNEW (struct command_line);
9171
9172 printf_cmd_line->control_type = simple_control;
9173 printf_cmd_line->body_count = 0;
9174 printf_cmd_line->body_list = NULL;
9175 printf_cmd_line->next = NULL;
9176 printf_cmd_line->line = printf_line;
9177
9178 breakpoint_set_commands (b, printf_cmd_line);
9179 }
9180 }
9181
9182 /* Update all dprintf commands, making their command lists reflect
9183 current style settings. */
9184
9185 static void
9186 update_dprintf_commands (char *args, int from_tty,
9187 struct cmd_list_element *c)
9188 {
9189 struct breakpoint *b;
9190
9191 ALL_BREAKPOINTS (b)
9192 {
9193 if (b->type == bp_dprintf)
9194 update_dprintf_command_list (b);
9195 }
9196 }
9197
9198 /* Create a breakpoint with SAL as location. Use LOCATION
9199 as a description of the location, and COND_STRING
9200 as condition expression. */
9201
9202 static void
9203 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9204 struct symtabs_and_lines sals,
9205 struct event_location *location,
9206 char *filter, char *cond_string,
9207 char *extra_string,
9208 enum bptype type, enum bpdisp disposition,
9209 int thread, int task, int ignore_count,
9210 const struct breakpoint_ops *ops, int from_tty,
9211 int enabled, int internal, unsigned flags,
9212 int display_canonical)
9213 {
9214 int i;
9215
9216 if (type == bp_hardware_breakpoint)
9217 {
9218 int target_resources_ok;
9219
9220 i = hw_breakpoint_used_count ();
9221 target_resources_ok =
9222 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9223 i + 1, 0);
9224 if (target_resources_ok == 0)
9225 error (_("No hardware breakpoint support in the target."));
9226 else if (target_resources_ok < 0)
9227 error (_("Hardware breakpoints used exceeds limit."));
9228 }
9229
9230 gdb_assert (sals.nelts > 0);
9231
9232 for (i = 0; i < sals.nelts; ++i)
9233 {
9234 struct symtab_and_line sal = sals.sals[i];
9235 struct bp_location *loc;
9236
9237 if (from_tty)
9238 {
9239 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9240 if (!loc_gdbarch)
9241 loc_gdbarch = gdbarch;
9242
9243 describe_other_breakpoints (loc_gdbarch,
9244 sal.pspace, sal.pc, sal.section, thread);
9245 }
9246
9247 if (i == 0)
9248 {
9249 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9250 b->thread = thread;
9251 b->task = task;
9252
9253 b->cond_string = cond_string;
9254 b->extra_string = extra_string;
9255 b->ignore_count = ignore_count;
9256 b->enable_state = enabled ? bp_enabled : bp_disabled;
9257 b->disposition = disposition;
9258
9259 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9260 b->loc->inserted = 1;
9261
9262 if (type == bp_static_tracepoint)
9263 {
9264 struct tracepoint *t = (struct tracepoint *) b;
9265 struct static_tracepoint_marker marker;
9266
9267 if (strace_marker_p (b))
9268 {
9269 /* We already know the marker exists, otherwise, we
9270 wouldn't see a sal for it. */
9271 const char *p = &event_location_to_string (b->location)[3];
9272 const char *endp;
9273 char *marker_str;
9274
9275 p = skip_spaces_const (p);
9276
9277 endp = skip_to_space_const (p);
9278
9279 marker_str = savestring (p, endp - p);
9280 t->static_trace_marker_id = marker_str;
9281
9282 printf_filtered (_("Probed static tracepoint "
9283 "marker \"%s\"\n"),
9284 t->static_trace_marker_id);
9285 }
9286 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9287 {
9288 t->static_trace_marker_id = xstrdup (marker.str_id);
9289 release_static_tracepoint_marker (&marker);
9290
9291 printf_filtered (_("Probed static tracepoint "
9292 "marker \"%s\"\n"),
9293 t->static_trace_marker_id);
9294 }
9295 else
9296 warning (_("Couldn't determine the static "
9297 "tracepoint marker to probe"));
9298 }
9299
9300 loc = b->loc;
9301 }
9302 else
9303 {
9304 loc = add_location_to_breakpoint (b, &sal);
9305 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9306 loc->inserted = 1;
9307 }
9308
9309 if (b->cond_string)
9310 {
9311 const char *arg = b->cond_string;
9312
9313 loc->cond = parse_exp_1 (&arg, loc->address,
9314 block_for_pc (loc->address), 0);
9315 if (*arg)
9316 error (_("Garbage '%s' follows condition"), arg);
9317 }
9318
9319 /* Dynamic printf requires and uses additional arguments on the
9320 command line, otherwise it's an error. */
9321 if (type == bp_dprintf)
9322 {
9323 if (b->extra_string)
9324 update_dprintf_command_list (b);
9325 else
9326 error (_("Format string required"));
9327 }
9328 else if (b->extra_string)
9329 error (_("Garbage '%s' at end of command"), b->extra_string);
9330 }
9331
9332 b->display_canonical = display_canonical;
9333 if (location != NULL)
9334 b->location = location;
9335 else
9336 b->location = new_address_location (b->loc->address);
9337 b->filter = filter;
9338 }
9339
9340 static void
9341 create_breakpoint_sal (struct gdbarch *gdbarch,
9342 struct symtabs_and_lines sals,
9343 struct event_location *location,
9344 char *filter, char *cond_string,
9345 char *extra_string,
9346 enum bptype type, enum bpdisp disposition,
9347 int thread, int task, int ignore_count,
9348 const struct breakpoint_ops *ops, int from_tty,
9349 int enabled, int internal, unsigned flags,
9350 int display_canonical)
9351 {
9352 struct breakpoint *b;
9353 struct cleanup *old_chain;
9354
9355 if (is_tracepoint_type (type))
9356 {
9357 struct tracepoint *t;
9358
9359 t = XCNEW (struct tracepoint);
9360 b = &t->base;
9361 }
9362 else
9363 b = XNEW (struct breakpoint);
9364
9365 old_chain = make_cleanup (xfree, b);
9366
9367 init_breakpoint_sal (b, gdbarch,
9368 sals, location,
9369 filter, cond_string, extra_string,
9370 type, disposition,
9371 thread, task, ignore_count,
9372 ops, from_tty,
9373 enabled, internal, flags,
9374 display_canonical);
9375 discard_cleanups (old_chain);
9376
9377 install_breakpoint (internal, b, 0);
9378 }
9379
9380 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9381 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9382 value. COND_STRING, if not NULL, specified the condition to be
9383 used for all breakpoints. Essentially the only case where
9384 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9385 function. In that case, it's still not possible to specify
9386 separate conditions for different overloaded functions, so
9387 we take just a single condition string.
9388
9389 NOTE: If the function succeeds, the caller is expected to cleanup
9390 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9391 array contents). If the function fails (error() is called), the
9392 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9393 COND and SALS arrays and each of those arrays contents. */
9394
9395 static void
9396 create_breakpoints_sal (struct gdbarch *gdbarch,
9397 struct linespec_result *canonical,
9398 char *cond_string, char *extra_string,
9399 enum bptype type, enum bpdisp disposition,
9400 int thread, int task, int ignore_count,
9401 const struct breakpoint_ops *ops, int from_tty,
9402 int enabled, int internal, unsigned flags)
9403 {
9404 int i;
9405 struct linespec_sals *lsal;
9406
9407 if (canonical->pre_expanded)
9408 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9409
9410 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9411 {
9412 /* Note that 'location' can be NULL in the case of a plain
9413 'break', without arguments. */
9414 struct event_location *location
9415 = (canonical->location != NULL
9416 ? copy_event_location (canonical->location) : NULL);
9417 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9418 struct cleanup *inner = make_cleanup_delete_event_location (location);
9419
9420 make_cleanup (xfree, filter_string);
9421 create_breakpoint_sal (gdbarch, lsal->sals,
9422 location,
9423 filter_string,
9424 cond_string, extra_string,
9425 type, disposition,
9426 thread, task, ignore_count, ops,
9427 from_tty, enabled, internal, flags,
9428 canonical->special_display);
9429 discard_cleanups (inner);
9430 }
9431 }
9432
9433 /* Parse LOCATION which is assumed to be a SAL specification possibly
9434 followed by conditionals. On return, SALS contains an array of SAL
9435 addresses found. LOCATION points to the end of the SAL (for
9436 linespec locations).
9437
9438 The array and the line spec strings are allocated on the heap, it is
9439 the caller's responsibility to free them. */
9440
9441 static void
9442 parse_breakpoint_sals (const struct event_location *location,
9443 struct linespec_result *canonical)
9444 {
9445 struct symtab_and_line cursal;
9446
9447 if (event_location_type (location) == LINESPEC_LOCATION)
9448 {
9449 const char *address = get_linespec_location (location);
9450
9451 if (address == NULL)
9452 {
9453 /* The last displayed codepoint, if it's valid, is our default
9454 breakpoint address. */
9455 if (last_displayed_sal_is_valid ())
9456 {
9457 struct linespec_sals lsal;
9458 struct symtab_and_line sal;
9459 CORE_ADDR pc;
9460
9461 init_sal (&sal); /* Initialize to zeroes. */
9462 lsal.sals.sals = XNEW (struct symtab_and_line);
9463
9464 /* Set sal's pspace, pc, symtab, and line to the values
9465 corresponding to the last call to print_frame_info.
9466 Be sure to reinitialize LINE with NOTCURRENT == 0
9467 as the breakpoint line number is inappropriate otherwise.
9468 find_pc_line would adjust PC, re-set it back. */
9469 get_last_displayed_sal (&sal);
9470 pc = sal.pc;
9471 sal = find_pc_line (pc, 0);
9472
9473 /* "break" without arguments is equivalent to "break *PC"
9474 where PC is the last displayed codepoint's address. So
9475 make sure to set sal.explicit_pc to prevent GDB from
9476 trying to expand the list of sals to include all other
9477 instances with the same symtab and line. */
9478 sal.pc = pc;
9479 sal.explicit_pc = 1;
9480
9481 lsal.sals.sals[0] = sal;
9482 lsal.sals.nelts = 1;
9483 lsal.canonical = NULL;
9484
9485 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9486 return;
9487 }
9488 else
9489 error (_("No default breakpoint address now."));
9490 }
9491 }
9492
9493 /* Force almost all breakpoints to be in terms of the
9494 current_source_symtab (which is decode_line_1's default).
9495 This should produce the results we want almost all of the
9496 time while leaving default_breakpoint_* alone.
9497
9498 ObjC: However, don't match an Objective-C method name which
9499 may have a '+' or '-' succeeded by a '['. */
9500 cursal = get_current_source_symtab_and_line ();
9501 if (last_displayed_sal_is_valid ())
9502 {
9503 const char *address = NULL;
9504
9505 if (event_location_type (location) == LINESPEC_LOCATION)
9506 address = get_linespec_location (location);
9507
9508 if (!cursal.symtab
9509 || (address != NULL
9510 && strchr ("+-", address[0]) != NULL
9511 && address[1] != '['))
9512 {
9513 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9514 get_last_displayed_symtab (),
9515 get_last_displayed_line (),
9516 canonical, NULL, NULL);
9517 return;
9518 }
9519 }
9520
9521 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9522 cursal.symtab, cursal.line, canonical, NULL, NULL);
9523 }
9524
9525
9526 /* Convert each SAL into a real PC. Verify that the PC can be
9527 inserted as a breakpoint. If it can't throw an error. */
9528
9529 static void
9530 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9531 {
9532 int i;
9533
9534 for (i = 0; i < sals->nelts; i++)
9535 resolve_sal_pc (&sals->sals[i]);
9536 }
9537
9538 /* Fast tracepoints may have restrictions on valid locations. For
9539 instance, a fast tracepoint using a jump instead of a trap will
9540 likely have to overwrite more bytes than a trap would, and so can
9541 only be placed where the instruction is longer than the jump, or a
9542 multi-instruction sequence does not have a jump into the middle of
9543 it, etc. */
9544
9545 static void
9546 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9547 struct symtabs_and_lines *sals)
9548 {
9549 int i, rslt;
9550 struct symtab_and_line *sal;
9551 char *msg;
9552 struct cleanup *old_chain;
9553
9554 for (i = 0; i < sals->nelts; i++)
9555 {
9556 struct gdbarch *sarch;
9557
9558 sal = &sals->sals[i];
9559
9560 sarch = get_sal_arch (*sal);
9561 /* We fall back to GDBARCH if there is no architecture
9562 associated with SAL. */
9563 if (sarch == NULL)
9564 sarch = gdbarch;
9565 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9566 old_chain = make_cleanup (xfree, msg);
9567
9568 if (!rslt)
9569 error (_("May not have a fast tracepoint at 0x%s%s"),
9570 paddress (sarch, sal->pc), (msg ? msg : ""));
9571
9572 do_cleanups (old_chain);
9573 }
9574 }
9575
9576 /* Given TOK, a string specification of condition and thread, as
9577 accepted by the 'break' command, extract the condition
9578 string and thread number and set *COND_STRING and *THREAD.
9579 PC identifies the context at which the condition should be parsed.
9580 If no condition is found, *COND_STRING is set to NULL.
9581 If no thread is found, *THREAD is set to -1. */
9582
9583 static void
9584 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9585 char **cond_string, int *thread, int *task,
9586 char **rest)
9587 {
9588 *cond_string = NULL;
9589 *thread = -1;
9590 *task = 0;
9591 *rest = NULL;
9592
9593 while (tok && *tok)
9594 {
9595 const char *end_tok;
9596 int toklen;
9597 const char *cond_start = NULL;
9598 const char *cond_end = NULL;
9599
9600 tok = skip_spaces_const (tok);
9601
9602 if ((*tok == '"' || *tok == ',') && rest)
9603 {
9604 *rest = savestring (tok, strlen (tok));
9605 return;
9606 }
9607
9608 end_tok = skip_to_space_const (tok);
9609
9610 toklen = end_tok - tok;
9611
9612 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9613 {
9614 struct expression *expr;
9615
9616 tok = cond_start = end_tok + 1;
9617 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9618 xfree (expr);
9619 cond_end = tok;
9620 *cond_string = savestring (cond_start, cond_end - cond_start);
9621 }
9622 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9623 {
9624 const char *tmptok;
9625 struct thread_info *thr;
9626
9627 tok = end_tok + 1;
9628 thr = parse_thread_id (tok, &tmptok);
9629 if (tok == tmptok)
9630 error (_("Junk after thread keyword."));
9631 *thread = thr->global_num;
9632 tok = tmptok;
9633 }
9634 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9635 {
9636 char *tmptok;
9637
9638 tok = end_tok + 1;
9639 *task = strtol (tok, &tmptok, 0);
9640 if (tok == tmptok)
9641 error (_("Junk after task keyword."));
9642 if (!valid_task_id (*task))
9643 error (_("Unknown task %d."), *task);
9644 tok = tmptok;
9645 }
9646 else if (rest)
9647 {
9648 *rest = savestring (tok, strlen (tok));
9649 return;
9650 }
9651 else
9652 error (_("Junk at end of arguments."));
9653 }
9654 }
9655
9656 /* Decode a static tracepoint marker spec. */
9657
9658 static struct symtabs_and_lines
9659 decode_static_tracepoint_spec (const char **arg_p)
9660 {
9661 VEC(static_tracepoint_marker_p) *markers = NULL;
9662 struct symtabs_and_lines sals;
9663 struct cleanup *old_chain;
9664 const char *p = &(*arg_p)[3];
9665 const char *endp;
9666 char *marker_str;
9667 int i;
9668
9669 p = skip_spaces_const (p);
9670
9671 endp = skip_to_space_const (p);
9672
9673 marker_str = savestring (p, endp - p);
9674 old_chain = make_cleanup (xfree, marker_str);
9675
9676 markers = target_static_tracepoint_markers_by_strid (marker_str);
9677 if (VEC_empty(static_tracepoint_marker_p, markers))
9678 error (_("No known static tracepoint marker named %s"), marker_str);
9679
9680 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9681 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9682
9683 for (i = 0; i < sals.nelts; i++)
9684 {
9685 struct static_tracepoint_marker *marker;
9686
9687 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9688
9689 init_sal (&sals.sals[i]);
9690
9691 sals.sals[i] = find_pc_line (marker->address, 0);
9692 sals.sals[i].pc = marker->address;
9693
9694 release_static_tracepoint_marker (marker);
9695 }
9696
9697 do_cleanups (old_chain);
9698
9699 *arg_p = endp;
9700 return sals;
9701 }
9702
9703 /* See breakpoint.h. */
9704
9705 int
9706 create_breakpoint (struct gdbarch *gdbarch,
9707 const struct event_location *location, char *cond_string,
9708 int thread, char *extra_string,
9709 int parse_extra,
9710 int tempflag, enum bptype type_wanted,
9711 int ignore_count,
9712 enum auto_boolean pending_break_support,
9713 const struct breakpoint_ops *ops,
9714 int from_tty, int enabled, int internal,
9715 unsigned flags)
9716 {
9717 struct linespec_result canonical;
9718 struct cleanup *old_chain;
9719 struct cleanup *bkpt_chain = NULL;
9720 int pending = 0;
9721 int task = 0;
9722 int prev_bkpt_count = breakpoint_count;
9723
9724 gdb_assert (ops != NULL);
9725
9726 /* If extra_string isn't useful, set it to NULL. */
9727 if (extra_string != NULL && *extra_string == '\0')
9728 extra_string = NULL;
9729
9730 init_linespec_result (&canonical);
9731
9732 TRY
9733 {
9734 ops->create_sals_from_location (location, &canonical, type_wanted);
9735 }
9736 CATCH (e, RETURN_MASK_ERROR)
9737 {
9738 /* If caller is interested in rc value from parse, set
9739 value. */
9740 if (e.error == NOT_FOUND_ERROR)
9741 {
9742 /* If pending breakpoint support is turned off, throw
9743 error. */
9744
9745 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9746 throw_exception (e);
9747
9748 exception_print (gdb_stderr, e);
9749
9750 /* If pending breakpoint support is auto query and the user
9751 selects no, then simply return the error code. */
9752 if (pending_break_support == AUTO_BOOLEAN_AUTO
9753 && !nquery (_("Make %s pending on future shared library load? "),
9754 bptype_string (type_wanted)))
9755 return 0;
9756
9757 /* At this point, either the user was queried about setting
9758 a pending breakpoint and selected yes, or pending
9759 breakpoint behavior is on and thus a pending breakpoint
9760 is defaulted on behalf of the user. */
9761 pending = 1;
9762 }
9763 else
9764 throw_exception (e);
9765 }
9766 END_CATCH
9767
9768 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9769 return 0;
9770
9771 /* Create a chain of things that always need to be cleaned up. */
9772 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9773
9774 /* ----------------------------- SNIP -----------------------------
9775 Anything added to the cleanup chain beyond this point is assumed
9776 to be part of a breakpoint. If the breakpoint create succeeds
9777 then the memory is not reclaimed. */
9778 bkpt_chain = make_cleanup (null_cleanup, 0);
9779
9780 /* Resolve all line numbers to PC's and verify that the addresses
9781 are ok for the target. */
9782 if (!pending)
9783 {
9784 int ix;
9785 struct linespec_sals *iter;
9786
9787 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9788 breakpoint_sals_to_pc (&iter->sals);
9789 }
9790
9791 /* Fast tracepoints may have additional restrictions on location. */
9792 if (!pending && type_wanted == bp_fast_tracepoint)
9793 {
9794 int ix;
9795 struct linespec_sals *iter;
9796
9797 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9798 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9799 }
9800
9801 /* Verify that condition can be parsed, before setting any
9802 breakpoints. Allocate a separate condition expression for each
9803 breakpoint. */
9804 if (!pending)
9805 {
9806 if (parse_extra)
9807 {
9808 char *rest;
9809 struct linespec_sals *lsal;
9810
9811 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9812
9813 /* Here we only parse 'arg' to separate condition
9814 from thread number, so parsing in context of first
9815 sal is OK. When setting the breakpoint we'll
9816 re-parse it in context of each sal. */
9817
9818 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9819 &cond_string, &thread, &task, &rest);
9820 if (cond_string)
9821 make_cleanup (xfree, cond_string);
9822 if (rest)
9823 make_cleanup (xfree, rest);
9824 if (rest)
9825 extra_string = rest;
9826 else
9827 extra_string = NULL;
9828 }
9829 else
9830 {
9831 if (type_wanted != bp_dprintf
9832 && extra_string != NULL && *extra_string != '\0')
9833 error (_("Garbage '%s' at end of location"), extra_string);
9834
9835 /* Create a private copy of condition string. */
9836 if (cond_string)
9837 {
9838 cond_string = xstrdup (cond_string);
9839 make_cleanup (xfree, cond_string);
9840 }
9841 /* Create a private copy of any extra string. */
9842 if (extra_string)
9843 {
9844 extra_string = xstrdup (extra_string);
9845 make_cleanup (xfree, extra_string);
9846 }
9847 }
9848
9849 ops->create_breakpoints_sal (gdbarch, &canonical,
9850 cond_string, extra_string, type_wanted,
9851 tempflag ? disp_del : disp_donttouch,
9852 thread, task, ignore_count, ops,
9853 from_tty, enabled, internal, flags);
9854 }
9855 else
9856 {
9857 struct breakpoint *b;
9858
9859 if (is_tracepoint_type (type_wanted))
9860 {
9861 struct tracepoint *t;
9862
9863 t = XCNEW (struct tracepoint);
9864 b = &t->base;
9865 }
9866 else
9867 b = XNEW (struct breakpoint);
9868
9869 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9870 b->location = copy_event_location (location);
9871
9872 if (parse_extra)
9873 b->cond_string = NULL;
9874 else
9875 {
9876 /* Create a private copy of condition string. */
9877 if (cond_string)
9878 {
9879 cond_string = xstrdup (cond_string);
9880 make_cleanup (xfree, cond_string);
9881 }
9882 b->cond_string = cond_string;
9883 b->thread = thread;
9884 }
9885
9886 /* Create a private copy of any extra string. */
9887 if (extra_string != NULL)
9888 {
9889 extra_string = xstrdup (extra_string);
9890 make_cleanup (xfree, extra_string);
9891 }
9892 b->extra_string = extra_string;
9893 b->ignore_count = ignore_count;
9894 b->disposition = tempflag ? disp_del : disp_donttouch;
9895 b->condition_not_parsed = 1;
9896 b->enable_state = enabled ? bp_enabled : bp_disabled;
9897 if ((type_wanted != bp_breakpoint
9898 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9899 b->pspace = current_program_space;
9900
9901 install_breakpoint (internal, b, 0);
9902 }
9903
9904 if (VEC_length (linespec_sals, canonical.sals) > 1)
9905 {
9906 warning (_("Multiple breakpoints were set.\nUse the "
9907 "\"delete\" command to delete unwanted breakpoints."));
9908 prev_breakpoint_count = prev_bkpt_count;
9909 }
9910
9911 /* That's it. Discard the cleanups for data inserted into the
9912 breakpoint. */
9913 discard_cleanups (bkpt_chain);
9914 /* But cleanup everything else. */
9915 do_cleanups (old_chain);
9916
9917 /* error call may happen here - have BKPT_CHAIN already discarded. */
9918 update_global_location_list (UGLL_MAY_INSERT);
9919
9920 return 1;
9921 }
9922
9923 /* Set a breakpoint.
9924 ARG is a string describing breakpoint address,
9925 condition, and thread.
9926 FLAG specifies if a breakpoint is hardware on,
9927 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9928 and BP_TEMPFLAG. */
9929
9930 static void
9931 break_command_1 (char *arg, int flag, int from_tty)
9932 {
9933 int tempflag = flag & BP_TEMPFLAG;
9934 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9935 ? bp_hardware_breakpoint
9936 : bp_breakpoint);
9937 struct breakpoint_ops *ops;
9938 struct event_location *location;
9939 struct cleanup *cleanup;
9940
9941 location = string_to_event_location (&arg, current_language);
9942 cleanup = make_cleanup_delete_event_location (location);
9943
9944 /* Matching breakpoints on probes. */
9945 if (location != NULL
9946 && event_location_type (location) == PROBE_LOCATION)
9947 ops = &bkpt_probe_breakpoint_ops;
9948 else
9949 ops = &bkpt_breakpoint_ops;
9950
9951 create_breakpoint (get_current_arch (),
9952 location,
9953 NULL, 0, arg, 1 /* parse arg */,
9954 tempflag, type_wanted,
9955 0 /* Ignore count */,
9956 pending_break_support,
9957 ops,
9958 from_tty,
9959 1 /* enabled */,
9960 0 /* internal */,
9961 0);
9962 do_cleanups (cleanup);
9963 }
9964
9965 /* Helper function for break_command_1 and disassemble_command. */
9966
9967 void
9968 resolve_sal_pc (struct symtab_and_line *sal)
9969 {
9970 CORE_ADDR pc;
9971
9972 if (sal->pc == 0 && sal->symtab != NULL)
9973 {
9974 if (!find_line_pc (sal->symtab, sal->line, &pc))
9975 error (_("No line %d in file \"%s\"."),
9976 sal->line, symtab_to_filename_for_display (sal->symtab));
9977 sal->pc = pc;
9978
9979 /* If this SAL corresponds to a breakpoint inserted using a line
9980 number, then skip the function prologue if necessary. */
9981 if (sal->explicit_line)
9982 skip_prologue_sal (sal);
9983 }
9984
9985 if (sal->section == 0 && sal->symtab != NULL)
9986 {
9987 const struct blockvector *bv;
9988 const struct block *b;
9989 struct symbol *sym;
9990
9991 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9992 SYMTAB_COMPUNIT (sal->symtab));
9993 if (bv != NULL)
9994 {
9995 sym = block_linkage_function (b);
9996 if (sym != NULL)
9997 {
9998 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9999 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10000 sym);
10001 }
10002 else
10003 {
10004 /* It really is worthwhile to have the section, so we'll
10005 just have to look harder. This case can be executed
10006 if we have line numbers but no functions (as can
10007 happen in assembly source). */
10008
10009 struct bound_minimal_symbol msym;
10010 struct cleanup *old_chain = save_current_space_and_thread ();
10011
10012 switch_to_program_space_and_thread (sal->pspace);
10013
10014 msym = lookup_minimal_symbol_by_pc (sal->pc);
10015 if (msym.minsym)
10016 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10017
10018 do_cleanups (old_chain);
10019 }
10020 }
10021 }
10022 }
10023
10024 void
10025 break_command (char *arg, int from_tty)
10026 {
10027 break_command_1 (arg, 0, from_tty);
10028 }
10029
10030 void
10031 tbreak_command (char *arg, int from_tty)
10032 {
10033 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10034 }
10035
10036 static void
10037 hbreak_command (char *arg, int from_tty)
10038 {
10039 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10040 }
10041
10042 static void
10043 thbreak_command (char *arg, int from_tty)
10044 {
10045 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10046 }
10047
10048 static void
10049 stop_command (char *arg, int from_tty)
10050 {
10051 printf_filtered (_("Specify the type of breakpoint to set.\n\
10052 Usage: stop in <function | address>\n\
10053 stop at <line>\n"));
10054 }
10055
10056 static void
10057 stopin_command (char *arg, int from_tty)
10058 {
10059 int badInput = 0;
10060
10061 if (arg == (char *) NULL)
10062 badInput = 1;
10063 else if (*arg != '*')
10064 {
10065 char *argptr = arg;
10066 int hasColon = 0;
10067
10068 /* Look for a ':'. If this is a line number specification, then
10069 say it is bad, otherwise, it should be an address or
10070 function/method name. */
10071 while (*argptr && !hasColon)
10072 {
10073 hasColon = (*argptr == ':');
10074 argptr++;
10075 }
10076
10077 if (hasColon)
10078 badInput = (*argptr != ':'); /* Not a class::method */
10079 else
10080 badInput = isdigit (*arg); /* a simple line number */
10081 }
10082
10083 if (badInput)
10084 printf_filtered (_("Usage: stop in <function | address>\n"));
10085 else
10086 break_command_1 (arg, 0, from_tty);
10087 }
10088
10089 static void
10090 stopat_command (char *arg, int from_tty)
10091 {
10092 int badInput = 0;
10093
10094 if (arg == (char *) NULL || *arg == '*') /* no line number */
10095 badInput = 1;
10096 else
10097 {
10098 char *argptr = arg;
10099 int hasColon = 0;
10100
10101 /* Look for a ':'. If there is a '::' then get out, otherwise
10102 it is probably a line number. */
10103 while (*argptr && !hasColon)
10104 {
10105 hasColon = (*argptr == ':');
10106 argptr++;
10107 }
10108
10109 if (hasColon)
10110 badInput = (*argptr == ':'); /* we have class::method */
10111 else
10112 badInput = !isdigit (*arg); /* not a line number */
10113 }
10114
10115 if (badInput)
10116 printf_filtered (_("Usage: stop at <line>\n"));
10117 else
10118 break_command_1 (arg, 0, from_tty);
10119 }
10120
10121 /* The dynamic printf command is mostly like a regular breakpoint, but
10122 with a prewired command list consisting of a single output command,
10123 built from extra arguments supplied on the dprintf command
10124 line. */
10125
10126 static void
10127 dprintf_command (char *arg, int from_tty)
10128 {
10129 struct event_location *location;
10130 struct cleanup *cleanup;
10131
10132 location = string_to_event_location (&arg, current_language);
10133 cleanup = make_cleanup_delete_event_location (location);
10134
10135 /* If non-NULL, ARG should have been advanced past the location;
10136 the next character must be ','. */
10137 if (arg != NULL)
10138 {
10139 if (arg[0] != ',' || arg[1] == '\0')
10140 error (_("Format string required"));
10141 else
10142 {
10143 /* Skip the comma. */
10144 ++arg;
10145 }
10146 }
10147
10148 create_breakpoint (get_current_arch (),
10149 location,
10150 NULL, 0, arg, 1 /* parse arg */,
10151 0, bp_dprintf,
10152 0 /* Ignore count */,
10153 pending_break_support,
10154 &dprintf_breakpoint_ops,
10155 from_tty,
10156 1 /* enabled */,
10157 0 /* internal */,
10158 0);
10159 do_cleanups (cleanup);
10160 }
10161
10162 static void
10163 agent_printf_command (char *arg, int from_tty)
10164 {
10165 error (_("May only run agent-printf on the target"));
10166 }
10167
10168 /* Implement the "breakpoint_hit" breakpoint_ops method for
10169 ranged breakpoints. */
10170
10171 static int
10172 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10173 struct address_space *aspace,
10174 CORE_ADDR bp_addr,
10175 const struct target_waitstatus *ws)
10176 {
10177 if (ws->kind != TARGET_WAITKIND_STOPPED
10178 || ws->value.sig != GDB_SIGNAL_TRAP)
10179 return 0;
10180
10181 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10182 bl->length, aspace, bp_addr);
10183 }
10184
10185 /* Implement the "resources_needed" breakpoint_ops method for
10186 ranged breakpoints. */
10187
10188 static int
10189 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10190 {
10191 return target_ranged_break_num_registers ();
10192 }
10193
10194 /* Implement the "print_it" breakpoint_ops method for
10195 ranged breakpoints. */
10196
10197 static enum print_stop_action
10198 print_it_ranged_breakpoint (bpstat bs)
10199 {
10200 struct breakpoint *b = bs->breakpoint_at;
10201 struct bp_location *bl = b->loc;
10202 struct ui_out *uiout = current_uiout;
10203
10204 gdb_assert (b->type == bp_hardware_breakpoint);
10205
10206 /* Ranged breakpoints have only one location. */
10207 gdb_assert (bl && bl->next == NULL);
10208
10209 annotate_breakpoint (b->number);
10210 if (b->disposition == disp_del)
10211 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10212 else
10213 ui_out_text (uiout, "\nRanged breakpoint ");
10214 if (ui_out_is_mi_like_p (uiout))
10215 {
10216 ui_out_field_string (uiout, "reason",
10217 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10218 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10219 }
10220 ui_out_field_int (uiout, "bkptno", b->number);
10221 ui_out_text (uiout, ", ");
10222
10223 return PRINT_SRC_AND_LOC;
10224 }
10225
10226 /* Implement the "print_one" breakpoint_ops method for
10227 ranged breakpoints. */
10228
10229 static void
10230 print_one_ranged_breakpoint (struct breakpoint *b,
10231 struct bp_location **last_loc)
10232 {
10233 struct bp_location *bl = b->loc;
10234 struct value_print_options opts;
10235 struct ui_out *uiout = current_uiout;
10236
10237 /* Ranged breakpoints have only one location. */
10238 gdb_assert (bl && bl->next == NULL);
10239
10240 get_user_print_options (&opts);
10241
10242 if (opts.addressprint)
10243 /* We don't print the address range here, it will be printed later
10244 by print_one_detail_ranged_breakpoint. */
10245 ui_out_field_skip (uiout, "addr");
10246 annotate_field (5);
10247 print_breakpoint_location (b, bl);
10248 *last_loc = bl;
10249 }
10250
10251 /* Implement the "print_one_detail" breakpoint_ops method for
10252 ranged breakpoints. */
10253
10254 static void
10255 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10256 struct ui_out *uiout)
10257 {
10258 CORE_ADDR address_start, address_end;
10259 struct bp_location *bl = b->loc;
10260 struct ui_file *stb = mem_fileopen ();
10261 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10262
10263 gdb_assert (bl);
10264
10265 address_start = bl->address;
10266 address_end = address_start + bl->length - 1;
10267
10268 ui_out_text (uiout, "\taddress range: ");
10269 fprintf_unfiltered (stb, "[%s, %s]",
10270 print_core_address (bl->gdbarch, address_start),
10271 print_core_address (bl->gdbarch, address_end));
10272 ui_out_field_stream (uiout, "addr", stb);
10273 ui_out_text (uiout, "\n");
10274
10275 do_cleanups (cleanup);
10276 }
10277
10278 /* Implement the "print_mention" breakpoint_ops method for
10279 ranged breakpoints. */
10280
10281 static void
10282 print_mention_ranged_breakpoint (struct breakpoint *b)
10283 {
10284 struct bp_location *bl = b->loc;
10285 struct ui_out *uiout = current_uiout;
10286
10287 gdb_assert (bl);
10288 gdb_assert (b->type == bp_hardware_breakpoint);
10289
10290 if (ui_out_is_mi_like_p (uiout))
10291 return;
10292
10293 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10294 b->number, paddress (bl->gdbarch, bl->address),
10295 paddress (bl->gdbarch, bl->address + bl->length - 1));
10296 }
10297
10298 /* Implement the "print_recreate" breakpoint_ops method for
10299 ranged breakpoints. */
10300
10301 static void
10302 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10303 {
10304 fprintf_unfiltered (fp, "break-range %s, %s",
10305 event_location_to_string (b->location),
10306 event_location_to_string (b->location_range_end));
10307 print_recreate_thread (b, fp);
10308 }
10309
10310 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10311
10312 static struct breakpoint_ops ranged_breakpoint_ops;
10313
10314 /* Find the address where the end of the breakpoint range should be
10315 placed, given the SAL of the end of the range. This is so that if
10316 the user provides a line number, the end of the range is set to the
10317 last instruction of the given line. */
10318
10319 static CORE_ADDR
10320 find_breakpoint_range_end (struct symtab_and_line sal)
10321 {
10322 CORE_ADDR end;
10323
10324 /* If the user provided a PC value, use it. Otherwise,
10325 find the address of the end of the given location. */
10326 if (sal.explicit_pc)
10327 end = sal.pc;
10328 else
10329 {
10330 int ret;
10331 CORE_ADDR start;
10332
10333 ret = find_line_pc_range (sal, &start, &end);
10334 if (!ret)
10335 error (_("Could not find location of the end of the range."));
10336
10337 /* find_line_pc_range returns the start of the next line. */
10338 end--;
10339 }
10340
10341 return end;
10342 }
10343
10344 /* Implement the "break-range" CLI command. */
10345
10346 static void
10347 break_range_command (char *arg, int from_tty)
10348 {
10349 char *arg_start, *addr_string_start, *addr_string_end;
10350 struct linespec_result canonical_start, canonical_end;
10351 int bp_count, can_use_bp, length;
10352 CORE_ADDR end;
10353 struct breakpoint *b;
10354 struct symtab_and_line sal_start, sal_end;
10355 struct cleanup *cleanup_bkpt;
10356 struct linespec_sals *lsal_start, *lsal_end;
10357 struct event_location *start_location, *end_location;
10358
10359 /* We don't support software ranged breakpoints. */
10360 if (target_ranged_break_num_registers () < 0)
10361 error (_("This target does not support hardware ranged breakpoints."));
10362
10363 bp_count = hw_breakpoint_used_count ();
10364 bp_count += target_ranged_break_num_registers ();
10365 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10366 bp_count, 0);
10367 if (can_use_bp < 0)
10368 error (_("Hardware breakpoints used exceeds limit."));
10369
10370 arg = skip_spaces (arg);
10371 if (arg == NULL || arg[0] == '\0')
10372 error(_("No address range specified."));
10373
10374 init_linespec_result (&canonical_start);
10375
10376 arg_start = arg;
10377 start_location = string_to_event_location (&arg, current_language);
10378 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10379 parse_breakpoint_sals (start_location, &canonical_start);
10380 make_cleanup_destroy_linespec_result (&canonical_start);
10381
10382 if (arg[0] != ',')
10383 error (_("Too few arguments."));
10384 else if (VEC_empty (linespec_sals, canonical_start.sals))
10385 error (_("Could not find location of the beginning of the range."));
10386
10387 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10388
10389 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10390 || lsal_start->sals.nelts != 1)
10391 error (_("Cannot create a ranged breakpoint with multiple locations."));
10392
10393 sal_start = lsal_start->sals.sals[0];
10394 addr_string_start = savestring (arg_start, arg - arg_start);
10395 make_cleanup (xfree, addr_string_start);
10396
10397 arg++; /* Skip the comma. */
10398 arg = skip_spaces (arg);
10399
10400 /* Parse the end location. */
10401
10402 init_linespec_result (&canonical_end);
10403 arg_start = arg;
10404
10405 /* We call decode_line_full directly here instead of using
10406 parse_breakpoint_sals because we need to specify the start location's
10407 symtab and line as the default symtab and line for the end of the
10408 range. This makes it possible to have ranges like "foo.c:27, +14",
10409 where +14 means 14 lines from the start location. */
10410 end_location = string_to_event_location (&arg, current_language);
10411 make_cleanup_delete_event_location (end_location);
10412 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE,
10413 sal_start.symtab, sal_start.line,
10414 &canonical_end, NULL, NULL);
10415
10416 make_cleanup_destroy_linespec_result (&canonical_end);
10417
10418 if (VEC_empty (linespec_sals, canonical_end.sals))
10419 error (_("Could not find location of the end of the range."));
10420
10421 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10422 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10423 || lsal_end->sals.nelts != 1)
10424 error (_("Cannot create a ranged breakpoint with multiple locations."));
10425
10426 sal_end = lsal_end->sals.sals[0];
10427
10428 end = find_breakpoint_range_end (sal_end);
10429 if (sal_start.pc > end)
10430 error (_("Invalid address range, end precedes start."));
10431
10432 length = end - sal_start.pc + 1;
10433 if (length < 0)
10434 /* Length overflowed. */
10435 error (_("Address range too large."));
10436 else if (length == 1)
10437 {
10438 /* This range is simple enough to be handled by
10439 the `hbreak' command. */
10440 hbreak_command (addr_string_start, 1);
10441
10442 do_cleanups (cleanup_bkpt);
10443
10444 return;
10445 }
10446
10447 /* Now set up the breakpoint. */
10448 b = set_raw_breakpoint (get_current_arch (), sal_start,
10449 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10450 set_breakpoint_count (breakpoint_count + 1);
10451 b->number = breakpoint_count;
10452 b->disposition = disp_donttouch;
10453 b->location = copy_event_location (start_location);
10454 b->location_range_end = copy_event_location (end_location);
10455 b->loc->length = length;
10456
10457 do_cleanups (cleanup_bkpt);
10458
10459 mention (b);
10460 observer_notify_breakpoint_created (b);
10461 update_global_location_list (UGLL_MAY_INSERT);
10462 }
10463
10464 /* Return non-zero if EXP is verified as constant. Returned zero
10465 means EXP is variable. Also the constant detection may fail for
10466 some constant expressions and in such case still falsely return
10467 zero. */
10468
10469 static int
10470 watchpoint_exp_is_const (const struct expression *exp)
10471 {
10472 int i = exp->nelts;
10473
10474 while (i > 0)
10475 {
10476 int oplenp, argsp;
10477
10478 /* We are only interested in the descriptor of each element. */
10479 operator_length (exp, i, &oplenp, &argsp);
10480 i -= oplenp;
10481
10482 switch (exp->elts[i].opcode)
10483 {
10484 case BINOP_ADD:
10485 case BINOP_SUB:
10486 case BINOP_MUL:
10487 case BINOP_DIV:
10488 case BINOP_REM:
10489 case BINOP_MOD:
10490 case BINOP_LSH:
10491 case BINOP_RSH:
10492 case BINOP_LOGICAL_AND:
10493 case BINOP_LOGICAL_OR:
10494 case BINOP_BITWISE_AND:
10495 case BINOP_BITWISE_IOR:
10496 case BINOP_BITWISE_XOR:
10497 case BINOP_EQUAL:
10498 case BINOP_NOTEQUAL:
10499 case BINOP_LESS:
10500 case BINOP_GTR:
10501 case BINOP_LEQ:
10502 case BINOP_GEQ:
10503 case BINOP_REPEAT:
10504 case BINOP_COMMA:
10505 case BINOP_EXP:
10506 case BINOP_MIN:
10507 case BINOP_MAX:
10508 case BINOP_INTDIV:
10509 case BINOP_CONCAT:
10510 case TERNOP_COND:
10511 case TERNOP_SLICE:
10512
10513 case OP_LONG:
10514 case OP_DOUBLE:
10515 case OP_DECFLOAT:
10516 case OP_LAST:
10517 case OP_COMPLEX:
10518 case OP_STRING:
10519 case OP_ARRAY:
10520 case OP_TYPE:
10521 case OP_TYPEOF:
10522 case OP_DECLTYPE:
10523 case OP_TYPEID:
10524 case OP_NAME:
10525 case OP_OBJC_NSSTRING:
10526
10527 case UNOP_NEG:
10528 case UNOP_LOGICAL_NOT:
10529 case UNOP_COMPLEMENT:
10530 case UNOP_ADDR:
10531 case UNOP_HIGH:
10532 case UNOP_CAST:
10533
10534 case UNOP_CAST_TYPE:
10535 case UNOP_REINTERPRET_CAST:
10536 case UNOP_DYNAMIC_CAST:
10537 /* Unary, binary and ternary operators: We have to check
10538 their operands. If they are constant, then so is the
10539 result of that operation. For instance, if A and B are
10540 determined to be constants, then so is "A + B".
10541
10542 UNOP_IND is one exception to the rule above, because the
10543 value of *ADDR is not necessarily a constant, even when
10544 ADDR is. */
10545 break;
10546
10547 case OP_VAR_VALUE:
10548 /* Check whether the associated symbol is a constant.
10549
10550 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10551 possible that a buggy compiler could mark a variable as
10552 constant even when it is not, and TYPE_CONST would return
10553 true in this case, while SYMBOL_CLASS wouldn't.
10554
10555 We also have to check for function symbols because they
10556 are always constant. */
10557 {
10558 struct symbol *s = exp->elts[i + 2].symbol;
10559
10560 if (SYMBOL_CLASS (s) != LOC_BLOCK
10561 && SYMBOL_CLASS (s) != LOC_CONST
10562 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10563 return 0;
10564 break;
10565 }
10566
10567 /* The default action is to return 0 because we are using
10568 the optimistic approach here: If we don't know something,
10569 then it is not a constant. */
10570 default:
10571 return 0;
10572 }
10573 }
10574
10575 return 1;
10576 }
10577
10578 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10579
10580 static void
10581 dtor_watchpoint (struct breakpoint *self)
10582 {
10583 struct watchpoint *w = (struct watchpoint *) self;
10584
10585 xfree (w->cond_exp);
10586 xfree (w->exp);
10587 xfree (w->exp_string);
10588 xfree (w->exp_string_reparse);
10589 value_free (w->val);
10590
10591 base_breakpoint_ops.dtor (self);
10592 }
10593
10594 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10595
10596 static void
10597 re_set_watchpoint (struct breakpoint *b)
10598 {
10599 struct watchpoint *w = (struct watchpoint *) b;
10600
10601 /* Watchpoint can be either on expression using entirely global
10602 variables, or it can be on local variables.
10603
10604 Watchpoints of the first kind are never auto-deleted, and even
10605 persist across program restarts. Since they can use variables
10606 from shared libraries, we need to reparse expression as libraries
10607 are loaded and unloaded.
10608
10609 Watchpoints on local variables can also change meaning as result
10610 of solib event. For example, if a watchpoint uses both a local
10611 and a global variables in expression, it's a local watchpoint,
10612 but unloading of a shared library will make the expression
10613 invalid. This is not a very common use case, but we still
10614 re-evaluate expression, to avoid surprises to the user.
10615
10616 Note that for local watchpoints, we re-evaluate it only if
10617 watchpoints frame id is still valid. If it's not, it means the
10618 watchpoint is out of scope and will be deleted soon. In fact,
10619 I'm not sure we'll ever be called in this case.
10620
10621 If a local watchpoint's frame id is still valid, then
10622 w->exp_valid_block is likewise valid, and we can safely use it.
10623
10624 Don't do anything about disabled watchpoints, since they will be
10625 reevaluated again when enabled. */
10626 update_watchpoint (w, 1 /* reparse */);
10627 }
10628
10629 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10630
10631 static int
10632 insert_watchpoint (struct bp_location *bl)
10633 {
10634 struct watchpoint *w = (struct watchpoint *) bl->owner;
10635 int length = w->exact ? 1 : bl->length;
10636
10637 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10638 w->cond_exp);
10639 }
10640
10641 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10642
10643 static int
10644 remove_watchpoint (struct bp_location *bl)
10645 {
10646 struct watchpoint *w = (struct watchpoint *) bl->owner;
10647 int length = w->exact ? 1 : bl->length;
10648
10649 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10650 w->cond_exp);
10651 }
10652
10653 static int
10654 breakpoint_hit_watchpoint (const struct bp_location *bl,
10655 struct address_space *aspace, CORE_ADDR bp_addr,
10656 const struct target_waitstatus *ws)
10657 {
10658 struct breakpoint *b = bl->owner;
10659 struct watchpoint *w = (struct watchpoint *) b;
10660
10661 /* Continuable hardware watchpoints are treated as non-existent if the
10662 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10663 some data address). Otherwise gdb won't stop on a break instruction
10664 in the code (not from a breakpoint) when a hardware watchpoint has
10665 been defined. Also skip watchpoints which we know did not trigger
10666 (did not match the data address). */
10667 if (is_hardware_watchpoint (b)
10668 && w->watchpoint_triggered == watch_triggered_no)
10669 return 0;
10670
10671 return 1;
10672 }
10673
10674 static void
10675 check_status_watchpoint (bpstat bs)
10676 {
10677 gdb_assert (is_watchpoint (bs->breakpoint_at));
10678
10679 bpstat_check_watchpoint (bs);
10680 }
10681
10682 /* Implement the "resources_needed" breakpoint_ops method for
10683 hardware watchpoints. */
10684
10685 static int
10686 resources_needed_watchpoint (const struct bp_location *bl)
10687 {
10688 struct watchpoint *w = (struct watchpoint *) bl->owner;
10689 int length = w->exact? 1 : bl->length;
10690
10691 return target_region_ok_for_hw_watchpoint (bl->address, length);
10692 }
10693
10694 /* Implement the "works_in_software_mode" breakpoint_ops method for
10695 hardware watchpoints. */
10696
10697 static int
10698 works_in_software_mode_watchpoint (const struct breakpoint *b)
10699 {
10700 /* Read and access watchpoints only work with hardware support. */
10701 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10702 }
10703
10704 static enum print_stop_action
10705 print_it_watchpoint (bpstat bs)
10706 {
10707 struct cleanup *old_chain;
10708 struct breakpoint *b;
10709 struct ui_file *stb;
10710 enum print_stop_action result;
10711 struct watchpoint *w;
10712 struct ui_out *uiout = current_uiout;
10713
10714 gdb_assert (bs->bp_location_at != NULL);
10715
10716 b = bs->breakpoint_at;
10717 w = (struct watchpoint *) b;
10718
10719 stb = mem_fileopen ();
10720 old_chain = make_cleanup_ui_file_delete (stb);
10721
10722 switch (b->type)
10723 {
10724 case bp_watchpoint:
10725 case bp_hardware_watchpoint:
10726 annotate_watchpoint (b->number);
10727 if (ui_out_is_mi_like_p (uiout))
10728 ui_out_field_string
10729 (uiout, "reason",
10730 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10731 mention (b);
10732 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10733 ui_out_text (uiout, "\nOld value = ");
10734 watchpoint_value_print (bs->old_val, stb);
10735 ui_out_field_stream (uiout, "old", stb);
10736 ui_out_text (uiout, "\nNew value = ");
10737 watchpoint_value_print (w->val, stb);
10738 ui_out_field_stream (uiout, "new", stb);
10739 ui_out_text (uiout, "\n");
10740 /* More than one watchpoint may have been triggered. */
10741 result = PRINT_UNKNOWN;
10742 break;
10743
10744 case bp_read_watchpoint:
10745 if (ui_out_is_mi_like_p (uiout))
10746 ui_out_field_string
10747 (uiout, "reason",
10748 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10749 mention (b);
10750 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10751 ui_out_text (uiout, "\nValue = ");
10752 watchpoint_value_print (w->val, stb);
10753 ui_out_field_stream (uiout, "value", stb);
10754 ui_out_text (uiout, "\n");
10755 result = PRINT_UNKNOWN;
10756 break;
10757
10758 case bp_access_watchpoint:
10759 if (bs->old_val != NULL)
10760 {
10761 annotate_watchpoint (b->number);
10762 if (ui_out_is_mi_like_p (uiout))
10763 ui_out_field_string
10764 (uiout, "reason",
10765 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10766 mention (b);
10767 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10768 ui_out_text (uiout, "\nOld value = ");
10769 watchpoint_value_print (bs->old_val, stb);
10770 ui_out_field_stream (uiout, "old", stb);
10771 ui_out_text (uiout, "\nNew value = ");
10772 }
10773 else
10774 {
10775 mention (b);
10776 if (ui_out_is_mi_like_p (uiout))
10777 ui_out_field_string
10778 (uiout, "reason",
10779 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10780 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10781 ui_out_text (uiout, "\nValue = ");
10782 }
10783 watchpoint_value_print (w->val, stb);
10784 ui_out_field_stream (uiout, "new", stb);
10785 ui_out_text (uiout, "\n");
10786 result = PRINT_UNKNOWN;
10787 break;
10788 default:
10789 result = PRINT_UNKNOWN;
10790 }
10791
10792 do_cleanups (old_chain);
10793 return result;
10794 }
10795
10796 /* Implement the "print_mention" breakpoint_ops method for hardware
10797 watchpoints. */
10798
10799 static void
10800 print_mention_watchpoint (struct breakpoint *b)
10801 {
10802 struct cleanup *ui_out_chain;
10803 struct watchpoint *w = (struct watchpoint *) b;
10804 struct ui_out *uiout = current_uiout;
10805
10806 switch (b->type)
10807 {
10808 case bp_watchpoint:
10809 ui_out_text (uiout, "Watchpoint ");
10810 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10811 break;
10812 case bp_hardware_watchpoint:
10813 ui_out_text (uiout, "Hardware watchpoint ");
10814 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10815 break;
10816 case bp_read_watchpoint:
10817 ui_out_text (uiout, "Hardware read watchpoint ");
10818 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10819 break;
10820 case bp_access_watchpoint:
10821 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10822 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10823 break;
10824 default:
10825 internal_error (__FILE__, __LINE__,
10826 _("Invalid hardware watchpoint type."));
10827 }
10828
10829 ui_out_field_int (uiout, "number", b->number);
10830 ui_out_text (uiout, ": ");
10831 ui_out_field_string (uiout, "exp", w->exp_string);
10832 do_cleanups (ui_out_chain);
10833 }
10834
10835 /* Implement the "print_recreate" breakpoint_ops method for
10836 watchpoints. */
10837
10838 static void
10839 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10840 {
10841 struct watchpoint *w = (struct watchpoint *) b;
10842
10843 switch (b->type)
10844 {
10845 case bp_watchpoint:
10846 case bp_hardware_watchpoint:
10847 fprintf_unfiltered (fp, "watch");
10848 break;
10849 case bp_read_watchpoint:
10850 fprintf_unfiltered (fp, "rwatch");
10851 break;
10852 case bp_access_watchpoint:
10853 fprintf_unfiltered (fp, "awatch");
10854 break;
10855 default:
10856 internal_error (__FILE__, __LINE__,
10857 _("Invalid watchpoint type."));
10858 }
10859
10860 fprintf_unfiltered (fp, " %s", w->exp_string);
10861 print_recreate_thread (b, fp);
10862 }
10863
10864 /* Implement the "explains_signal" breakpoint_ops method for
10865 watchpoints. */
10866
10867 static int
10868 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10869 {
10870 /* A software watchpoint cannot cause a signal other than
10871 GDB_SIGNAL_TRAP. */
10872 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10873 return 0;
10874
10875 return 1;
10876 }
10877
10878 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10879
10880 static struct breakpoint_ops watchpoint_breakpoint_ops;
10881
10882 /* Implement the "insert" breakpoint_ops method for
10883 masked hardware watchpoints. */
10884
10885 static int
10886 insert_masked_watchpoint (struct bp_location *bl)
10887 {
10888 struct watchpoint *w = (struct watchpoint *) bl->owner;
10889
10890 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10891 bl->watchpoint_type);
10892 }
10893
10894 /* Implement the "remove" breakpoint_ops method for
10895 masked hardware watchpoints. */
10896
10897 static int
10898 remove_masked_watchpoint (struct bp_location *bl)
10899 {
10900 struct watchpoint *w = (struct watchpoint *) bl->owner;
10901
10902 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10903 bl->watchpoint_type);
10904 }
10905
10906 /* Implement the "resources_needed" breakpoint_ops method for
10907 masked hardware watchpoints. */
10908
10909 static int
10910 resources_needed_masked_watchpoint (const struct bp_location *bl)
10911 {
10912 struct watchpoint *w = (struct watchpoint *) bl->owner;
10913
10914 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10915 }
10916
10917 /* Implement the "works_in_software_mode" breakpoint_ops method for
10918 masked hardware watchpoints. */
10919
10920 static int
10921 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10922 {
10923 return 0;
10924 }
10925
10926 /* Implement the "print_it" breakpoint_ops method for
10927 masked hardware watchpoints. */
10928
10929 static enum print_stop_action
10930 print_it_masked_watchpoint (bpstat bs)
10931 {
10932 struct breakpoint *b = bs->breakpoint_at;
10933 struct ui_out *uiout = current_uiout;
10934
10935 /* Masked watchpoints have only one location. */
10936 gdb_assert (b->loc && b->loc->next == NULL);
10937
10938 switch (b->type)
10939 {
10940 case bp_hardware_watchpoint:
10941 annotate_watchpoint (b->number);
10942 if (ui_out_is_mi_like_p (uiout))
10943 ui_out_field_string
10944 (uiout, "reason",
10945 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10946 break;
10947
10948 case bp_read_watchpoint:
10949 if (ui_out_is_mi_like_p (uiout))
10950 ui_out_field_string
10951 (uiout, "reason",
10952 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10953 break;
10954
10955 case bp_access_watchpoint:
10956 if (ui_out_is_mi_like_p (uiout))
10957 ui_out_field_string
10958 (uiout, "reason",
10959 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10960 break;
10961 default:
10962 internal_error (__FILE__, __LINE__,
10963 _("Invalid hardware watchpoint type."));
10964 }
10965
10966 mention (b);
10967 ui_out_text (uiout, _("\n\
10968 Check the underlying instruction at PC for the memory\n\
10969 address and value which triggered this watchpoint.\n"));
10970 ui_out_text (uiout, "\n");
10971
10972 /* More than one watchpoint may have been triggered. */
10973 return PRINT_UNKNOWN;
10974 }
10975
10976 /* Implement the "print_one_detail" breakpoint_ops method for
10977 masked hardware watchpoints. */
10978
10979 static void
10980 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10981 struct ui_out *uiout)
10982 {
10983 struct watchpoint *w = (struct watchpoint *) b;
10984
10985 /* Masked watchpoints have only one location. */
10986 gdb_assert (b->loc && b->loc->next == NULL);
10987
10988 ui_out_text (uiout, "\tmask ");
10989 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
10990 ui_out_text (uiout, "\n");
10991 }
10992
10993 /* Implement the "print_mention" breakpoint_ops method for
10994 masked hardware watchpoints. */
10995
10996 static void
10997 print_mention_masked_watchpoint (struct breakpoint *b)
10998 {
10999 struct watchpoint *w = (struct watchpoint *) b;
11000 struct ui_out *uiout = current_uiout;
11001 struct cleanup *ui_out_chain;
11002
11003 switch (b->type)
11004 {
11005 case bp_hardware_watchpoint:
11006 ui_out_text (uiout, "Masked hardware watchpoint ");
11007 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11008 break;
11009 case bp_read_watchpoint:
11010 ui_out_text (uiout, "Masked hardware read watchpoint ");
11011 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11012 break;
11013 case bp_access_watchpoint:
11014 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11015 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11016 break;
11017 default:
11018 internal_error (__FILE__, __LINE__,
11019 _("Invalid hardware watchpoint type."));
11020 }
11021
11022 ui_out_field_int (uiout, "number", b->number);
11023 ui_out_text (uiout, ": ");
11024 ui_out_field_string (uiout, "exp", w->exp_string);
11025 do_cleanups (ui_out_chain);
11026 }
11027
11028 /* Implement the "print_recreate" breakpoint_ops method for
11029 masked hardware watchpoints. */
11030
11031 static void
11032 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11033 {
11034 struct watchpoint *w = (struct watchpoint *) b;
11035 char tmp[40];
11036
11037 switch (b->type)
11038 {
11039 case bp_hardware_watchpoint:
11040 fprintf_unfiltered (fp, "watch");
11041 break;
11042 case bp_read_watchpoint:
11043 fprintf_unfiltered (fp, "rwatch");
11044 break;
11045 case bp_access_watchpoint:
11046 fprintf_unfiltered (fp, "awatch");
11047 break;
11048 default:
11049 internal_error (__FILE__, __LINE__,
11050 _("Invalid hardware watchpoint type."));
11051 }
11052
11053 sprintf_vma (tmp, w->hw_wp_mask);
11054 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11055 print_recreate_thread (b, fp);
11056 }
11057
11058 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11059
11060 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11061
11062 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11063
11064 static int
11065 is_masked_watchpoint (const struct breakpoint *b)
11066 {
11067 return b->ops == &masked_watchpoint_breakpoint_ops;
11068 }
11069
11070 /* accessflag: hw_write: watch write,
11071 hw_read: watch read,
11072 hw_access: watch access (read or write) */
11073 static void
11074 watch_command_1 (const char *arg, int accessflag, int from_tty,
11075 int just_location, int internal)
11076 {
11077 struct breakpoint *b, *scope_breakpoint = NULL;
11078 struct expression *exp;
11079 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11080 struct value *val, *mark, *result;
11081 int saved_bitpos = 0, saved_bitsize = 0;
11082 struct frame_info *frame;
11083 const char *exp_start = NULL;
11084 const char *exp_end = NULL;
11085 const char *tok, *end_tok;
11086 int toklen = -1;
11087 const char *cond_start = NULL;
11088 const char *cond_end = NULL;
11089 enum bptype bp_type;
11090 int thread = -1;
11091 int pc = 0;
11092 /* Flag to indicate whether we are going to use masks for
11093 the hardware watchpoint. */
11094 int use_mask = 0;
11095 CORE_ADDR mask = 0;
11096 struct watchpoint *w;
11097 char *expression;
11098 struct cleanup *back_to;
11099
11100 /* Make sure that we actually have parameters to parse. */
11101 if (arg != NULL && arg[0] != '\0')
11102 {
11103 const char *value_start;
11104
11105 exp_end = arg + strlen (arg);
11106
11107 /* Look for "parameter value" pairs at the end
11108 of the arguments string. */
11109 for (tok = exp_end - 1; tok > arg; tok--)
11110 {
11111 /* Skip whitespace at the end of the argument list. */
11112 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11113 tok--;
11114
11115 /* Find the beginning of the last token.
11116 This is the value of the parameter. */
11117 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11118 tok--;
11119 value_start = tok + 1;
11120
11121 /* Skip whitespace. */
11122 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11123 tok--;
11124
11125 end_tok = tok;
11126
11127 /* Find the beginning of the second to last token.
11128 This is the parameter itself. */
11129 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11130 tok--;
11131 tok++;
11132 toklen = end_tok - tok + 1;
11133
11134 if (toklen == 6 && startswith (tok, "thread"))
11135 {
11136 struct thread_info *thr;
11137 /* At this point we've found a "thread" token, which means
11138 the user is trying to set a watchpoint that triggers
11139 only in a specific thread. */
11140 const char *endp;
11141
11142 if (thread != -1)
11143 error(_("You can specify only one thread."));
11144
11145 /* Extract the thread ID from the next token. */
11146 thr = parse_thread_id (value_start, &endp);
11147
11148 /* Check if the user provided a valid thread ID. */
11149 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11150 invalid_thread_id_error (value_start);
11151
11152 thread = thr->global_num;
11153 }
11154 else if (toklen == 4 && startswith (tok, "mask"))
11155 {
11156 /* We've found a "mask" token, which means the user wants to
11157 create a hardware watchpoint that is going to have the mask
11158 facility. */
11159 struct value *mask_value, *mark;
11160
11161 if (use_mask)
11162 error(_("You can specify only one mask."));
11163
11164 use_mask = just_location = 1;
11165
11166 mark = value_mark ();
11167 mask_value = parse_to_comma_and_eval (&value_start);
11168 mask = value_as_address (mask_value);
11169 value_free_to_mark (mark);
11170 }
11171 else
11172 /* We didn't recognize what we found. We should stop here. */
11173 break;
11174
11175 /* Truncate the string and get rid of the "parameter value" pair before
11176 the arguments string is parsed by the parse_exp_1 function. */
11177 exp_end = tok;
11178 }
11179 }
11180 else
11181 exp_end = arg;
11182
11183 /* Parse the rest of the arguments. From here on out, everything
11184 is in terms of a newly allocated string instead of the original
11185 ARG. */
11186 innermost_block = NULL;
11187 expression = savestring (arg, exp_end - arg);
11188 back_to = make_cleanup (xfree, expression);
11189 exp_start = arg = expression;
11190 exp = parse_exp_1 (&arg, 0, 0, 0);
11191 exp_end = arg;
11192 /* Remove trailing whitespace from the expression before saving it.
11193 This makes the eventual display of the expression string a bit
11194 prettier. */
11195 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11196 --exp_end;
11197
11198 /* Checking if the expression is not constant. */
11199 if (watchpoint_exp_is_const (exp))
11200 {
11201 int len;
11202
11203 len = exp_end - exp_start;
11204 while (len > 0 && isspace (exp_start[len - 1]))
11205 len--;
11206 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11207 }
11208
11209 exp_valid_block = innermost_block;
11210 mark = value_mark ();
11211 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11212
11213 if (val != NULL && just_location)
11214 {
11215 saved_bitpos = value_bitpos (val);
11216 saved_bitsize = value_bitsize (val);
11217 }
11218
11219 if (just_location)
11220 {
11221 int ret;
11222
11223 exp_valid_block = NULL;
11224 val = value_addr (result);
11225 release_value (val);
11226 value_free_to_mark (mark);
11227
11228 if (use_mask)
11229 {
11230 ret = target_masked_watch_num_registers (value_as_address (val),
11231 mask);
11232 if (ret == -1)
11233 error (_("This target does not support masked watchpoints."));
11234 else if (ret == -2)
11235 error (_("Invalid mask or memory region."));
11236 }
11237 }
11238 else if (val != NULL)
11239 release_value (val);
11240
11241 tok = skip_spaces_const (arg);
11242 end_tok = skip_to_space_const (tok);
11243
11244 toklen = end_tok - tok;
11245 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11246 {
11247 struct expression *cond;
11248
11249 innermost_block = NULL;
11250 tok = cond_start = end_tok + 1;
11251 cond = parse_exp_1 (&tok, 0, 0, 0);
11252
11253 /* The watchpoint expression may not be local, but the condition
11254 may still be. E.g.: `watch global if local > 0'. */
11255 cond_exp_valid_block = innermost_block;
11256
11257 xfree (cond);
11258 cond_end = tok;
11259 }
11260 if (*tok)
11261 error (_("Junk at end of command."));
11262
11263 frame = block_innermost_frame (exp_valid_block);
11264
11265 /* If the expression is "local", then set up a "watchpoint scope"
11266 breakpoint at the point where we've left the scope of the watchpoint
11267 expression. Create the scope breakpoint before the watchpoint, so
11268 that we will encounter it first in bpstat_stop_status. */
11269 if (exp_valid_block && frame)
11270 {
11271 if (frame_id_p (frame_unwind_caller_id (frame)))
11272 {
11273 scope_breakpoint
11274 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11275 frame_unwind_caller_pc (frame),
11276 bp_watchpoint_scope,
11277 &momentary_breakpoint_ops);
11278
11279 scope_breakpoint->enable_state = bp_enabled;
11280
11281 /* Automatically delete the breakpoint when it hits. */
11282 scope_breakpoint->disposition = disp_del;
11283
11284 /* Only break in the proper frame (help with recursion). */
11285 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11286
11287 /* Set the address at which we will stop. */
11288 scope_breakpoint->loc->gdbarch
11289 = frame_unwind_caller_arch (frame);
11290 scope_breakpoint->loc->requested_address
11291 = frame_unwind_caller_pc (frame);
11292 scope_breakpoint->loc->address
11293 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11294 scope_breakpoint->loc->requested_address,
11295 scope_breakpoint->type);
11296 }
11297 }
11298
11299 /* Now set up the breakpoint. We create all watchpoints as hardware
11300 watchpoints here even if hardware watchpoints are turned off, a call
11301 to update_watchpoint later in this function will cause the type to
11302 drop back to bp_watchpoint (software watchpoint) if required. */
11303
11304 if (accessflag == hw_read)
11305 bp_type = bp_read_watchpoint;
11306 else if (accessflag == hw_access)
11307 bp_type = bp_access_watchpoint;
11308 else
11309 bp_type = bp_hardware_watchpoint;
11310
11311 w = XCNEW (struct watchpoint);
11312 b = &w->base;
11313 if (use_mask)
11314 init_raw_breakpoint_without_location (b, NULL, bp_type,
11315 &masked_watchpoint_breakpoint_ops);
11316 else
11317 init_raw_breakpoint_without_location (b, NULL, bp_type,
11318 &watchpoint_breakpoint_ops);
11319 b->thread = thread;
11320 b->disposition = disp_donttouch;
11321 b->pspace = current_program_space;
11322 w->exp = exp;
11323 w->exp_valid_block = exp_valid_block;
11324 w->cond_exp_valid_block = cond_exp_valid_block;
11325 if (just_location)
11326 {
11327 struct type *t = value_type (val);
11328 CORE_ADDR addr = value_as_address (val);
11329 char *name;
11330
11331 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11332 name = type_to_string (t);
11333
11334 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11335 core_addr_to_string (addr));
11336 xfree (name);
11337
11338 w->exp_string = xstrprintf ("-location %.*s",
11339 (int) (exp_end - exp_start), exp_start);
11340
11341 /* The above expression is in C. */
11342 b->language = language_c;
11343 }
11344 else
11345 w->exp_string = savestring (exp_start, exp_end - exp_start);
11346
11347 if (use_mask)
11348 {
11349 w->hw_wp_mask = mask;
11350 }
11351 else
11352 {
11353 w->val = val;
11354 w->val_bitpos = saved_bitpos;
11355 w->val_bitsize = saved_bitsize;
11356 w->val_valid = 1;
11357 }
11358
11359 if (cond_start)
11360 b->cond_string = savestring (cond_start, cond_end - cond_start);
11361 else
11362 b->cond_string = 0;
11363
11364 if (frame)
11365 {
11366 w->watchpoint_frame = get_frame_id (frame);
11367 w->watchpoint_thread = inferior_ptid;
11368 }
11369 else
11370 {
11371 w->watchpoint_frame = null_frame_id;
11372 w->watchpoint_thread = null_ptid;
11373 }
11374
11375 if (scope_breakpoint != NULL)
11376 {
11377 /* The scope breakpoint is related to the watchpoint. We will
11378 need to act on them together. */
11379 b->related_breakpoint = scope_breakpoint;
11380 scope_breakpoint->related_breakpoint = b;
11381 }
11382
11383 if (!just_location)
11384 value_free_to_mark (mark);
11385
11386 TRY
11387 {
11388 /* Finally update the new watchpoint. This creates the locations
11389 that should be inserted. */
11390 update_watchpoint (w, 1);
11391 }
11392 CATCH (e, RETURN_MASK_ALL)
11393 {
11394 delete_breakpoint (b);
11395 throw_exception (e);
11396 }
11397 END_CATCH
11398
11399 install_breakpoint (internal, b, 1);
11400 do_cleanups (back_to);
11401 }
11402
11403 /* Return count of debug registers needed to watch the given expression.
11404 If the watchpoint cannot be handled in hardware return zero. */
11405
11406 static int
11407 can_use_hardware_watchpoint (struct value *v)
11408 {
11409 int found_memory_cnt = 0;
11410 struct value *head = v;
11411
11412 /* Did the user specifically forbid us to use hardware watchpoints? */
11413 if (!can_use_hw_watchpoints)
11414 return 0;
11415
11416 /* Make sure that the value of the expression depends only upon
11417 memory contents, and values computed from them within GDB. If we
11418 find any register references or function calls, we can't use a
11419 hardware watchpoint.
11420
11421 The idea here is that evaluating an expression generates a series
11422 of values, one holding the value of every subexpression. (The
11423 expression a*b+c has five subexpressions: a, b, a*b, c, and
11424 a*b+c.) GDB's values hold almost enough information to establish
11425 the criteria given above --- they identify memory lvalues,
11426 register lvalues, computed values, etcetera. So we can evaluate
11427 the expression, and then scan the chain of values that leaves
11428 behind to decide whether we can detect any possible change to the
11429 expression's final value using only hardware watchpoints.
11430
11431 However, I don't think that the values returned by inferior
11432 function calls are special in any way. So this function may not
11433 notice that an expression involving an inferior function call
11434 can't be watched with hardware watchpoints. FIXME. */
11435 for (; v; v = value_next (v))
11436 {
11437 if (VALUE_LVAL (v) == lval_memory)
11438 {
11439 if (v != head && value_lazy (v))
11440 /* A lazy memory lvalue in the chain is one that GDB never
11441 needed to fetch; we either just used its address (e.g.,
11442 `a' in `a.b') or we never needed it at all (e.g., `a'
11443 in `a,b'). This doesn't apply to HEAD; if that is
11444 lazy then it was not readable, but watch it anyway. */
11445 ;
11446 else
11447 {
11448 /* Ahh, memory we actually used! Check if we can cover
11449 it with hardware watchpoints. */
11450 struct type *vtype = check_typedef (value_type (v));
11451
11452 /* We only watch structs and arrays if user asked for it
11453 explicitly, never if they just happen to appear in a
11454 middle of some value chain. */
11455 if (v == head
11456 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11457 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11458 {
11459 CORE_ADDR vaddr = value_address (v);
11460 int len;
11461 int num_regs;
11462
11463 len = (target_exact_watchpoints
11464 && is_scalar_type_recursive (vtype))?
11465 1 : TYPE_LENGTH (value_type (v));
11466
11467 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11468 if (!num_regs)
11469 return 0;
11470 else
11471 found_memory_cnt += num_regs;
11472 }
11473 }
11474 }
11475 else if (VALUE_LVAL (v) != not_lval
11476 && deprecated_value_modifiable (v) == 0)
11477 return 0; /* These are values from the history (e.g., $1). */
11478 else if (VALUE_LVAL (v) == lval_register)
11479 return 0; /* Cannot watch a register with a HW watchpoint. */
11480 }
11481
11482 /* The expression itself looks suitable for using a hardware
11483 watchpoint, but give the target machine a chance to reject it. */
11484 return found_memory_cnt;
11485 }
11486
11487 void
11488 watch_command_wrapper (char *arg, int from_tty, int internal)
11489 {
11490 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11491 }
11492
11493 /* A helper function that looks for the "-location" argument and then
11494 calls watch_command_1. */
11495
11496 static void
11497 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11498 {
11499 int just_location = 0;
11500
11501 if (arg
11502 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11503 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11504 {
11505 arg = skip_spaces (arg);
11506 just_location = 1;
11507 }
11508
11509 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11510 }
11511
11512 static void
11513 watch_command (char *arg, int from_tty)
11514 {
11515 watch_maybe_just_location (arg, hw_write, from_tty);
11516 }
11517
11518 void
11519 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11520 {
11521 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11522 }
11523
11524 static void
11525 rwatch_command (char *arg, int from_tty)
11526 {
11527 watch_maybe_just_location (arg, hw_read, from_tty);
11528 }
11529
11530 void
11531 awatch_command_wrapper (char *arg, int from_tty, int internal)
11532 {
11533 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11534 }
11535
11536 static void
11537 awatch_command (char *arg, int from_tty)
11538 {
11539 watch_maybe_just_location (arg, hw_access, from_tty);
11540 }
11541 \f
11542
11543 /* Data for the FSM that manages the until(location)/advance commands
11544 in infcmd.c. Here because it uses the mechanisms of
11545 breakpoints. */
11546
11547 struct until_break_fsm
11548 {
11549 /* The base class. */
11550 struct thread_fsm thread_fsm;
11551
11552 /* The thread that as current when the command was executed. */
11553 int thread;
11554
11555 /* The breakpoint set at the destination location. */
11556 struct breakpoint *location_breakpoint;
11557
11558 /* Breakpoint set at the return address in the caller frame. May be
11559 NULL. */
11560 struct breakpoint *caller_breakpoint;
11561 };
11562
11563 static void until_break_fsm_clean_up (struct thread_fsm *self);
11564 static int until_break_fsm_should_stop (struct thread_fsm *self);
11565 static enum async_reply_reason
11566 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11567
11568 /* until_break_fsm's vtable. */
11569
11570 static struct thread_fsm_ops until_break_fsm_ops =
11571 {
11572 NULL, /* dtor */
11573 until_break_fsm_clean_up,
11574 until_break_fsm_should_stop,
11575 NULL, /* return_value */
11576 until_break_fsm_async_reply_reason,
11577 };
11578
11579 /* Allocate a new until_break_command_fsm. */
11580
11581 static struct until_break_fsm *
11582 new_until_break_fsm (int thread,
11583 struct breakpoint *location_breakpoint,
11584 struct breakpoint *caller_breakpoint)
11585 {
11586 struct until_break_fsm *sm;
11587
11588 sm = XCNEW (struct until_break_fsm);
11589 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops);
11590
11591 sm->thread = thread;
11592 sm->location_breakpoint = location_breakpoint;
11593 sm->caller_breakpoint = caller_breakpoint;
11594
11595 return sm;
11596 }
11597
11598 /* Implementation of the 'should_stop' FSM method for the
11599 until(location)/advance commands. */
11600
11601 static int
11602 until_break_fsm_should_stop (struct thread_fsm *self)
11603 {
11604 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11605 struct thread_info *tp = inferior_thread ();
11606
11607 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11608 sm->location_breakpoint) != NULL
11609 || (sm->caller_breakpoint != NULL
11610 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11611 sm->caller_breakpoint) != NULL))
11612 thread_fsm_set_finished (self);
11613
11614 return 1;
11615 }
11616
11617 /* Implementation of the 'clean_up' FSM method for the
11618 until(location)/advance commands. */
11619
11620 static void
11621 until_break_fsm_clean_up (struct thread_fsm *self)
11622 {
11623 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11624
11625 /* Clean up our temporary breakpoints. */
11626 if (sm->location_breakpoint != NULL)
11627 {
11628 delete_breakpoint (sm->location_breakpoint);
11629 sm->location_breakpoint = NULL;
11630 }
11631 if (sm->caller_breakpoint != NULL)
11632 {
11633 delete_breakpoint (sm->caller_breakpoint);
11634 sm->caller_breakpoint = NULL;
11635 }
11636 delete_longjmp_breakpoint (sm->thread);
11637 }
11638
11639 /* Implementation of the 'async_reply_reason' FSM method for the
11640 until(location)/advance commands. */
11641
11642 static enum async_reply_reason
11643 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11644 {
11645 return EXEC_ASYNC_LOCATION_REACHED;
11646 }
11647
11648 void
11649 until_break_command (char *arg, int from_tty, int anywhere)
11650 {
11651 struct symtabs_and_lines sals;
11652 struct symtab_and_line sal;
11653 struct frame_info *frame;
11654 struct gdbarch *frame_gdbarch;
11655 struct frame_id stack_frame_id;
11656 struct frame_id caller_frame_id;
11657 struct breakpoint *location_breakpoint;
11658 struct breakpoint *caller_breakpoint = NULL;
11659 struct cleanup *old_chain, *cleanup;
11660 int thread;
11661 struct thread_info *tp;
11662 struct event_location *location;
11663 struct until_break_fsm *sm;
11664
11665 clear_proceed_status (0);
11666
11667 /* Set a breakpoint where the user wants it and at return from
11668 this function. */
11669
11670 location = string_to_event_location (&arg, current_language);
11671 cleanup = make_cleanup_delete_event_location (location);
11672
11673 if (last_displayed_sal_is_valid ())
11674 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11675 get_last_displayed_symtab (),
11676 get_last_displayed_line ());
11677 else
11678 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11679 (struct symtab *) NULL, 0);
11680
11681 if (sals.nelts != 1)
11682 error (_("Couldn't get information on specified line."));
11683
11684 sal = sals.sals[0];
11685 xfree (sals.sals); /* malloc'd, so freed. */
11686
11687 if (*arg)
11688 error (_("Junk at end of arguments."));
11689
11690 resolve_sal_pc (&sal);
11691
11692 tp = inferior_thread ();
11693 thread = tp->global_num;
11694
11695 old_chain = make_cleanup (null_cleanup, NULL);
11696
11697 /* Note linespec handling above invalidates the frame chain.
11698 Installing a breakpoint also invalidates the frame chain (as it
11699 may need to switch threads), so do any frame handling before
11700 that. */
11701
11702 frame = get_selected_frame (NULL);
11703 frame_gdbarch = get_frame_arch (frame);
11704 stack_frame_id = get_stack_frame_id (frame);
11705 caller_frame_id = frame_unwind_caller_id (frame);
11706
11707 /* Keep within the current frame, or in frames called by the current
11708 one. */
11709
11710 if (frame_id_p (caller_frame_id))
11711 {
11712 struct symtab_and_line sal2;
11713 struct gdbarch *caller_gdbarch;
11714
11715 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11716 sal2.pc = frame_unwind_caller_pc (frame);
11717 caller_gdbarch = frame_unwind_caller_arch (frame);
11718 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11719 sal2,
11720 caller_frame_id,
11721 bp_until);
11722 make_cleanup_delete_breakpoint (caller_breakpoint);
11723
11724 set_longjmp_breakpoint (tp, caller_frame_id);
11725 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11726 }
11727
11728 /* set_momentary_breakpoint could invalidate FRAME. */
11729 frame = NULL;
11730
11731 if (anywhere)
11732 /* If the user told us to continue until a specified location,
11733 we don't specify a frame at which we need to stop. */
11734 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11735 null_frame_id, bp_until);
11736 else
11737 /* Otherwise, specify the selected frame, because we want to stop
11738 only at the very same frame. */
11739 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11740 stack_frame_id, bp_until);
11741 make_cleanup_delete_breakpoint (location_breakpoint);
11742
11743 sm = new_until_break_fsm (tp->global_num,
11744 location_breakpoint, caller_breakpoint);
11745 tp->thread_fsm = &sm->thread_fsm;
11746
11747 discard_cleanups (old_chain);
11748
11749 proceed (-1, GDB_SIGNAL_DEFAULT);
11750
11751 do_cleanups (cleanup);
11752 }
11753
11754 /* This function attempts to parse an optional "if <cond>" clause
11755 from the arg string. If one is not found, it returns NULL.
11756
11757 Else, it returns a pointer to the condition string. (It does not
11758 attempt to evaluate the string against a particular block.) And,
11759 it updates arg to point to the first character following the parsed
11760 if clause in the arg string. */
11761
11762 char *
11763 ep_parse_optional_if_clause (char **arg)
11764 {
11765 char *cond_string;
11766
11767 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11768 return NULL;
11769
11770 /* Skip the "if" keyword. */
11771 (*arg) += 2;
11772
11773 /* Skip any extra leading whitespace, and record the start of the
11774 condition string. */
11775 *arg = skip_spaces (*arg);
11776 cond_string = *arg;
11777
11778 /* Assume that the condition occupies the remainder of the arg
11779 string. */
11780 (*arg) += strlen (cond_string);
11781
11782 return cond_string;
11783 }
11784
11785 /* Commands to deal with catching events, such as signals, exceptions,
11786 process start/exit, etc. */
11787
11788 typedef enum
11789 {
11790 catch_fork_temporary, catch_vfork_temporary,
11791 catch_fork_permanent, catch_vfork_permanent
11792 }
11793 catch_fork_kind;
11794
11795 static void
11796 catch_fork_command_1 (char *arg, int from_tty,
11797 struct cmd_list_element *command)
11798 {
11799 struct gdbarch *gdbarch = get_current_arch ();
11800 char *cond_string = NULL;
11801 catch_fork_kind fork_kind;
11802 int tempflag;
11803
11804 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11805 tempflag = (fork_kind == catch_fork_temporary
11806 || fork_kind == catch_vfork_temporary);
11807
11808 if (!arg)
11809 arg = "";
11810 arg = skip_spaces (arg);
11811
11812 /* The allowed syntax is:
11813 catch [v]fork
11814 catch [v]fork if <cond>
11815
11816 First, check if there's an if clause. */
11817 cond_string = ep_parse_optional_if_clause (&arg);
11818
11819 if ((*arg != '\0') && !isspace (*arg))
11820 error (_("Junk at end of arguments."));
11821
11822 /* If this target supports it, create a fork or vfork catchpoint
11823 and enable reporting of such events. */
11824 switch (fork_kind)
11825 {
11826 case catch_fork_temporary:
11827 case catch_fork_permanent:
11828 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11829 &catch_fork_breakpoint_ops);
11830 break;
11831 case catch_vfork_temporary:
11832 case catch_vfork_permanent:
11833 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11834 &catch_vfork_breakpoint_ops);
11835 break;
11836 default:
11837 error (_("unsupported or unknown fork kind; cannot catch it"));
11838 break;
11839 }
11840 }
11841
11842 static void
11843 catch_exec_command_1 (char *arg, int from_tty,
11844 struct cmd_list_element *command)
11845 {
11846 struct exec_catchpoint *c;
11847 struct gdbarch *gdbarch = get_current_arch ();
11848 int tempflag;
11849 char *cond_string = NULL;
11850
11851 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11852
11853 if (!arg)
11854 arg = "";
11855 arg = skip_spaces (arg);
11856
11857 /* The allowed syntax is:
11858 catch exec
11859 catch exec if <cond>
11860
11861 First, check if there's an if clause. */
11862 cond_string = ep_parse_optional_if_clause (&arg);
11863
11864 if ((*arg != '\0') && !isspace (*arg))
11865 error (_("Junk at end of arguments."));
11866
11867 c = XNEW (struct exec_catchpoint);
11868 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11869 &catch_exec_breakpoint_ops);
11870 c->exec_pathname = NULL;
11871
11872 install_breakpoint (0, &c->base, 1);
11873 }
11874
11875 void
11876 init_ada_exception_breakpoint (struct breakpoint *b,
11877 struct gdbarch *gdbarch,
11878 struct symtab_and_line sal,
11879 char *addr_string,
11880 const struct breakpoint_ops *ops,
11881 int tempflag,
11882 int enabled,
11883 int from_tty)
11884 {
11885 if (from_tty)
11886 {
11887 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11888 if (!loc_gdbarch)
11889 loc_gdbarch = gdbarch;
11890
11891 describe_other_breakpoints (loc_gdbarch,
11892 sal.pspace, sal.pc, sal.section, -1);
11893 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11894 version for exception catchpoints, because two catchpoints
11895 used for different exception names will use the same address.
11896 In this case, a "breakpoint ... also set at..." warning is
11897 unproductive. Besides, the warning phrasing is also a bit
11898 inappropriate, we should use the word catchpoint, and tell
11899 the user what type of catchpoint it is. The above is good
11900 enough for now, though. */
11901 }
11902
11903 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11904
11905 b->enable_state = enabled ? bp_enabled : bp_disabled;
11906 b->disposition = tempflag ? disp_del : disp_donttouch;
11907 b->location = string_to_event_location (&addr_string,
11908 language_def (language_ada));
11909 b->language = language_ada;
11910 }
11911
11912 static void
11913 catch_command (char *arg, int from_tty)
11914 {
11915 error (_("Catch requires an event name."));
11916 }
11917 \f
11918
11919 static void
11920 tcatch_command (char *arg, int from_tty)
11921 {
11922 error (_("Catch requires an event name."));
11923 }
11924
11925 /* A qsort comparison function that sorts breakpoints in order. */
11926
11927 static int
11928 compare_breakpoints (const void *a, const void *b)
11929 {
11930 const breakpoint_p *ba = (const breakpoint_p *) a;
11931 uintptr_t ua = (uintptr_t) *ba;
11932 const breakpoint_p *bb = (const breakpoint_p *) b;
11933 uintptr_t ub = (uintptr_t) *bb;
11934
11935 if ((*ba)->number < (*bb)->number)
11936 return -1;
11937 else if ((*ba)->number > (*bb)->number)
11938 return 1;
11939
11940 /* Now sort by address, in case we see, e..g, two breakpoints with
11941 the number 0. */
11942 if (ua < ub)
11943 return -1;
11944 return ua > ub ? 1 : 0;
11945 }
11946
11947 /* Delete breakpoints by address or line. */
11948
11949 static void
11950 clear_command (char *arg, int from_tty)
11951 {
11952 struct breakpoint *b, *prev;
11953 VEC(breakpoint_p) *found = 0;
11954 int ix;
11955 int default_match;
11956 struct symtabs_and_lines sals;
11957 struct symtab_and_line sal;
11958 int i;
11959 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11960
11961 if (arg)
11962 {
11963 sals = decode_line_with_current_source (arg,
11964 (DECODE_LINE_FUNFIRSTLINE
11965 | DECODE_LINE_LIST_MODE));
11966 make_cleanup (xfree, sals.sals);
11967 default_match = 0;
11968 }
11969 else
11970 {
11971 sals.sals = XNEW (struct symtab_and_line);
11972 make_cleanup (xfree, sals.sals);
11973 init_sal (&sal); /* Initialize to zeroes. */
11974
11975 /* Set sal's line, symtab, pc, and pspace to the values
11976 corresponding to the last call to print_frame_info. If the
11977 codepoint is not valid, this will set all the fields to 0. */
11978 get_last_displayed_sal (&sal);
11979 if (sal.symtab == 0)
11980 error (_("No source file specified."));
11981
11982 sals.sals[0] = sal;
11983 sals.nelts = 1;
11984
11985 default_match = 1;
11986 }
11987
11988 /* We don't call resolve_sal_pc here. That's not as bad as it
11989 seems, because all existing breakpoints typically have both
11990 file/line and pc set. So, if clear is given file/line, we can
11991 match this to existing breakpoint without obtaining pc at all.
11992
11993 We only support clearing given the address explicitly
11994 present in breakpoint table. Say, we've set breakpoint
11995 at file:line. There were several PC values for that file:line,
11996 due to optimization, all in one block.
11997
11998 We've picked one PC value. If "clear" is issued with another
11999 PC corresponding to the same file:line, the breakpoint won't
12000 be cleared. We probably can still clear the breakpoint, but
12001 since the other PC value is never presented to user, user
12002 can only find it by guessing, and it does not seem important
12003 to support that. */
12004
12005 /* For each line spec given, delete bps which correspond to it. Do
12006 it in two passes, solely to preserve the current behavior that
12007 from_tty is forced true if we delete more than one
12008 breakpoint. */
12009
12010 found = NULL;
12011 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12012 for (i = 0; i < sals.nelts; i++)
12013 {
12014 const char *sal_fullname;
12015
12016 /* If exact pc given, clear bpts at that pc.
12017 If line given (pc == 0), clear all bpts on specified line.
12018 If defaulting, clear all bpts on default line
12019 or at default pc.
12020
12021 defaulting sal.pc != 0 tests to do
12022
12023 0 1 pc
12024 1 1 pc _and_ line
12025 0 0 line
12026 1 0 <can't happen> */
12027
12028 sal = sals.sals[i];
12029 sal_fullname = (sal.symtab == NULL
12030 ? NULL : symtab_to_fullname (sal.symtab));
12031
12032 /* Find all matching breakpoints and add them to 'found'. */
12033 ALL_BREAKPOINTS (b)
12034 {
12035 int match = 0;
12036 /* Are we going to delete b? */
12037 if (b->type != bp_none && !is_watchpoint (b))
12038 {
12039 struct bp_location *loc = b->loc;
12040 for (; loc; loc = loc->next)
12041 {
12042 /* If the user specified file:line, don't allow a PC
12043 match. This matches historical gdb behavior. */
12044 int pc_match = (!sal.explicit_line
12045 && sal.pc
12046 && (loc->pspace == sal.pspace)
12047 && (loc->address == sal.pc)
12048 && (!section_is_overlay (loc->section)
12049 || loc->section == sal.section));
12050 int line_match = 0;
12051
12052 if ((default_match || sal.explicit_line)
12053 && loc->symtab != NULL
12054 && sal_fullname != NULL
12055 && sal.pspace == loc->pspace
12056 && loc->line_number == sal.line
12057 && filename_cmp (symtab_to_fullname (loc->symtab),
12058 sal_fullname) == 0)
12059 line_match = 1;
12060
12061 if (pc_match || line_match)
12062 {
12063 match = 1;
12064 break;
12065 }
12066 }
12067 }
12068
12069 if (match)
12070 VEC_safe_push(breakpoint_p, found, b);
12071 }
12072 }
12073
12074 /* Now go thru the 'found' chain and delete them. */
12075 if (VEC_empty(breakpoint_p, found))
12076 {
12077 if (arg)
12078 error (_("No breakpoint at %s."), arg);
12079 else
12080 error (_("No breakpoint at this line."));
12081 }
12082
12083 /* Remove duplicates from the vec. */
12084 qsort (VEC_address (breakpoint_p, found),
12085 VEC_length (breakpoint_p, found),
12086 sizeof (breakpoint_p),
12087 compare_breakpoints);
12088 prev = VEC_index (breakpoint_p, found, 0);
12089 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12090 {
12091 if (b == prev)
12092 {
12093 VEC_ordered_remove (breakpoint_p, found, ix);
12094 --ix;
12095 }
12096 }
12097
12098 if (VEC_length(breakpoint_p, found) > 1)
12099 from_tty = 1; /* Always report if deleted more than one. */
12100 if (from_tty)
12101 {
12102 if (VEC_length(breakpoint_p, found) == 1)
12103 printf_unfiltered (_("Deleted breakpoint "));
12104 else
12105 printf_unfiltered (_("Deleted breakpoints "));
12106 }
12107
12108 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12109 {
12110 if (from_tty)
12111 printf_unfiltered ("%d ", b->number);
12112 delete_breakpoint (b);
12113 }
12114 if (from_tty)
12115 putchar_unfiltered ('\n');
12116
12117 do_cleanups (cleanups);
12118 }
12119 \f
12120 /* Delete breakpoint in BS if they are `delete' breakpoints and
12121 all breakpoints that are marked for deletion, whether hit or not.
12122 This is called after any breakpoint is hit, or after errors. */
12123
12124 void
12125 breakpoint_auto_delete (bpstat bs)
12126 {
12127 struct breakpoint *b, *b_tmp;
12128
12129 for (; bs; bs = bs->next)
12130 if (bs->breakpoint_at
12131 && bs->breakpoint_at->disposition == disp_del
12132 && bs->stop)
12133 delete_breakpoint (bs->breakpoint_at);
12134
12135 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12136 {
12137 if (b->disposition == disp_del_at_next_stop)
12138 delete_breakpoint (b);
12139 }
12140 }
12141
12142 /* A comparison function for bp_location AP and BP being interfaced to
12143 qsort. Sort elements primarily by their ADDRESS (no matter what
12144 does breakpoint_address_is_meaningful say for its OWNER),
12145 secondarily by ordering first permanent elements and
12146 terciarily just ensuring the array is sorted stable way despite
12147 qsort being an unstable algorithm. */
12148
12149 static int
12150 bp_location_compare (const void *ap, const void *bp)
12151 {
12152 const struct bp_location *a = *(const struct bp_location **) ap;
12153 const struct bp_location *b = *(const struct bp_location **) bp;
12154
12155 if (a->address != b->address)
12156 return (a->address > b->address) - (a->address < b->address);
12157
12158 /* Sort locations at the same address by their pspace number, keeping
12159 locations of the same inferior (in a multi-inferior environment)
12160 grouped. */
12161
12162 if (a->pspace->num != b->pspace->num)
12163 return ((a->pspace->num > b->pspace->num)
12164 - (a->pspace->num < b->pspace->num));
12165
12166 /* Sort permanent breakpoints first. */
12167 if (a->permanent != b->permanent)
12168 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12169
12170 /* Make the internal GDB representation stable across GDB runs
12171 where A and B memory inside GDB can differ. Breakpoint locations of
12172 the same type at the same address can be sorted in arbitrary order. */
12173
12174 if (a->owner->number != b->owner->number)
12175 return ((a->owner->number > b->owner->number)
12176 - (a->owner->number < b->owner->number));
12177
12178 return (a > b) - (a < b);
12179 }
12180
12181 /* Set bp_location_placed_address_before_address_max and
12182 bp_location_shadow_len_after_address_max according to the current
12183 content of the bp_location array. */
12184
12185 static void
12186 bp_location_target_extensions_update (void)
12187 {
12188 struct bp_location *bl, **blp_tmp;
12189
12190 bp_location_placed_address_before_address_max = 0;
12191 bp_location_shadow_len_after_address_max = 0;
12192
12193 ALL_BP_LOCATIONS (bl, blp_tmp)
12194 {
12195 CORE_ADDR start, end, addr;
12196
12197 if (!bp_location_has_shadow (bl))
12198 continue;
12199
12200 start = bl->target_info.placed_address;
12201 end = start + bl->target_info.shadow_len;
12202
12203 gdb_assert (bl->address >= start);
12204 addr = bl->address - start;
12205 if (addr > bp_location_placed_address_before_address_max)
12206 bp_location_placed_address_before_address_max = addr;
12207
12208 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12209
12210 gdb_assert (bl->address < end);
12211 addr = end - bl->address;
12212 if (addr > bp_location_shadow_len_after_address_max)
12213 bp_location_shadow_len_after_address_max = addr;
12214 }
12215 }
12216
12217 /* Download tracepoint locations if they haven't been. */
12218
12219 static void
12220 download_tracepoint_locations (void)
12221 {
12222 struct breakpoint *b;
12223 struct cleanup *old_chain;
12224 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12225
12226 old_chain = save_current_space_and_thread ();
12227
12228 ALL_TRACEPOINTS (b)
12229 {
12230 struct bp_location *bl;
12231 struct tracepoint *t;
12232 int bp_location_downloaded = 0;
12233
12234 if ((b->type == bp_fast_tracepoint
12235 ? !may_insert_fast_tracepoints
12236 : !may_insert_tracepoints))
12237 continue;
12238
12239 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12240 {
12241 if (target_can_download_tracepoint ())
12242 can_download_tracepoint = TRIBOOL_TRUE;
12243 else
12244 can_download_tracepoint = TRIBOOL_FALSE;
12245 }
12246
12247 if (can_download_tracepoint == TRIBOOL_FALSE)
12248 break;
12249
12250 for (bl = b->loc; bl; bl = bl->next)
12251 {
12252 /* In tracepoint, locations are _never_ duplicated, so
12253 should_be_inserted is equivalent to
12254 unduplicated_should_be_inserted. */
12255 if (!should_be_inserted (bl) || bl->inserted)
12256 continue;
12257
12258 switch_to_program_space_and_thread (bl->pspace);
12259
12260 target_download_tracepoint (bl);
12261
12262 bl->inserted = 1;
12263 bp_location_downloaded = 1;
12264 }
12265 t = (struct tracepoint *) b;
12266 t->number_on_target = b->number;
12267 if (bp_location_downloaded)
12268 observer_notify_breakpoint_modified (b);
12269 }
12270
12271 do_cleanups (old_chain);
12272 }
12273
12274 /* Swap the insertion/duplication state between two locations. */
12275
12276 static void
12277 swap_insertion (struct bp_location *left, struct bp_location *right)
12278 {
12279 const int left_inserted = left->inserted;
12280 const int left_duplicate = left->duplicate;
12281 const int left_needs_update = left->needs_update;
12282 const struct bp_target_info left_target_info = left->target_info;
12283
12284 /* Locations of tracepoints can never be duplicated. */
12285 if (is_tracepoint (left->owner))
12286 gdb_assert (!left->duplicate);
12287 if (is_tracepoint (right->owner))
12288 gdb_assert (!right->duplicate);
12289
12290 left->inserted = right->inserted;
12291 left->duplicate = right->duplicate;
12292 left->needs_update = right->needs_update;
12293 left->target_info = right->target_info;
12294 right->inserted = left_inserted;
12295 right->duplicate = left_duplicate;
12296 right->needs_update = left_needs_update;
12297 right->target_info = left_target_info;
12298 }
12299
12300 /* Force the re-insertion of the locations at ADDRESS. This is called
12301 once a new/deleted/modified duplicate location is found and we are evaluating
12302 conditions on the target's side. Such conditions need to be updated on
12303 the target. */
12304
12305 static void
12306 force_breakpoint_reinsertion (struct bp_location *bl)
12307 {
12308 struct bp_location **locp = NULL, **loc2p;
12309 struct bp_location *loc;
12310 CORE_ADDR address = 0;
12311 int pspace_num;
12312
12313 address = bl->address;
12314 pspace_num = bl->pspace->num;
12315
12316 /* This is only meaningful if the target is
12317 evaluating conditions and if the user has
12318 opted for condition evaluation on the target's
12319 side. */
12320 if (gdb_evaluates_breakpoint_condition_p ()
12321 || !target_supports_evaluation_of_breakpoint_conditions ())
12322 return;
12323
12324 /* Flag all breakpoint locations with this address and
12325 the same program space as the location
12326 as "its condition has changed". We need to
12327 update the conditions on the target's side. */
12328 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12329 {
12330 loc = *loc2p;
12331
12332 if (!is_breakpoint (loc->owner)
12333 || pspace_num != loc->pspace->num)
12334 continue;
12335
12336 /* Flag the location appropriately. We use a different state to
12337 let everyone know that we already updated the set of locations
12338 with addr bl->address and program space bl->pspace. This is so
12339 we don't have to keep calling these functions just to mark locations
12340 that have already been marked. */
12341 loc->condition_changed = condition_updated;
12342
12343 /* Free the agent expression bytecode as well. We will compute
12344 it later on. */
12345 if (loc->cond_bytecode)
12346 {
12347 free_agent_expr (loc->cond_bytecode);
12348 loc->cond_bytecode = NULL;
12349 }
12350 }
12351 }
12352 /* Called whether new breakpoints are created, or existing breakpoints
12353 deleted, to update the global location list and recompute which
12354 locations are duplicate of which.
12355
12356 The INSERT_MODE flag determines whether locations may not, may, or
12357 shall be inserted now. See 'enum ugll_insert_mode' for more
12358 info. */
12359
12360 static void
12361 update_global_location_list (enum ugll_insert_mode insert_mode)
12362 {
12363 struct breakpoint *b;
12364 struct bp_location **locp, *loc;
12365 struct cleanup *cleanups;
12366 /* Last breakpoint location address that was marked for update. */
12367 CORE_ADDR last_addr = 0;
12368 /* Last breakpoint location program space that was marked for update. */
12369 int last_pspace_num = -1;
12370
12371 /* Used in the duplicates detection below. When iterating over all
12372 bp_locations, points to the first bp_location of a given address.
12373 Breakpoints and watchpoints of different types are never
12374 duplicates of each other. Keep one pointer for each type of
12375 breakpoint/watchpoint, so we only need to loop over all locations
12376 once. */
12377 struct bp_location *bp_loc_first; /* breakpoint */
12378 struct bp_location *wp_loc_first; /* hardware watchpoint */
12379 struct bp_location *awp_loc_first; /* access watchpoint */
12380 struct bp_location *rwp_loc_first; /* read watchpoint */
12381
12382 /* Saved former bp_location array which we compare against the newly
12383 built bp_location from the current state of ALL_BREAKPOINTS. */
12384 struct bp_location **old_location, **old_locp;
12385 unsigned old_location_count;
12386
12387 old_location = bp_location;
12388 old_location_count = bp_location_count;
12389 bp_location = NULL;
12390 bp_location_count = 0;
12391 cleanups = make_cleanup (xfree, old_location);
12392
12393 ALL_BREAKPOINTS (b)
12394 for (loc = b->loc; loc; loc = loc->next)
12395 bp_location_count++;
12396
12397 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
12398 locp = bp_location;
12399 ALL_BREAKPOINTS (b)
12400 for (loc = b->loc; loc; loc = loc->next)
12401 *locp++ = loc;
12402 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12403 bp_location_compare);
12404
12405 bp_location_target_extensions_update ();
12406
12407 /* Identify bp_location instances that are no longer present in the
12408 new list, and therefore should be freed. Note that it's not
12409 necessary that those locations should be removed from inferior --
12410 if there's another location at the same address (previously
12411 marked as duplicate), we don't need to remove/insert the
12412 location.
12413
12414 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12415 and former bp_location array state respectively. */
12416
12417 locp = bp_location;
12418 for (old_locp = old_location; old_locp < old_location + old_location_count;
12419 old_locp++)
12420 {
12421 struct bp_location *old_loc = *old_locp;
12422 struct bp_location **loc2p;
12423
12424 /* Tells if 'old_loc' is found among the new locations. If
12425 not, we have to free it. */
12426 int found_object = 0;
12427 /* Tells if the location should remain inserted in the target. */
12428 int keep_in_target = 0;
12429 int removed = 0;
12430
12431 /* Skip LOCP entries which will definitely never be needed.
12432 Stop either at or being the one matching OLD_LOC. */
12433 while (locp < bp_location + bp_location_count
12434 && (*locp)->address < old_loc->address)
12435 locp++;
12436
12437 for (loc2p = locp;
12438 (loc2p < bp_location + bp_location_count
12439 && (*loc2p)->address == old_loc->address);
12440 loc2p++)
12441 {
12442 /* Check if this is a new/duplicated location or a duplicated
12443 location that had its condition modified. If so, we want to send
12444 its condition to the target if evaluation of conditions is taking
12445 place there. */
12446 if ((*loc2p)->condition_changed == condition_modified
12447 && (last_addr != old_loc->address
12448 || last_pspace_num != old_loc->pspace->num))
12449 {
12450 force_breakpoint_reinsertion (*loc2p);
12451 last_pspace_num = old_loc->pspace->num;
12452 }
12453
12454 if (*loc2p == old_loc)
12455 found_object = 1;
12456 }
12457
12458 /* We have already handled this address, update it so that we don't
12459 have to go through updates again. */
12460 last_addr = old_loc->address;
12461
12462 /* Target-side condition evaluation: Handle deleted locations. */
12463 if (!found_object)
12464 force_breakpoint_reinsertion (old_loc);
12465
12466 /* If this location is no longer present, and inserted, look if
12467 there's maybe a new location at the same address. If so,
12468 mark that one inserted, and don't remove this one. This is
12469 needed so that we don't have a time window where a breakpoint
12470 at certain location is not inserted. */
12471
12472 if (old_loc->inserted)
12473 {
12474 /* If the location is inserted now, we might have to remove
12475 it. */
12476
12477 if (found_object && should_be_inserted (old_loc))
12478 {
12479 /* The location is still present in the location list,
12480 and still should be inserted. Don't do anything. */
12481 keep_in_target = 1;
12482 }
12483 else
12484 {
12485 /* This location still exists, but it won't be kept in the
12486 target since it may have been disabled. We proceed to
12487 remove its target-side condition. */
12488
12489 /* The location is either no longer present, or got
12490 disabled. See if there's another location at the
12491 same address, in which case we don't need to remove
12492 this one from the target. */
12493
12494 /* OLD_LOC comes from existing struct breakpoint. */
12495 if (breakpoint_address_is_meaningful (old_loc->owner))
12496 {
12497 for (loc2p = locp;
12498 (loc2p < bp_location + bp_location_count
12499 && (*loc2p)->address == old_loc->address);
12500 loc2p++)
12501 {
12502 struct bp_location *loc2 = *loc2p;
12503
12504 if (breakpoint_locations_match (loc2, old_loc))
12505 {
12506 /* Read watchpoint locations are switched to
12507 access watchpoints, if the former are not
12508 supported, but the latter are. */
12509 if (is_hardware_watchpoint (old_loc->owner))
12510 {
12511 gdb_assert (is_hardware_watchpoint (loc2->owner));
12512 loc2->watchpoint_type = old_loc->watchpoint_type;
12513 }
12514
12515 /* loc2 is a duplicated location. We need to check
12516 if it should be inserted in case it will be
12517 unduplicated. */
12518 if (loc2 != old_loc
12519 && unduplicated_should_be_inserted (loc2))
12520 {
12521 swap_insertion (old_loc, loc2);
12522 keep_in_target = 1;
12523 break;
12524 }
12525 }
12526 }
12527 }
12528 }
12529
12530 if (!keep_in_target)
12531 {
12532 if (remove_breakpoint (old_loc, mark_uninserted))
12533 {
12534 /* This is just about all we can do. We could keep
12535 this location on the global list, and try to
12536 remove it next time, but there's no particular
12537 reason why we will succeed next time.
12538
12539 Note that at this point, old_loc->owner is still
12540 valid, as delete_breakpoint frees the breakpoint
12541 only after calling us. */
12542 printf_filtered (_("warning: Error removing "
12543 "breakpoint %d\n"),
12544 old_loc->owner->number);
12545 }
12546 removed = 1;
12547 }
12548 }
12549
12550 if (!found_object)
12551 {
12552 if (removed && target_is_non_stop_p ()
12553 && need_moribund_for_location_type (old_loc))
12554 {
12555 /* This location was removed from the target. In
12556 non-stop mode, a race condition is possible where
12557 we've removed a breakpoint, but stop events for that
12558 breakpoint are already queued and will arrive later.
12559 We apply an heuristic to be able to distinguish such
12560 SIGTRAPs from other random SIGTRAPs: we keep this
12561 breakpoint location for a bit, and will retire it
12562 after we see some number of events. The theory here
12563 is that reporting of events should, "on the average",
12564 be fair, so after a while we'll see events from all
12565 threads that have anything of interest, and no longer
12566 need to keep this breakpoint location around. We
12567 don't hold locations forever so to reduce chances of
12568 mistaking a non-breakpoint SIGTRAP for a breakpoint
12569 SIGTRAP.
12570
12571 The heuristic failing can be disastrous on
12572 decr_pc_after_break targets.
12573
12574 On decr_pc_after_break targets, like e.g., x86-linux,
12575 if we fail to recognize a late breakpoint SIGTRAP,
12576 because events_till_retirement has reached 0 too
12577 soon, we'll fail to do the PC adjustment, and report
12578 a random SIGTRAP to the user. When the user resumes
12579 the inferior, it will most likely immediately crash
12580 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12581 corrupted, because of being resumed e.g., in the
12582 middle of a multi-byte instruction, or skipped a
12583 one-byte instruction. This was actually seen happen
12584 on native x86-linux, and should be less rare on
12585 targets that do not support new thread events, like
12586 remote, due to the heuristic depending on
12587 thread_count.
12588
12589 Mistaking a random SIGTRAP for a breakpoint trap
12590 causes similar symptoms (PC adjustment applied when
12591 it shouldn't), but then again, playing with SIGTRAPs
12592 behind the debugger's back is asking for trouble.
12593
12594 Since hardware watchpoint traps are always
12595 distinguishable from other traps, so we don't need to
12596 apply keep hardware watchpoint moribund locations
12597 around. We simply always ignore hardware watchpoint
12598 traps we can no longer explain. */
12599
12600 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12601 old_loc->owner = NULL;
12602
12603 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12604 }
12605 else
12606 {
12607 old_loc->owner = NULL;
12608 decref_bp_location (&old_loc);
12609 }
12610 }
12611 }
12612
12613 /* Rescan breakpoints at the same address and section, marking the
12614 first one as "first" and any others as "duplicates". This is so
12615 that the bpt instruction is only inserted once. If we have a
12616 permanent breakpoint at the same place as BPT, make that one the
12617 official one, and the rest as duplicates. Permanent breakpoints
12618 are sorted first for the same address.
12619
12620 Do the same for hardware watchpoints, but also considering the
12621 watchpoint's type (regular/access/read) and length. */
12622
12623 bp_loc_first = NULL;
12624 wp_loc_first = NULL;
12625 awp_loc_first = NULL;
12626 rwp_loc_first = NULL;
12627 ALL_BP_LOCATIONS (loc, locp)
12628 {
12629 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12630 non-NULL. */
12631 struct bp_location **loc_first_p;
12632 b = loc->owner;
12633
12634 if (!unduplicated_should_be_inserted (loc)
12635 || !breakpoint_address_is_meaningful (b)
12636 /* Don't detect duplicate for tracepoint locations because they are
12637 never duplicated. See the comments in field `duplicate' of
12638 `struct bp_location'. */
12639 || is_tracepoint (b))
12640 {
12641 /* Clear the condition modification flag. */
12642 loc->condition_changed = condition_unchanged;
12643 continue;
12644 }
12645
12646 if (b->type == bp_hardware_watchpoint)
12647 loc_first_p = &wp_loc_first;
12648 else if (b->type == bp_read_watchpoint)
12649 loc_first_p = &rwp_loc_first;
12650 else if (b->type == bp_access_watchpoint)
12651 loc_first_p = &awp_loc_first;
12652 else
12653 loc_first_p = &bp_loc_first;
12654
12655 if (*loc_first_p == NULL
12656 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12657 || !breakpoint_locations_match (loc, *loc_first_p))
12658 {
12659 *loc_first_p = loc;
12660 loc->duplicate = 0;
12661
12662 if (is_breakpoint (loc->owner) && loc->condition_changed)
12663 {
12664 loc->needs_update = 1;
12665 /* Clear the condition modification flag. */
12666 loc->condition_changed = condition_unchanged;
12667 }
12668 continue;
12669 }
12670
12671
12672 /* This and the above ensure the invariant that the first location
12673 is not duplicated, and is the inserted one.
12674 All following are marked as duplicated, and are not inserted. */
12675 if (loc->inserted)
12676 swap_insertion (loc, *loc_first_p);
12677 loc->duplicate = 1;
12678
12679 /* Clear the condition modification flag. */
12680 loc->condition_changed = condition_unchanged;
12681 }
12682
12683 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12684 {
12685 if (insert_mode != UGLL_DONT_INSERT)
12686 insert_breakpoint_locations ();
12687 else
12688 {
12689 /* Even though the caller told us to not insert new
12690 locations, we may still need to update conditions on the
12691 target's side of breakpoints that were already inserted
12692 if the target is evaluating breakpoint conditions. We
12693 only update conditions for locations that are marked
12694 "needs_update". */
12695 update_inserted_breakpoint_locations ();
12696 }
12697 }
12698
12699 if (insert_mode != UGLL_DONT_INSERT)
12700 download_tracepoint_locations ();
12701
12702 do_cleanups (cleanups);
12703 }
12704
12705 void
12706 breakpoint_retire_moribund (void)
12707 {
12708 struct bp_location *loc;
12709 int ix;
12710
12711 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12712 if (--(loc->events_till_retirement) == 0)
12713 {
12714 decref_bp_location (&loc);
12715 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12716 --ix;
12717 }
12718 }
12719
12720 static void
12721 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12722 {
12723
12724 TRY
12725 {
12726 update_global_location_list (insert_mode);
12727 }
12728 CATCH (e, RETURN_MASK_ERROR)
12729 {
12730 }
12731 END_CATCH
12732 }
12733
12734 /* Clear BKP from a BPS. */
12735
12736 static void
12737 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12738 {
12739 bpstat bs;
12740
12741 for (bs = bps; bs; bs = bs->next)
12742 if (bs->breakpoint_at == bpt)
12743 {
12744 bs->breakpoint_at = NULL;
12745 bs->old_val = NULL;
12746 /* bs->commands will be freed later. */
12747 }
12748 }
12749
12750 /* Callback for iterate_over_threads. */
12751 static int
12752 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12753 {
12754 struct breakpoint *bpt = (struct breakpoint *) data;
12755
12756 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12757 return 0;
12758 }
12759
12760 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12761 callbacks. */
12762
12763 static void
12764 say_where (struct breakpoint *b)
12765 {
12766 struct value_print_options opts;
12767
12768 get_user_print_options (&opts);
12769
12770 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12771 single string. */
12772 if (b->loc == NULL)
12773 {
12774 /* For pending locations, the output differs slightly based
12775 on b->extra_string. If this is non-NULL, it contains either
12776 a condition or dprintf arguments. */
12777 if (b->extra_string == NULL)
12778 {
12779 printf_filtered (_(" (%s) pending."),
12780 event_location_to_string (b->location));
12781 }
12782 else if (b->type == bp_dprintf)
12783 {
12784 printf_filtered (_(" (%s,%s) pending."),
12785 event_location_to_string (b->location),
12786 b->extra_string);
12787 }
12788 else
12789 {
12790 printf_filtered (_(" (%s %s) pending."),
12791 event_location_to_string (b->location),
12792 b->extra_string);
12793 }
12794 }
12795 else
12796 {
12797 if (opts.addressprint || b->loc->symtab == NULL)
12798 {
12799 printf_filtered (" at ");
12800 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12801 gdb_stdout);
12802 }
12803 if (b->loc->symtab != NULL)
12804 {
12805 /* If there is a single location, we can print the location
12806 more nicely. */
12807 if (b->loc->next == NULL)
12808 printf_filtered (": file %s, line %d.",
12809 symtab_to_filename_for_display (b->loc->symtab),
12810 b->loc->line_number);
12811 else
12812 /* This is not ideal, but each location may have a
12813 different file name, and this at least reflects the
12814 real situation somewhat. */
12815 printf_filtered (": %s.",
12816 event_location_to_string (b->location));
12817 }
12818
12819 if (b->loc->next)
12820 {
12821 struct bp_location *loc = b->loc;
12822 int n = 0;
12823 for (; loc; loc = loc->next)
12824 ++n;
12825 printf_filtered (" (%d locations)", n);
12826 }
12827 }
12828 }
12829
12830 /* Default bp_location_ops methods. */
12831
12832 static void
12833 bp_location_dtor (struct bp_location *self)
12834 {
12835 xfree (self->cond);
12836 if (self->cond_bytecode)
12837 free_agent_expr (self->cond_bytecode);
12838 xfree (self->function_name);
12839
12840 VEC_free (agent_expr_p, self->target_info.conditions);
12841 VEC_free (agent_expr_p, self->target_info.tcommands);
12842 }
12843
12844 static const struct bp_location_ops bp_location_ops =
12845 {
12846 bp_location_dtor
12847 };
12848
12849 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12850 inherit from. */
12851
12852 static void
12853 base_breakpoint_dtor (struct breakpoint *self)
12854 {
12855 decref_counted_command_line (&self->commands);
12856 xfree (self->cond_string);
12857 xfree (self->extra_string);
12858 xfree (self->filter);
12859 delete_event_location (self->location);
12860 delete_event_location (self->location_range_end);
12861 }
12862
12863 static struct bp_location *
12864 base_breakpoint_allocate_location (struct breakpoint *self)
12865 {
12866 struct bp_location *loc;
12867
12868 loc = XNEW (struct bp_location);
12869 init_bp_location (loc, &bp_location_ops, self);
12870 return loc;
12871 }
12872
12873 static void
12874 base_breakpoint_re_set (struct breakpoint *b)
12875 {
12876 /* Nothing to re-set. */
12877 }
12878
12879 #define internal_error_pure_virtual_called() \
12880 gdb_assert_not_reached ("pure virtual function called")
12881
12882 static int
12883 base_breakpoint_insert_location (struct bp_location *bl)
12884 {
12885 internal_error_pure_virtual_called ();
12886 }
12887
12888 static int
12889 base_breakpoint_remove_location (struct bp_location *bl)
12890 {
12891 internal_error_pure_virtual_called ();
12892 }
12893
12894 static int
12895 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12896 struct address_space *aspace,
12897 CORE_ADDR bp_addr,
12898 const struct target_waitstatus *ws)
12899 {
12900 internal_error_pure_virtual_called ();
12901 }
12902
12903 static void
12904 base_breakpoint_check_status (bpstat bs)
12905 {
12906 /* Always stop. */
12907 }
12908
12909 /* A "works_in_software_mode" breakpoint_ops method that just internal
12910 errors. */
12911
12912 static int
12913 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12914 {
12915 internal_error_pure_virtual_called ();
12916 }
12917
12918 /* A "resources_needed" breakpoint_ops method that just internal
12919 errors. */
12920
12921 static int
12922 base_breakpoint_resources_needed (const struct bp_location *bl)
12923 {
12924 internal_error_pure_virtual_called ();
12925 }
12926
12927 static enum print_stop_action
12928 base_breakpoint_print_it (bpstat bs)
12929 {
12930 internal_error_pure_virtual_called ();
12931 }
12932
12933 static void
12934 base_breakpoint_print_one_detail (const struct breakpoint *self,
12935 struct ui_out *uiout)
12936 {
12937 /* nothing */
12938 }
12939
12940 static void
12941 base_breakpoint_print_mention (struct breakpoint *b)
12942 {
12943 internal_error_pure_virtual_called ();
12944 }
12945
12946 static void
12947 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12948 {
12949 internal_error_pure_virtual_called ();
12950 }
12951
12952 static void
12953 base_breakpoint_create_sals_from_location
12954 (const struct event_location *location,
12955 struct linespec_result *canonical,
12956 enum bptype type_wanted)
12957 {
12958 internal_error_pure_virtual_called ();
12959 }
12960
12961 static void
12962 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12963 struct linespec_result *c,
12964 char *cond_string,
12965 char *extra_string,
12966 enum bptype type_wanted,
12967 enum bpdisp disposition,
12968 int thread,
12969 int task, int ignore_count,
12970 const struct breakpoint_ops *o,
12971 int from_tty, int enabled,
12972 int internal, unsigned flags)
12973 {
12974 internal_error_pure_virtual_called ();
12975 }
12976
12977 static void
12978 base_breakpoint_decode_location (struct breakpoint *b,
12979 const struct event_location *location,
12980 struct symtabs_and_lines *sals)
12981 {
12982 internal_error_pure_virtual_called ();
12983 }
12984
12985 /* The default 'explains_signal' method. */
12986
12987 static int
12988 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12989 {
12990 return 1;
12991 }
12992
12993 /* The default "after_condition_true" method. */
12994
12995 static void
12996 base_breakpoint_after_condition_true (struct bpstats *bs)
12997 {
12998 /* Nothing to do. */
12999 }
13000
13001 struct breakpoint_ops base_breakpoint_ops =
13002 {
13003 base_breakpoint_dtor,
13004 base_breakpoint_allocate_location,
13005 base_breakpoint_re_set,
13006 base_breakpoint_insert_location,
13007 base_breakpoint_remove_location,
13008 base_breakpoint_breakpoint_hit,
13009 base_breakpoint_check_status,
13010 base_breakpoint_resources_needed,
13011 base_breakpoint_works_in_software_mode,
13012 base_breakpoint_print_it,
13013 NULL,
13014 base_breakpoint_print_one_detail,
13015 base_breakpoint_print_mention,
13016 base_breakpoint_print_recreate,
13017 base_breakpoint_create_sals_from_location,
13018 base_breakpoint_create_breakpoints_sal,
13019 base_breakpoint_decode_location,
13020 base_breakpoint_explains_signal,
13021 base_breakpoint_after_condition_true,
13022 };
13023
13024 /* Default breakpoint_ops methods. */
13025
13026 static void
13027 bkpt_re_set (struct breakpoint *b)
13028 {
13029 /* FIXME: is this still reachable? */
13030 if (event_location_empty_p (b->location))
13031 {
13032 /* Anything without a location can't be re-set. */
13033 delete_breakpoint (b);
13034 return;
13035 }
13036
13037 breakpoint_re_set_default (b);
13038 }
13039
13040 static int
13041 bkpt_insert_location (struct bp_location *bl)
13042 {
13043 if (bl->loc_type == bp_loc_hardware_breakpoint)
13044 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13045 else
13046 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13047 }
13048
13049 static int
13050 bkpt_remove_location (struct bp_location *bl)
13051 {
13052 if (bl->loc_type == bp_loc_hardware_breakpoint)
13053 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13054 else
13055 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
13056 }
13057
13058 static int
13059 bkpt_breakpoint_hit (const struct bp_location *bl,
13060 struct address_space *aspace, CORE_ADDR bp_addr,
13061 const struct target_waitstatus *ws)
13062 {
13063 if (ws->kind != TARGET_WAITKIND_STOPPED
13064 || ws->value.sig != GDB_SIGNAL_TRAP)
13065 return 0;
13066
13067 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13068 aspace, bp_addr))
13069 return 0;
13070
13071 if (overlay_debugging /* unmapped overlay section */
13072 && section_is_overlay (bl->section)
13073 && !section_is_mapped (bl->section))
13074 return 0;
13075
13076 return 1;
13077 }
13078
13079 static int
13080 dprintf_breakpoint_hit (const struct bp_location *bl,
13081 struct address_space *aspace, CORE_ADDR bp_addr,
13082 const struct target_waitstatus *ws)
13083 {
13084 if (dprintf_style == dprintf_style_agent
13085 && target_can_run_breakpoint_commands ())
13086 {
13087 /* An agent-style dprintf never causes a stop. If we see a trap
13088 for this address it must be for a breakpoint that happens to
13089 be set at the same address. */
13090 return 0;
13091 }
13092
13093 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13094 }
13095
13096 static int
13097 bkpt_resources_needed (const struct bp_location *bl)
13098 {
13099 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13100
13101 return 1;
13102 }
13103
13104 static enum print_stop_action
13105 bkpt_print_it (bpstat bs)
13106 {
13107 struct breakpoint *b;
13108 const struct bp_location *bl;
13109 int bp_temp;
13110 struct ui_out *uiout = current_uiout;
13111
13112 gdb_assert (bs->bp_location_at != NULL);
13113
13114 bl = bs->bp_location_at;
13115 b = bs->breakpoint_at;
13116
13117 bp_temp = b->disposition == disp_del;
13118 if (bl->address != bl->requested_address)
13119 breakpoint_adjustment_warning (bl->requested_address,
13120 bl->address,
13121 b->number, 1);
13122 annotate_breakpoint (b->number);
13123 if (bp_temp)
13124 ui_out_text (uiout, "\nTemporary breakpoint ");
13125 else
13126 ui_out_text (uiout, "\nBreakpoint ");
13127 if (ui_out_is_mi_like_p (uiout))
13128 {
13129 ui_out_field_string (uiout, "reason",
13130 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13131 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13132 }
13133 ui_out_field_int (uiout, "bkptno", b->number);
13134 ui_out_text (uiout, ", ");
13135
13136 return PRINT_SRC_AND_LOC;
13137 }
13138
13139 static void
13140 bkpt_print_mention (struct breakpoint *b)
13141 {
13142 if (ui_out_is_mi_like_p (current_uiout))
13143 return;
13144
13145 switch (b->type)
13146 {
13147 case bp_breakpoint:
13148 case bp_gnu_ifunc_resolver:
13149 if (b->disposition == disp_del)
13150 printf_filtered (_("Temporary breakpoint"));
13151 else
13152 printf_filtered (_("Breakpoint"));
13153 printf_filtered (_(" %d"), b->number);
13154 if (b->type == bp_gnu_ifunc_resolver)
13155 printf_filtered (_(" at gnu-indirect-function resolver"));
13156 break;
13157 case bp_hardware_breakpoint:
13158 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13159 break;
13160 case bp_dprintf:
13161 printf_filtered (_("Dprintf %d"), b->number);
13162 break;
13163 }
13164
13165 say_where (b);
13166 }
13167
13168 static void
13169 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13170 {
13171 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13172 fprintf_unfiltered (fp, "tbreak");
13173 else if (tp->type == bp_breakpoint)
13174 fprintf_unfiltered (fp, "break");
13175 else if (tp->type == bp_hardware_breakpoint
13176 && tp->disposition == disp_del)
13177 fprintf_unfiltered (fp, "thbreak");
13178 else if (tp->type == bp_hardware_breakpoint)
13179 fprintf_unfiltered (fp, "hbreak");
13180 else
13181 internal_error (__FILE__, __LINE__,
13182 _("unhandled breakpoint type %d"), (int) tp->type);
13183
13184 fprintf_unfiltered (fp, " %s",
13185 event_location_to_string (tp->location));
13186
13187 /* Print out extra_string if this breakpoint is pending. It might
13188 contain, for example, conditions that were set by the user. */
13189 if (tp->loc == NULL && tp->extra_string != NULL)
13190 fprintf_unfiltered (fp, " %s", tp->extra_string);
13191
13192 print_recreate_thread (tp, fp);
13193 }
13194
13195 static void
13196 bkpt_create_sals_from_location (const struct event_location *location,
13197 struct linespec_result *canonical,
13198 enum bptype type_wanted)
13199 {
13200 create_sals_from_location_default (location, canonical, type_wanted);
13201 }
13202
13203 static void
13204 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13205 struct linespec_result *canonical,
13206 char *cond_string,
13207 char *extra_string,
13208 enum bptype type_wanted,
13209 enum bpdisp disposition,
13210 int thread,
13211 int task, int ignore_count,
13212 const struct breakpoint_ops *ops,
13213 int from_tty, int enabled,
13214 int internal, unsigned flags)
13215 {
13216 create_breakpoints_sal_default (gdbarch, canonical,
13217 cond_string, extra_string,
13218 type_wanted,
13219 disposition, thread, task,
13220 ignore_count, ops, from_tty,
13221 enabled, internal, flags);
13222 }
13223
13224 static void
13225 bkpt_decode_location (struct breakpoint *b,
13226 const struct event_location *location,
13227 struct symtabs_and_lines *sals)
13228 {
13229 decode_location_default (b, location, sals);
13230 }
13231
13232 /* Virtual table for internal breakpoints. */
13233
13234 static void
13235 internal_bkpt_re_set (struct breakpoint *b)
13236 {
13237 switch (b->type)
13238 {
13239 /* Delete overlay event and longjmp master breakpoints; they
13240 will be reset later by breakpoint_re_set. */
13241 case bp_overlay_event:
13242 case bp_longjmp_master:
13243 case bp_std_terminate_master:
13244 case bp_exception_master:
13245 delete_breakpoint (b);
13246 break;
13247
13248 /* This breakpoint is special, it's set up when the inferior
13249 starts and we really don't want to touch it. */
13250 case bp_shlib_event:
13251
13252 /* Like bp_shlib_event, this breakpoint type is special. Once
13253 it is set up, we do not want to touch it. */
13254 case bp_thread_event:
13255 break;
13256 }
13257 }
13258
13259 static void
13260 internal_bkpt_check_status (bpstat bs)
13261 {
13262 if (bs->breakpoint_at->type == bp_shlib_event)
13263 {
13264 /* If requested, stop when the dynamic linker notifies GDB of
13265 events. This allows the user to get control and place
13266 breakpoints in initializer routines for dynamically loaded
13267 objects (among other things). */
13268 bs->stop = stop_on_solib_events;
13269 bs->print = stop_on_solib_events;
13270 }
13271 else
13272 bs->stop = 0;
13273 }
13274
13275 static enum print_stop_action
13276 internal_bkpt_print_it (bpstat bs)
13277 {
13278 struct breakpoint *b;
13279
13280 b = bs->breakpoint_at;
13281
13282 switch (b->type)
13283 {
13284 case bp_shlib_event:
13285 /* Did we stop because the user set the stop_on_solib_events
13286 variable? (If so, we report this as a generic, "Stopped due
13287 to shlib event" message.) */
13288 print_solib_event (0);
13289 break;
13290
13291 case bp_thread_event:
13292 /* Not sure how we will get here.
13293 GDB should not stop for these breakpoints. */
13294 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13295 break;
13296
13297 case bp_overlay_event:
13298 /* By analogy with the thread event, GDB should not stop for these. */
13299 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13300 break;
13301
13302 case bp_longjmp_master:
13303 /* These should never be enabled. */
13304 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13305 break;
13306
13307 case bp_std_terminate_master:
13308 /* These should never be enabled. */
13309 printf_filtered (_("std::terminate Master Breakpoint: "
13310 "gdb should not stop!\n"));
13311 break;
13312
13313 case bp_exception_master:
13314 /* These should never be enabled. */
13315 printf_filtered (_("Exception Master Breakpoint: "
13316 "gdb should not stop!\n"));
13317 break;
13318 }
13319
13320 return PRINT_NOTHING;
13321 }
13322
13323 static void
13324 internal_bkpt_print_mention (struct breakpoint *b)
13325 {
13326 /* Nothing to mention. These breakpoints are internal. */
13327 }
13328
13329 /* Virtual table for momentary breakpoints */
13330
13331 static void
13332 momentary_bkpt_re_set (struct breakpoint *b)
13333 {
13334 /* Keep temporary breakpoints, which can be encountered when we step
13335 over a dlopen call and solib_add is resetting the breakpoints.
13336 Otherwise these should have been blown away via the cleanup chain
13337 or by breakpoint_init_inferior when we rerun the executable. */
13338 }
13339
13340 static void
13341 momentary_bkpt_check_status (bpstat bs)
13342 {
13343 /* Nothing. The point of these breakpoints is causing a stop. */
13344 }
13345
13346 static enum print_stop_action
13347 momentary_bkpt_print_it (bpstat bs)
13348 {
13349 return PRINT_UNKNOWN;
13350 }
13351
13352 static void
13353 momentary_bkpt_print_mention (struct breakpoint *b)
13354 {
13355 /* Nothing to mention. These breakpoints are internal. */
13356 }
13357
13358 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13359
13360 It gets cleared already on the removal of the first one of such placed
13361 breakpoints. This is OK as they get all removed altogether. */
13362
13363 static void
13364 longjmp_bkpt_dtor (struct breakpoint *self)
13365 {
13366 struct thread_info *tp = find_thread_global_id (self->thread);
13367
13368 if (tp)
13369 tp->initiating_frame = null_frame_id;
13370
13371 momentary_breakpoint_ops.dtor (self);
13372 }
13373
13374 /* Specific methods for probe breakpoints. */
13375
13376 static int
13377 bkpt_probe_insert_location (struct bp_location *bl)
13378 {
13379 int v = bkpt_insert_location (bl);
13380
13381 if (v == 0)
13382 {
13383 /* The insertion was successful, now let's set the probe's semaphore
13384 if needed. */
13385 if (bl->probe.probe->pops->set_semaphore != NULL)
13386 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13387 bl->probe.objfile,
13388 bl->gdbarch);
13389 }
13390
13391 return v;
13392 }
13393
13394 static int
13395 bkpt_probe_remove_location (struct bp_location *bl)
13396 {
13397 /* Let's clear the semaphore before removing the location. */
13398 if (bl->probe.probe->pops->clear_semaphore != NULL)
13399 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13400 bl->probe.objfile,
13401 bl->gdbarch);
13402
13403 return bkpt_remove_location (bl);
13404 }
13405
13406 static void
13407 bkpt_probe_create_sals_from_location (const struct event_location *location,
13408 struct linespec_result *canonical,
13409 enum bptype type_wanted)
13410 {
13411 struct linespec_sals lsal;
13412
13413 lsal.sals = parse_probes (location, canonical);
13414 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13415 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13416 }
13417
13418 static void
13419 bkpt_probe_decode_location (struct breakpoint *b,
13420 const struct event_location *location,
13421 struct symtabs_and_lines *sals)
13422 {
13423 *sals = parse_probes (location, NULL);
13424 if (!sals->sals)
13425 error (_("probe not found"));
13426 }
13427
13428 /* The breakpoint_ops structure to be used in tracepoints. */
13429
13430 static void
13431 tracepoint_re_set (struct breakpoint *b)
13432 {
13433 breakpoint_re_set_default (b);
13434 }
13435
13436 static int
13437 tracepoint_breakpoint_hit (const struct bp_location *bl,
13438 struct address_space *aspace, CORE_ADDR bp_addr,
13439 const struct target_waitstatus *ws)
13440 {
13441 /* By definition, the inferior does not report stops at
13442 tracepoints. */
13443 return 0;
13444 }
13445
13446 static void
13447 tracepoint_print_one_detail (const struct breakpoint *self,
13448 struct ui_out *uiout)
13449 {
13450 struct tracepoint *tp = (struct tracepoint *) self;
13451 if (tp->static_trace_marker_id)
13452 {
13453 gdb_assert (self->type == bp_static_tracepoint);
13454
13455 ui_out_text (uiout, "\tmarker id is ");
13456 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13457 tp->static_trace_marker_id);
13458 ui_out_text (uiout, "\n");
13459 }
13460 }
13461
13462 static void
13463 tracepoint_print_mention (struct breakpoint *b)
13464 {
13465 if (ui_out_is_mi_like_p (current_uiout))
13466 return;
13467
13468 switch (b->type)
13469 {
13470 case bp_tracepoint:
13471 printf_filtered (_("Tracepoint"));
13472 printf_filtered (_(" %d"), b->number);
13473 break;
13474 case bp_fast_tracepoint:
13475 printf_filtered (_("Fast tracepoint"));
13476 printf_filtered (_(" %d"), b->number);
13477 break;
13478 case bp_static_tracepoint:
13479 printf_filtered (_("Static tracepoint"));
13480 printf_filtered (_(" %d"), b->number);
13481 break;
13482 default:
13483 internal_error (__FILE__, __LINE__,
13484 _("unhandled tracepoint type %d"), (int) b->type);
13485 }
13486
13487 say_where (b);
13488 }
13489
13490 static void
13491 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13492 {
13493 struct tracepoint *tp = (struct tracepoint *) self;
13494
13495 if (self->type == bp_fast_tracepoint)
13496 fprintf_unfiltered (fp, "ftrace");
13497 else if (self->type == bp_static_tracepoint)
13498 fprintf_unfiltered (fp, "strace");
13499 else if (self->type == bp_tracepoint)
13500 fprintf_unfiltered (fp, "trace");
13501 else
13502 internal_error (__FILE__, __LINE__,
13503 _("unhandled tracepoint type %d"), (int) self->type);
13504
13505 fprintf_unfiltered (fp, " %s",
13506 event_location_to_string (self->location));
13507 print_recreate_thread (self, fp);
13508
13509 if (tp->pass_count)
13510 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13511 }
13512
13513 static void
13514 tracepoint_create_sals_from_location (const struct event_location *location,
13515 struct linespec_result *canonical,
13516 enum bptype type_wanted)
13517 {
13518 create_sals_from_location_default (location, canonical, type_wanted);
13519 }
13520
13521 static void
13522 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13523 struct linespec_result *canonical,
13524 char *cond_string,
13525 char *extra_string,
13526 enum bptype type_wanted,
13527 enum bpdisp disposition,
13528 int thread,
13529 int task, int ignore_count,
13530 const struct breakpoint_ops *ops,
13531 int from_tty, int enabled,
13532 int internal, unsigned flags)
13533 {
13534 create_breakpoints_sal_default (gdbarch, canonical,
13535 cond_string, extra_string,
13536 type_wanted,
13537 disposition, thread, task,
13538 ignore_count, ops, from_tty,
13539 enabled, internal, flags);
13540 }
13541
13542 static void
13543 tracepoint_decode_location (struct breakpoint *b,
13544 const struct event_location *location,
13545 struct symtabs_and_lines *sals)
13546 {
13547 decode_location_default (b, location, sals);
13548 }
13549
13550 struct breakpoint_ops tracepoint_breakpoint_ops;
13551
13552 /* The breakpoint_ops structure to be use on tracepoints placed in a
13553 static probe. */
13554
13555 static void
13556 tracepoint_probe_create_sals_from_location
13557 (const struct event_location *location,
13558 struct linespec_result *canonical,
13559 enum bptype type_wanted)
13560 {
13561 /* We use the same method for breakpoint on probes. */
13562 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13563 }
13564
13565 static void
13566 tracepoint_probe_decode_location (struct breakpoint *b,
13567 const struct event_location *location,
13568 struct symtabs_and_lines *sals)
13569 {
13570 /* We use the same method for breakpoint on probes. */
13571 bkpt_probe_decode_location (b, location, sals);
13572 }
13573
13574 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13575
13576 /* Dprintf breakpoint_ops methods. */
13577
13578 static void
13579 dprintf_re_set (struct breakpoint *b)
13580 {
13581 breakpoint_re_set_default (b);
13582
13583 /* extra_string should never be non-NULL for dprintf. */
13584 gdb_assert (b->extra_string != NULL);
13585
13586 /* 1 - connect to target 1, that can run breakpoint commands.
13587 2 - create a dprintf, which resolves fine.
13588 3 - disconnect from target 1
13589 4 - connect to target 2, that can NOT run breakpoint commands.
13590
13591 After steps #3/#4, you'll want the dprintf command list to
13592 be updated, because target 1 and 2 may well return different
13593 answers for target_can_run_breakpoint_commands().
13594 Given absence of finer grained resetting, we get to do
13595 it all the time. */
13596 if (b->extra_string != NULL)
13597 update_dprintf_command_list (b);
13598 }
13599
13600 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13601
13602 static void
13603 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13604 {
13605 fprintf_unfiltered (fp, "dprintf %s,%s",
13606 event_location_to_string (tp->location),
13607 tp->extra_string);
13608 print_recreate_thread (tp, fp);
13609 }
13610
13611 /* Implement the "after_condition_true" breakpoint_ops method for
13612 dprintf.
13613
13614 dprintf's are implemented with regular commands in their command
13615 list, but we run the commands here instead of before presenting the
13616 stop to the user, as dprintf's don't actually cause a stop. This
13617 also makes it so that the commands of multiple dprintfs at the same
13618 address are all handled. */
13619
13620 static void
13621 dprintf_after_condition_true (struct bpstats *bs)
13622 {
13623 struct cleanup *old_chain;
13624 struct bpstats tmp_bs = { NULL };
13625 struct bpstats *tmp_bs_p = &tmp_bs;
13626
13627 /* dprintf's never cause a stop. This wasn't set in the
13628 check_status hook instead because that would make the dprintf's
13629 condition not be evaluated. */
13630 bs->stop = 0;
13631
13632 /* Run the command list here. Take ownership of it instead of
13633 copying. We never want these commands to run later in
13634 bpstat_do_actions, if a breakpoint that causes a stop happens to
13635 be set at same address as this dprintf, or even if running the
13636 commands here throws. */
13637 tmp_bs.commands = bs->commands;
13638 bs->commands = NULL;
13639 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13640
13641 bpstat_do_actions_1 (&tmp_bs_p);
13642
13643 /* 'tmp_bs.commands' will usually be NULL by now, but
13644 bpstat_do_actions_1 may return early without processing the whole
13645 list. */
13646 do_cleanups (old_chain);
13647 }
13648
13649 /* The breakpoint_ops structure to be used on static tracepoints with
13650 markers (`-m'). */
13651
13652 static void
13653 strace_marker_create_sals_from_location (const struct event_location *location,
13654 struct linespec_result *canonical,
13655 enum bptype type_wanted)
13656 {
13657 struct linespec_sals lsal;
13658 const char *arg_start, *arg;
13659 char *str;
13660 struct cleanup *cleanup;
13661
13662 arg = arg_start = get_linespec_location (location);
13663 lsal.sals = decode_static_tracepoint_spec (&arg);
13664
13665 str = savestring (arg_start, arg - arg_start);
13666 cleanup = make_cleanup (xfree, str);
13667 canonical->location = new_linespec_location (&str);
13668 do_cleanups (cleanup);
13669
13670 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13671 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13672 }
13673
13674 static void
13675 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13676 struct linespec_result *canonical,
13677 char *cond_string,
13678 char *extra_string,
13679 enum bptype type_wanted,
13680 enum bpdisp disposition,
13681 int thread,
13682 int task, int ignore_count,
13683 const struct breakpoint_ops *ops,
13684 int from_tty, int enabled,
13685 int internal, unsigned flags)
13686 {
13687 int i;
13688 struct linespec_sals *lsal = VEC_index (linespec_sals,
13689 canonical->sals, 0);
13690
13691 /* If the user is creating a static tracepoint by marker id
13692 (strace -m MARKER_ID), then store the sals index, so that
13693 breakpoint_re_set can try to match up which of the newly
13694 found markers corresponds to this one, and, don't try to
13695 expand multiple locations for each sal, given than SALS
13696 already should contain all sals for MARKER_ID. */
13697
13698 for (i = 0; i < lsal->sals.nelts; ++i)
13699 {
13700 struct symtabs_and_lines expanded;
13701 struct tracepoint *tp;
13702 struct cleanup *old_chain;
13703 struct event_location *location;
13704
13705 expanded.nelts = 1;
13706 expanded.sals = &lsal->sals.sals[i];
13707
13708 location = copy_event_location (canonical->location);
13709 old_chain = make_cleanup_delete_event_location (location);
13710
13711 tp = XCNEW (struct tracepoint);
13712 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13713 location, NULL,
13714 cond_string, extra_string,
13715 type_wanted, disposition,
13716 thread, task, ignore_count, ops,
13717 from_tty, enabled, internal, flags,
13718 canonical->special_display);
13719 /* Given that its possible to have multiple markers with
13720 the same string id, if the user is creating a static
13721 tracepoint by marker id ("strace -m MARKER_ID"), then
13722 store the sals index, so that breakpoint_re_set can
13723 try to match up which of the newly found markers
13724 corresponds to this one */
13725 tp->static_trace_marker_id_idx = i;
13726
13727 install_breakpoint (internal, &tp->base, 0);
13728
13729 discard_cleanups (old_chain);
13730 }
13731 }
13732
13733 static void
13734 strace_marker_decode_location (struct breakpoint *b,
13735 const struct event_location *location,
13736 struct symtabs_and_lines *sals)
13737 {
13738 struct tracepoint *tp = (struct tracepoint *) b;
13739 const char *s = get_linespec_location (location);
13740
13741 *sals = decode_static_tracepoint_spec (&s);
13742 if (sals->nelts > tp->static_trace_marker_id_idx)
13743 {
13744 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13745 sals->nelts = 1;
13746 }
13747 else
13748 error (_("marker %s not found"), tp->static_trace_marker_id);
13749 }
13750
13751 static struct breakpoint_ops strace_marker_breakpoint_ops;
13752
13753 static int
13754 strace_marker_p (struct breakpoint *b)
13755 {
13756 return b->ops == &strace_marker_breakpoint_ops;
13757 }
13758
13759 /* Delete a breakpoint and clean up all traces of it in the data
13760 structures. */
13761
13762 void
13763 delete_breakpoint (struct breakpoint *bpt)
13764 {
13765 struct breakpoint *b;
13766
13767 gdb_assert (bpt != NULL);
13768
13769 /* Has this bp already been deleted? This can happen because
13770 multiple lists can hold pointers to bp's. bpstat lists are
13771 especial culprits.
13772
13773 One example of this happening is a watchpoint's scope bp. When
13774 the scope bp triggers, we notice that the watchpoint is out of
13775 scope, and delete it. We also delete its scope bp. But the
13776 scope bp is marked "auto-deleting", and is already on a bpstat.
13777 That bpstat is then checked for auto-deleting bp's, which are
13778 deleted.
13779
13780 A real solution to this problem might involve reference counts in
13781 bp's, and/or giving them pointers back to their referencing
13782 bpstat's, and teaching delete_breakpoint to only free a bp's
13783 storage when no more references were extent. A cheaper bandaid
13784 was chosen. */
13785 if (bpt->type == bp_none)
13786 return;
13787
13788 /* At least avoid this stale reference until the reference counting
13789 of breakpoints gets resolved. */
13790 if (bpt->related_breakpoint != bpt)
13791 {
13792 struct breakpoint *related;
13793 struct watchpoint *w;
13794
13795 if (bpt->type == bp_watchpoint_scope)
13796 w = (struct watchpoint *) bpt->related_breakpoint;
13797 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13798 w = (struct watchpoint *) bpt;
13799 else
13800 w = NULL;
13801 if (w != NULL)
13802 watchpoint_del_at_next_stop (w);
13803
13804 /* Unlink bpt from the bpt->related_breakpoint ring. */
13805 for (related = bpt; related->related_breakpoint != bpt;
13806 related = related->related_breakpoint);
13807 related->related_breakpoint = bpt->related_breakpoint;
13808 bpt->related_breakpoint = bpt;
13809 }
13810
13811 /* watch_command_1 creates a watchpoint but only sets its number if
13812 update_watchpoint succeeds in creating its bp_locations. If there's
13813 a problem in that process, we'll be asked to delete the half-created
13814 watchpoint. In that case, don't announce the deletion. */
13815 if (bpt->number)
13816 observer_notify_breakpoint_deleted (bpt);
13817
13818 if (breakpoint_chain == bpt)
13819 breakpoint_chain = bpt->next;
13820
13821 ALL_BREAKPOINTS (b)
13822 if (b->next == bpt)
13823 {
13824 b->next = bpt->next;
13825 break;
13826 }
13827
13828 /* Be sure no bpstat's are pointing at the breakpoint after it's
13829 been freed. */
13830 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13831 in all threads for now. Note that we cannot just remove bpstats
13832 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13833 commands are associated with the bpstat; if we remove it here,
13834 then the later call to bpstat_do_actions (&stop_bpstat); in
13835 event-top.c won't do anything, and temporary breakpoints with
13836 commands won't work. */
13837
13838 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13839
13840 /* Now that breakpoint is removed from breakpoint list, update the
13841 global location list. This will remove locations that used to
13842 belong to this breakpoint. Do this before freeing the breakpoint
13843 itself, since remove_breakpoint looks at location's owner. It
13844 might be better design to have location completely
13845 self-contained, but it's not the case now. */
13846 update_global_location_list (UGLL_DONT_INSERT);
13847
13848 bpt->ops->dtor (bpt);
13849 /* On the chance that someone will soon try again to delete this
13850 same bp, we mark it as deleted before freeing its storage. */
13851 bpt->type = bp_none;
13852 xfree (bpt);
13853 }
13854
13855 static void
13856 do_delete_breakpoint_cleanup (void *b)
13857 {
13858 delete_breakpoint ((struct breakpoint *) b);
13859 }
13860
13861 struct cleanup *
13862 make_cleanup_delete_breakpoint (struct breakpoint *b)
13863 {
13864 return make_cleanup (do_delete_breakpoint_cleanup, b);
13865 }
13866
13867 /* Iterator function to call a user-provided callback function once
13868 for each of B and its related breakpoints. */
13869
13870 static void
13871 iterate_over_related_breakpoints (struct breakpoint *b,
13872 void (*function) (struct breakpoint *,
13873 void *),
13874 void *data)
13875 {
13876 struct breakpoint *related;
13877
13878 related = b;
13879 do
13880 {
13881 struct breakpoint *next;
13882
13883 /* FUNCTION may delete RELATED. */
13884 next = related->related_breakpoint;
13885
13886 if (next == related)
13887 {
13888 /* RELATED is the last ring entry. */
13889 function (related, data);
13890
13891 /* FUNCTION may have deleted it, so we'd never reach back to
13892 B. There's nothing left to do anyway, so just break
13893 out. */
13894 break;
13895 }
13896 else
13897 function (related, data);
13898
13899 related = next;
13900 }
13901 while (related != b);
13902 }
13903
13904 static void
13905 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13906 {
13907 delete_breakpoint (b);
13908 }
13909
13910 /* A callback for map_breakpoint_numbers that calls
13911 delete_breakpoint. */
13912
13913 static void
13914 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13915 {
13916 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13917 }
13918
13919 void
13920 delete_command (char *arg, int from_tty)
13921 {
13922 struct breakpoint *b, *b_tmp;
13923
13924 dont_repeat ();
13925
13926 if (arg == 0)
13927 {
13928 int breaks_to_delete = 0;
13929
13930 /* Delete all breakpoints if no argument. Do not delete
13931 internal breakpoints, these have to be deleted with an
13932 explicit breakpoint number argument. */
13933 ALL_BREAKPOINTS (b)
13934 if (user_breakpoint_p (b))
13935 {
13936 breaks_to_delete = 1;
13937 break;
13938 }
13939
13940 /* Ask user only if there are some breakpoints to delete. */
13941 if (!from_tty
13942 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13943 {
13944 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13945 if (user_breakpoint_p (b))
13946 delete_breakpoint (b);
13947 }
13948 }
13949 else
13950 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13951 }
13952
13953 static int
13954 all_locations_are_pending (struct bp_location *loc)
13955 {
13956 for (; loc; loc = loc->next)
13957 if (!loc->shlib_disabled
13958 && !loc->pspace->executing_startup)
13959 return 0;
13960 return 1;
13961 }
13962
13963 /* Subroutine of update_breakpoint_locations to simplify it.
13964 Return non-zero if multiple fns in list LOC have the same name.
13965 Null names are ignored. */
13966
13967 static int
13968 ambiguous_names_p (struct bp_location *loc)
13969 {
13970 struct bp_location *l;
13971 htab_t htab = htab_create_alloc (13, htab_hash_string,
13972 (int (*) (const void *,
13973 const void *)) streq,
13974 NULL, xcalloc, xfree);
13975
13976 for (l = loc; l != NULL; l = l->next)
13977 {
13978 const char **slot;
13979 const char *name = l->function_name;
13980
13981 /* Allow for some names to be NULL, ignore them. */
13982 if (name == NULL)
13983 continue;
13984
13985 slot = (const char **) htab_find_slot (htab, (const void *) name,
13986 INSERT);
13987 /* NOTE: We can assume slot != NULL here because xcalloc never
13988 returns NULL. */
13989 if (*slot != NULL)
13990 {
13991 htab_delete (htab);
13992 return 1;
13993 }
13994 *slot = name;
13995 }
13996
13997 htab_delete (htab);
13998 return 0;
13999 }
14000
14001 /* When symbols change, it probably means the sources changed as well,
14002 and it might mean the static tracepoint markers are no longer at
14003 the same address or line numbers they used to be at last we
14004 checked. Losing your static tracepoints whenever you rebuild is
14005 undesirable. This function tries to resync/rematch gdb static
14006 tracepoints with the markers on the target, for static tracepoints
14007 that have not been set by marker id. Static tracepoint that have
14008 been set by marker id are reset by marker id in breakpoint_re_set.
14009 The heuristic is:
14010
14011 1) For a tracepoint set at a specific address, look for a marker at
14012 the old PC. If one is found there, assume to be the same marker.
14013 If the name / string id of the marker found is different from the
14014 previous known name, assume that means the user renamed the marker
14015 in the sources, and output a warning.
14016
14017 2) For a tracepoint set at a given line number, look for a marker
14018 at the new address of the old line number. If one is found there,
14019 assume to be the same marker. If the name / string id of the
14020 marker found is different from the previous known name, assume that
14021 means the user renamed the marker in the sources, and output a
14022 warning.
14023
14024 3) If a marker is no longer found at the same address or line, it
14025 may mean the marker no longer exists. But it may also just mean
14026 the code changed a bit. Maybe the user added a few lines of code
14027 that made the marker move up or down (in line number terms). Ask
14028 the target for info about the marker with the string id as we knew
14029 it. If found, update line number and address in the matching
14030 static tracepoint. This will get confused if there's more than one
14031 marker with the same ID (possible in UST, although unadvised
14032 precisely because it confuses tools). */
14033
14034 static struct symtab_and_line
14035 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14036 {
14037 struct tracepoint *tp = (struct tracepoint *) b;
14038 struct static_tracepoint_marker marker;
14039 CORE_ADDR pc;
14040
14041 pc = sal.pc;
14042 if (sal.line)
14043 find_line_pc (sal.symtab, sal.line, &pc);
14044
14045 if (target_static_tracepoint_marker_at (pc, &marker))
14046 {
14047 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14048 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14049 b->number,
14050 tp->static_trace_marker_id, marker.str_id);
14051
14052 xfree (tp->static_trace_marker_id);
14053 tp->static_trace_marker_id = xstrdup (marker.str_id);
14054 release_static_tracepoint_marker (&marker);
14055
14056 return sal;
14057 }
14058
14059 /* Old marker wasn't found on target at lineno. Try looking it up
14060 by string ID. */
14061 if (!sal.explicit_pc
14062 && sal.line != 0
14063 && sal.symtab != NULL
14064 && tp->static_trace_marker_id != NULL)
14065 {
14066 VEC(static_tracepoint_marker_p) *markers;
14067
14068 markers
14069 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14070
14071 if (!VEC_empty(static_tracepoint_marker_p, markers))
14072 {
14073 struct symtab_and_line sal2;
14074 struct symbol *sym;
14075 struct static_tracepoint_marker *tpmarker;
14076 struct ui_out *uiout = current_uiout;
14077 struct explicit_location explicit_loc;
14078
14079 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14080
14081 xfree (tp->static_trace_marker_id);
14082 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14083
14084 warning (_("marker for static tracepoint %d (%s) not "
14085 "found at previous line number"),
14086 b->number, tp->static_trace_marker_id);
14087
14088 init_sal (&sal2);
14089
14090 sal2.pc = tpmarker->address;
14091
14092 sal2 = find_pc_line (tpmarker->address, 0);
14093 sym = find_pc_sect_function (tpmarker->address, NULL);
14094 ui_out_text (uiout, "Now in ");
14095 if (sym)
14096 {
14097 ui_out_field_string (uiout, "func",
14098 SYMBOL_PRINT_NAME (sym));
14099 ui_out_text (uiout, " at ");
14100 }
14101 ui_out_field_string (uiout, "file",
14102 symtab_to_filename_for_display (sal2.symtab));
14103 ui_out_text (uiout, ":");
14104
14105 if (ui_out_is_mi_like_p (uiout))
14106 {
14107 const char *fullname = symtab_to_fullname (sal2.symtab);
14108
14109 ui_out_field_string (uiout, "fullname", fullname);
14110 }
14111
14112 ui_out_field_int (uiout, "line", sal2.line);
14113 ui_out_text (uiout, "\n");
14114
14115 b->loc->line_number = sal2.line;
14116 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14117
14118 delete_event_location (b->location);
14119 initialize_explicit_location (&explicit_loc);
14120 explicit_loc.source_filename
14121 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14122 explicit_loc.line_offset.offset = b->loc->line_number;
14123 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14124 b->location = new_explicit_location (&explicit_loc);
14125
14126 /* Might be nice to check if function changed, and warn if
14127 so. */
14128
14129 release_static_tracepoint_marker (tpmarker);
14130 }
14131 }
14132 return sal;
14133 }
14134
14135 /* Returns 1 iff locations A and B are sufficiently same that
14136 we don't need to report breakpoint as changed. */
14137
14138 static int
14139 locations_are_equal (struct bp_location *a, struct bp_location *b)
14140 {
14141 while (a && b)
14142 {
14143 if (a->address != b->address)
14144 return 0;
14145
14146 if (a->shlib_disabled != b->shlib_disabled)
14147 return 0;
14148
14149 if (a->enabled != b->enabled)
14150 return 0;
14151
14152 a = a->next;
14153 b = b->next;
14154 }
14155
14156 if ((a == NULL) != (b == NULL))
14157 return 0;
14158
14159 return 1;
14160 }
14161
14162 /* Create new breakpoint locations for B (a hardware or software breakpoint)
14163 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14164 a ranged breakpoint. */
14165
14166 void
14167 update_breakpoint_locations (struct breakpoint *b,
14168 struct symtabs_and_lines sals,
14169 struct symtabs_and_lines sals_end)
14170 {
14171 int i;
14172 struct bp_location *existing_locations = b->loc;
14173
14174 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14175 {
14176 /* Ranged breakpoints have only one start location and one end
14177 location. */
14178 b->enable_state = bp_disabled;
14179 update_global_location_list (UGLL_MAY_INSERT);
14180 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14181 "multiple locations found\n"),
14182 b->number);
14183 return;
14184 }
14185
14186 /* If there's no new locations, and all existing locations are
14187 pending, don't do anything. This optimizes the common case where
14188 all locations are in the same shared library, that was unloaded.
14189 We'd like to retain the location, so that when the library is
14190 loaded again, we don't loose the enabled/disabled status of the
14191 individual locations. */
14192 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
14193 return;
14194
14195 b->loc = NULL;
14196
14197 for (i = 0; i < sals.nelts; ++i)
14198 {
14199 struct bp_location *new_loc;
14200
14201 switch_to_program_space_and_thread (sals.sals[i].pspace);
14202
14203 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14204
14205 /* Reparse conditions, they might contain references to the
14206 old symtab. */
14207 if (b->cond_string != NULL)
14208 {
14209 const char *s;
14210
14211 s = b->cond_string;
14212 TRY
14213 {
14214 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14215 block_for_pc (sals.sals[i].pc),
14216 0);
14217 }
14218 CATCH (e, RETURN_MASK_ERROR)
14219 {
14220 warning (_("failed to reevaluate condition "
14221 "for breakpoint %d: %s"),
14222 b->number, e.message);
14223 new_loc->enabled = 0;
14224 }
14225 END_CATCH
14226 }
14227
14228 if (sals_end.nelts)
14229 {
14230 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14231
14232 new_loc->length = end - sals.sals[0].pc + 1;
14233 }
14234 }
14235
14236 /* If possible, carry over 'disable' status from existing
14237 breakpoints. */
14238 {
14239 struct bp_location *e = existing_locations;
14240 /* If there are multiple breakpoints with the same function name,
14241 e.g. for inline functions, comparing function names won't work.
14242 Instead compare pc addresses; this is just a heuristic as things
14243 may have moved, but in practice it gives the correct answer
14244 often enough until a better solution is found. */
14245 int have_ambiguous_names = ambiguous_names_p (b->loc);
14246
14247 for (; e; e = e->next)
14248 {
14249 if (!e->enabled && e->function_name)
14250 {
14251 struct bp_location *l = b->loc;
14252 if (have_ambiguous_names)
14253 {
14254 for (; l; l = l->next)
14255 if (breakpoint_locations_match (e, l))
14256 {
14257 l->enabled = 0;
14258 break;
14259 }
14260 }
14261 else
14262 {
14263 for (; l; l = l->next)
14264 if (l->function_name
14265 && strcmp (e->function_name, l->function_name) == 0)
14266 {
14267 l->enabled = 0;
14268 break;
14269 }
14270 }
14271 }
14272 }
14273 }
14274
14275 if (!locations_are_equal (existing_locations, b->loc))
14276 observer_notify_breakpoint_modified (b);
14277
14278 update_global_location_list (UGLL_MAY_INSERT);
14279 }
14280
14281 /* Find the SaL locations corresponding to the given LOCATION.
14282 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14283
14284 static struct symtabs_and_lines
14285 location_to_sals (struct breakpoint *b, struct event_location *location,
14286 int *found)
14287 {
14288 struct symtabs_and_lines sals = {0};
14289 struct gdb_exception exception = exception_none;
14290
14291 gdb_assert (b->ops != NULL);
14292
14293 TRY
14294 {
14295 b->ops->decode_location (b, location, &sals);
14296 }
14297 CATCH (e, RETURN_MASK_ERROR)
14298 {
14299 int not_found_and_ok = 0;
14300
14301 exception = e;
14302
14303 /* For pending breakpoints, it's expected that parsing will
14304 fail until the right shared library is loaded. User has
14305 already told to create pending breakpoints and don't need
14306 extra messages. If breakpoint is in bp_shlib_disabled
14307 state, then user already saw the message about that
14308 breakpoint being disabled, and don't want to see more
14309 errors. */
14310 if (e.error == NOT_FOUND_ERROR
14311 && (b->condition_not_parsed
14312 || (b->loc && b->loc->shlib_disabled)
14313 || (b->loc && b->loc->pspace->executing_startup)
14314 || b->enable_state == bp_disabled))
14315 not_found_and_ok = 1;
14316
14317 if (!not_found_and_ok)
14318 {
14319 /* We surely don't want to warn about the same breakpoint
14320 10 times. One solution, implemented here, is disable
14321 the breakpoint on error. Another solution would be to
14322 have separate 'warning emitted' flag. Since this
14323 happens only when a binary has changed, I don't know
14324 which approach is better. */
14325 b->enable_state = bp_disabled;
14326 throw_exception (e);
14327 }
14328 }
14329 END_CATCH
14330
14331 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14332 {
14333 int i;
14334
14335 for (i = 0; i < sals.nelts; ++i)
14336 resolve_sal_pc (&sals.sals[i]);
14337 if (b->condition_not_parsed && b->extra_string != NULL)
14338 {
14339 char *cond_string, *extra_string;
14340 int thread, task;
14341
14342 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14343 &cond_string, &thread, &task,
14344 &extra_string);
14345 gdb_assert (b->cond_string == NULL);
14346 if (cond_string)
14347 b->cond_string = cond_string;
14348 b->thread = thread;
14349 b->task = task;
14350 if (extra_string)
14351 {
14352 xfree (b->extra_string);
14353 b->extra_string = extra_string;
14354 }
14355 b->condition_not_parsed = 0;
14356 }
14357
14358 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14359 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14360
14361 *found = 1;
14362 }
14363 else
14364 *found = 0;
14365
14366 return sals;
14367 }
14368
14369 /* The default re_set method, for typical hardware or software
14370 breakpoints. Reevaluate the breakpoint and recreate its
14371 locations. */
14372
14373 static void
14374 breakpoint_re_set_default (struct breakpoint *b)
14375 {
14376 int found;
14377 struct symtabs_and_lines sals, sals_end;
14378 struct symtabs_and_lines expanded = {0};
14379 struct symtabs_and_lines expanded_end = {0};
14380
14381 sals = location_to_sals (b, b->location, &found);
14382 if (found)
14383 {
14384 make_cleanup (xfree, sals.sals);
14385 expanded = sals;
14386 }
14387
14388 if (b->location_range_end != NULL)
14389 {
14390 sals_end = location_to_sals (b, b->location_range_end, &found);
14391 if (found)
14392 {
14393 make_cleanup (xfree, sals_end.sals);
14394 expanded_end = sals_end;
14395 }
14396 }
14397
14398 update_breakpoint_locations (b, expanded, expanded_end);
14399 }
14400
14401 /* Default method for creating SALs from an address string. It basically
14402 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14403
14404 static void
14405 create_sals_from_location_default (const struct event_location *location,
14406 struct linespec_result *canonical,
14407 enum bptype type_wanted)
14408 {
14409 parse_breakpoint_sals (location, canonical);
14410 }
14411
14412 /* Call create_breakpoints_sal for the given arguments. This is the default
14413 function for the `create_breakpoints_sal' method of
14414 breakpoint_ops. */
14415
14416 static void
14417 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14418 struct linespec_result *canonical,
14419 char *cond_string,
14420 char *extra_string,
14421 enum bptype type_wanted,
14422 enum bpdisp disposition,
14423 int thread,
14424 int task, int ignore_count,
14425 const struct breakpoint_ops *ops,
14426 int from_tty, int enabled,
14427 int internal, unsigned flags)
14428 {
14429 create_breakpoints_sal (gdbarch, canonical, cond_string,
14430 extra_string,
14431 type_wanted, disposition,
14432 thread, task, ignore_count, ops, from_tty,
14433 enabled, internal, flags);
14434 }
14435
14436 /* Decode the line represented by S by calling decode_line_full. This is the
14437 default function for the `decode_location' method of breakpoint_ops. */
14438
14439 static void
14440 decode_location_default (struct breakpoint *b,
14441 const struct event_location *location,
14442 struct symtabs_and_lines *sals)
14443 {
14444 struct linespec_result canonical;
14445
14446 init_linespec_result (&canonical);
14447 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
14448 (struct symtab *) NULL, 0,
14449 &canonical, multiple_symbols_all,
14450 b->filter);
14451
14452 /* We should get 0 or 1 resulting SALs. */
14453 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14454
14455 if (VEC_length (linespec_sals, canonical.sals) > 0)
14456 {
14457 struct linespec_sals *lsal;
14458
14459 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14460 *sals = lsal->sals;
14461 /* Arrange it so the destructor does not free the
14462 contents. */
14463 lsal->sals.sals = NULL;
14464 }
14465
14466 destroy_linespec_result (&canonical);
14467 }
14468
14469 /* Prepare the global context for a re-set of breakpoint B. */
14470
14471 static struct cleanup *
14472 prepare_re_set_context (struct breakpoint *b)
14473 {
14474 struct cleanup *cleanups;
14475
14476 input_radix = b->input_radix;
14477 cleanups = save_current_space_and_thread ();
14478 if (b->pspace != NULL)
14479 switch_to_program_space_and_thread (b->pspace);
14480 set_language (b->language);
14481
14482 return cleanups;
14483 }
14484
14485 /* Reset a breakpoint given it's struct breakpoint * BINT.
14486 The value we return ends up being the return value from catch_errors.
14487 Unused in this case. */
14488
14489 static int
14490 breakpoint_re_set_one (void *bint)
14491 {
14492 /* Get past catch_errs. */
14493 struct breakpoint *b = (struct breakpoint *) bint;
14494 struct cleanup *cleanups;
14495
14496 cleanups = prepare_re_set_context (b);
14497 b->ops->re_set (b);
14498 do_cleanups (cleanups);
14499 return 0;
14500 }
14501
14502 /* Re-set all breakpoints after symbols have been re-loaded. */
14503 void
14504 breakpoint_re_set (void)
14505 {
14506 struct breakpoint *b, *b_tmp;
14507 enum language save_language;
14508 int save_input_radix;
14509 struct cleanup *old_chain;
14510
14511 save_language = current_language->la_language;
14512 save_input_radix = input_radix;
14513 old_chain = save_current_program_space ();
14514
14515 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14516 {
14517 /* Format possible error msg. */
14518 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14519 b->number);
14520 struct cleanup *cleanups = make_cleanup (xfree, message);
14521 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14522 do_cleanups (cleanups);
14523 }
14524 set_language (save_language);
14525 input_radix = save_input_radix;
14526
14527 jit_breakpoint_re_set ();
14528
14529 do_cleanups (old_chain);
14530
14531 create_overlay_event_breakpoint ();
14532 create_longjmp_master_breakpoint ();
14533 create_std_terminate_master_breakpoint ();
14534 create_exception_master_breakpoint ();
14535 }
14536 \f
14537 /* Reset the thread number of this breakpoint:
14538
14539 - If the breakpoint is for all threads, leave it as-is.
14540 - Else, reset it to the current thread for inferior_ptid. */
14541 void
14542 breakpoint_re_set_thread (struct breakpoint *b)
14543 {
14544 if (b->thread != -1)
14545 {
14546 if (in_thread_list (inferior_ptid))
14547 b->thread = ptid_to_global_thread_id (inferior_ptid);
14548
14549 /* We're being called after following a fork. The new fork is
14550 selected as current, and unless this was a vfork will have a
14551 different program space from the original thread. Reset that
14552 as well. */
14553 b->loc->pspace = current_program_space;
14554 }
14555 }
14556
14557 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14558 If from_tty is nonzero, it prints a message to that effect,
14559 which ends with a period (no newline). */
14560
14561 void
14562 set_ignore_count (int bptnum, int count, int from_tty)
14563 {
14564 struct breakpoint *b;
14565
14566 if (count < 0)
14567 count = 0;
14568
14569 ALL_BREAKPOINTS (b)
14570 if (b->number == bptnum)
14571 {
14572 if (is_tracepoint (b))
14573 {
14574 if (from_tty && count != 0)
14575 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14576 bptnum);
14577 return;
14578 }
14579
14580 b->ignore_count = count;
14581 if (from_tty)
14582 {
14583 if (count == 0)
14584 printf_filtered (_("Will stop next time "
14585 "breakpoint %d is reached."),
14586 bptnum);
14587 else if (count == 1)
14588 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14589 bptnum);
14590 else
14591 printf_filtered (_("Will ignore next %d "
14592 "crossings of breakpoint %d."),
14593 count, bptnum);
14594 }
14595 observer_notify_breakpoint_modified (b);
14596 return;
14597 }
14598
14599 error (_("No breakpoint number %d."), bptnum);
14600 }
14601
14602 /* Command to set ignore-count of breakpoint N to COUNT. */
14603
14604 static void
14605 ignore_command (char *args, int from_tty)
14606 {
14607 char *p = args;
14608 int num;
14609
14610 if (p == 0)
14611 error_no_arg (_("a breakpoint number"));
14612
14613 num = get_number (&p);
14614 if (num == 0)
14615 error (_("bad breakpoint number: '%s'"), args);
14616 if (*p == 0)
14617 error (_("Second argument (specified ignore-count) is missing."));
14618
14619 set_ignore_count (num,
14620 longest_to_int (value_as_long (parse_and_eval (p))),
14621 from_tty);
14622 if (from_tty)
14623 printf_filtered ("\n");
14624 }
14625 \f
14626 /* Call FUNCTION on each of the breakpoints
14627 whose numbers are given in ARGS. */
14628
14629 static void
14630 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14631 void *),
14632 void *data)
14633 {
14634 int num;
14635 struct breakpoint *b, *tmp;
14636 int match;
14637 struct get_number_or_range_state state;
14638
14639 if (args == 0 || *args == '\0')
14640 error_no_arg (_("one or more breakpoint numbers"));
14641
14642 init_number_or_range (&state, args);
14643
14644 while (!state.finished)
14645 {
14646 const char *p = state.string;
14647
14648 match = 0;
14649
14650 num = get_number_or_range (&state);
14651 if (num == 0)
14652 {
14653 warning (_("bad breakpoint number at or near '%s'"), p);
14654 }
14655 else
14656 {
14657 ALL_BREAKPOINTS_SAFE (b, tmp)
14658 if (b->number == num)
14659 {
14660 match = 1;
14661 function (b, data);
14662 break;
14663 }
14664 if (match == 0)
14665 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14666 }
14667 }
14668 }
14669
14670 static struct bp_location *
14671 find_location_by_number (char *number)
14672 {
14673 char *dot = strchr (number, '.');
14674 char *p1;
14675 int bp_num;
14676 int loc_num;
14677 struct breakpoint *b;
14678 struct bp_location *loc;
14679
14680 *dot = '\0';
14681
14682 p1 = number;
14683 bp_num = get_number (&p1);
14684 if (bp_num == 0)
14685 error (_("Bad breakpoint number '%s'"), number);
14686
14687 ALL_BREAKPOINTS (b)
14688 if (b->number == bp_num)
14689 {
14690 break;
14691 }
14692
14693 if (!b || b->number != bp_num)
14694 error (_("Bad breakpoint number '%s'"), number);
14695
14696 p1 = dot+1;
14697 loc_num = get_number (&p1);
14698 if (loc_num == 0)
14699 error (_("Bad breakpoint location number '%s'"), number);
14700
14701 --loc_num;
14702 loc = b->loc;
14703 for (;loc_num && loc; --loc_num, loc = loc->next)
14704 ;
14705 if (!loc)
14706 error (_("Bad breakpoint location number '%s'"), dot+1);
14707
14708 return loc;
14709 }
14710
14711
14712 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14713 If from_tty is nonzero, it prints a message to that effect,
14714 which ends with a period (no newline). */
14715
14716 void
14717 disable_breakpoint (struct breakpoint *bpt)
14718 {
14719 /* Never disable a watchpoint scope breakpoint; we want to
14720 hit them when we leave scope so we can delete both the
14721 watchpoint and its scope breakpoint at that time. */
14722 if (bpt->type == bp_watchpoint_scope)
14723 return;
14724
14725 bpt->enable_state = bp_disabled;
14726
14727 /* Mark breakpoint locations modified. */
14728 mark_breakpoint_modified (bpt);
14729
14730 if (target_supports_enable_disable_tracepoint ()
14731 && current_trace_status ()->running && is_tracepoint (bpt))
14732 {
14733 struct bp_location *location;
14734
14735 for (location = bpt->loc; location; location = location->next)
14736 target_disable_tracepoint (location);
14737 }
14738
14739 update_global_location_list (UGLL_DONT_INSERT);
14740
14741 observer_notify_breakpoint_modified (bpt);
14742 }
14743
14744 /* A callback for iterate_over_related_breakpoints. */
14745
14746 static void
14747 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14748 {
14749 disable_breakpoint (b);
14750 }
14751
14752 /* A callback for map_breakpoint_numbers that calls
14753 disable_breakpoint. */
14754
14755 static void
14756 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14757 {
14758 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14759 }
14760
14761 static void
14762 disable_command (char *args, int from_tty)
14763 {
14764 if (args == 0)
14765 {
14766 struct breakpoint *bpt;
14767
14768 ALL_BREAKPOINTS (bpt)
14769 if (user_breakpoint_p (bpt))
14770 disable_breakpoint (bpt);
14771 }
14772 else
14773 {
14774 char *num = extract_arg (&args);
14775
14776 while (num)
14777 {
14778 if (strchr (num, '.'))
14779 {
14780 struct bp_location *loc = find_location_by_number (num);
14781
14782 if (loc)
14783 {
14784 if (loc->enabled)
14785 {
14786 loc->enabled = 0;
14787 mark_breakpoint_location_modified (loc);
14788 }
14789 if (target_supports_enable_disable_tracepoint ()
14790 && current_trace_status ()->running && loc->owner
14791 && is_tracepoint (loc->owner))
14792 target_disable_tracepoint (loc);
14793 }
14794 update_global_location_list (UGLL_DONT_INSERT);
14795 }
14796 else
14797 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14798 num = extract_arg (&args);
14799 }
14800 }
14801 }
14802
14803 static void
14804 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14805 int count)
14806 {
14807 int target_resources_ok;
14808
14809 if (bpt->type == bp_hardware_breakpoint)
14810 {
14811 int i;
14812 i = hw_breakpoint_used_count ();
14813 target_resources_ok =
14814 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14815 i + 1, 0);
14816 if (target_resources_ok == 0)
14817 error (_("No hardware breakpoint support in the target."));
14818 else if (target_resources_ok < 0)
14819 error (_("Hardware breakpoints used exceeds limit."));
14820 }
14821
14822 if (is_watchpoint (bpt))
14823 {
14824 /* Initialize it just to avoid a GCC false warning. */
14825 enum enable_state orig_enable_state = bp_disabled;
14826
14827 TRY
14828 {
14829 struct watchpoint *w = (struct watchpoint *) bpt;
14830
14831 orig_enable_state = bpt->enable_state;
14832 bpt->enable_state = bp_enabled;
14833 update_watchpoint (w, 1 /* reparse */);
14834 }
14835 CATCH (e, RETURN_MASK_ALL)
14836 {
14837 bpt->enable_state = orig_enable_state;
14838 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14839 bpt->number);
14840 return;
14841 }
14842 END_CATCH
14843 }
14844
14845 bpt->enable_state = bp_enabled;
14846
14847 /* Mark breakpoint locations modified. */
14848 mark_breakpoint_modified (bpt);
14849
14850 if (target_supports_enable_disable_tracepoint ()
14851 && current_trace_status ()->running && is_tracepoint (bpt))
14852 {
14853 struct bp_location *location;
14854
14855 for (location = bpt->loc; location; location = location->next)
14856 target_enable_tracepoint (location);
14857 }
14858
14859 bpt->disposition = disposition;
14860 bpt->enable_count = count;
14861 update_global_location_list (UGLL_MAY_INSERT);
14862
14863 observer_notify_breakpoint_modified (bpt);
14864 }
14865
14866
14867 void
14868 enable_breakpoint (struct breakpoint *bpt)
14869 {
14870 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14871 }
14872
14873 static void
14874 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14875 {
14876 enable_breakpoint (bpt);
14877 }
14878
14879 /* A callback for map_breakpoint_numbers that calls
14880 enable_breakpoint. */
14881
14882 static void
14883 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14884 {
14885 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14886 }
14887
14888 /* The enable command enables the specified breakpoints (or all defined
14889 breakpoints) so they once again become (or continue to be) effective
14890 in stopping the inferior. */
14891
14892 static void
14893 enable_command (char *args, int from_tty)
14894 {
14895 if (args == 0)
14896 {
14897 struct breakpoint *bpt;
14898
14899 ALL_BREAKPOINTS (bpt)
14900 if (user_breakpoint_p (bpt))
14901 enable_breakpoint (bpt);
14902 }
14903 else
14904 {
14905 char *num = extract_arg (&args);
14906
14907 while (num)
14908 {
14909 if (strchr (num, '.'))
14910 {
14911 struct bp_location *loc = find_location_by_number (num);
14912
14913 if (loc)
14914 {
14915 if (!loc->enabled)
14916 {
14917 loc->enabled = 1;
14918 mark_breakpoint_location_modified (loc);
14919 }
14920 if (target_supports_enable_disable_tracepoint ()
14921 && current_trace_status ()->running && loc->owner
14922 && is_tracepoint (loc->owner))
14923 target_enable_tracepoint (loc);
14924 }
14925 update_global_location_list (UGLL_MAY_INSERT);
14926 }
14927 else
14928 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14929 num = extract_arg (&args);
14930 }
14931 }
14932 }
14933
14934 /* This struct packages up disposition data for application to multiple
14935 breakpoints. */
14936
14937 struct disp_data
14938 {
14939 enum bpdisp disp;
14940 int count;
14941 };
14942
14943 static void
14944 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14945 {
14946 struct disp_data disp_data = *(struct disp_data *) arg;
14947
14948 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14949 }
14950
14951 static void
14952 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14953 {
14954 struct disp_data disp = { disp_disable, 1 };
14955
14956 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14957 }
14958
14959 static void
14960 enable_once_command (char *args, int from_tty)
14961 {
14962 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14963 }
14964
14965 static void
14966 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14967 {
14968 struct disp_data disp = { disp_disable, *(int *) countptr };
14969
14970 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14971 }
14972
14973 static void
14974 enable_count_command (char *args, int from_tty)
14975 {
14976 int count;
14977
14978 if (args == NULL)
14979 error_no_arg (_("hit count"));
14980
14981 count = get_number (&args);
14982
14983 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14984 }
14985
14986 static void
14987 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14988 {
14989 struct disp_data disp = { disp_del, 1 };
14990
14991 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14992 }
14993
14994 static void
14995 enable_delete_command (char *args, int from_tty)
14996 {
14997 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14998 }
14999 \f
15000 static void
15001 set_breakpoint_cmd (char *args, int from_tty)
15002 {
15003 }
15004
15005 static void
15006 show_breakpoint_cmd (char *args, int from_tty)
15007 {
15008 }
15009
15010 /* Invalidate last known value of any hardware watchpoint if
15011 the memory which that value represents has been written to by
15012 GDB itself. */
15013
15014 static void
15015 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15016 CORE_ADDR addr, ssize_t len,
15017 const bfd_byte *data)
15018 {
15019 struct breakpoint *bp;
15020
15021 ALL_BREAKPOINTS (bp)
15022 if (bp->enable_state == bp_enabled
15023 && bp->type == bp_hardware_watchpoint)
15024 {
15025 struct watchpoint *wp = (struct watchpoint *) bp;
15026
15027 if (wp->val_valid && wp->val)
15028 {
15029 struct bp_location *loc;
15030
15031 for (loc = bp->loc; loc != NULL; loc = loc->next)
15032 if (loc->loc_type == bp_loc_hardware_watchpoint
15033 && loc->address + loc->length > addr
15034 && addr + len > loc->address)
15035 {
15036 value_free (wp->val);
15037 wp->val = NULL;
15038 wp->val_valid = 0;
15039 }
15040 }
15041 }
15042 }
15043
15044 /* Create and insert a breakpoint for software single step. */
15045
15046 void
15047 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15048 struct address_space *aspace,
15049 CORE_ADDR next_pc)
15050 {
15051 struct thread_info *tp = inferior_thread ();
15052 struct symtab_and_line sal;
15053 CORE_ADDR pc = next_pc;
15054
15055 if (tp->control.single_step_breakpoints == NULL)
15056 {
15057 tp->control.single_step_breakpoints
15058 = new_single_step_breakpoint (tp->global_num, gdbarch);
15059 }
15060
15061 sal = find_pc_line (pc, 0);
15062 sal.pc = pc;
15063 sal.section = find_pc_overlay (pc);
15064 sal.explicit_pc = 1;
15065 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15066
15067 update_global_location_list (UGLL_INSERT);
15068 }
15069
15070 /* See breakpoint.h. */
15071
15072 int
15073 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15074 struct address_space *aspace,
15075 CORE_ADDR pc)
15076 {
15077 struct bp_location *loc;
15078
15079 for (loc = bp->loc; loc != NULL; loc = loc->next)
15080 if (loc->inserted
15081 && breakpoint_location_address_match (loc, aspace, pc))
15082 return 1;
15083
15084 return 0;
15085 }
15086
15087 /* Check whether a software single-step breakpoint is inserted at
15088 PC. */
15089
15090 int
15091 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15092 CORE_ADDR pc)
15093 {
15094 struct breakpoint *bpt;
15095
15096 ALL_BREAKPOINTS (bpt)
15097 {
15098 if (bpt->type == bp_single_step
15099 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15100 return 1;
15101 }
15102 return 0;
15103 }
15104
15105 /* Tracepoint-specific operations. */
15106
15107 /* Set tracepoint count to NUM. */
15108 static void
15109 set_tracepoint_count (int num)
15110 {
15111 tracepoint_count = num;
15112 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15113 }
15114
15115 static void
15116 trace_command (char *arg, int from_tty)
15117 {
15118 struct breakpoint_ops *ops;
15119 struct event_location *location;
15120 struct cleanup *back_to;
15121
15122 location = string_to_event_location (&arg, current_language);
15123 back_to = make_cleanup_delete_event_location (location);
15124 if (location != NULL
15125 && event_location_type (location) == PROBE_LOCATION)
15126 ops = &tracepoint_probe_breakpoint_ops;
15127 else
15128 ops = &tracepoint_breakpoint_ops;
15129
15130 create_breakpoint (get_current_arch (),
15131 location,
15132 NULL, 0, arg, 1 /* parse arg */,
15133 0 /* tempflag */,
15134 bp_tracepoint /* type_wanted */,
15135 0 /* Ignore count */,
15136 pending_break_support,
15137 ops,
15138 from_tty,
15139 1 /* enabled */,
15140 0 /* internal */, 0);
15141 do_cleanups (back_to);
15142 }
15143
15144 static void
15145 ftrace_command (char *arg, int from_tty)
15146 {
15147 struct event_location *location;
15148 struct cleanup *back_to;
15149
15150 location = string_to_event_location (&arg, current_language);
15151 back_to = make_cleanup_delete_event_location (location);
15152 create_breakpoint (get_current_arch (),
15153 location,
15154 NULL, 0, arg, 1 /* parse arg */,
15155 0 /* tempflag */,
15156 bp_fast_tracepoint /* type_wanted */,
15157 0 /* Ignore count */,
15158 pending_break_support,
15159 &tracepoint_breakpoint_ops,
15160 from_tty,
15161 1 /* enabled */,
15162 0 /* internal */, 0);
15163 do_cleanups (back_to);
15164 }
15165
15166 /* strace command implementation. Creates a static tracepoint. */
15167
15168 static void
15169 strace_command (char *arg, int from_tty)
15170 {
15171 struct breakpoint_ops *ops;
15172 struct event_location *location;
15173 struct cleanup *back_to;
15174
15175 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15176 or with a normal static tracepoint. */
15177 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15178 {
15179 ops = &strace_marker_breakpoint_ops;
15180 location = new_linespec_location (&arg);
15181 }
15182 else
15183 {
15184 ops = &tracepoint_breakpoint_ops;
15185 location = string_to_event_location (&arg, current_language);
15186 }
15187
15188 back_to = make_cleanup_delete_event_location (location);
15189 create_breakpoint (get_current_arch (),
15190 location,
15191 NULL, 0, arg, 1 /* parse arg */,
15192 0 /* tempflag */,
15193 bp_static_tracepoint /* type_wanted */,
15194 0 /* Ignore count */,
15195 pending_break_support,
15196 ops,
15197 from_tty,
15198 1 /* enabled */,
15199 0 /* internal */, 0);
15200 do_cleanups (back_to);
15201 }
15202
15203 /* Set up a fake reader function that gets command lines from a linked
15204 list that was acquired during tracepoint uploading. */
15205
15206 static struct uploaded_tp *this_utp;
15207 static int next_cmd;
15208
15209 static char *
15210 read_uploaded_action (void)
15211 {
15212 char *rslt;
15213
15214 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15215
15216 next_cmd++;
15217
15218 return rslt;
15219 }
15220
15221 /* Given information about a tracepoint as recorded on a target (which
15222 can be either a live system or a trace file), attempt to create an
15223 equivalent GDB tracepoint. This is not a reliable process, since
15224 the target does not necessarily have all the information used when
15225 the tracepoint was originally defined. */
15226
15227 struct tracepoint *
15228 create_tracepoint_from_upload (struct uploaded_tp *utp)
15229 {
15230 char *addr_str, small_buf[100];
15231 struct tracepoint *tp;
15232 struct event_location *location;
15233 struct cleanup *cleanup;
15234
15235 if (utp->at_string)
15236 addr_str = utp->at_string;
15237 else
15238 {
15239 /* In the absence of a source location, fall back to raw
15240 address. Since there is no way to confirm that the address
15241 means the same thing as when the trace was started, warn the
15242 user. */
15243 warning (_("Uploaded tracepoint %d has no "
15244 "source location, using raw address"),
15245 utp->number);
15246 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15247 addr_str = small_buf;
15248 }
15249
15250 /* There's not much we can do with a sequence of bytecodes. */
15251 if (utp->cond && !utp->cond_string)
15252 warning (_("Uploaded tracepoint %d condition "
15253 "has no source form, ignoring it"),
15254 utp->number);
15255
15256 location = string_to_event_location (&addr_str, current_language);
15257 cleanup = make_cleanup_delete_event_location (location);
15258 if (!create_breakpoint (get_current_arch (),
15259 location,
15260 utp->cond_string, -1, addr_str,
15261 0 /* parse cond/thread */,
15262 0 /* tempflag */,
15263 utp->type /* type_wanted */,
15264 0 /* Ignore count */,
15265 pending_break_support,
15266 &tracepoint_breakpoint_ops,
15267 0 /* from_tty */,
15268 utp->enabled /* enabled */,
15269 0 /* internal */,
15270 CREATE_BREAKPOINT_FLAGS_INSERTED))
15271 {
15272 do_cleanups (cleanup);
15273 return NULL;
15274 }
15275
15276 do_cleanups (cleanup);
15277
15278 /* Get the tracepoint we just created. */
15279 tp = get_tracepoint (tracepoint_count);
15280 gdb_assert (tp != NULL);
15281
15282 if (utp->pass > 0)
15283 {
15284 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15285 tp->base.number);
15286
15287 trace_pass_command (small_buf, 0);
15288 }
15289
15290 /* If we have uploaded versions of the original commands, set up a
15291 special-purpose "reader" function and call the usual command line
15292 reader, then pass the result to the breakpoint command-setting
15293 function. */
15294 if (!VEC_empty (char_ptr, utp->cmd_strings))
15295 {
15296 struct command_line *cmd_list;
15297
15298 this_utp = utp;
15299 next_cmd = 0;
15300
15301 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15302
15303 breakpoint_set_commands (&tp->base, cmd_list);
15304 }
15305 else if (!VEC_empty (char_ptr, utp->actions)
15306 || !VEC_empty (char_ptr, utp->step_actions))
15307 warning (_("Uploaded tracepoint %d actions "
15308 "have no source form, ignoring them"),
15309 utp->number);
15310
15311 /* Copy any status information that might be available. */
15312 tp->base.hit_count = utp->hit_count;
15313 tp->traceframe_usage = utp->traceframe_usage;
15314
15315 return tp;
15316 }
15317
15318 /* Print information on tracepoint number TPNUM_EXP, or all if
15319 omitted. */
15320
15321 static void
15322 tracepoints_info (char *args, int from_tty)
15323 {
15324 struct ui_out *uiout = current_uiout;
15325 int num_printed;
15326
15327 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15328
15329 if (num_printed == 0)
15330 {
15331 if (args == NULL || *args == '\0')
15332 ui_out_message (uiout, 0, "No tracepoints.\n");
15333 else
15334 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15335 }
15336
15337 default_collect_info ();
15338 }
15339
15340 /* The 'enable trace' command enables tracepoints.
15341 Not supported by all targets. */
15342 static void
15343 enable_trace_command (char *args, int from_tty)
15344 {
15345 enable_command (args, from_tty);
15346 }
15347
15348 /* The 'disable trace' command disables tracepoints.
15349 Not supported by all targets. */
15350 static void
15351 disable_trace_command (char *args, int from_tty)
15352 {
15353 disable_command (args, from_tty);
15354 }
15355
15356 /* Remove a tracepoint (or all if no argument). */
15357 static void
15358 delete_trace_command (char *arg, int from_tty)
15359 {
15360 struct breakpoint *b, *b_tmp;
15361
15362 dont_repeat ();
15363
15364 if (arg == 0)
15365 {
15366 int breaks_to_delete = 0;
15367
15368 /* Delete all breakpoints if no argument.
15369 Do not delete internal or call-dummy breakpoints, these
15370 have to be deleted with an explicit breakpoint number
15371 argument. */
15372 ALL_TRACEPOINTS (b)
15373 if (is_tracepoint (b) && user_breakpoint_p (b))
15374 {
15375 breaks_to_delete = 1;
15376 break;
15377 }
15378
15379 /* Ask user only if there are some breakpoints to delete. */
15380 if (!from_tty
15381 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15382 {
15383 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15384 if (is_tracepoint (b) && user_breakpoint_p (b))
15385 delete_breakpoint (b);
15386 }
15387 }
15388 else
15389 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15390 }
15391
15392 /* Helper function for trace_pass_command. */
15393
15394 static void
15395 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15396 {
15397 tp->pass_count = count;
15398 observer_notify_breakpoint_modified (&tp->base);
15399 if (from_tty)
15400 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15401 tp->base.number, count);
15402 }
15403
15404 /* Set passcount for tracepoint.
15405
15406 First command argument is passcount, second is tracepoint number.
15407 If tracepoint number omitted, apply to most recently defined.
15408 Also accepts special argument "all". */
15409
15410 static void
15411 trace_pass_command (char *args, int from_tty)
15412 {
15413 struct tracepoint *t1;
15414 unsigned int count;
15415
15416 if (args == 0 || *args == 0)
15417 error (_("passcount command requires an "
15418 "argument (count + optional TP num)"));
15419
15420 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15421
15422 args = skip_spaces (args);
15423 if (*args && strncasecmp (args, "all", 3) == 0)
15424 {
15425 struct breakpoint *b;
15426
15427 args += 3; /* Skip special argument "all". */
15428 if (*args)
15429 error (_("Junk at end of arguments."));
15430
15431 ALL_TRACEPOINTS (b)
15432 {
15433 t1 = (struct tracepoint *) b;
15434 trace_pass_set_count (t1, count, from_tty);
15435 }
15436 }
15437 else if (*args == '\0')
15438 {
15439 t1 = get_tracepoint_by_number (&args, NULL);
15440 if (t1)
15441 trace_pass_set_count (t1, count, from_tty);
15442 }
15443 else
15444 {
15445 struct get_number_or_range_state state;
15446
15447 init_number_or_range (&state, args);
15448 while (!state.finished)
15449 {
15450 t1 = get_tracepoint_by_number (&args, &state);
15451 if (t1)
15452 trace_pass_set_count (t1, count, from_tty);
15453 }
15454 }
15455 }
15456
15457 struct tracepoint *
15458 get_tracepoint (int num)
15459 {
15460 struct breakpoint *t;
15461
15462 ALL_TRACEPOINTS (t)
15463 if (t->number == num)
15464 return (struct tracepoint *) t;
15465
15466 return NULL;
15467 }
15468
15469 /* Find the tracepoint with the given target-side number (which may be
15470 different from the tracepoint number after disconnecting and
15471 reconnecting). */
15472
15473 struct tracepoint *
15474 get_tracepoint_by_number_on_target (int num)
15475 {
15476 struct breakpoint *b;
15477
15478 ALL_TRACEPOINTS (b)
15479 {
15480 struct tracepoint *t = (struct tracepoint *) b;
15481
15482 if (t->number_on_target == num)
15483 return t;
15484 }
15485
15486 return NULL;
15487 }
15488
15489 /* Utility: parse a tracepoint number and look it up in the list.
15490 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15491 If the argument is missing, the most recent tracepoint
15492 (tracepoint_count) is returned. */
15493
15494 struct tracepoint *
15495 get_tracepoint_by_number (char **arg,
15496 struct get_number_or_range_state *state)
15497 {
15498 struct breakpoint *t;
15499 int tpnum;
15500 char *instring = arg == NULL ? NULL : *arg;
15501
15502 if (state)
15503 {
15504 gdb_assert (!state->finished);
15505 tpnum = get_number_or_range (state);
15506 }
15507 else if (arg == NULL || *arg == NULL || ! **arg)
15508 tpnum = tracepoint_count;
15509 else
15510 tpnum = get_number (arg);
15511
15512 if (tpnum <= 0)
15513 {
15514 if (instring && *instring)
15515 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15516 instring);
15517 else
15518 printf_filtered (_("No previous tracepoint\n"));
15519 return NULL;
15520 }
15521
15522 ALL_TRACEPOINTS (t)
15523 if (t->number == tpnum)
15524 {
15525 return (struct tracepoint *) t;
15526 }
15527
15528 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15529 return NULL;
15530 }
15531
15532 void
15533 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15534 {
15535 if (b->thread != -1)
15536 fprintf_unfiltered (fp, " thread %d", b->thread);
15537
15538 if (b->task != 0)
15539 fprintf_unfiltered (fp, " task %d", b->task);
15540
15541 fprintf_unfiltered (fp, "\n");
15542 }
15543
15544 /* Save information on user settable breakpoints (watchpoints, etc) to
15545 a new script file named FILENAME. If FILTER is non-NULL, call it
15546 on each breakpoint and only include the ones for which it returns
15547 non-zero. */
15548
15549 static void
15550 save_breakpoints (char *filename, int from_tty,
15551 int (*filter) (const struct breakpoint *))
15552 {
15553 struct breakpoint *tp;
15554 int any = 0;
15555 struct cleanup *cleanup;
15556 struct ui_file *fp;
15557 int extra_trace_bits = 0;
15558
15559 if (filename == 0 || *filename == 0)
15560 error (_("Argument required (file name in which to save)"));
15561
15562 /* See if we have anything to save. */
15563 ALL_BREAKPOINTS (tp)
15564 {
15565 /* Skip internal and momentary breakpoints. */
15566 if (!user_breakpoint_p (tp))
15567 continue;
15568
15569 /* If we have a filter, only save the breakpoints it accepts. */
15570 if (filter && !filter (tp))
15571 continue;
15572
15573 any = 1;
15574
15575 if (is_tracepoint (tp))
15576 {
15577 extra_trace_bits = 1;
15578
15579 /* We can stop searching. */
15580 break;
15581 }
15582 }
15583
15584 if (!any)
15585 {
15586 warning (_("Nothing to save."));
15587 return;
15588 }
15589
15590 filename = tilde_expand (filename);
15591 cleanup = make_cleanup (xfree, filename);
15592 fp = gdb_fopen (filename, "w");
15593 if (!fp)
15594 error (_("Unable to open file '%s' for saving (%s)"),
15595 filename, safe_strerror (errno));
15596 make_cleanup_ui_file_delete (fp);
15597
15598 if (extra_trace_bits)
15599 save_trace_state_variables (fp);
15600
15601 ALL_BREAKPOINTS (tp)
15602 {
15603 /* Skip internal and momentary breakpoints. */
15604 if (!user_breakpoint_p (tp))
15605 continue;
15606
15607 /* If we have a filter, only save the breakpoints it accepts. */
15608 if (filter && !filter (tp))
15609 continue;
15610
15611 tp->ops->print_recreate (tp, fp);
15612
15613 /* Note, we can't rely on tp->number for anything, as we can't
15614 assume the recreated breakpoint numbers will match. Use $bpnum
15615 instead. */
15616
15617 if (tp->cond_string)
15618 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15619
15620 if (tp->ignore_count)
15621 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15622
15623 if (tp->type != bp_dprintf && tp->commands)
15624 {
15625 struct gdb_exception exception;
15626
15627 fprintf_unfiltered (fp, " commands\n");
15628
15629 ui_out_redirect (current_uiout, fp);
15630 TRY
15631 {
15632 print_command_lines (current_uiout, tp->commands->commands, 2);
15633 }
15634 CATCH (ex, RETURN_MASK_ALL)
15635 {
15636 ui_out_redirect (current_uiout, NULL);
15637 throw_exception (ex);
15638 }
15639 END_CATCH
15640
15641 ui_out_redirect (current_uiout, NULL);
15642 fprintf_unfiltered (fp, " end\n");
15643 }
15644
15645 if (tp->enable_state == bp_disabled)
15646 fprintf_unfiltered (fp, "disable $bpnum\n");
15647
15648 /* If this is a multi-location breakpoint, check if the locations
15649 should be individually disabled. Watchpoint locations are
15650 special, and not user visible. */
15651 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15652 {
15653 struct bp_location *loc;
15654 int n = 1;
15655
15656 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15657 if (!loc->enabled)
15658 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15659 }
15660 }
15661
15662 if (extra_trace_bits && *default_collect)
15663 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15664
15665 if (from_tty)
15666 printf_filtered (_("Saved to file '%s'.\n"), filename);
15667 do_cleanups (cleanup);
15668 }
15669
15670 /* The `save breakpoints' command. */
15671
15672 static void
15673 save_breakpoints_command (char *args, int from_tty)
15674 {
15675 save_breakpoints (args, from_tty, NULL);
15676 }
15677
15678 /* The `save tracepoints' command. */
15679
15680 static void
15681 save_tracepoints_command (char *args, int from_tty)
15682 {
15683 save_breakpoints (args, from_tty, is_tracepoint);
15684 }
15685
15686 /* Create a vector of all tracepoints. */
15687
15688 VEC(breakpoint_p) *
15689 all_tracepoints (void)
15690 {
15691 VEC(breakpoint_p) *tp_vec = 0;
15692 struct breakpoint *tp;
15693
15694 ALL_TRACEPOINTS (tp)
15695 {
15696 VEC_safe_push (breakpoint_p, tp_vec, tp);
15697 }
15698
15699 return tp_vec;
15700 }
15701
15702 \f
15703 /* This help string is used to consolidate all the help string for specifying
15704 locations used by several commands. */
15705
15706 #define LOCATION_HELP_STRING \
15707 "Linespecs are colon-separated lists of location parameters, such as\n\
15708 source filename, function name, label name, and line number.\n\
15709 Example: To specify the start of a label named \"the_top\" in the\n\
15710 function \"fact\" in the file \"factorial.c\", use\n\
15711 \"factorial.c:fact:the_top\".\n\
15712 \n\
15713 Address locations begin with \"*\" and specify an exact address in the\n\
15714 program. Example: To specify the fourth byte past the start function\n\
15715 \"main\", use \"*main + 4\".\n\
15716 \n\
15717 Explicit locations are similar to linespecs but use an option/argument\n\
15718 syntax to specify location parameters.\n\
15719 Example: To specify the start of the label named \"the_top\" in the\n\
15720 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15721 -function fact -label the_top\".\n"
15722
15723 /* This help string is used for the break, hbreak, tbreak and thbreak
15724 commands. It is defined as a macro to prevent duplication.
15725 COMMAND should be a string constant containing the name of the
15726 command. */
15727
15728 #define BREAK_ARGS_HELP(command) \
15729 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15730 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15731 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15732 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15733 `-probe-dtrace' (for a DTrace probe).\n\
15734 LOCATION may be a linespec, address, or explicit location as described\n\
15735 below.\n\
15736 \n\
15737 With no LOCATION, uses current execution address of the selected\n\
15738 stack frame. This is useful for breaking on return to a stack frame.\n\
15739 \n\
15740 THREADNUM is the number from \"info threads\".\n\
15741 CONDITION is a boolean expression.\n\
15742 \n" LOCATION_HELP_STRING "\n\
15743 Multiple breakpoints at one place are permitted, and useful if their\n\
15744 conditions are different.\n\
15745 \n\
15746 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15747
15748 /* List of subcommands for "catch". */
15749 static struct cmd_list_element *catch_cmdlist;
15750
15751 /* List of subcommands for "tcatch". */
15752 static struct cmd_list_element *tcatch_cmdlist;
15753
15754 void
15755 add_catch_command (char *name, char *docstring,
15756 cmd_sfunc_ftype *sfunc,
15757 completer_ftype *completer,
15758 void *user_data_catch,
15759 void *user_data_tcatch)
15760 {
15761 struct cmd_list_element *command;
15762
15763 command = add_cmd (name, class_breakpoint, NULL, docstring,
15764 &catch_cmdlist);
15765 set_cmd_sfunc (command, sfunc);
15766 set_cmd_context (command, user_data_catch);
15767 set_cmd_completer (command, completer);
15768
15769 command = add_cmd (name, class_breakpoint, NULL, docstring,
15770 &tcatch_cmdlist);
15771 set_cmd_sfunc (command, sfunc);
15772 set_cmd_context (command, user_data_tcatch);
15773 set_cmd_completer (command, completer);
15774 }
15775
15776 static void
15777 save_command (char *arg, int from_tty)
15778 {
15779 printf_unfiltered (_("\"save\" must be followed by "
15780 "the name of a save subcommand.\n"));
15781 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15782 }
15783
15784 struct breakpoint *
15785 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15786 void *data)
15787 {
15788 struct breakpoint *b, *b_tmp;
15789
15790 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15791 {
15792 if ((*callback) (b, data))
15793 return b;
15794 }
15795
15796 return NULL;
15797 }
15798
15799 /* Zero if any of the breakpoint's locations could be a location where
15800 functions have been inlined, nonzero otherwise. */
15801
15802 static int
15803 is_non_inline_function (struct breakpoint *b)
15804 {
15805 /* The shared library event breakpoint is set on the address of a
15806 non-inline function. */
15807 if (b->type == bp_shlib_event)
15808 return 1;
15809
15810 return 0;
15811 }
15812
15813 /* Nonzero if the specified PC cannot be a location where functions
15814 have been inlined. */
15815
15816 int
15817 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15818 const struct target_waitstatus *ws)
15819 {
15820 struct breakpoint *b;
15821 struct bp_location *bl;
15822
15823 ALL_BREAKPOINTS (b)
15824 {
15825 if (!is_non_inline_function (b))
15826 continue;
15827
15828 for (bl = b->loc; bl != NULL; bl = bl->next)
15829 {
15830 if (!bl->shlib_disabled
15831 && bpstat_check_location (bl, aspace, pc, ws))
15832 return 1;
15833 }
15834 }
15835
15836 return 0;
15837 }
15838
15839 /* Remove any references to OBJFILE which is going to be freed. */
15840
15841 void
15842 breakpoint_free_objfile (struct objfile *objfile)
15843 {
15844 struct bp_location **locp, *loc;
15845
15846 ALL_BP_LOCATIONS (loc, locp)
15847 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15848 loc->symtab = NULL;
15849 }
15850
15851 void
15852 initialize_breakpoint_ops (void)
15853 {
15854 static int initialized = 0;
15855
15856 struct breakpoint_ops *ops;
15857
15858 if (initialized)
15859 return;
15860 initialized = 1;
15861
15862 /* The breakpoint_ops structure to be inherit by all kinds of
15863 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15864 internal and momentary breakpoints, etc.). */
15865 ops = &bkpt_base_breakpoint_ops;
15866 *ops = base_breakpoint_ops;
15867 ops->re_set = bkpt_re_set;
15868 ops->insert_location = bkpt_insert_location;
15869 ops->remove_location = bkpt_remove_location;
15870 ops->breakpoint_hit = bkpt_breakpoint_hit;
15871 ops->create_sals_from_location = bkpt_create_sals_from_location;
15872 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15873 ops->decode_location = bkpt_decode_location;
15874
15875 /* The breakpoint_ops structure to be used in regular breakpoints. */
15876 ops = &bkpt_breakpoint_ops;
15877 *ops = bkpt_base_breakpoint_ops;
15878 ops->re_set = bkpt_re_set;
15879 ops->resources_needed = bkpt_resources_needed;
15880 ops->print_it = bkpt_print_it;
15881 ops->print_mention = bkpt_print_mention;
15882 ops->print_recreate = bkpt_print_recreate;
15883
15884 /* Ranged breakpoints. */
15885 ops = &ranged_breakpoint_ops;
15886 *ops = bkpt_breakpoint_ops;
15887 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15888 ops->resources_needed = resources_needed_ranged_breakpoint;
15889 ops->print_it = print_it_ranged_breakpoint;
15890 ops->print_one = print_one_ranged_breakpoint;
15891 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15892 ops->print_mention = print_mention_ranged_breakpoint;
15893 ops->print_recreate = print_recreate_ranged_breakpoint;
15894
15895 /* Internal breakpoints. */
15896 ops = &internal_breakpoint_ops;
15897 *ops = bkpt_base_breakpoint_ops;
15898 ops->re_set = internal_bkpt_re_set;
15899 ops->check_status = internal_bkpt_check_status;
15900 ops->print_it = internal_bkpt_print_it;
15901 ops->print_mention = internal_bkpt_print_mention;
15902
15903 /* Momentary breakpoints. */
15904 ops = &momentary_breakpoint_ops;
15905 *ops = bkpt_base_breakpoint_ops;
15906 ops->re_set = momentary_bkpt_re_set;
15907 ops->check_status = momentary_bkpt_check_status;
15908 ops->print_it = momentary_bkpt_print_it;
15909 ops->print_mention = momentary_bkpt_print_mention;
15910
15911 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15912 ops = &longjmp_breakpoint_ops;
15913 *ops = momentary_breakpoint_ops;
15914 ops->dtor = longjmp_bkpt_dtor;
15915
15916 /* Probe breakpoints. */
15917 ops = &bkpt_probe_breakpoint_ops;
15918 *ops = bkpt_breakpoint_ops;
15919 ops->insert_location = bkpt_probe_insert_location;
15920 ops->remove_location = bkpt_probe_remove_location;
15921 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15922 ops->decode_location = bkpt_probe_decode_location;
15923
15924 /* Watchpoints. */
15925 ops = &watchpoint_breakpoint_ops;
15926 *ops = base_breakpoint_ops;
15927 ops->dtor = dtor_watchpoint;
15928 ops->re_set = re_set_watchpoint;
15929 ops->insert_location = insert_watchpoint;
15930 ops->remove_location = remove_watchpoint;
15931 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15932 ops->check_status = check_status_watchpoint;
15933 ops->resources_needed = resources_needed_watchpoint;
15934 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15935 ops->print_it = print_it_watchpoint;
15936 ops->print_mention = print_mention_watchpoint;
15937 ops->print_recreate = print_recreate_watchpoint;
15938 ops->explains_signal = explains_signal_watchpoint;
15939
15940 /* Masked watchpoints. */
15941 ops = &masked_watchpoint_breakpoint_ops;
15942 *ops = watchpoint_breakpoint_ops;
15943 ops->insert_location = insert_masked_watchpoint;
15944 ops->remove_location = remove_masked_watchpoint;
15945 ops->resources_needed = resources_needed_masked_watchpoint;
15946 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15947 ops->print_it = print_it_masked_watchpoint;
15948 ops->print_one_detail = print_one_detail_masked_watchpoint;
15949 ops->print_mention = print_mention_masked_watchpoint;
15950 ops->print_recreate = print_recreate_masked_watchpoint;
15951
15952 /* Tracepoints. */
15953 ops = &tracepoint_breakpoint_ops;
15954 *ops = base_breakpoint_ops;
15955 ops->re_set = tracepoint_re_set;
15956 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15957 ops->print_one_detail = tracepoint_print_one_detail;
15958 ops->print_mention = tracepoint_print_mention;
15959 ops->print_recreate = tracepoint_print_recreate;
15960 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15961 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15962 ops->decode_location = tracepoint_decode_location;
15963
15964 /* Probe tracepoints. */
15965 ops = &tracepoint_probe_breakpoint_ops;
15966 *ops = tracepoint_breakpoint_ops;
15967 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15968 ops->decode_location = tracepoint_probe_decode_location;
15969
15970 /* Static tracepoints with marker (`-m'). */
15971 ops = &strace_marker_breakpoint_ops;
15972 *ops = tracepoint_breakpoint_ops;
15973 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15974 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15975 ops->decode_location = strace_marker_decode_location;
15976
15977 /* Fork catchpoints. */
15978 ops = &catch_fork_breakpoint_ops;
15979 *ops = base_breakpoint_ops;
15980 ops->insert_location = insert_catch_fork;
15981 ops->remove_location = remove_catch_fork;
15982 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15983 ops->print_it = print_it_catch_fork;
15984 ops->print_one = print_one_catch_fork;
15985 ops->print_mention = print_mention_catch_fork;
15986 ops->print_recreate = print_recreate_catch_fork;
15987
15988 /* Vfork catchpoints. */
15989 ops = &catch_vfork_breakpoint_ops;
15990 *ops = base_breakpoint_ops;
15991 ops->insert_location = insert_catch_vfork;
15992 ops->remove_location = remove_catch_vfork;
15993 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15994 ops->print_it = print_it_catch_vfork;
15995 ops->print_one = print_one_catch_vfork;
15996 ops->print_mention = print_mention_catch_vfork;
15997 ops->print_recreate = print_recreate_catch_vfork;
15998
15999 /* Exec catchpoints. */
16000 ops = &catch_exec_breakpoint_ops;
16001 *ops = base_breakpoint_ops;
16002 ops->dtor = dtor_catch_exec;
16003 ops->insert_location = insert_catch_exec;
16004 ops->remove_location = remove_catch_exec;
16005 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16006 ops->print_it = print_it_catch_exec;
16007 ops->print_one = print_one_catch_exec;
16008 ops->print_mention = print_mention_catch_exec;
16009 ops->print_recreate = print_recreate_catch_exec;
16010
16011 /* Solib-related catchpoints. */
16012 ops = &catch_solib_breakpoint_ops;
16013 *ops = base_breakpoint_ops;
16014 ops->dtor = dtor_catch_solib;
16015 ops->insert_location = insert_catch_solib;
16016 ops->remove_location = remove_catch_solib;
16017 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16018 ops->check_status = check_status_catch_solib;
16019 ops->print_it = print_it_catch_solib;
16020 ops->print_one = print_one_catch_solib;
16021 ops->print_mention = print_mention_catch_solib;
16022 ops->print_recreate = print_recreate_catch_solib;
16023
16024 ops = &dprintf_breakpoint_ops;
16025 *ops = bkpt_base_breakpoint_ops;
16026 ops->re_set = dprintf_re_set;
16027 ops->resources_needed = bkpt_resources_needed;
16028 ops->print_it = bkpt_print_it;
16029 ops->print_mention = bkpt_print_mention;
16030 ops->print_recreate = dprintf_print_recreate;
16031 ops->after_condition_true = dprintf_after_condition_true;
16032 ops->breakpoint_hit = dprintf_breakpoint_hit;
16033 }
16034
16035 /* Chain containing all defined "enable breakpoint" subcommands. */
16036
16037 static struct cmd_list_element *enablebreaklist = NULL;
16038
16039 void
16040 _initialize_breakpoint (void)
16041 {
16042 struct cmd_list_element *c;
16043
16044 initialize_breakpoint_ops ();
16045
16046 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16047 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16048 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16049
16050 breakpoint_objfile_key
16051 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16052
16053 breakpoint_chain = 0;
16054 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16055 before a breakpoint is set. */
16056 breakpoint_count = 0;
16057
16058 tracepoint_count = 0;
16059
16060 add_com ("ignore", class_breakpoint, ignore_command, _("\
16061 Set ignore-count of breakpoint number N to COUNT.\n\
16062 Usage is `ignore N COUNT'."));
16063
16064 add_com ("commands", class_breakpoint, commands_command, _("\
16065 Set commands to be executed when a breakpoint is hit.\n\
16066 Give breakpoint number as argument after \"commands\".\n\
16067 With no argument, the targeted breakpoint is the last one set.\n\
16068 The commands themselves follow starting on the next line.\n\
16069 Type a line containing \"end\" to indicate the end of them.\n\
16070 Give \"silent\" as the first line to make the breakpoint silent;\n\
16071 then no output is printed when it is hit, except what the commands print."));
16072
16073 c = add_com ("condition", class_breakpoint, condition_command, _("\
16074 Specify breakpoint number N to break only if COND is true.\n\
16075 Usage is `condition N COND', where N is an integer and COND is an\n\
16076 expression to be evaluated whenever breakpoint N is reached."));
16077 set_cmd_completer (c, condition_completer);
16078
16079 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16080 Set a temporary breakpoint.\n\
16081 Like \"break\" except the breakpoint is only temporary,\n\
16082 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16083 by using \"enable delete\" on the breakpoint number.\n\
16084 \n"
16085 BREAK_ARGS_HELP ("tbreak")));
16086 set_cmd_completer (c, location_completer);
16087
16088 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16089 Set a hardware assisted breakpoint.\n\
16090 Like \"break\" except the breakpoint requires hardware support,\n\
16091 some target hardware may not have this support.\n\
16092 \n"
16093 BREAK_ARGS_HELP ("hbreak")));
16094 set_cmd_completer (c, location_completer);
16095
16096 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16097 Set a temporary hardware assisted breakpoint.\n\
16098 Like \"hbreak\" except the breakpoint is only temporary,\n\
16099 so it will be deleted when hit.\n\
16100 \n"
16101 BREAK_ARGS_HELP ("thbreak")));
16102 set_cmd_completer (c, location_completer);
16103
16104 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16105 Enable some breakpoints.\n\
16106 Give breakpoint numbers (separated by spaces) as arguments.\n\
16107 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16108 This is used to cancel the effect of the \"disable\" command.\n\
16109 With a subcommand you can enable temporarily."),
16110 &enablelist, "enable ", 1, &cmdlist);
16111
16112 add_com_alias ("en", "enable", class_breakpoint, 1);
16113
16114 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16115 Enable some breakpoints.\n\
16116 Give breakpoint numbers (separated by spaces) as arguments.\n\
16117 This is used to cancel the effect of the \"disable\" command.\n\
16118 May be abbreviated to simply \"enable\".\n"),
16119 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16120
16121 add_cmd ("once", no_class, enable_once_command, _("\
16122 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16123 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16124 &enablebreaklist);
16125
16126 add_cmd ("delete", no_class, enable_delete_command, _("\
16127 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16128 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16129 &enablebreaklist);
16130
16131 add_cmd ("count", no_class, enable_count_command, _("\
16132 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16133 If a breakpoint is hit while enabled in this fashion,\n\
16134 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16135 &enablebreaklist);
16136
16137 add_cmd ("delete", no_class, enable_delete_command, _("\
16138 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16139 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16140 &enablelist);
16141
16142 add_cmd ("once", no_class, enable_once_command, _("\
16143 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16144 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16145 &enablelist);
16146
16147 add_cmd ("count", no_class, enable_count_command, _("\
16148 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16149 If a breakpoint is hit while enabled in this fashion,\n\
16150 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16151 &enablelist);
16152
16153 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16154 Disable some breakpoints.\n\
16155 Arguments are breakpoint numbers with spaces in between.\n\
16156 To disable all breakpoints, give no argument.\n\
16157 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16158 &disablelist, "disable ", 1, &cmdlist);
16159 add_com_alias ("dis", "disable", class_breakpoint, 1);
16160 add_com_alias ("disa", "disable", class_breakpoint, 1);
16161
16162 add_cmd ("breakpoints", class_alias, disable_command, _("\
16163 Disable some breakpoints.\n\
16164 Arguments are breakpoint numbers with spaces in between.\n\
16165 To disable all breakpoints, give no argument.\n\
16166 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16167 This command may be abbreviated \"disable\"."),
16168 &disablelist);
16169
16170 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16171 Delete some breakpoints or auto-display expressions.\n\
16172 Arguments are breakpoint numbers with spaces in between.\n\
16173 To delete all breakpoints, give no argument.\n\
16174 \n\
16175 Also a prefix command for deletion of other GDB objects.\n\
16176 The \"unset\" command is also an alias for \"delete\"."),
16177 &deletelist, "delete ", 1, &cmdlist);
16178 add_com_alias ("d", "delete", class_breakpoint, 1);
16179 add_com_alias ("del", "delete", class_breakpoint, 1);
16180
16181 add_cmd ("breakpoints", class_alias, delete_command, _("\
16182 Delete some breakpoints or auto-display expressions.\n\
16183 Arguments are breakpoint numbers with spaces in between.\n\
16184 To delete all breakpoints, give no argument.\n\
16185 This command may be abbreviated \"delete\"."),
16186 &deletelist);
16187
16188 add_com ("clear", class_breakpoint, clear_command, _("\
16189 Clear breakpoint at specified location.\n\
16190 Argument may be a linespec, explicit, or address location as described below.\n\
16191 \n\
16192 With no argument, clears all breakpoints in the line that the selected frame\n\
16193 is executing in.\n"
16194 "\n" LOCATION_HELP_STRING "\n\
16195 See also the \"delete\" command which clears breakpoints by number."));
16196 add_com_alias ("cl", "clear", class_breakpoint, 1);
16197
16198 c = add_com ("break", class_breakpoint, break_command, _("\
16199 Set breakpoint at specified location.\n"
16200 BREAK_ARGS_HELP ("break")));
16201 set_cmd_completer (c, location_completer);
16202
16203 add_com_alias ("b", "break", class_run, 1);
16204 add_com_alias ("br", "break", class_run, 1);
16205 add_com_alias ("bre", "break", class_run, 1);
16206 add_com_alias ("brea", "break", class_run, 1);
16207
16208 if (dbx_commands)
16209 {
16210 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16211 Break in function/address or break at a line in the current file."),
16212 &stoplist, "stop ", 1, &cmdlist);
16213 add_cmd ("in", class_breakpoint, stopin_command,
16214 _("Break in function or address."), &stoplist);
16215 add_cmd ("at", class_breakpoint, stopat_command,
16216 _("Break at a line in the current file."), &stoplist);
16217 add_com ("status", class_info, breakpoints_info, _("\
16218 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16219 The \"Type\" column indicates one of:\n\
16220 \tbreakpoint - normal breakpoint\n\
16221 \twatchpoint - watchpoint\n\
16222 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16223 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16224 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16225 address and file/line number respectively.\n\
16226 \n\
16227 Convenience variable \"$_\" and default examine address for \"x\"\n\
16228 are set to the address of the last breakpoint listed unless the command\n\
16229 is prefixed with \"server \".\n\n\
16230 Convenience variable \"$bpnum\" contains the number of the last\n\
16231 breakpoint set."));
16232 }
16233
16234 add_info ("breakpoints", breakpoints_info, _("\
16235 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16236 The \"Type\" column indicates one of:\n\
16237 \tbreakpoint - normal breakpoint\n\
16238 \twatchpoint - watchpoint\n\
16239 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16240 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16241 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16242 address and file/line number respectively.\n\
16243 \n\
16244 Convenience variable \"$_\" and default examine address for \"x\"\n\
16245 are set to the address of the last breakpoint listed unless the command\n\
16246 is prefixed with \"server \".\n\n\
16247 Convenience variable \"$bpnum\" contains the number of the last\n\
16248 breakpoint set."));
16249
16250 add_info_alias ("b", "breakpoints", 1);
16251
16252 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16253 Status of all breakpoints, or breakpoint number NUMBER.\n\
16254 The \"Type\" column indicates one of:\n\
16255 \tbreakpoint - normal breakpoint\n\
16256 \twatchpoint - watchpoint\n\
16257 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16258 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16259 \tuntil - internal breakpoint used by the \"until\" command\n\
16260 \tfinish - internal breakpoint used by the \"finish\" command\n\
16261 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16262 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16263 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16264 address and file/line number respectively.\n\
16265 \n\
16266 Convenience variable \"$_\" and default examine address for \"x\"\n\
16267 are set to the address of the last breakpoint listed unless the command\n\
16268 is prefixed with \"server \".\n\n\
16269 Convenience variable \"$bpnum\" contains the number of the last\n\
16270 breakpoint set."),
16271 &maintenanceinfolist);
16272
16273 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16274 Set catchpoints to catch events."),
16275 &catch_cmdlist, "catch ",
16276 0/*allow-unknown*/, &cmdlist);
16277
16278 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16279 Set temporary catchpoints to catch events."),
16280 &tcatch_cmdlist, "tcatch ",
16281 0/*allow-unknown*/, &cmdlist);
16282
16283 add_catch_command ("fork", _("Catch calls to fork."),
16284 catch_fork_command_1,
16285 NULL,
16286 (void *) (uintptr_t) catch_fork_permanent,
16287 (void *) (uintptr_t) catch_fork_temporary);
16288 add_catch_command ("vfork", _("Catch calls to vfork."),
16289 catch_fork_command_1,
16290 NULL,
16291 (void *) (uintptr_t) catch_vfork_permanent,
16292 (void *) (uintptr_t) catch_vfork_temporary);
16293 add_catch_command ("exec", _("Catch calls to exec."),
16294 catch_exec_command_1,
16295 NULL,
16296 CATCH_PERMANENT,
16297 CATCH_TEMPORARY);
16298 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16299 Usage: catch load [REGEX]\n\
16300 If REGEX is given, only stop for libraries matching the regular expression."),
16301 catch_load_command_1,
16302 NULL,
16303 CATCH_PERMANENT,
16304 CATCH_TEMPORARY);
16305 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16306 Usage: catch unload [REGEX]\n\
16307 If REGEX is given, only stop for libraries matching the regular expression."),
16308 catch_unload_command_1,
16309 NULL,
16310 CATCH_PERMANENT,
16311 CATCH_TEMPORARY);
16312
16313 c = add_com ("watch", class_breakpoint, watch_command, _("\
16314 Set a watchpoint for an expression.\n\
16315 Usage: watch [-l|-location] EXPRESSION\n\
16316 A watchpoint stops execution of your program whenever the value of\n\
16317 an expression changes.\n\
16318 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16319 the memory to which it refers."));
16320 set_cmd_completer (c, expression_completer);
16321
16322 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16323 Set a read watchpoint for an expression.\n\
16324 Usage: rwatch [-l|-location] EXPRESSION\n\
16325 A watchpoint stops execution of your program whenever the value of\n\
16326 an expression is read.\n\
16327 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16328 the memory to which it refers."));
16329 set_cmd_completer (c, expression_completer);
16330
16331 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16332 Set a watchpoint for an expression.\n\
16333 Usage: awatch [-l|-location] EXPRESSION\n\
16334 A watchpoint stops execution of your program whenever the value of\n\
16335 an expression is either read or written.\n\
16336 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16337 the memory to which it refers."));
16338 set_cmd_completer (c, expression_completer);
16339
16340 add_info ("watchpoints", watchpoints_info, _("\
16341 Status of specified watchpoints (all watchpoints if no argument)."));
16342
16343 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16344 respond to changes - contrary to the description. */
16345 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16346 &can_use_hw_watchpoints, _("\
16347 Set debugger's willingness to use watchpoint hardware."), _("\
16348 Show debugger's willingness to use watchpoint hardware."), _("\
16349 If zero, gdb will not use hardware for new watchpoints, even if\n\
16350 such is available. (However, any hardware watchpoints that were\n\
16351 created before setting this to nonzero, will continue to use watchpoint\n\
16352 hardware.)"),
16353 NULL,
16354 show_can_use_hw_watchpoints,
16355 &setlist, &showlist);
16356
16357 can_use_hw_watchpoints = 1;
16358
16359 /* Tracepoint manipulation commands. */
16360
16361 c = add_com ("trace", class_breakpoint, trace_command, _("\
16362 Set a tracepoint at specified location.\n\
16363 \n"
16364 BREAK_ARGS_HELP ("trace") "\n\
16365 Do \"help tracepoints\" for info on other tracepoint commands."));
16366 set_cmd_completer (c, location_completer);
16367
16368 add_com_alias ("tp", "trace", class_alias, 0);
16369 add_com_alias ("tr", "trace", class_alias, 1);
16370 add_com_alias ("tra", "trace", class_alias, 1);
16371 add_com_alias ("trac", "trace", class_alias, 1);
16372
16373 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16374 Set a fast tracepoint at specified location.\n\
16375 \n"
16376 BREAK_ARGS_HELP ("ftrace") "\n\
16377 Do \"help tracepoints\" for info on other tracepoint commands."));
16378 set_cmd_completer (c, location_completer);
16379
16380 c = add_com ("strace", class_breakpoint, strace_command, _("\
16381 Set a static tracepoint at location or marker.\n\
16382 \n\
16383 strace [LOCATION] [if CONDITION]\n\
16384 LOCATION may be a linespec, explicit, or address location (described below) \n\
16385 or -m MARKER_ID.\n\n\
16386 If a marker id is specified, probe the marker with that name. With\n\
16387 no LOCATION, uses current execution address of the selected stack frame.\n\
16388 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16389 This collects arbitrary user data passed in the probe point call to the\n\
16390 tracing library. You can inspect it when analyzing the trace buffer,\n\
16391 by printing the $_sdata variable like any other convenience variable.\n\
16392 \n\
16393 CONDITION is a boolean expression.\n\
16394 \n" LOCATION_HELP_STRING "\n\
16395 Multiple tracepoints at one place are permitted, and useful if their\n\
16396 conditions are different.\n\
16397 \n\
16398 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16399 Do \"help tracepoints\" for info on other tracepoint commands."));
16400 set_cmd_completer (c, location_completer);
16401
16402 add_info ("tracepoints", tracepoints_info, _("\
16403 Status of specified tracepoints (all tracepoints if no argument).\n\
16404 Convenience variable \"$tpnum\" contains the number of the\n\
16405 last tracepoint set."));
16406
16407 add_info_alias ("tp", "tracepoints", 1);
16408
16409 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16410 Delete specified tracepoints.\n\
16411 Arguments are tracepoint numbers, separated by spaces.\n\
16412 No argument means delete all tracepoints."),
16413 &deletelist);
16414 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16415
16416 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16417 Disable specified tracepoints.\n\
16418 Arguments are tracepoint numbers, separated by spaces.\n\
16419 No argument means disable all tracepoints."),
16420 &disablelist);
16421 deprecate_cmd (c, "disable");
16422
16423 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16424 Enable specified tracepoints.\n\
16425 Arguments are tracepoint numbers, separated by spaces.\n\
16426 No argument means enable all tracepoints."),
16427 &enablelist);
16428 deprecate_cmd (c, "enable");
16429
16430 add_com ("passcount", class_trace, trace_pass_command, _("\
16431 Set the passcount for a tracepoint.\n\
16432 The trace will end when the tracepoint has been passed 'count' times.\n\
16433 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16434 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16435
16436 add_prefix_cmd ("save", class_breakpoint, save_command,
16437 _("Save breakpoint definitions as a script."),
16438 &save_cmdlist, "save ",
16439 0/*allow-unknown*/, &cmdlist);
16440
16441 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16442 Save current breakpoint definitions as a script.\n\
16443 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16444 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16445 session to restore them."),
16446 &save_cmdlist);
16447 set_cmd_completer (c, filename_completer);
16448
16449 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16450 Save current tracepoint definitions as a script.\n\
16451 Use the 'source' command in another debug session to restore them."),
16452 &save_cmdlist);
16453 set_cmd_completer (c, filename_completer);
16454
16455 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16456 deprecate_cmd (c, "save tracepoints");
16457
16458 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16459 Breakpoint specific settings\n\
16460 Configure various breakpoint-specific variables such as\n\
16461 pending breakpoint behavior"),
16462 &breakpoint_set_cmdlist, "set breakpoint ",
16463 0/*allow-unknown*/, &setlist);
16464 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16465 Breakpoint specific settings\n\
16466 Configure various breakpoint-specific variables such as\n\
16467 pending breakpoint behavior"),
16468 &breakpoint_show_cmdlist, "show breakpoint ",
16469 0/*allow-unknown*/, &showlist);
16470
16471 add_setshow_auto_boolean_cmd ("pending", no_class,
16472 &pending_break_support, _("\
16473 Set debugger's behavior regarding pending breakpoints."), _("\
16474 Show debugger's behavior regarding pending breakpoints."), _("\
16475 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16476 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16477 an error. If auto, an unrecognized breakpoint location results in a\n\
16478 user-query to see if a pending breakpoint should be created."),
16479 NULL,
16480 show_pending_break_support,
16481 &breakpoint_set_cmdlist,
16482 &breakpoint_show_cmdlist);
16483
16484 pending_break_support = AUTO_BOOLEAN_AUTO;
16485
16486 add_setshow_boolean_cmd ("auto-hw", no_class,
16487 &automatic_hardware_breakpoints, _("\
16488 Set automatic usage of hardware breakpoints."), _("\
16489 Show automatic usage of hardware breakpoints."), _("\
16490 If set, the debugger will automatically use hardware breakpoints for\n\
16491 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16492 a warning will be emitted for such breakpoints."),
16493 NULL,
16494 show_automatic_hardware_breakpoints,
16495 &breakpoint_set_cmdlist,
16496 &breakpoint_show_cmdlist);
16497
16498 add_setshow_boolean_cmd ("always-inserted", class_support,
16499 &always_inserted_mode, _("\
16500 Set mode for inserting breakpoints."), _("\
16501 Show mode for inserting breakpoints."), _("\
16502 When this mode is on, breakpoints are inserted immediately as soon as\n\
16503 they're created, kept inserted even when execution stops, and removed\n\
16504 only when the user deletes them. When this mode is off (the default),\n\
16505 breakpoints are inserted only when execution continues, and removed\n\
16506 when execution stops."),
16507 NULL,
16508 &show_always_inserted_mode,
16509 &breakpoint_set_cmdlist,
16510 &breakpoint_show_cmdlist);
16511
16512 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16513 condition_evaluation_enums,
16514 &condition_evaluation_mode_1, _("\
16515 Set mode of breakpoint condition evaluation."), _("\
16516 Show mode of breakpoint condition evaluation."), _("\
16517 When this is set to \"host\", breakpoint conditions will be\n\
16518 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16519 breakpoint conditions will be downloaded to the target (if the target\n\
16520 supports such feature) and conditions will be evaluated on the target's side.\n\
16521 If this is set to \"auto\" (default), this will be automatically set to\n\
16522 \"target\" if it supports condition evaluation, otherwise it will\n\
16523 be set to \"gdb\""),
16524 &set_condition_evaluation_mode,
16525 &show_condition_evaluation_mode,
16526 &breakpoint_set_cmdlist,
16527 &breakpoint_show_cmdlist);
16528
16529 add_com ("break-range", class_breakpoint, break_range_command, _("\
16530 Set a breakpoint for an address range.\n\
16531 break-range START-LOCATION, END-LOCATION\n\
16532 where START-LOCATION and END-LOCATION can be one of the following:\n\
16533 LINENUM, for that line in the current file,\n\
16534 FILE:LINENUM, for that line in that file,\n\
16535 +OFFSET, for that number of lines after the current line\n\
16536 or the start of the range\n\
16537 FUNCTION, for the first line in that function,\n\
16538 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16539 *ADDRESS, for the instruction at that address.\n\
16540 \n\
16541 The breakpoint will stop execution of the inferior whenever it executes\n\
16542 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16543 range (including START-LOCATION and END-LOCATION)."));
16544
16545 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16546 Set a dynamic printf at specified location.\n\
16547 dprintf location,format string,arg1,arg2,...\n\
16548 location may be a linespec, explicit, or address location.\n"
16549 "\n" LOCATION_HELP_STRING));
16550 set_cmd_completer (c, location_completer);
16551
16552 add_setshow_enum_cmd ("dprintf-style", class_support,
16553 dprintf_style_enums, &dprintf_style, _("\
16554 Set the style of usage for dynamic printf."), _("\
16555 Show the style of usage for dynamic printf."), _("\
16556 This setting chooses how GDB will do a dynamic printf.\n\
16557 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16558 console, as with the \"printf\" command.\n\
16559 If the value is \"call\", the print is done by calling a function in your\n\
16560 program; by default printf(), but you can choose a different function or\n\
16561 output stream by setting dprintf-function and dprintf-channel."),
16562 update_dprintf_commands, NULL,
16563 &setlist, &showlist);
16564
16565 dprintf_function = xstrdup ("printf");
16566 add_setshow_string_cmd ("dprintf-function", class_support,
16567 &dprintf_function, _("\
16568 Set the function to use for dynamic printf"), _("\
16569 Show the function to use for dynamic printf"), NULL,
16570 update_dprintf_commands, NULL,
16571 &setlist, &showlist);
16572
16573 dprintf_channel = xstrdup ("");
16574 add_setshow_string_cmd ("dprintf-channel", class_support,
16575 &dprintf_channel, _("\
16576 Set the channel to use for dynamic printf"), _("\
16577 Show the channel to use for dynamic printf"), NULL,
16578 update_dprintf_commands, NULL,
16579 &setlist, &showlist);
16580
16581 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16582 &disconnected_dprintf, _("\
16583 Set whether dprintf continues after GDB disconnects."), _("\
16584 Show whether dprintf continues after GDB disconnects."), _("\
16585 Use this to let dprintf commands continue to hit and produce output\n\
16586 even if GDB disconnects or detaches from the target."),
16587 NULL,
16588 NULL,
16589 &setlist, &showlist);
16590
16591 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16592 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16593 (target agent only) This is useful for formatted output in user-defined commands."));
16594
16595 automatic_hardware_breakpoints = 1;
16596
16597 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16598 observer_attach_thread_exit (remove_threaded_breakpoints);
16599 }
This page took 0.406899 seconds and 4 git commands to generate.