]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/breakpoint.c
2011-03-23 Kai Tietz <ktietz@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "arch-utils.h"
24 #include <ctype.h>
25 #include "hashtab.h"
26 #include "symtab.h"
27 #include "frame.h"
28 #include "breakpoint.h"
29 #include "tracepoint.h"
30 #include "gdbtypes.h"
31 #include "expression.h"
32 #include "gdbcore.h"
33 #include "gdbcmd.h"
34 #include "value.h"
35 #include "command.h"
36 #include "inferior.h"
37 #include "gdbthread.h"
38 #include "target.h"
39 #include "language.h"
40 #include "gdb_string.h"
41 #include "demangle.h"
42 #include "filenames.h"
43 #include "annotate.h"
44 #include "symfile.h"
45 #include "objfiles.h"
46 #include "source.h"
47 #include "linespec.h"
48 #include "completer.h"
49 #include "gdb.h"
50 #include "ui-out.h"
51 #include "cli/cli-script.h"
52 #include "gdb_assert.h"
53 #include "block.h"
54 #include "solib.h"
55 #include "solist.h"
56 #include "observer.h"
57 #include "exceptions.h"
58 #include "memattr.h"
59 #include "ada-lang.h"
60 #include "top.h"
61 #include "wrapper.h"
62 #include "valprint.h"
63 #include "jit.h"
64 #include "xml-syscall.h"
65 #include "parser-defs.h"
66 #include "cli/cli-utils.h"
67
68 /* readline include files */
69 #include "readline/readline.h"
70 #include "readline/history.h"
71
72 /* readline defines this. */
73 #undef savestring
74
75 #include "mi/mi-common.h"
76 #include "python/python.h"
77
78 /* Arguments to pass as context to some catch command handlers. */
79 #define CATCH_PERMANENT ((void *) (uintptr_t) 0)
80 #define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
81
82 /* Prototypes for local functions. */
83
84 static void enable_delete_command (char *, int);
85
86 static void enable_once_command (char *, int);
87
88 static void disable_command (char *, int);
89
90 static void enable_command (char *, int);
91
92 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
93 void *),
94 void *);
95
96 static void ignore_command (char *, int);
97
98 static int breakpoint_re_set_one (void *);
99
100 static void clear_command (char *, int);
101
102 static void catch_command (char *, int);
103
104 static int can_use_hardware_watchpoint (struct value *, int);
105
106 static void break_command_1 (char *, int, int);
107
108 static void mention (struct breakpoint *);
109
110 /* This function is used in gdbtk sources and thus can not be made
111 static. */
112 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
113 struct symtab_and_line,
114 enum bptype);
115
116 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
117
118 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
119 CORE_ADDR bpaddr,
120 enum bptype bptype);
121
122 static void describe_other_breakpoints (struct gdbarch *,
123 struct program_space *, CORE_ADDR,
124 struct obj_section *, int);
125
126 static int breakpoint_address_match (struct address_space *aspace1,
127 CORE_ADDR addr1,
128 struct address_space *aspace2,
129 CORE_ADDR addr2);
130
131 static int watchpoint_locations_match (struct bp_location *loc1,
132 struct bp_location *loc2);
133
134 static void breakpoints_info (char *, int);
135
136 static void watchpoints_info (char *, int);
137
138 static int breakpoint_1 (char *, int,
139 int (*) (const struct breakpoint *));
140
141 static int breakpoint_cond_eval (void *);
142
143 static void cleanup_executing_breakpoints (void *);
144
145 static void commands_command (char *, int);
146
147 static void condition_command (char *, int);
148
149 typedef enum
150 {
151 mark_inserted,
152 mark_uninserted
153 }
154 insertion_state_t;
155
156 static int remove_breakpoint (struct bp_location *, insertion_state_t);
157 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
158
159 static enum print_stop_action print_it_typical (bpstat);
160
161 static enum print_stop_action print_bp_stop_message (bpstat bs);
162
163 static int watchpoint_check (void *);
164
165 static void maintenance_info_breakpoints (char *, int);
166
167 static int hw_breakpoint_used_count (void);
168
169 static int hw_watchpoint_used_count (enum bptype, int *);
170
171 static void hbreak_command (char *, int);
172
173 static void thbreak_command (char *, int);
174
175 static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
176
177 static void stop_command (char *arg, int from_tty);
178
179 static void stopin_command (char *arg, int from_tty);
180
181 static void stopat_command (char *arg, int from_tty);
182
183 static char *ep_parse_optional_if_clause (char **arg);
184
185 static void catch_exception_command_1 (enum exception_event_kind ex_event,
186 char *arg, int tempflag, int from_tty);
187
188 static void tcatch_command (char *arg, int from_tty);
189
190 static void detach_single_step_breakpoints (void);
191
192 static int single_step_breakpoint_inserted_here_p (struct address_space *,
193 CORE_ADDR pc);
194
195 static void free_bp_location (struct bp_location *loc);
196 static void incref_bp_location (struct bp_location *loc);
197 static void decref_bp_location (struct bp_location **loc);
198
199 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
200
201 static void update_global_location_list (int);
202
203 static void update_global_location_list_nothrow (int);
204
205 static int is_hardware_watchpoint (const struct breakpoint *bpt);
206
207 static int is_watchpoint (const struct breakpoint *bpt);
208
209 static void insert_breakpoint_locations (void);
210
211 static int syscall_catchpoint_p (struct breakpoint *b);
212
213 static void tracepoints_info (char *, int);
214
215 static void delete_trace_command (char *, int);
216
217 static void enable_trace_command (char *, int);
218
219 static void disable_trace_command (char *, int);
220
221 static void trace_pass_command (char *, int);
222
223 /* Assuming we're creating a static tracepoint, does S look like a
224 static tracepoint marker spec ("-m MARKER_ID")? */
225 #define is_marker_spec(s) \
226 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
227
228 /* A reference-counted struct command_line. This lets multiple
229 breakpoints share a single command list. */
230 struct counted_command_line
231 {
232 /* The reference count. */
233 int refc;
234
235 /* The command list. */
236 struct command_line *commands;
237 };
238
239 struct command_line *
240 breakpoint_commands (struct breakpoint *b)
241 {
242 return b->commands ? b->commands->commands : NULL;
243 }
244
245 /* Flag indicating that a command has proceeded the inferior past the
246 current breakpoint. */
247
248 static int breakpoint_proceeded;
249
250 static const char *
251 bpdisp_text (enum bpdisp disp)
252 {
253 /* NOTE: the following values are a part of MI protocol and
254 represent values of 'disp' field returned when inferior stops at
255 a breakpoint. */
256 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
257
258 return bpdisps[(int) disp];
259 }
260
261 /* Prototypes for exported functions. */
262 /* If FALSE, gdb will not use hardware support for watchpoints, even
263 if such is available. */
264 static int can_use_hw_watchpoints;
265
266 static void
267 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
268 struct cmd_list_element *c,
269 const char *value)
270 {
271 fprintf_filtered (file,
272 _("Debugger's willingness to use "
273 "watchpoint hardware is %s.\n"),
274 value);
275 }
276
277 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
278 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
279 for unrecognized breakpoint locations.
280 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
281 static enum auto_boolean pending_break_support;
282 static void
283 show_pending_break_support (struct ui_file *file, int from_tty,
284 struct cmd_list_element *c,
285 const char *value)
286 {
287 fprintf_filtered (file,
288 _("Debugger's behavior regarding "
289 "pending breakpoints is %s.\n"),
290 value);
291 }
292
293 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
294 set with "break" but falling in read-only memory.
295 If 0, gdb will warn about such breakpoints, but won't automatically
296 use hardware breakpoints. */
297 static int automatic_hardware_breakpoints;
298 static void
299 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
300 struct cmd_list_element *c,
301 const char *value)
302 {
303 fprintf_filtered (file,
304 _("Automatic usage of hardware breakpoints is %s.\n"),
305 value);
306 }
307
308 /* If on, gdb will keep breakpoints inserted even as inferior is
309 stopped, and immediately insert any new breakpoints. If off, gdb
310 will insert breakpoints into inferior only when resuming it, and
311 will remove breakpoints upon stop. If auto, GDB will behave as ON
312 if in non-stop mode, and as OFF if all-stop mode.*/
313
314 static const char always_inserted_auto[] = "auto";
315 static const char always_inserted_on[] = "on";
316 static const char always_inserted_off[] = "off";
317 static const char *always_inserted_enums[] = {
318 always_inserted_auto,
319 always_inserted_off,
320 always_inserted_on,
321 NULL
322 };
323 static const char *always_inserted_mode = always_inserted_auto;
324 static void
325 show_always_inserted_mode (struct ui_file *file, int from_tty,
326 struct cmd_list_element *c, const char *value)
327 {
328 if (always_inserted_mode == always_inserted_auto)
329 fprintf_filtered (file,
330 _("Always inserted breakpoint "
331 "mode is %s (currently %s).\n"),
332 value,
333 breakpoints_always_inserted_mode () ? "on" : "off");
334 else
335 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
336 value);
337 }
338
339 int
340 breakpoints_always_inserted_mode (void)
341 {
342 return (always_inserted_mode == always_inserted_on
343 || (always_inserted_mode == always_inserted_auto && non_stop));
344 }
345
346 void _initialize_breakpoint (void);
347
348 /* Are we executing breakpoint commands? */
349 static int executing_breakpoint_commands;
350
351 /* Are overlay event breakpoints enabled? */
352 static int overlay_events_enabled;
353
354 /* See description in breakpoint.h. */
355 int target_exact_watchpoints = 0;
356
357 /* Walk the following statement or block through all breakpoints.
358 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the
359 current breakpoint. */
360
361 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
362
363 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
364 for (B = breakpoint_chain; \
365 B ? (TMP=B->next, 1): 0; \
366 B = TMP)
367
368 /* Similar iterator for the low-level breakpoints. SAFE variant is
369 not provided so update_global_location_list must not be called
370 while executing the block of ALL_BP_LOCATIONS. */
371
372 #define ALL_BP_LOCATIONS(B,BP_TMP) \
373 for (BP_TMP = bp_location; \
374 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
375 BP_TMP++)
376
377 /* Iterator for tracepoints only. */
378
379 #define ALL_TRACEPOINTS(B) \
380 for (B = breakpoint_chain; B; B = B->next) \
381 if (is_tracepoint (B))
382
383 /* Chains of all breakpoints defined. */
384
385 struct breakpoint *breakpoint_chain;
386
387 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
388
389 static struct bp_location **bp_location;
390
391 /* Number of elements of BP_LOCATION. */
392
393 static unsigned bp_location_count;
394
395 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
396 ADDRESS for the current elements of BP_LOCATION which get a valid
397 result from bp_location_has_shadow. You can use it for roughly
398 limiting the subrange of BP_LOCATION to scan for shadow bytes for
399 an address you need to read. */
400
401 static CORE_ADDR bp_location_placed_address_before_address_max;
402
403 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
404 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
405 BP_LOCATION which get a valid result from bp_location_has_shadow.
406 You can use it for roughly limiting the subrange of BP_LOCATION to
407 scan for shadow bytes for an address you need to read. */
408
409 static CORE_ADDR bp_location_shadow_len_after_address_max;
410
411 /* The locations that no longer correspond to any breakpoint, unlinked
412 from bp_location array, but for which a hit may still be reported
413 by a target. */
414 VEC(bp_location_p) *moribund_locations = NULL;
415
416 /* Number of last breakpoint made. */
417
418 static int breakpoint_count;
419
420 /* The value of `breakpoint_count' before the last command that
421 created breakpoints. If the last (break-like) command created more
422 than one breakpoint, then the difference between BREAKPOINT_COUNT
423 and PREV_BREAKPOINT_COUNT is more than one. */
424 static int prev_breakpoint_count;
425
426 /* Number of last tracepoint made. */
427
428 static int tracepoint_count;
429
430 static struct cmd_list_element *breakpoint_set_cmdlist;
431 static struct cmd_list_element *breakpoint_show_cmdlist;
432 struct cmd_list_element *save_cmdlist;
433
434 /* Return whether a breakpoint is an active enabled breakpoint. */
435 static int
436 breakpoint_enabled (struct breakpoint *b)
437 {
438 return (b->enable_state == bp_enabled);
439 }
440
441 /* Set breakpoint count to NUM. */
442
443 static void
444 set_breakpoint_count (int num)
445 {
446 prev_breakpoint_count = breakpoint_count;
447 breakpoint_count = num;
448 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
449 }
450
451 /* Used by `start_rbreak_breakpoints' below, to record the current
452 breakpoint count before "rbreak" creates any breakpoint. */
453 static int rbreak_start_breakpoint_count;
454
455 /* Called at the start an "rbreak" command to record the first
456 breakpoint made. */
457
458 void
459 start_rbreak_breakpoints (void)
460 {
461 rbreak_start_breakpoint_count = breakpoint_count;
462 }
463
464 /* Called at the end of an "rbreak" command to record the last
465 breakpoint made. */
466
467 void
468 end_rbreak_breakpoints (void)
469 {
470 prev_breakpoint_count = rbreak_start_breakpoint_count;
471 }
472
473 /* Used in run_command to zero the hit count when a new run starts. */
474
475 void
476 clear_breakpoint_hit_counts (void)
477 {
478 struct breakpoint *b;
479
480 ALL_BREAKPOINTS (b)
481 b->hit_count = 0;
482 }
483
484 /* Allocate a new counted_command_line with reference count of 1.
485 The new structure owns COMMANDS. */
486
487 static struct counted_command_line *
488 alloc_counted_command_line (struct command_line *commands)
489 {
490 struct counted_command_line *result
491 = xmalloc (sizeof (struct counted_command_line));
492
493 result->refc = 1;
494 result->commands = commands;
495 return result;
496 }
497
498 /* Increment reference count. This does nothing if CMD is NULL. */
499
500 static void
501 incref_counted_command_line (struct counted_command_line *cmd)
502 {
503 if (cmd)
504 ++cmd->refc;
505 }
506
507 /* Decrement reference count. If the reference count reaches 0,
508 destroy the counted_command_line. Sets *CMDP to NULL. This does
509 nothing if *CMDP is NULL. */
510
511 static void
512 decref_counted_command_line (struct counted_command_line **cmdp)
513 {
514 if (*cmdp)
515 {
516 if (--(*cmdp)->refc == 0)
517 {
518 free_command_lines (&(*cmdp)->commands);
519 xfree (*cmdp);
520 }
521 *cmdp = NULL;
522 }
523 }
524
525 /* A cleanup function that calls decref_counted_command_line. */
526
527 static void
528 do_cleanup_counted_command_line (void *arg)
529 {
530 decref_counted_command_line (arg);
531 }
532
533 /* Create a cleanup that calls decref_counted_command_line on the
534 argument. */
535
536 static struct cleanup *
537 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
538 {
539 return make_cleanup (do_cleanup_counted_command_line, cmdp);
540 }
541
542 /* Default address, symtab and line to put a breakpoint at
543 for "break" command with no arg.
544 If default_breakpoint_valid is zero, the other three are
545 not valid, and "break" with no arg is an error.
546
547 This set by print_stack_frame, which calls set_default_breakpoint. */
548
549 int default_breakpoint_valid;
550 CORE_ADDR default_breakpoint_address;
551 struct symtab *default_breakpoint_symtab;
552 int default_breakpoint_line;
553 struct program_space *default_breakpoint_pspace;
554
555 \f
556 /* Return the breakpoint with the specified number, or NULL
557 if the number does not refer to an existing breakpoint. */
558
559 struct breakpoint *
560 get_breakpoint (int num)
561 {
562 struct breakpoint *b;
563
564 ALL_BREAKPOINTS (b)
565 if (b->number == num)
566 return b;
567
568 return NULL;
569 }
570
571 \f
572
573 void
574 set_breakpoint_condition (struct breakpoint *b, char *exp,
575 int from_tty)
576 {
577 struct bp_location *loc = b->loc;
578
579 for (; loc; loc = loc->next)
580 {
581 xfree (loc->cond);
582 loc->cond = NULL;
583 }
584 xfree (b->cond_string);
585 b->cond_string = NULL;
586 xfree (b->cond_exp);
587 b->cond_exp = NULL;
588
589 if (*exp == 0)
590 {
591 if (from_tty)
592 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
593 }
594 else
595 {
596 char *arg = exp;
597
598 /* I don't know if it matters whether this is the string the user
599 typed in or the decompiled expression. */
600 b->cond_string = xstrdup (arg);
601 b->condition_not_parsed = 0;
602
603 if (is_watchpoint (b))
604 {
605 innermost_block = NULL;
606 arg = exp;
607 b->cond_exp = parse_exp_1 (&arg, 0, 0);
608 if (*arg)
609 error (_("Junk at end of expression"));
610 b->cond_exp_valid_block = innermost_block;
611 }
612 else
613 {
614 for (loc = b->loc; loc; loc = loc->next)
615 {
616 arg = exp;
617 loc->cond =
618 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
619 if (*arg)
620 error (_("Junk at end of expression"));
621 }
622 }
623 }
624 breakpoints_changed ();
625 observer_notify_breakpoint_modified (b->number);
626 }
627
628 /* condition N EXP -- set break condition of breakpoint N to EXP. */
629
630 static void
631 condition_command (char *arg, int from_tty)
632 {
633 struct breakpoint *b;
634 char *p;
635 int bnum;
636
637 if (arg == 0)
638 error_no_arg (_("breakpoint number"));
639
640 p = arg;
641 bnum = get_number (&p);
642 if (bnum == 0)
643 error (_("Bad breakpoint argument: '%s'"), arg);
644
645 ALL_BREAKPOINTS (b)
646 if (b->number == bnum)
647 {
648 /* Check if this breakpoint has a Python object assigned to
649 it, and if it has a definition of the "stop"
650 method. This method and conditions entered into GDB from
651 the CLI are mutually exclusive. */
652 if (b->py_bp_object
653 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
654 error (_("Cannot set a condition where a Python 'stop' "
655 "method has been defined in the breakpoint."));
656 set_breakpoint_condition (b, p, from_tty);
657 return;
658 }
659
660 error (_("No breakpoint number %d."), bnum);
661 }
662
663 /* Check that COMMAND do not contain commands that are suitable
664 only for tracepoints and not suitable for ordinary breakpoints.
665 Throw if any such commands is found. */
666
667 static void
668 check_no_tracepoint_commands (struct command_line *commands)
669 {
670 struct command_line *c;
671
672 for (c = commands; c; c = c->next)
673 {
674 int i;
675
676 if (c->control_type == while_stepping_control)
677 error (_("The 'while-stepping' command can "
678 "only be used for tracepoints"));
679
680 for (i = 0; i < c->body_count; ++i)
681 check_no_tracepoint_commands ((c->body_list)[i]);
682
683 /* Not that command parsing removes leading whitespace and comment
684 lines and also empty lines. So, we only need to check for
685 command directly. */
686 if (strstr (c->line, "collect ") == c->line)
687 error (_("The 'collect' command can only be used for tracepoints"));
688
689 if (strstr (c->line, "teval ") == c->line)
690 error (_("The 'teval' command can only be used for tracepoints"));
691 }
692 }
693
694 /* Encapsulate tests for different types of tracepoints. */
695
696 int
697 is_tracepoint (const struct breakpoint *b)
698 {
699 return (b->type == bp_tracepoint
700 || b->type == bp_fast_tracepoint
701 || b->type == bp_static_tracepoint);
702 }
703
704 /* A helper function that validsates that COMMANDS are valid for a
705 breakpoint. This function will throw an exception if a problem is
706 found. */
707
708 static void
709 validate_commands_for_breakpoint (struct breakpoint *b,
710 struct command_line *commands)
711 {
712 if (is_tracepoint (b))
713 {
714 /* We need to verify that each top-level element of commands is
715 valid for tracepoints, that there's at most one
716 while-stepping element, and that while-stepping's body has
717 valid tracing commands excluding nested while-stepping. */
718 struct command_line *c;
719 struct command_line *while_stepping = 0;
720 for (c = commands; c; c = c->next)
721 {
722 if (c->control_type == while_stepping_control)
723 {
724 if (b->type == bp_fast_tracepoint)
725 error (_("The 'while-stepping' command "
726 "cannot be used for fast tracepoint"));
727 else if (b->type == bp_static_tracepoint)
728 error (_("The 'while-stepping' command "
729 "cannot be used for static tracepoint"));
730
731 if (while_stepping)
732 error (_("The 'while-stepping' command "
733 "can be used only once"));
734 else
735 while_stepping = c;
736 }
737 }
738 if (while_stepping)
739 {
740 struct command_line *c2;
741
742 gdb_assert (while_stepping->body_count == 1);
743 c2 = while_stepping->body_list[0];
744 for (; c2; c2 = c2->next)
745 {
746 if (c2->control_type == while_stepping_control)
747 error (_("The 'while-stepping' command cannot be nested"));
748 }
749 }
750 }
751 else
752 {
753 check_no_tracepoint_commands (commands);
754 }
755 }
756
757 /* Return a vector of all the static tracepoints set at ADDR. The
758 caller is responsible for releasing the vector. */
759
760 VEC(breakpoint_p) *
761 static_tracepoints_here (CORE_ADDR addr)
762 {
763 struct breakpoint *b;
764 VEC(breakpoint_p) *found = 0;
765 struct bp_location *loc;
766
767 ALL_BREAKPOINTS (b)
768 if (b->type == bp_static_tracepoint)
769 {
770 for (loc = b->loc; loc; loc = loc->next)
771 if (loc->address == addr)
772 VEC_safe_push(breakpoint_p, found, b);
773 }
774
775 return found;
776 }
777
778 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
779 validate that only allowed commands are included. */
780
781 void
782 breakpoint_set_commands (struct breakpoint *b,
783 struct command_line *commands)
784 {
785 validate_commands_for_breakpoint (b, commands);
786
787 decref_counted_command_line (&b->commands);
788 b->commands = alloc_counted_command_line (commands);
789 breakpoints_changed ();
790 observer_notify_breakpoint_modified (b->number);
791 }
792
793 /* Set the internal `silent' flag on the breakpoint. Note that this
794 is not the same as the "silent" that may appear in the breakpoint's
795 commands. */
796
797 void
798 breakpoint_set_silent (struct breakpoint *b, int silent)
799 {
800 int old_silent = b->silent;
801
802 b->silent = silent;
803 if (old_silent != silent)
804 observer_notify_breakpoint_modified (b->number);
805 }
806
807 /* Set the thread for this breakpoint. If THREAD is -1, make the
808 breakpoint work for any thread. */
809
810 void
811 breakpoint_set_thread (struct breakpoint *b, int thread)
812 {
813 int old_thread = b->thread;
814
815 b->thread = thread;
816 if (old_thread != thread)
817 observer_notify_breakpoint_modified (b->number);
818 }
819
820 /* Set the task for this breakpoint. If TASK is 0, make the
821 breakpoint work for any task. */
822
823 void
824 breakpoint_set_task (struct breakpoint *b, int task)
825 {
826 int old_task = b->task;
827
828 b->task = task;
829 if (old_task != task)
830 observer_notify_breakpoint_modified (b->number);
831 }
832
833 void
834 check_tracepoint_command (char *line, void *closure)
835 {
836 struct breakpoint *b = closure;
837
838 validate_actionline (&line, b);
839 }
840
841 /* A structure used to pass information through
842 map_breakpoint_numbers. */
843
844 struct commands_info
845 {
846 /* True if the command was typed at a tty. */
847 int from_tty;
848
849 /* The breakpoint range spec. */
850 char *arg;
851
852 /* Non-NULL if the body of the commands are being read from this
853 already-parsed command. */
854 struct command_line *control;
855
856 /* The command lines read from the user, or NULL if they have not
857 yet been read. */
858 struct counted_command_line *cmd;
859 };
860
861 /* A callback for map_breakpoint_numbers that sets the commands for
862 commands_command. */
863
864 static void
865 do_map_commands_command (struct breakpoint *b, void *data)
866 {
867 struct commands_info *info = data;
868
869 if (info->cmd == NULL)
870 {
871 struct command_line *l;
872
873 if (info->control != NULL)
874 l = copy_command_lines (info->control->body_list[0]);
875 else
876 {
877 struct cleanup *old_chain;
878 char *str;
879
880 str = xstrprintf (_("Type commands for breakpoint(s) "
881 "%s, one per line."),
882 info->arg);
883
884 old_chain = make_cleanup (xfree, str);
885
886 l = read_command_lines (str,
887 info->from_tty, 1,
888 (is_tracepoint (b)
889 ? check_tracepoint_command : 0),
890 b);
891
892 do_cleanups (old_chain);
893 }
894
895 info->cmd = alloc_counted_command_line (l);
896 }
897
898 /* If a breakpoint was on the list more than once, we don't need to
899 do anything. */
900 if (b->commands != info->cmd)
901 {
902 validate_commands_for_breakpoint (b, info->cmd->commands);
903 incref_counted_command_line (info->cmd);
904 decref_counted_command_line (&b->commands);
905 b->commands = info->cmd;
906 breakpoints_changed ();
907 observer_notify_breakpoint_modified (b->number);
908 }
909 }
910
911 static void
912 commands_command_1 (char *arg, int from_tty,
913 struct command_line *control)
914 {
915 struct cleanup *cleanups;
916 struct commands_info info;
917
918 info.from_tty = from_tty;
919 info.control = control;
920 info.cmd = NULL;
921 /* If we read command lines from the user, then `info' will hold an
922 extra reference to the commands that we must clean up. */
923 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
924
925 if (arg == NULL || !*arg)
926 {
927 if (breakpoint_count - prev_breakpoint_count > 1)
928 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
929 breakpoint_count);
930 else if (breakpoint_count > 0)
931 arg = xstrprintf ("%d", breakpoint_count);
932 else
933 {
934 /* So that we don't try to free the incoming non-NULL
935 argument in the cleanup below. Mapping breakpoint
936 numbers will fail in this case. */
937 arg = NULL;
938 }
939 }
940 else
941 /* The command loop has some static state, so we need to preserve
942 our argument. */
943 arg = xstrdup (arg);
944
945 if (arg != NULL)
946 make_cleanup (xfree, arg);
947
948 info.arg = arg;
949
950 map_breakpoint_numbers (arg, do_map_commands_command, &info);
951
952 if (info.cmd == NULL)
953 error (_("No breakpoints specified."));
954
955 do_cleanups (cleanups);
956 }
957
958 static void
959 commands_command (char *arg, int from_tty)
960 {
961 commands_command_1 (arg, from_tty, NULL);
962 }
963
964 /* Like commands_command, but instead of reading the commands from
965 input stream, takes them from an already parsed command structure.
966
967 This is used by cli-script.c to DTRT with breakpoint commands
968 that are part of if and while bodies. */
969 enum command_control_type
970 commands_from_control_command (char *arg, struct command_line *cmd)
971 {
972 commands_command_1 (arg, 0, cmd);
973 return simple_control;
974 }
975
976 /* Return non-zero if BL->TARGET_INFO contains valid information. */
977
978 static int
979 bp_location_has_shadow (struct bp_location *bl)
980 {
981 if (bl->loc_type != bp_loc_software_breakpoint)
982 return 0;
983 if (!bl->inserted)
984 return 0;
985 if (bl->target_info.shadow_len == 0)
986 /* bp isn't valid, or doesn't shadow memory. */
987 return 0;
988 return 1;
989 }
990
991 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
992 by replacing any memory breakpoints with their shadowed contents.
993
994 The range of shadowed area by each bp_location is:
995 bl->address - bp_location_placed_address_before_address_max
996 up to bl->address + bp_location_shadow_len_after_address_max
997 The range we were requested to resolve shadows for is:
998 memaddr ... memaddr + len
999 Thus the safe cutoff boundaries for performance optimization are
1000 memaddr + len <= (bl->address
1001 - bp_location_placed_address_before_address_max)
1002 and:
1003 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1004
1005 void
1006 breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
1007 {
1008 /* Left boundary, right boundary and median element of our binary
1009 search. */
1010 unsigned bc_l, bc_r, bc;
1011
1012 /* Find BC_L which is a leftmost element which may affect BUF
1013 content. It is safe to report lower value but a failure to
1014 report higher one. */
1015
1016 bc_l = 0;
1017 bc_r = bp_location_count;
1018 while (bc_l + 1 < bc_r)
1019 {
1020 struct bp_location *bl;
1021
1022 bc = (bc_l + bc_r) / 2;
1023 bl = bp_location[bc];
1024
1025 /* Check first BL->ADDRESS will not overflow due to the added
1026 constant. Then advance the left boundary only if we are sure
1027 the BC element can in no way affect the BUF content (MEMADDR
1028 to MEMADDR + LEN range).
1029
1030 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1031 offset so that we cannot miss a breakpoint with its shadow
1032 range tail still reaching MEMADDR. */
1033
1034 if ((bl->address + bp_location_shadow_len_after_address_max
1035 >= bl->address)
1036 && (bl->address + bp_location_shadow_len_after_address_max
1037 <= memaddr))
1038 bc_l = bc;
1039 else
1040 bc_r = bc;
1041 }
1042
1043 /* Due to the binary search above, we need to make sure we pick the
1044 first location that's at BC_L's address. E.g., if there are
1045 multiple locations at the same address, BC_L may end up pointing
1046 at a duplicate location, and miss the "master"/"inserted"
1047 location. Say, given locations L1, L2 and L3 at addresses A and
1048 B:
1049
1050 L1@A, L2@A, L3@B, ...
1051
1052 BC_L could end up pointing at location L2, while the "master"
1053 location could be L1. Since the `loc->inserted' flag is only set
1054 on "master" locations, we'd forget to restore the shadow of L1
1055 and L2. */
1056 while (bc_l > 0
1057 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1058 bc_l--;
1059
1060 /* Now do full processing of the found relevant range of elements. */
1061
1062 for (bc = bc_l; bc < bp_location_count; bc++)
1063 {
1064 struct bp_location *bl = bp_location[bc];
1065 CORE_ADDR bp_addr = 0;
1066 int bp_size = 0;
1067 int bptoffset = 0;
1068
1069 /* bp_location array has BL->OWNER always non-NULL. */
1070 if (bl->owner->type == bp_none)
1071 warning (_("reading through apparently deleted breakpoint #%d?"),
1072 bl->owner->number);
1073
1074 /* Performance optimization: any futher element can no longer affect BUF
1075 content. */
1076
1077 if (bl->address >= bp_location_placed_address_before_address_max
1078 && memaddr + len <= (bl->address
1079 - bp_location_placed_address_before_address_max))
1080 break;
1081
1082 if (!bp_location_has_shadow (bl))
1083 continue;
1084 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1085 current_program_space->aspace, 0))
1086 continue;
1087
1088 /* Addresses and length of the part of the breakpoint that
1089 we need to copy. */
1090 bp_addr = bl->target_info.placed_address;
1091 bp_size = bl->target_info.shadow_len;
1092
1093 if (bp_addr + bp_size <= memaddr)
1094 /* The breakpoint is entirely before the chunk of memory we
1095 are reading. */
1096 continue;
1097
1098 if (bp_addr >= memaddr + len)
1099 /* The breakpoint is entirely after the chunk of memory we are
1100 reading. */
1101 continue;
1102
1103 /* Offset within shadow_contents. */
1104 if (bp_addr < memaddr)
1105 {
1106 /* Only copy the second part of the breakpoint. */
1107 bp_size -= memaddr - bp_addr;
1108 bptoffset = memaddr - bp_addr;
1109 bp_addr = memaddr;
1110 }
1111
1112 if (bp_addr + bp_size > memaddr + len)
1113 {
1114 /* Only copy the first part of the breakpoint. */
1115 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1116 }
1117
1118 memcpy (buf + bp_addr - memaddr,
1119 bl->target_info.shadow_contents + bptoffset, bp_size);
1120 }
1121 }
1122 \f
1123
1124 /* Return true if BPT is of any hardware watchpoint kind. */
1125
1126 static int
1127 is_hardware_watchpoint (const struct breakpoint *bpt)
1128 {
1129 return (bpt->type == bp_hardware_watchpoint
1130 || bpt->type == bp_read_watchpoint
1131 || bpt->type == bp_access_watchpoint);
1132 }
1133
1134 /* Return true if BPT is of any watchpoint kind, hardware or
1135 software. */
1136
1137 static int
1138 is_watchpoint (const struct breakpoint *bpt)
1139 {
1140 return (is_hardware_watchpoint (bpt)
1141 || bpt->type == bp_watchpoint);
1142 }
1143
1144 /* Assuming that B is a watchpoint: returns true if the current thread
1145 and its running state are safe to evaluate or update watchpoint B.
1146 Watchpoints on local expressions need to be evaluated in the
1147 context of the thread that was current when the watchpoint was
1148 created, and, that thread needs to be stopped to be able to select
1149 the correct frame context. Watchpoints on global expressions can
1150 be evaluated on any thread, and in any state. It is presently left
1151 to the target allowing memory accesses when threads are
1152 running. */
1153
1154 static int
1155 watchpoint_in_thread_scope (struct breakpoint *b)
1156 {
1157 return (ptid_equal (b->watchpoint_thread, null_ptid)
1158 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1159 && !is_executing (inferior_ptid)));
1160 }
1161
1162 /* Assuming that B is a watchpoint:
1163 - Reparse watchpoint expression, if REPARSE is non-zero
1164 - Evaluate expression and store the result in B->val
1165 - Evaluate the condition if there is one, and store the result
1166 in b->loc->cond.
1167 - Update the list of values that must be watched in B->loc.
1168
1169 If the watchpoint disposition is disp_del_at_next_stop, then do
1170 nothing. If this is local watchpoint that is out of scope, delete
1171 it.
1172
1173 Even with `set breakpoint always-inserted on' the watchpoints are
1174 removed + inserted on each stop here. Normal breakpoints must
1175 never be removed because they might be missed by a running thread
1176 when debugging in non-stop mode. On the other hand, hardware
1177 watchpoints (is_hardware_watchpoint; processed here) are specific
1178 to each LWP since they are stored in each LWP's hardware debug
1179 registers. Therefore, such LWP must be stopped first in order to
1180 be able to modify its hardware watchpoints.
1181
1182 Hardware watchpoints must be reset exactly once after being
1183 presented to the user. It cannot be done sooner, because it would
1184 reset the data used to present the watchpoint hit to the user. And
1185 it must not be done later because it could display the same single
1186 watchpoint hit during multiple GDB stops. Note that the latter is
1187 relevant only to the hardware watchpoint types bp_read_watchpoint
1188 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1189 not user-visible - its hit is suppressed if the memory content has
1190 not changed.
1191
1192 The following constraints influence the location where we can reset
1193 hardware watchpoints:
1194
1195 * target_stopped_by_watchpoint and target_stopped_data_address are
1196 called several times when GDB stops.
1197
1198 [linux]
1199 * Multiple hardware watchpoints can be hit at the same time,
1200 causing GDB to stop. GDB only presents one hardware watchpoint
1201 hit at a time as the reason for stopping, and all the other hits
1202 are presented later, one after the other, each time the user
1203 requests the execution to be resumed. Execution is not resumed
1204 for the threads still having pending hit event stored in
1205 LWP_INFO->STATUS. While the watchpoint is already removed from
1206 the inferior on the first stop the thread hit event is kept being
1207 reported from its cached value by linux_nat_stopped_data_address
1208 until the real thread resume happens after the watchpoint gets
1209 presented and thus its LWP_INFO->STATUS gets reset.
1210
1211 Therefore the hardware watchpoint hit can get safely reset on the
1212 watchpoint removal from inferior. */
1213
1214 static void
1215 update_watchpoint (struct breakpoint *b, int reparse)
1216 {
1217 int within_current_scope;
1218 struct frame_id saved_frame_id;
1219 int frame_saved;
1220
1221 /* If this is a local watchpoint, we only want to check if the
1222 watchpoint frame is in scope if the current thread is the thread
1223 that was used to create the watchpoint. */
1224 if (!watchpoint_in_thread_scope (b))
1225 return;
1226
1227 if (b->disposition == disp_del_at_next_stop)
1228 return;
1229
1230 frame_saved = 0;
1231
1232 /* Determine if the watchpoint is within scope. */
1233 if (b->exp_valid_block == NULL)
1234 within_current_scope = 1;
1235 else
1236 {
1237 struct frame_info *fi = get_current_frame ();
1238 struct gdbarch *frame_arch = get_frame_arch (fi);
1239 CORE_ADDR frame_pc = get_frame_pc (fi);
1240
1241 /* If we're in a function epilogue, unwinding may not work
1242 properly, so do not attempt to recreate locations at this
1243 point. See similar comments in watchpoint_check. */
1244 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1245 return;
1246
1247 /* Save the current frame's ID so we can restore it after
1248 evaluating the watchpoint expression on its own frame. */
1249 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1250 took a frame parameter, so that we didn't have to change the
1251 selected frame. */
1252 frame_saved = 1;
1253 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1254
1255 fi = frame_find_by_id (b->watchpoint_frame);
1256 within_current_scope = (fi != NULL);
1257 if (within_current_scope)
1258 select_frame (fi);
1259 }
1260
1261 /* We don't free locations. They are stored in the bp_location array
1262 and update_global_location_list will eventually delete them and
1263 remove breakpoints if needed. */
1264 b->loc = NULL;
1265
1266 if (within_current_scope && reparse)
1267 {
1268 char *s;
1269
1270 if (b->exp)
1271 {
1272 xfree (b->exp);
1273 b->exp = NULL;
1274 }
1275 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1276 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1277 /* If the meaning of expression itself changed, the old value is
1278 no longer relevant. We don't want to report a watchpoint hit
1279 to the user when the old value and the new value may actually
1280 be completely different objects. */
1281 value_free (b->val);
1282 b->val = NULL;
1283 b->val_valid = 0;
1284
1285 /* Note that unlike with breakpoints, the watchpoint's condition
1286 expression is stored in the breakpoint object, not in the
1287 locations (re)created below. */
1288 if (b->cond_string != NULL)
1289 {
1290 if (b->cond_exp != NULL)
1291 {
1292 xfree (b->cond_exp);
1293 b->cond_exp = NULL;
1294 }
1295
1296 s = b->cond_string;
1297 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1298 }
1299 }
1300
1301 /* If we failed to parse the expression, for example because
1302 it refers to a global variable in a not-yet-loaded shared library,
1303 don't try to insert watchpoint. We don't automatically delete
1304 such watchpoint, though, since failure to parse expression
1305 is different from out-of-scope watchpoint. */
1306 if ( !target_has_execution)
1307 {
1308 /* Without execution, memory can't change. No use to try and
1309 set watchpoint locations. The watchpoint will be reset when
1310 the target gains execution, through breakpoint_re_set. */
1311 }
1312 else if (within_current_scope && b->exp)
1313 {
1314 int pc = 0;
1315 struct value *val_chain, *v, *result, *next;
1316 struct program_space *frame_pspace;
1317
1318 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
1319
1320 /* Avoid setting b->val if it's already set. The meaning of
1321 b->val is 'the last value' user saw, and we should update
1322 it only if we reported that last value to user. As it
1323 happens, the code that reports it updates b->val directly. */
1324 if (!b->val_valid)
1325 {
1326 b->val = v;
1327 b->val_valid = 1;
1328 }
1329
1330 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1331
1332 /* Look at each value on the value chain. */
1333 for (v = val_chain; v; v = value_next (v))
1334 {
1335 /* If it's a memory location, and GDB actually needed
1336 its contents to evaluate the expression, then we
1337 must watch it. If the first value returned is
1338 still lazy, that means an error occurred reading it;
1339 watch it anyway in case it becomes readable. */
1340 if (VALUE_LVAL (v) == lval_memory
1341 && (v == val_chain || ! value_lazy (v)))
1342 {
1343 struct type *vtype = check_typedef (value_type (v));
1344
1345 /* We only watch structs and arrays if user asked
1346 for it explicitly, never if they just happen to
1347 appear in the middle of some value chain. */
1348 if (v == result
1349 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1350 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1351 {
1352 CORE_ADDR addr;
1353 int len, type;
1354 struct bp_location *loc, **tmp;
1355
1356 addr = value_address (v);
1357 len = TYPE_LENGTH (value_type (v));
1358 type = hw_write;
1359 if (b->type == bp_read_watchpoint)
1360 type = hw_read;
1361 else if (b->type == bp_access_watchpoint)
1362 type = hw_access;
1363
1364 loc = allocate_bp_location (b);
1365 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1366 ;
1367 *tmp = loc;
1368 loc->gdbarch = get_type_arch (value_type (v));
1369
1370 loc->pspace = frame_pspace;
1371 loc->address = addr;
1372 loc->length = len;
1373 loc->watchpoint_type = type;
1374 }
1375 }
1376 }
1377
1378 /* Change the type of breakpoint between hardware assisted or
1379 an ordinary watchpoint depending on the hardware support
1380 and free hardware slots. REPARSE is set when the inferior
1381 is started. */
1382 if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
1383 && reparse)
1384 {
1385 int reg_cnt;
1386 enum bp_loc_type loc_type;
1387 struct bp_location *bl;
1388
1389 reg_cnt = can_use_hardware_watchpoint (val_chain, b->exact);
1390
1391 if (reg_cnt)
1392 {
1393 int i, target_resources_ok, other_type_used;
1394 enum enable_state orig_enable_state;
1395
1396 /* We need to determine how many resources are already
1397 used for all other hardware watchpoints plus this one
1398 to see if we still have enough resources to also fit
1399 this watchpoint in as well. To guarantee the
1400 hw_watchpoint_used_count call below counts this
1401 watchpoint, make sure that it is marked as a hardware
1402 watchpoint. */
1403 b->type = bp_hardware_watchpoint;
1404
1405 /* hw_watchpoint_used_count ignores disabled watchpoints,
1406 and b might be disabled if we're being called from
1407 do_enable_breakpoint. */
1408 orig_enable_state = b->enable_state;
1409 b->enable_state = bp_enabled;
1410
1411 i = hw_watchpoint_used_count (bp_hardware_watchpoint,
1412 &other_type_used);
1413
1414 b->enable_state = orig_enable_state;
1415
1416 target_resources_ok = target_can_use_hardware_watchpoint
1417 (bp_hardware_watchpoint, i, other_type_used);
1418 if (target_resources_ok <= 0)
1419 b->type = bp_watchpoint;
1420 }
1421 else
1422 b->type = bp_watchpoint;
1423
1424 loc_type = (b->type == bp_watchpoint? bp_loc_other
1425 : bp_loc_hardware_watchpoint);
1426 for (bl = b->loc; bl; bl = bl->next)
1427 bl->loc_type = loc_type;
1428 }
1429
1430 for (v = val_chain; v; v = next)
1431 {
1432 next = value_next (v);
1433 if (v != b->val)
1434 value_free (v);
1435 }
1436
1437 /* If a software watchpoint is not watching any memory, then the
1438 above left it without any location set up. But,
1439 bpstat_stop_status requires a location to be able to report
1440 stops, so make sure there's at least a dummy one. */
1441 if (b->type == bp_watchpoint && b->loc == NULL)
1442 {
1443 b->loc = allocate_bp_location (b);
1444 b->loc->pspace = frame_pspace;
1445 b->loc->address = -1;
1446 b->loc->length = -1;
1447 b->loc->watchpoint_type = -1;
1448 }
1449 }
1450 else if (!within_current_scope)
1451 {
1452 printf_filtered (_("\
1453 Watchpoint %d deleted because the program has left the block\n\
1454 in which its expression is valid.\n"),
1455 b->number);
1456 if (b->related_breakpoint)
1457 {
1458 b->related_breakpoint->disposition = disp_del_at_next_stop;
1459 b->related_breakpoint->related_breakpoint = NULL;
1460 b->related_breakpoint= NULL;
1461 }
1462 b->disposition = disp_del_at_next_stop;
1463 }
1464
1465 /* Restore the selected frame. */
1466 if (frame_saved)
1467 select_frame (frame_find_by_id (saved_frame_id));
1468 }
1469
1470
1471 /* Returns 1 iff breakpoint location should be
1472 inserted in the inferior. */
1473 static int
1474 should_be_inserted (struct bp_location *bl)
1475 {
1476 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1477 return 0;
1478
1479 if (bl->owner->disposition == disp_del_at_next_stop)
1480 return 0;
1481
1482 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1483 return 0;
1484
1485 /* This is set for example, when we're attached to the parent of a
1486 vfork, and have detached from the child. The child is running
1487 free, and we expect it to do an exec or exit, at which point the
1488 OS makes the parent schedulable again (and the target reports
1489 that the vfork is done). Until the child is done with the shared
1490 memory region, do not insert breakpoints in the parent, otherwise
1491 the child could still trip on the parent's breakpoints. Since
1492 the parent is blocked anyway, it won't miss any breakpoint. */
1493 if (bl->pspace->breakpoints_not_allowed)
1494 return 0;
1495
1496 /* Tracepoints are inserted by the target at a time of its choosing,
1497 not by us. */
1498 if (is_tracepoint (bl->owner))
1499 return 0;
1500
1501 return 1;
1502 }
1503
1504 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1505 location. Any error messages are printed to TMP_ERROR_STREAM; and
1506 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
1507
1508 NOTE drow/2003-09-09: This routine could be broken down to an
1509 object-style method for each breakpoint or catchpoint type. */
1510 static int
1511 insert_bp_location (struct bp_location *bl,
1512 struct ui_file *tmp_error_stream,
1513 int *disabled_breaks,
1514 int *hw_breakpoint_error)
1515 {
1516 int val = 0;
1517
1518 if (!should_be_inserted (bl) || bl->inserted)
1519 return 0;
1520
1521 /* Initialize the target-specific information. */
1522 memset (&bl->target_info, 0, sizeof (bl->target_info));
1523 bl->target_info.placed_address = bl->address;
1524 bl->target_info.placed_address_space = bl->pspace->aspace;
1525
1526 if (bl->loc_type == bp_loc_software_breakpoint
1527 || bl->loc_type == bp_loc_hardware_breakpoint)
1528 {
1529 if (bl->owner->type != bp_hardware_breakpoint)
1530 {
1531 /* If the explicitly specified breakpoint type
1532 is not hardware breakpoint, check the memory map to see
1533 if the breakpoint address is in read only memory or not.
1534
1535 Two important cases are:
1536 - location type is not hardware breakpoint, memory
1537 is readonly. We change the type of the location to
1538 hardware breakpoint.
1539 - location type is hardware breakpoint, memory is
1540 read-write. This means we've previously made the
1541 location hardware one, but then the memory map changed,
1542 so we undo.
1543
1544 When breakpoints are removed, remove_breakpoints will use
1545 location types we've just set here, the only possible
1546 problem is that memory map has changed during running
1547 program, but it's not going to work anyway with current
1548 gdb. */
1549 struct mem_region *mr
1550 = lookup_mem_region (bl->target_info.placed_address);
1551
1552 if (mr)
1553 {
1554 if (automatic_hardware_breakpoints)
1555 {
1556 enum bp_loc_type new_type;
1557
1558 if (mr->attrib.mode != MEM_RW)
1559 new_type = bp_loc_hardware_breakpoint;
1560 else
1561 new_type = bp_loc_software_breakpoint;
1562
1563 if (new_type != bl->loc_type)
1564 {
1565 static int said = 0;
1566
1567 bl->loc_type = new_type;
1568 if (!said)
1569 {
1570 fprintf_filtered (gdb_stdout,
1571 _("Note: automatically using "
1572 "hardware breakpoints for "
1573 "read-only addresses.\n"));
1574 said = 1;
1575 }
1576 }
1577 }
1578 else if (bl->loc_type == bp_loc_software_breakpoint
1579 && mr->attrib.mode != MEM_RW)
1580 warning (_("cannot set software breakpoint "
1581 "at readonly address %s"),
1582 paddress (bl->gdbarch, bl->address));
1583 }
1584 }
1585
1586 /* First check to see if we have to handle an overlay. */
1587 if (overlay_debugging == ovly_off
1588 || bl->section == NULL
1589 || !(section_is_overlay (bl->section)))
1590 {
1591 /* No overlay handling: just set the breakpoint. */
1592
1593 if (bl->loc_type == bp_loc_hardware_breakpoint)
1594 val = target_insert_hw_breakpoint (bl->gdbarch,
1595 &bl->target_info);
1596 else
1597 val = target_insert_breakpoint (bl->gdbarch,
1598 &bl->target_info);
1599 }
1600 else
1601 {
1602 /* This breakpoint is in an overlay section.
1603 Shall we set a breakpoint at the LMA? */
1604 if (!overlay_events_enabled)
1605 {
1606 /* Yes -- overlay event support is not active,
1607 so we must try to set a breakpoint at the LMA.
1608 This will not work for a hardware breakpoint. */
1609 if (bl->loc_type == bp_loc_hardware_breakpoint)
1610 warning (_("hardware breakpoint %d not supported in overlay!"),
1611 bl->owner->number);
1612 else
1613 {
1614 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1615 bl->section);
1616 /* Set a software (trap) breakpoint at the LMA. */
1617 bl->overlay_target_info = bl->target_info;
1618 bl->overlay_target_info.placed_address = addr;
1619 val = target_insert_breakpoint (bl->gdbarch,
1620 &bl->overlay_target_info);
1621 if (val != 0)
1622 fprintf_unfiltered (tmp_error_stream,
1623 "Overlay breakpoint %d "
1624 "failed: in ROM?\n",
1625 bl->owner->number);
1626 }
1627 }
1628 /* Shall we set a breakpoint at the VMA? */
1629 if (section_is_mapped (bl->section))
1630 {
1631 /* Yes. This overlay section is mapped into memory. */
1632 if (bl->loc_type == bp_loc_hardware_breakpoint)
1633 val = target_insert_hw_breakpoint (bl->gdbarch,
1634 &bl->target_info);
1635 else
1636 val = target_insert_breakpoint (bl->gdbarch,
1637 &bl->target_info);
1638 }
1639 else
1640 {
1641 /* No. This breakpoint will not be inserted.
1642 No error, but do not mark the bp as 'inserted'. */
1643 return 0;
1644 }
1645 }
1646
1647 if (val)
1648 {
1649 /* Can't set the breakpoint. */
1650 if (solib_name_from_address (bl->pspace, bl->address))
1651 {
1652 /* See also: disable_breakpoints_in_shlibs. */
1653 val = 0;
1654 bl->shlib_disabled = 1;
1655 if (!*disabled_breaks)
1656 {
1657 fprintf_unfiltered (tmp_error_stream,
1658 "Cannot insert breakpoint %d.\n",
1659 bl->owner->number);
1660 fprintf_unfiltered (tmp_error_stream,
1661 "Temporarily disabling shared "
1662 "library breakpoints:\n");
1663 }
1664 *disabled_breaks = 1;
1665 fprintf_unfiltered (tmp_error_stream,
1666 "breakpoint #%d\n", bl->owner->number);
1667 }
1668 else
1669 {
1670 if (bl->loc_type == bp_loc_hardware_breakpoint)
1671 {
1672 *hw_breakpoint_error = 1;
1673 fprintf_unfiltered (tmp_error_stream,
1674 "Cannot insert hardware "
1675 "breakpoint %d.\n",
1676 bl->owner->number);
1677 }
1678 else
1679 {
1680 fprintf_unfiltered (tmp_error_stream,
1681 "Cannot insert breakpoint %d.\n",
1682 bl->owner->number);
1683 fprintf_filtered (tmp_error_stream,
1684 "Error accessing memory address ");
1685 fputs_filtered (paddress (bl->gdbarch, bl->address),
1686 tmp_error_stream);
1687 fprintf_filtered (tmp_error_stream, ": %s.\n",
1688 safe_strerror (val));
1689 }
1690
1691 }
1692 }
1693 else
1694 bl->inserted = 1;
1695
1696 return val;
1697 }
1698
1699 else if (bl->loc_type == bp_loc_hardware_watchpoint
1700 /* NOTE drow/2003-09-08: This state only exists for removing
1701 watchpoints. It's not clear that it's necessary... */
1702 && bl->owner->disposition != disp_del_at_next_stop)
1703 {
1704 gdb_assert (bl->owner->ops != NULL
1705 && bl->owner->ops->insert_location != NULL);
1706
1707 val = bl->owner->ops->insert_location (bl);
1708
1709 /* If trying to set a read-watchpoint, and it turns out it's not
1710 supported, try emulating one with an access watchpoint. */
1711 if (val == 1 && bl->watchpoint_type == hw_read)
1712 {
1713 struct bp_location *loc, **loc_temp;
1714
1715 /* But don't try to insert it, if there's already another
1716 hw_access location that would be considered a duplicate
1717 of this one. */
1718 ALL_BP_LOCATIONS (loc, loc_temp)
1719 if (loc != bl
1720 && loc->watchpoint_type == hw_access
1721 && watchpoint_locations_match (bl, loc))
1722 {
1723 bl->duplicate = 1;
1724 bl->inserted = 1;
1725 bl->target_info = loc->target_info;
1726 bl->watchpoint_type = hw_access;
1727 val = 0;
1728 break;
1729 }
1730
1731 if (val == 1)
1732 {
1733 bl->watchpoint_type = hw_access;
1734 val = bl->owner->ops->insert_location (bl);
1735
1736 if (val)
1737 /* Back to the original value. */
1738 bl->watchpoint_type = hw_read;
1739 }
1740 }
1741
1742 bl->inserted = (val == 0);
1743 }
1744
1745 else if (bl->owner->type == bp_catchpoint)
1746 {
1747 gdb_assert (bl->owner->ops != NULL
1748 && bl->owner->ops->insert_location != NULL);
1749
1750 val = bl->owner->ops->insert_location (bl);
1751 if (val)
1752 {
1753 bl->owner->enable_state = bp_disabled;
1754
1755 if (val == 1)
1756 warning (_("\
1757 Error inserting catchpoint %d: Your system does not support this type\n\
1758 of catchpoint."), bl->owner->number);
1759 else
1760 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1761 }
1762
1763 bl->inserted = (val == 0);
1764
1765 /* We've already printed an error message if there was a problem
1766 inserting this catchpoint, and we've disabled the catchpoint,
1767 so just return success. */
1768 return 0;
1769 }
1770
1771 return 0;
1772 }
1773
1774 /* This function is called when program space PSPACE is about to be
1775 deleted. It takes care of updating breakpoints to not reference
1776 PSPACE anymore. */
1777
1778 void
1779 breakpoint_program_space_exit (struct program_space *pspace)
1780 {
1781 struct breakpoint *b, *b_temp;
1782 struct bp_location *loc, **loc_temp;
1783
1784 /* Remove any breakpoint that was set through this program space. */
1785 ALL_BREAKPOINTS_SAFE (b, b_temp)
1786 {
1787 if (b->pspace == pspace)
1788 delete_breakpoint (b);
1789 }
1790
1791 /* Breakpoints set through other program spaces could have locations
1792 bound to PSPACE as well. Remove those. */
1793 ALL_BP_LOCATIONS (loc, loc_temp)
1794 {
1795 struct bp_location *tmp;
1796
1797 if (loc->pspace == pspace)
1798 {
1799 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
1800 if (loc->owner->loc == loc)
1801 loc->owner->loc = loc->next;
1802 else
1803 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1804 if (tmp->next == loc)
1805 {
1806 tmp->next = loc->next;
1807 break;
1808 }
1809 }
1810 }
1811
1812 /* Now update the global location list to permanently delete the
1813 removed locations above. */
1814 update_global_location_list (0);
1815 }
1816
1817 /* Make sure all breakpoints are inserted in inferior.
1818 Throws exception on any error.
1819 A breakpoint that is already inserted won't be inserted
1820 again, so calling this function twice is safe. */
1821 void
1822 insert_breakpoints (void)
1823 {
1824 struct breakpoint *bpt;
1825
1826 ALL_BREAKPOINTS (bpt)
1827 if (is_hardware_watchpoint (bpt))
1828 update_watchpoint (bpt, 0 /* don't reparse. */);
1829
1830 update_global_location_list (1);
1831
1832 /* update_global_location_list does not insert breakpoints when
1833 always_inserted_mode is not enabled. Explicitly insert them
1834 now. */
1835 if (!breakpoints_always_inserted_mode ())
1836 insert_breakpoint_locations ();
1837 }
1838
1839 /* insert_breakpoints is used when starting or continuing the program.
1840 remove_breakpoints is used when the program stops.
1841 Both return zero if successful,
1842 or an `errno' value if could not write the inferior. */
1843
1844 static void
1845 insert_breakpoint_locations (void)
1846 {
1847 struct breakpoint *bpt;
1848 struct bp_location *bl, **blp_tmp;
1849 int error = 0;
1850 int val = 0;
1851 int disabled_breaks = 0;
1852 int hw_breakpoint_error = 0;
1853
1854 struct ui_file *tmp_error_stream = mem_fileopen ();
1855 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
1856
1857 /* Explicitly mark the warning -- this will only be printed if
1858 there was an error. */
1859 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
1860
1861 save_current_space_and_thread ();
1862
1863 ALL_BP_LOCATIONS (bl, blp_tmp)
1864 {
1865 if (!should_be_inserted (bl) || bl->inserted)
1866 continue;
1867
1868 /* There is no point inserting thread-specific breakpoints if
1869 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1870 has BL->OWNER always non-NULL. */
1871 if (bl->owner->thread != -1
1872 && !valid_thread_id (bl->owner->thread))
1873 continue;
1874
1875 switch_to_program_space_and_thread (bl->pspace);
1876
1877 /* For targets that support global breakpoints, there's no need
1878 to select an inferior to insert breakpoint to. In fact, even
1879 if we aren't attached to any process yet, we should still
1880 insert breakpoints. */
1881 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1882 && ptid_equal (inferior_ptid, null_ptid))
1883 continue;
1884
1885 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
1886 &hw_breakpoint_error);
1887 if (val)
1888 error = val;
1889 }
1890
1891 /* If we failed to insert all locations of a watchpoint, remove
1892 them, as half-inserted watchpoint is of limited use. */
1893 ALL_BREAKPOINTS (bpt)
1894 {
1895 int some_failed = 0;
1896 struct bp_location *loc;
1897
1898 if (!is_hardware_watchpoint (bpt))
1899 continue;
1900
1901 if (!breakpoint_enabled (bpt))
1902 continue;
1903
1904 if (bpt->disposition == disp_del_at_next_stop)
1905 continue;
1906
1907 for (loc = bpt->loc; loc; loc = loc->next)
1908 if (!loc->inserted && should_be_inserted (loc))
1909 {
1910 some_failed = 1;
1911 break;
1912 }
1913 if (some_failed)
1914 {
1915 for (loc = bpt->loc; loc; loc = loc->next)
1916 if (loc->inserted)
1917 remove_breakpoint (loc, mark_uninserted);
1918
1919 hw_breakpoint_error = 1;
1920 fprintf_unfiltered (tmp_error_stream,
1921 "Could not insert hardware watchpoint %d.\n",
1922 bpt->number);
1923 error = -1;
1924 }
1925 }
1926
1927 if (error)
1928 {
1929 /* If a hardware breakpoint or watchpoint was inserted, add a
1930 message about possibly exhausted resources. */
1931 if (hw_breakpoint_error)
1932 {
1933 fprintf_unfiltered (tmp_error_stream,
1934 "Could not insert hardware breakpoints:\n\
1935 You may have requested too many hardware breakpoints/watchpoints.\n");
1936 }
1937 target_terminal_ours_for_output ();
1938 error_stream (tmp_error_stream);
1939 }
1940
1941 do_cleanups (cleanups);
1942 }
1943
1944 int
1945 remove_breakpoints (void)
1946 {
1947 struct bp_location *bl, **blp_tmp;
1948 int val = 0;
1949
1950 ALL_BP_LOCATIONS (bl, blp_tmp)
1951 {
1952 if (bl->inserted)
1953 val |= remove_breakpoint (bl, mark_uninserted);
1954 }
1955 return val;
1956 }
1957
1958 /* Remove breakpoints of process PID. */
1959
1960 int
1961 remove_breakpoints_pid (int pid)
1962 {
1963 struct bp_location *bl, **blp_tmp;
1964 int val;
1965 struct inferior *inf = find_inferior_pid (pid);
1966
1967 ALL_BP_LOCATIONS (bl, blp_tmp)
1968 {
1969 if (bl->pspace != inf->pspace)
1970 continue;
1971
1972 if (bl->inserted)
1973 {
1974 val = remove_breakpoint (bl, mark_uninserted);
1975 if (val != 0)
1976 return val;
1977 }
1978 }
1979 return 0;
1980 }
1981
1982 int
1983 remove_hw_watchpoints (void)
1984 {
1985 struct bp_location *bl, **blp_tmp;
1986 int val = 0;
1987
1988 ALL_BP_LOCATIONS (bl, blp_tmp)
1989 {
1990 if (bl->inserted && bl->loc_type == bp_loc_hardware_watchpoint)
1991 val |= remove_breakpoint (bl, mark_uninserted);
1992 }
1993 return val;
1994 }
1995
1996 int
1997 reattach_breakpoints (int pid)
1998 {
1999 struct cleanup *old_chain;
2000 struct bp_location *bl, **blp_tmp;
2001 int val;
2002 struct ui_file *tmp_error_stream;
2003 int dummy1 = 0, dummy2 = 0;
2004 struct inferior *inf;
2005 struct thread_info *tp;
2006
2007 tp = any_live_thread_of_process (pid);
2008 if (tp == NULL)
2009 return 1;
2010
2011 inf = find_inferior_pid (pid);
2012 old_chain = save_inferior_ptid ();
2013
2014 inferior_ptid = tp->ptid;
2015
2016 tmp_error_stream = mem_fileopen ();
2017 make_cleanup_ui_file_delete (tmp_error_stream);
2018
2019 ALL_BP_LOCATIONS (bl, blp_tmp)
2020 {
2021 if (bl->pspace != inf->pspace)
2022 continue;
2023
2024 if (bl->inserted)
2025 {
2026 bl->inserted = 0;
2027 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
2028 if (val != 0)
2029 {
2030 do_cleanups (old_chain);
2031 return val;
2032 }
2033 }
2034 }
2035 do_cleanups (old_chain);
2036 return 0;
2037 }
2038
2039 static int internal_breakpoint_number = -1;
2040
2041 /* Set the breakpoint number of B, depending on the value of INTERNAL.
2042 If INTERNAL is non-zero, the breakpoint number will be populated
2043 from internal_breakpoint_number and that variable decremented.
2044 Otherwis the breakpoint number will be populated from
2045 breakpoint_count and that value incremented. Internal breakpoints
2046 do not set the internal var bpnum. */
2047 static void
2048 set_breakpoint_number (int internal, struct breakpoint *b)
2049 {
2050 if (internal)
2051 b->number = internal_breakpoint_number--;
2052 else
2053 {
2054 set_breakpoint_count (breakpoint_count + 1);
2055 b->number = breakpoint_count;
2056 }
2057 }
2058
2059 static struct breakpoint *
2060 create_internal_breakpoint (struct gdbarch *gdbarch,
2061 CORE_ADDR address, enum bptype type)
2062 {
2063 struct symtab_and_line sal;
2064 struct breakpoint *b;
2065
2066 init_sal (&sal); /* Initialize to zeroes. */
2067
2068 sal.pc = address;
2069 sal.section = find_pc_overlay (sal.pc);
2070 sal.pspace = current_program_space;
2071
2072 b = set_raw_breakpoint (gdbarch, sal, type);
2073 b->number = internal_breakpoint_number--;
2074 b->disposition = disp_donttouch;
2075
2076 return b;
2077 }
2078
2079 static const char *const longjmp_names[] =
2080 {
2081 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2082 };
2083 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2084
2085 /* Per-objfile data private to breakpoint.c. */
2086 struct breakpoint_objfile_data
2087 {
2088 /* Minimal symbol for "_ovly_debug_event" (if any). */
2089 struct minimal_symbol *overlay_msym;
2090
2091 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2092 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2093
2094 /* Minimal symbol for "std::terminate()" (if any). */
2095 struct minimal_symbol *terminate_msym;
2096
2097 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2098 struct minimal_symbol *exception_msym;
2099 };
2100
2101 static const struct objfile_data *breakpoint_objfile_key;
2102
2103 /* Minimal symbol not found sentinel. */
2104 static struct minimal_symbol msym_not_found;
2105
2106 /* Returns TRUE if MSYM point to the "not found" sentinel. */
2107
2108 static int
2109 msym_not_found_p (const struct minimal_symbol *msym)
2110 {
2111 return msym == &msym_not_found;
2112 }
2113
2114 /* Return per-objfile data needed by breakpoint.c.
2115 Allocate the data if necessary. */
2116
2117 static struct breakpoint_objfile_data *
2118 get_breakpoint_objfile_data (struct objfile *objfile)
2119 {
2120 struct breakpoint_objfile_data *bp_objfile_data;
2121
2122 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2123 if (bp_objfile_data == NULL)
2124 {
2125 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2126 sizeof (*bp_objfile_data));
2127
2128 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2129 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2130 }
2131 return bp_objfile_data;
2132 }
2133
2134 static void
2135 create_overlay_event_breakpoint (void)
2136 {
2137 struct objfile *objfile;
2138 const char *const func_name = "_ovly_debug_event";
2139
2140 ALL_OBJFILES (objfile)
2141 {
2142 struct breakpoint *b;
2143 struct breakpoint_objfile_data *bp_objfile_data;
2144 CORE_ADDR addr;
2145
2146 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2147
2148 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2149 continue;
2150
2151 if (bp_objfile_data->overlay_msym == NULL)
2152 {
2153 struct minimal_symbol *m;
2154
2155 m = lookup_minimal_symbol_text (func_name, objfile);
2156 if (m == NULL)
2157 {
2158 /* Avoid future lookups in this objfile. */
2159 bp_objfile_data->overlay_msym = &msym_not_found;
2160 continue;
2161 }
2162 bp_objfile_data->overlay_msym = m;
2163 }
2164
2165 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2166 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2167 bp_overlay_event);
2168 b->addr_string = xstrdup (func_name);
2169
2170 if (overlay_debugging == ovly_auto)
2171 {
2172 b->enable_state = bp_enabled;
2173 overlay_events_enabled = 1;
2174 }
2175 else
2176 {
2177 b->enable_state = bp_disabled;
2178 overlay_events_enabled = 0;
2179 }
2180 }
2181 update_global_location_list (1);
2182 }
2183
2184 static void
2185 create_longjmp_master_breakpoint (void)
2186 {
2187 struct program_space *pspace;
2188 struct cleanup *old_chain;
2189
2190 old_chain = save_current_program_space ();
2191
2192 ALL_PSPACES (pspace)
2193 {
2194 struct objfile *objfile;
2195
2196 set_current_program_space (pspace);
2197
2198 ALL_OBJFILES (objfile)
2199 {
2200 int i;
2201 struct gdbarch *gdbarch;
2202 struct breakpoint_objfile_data *bp_objfile_data;
2203
2204 gdbarch = get_objfile_arch (objfile);
2205 if (!gdbarch_get_longjmp_target_p (gdbarch))
2206 continue;
2207
2208 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2209
2210 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
2211 {
2212 struct breakpoint *b;
2213 const char *func_name;
2214 CORE_ADDR addr;
2215
2216 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
2217 continue;
2218
2219 func_name = longjmp_names[i];
2220 if (bp_objfile_data->longjmp_msym[i] == NULL)
2221 {
2222 struct minimal_symbol *m;
2223
2224 m = lookup_minimal_symbol_text (func_name, objfile);
2225 if (m == NULL)
2226 {
2227 /* Prevent future lookups in this objfile. */
2228 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2229 continue;
2230 }
2231 bp_objfile_data->longjmp_msym[i] = m;
2232 }
2233
2234 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
2235 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
2236 b->addr_string = xstrdup (func_name);
2237 b->enable_state = bp_disabled;
2238 }
2239 }
2240 }
2241 update_global_location_list (1);
2242
2243 do_cleanups (old_chain);
2244 }
2245
2246 /* Create a master std::terminate breakpoint. */
2247 static void
2248 create_std_terminate_master_breakpoint (void)
2249 {
2250 struct program_space *pspace;
2251 struct cleanup *old_chain;
2252 const char *const func_name = "std::terminate()";
2253
2254 old_chain = save_current_program_space ();
2255
2256 ALL_PSPACES (pspace)
2257 {
2258 struct objfile *objfile;
2259 CORE_ADDR addr;
2260
2261 set_current_program_space (pspace);
2262
2263 ALL_OBJFILES (objfile)
2264 {
2265 struct breakpoint *b;
2266 struct breakpoint_objfile_data *bp_objfile_data;
2267
2268 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2269
2270 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2271 continue;
2272
2273 if (bp_objfile_data->terminate_msym == NULL)
2274 {
2275 struct minimal_symbol *m;
2276
2277 m = lookup_minimal_symbol (func_name, NULL, objfile);
2278 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2279 && MSYMBOL_TYPE (m) != mst_file_text))
2280 {
2281 /* Prevent future lookups in this objfile. */
2282 bp_objfile_data->terminate_msym = &msym_not_found;
2283 continue;
2284 }
2285 bp_objfile_data->terminate_msym = m;
2286 }
2287
2288 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2289 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2290 bp_std_terminate_master);
2291 b->addr_string = xstrdup (func_name);
2292 b->enable_state = bp_disabled;
2293 }
2294 }
2295
2296 update_global_location_list (1);
2297
2298 do_cleanups (old_chain);
2299 }
2300
2301 /* Install a master breakpoint on the unwinder's debug hook. */
2302
2303 void
2304 create_exception_master_breakpoint (void)
2305 {
2306 struct objfile *objfile;
2307 const char *const func_name = "_Unwind_DebugHook";
2308
2309 ALL_OBJFILES (objfile)
2310 {
2311 struct breakpoint *b;
2312 struct gdbarch *gdbarch;
2313 struct breakpoint_objfile_data *bp_objfile_data;
2314 CORE_ADDR addr;
2315
2316 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2317
2318 if (msym_not_found_p (bp_objfile_data->exception_msym))
2319 continue;
2320
2321 gdbarch = get_objfile_arch (objfile);
2322
2323 if (bp_objfile_data->exception_msym == NULL)
2324 {
2325 struct minimal_symbol *debug_hook;
2326
2327 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2328 if (debug_hook == NULL)
2329 {
2330 bp_objfile_data->exception_msym = &msym_not_found;
2331 continue;
2332 }
2333
2334 bp_objfile_data->exception_msym = debug_hook;
2335 }
2336
2337 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2338 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2339 &current_target);
2340 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
2341 b->addr_string = xstrdup (func_name);
2342 b->enable_state = bp_disabled;
2343 }
2344
2345 update_global_location_list (1);
2346 }
2347
2348 void
2349 update_breakpoints_after_exec (void)
2350 {
2351 struct breakpoint *b, *b_tmp;
2352 struct bp_location *bploc, **bplocp_tmp;
2353
2354 /* We're about to delete breakpoints from GDB's lists. If the
2355 INSERTED flag is true, GDB will try to lift the breakpoints by
2356 writing the breakpoints' "shadow contents" back into memory. The
2357 "shadow contents" are NOT valid after an exec, so GDB should not
2358 do that. Instead, the target is responsible from marking
2359 breakpoints out as soon as it detects an exec. We don't do that
2360 here instead, because there may be other attempts to delete
2361 breakpoints after detecting an exec and before reaching here. */
2362 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
2363 if (bploc->pspace == current_program_space)
2364 gdb_assert (!bploc->inserted);
2365
2366 ALL_BREAKPOINTS_SAFE (b, b_tmp)
2367 {
2368 if (b->pspace != current_program_space)
2369 continue;
2370
2371 /* Solib breakpoints must be explicitly reset after an exec(). */
2372 if (b->type == bp_shlib_event)
2373 {
2374 delete_breakpoint (b);
2375 continue;
2376 }
2377
2378 /* JIT breakpoints must be explicitly reset after an exec(). */
2379 if (b->type == bp_jit_event)
2380 {
2381 delete_breakpoint (b);
2382 continue;
2383 }
2384
2385 /* Thread event breakpoints must be set anew after an exec(),
2386 as must overlay event and longjmp master breakpoints. */
2387 if (b->type == bp_thread_event || b->type == bp_overlay_event
2388 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2389 || b->type == bp_exception_master)
2390 {
2391 delete_breakpoint (b);
2392 continue;
2393 }
2394
2395 /* Step-resume breakpoints are meaningless after an exec(). */
2396 if (b->type == bp_step_resume)
2397 {
2398 delete_breakpoint (b);
2399 continue;
2400 }
2401
2402 /* Longjmp and longjmp-resume breakpoints are also meaningless
2403 after an exec. */
2404 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2405 || b->type == bp_exception || b->type == bp_exception_resume)
2406 {
2407 delete_breakpoint (b);
2408 continue;
2409 }
2410
2411 if (b->type == bp_catchpoint)
2412 {
2413 /* For now, none of the bp_catchpoint breakpoints need to
2414 do anything at this point. In the future, if some of
2415 the catchpoints need to something, we will need to add
2416 a new method, and call this method from here. */
2417 continue;
2418 }
2419
2420 /* bp_finish is a special case. The only way we ought to be able
2421 to see one of these when an exec() has happened, is if the user
2422 caught a vfork, and then said "finish". Ordinarily a finish just
2423 carries them to the call-site of the current callee, by setting
2424 a temporary bp there and resuming. But in this case, the finish
2425 will carry them entirely through the vfork & exec.
2426
2427 We don't want to allow a bp_finish to remain inserted now. But
2428 we can't safely delete it, 'cause finish_command has a handle to
2429 the bp on a bpstat, and will later want to delete it. There's a
2430 chance (and I've seen it happen) that if we delete the bp_finish
2431 here, that its storage will get reused by the time finish_command
2432 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2433 We really must allow finish_command to delete a bp_finish.
2434
2435 In the absense of a general solution for the "how do we know
2436 it's safe to delete something others may have handles to?"
2437 problem, what we'll do here is just uninsert the bp_finish, and
2438 let finish_command delete it.
2439
2440 (We know the bp_finish is "doomed" in the sense that it's
2441 momentary, and will be deleted as soon as finish_command sees
2442 the inferior stopped. So it doesn't matter that the bp's
2443 address is probably bogus in the new a.out, unlike e.g., the
2444 solib breakpoints.) */
2445
2446 if (b->type == bp_finish)
2447 {
2448 continue;
2449 }
2450
2451 /* Without a symbolic address, we have little hope of the
2452 pre-exec() address meaning the same thing in the post-exec()
2453 a.out. */
2454 if (b->addr_string == NULL)
2455 {
2456 delete_breakpoint (b);
2457 continue;
2458 }
2459 }
2460 /* FIXME what about longjmp breakpoints? Re-create them here? */
2461 create_overlay_event_breakpoint ();
2462 create_longjmp_master_breakpoint ();
2463 create_std_terminate_master_breakpoint ();
2464 create_exception_master_breakpoint ();
2465 }
2466
2467 int
2468 detach_breakpoints (int pid)
2469 {
2470 struct bp_location *bl, **blp_tmp;
2471 int val = 0;
2472 struct cleanup *old_chain = save_inferior_ptid ();
2473 struct inferior *inf = current_inferior ();
2474
2475 if (pid == PIDGET (inferior_ptid))
2476 error (_("Cannot detach breakpoints of inferior_ptid"));
2477
2478 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
2479 inferior_ptid = pid_to_ptid (pid);
2480 ALL_BP_LOCATIONS (bl, blp_tmp)
2481 {
2482 if (bl->pspace != inf->pspace)
2483 continue;
2484
2485 if (bl->inserted)
2486 val |= remove_breakpoint_1 (bl, mark_inserted);
2487 }
2488
2489 /* Detach single-step breakpoints as well. */
2490 detach_single_step_breakpoints ();
2491
2492 do_cleanups (old_chain);
2493 return val;
2494 }
2495
2496 /* Remove the breakpoint location BL from the current address space.
2497 Note that this is used to detach breakpoints from a child fork.
2498 When we get here, the child isn't in the inferior list, and neither
2499 do we have objects to represent its address space --- we should
2500 *not* look at bl->pspace->aspace here. */
2501
2502 static int
2503 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
2504 {
2505 int val;
2506
2507 /* BL is never in moribund_locations by our callers. */
2508 gdb_assert (bl->owner != NULL);
2509
2510 if (bl->owner->enable_state == bp_permanent)
2511 /* Permanent breakpoints cannot be inserted or removed. */
2512 return 0;
2513
2514 /* The type of none suggests that owner is actually deleted.
2515 This should not ever happen. */
2516 gdb_assert (bl->owner->type != bp_none);
2517
2518 if (bl->loc_type == bp_loc_software_breakpoint
2519 || bl->loc_type == bp_loc_hardware_breakpoint)
2520 {
2521 /* "Normal" instruction breakpoint: either the standard
2522 trap-instruction bp (bp_breakpoint), or a
2523 bp_hardware_breakpoint. */
2524
2525 /* First check to see if we have to handle an overlay. */
2526 if (overlay_debugging == ovly_off
2527 || bl->section == NULL
2528 || !(section_is_overlay (bl->section)))
2529 {
2530 /* No overlay handling: just remove the breakpoint. */
2531
2532 if (bl->loc_type == bp_loc_hardware_breakpoint)
2533 val = target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
2534 else
2535 val = target_remove_breakpoint (bl->gdbarch, &bl->target_info);
2536 }
2537 else
2538 {
2539 /* This breakpoint is in an overlay section.
2540 Did we set a breakpoint at the LMA? */
2541 if (!overlay_events_enabled)
2542 {
2543 /* Yes -- overlay event support is not active, so we
2544 should have set a breakpoint at the LMA. Remove it.
2545 */
2546 /* Ignore any failures: if the LMA is in ROM, we will
2547 have already warned when we failed to insert it. */
2548 if (bl->loc_type == bp_loc_hardware_breakpoint)
2549 target_remove_hw_breakpoint (bl->gdbarch,
2550 &bl->overlay_target_info);
2551 else
2552 target_remove_breakpoint (bl->gdbarch,
2553 &bl->overlay_target_info);
2554 }
2555 /* Did we set a breakpoint at the VMA?
2556 If so, we will have marked the breakpoint 'inserted'. */
2557 if (bl->inserted)
2558 {
2559 /* Yes -- remove it. Previously we did not bother to
2560 remove the breakpoint if the section had been
2561 unmapped, but let's not rely on that being safe. We
2562 don't know what the overlay manager might do. */
2563 if (bl->loc_type == bp_loc_hardware_breakpoint)
2564 val = target_remove_hw_breakpoint (bl->gdbarch,
2565 &bl->target_info);
2566
2567 /* However, we should remove *software* breakpoints only
2568 if the section is still mapped, or else we overwrite
2569 wrong code with the saved shadow contents. */
2570 else if (section_is_mapped (bl->section))
2571 val = target_remove_breakpoint (bl->gdbarch,
2572 &bl->target_info);
2573 else
2574 val = 0;
2575 }
2576 else
2577 {
2578 /* No -- not inserted, so no need to remove. No error. */
2579 val = 0;
2580 }
2581 }
2582
2583 /* In some cases, we might not be able to remove a breakpoint
2584 in a shared library that has already been removed, but we
2585 have not yet processed the shlib unload event. */
2586 if (val && solib_name_from_address (bl->pspace, bl->address))
2587 val = 0;
2588
2589 if (val)
2590 return val;
2591 bl->inserted = (is == mark_inserted);
2592 }
2593 else if (bl->loc_type == bp_loc_hardware_watchpoint)
2594 {
2595 gdb_assert (bl->owner->ops != NULL
2596 && bl->owner->ops->remove_location != NULL);
2597
2598 bl->inserted = (is == mark_inserted);
2599 bl->owner->ops->remove_location (bl);
2600
2601 /* Failure to remove any of the hardware watchpoints comes here. */
2602 if ((is == mark_uninserted) && (bl->inserted))
2603 warning (_("Could not remove hardware watchpoint %d."),
2604 bl->owner->number);
2605 }
2606 else if (bl->owner->type == bp_catchpoint
2607 && breakpoint_enabled (bl->owner)
2608 && !bl->duplicate)
2609 {
2610 gdb_assert (bl->owner->ops != NULL
2611 && bl->owner->ops->remove_location != NULL);
2612
2613 val = bl->owner->ops->remove_location (bl);
2614 if (val)
2615 return val;
2616
2617 bl->inserted = (is == mark_inserted);
2618 }
2619
2620 return 0;
2621 }
2622
2623 static int
2624 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
2625 {
2626 int ret;
2627 struct cleanup *old_chain;
2628
2629 /* BL is never in moribund_locations by our callers. */
2630 gdb_assert (bl->owner != NULL);
2631
2632 if (bl->owner->enable_state == bp_permanent)
2633 /* Permanent breakpoints cannot be inserted or removed. */
2634 return 0;
2635
2636 /* The type of none suggests that owner is actually deleted.
2637 This should not ever happen. */
2638 gdb_assert (bl->owner->type != bp_none);
2639
2640 old_chain = save_current_space_and_thread ();
2641
2642 switch_to_program_space_and_thread (bl->pspace);
2643
2644 ret = remove_breakpoint_1 (bl, is);
2645
2646 do_cleanups (old_chain);
2647 return ret;
2648 }
2649
2650 /* Clear the "inserted" flag in all breakpoints. */
2651
2652 void
2653 mark_breakpoints_out (void)
2654 {
2655 struct bp_location *bl, **blp_tmp;
2656
2657 ALL_BP_LOCATIONS (bl, blp_tmp)
2658 if (bl->pspace == current_program_space)
2659 bl->inserted = 0;
2660 }
2661
2662 /* Clear the "inserted" flag in all breakpoints and delete any
2663 breakpoints which should go away between runs of the program.
2664
2665 Plus other such housekeeping that has to be done for breakpoints
2666 between runs.
2667
2668 Note: this function gets called at the end of a run (by
2669 generic_mourn_inferior) and when a run begins (by
2670 init_wait_for_inferior). */
2671
2672
2673
2674 void
2675 breakpoint_init_inferior (enum inf_context context)
2676 {
2677 struct breakpoint *b, *b_tmp;
2678 struct bp_location *bl, **blp_tmp;
2679 int ix;
2680 struct program_space *pspace = current_program_space;
2681
2682 /* If breakpoint locations are shared across processes, then there's
2683 nothing to do. */
2684 if (gdbarch_has_global_breakpoints (target_gdbarch))
2685 return;
2686
2687 ALL_BP_LOCATIONS (bl, blp_tmp)
2688 {
2689 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2690 if (bl->pspace == pspace
2691 && bl->owner->enable_state != bp_permanent)
2692 bl->inserted = 0;
2693 }
2694
2695 ALL_BREAKPOINTS_SAFE (b, b_tmp)
2696 {
2697 if (b->loc && b->loc->pspace != pspace)
2698 continue;
2699
2700 switch (b->type)
2701 {
2702 case bp_call_dummy:
2703
2704 /* If the call dummy breakpoint is at the entry point it will
2705 cause problems when the inferior is rerun, so we better get
2706 rid of it. */
2707
2708 case bp_watchpoint_scope:
2709
2710 /* Also get rid of scope breakpoints. */
2711
2712 case bp_shlib_event:
2713
2714 /* Also remove solib event breakpoints. Their addresses may
2715 have changed since the last time we ran the program.
2716 Actually we may now be debugging against different target;
2717 and so the solib backend that installed this breakpoint may
2718 not be used in by the target. E.g.,
2719
2720 (gdb) file prog-linux
2721 (gdb) run # native linux target
2722 ...
2723 (gdb) kill
2724 (gdb) file prog-win.exe
2725 (gdb) tar rem :9999 # remote Windows gdbserver.
2726 */
2727
2728 delete_breakpoint (b);
2729 break;
2730
2731 case bp_watchpoint:
2732 case bp_hardware_watchpoint:
2733 case bp_read_watchpoint:
2734 case bp_access_watchpoint:
2735
2736 /* Likewise for watchpoints on local expressions. */
2737 if (b->exp_valid_block != NULL)
2738 delete_breakpoint (b);
2739 else if (context == inf_starting)
2740 {
2741 /* Reset val field to force reread of starting value in
2742 insert_breakpoints. */
2743 if (b->val)
2744 value_free (b->val);
2745 b->val = NULL;
2746 b->val_valid = 0;
2747 }
2748 break;
2749 default:
2750 break;
2751 }
2752 }
2753
2754 /* Get rid of the moribund locations. */
2755 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2756 decref_bp_location (&bl);
2757 VEC_free (bp_location_p, moribund_locations);
2758 }
2759
2760 /* These functions concern about actual breakpoints inserted in the
2761 target --- to e.g. check if we need to do decr_pc adjustment or if
2762 we need to hop over the bkpt --- so we check for address space
2763 match, not program space. */
2764
2765 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2766 exists at PC. It returns ordinary_breakpoint_here if it's an
2767 ordinary breakpoint, or permanent_breakpoint_here if it's a
2768 permanent breakpoint.
2769 - When continuing from a location with an ordinary breakpoint, we
2770 actually single step once before calling insert_breakpoints.
2771 - When continuing from a localion with a permanent breakpoint, we
2772 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2773 the target, to advance the PC past the breakpoint. */
2774
2775 enum breakpoint_here
2776 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
2777 {
2778 struct bp_location *bl, **blp_tmp;
2779 int any_breakpoint_here = 0;
2780
2781 ALL_BP_LOCATIONS (bl, blp_tmp)
2782 {
2783 if (bl->loc_type != bp_loc_software_breakpoint
2784 && bl->loc_type != bp_loc_hardware_breakpoint)
2785 continue;
2786
2787 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2788 if ((breakpoint_enabled (bl->owner)
2789 || bl->owner->enable_state == bp_permanent)
2790 && breakpoint_address_match (bl->pspace->aspace, bl->address,
2791 aspace, pc))
2792 {
2793 if (overlay_debugging
2794 && section_is_overlay (bl->section)
2795 && !section_is_mapped (bl->section))
2796 continue; /* unmapped overlay -- can't be a match */
2797 else if (bl->owner->enable_state == bp_permanent)
2798 return permanent_breakpoint_here;
2799 else
2800 any_breakpoint_here = 1;
2801 }
2802 }
2803
2804 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
2805 }
2806
2807 /* Return true if there's a moribund breakpoint at PC. */
2808
2809 int
2810 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
2811 {
2812 struct bp_location *loc;
2813 int ix;
2814
2815 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
2816 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
2817 aspace, pc))
2818 return 1;
2819
2820 return 0;
2821 }
2822
2823 /* Returns non-zero if there's a breakpoint inserted at PC, which is
2824 inserted using regular breakpoint_chain / bp_location array
2825 mechanism. This does not check for single-step breakpoints, which
2826 are inserted and removed using direct target manipulation. */
2827
2828 int
2829 regular_breakpoint_inserted_here_p (struct address_space *aspace,
2830 CORE_ADDR pc)
2831 {
2832 struct bp_location *bl, **blp_tmp;
2833
2834 ALL_BP_LOCATIONS (bl, blp_tmp)
2835 {
2836 if (bl->loc_type != bp_loc_software_breakpoint
2837 && bl->loc_type != bp_loc_hardware_breakpoint)
2838 continue;
2839
2840 if (bl->inserted
2841 && breakpoint_address_match (bl->pspace->aspace, bl->address,
2842 aspace, pc))
2843 {
2844 if (overlay_debugging
2845 && section_is_overlay (bl->section)
2846 && !section_is_mapped (bl->section))
2847 continue; /* unmapped overlay -- can't be a match */
2848 else
2849 return 1;
2850 }
2851 }
2852 return 0;
2853 }
2854
2855 /* Returns non-zero iff there's either regular breakpoint
2856 or a single step breakpoint inserted at PC. */
2857
2858 int
2859 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
2860 {
2861 if (regular_breakpoint_inserted_here_p (aspace, pc))
2862 return 1;
2863
2864 if (single_step_breakpoint_inserted_here_p (aspace, pc))
2865 return 1;
2866
2867 return 0;
2868 }
2869
2870 /* This function returns non-zero iff there is a software breakpoint
2871 inserted at PC. */
2872
2873 int
2874 software_breakpoint_inserted_here_p (struct address_space *aspace,
2875 CORE_ADDR pc)
2876 {
2877 struct bp_location *bl, **blp_tmp;
2878
2879 ALL_BP_LOCATIONS (bl, blp_tmp)
2880 {
2881 if (bl->loc_type != bp_loc_software_breakpoint)
2882 continue;
2883
2884 if (bl->inserted
2885 && breakpoint_address_match (bl->pspace->aspace, bl->address,
2886 aspace, pc))
2887 {
2888 if (overlay_debugging
2889 && section_is_overlay (bl->section)
2890 && !section_is_mapped (bl->section))
2891 continue; /* unmapped overlay -- can't be a match */
2892 else
2893 return 1;
2894 }
2895 }
2896
2897 /* Also check for software single-step breakpoints. */
2898 if (single_step_breakpoint_inserted_here_p (aspace, pc))
2899 return 1;
2900
2901 return 0;
2902 }
2903
2904 int
2905 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2906 CORE_ADDR addr, ULONGEST len)
2907 {
2908 struct breakpoint *bpt;
2909
2910 ALL_BREAKPOINTS (bpt)
2911 {
2912 struct bp_location *loc;
2913
2914 if (bpt->type != bp_hardware_watchpoint
2915 && bpt->type != bp_access_watchpoint)
2916 continue;
2917
2918 if (!breakpoint_enabled (bpt))
2919 continue;
2920
2921 for (loc = bpt->loc; loc; loc = loc->next)
2922 if (loc->pspace->aspace == aspace && loc->inserted)
2923 {
2924 CORE_ADDR l, h;
2925
2926 /* Check for intersection. */
2927 l = max (loc->address, addr);
2928 h = min (loc->address + loc->length, addr + len);
2929 if (l < h)
2930 return 1;
2931 }
2932 }
2933 return 0;
2934 }
2935
2936 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2937 PC is valid for process/thread PTID. */
2938
2939 int
2940 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2941 ptid_t ptid)
2942 {
2943 struct bp_location *bl, **blp_tmp;
2944 /* The thread and task IDs associated to PTID, computed lazily. */
2945 int thread = -1;
2946 int task = 0;
2947
2948 ALL_BP_LOCATIONS (bl, blp_tmp)
2949 {
2950 if (bl->loc_type != bp_loc_software_breakpoint
2951 && bl->loc_type != bp_loc_hardware_breakpoint)
2952 continue;
2953
2954 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2955 if (!breakpoint_enabled (bl->owner)
2956 && bl->owner->enable_state != bp_permanent)
2957 continue;
2958
2959 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
2960 aspace, pc))
2961 continue;
2962
2963 if (bl->owner->thread != -1)
2964 {
2965 /* This is a thread-specific breakpoint. Check that ptid
2966 matches that thread. If thread hasn't been computed yet,
2967 it is now time to do so. */
2968 if (thread == -1)
2969 thread = pid_to_thread_id (ptid);
2970 if (bl->owner->thread != thread)
2971 continue;
2972 }
2973
2974 if (bl->owner->task != 0)
2975 {
2976 /* This is a task-specific breakpoint. Check that ptid
2977 matches that task. If task hasn't been computed yet,
2978 it is now time to do so. */
2979 if (task == 0)
2980 task = ada_get_task_number (ptid);
2981 if (bl->owner->task != task)
2982 continue;
2983 }
2984
2985 if (overlay_debugging
2986 && section_is_overlay (bl->section)
2987 && !section_is_mapped (bl->section))
2988 continue; /* unmapped overlay -- can't be a match */
2989
2990 return 1;
2991 }
2992
2993 return 0;
2994 }
2995 \f
2996
2997 /* bpstat stuff. External routines' interfaces are documented
2998 in breakpoint.h. */
2999
3000 int
3001 ep_is_catchpoint (struct breakpoint *ep)
3002 {
3003 return (ep->type == bp_catchpoint);
3004 }
3005
3006 /* Frees any storage that is part of a bpstat. Does not walk the
3007 'next' chain. */
3008
3009 static void
3010 bpstat_free (bpstat bs)
3011 {
3012 if (bs->old_val != NULL)
3013 value_free (bs->old_val);
3014 decref_counted_command_line (&bs->commands);
3015 decref_bp_location (&bs->bp_location_at);
3016 xfree (bs);
3017 }
3018
3019 /* Clear a bpstat so that it says we are not at any breakpoint.
3020 Also free any storage that is part of a bpstat. */
3021
3022 void
3023 bpstat_clear (bpstat *bsp)
3024 {
3025 bpstat p;
3026 bpstat q;
3027
3028 if (bsp == 0)
3029 return;
3030 p = *bsp;
3031 while (p != NULL)
3032 {
3033 q = p->next;
3034 bpstat_free (p);
3035 p = q;
3036 }
3037 *bsp = NULL;
3038 }
3039
3040 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3041 is part of the bpstat is copied as well. */
3042
3043 bpstat
3044 bpstat_copy (bpstat bs)
3045 {
3046 bpstat p = NULL;
3047 bpstat tmp;
3048 bpstat retval = NULL;
3049
3050 if (bs == NULL)
3051 return bs;
3052
3053 for (; bs != NULL; bs = bs->next)
3054 {
3055 tmp = (bpstat) xmalloc (sizeof (*tmp));
3056 memcpy (tmp, bs, sizeof (*tmp));
3057 incref_counted_command_line (tmp->commands);
3058 incref_bp_location (tmp->bp_location_at);
3059 if (bs->old_val != NULL)
3060 {
3061 tmp->old_val = value_copy (bs->old_val);
3062 release_value (tmp->old_val);
3063 }
3064
3065 if (p == NULL)
3066 /* This is the first thing in the chain. */
3067 retval = tmp;
3068 else
3069 p->next = tmp;
3070 p = tmp;
3071 }
3072 p->next = NULL;
3073 return retval;
3074 }
3075
3076 /* Find the bpstat associated with this breakpoint. */
3077
3078 bpstat
3079 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
3080 {
3081 if (bsp == NULL)
3082 return NULL;
3083
3084 for (; bsp != NULL; bsp = bsp->next)
3085 {
3086 if (bsp->breakpoint_at == breakpoint)
3087 return bsp;
3088 }
3089 return NULL;
3090 }
3091
3092 /* Put in *NUM the breakpoint number of the first breakpoint we are
3093 stopped at. *BSP upon return is a bpstat which points to the
3094 remaining breakpoints stopped at (but which is not guaranteed to be
3095 good for anything but further calls to bpstat_num).
3096
3097 Return 0 if passed a bpstat which does not indicate any breakpoints.
3098 Return -1 if stopped at a breakpoint that has been deleted since
3099 we set it.
3100 Return 1 otherwise. */
3101
3102 int
3103 bpstat_num (bpstat *bsp, int *num)
3104 {
3105 struct breakpoint *b;
3106
3107 if ((*bsp) == NULL)
3108 return 0; /* No more breakpoint values */
3109
3110 /* We assume we'll never have several bpstats that correspond to a
3111 single breakpoint -- otherwise, this function might return the
3112 same number more than once and this will look ugly. */
3113 b = (*bsp)->breakpoint_at;
3114 *bsp = (*bsp)->next;
3115 if (b == NULL)
3116 return -1; /* breakpoint that's been deleted since */
3117
3118 *num = b->number; /* We have its number */
3119 return 1;
3120 }
3121
3122 /* Modify BS so that the actions will not be performed. */
3123
3124 void
3125 bpstat_clear_actions (bpstat bs)
3126 {
3127 for (; bs != NULL; bs = bs->next)
3128 {
3129 decref_counted_command_line (&bs->commands);
3130 bs->commands_left = NULL;
3131 if (bs->old_val != NULL)
3132 {
3133 value_free (bs->old_val);
3134 bs->old_val = NULL;
3135 }
3136 }
3137 }
3138
3139 /* Called when a command is about to proceed the inferior. */
3140
3141 static void
3142 breakpoint_about_to_proceed (void)
3143 {
3144 if (!ptid_equal (inferior_ptid, null_ptid))
3145 {
3146 struct thread_info *tp = inferior_thread ();
3147
3148 /* Allow inferior function calls in breakpoint commands to not
3149 interrupt the command list. When the call finishes
3150 successfully, the inferior will be standing at the same
3151 breakpoint as if nothing happened. */
3152 if (tp->control.in_infcall)
3153 return;
3154 }
3155
3156 breakpoint_proceeded = 1;
3157 }
3158
3159 /* Stub for cleaning up our state if we error-out of a breakpoint
3160 command. */
3161 static void
3162 cleanup_executing_breakpoints (void *ignore)
3163 {
3164 executing_breakpoint_commands = 0;
3165 }
3166
3167 /* Execute all the commands associated with all the breakpoints at
3168 this location. Any of these commands could cause the process to
3169 proceed beyond this point, etc. We look out for such changes by
3170 checking the global "breakpoint_proceeded" after each command.
3171
3172 Returns true if a breakpoint command resumed the inferior. In that
3173 case, it is the caller's responsibility to recall it again with the
3174 bpstat of the current thread. */
3175
3176 static int
3177 bpstat_do_actions_1 (bpstat *bsp)
3178 {
3179 bpstat bs;
3180 struct cleanup *old_chain;
3181 int again = 0;
3182
3183 /* Avoid endless recursion if a `source' command is contained
3184 in bs->commands. */
3185 if (executing_breakpoint_commands)
3186 return 0;
3187
3188 executing_breakpoint_commands = 1;
3189 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3190
3191 /* This pointer will iterate over the list of bpstat's. */
3192 bs = *bsp;
3193
3194 breakpoint_proceeded = 0;
3195 for (; bs != NULL; bs = bs->next)
3196 {
3197 struct counted_command_line *ccmd;
3198 struct command_line *cmd;
3199 struct cleanup *this_cmd_tree_chain;
3200
3201 /* Take ownership of the BSP's command tree, if it has one.
3202
3203 The command tree could legitimately contain commands like
3204 'step' and 'next', which call clear_proceed_status, which
3205 frees stop_bpstat's command tree. To make sure this doesn't
3206 free the tree we're executing out from under us, we need to
3207 take ownership of the tree ourselves. Since a given bpstat's
3208 commands are only executed once, we don't need to copy it; we
3209 can clear the pointer in the bpstat, and make sure we free
3210 the tree when we're done. */
3211 ccmd = bs->commands;
3212 bs->commands = NULL;
3213 this_cmd_tree_chain
3214 = make_cleanup_decref_counted_command_line (&ccmd);
3215 cmd = bs->commands_left;
3216 bs->commands_left = NULL;
3217
3218 while (cmd != NULL)
3219 {
3220 execute_control_command (cmd);
3221
3222 if (breakpoint_proceeded)
3223 break;
3224 else
3225 cmd = cmd->next;
3226 }
3227
3228 /* We can free this command tree now. */
3229 do_cleanups (this_cmd_tree_chain);
3230
3231 if (breakpoint_proceeded)
3232 {
3233 if (target_can_async_p ())
3234 /* If we are in async mode, then the target might be still
3235 running, not stopped at any breakpoint, so nothing for
3236 us to do here -- just return to the event loop. */
3237 ;
3238 else
3239 /* In sync mode, when execute_control_command returns
3240 we're already standing on the next breakpoint.
3241 Breakpoint commands for that stop were not run, since
3242 execute_command does not run breakpoint commands --
3243 only command_line_handler does, but that one is not
3244 involved in execution of breakpoint commands. So, we
3245 can now execute breakpoint commands. It should be
3246 noted that making execute_command do bpstat actions is
3247 not an option -- in this case we'll have recursive
3248 invocation of bpstat for each breakpoint with a
3249 command, and can easily blow up GDB stack. Instead, we
3250 return true, which will trigger the caller to recall us
3251 with the new stop_bpstat. */
3252 again = 1;
3253 break;
3254 }
3255 }
3256 do_cleanups (old_chain);
3257 return again;
3258 }
3259
3260 void
3261 bpstat_do_actions (void)
3262 {
3263 /* Do any commands attached to breakpoint we are stopped at. */
3264 while (!ptid_equal (inferior_ptid, null_ptid)
3265 && target_has_execution
3266 && !is_exited (inferior_ptid)
3267 && !is_executing (inferior_ptid))
3268 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3269 and only return when it is stopped at the next breakpoint, we
3270 keep doing breakpoint actions until it returns false to
3271 indicate the inferior was not resumed. */
3272 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
3273 break;
3274 }
3275
3276 /* Print out the (old or new) value associated with a watchpoint. */
3277
3278 static void
3279 watchpoint_value_print (struct value *val, struct ui_file *stream)
3280 {
3281 if (val == NULL)
3282 fprintf_unfiltered (stream, _("<unreadable>"));
3283 else
3284 {
3285 struct value_print_options opts;
3286 get_user_print_options (&opts);
3287 value_print (val, stream, &opts);
3288 }
3289 }
3290
3291 /* This is the normal print function for a bpstat. In the future,
3292 much of this logic could (should?) be moved to bpstat_stop_status,
3293 by having it set different print_it values.
3294
3295 Current scheme: When we stop, bpstat_print() is called. It loops
3296 through the bpstat list of things causing this stop, calling the
3297 print_bp_stop_message function on each one. The behavior of the
3298 print_bp_stop_message function depends on the print_it field of
3299 bpstat. If such field so indicates, call this function here.
3300
3301 Return values from this routine (ultimately used by bpstat_print()
3302 and normal_stop() to decide what to do):
3303 PRINT_NOTHING: Means we already printed all we needed to print,
3304 don't print anything else.
3305 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
3306 that something to be followed by a location.
3307 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
3308 that something to be followed by a location.
3309 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
3310 analysis. */
3311
3312 static enum print_stop_action
3313 print_it_typical (bpstat bs)
3314 {
3315 struct cleanup *old_chain;
3316 struct breakpoint *b;
3317 const struct bp_location *bl;
3318 struct ui_stream *stb;
3319 int bp_temp = 0;
3320 enum print_stop_action result;
3321
3322 gdb_assert (bs->bp_location_at != NULL);
3323
3324 bl = bs->bp_location_at;
3325 b = bs->breakpoint_at;
3326
3327 stb = ui_out_stream_new (uiout);
3328 old_chain = make_cleanup_ui_out_stream_delete (stb);
3329
3330 switch (b->type)
3331 {
3332 case bp_breakpoint:
3333 case bp_hardware_breakpoint:
3334 bp_temp = b->disposition == disp_del;
3335 if (bl->address != bl->requested_address)
3336 breakpoint_adjustment_warning (bl->requested_address,
3337 bl->address,
3338 b->number, 1);
3339 annotate_breakpoint (b->number);
3340 if (bp_temp)
3341 ui_out_text (uiout, "\nTemporary breakpoint ");
3342 else
3343 ui_out_text (uiout, "\nBreakpoint ");
3344 if (ui_out_is_mi_like_p (uiout))
3345 {
3346 ui_out_field_string (uiout, "reason",
3347 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
3348 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3349 }
3350 ui_out_field_int (uiout, "bkptno", b->number);
3351 ui_out_text (uiout, ", ");
3352 result = PRINT_SRC_AND_LOC;
3353 break;
3354
3355 case bp_shlib_event:
3356 /* Did we stop because the user set the stop_on_solib_events
3357 variable? (If so, we report this as a generic, "Stopped due
3358 to shlib event" message.) */
3359 printf_filtered (_("Stopped due to shared library event\n"));
3360 result = PRINT_NOTHING;
3361 break;
3362
3363 case bp_thread_event:
3364 /* Not sure how we will get here.
3365 GDB should not stop for these breakpoints. */
3366 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
3367 result = PRINT_NOTHING;
3368 break;
3369
3370 case bp_overlay_event:
3371 /* By analogy with the thread event, GDB should not stop for these. */
3372 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
3373 result = PRINT_NOTHING;
3374 break;
3375
3376 case bp_longjmp_master:
3377 /* These should never be enabled. */
3378 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
3379 result = PRINT_NOTHING;
3380 break;
3381
3382 case bp_std_terminate_master:
3383 /* These should never be enabled. */
3384 printf_filtered (_("std::terminate Master Breakpoint: "
3385 "gdb should not stop!\n"));
3386 result = PRINT_NOTHING;
3387 break;
3388
3389 case bp_exception_master:
3390 /* These should never be enabled. */
3391 printf_filtered (_("Exception Master Breakpoint: "
3392 "gdb should not stop!\n"));
3393 result = PRINT_NOTHING;
3394 break;
3395
3396 case bp_watchpoint:
3397 case bp_hardware_watchpoint:
3398 annotate_watchpoint (b->number);
3399 if (ui_out_is_mi_like_p (uiout))
3400 ui_out_field_string
3401 (uiout, "reason",
3402 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
3403 mention (b);
3404 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3405 ui_out_text (uiout, "\nOld value = ");
3406 watchpoint_value_print (bs->old_val, stb->stream);
3407 ui_out_field_stream (uiout, "old", stb);
3408 ui_out_text (uiout, "\nNew value = ");
3409 watchpoint_value_print (b->val, stb->stream);
3410 ui_out_field_stream (uiout, "new", stb);
3411 ui_out_text (uiout, "\n");
3412 /* More than one watchpoint may have been triggered. */
3413 result = PRINT_UNKNOWN;
3414 break;
3415
3416 case bp_read_watchpoint:
3417 if (ui_out_is_mi_like_p (uiout))
3418 ui_out_field_string
3419 (uiout, "reason",
3420 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
3421 mention (b);
3422 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3423 ui_out_text (uiout, "\nValue = ");
3424 watchpoint_value_print (b->val, stb->stream);
3425 ui_out_field_stream (uiout, "value", stb);
3426 ui_out_text (uiout, "\n");
3427 result = PRINT_UNKNOWN;
3428 break;
3429
3430 case bp_access_watchpoint:
3431 if (bs->old_val != NULL)
3432 {
3433 annotate_watchpoint (b->number);
3434 if (ui_out_is_mi_like_p (uiout))
3435 ui_out_field_string
3436 (uiout, "reason",
3437 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
3438 mention (b);
3439 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3440 ui_out_text (uiout, "\nOld value = ");
3441 watchpoint_value_print (bs->old_val, stb->stream);
3442 ui_out_field_stream (uiout, "old", stb);
3443 ui_out_text (uiout, "\nNew value = ");
3444 }
3445 else
3446 {
3447 mention (b);
3448 if (ui_out_is_mi_like_p (uiout))
3449 ui_out_field_string
3450 (uiout, "reason",
3451 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
3452 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3453 ui_out_text (uiout, "\nValue = ");
3454 }
3455 watchpoint_value_print (b->val, stb->stream);
3456 ui_out_field_stream (uiout, "new", stb);
3457 ui_out_text (uiout, "\n");
3458 result = PRINT_UNKNOWN;
3459 break;
3460
3461 /* Fall through, we don't deal with these types of breakpoints
3462 here. */
3463
3464 case bp_finish:
3465 if (ui_out_is_mi_like_p (uiout))
3466 ui_out_field_string
3467 (uiout, "reason",
3468 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
3469 result = PRINT_UNKNOWN;
3470 break;
3471
3472 case bp_until:
3473 if (ui_out_is_mi_like_p (uiout))
3474 ui_out_field_string
3475 (uiout, "reason",
3476 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
3477 result = PRINT_UNKNOWN;
3478 break;
3479
3480 case bp_none:
3481 case bp_longjmp:
3482 case bp_longjmp_resume:
3483 case bp_exception:
3484 case bp_exception_resume:
3485 case bp_step_resume:
3486 case bp_watchpoint_scope:
3487 case bp_call_dummy:
3488 case bp_std_terminate:
3489 case bp_tracepoint:
3490 case bp_fast_tracepoint:
3491 case bp_jit_event:
3492 default:
3493 result = PRINT_UNKNOWN;
3494 break;
3495 }
3496
3497 do_cleanups (old_chain);
3498 return result;
3499 }
3500
3501 /* Generic routine for printing messages indicating why we
3502 stopped. The behavior of this function depends on the value
3503 'print_it' in the bpstat structure. Under some circumstances we
3504 may decide not to print anything here and delegate the task to
3505 normal_stop(). */
3506
3507 static enum print_stop_action
3508 print_bp_stop_message (bpstat bs)
3509 {
3510 switch (bs->print_it)
3511 {
3512 case print_it_noop:
3513 /* Nothing should be printed for this bpstat entry. */
3514 return PRINT_UNKNOWN;
3515 break;
3516
3517 case print_it_done:
3518 /* We still want to print the frame, but we already printed the
3519 relevant messages. */
3520 return PRINT_SRC_AND_LOC;
3521 break;
3522
3523 case print_it_normal:
3524 {
3525 struct breakpoint *b = bs->breakpoint_at;
3526
3527 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3528 which has since been deleted. */
3529 if (b == NULL)
3530 return PRINT_UNKNOWN;
3531
3532 /* Normal case. Call the breakpoint's print_it method, or
3533 print_it_typical. */
3534 if (b->ops != NULL && b->ops->print_it != NULL)
3535 return b->ops->print_it (b);
3536 else
3537 return print_it_typical (bs);
3538 }
3539 break;
3540
3541 default:
3542 internal_error (__FILE__, __LINE__,
3543 _("print_bp_stop_message: unrecognized enum value"));
3544 break;
3545 }
3546 }
3547
3548 /* Print a message indicating what happened. This is called from
3549 normal_stop(). The input to this routine is the head of the bpstat
3550 list - a list of the eventpoints that caused this stop. This
3551 routine calls the generic print routine for printing a message
3552 about reasons for stopping. This will print (for example) the
3553 "Breakpoint n," part of the output. The return value of this
3554 routine is one of:
3555
3556 PRINT_UNKNOWN: Means we printed nothing.
3557 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
3558 code to print the location. An example is
3559 "Breakpoint 1, " which should be followed by
3560 the location.
3561 PRINT_SRC_ONLY: Means we printed something, but there is no need
3562 to also print the location part of the message.
3563 An example is the catch/throw messages, which
3564 don't require a location appended to the end.
3565 PRINT_NOTHING: We have done some printing and we don't need any
3566 further info to be printed. */
3567
3568 enum print_stop_action
3569 bpstat_print (bpstat bs)
3570 {
3571 int val;
3572
3573 /* Maybe another breakpoint in the chain caused us to stop.
3574 (Currently all watchpoints go on the bpstat whether hit or not.
3575 That probably could (should) be changed, provided care is taken
3576 with respect to bpstat_explains_signal). */
3577 for (; bs; bs = bs->next)
3578 {
3579 val = print_bp_stop_message (bs);
3580 if (val == PRINT_SRC_ONLY
3581 || val == PRINT_SRC_AND_LOC
3582 || val == PRINT_NOTHING)
3583 return val;
3584 }
3585
3586 /* We reached the end of the chain, or we got a null BS to start
3587 with and nothing was printed. */
3588 return PRINT_UNKNOWN;
3589 }
3590
3591 /* Evaluate the expression EXP and return 1 if value is zero. This is
3592 used inside a catch_errors to evaluate the breakpoint condition.
3593 The argument is a "struct expression *" that has been cast to a
3594 "char *" to make it pass through catch_errors. */
3595
3596 static int
3597 breakpoint_cond_eval (void *exp)
3598 {
3599 struct value *mark = value_mark ();
3600 int i = !value_true (evaluate_expression ((struct expression *) exp));
3601
3602 value_free_to_mark (mark);
3603 return i;
3604 }
3605
3606 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
3607
3608 static bpstat
3609 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
3610 {
3611 bpstat bs;
3612
3613 bs = (bpstat) xmalloc (sizeof (*bs));
3614 bs->next = NULL;
3615 **bs_link_pointer = bs;
3616 *bs_link_pointer = &bs->next;
3617 bs->breakpoint_at = bl->owner;
3618 bs->bp_location_at = bl;
3619 incref_bp_location (bl);
3620 /* If the condition is false, etc., don't do the commands. */
3621 bs->commands = NULL;
3622 bs->commands_left = NULL;
3623 bs->old_val = NULL;
3624 bs->print_it = print_it_normal;
3625 return bs;
3626 }
3627 \f
3628 /* The target has stopped with waitstatus WS. Check if any hardware
3629 watchpoints have triggered, according to the target. */
3630
3631 int
3632 watchpoints_triggered (struct target_waitstatus *ws)
3633 {
3634 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
3635 CORE_ADDR addr;
3636 struct breakpoint *b;
3637
3638 if (!stopped_by_watchpoint)
3639 {
3640 /* We were not stopped by a watchpoint. Mark all watchpoints
3641 as not triggered. */
3642 ALL_BREAKPOINTS (b)
3643 if (is_hardware_watchpoint (b))
3644 b->watchpoint_triggered = watch_triggered_no;
3645
3646 return 0;
3647 }
3648
3649 if (!target_stopped_data_address (&current_target, &addr))
3650 {
3651 /* We were stopped by a watchpoint, but we don't know where.
3652 Mark all watchpoints as unknown. */
3653 ALL_BREAKPOINTS (b)
3654 if (is_hardware_watchpoint (b))
3655 b->watchpoint_triggered = watch_triggered_unknown;
3656
3657 return stopped_by_watchpoint;
3658 }
3659
3660 /* The target could report the data address. Mark watchpoints
3661 affected by this data address as triggered, and all others as not
3662 triggered. */
3663
3664 ALL_BREAKPOINTS (b)
3665 if (is_hardware_watchpoint (b))
3666 {
3667 struct bp_location *loc;
3668
3669 b->watchpoint_triggered = watch_triggered_no;
3670 for (loc = b->loc; loc; loc = loc->next)
3671 /* Exact match not required. Within range is
3672 sufficient. */
3673 if (target_watchpoint_addr_within_range (&current_target,
3674 addr, loc->address,
3675 loc->length))
3676 {
3677 b->watchpoint_triggered = watch_triggered_yes;
3678 break;
3679 }
3680 }
3681
3682 return 1;
3683 }
3684
3685 /* Possible return values for watchpoint_check (this can't be an enum
3686 because of check_errors). */
3687 /* The watchpoint has been deleted. */
3688 #define WP_DELETED 1
3689 /* The value has changed. */
3690 #define WP_VALUE_CHANGED 2
3691 /* The value has not changed. */
3692 #define WP_VALUE_NOT_CHANGED 3
3693 /* Ignore this watchpoint, no matter if the value changed or not. */
3694 #define WP_IGNORE 4
3695
3696 #define BP_TEMPFLAG 1
3697 #define BP_HARDWAREFLAG 2
3698
3699 /* Evaluate watchpoint condition expression and check if its value
3700 changed.
3701
3702 P should be a pointer to struct bpstat, but is defined as a void *
3703 in order for this function to be usable with catch_errors. */
3704
3705 static int
3706 watchpoint_check (void *p)
3707 {
3708 bpstat bs = (bpstat) p;
3709 struct breakpoint *b;
3710 struct frame_info *fr;
3711 int within_current_scope;
3712
3713 /* BS is built from an existing struct breakpoint. */
3714 gdb_assert (bs->breakpoint_at != NULL);
3715 b = bs->breakpoint_at;
3716
3717 /* If this is a local watchpoint, we only want to check if the
3718 watchpoint frame is in scope if the current thread is the thread
3719 that was used to create the watchpoint. */
3720 if (!watchpoint_in_thread_scope (b))
3721 return WP_IGNORE;
3722
3723 if (b->exp_valid_block == NULL)
3724 within_current_scope = 1;
3725 else
3726 {
3727 struct frame_info *frame = get_current_frame ();
3728 struct gdbarch *frame_arch = get_frame_arch (frame);
3729 CORE_ADDR frame_pc = get_frame_pc (frame);
3730
3731 /* in_function_epilogue_p() returns a non-zero value if we're
3732 still in the function but the stack frame has already been
3733 invalidated. Since we can't rely on the values of local
3734 variables after the stack has been destroyed, we are treating
3735 the watchpoint in that state as `not changed' without further
3736 checking. Don't mark watchpoints as changed if the current
3737 frame is in an epilogue - even if they are in some other
3738 frame, our view of the stack is likely to be wrong and
3739 frame_find_by_id could error out. */
3740 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
3741 return WP_IGNORE;
3742
3743 fr = frame_find_by_id (b->watchpoint_frame);
3744 within_current_scope = (fr != NULL);
3745
3746 /* If we've gotten confused in the unwinder, we might have
3747 returned a frame that can't describe this variable. */
3748 if (within_current_scope)
3749 {
3750 struct symbol *function;
3751
3752 function = get_frame_function (fr);
3753 if (function == NULL
3754 || !contained_in (b->exp_valid_block,
3755 SYMBOL_BLOCK_VALUE (function)))
3756 within_current_scope = 0;
3757 }
3758
3759 if (within_current_scope)
3760 /* If we end up stopping, the current frame will get selected
3761 in normal_stop. So this call to select_frame won't affect
3762 the user. */
3763 select_frame (fr);
3764 }
3765
3766 if (within_current_scope)
3767 {
3768 /* We use value_{,free_to_}mark because it could be a *long*
3769 time before we return to the command level and call
3770 free_all_values. We can't call free_all_values because we
3771 might be in the middle of evaluating a function call. */
3772
3773 int pc = 0;
3774 struct value *mark = value_mark ();
3775 struct value *new_val;
3776
3777 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
3778
3779 /* We use value_equal_contents instead of value_equal because
3780 the latter coerces an array to a pointer, thus comparing just
3781 the address of the array instead of its contents. This is
3782 not what we want. */
3783 if ((b->val != NULL) != (new_val != NULL)
3784 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
3785 {
3786 if (new_val != NULL)
3787 {
3788 release_value (new_val);
3789 value_free_to_mark (mark);
3790 }
3791 bs->old_val = b->val;
3792 b->val = new_val;
3793 b->val_valid = 1;
3794 return WP_VALUE_CHANGED;
3795 }
3796 else
3797 {
3798 /* Nothing changed. */
3799 value_free_to_mark (mark);
3800 return WP_VALUE_NOT_CHANGED;
3801 }
3802 }
3803 else
3804 {
3805 /* This seems like the only logical thing to do because
3806 if we temporarily ignored the watchpoint, then when
3807 we reenter the block in which it is valid it contains
3808 garbage (in the case of a function, it may have two
3809 garbage values, one before and one after the prologue).
3810 So we can't even detect the first assignment to it and
3811 watch after that (since the garbage may or may not equal
3812 the first value assigned). */
3813 /* We print all the stop information in print_it_typical(), but
3814 in this case, by the time we call print_it_typical() this bp
3815 will be deleted already. So we have no choice but print the
3816 information here. */
3817 if (ui_out_is_mi_like_p (uiout))
3818 ui_out_field_string
3819 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
3820 ui_out_text (uiout, "\nWatchpoint ");
3821 ui_out_field_int (uiout, "wpnum", b->number);
3822 ui_out_text (uiout,
3823 " deleted because the program has left the block in\n\
3824 which its expression is valid.\n");
3825
3826 if (b->related_breakpoint)
3827 {
3828 b->related_breakpoint->disposition = disp_del_at_next_stop;
3829 b->related_breakpoint->related_breakpoint = NULL;
3830 b->related_breakpoint = NULL;
3831 }
3832 b->disposition = disp_del_at_next_stop;
3833
3834 return WP_DELETED;
3835 }
3836 }
3837
3838 /* Return true if it looks like target has stopped due to hitting
3839 breakpoint location BL. This function does not check if we
3840 should stop, only if BL explains the stop. */
3841 static int
3842 bpstat_check_location (const struct bp_location *bl,
3843 struct address_space *aspace, CORE_ADDR bp_addr)
3844 {
3845 struct breakpoint *b = bl->owner;
3846
3847 /* BL is from existing struct breakpoint. */
3848 gdb_assert (b != NULL);
3849
3850 /* By definition, the inferior does not report stops at
3851 tracepoints. */
3852 if (is_tracepoint (b))
3853 return 0;
3854
3855 if (!is_watchpoint (b)
3856 && b->type != bp_hardware_breakpoint
3857 && b->type != bp_catchpoint) /* a non-watchpoint bp */
3858 {
3859 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
3860 aspace, bp_addr))
3861 return 0;
3862 if (overlay_debugging /* unmapped overlay section */
3863 && section_is_overlay (bl->section)
3864 && !section_is_mapped (bl->section))
3865 return 0;
3866 }
3867
3868 /* Continuable hardware watchpoints are treated as non-existent if the
3869 reason we stopped wasn't a hardware watchpoint (we didn't stop on
3870 some data address). Otherwise gdb won't stop on a break instruction
3871 in the code (not from a breakpoint) when a hardware watchpoint has
3872 been defined. Also skip watchpoints which we know did not trigger
3873 (did not match the data address). */
3874
3875 if (is_hardware_watchpoint (b)
3876 && b->watchpoint_triggered == watch_triggered_no)
3877 return 0;
3878
3879 if (b->type == bp_hardware_breakpoint)
3880 {
3881 if (bl->address != bp_addr)
3882 return 0;
3883 if (overlay_debugging /* unmapped overlay section */
3884 && section_is_overlay (bl->section)
3885 && !section_is_mapped (bl->section))
3886 return 0;
3887 }
3888
3889 if (b->type == bp_catchpoint)
3890 {
3891 gdb_assert (b->ops != NULL && b->ops->breakpoint_hit != NULL);
3892 if (!b->ops->breakpoint_hit (b))
3893 return 0;
3894 }
3895
3896 return 1;
3897 }
3898
3899 /* If BS refers to a watchpoint, determine if the watched values
3900 has actually changed, and we should stop. If not, set BS->stop
3901 to 0. */
3902 static void
3903 bpstat_check_watchpoint (bpstat bs)
3904 {
3905 const struct bp_location *bl;
3906 struct breakpoint *b;
3907
3908 /* BS is built for existing struct breakpoint. */
3909 bl = bs->bp_location_at;
3910 gdb_assert (bl != NULL);
3911 b = bs->breakpoint_at;
3912 gdb_assert (b != NULL);
3913
3914 if (is_watchpoint (b))
3915 {
3916 int must_check_value = 0;
3917
3918 if (b->type == bp_watchpoint)
3919 /* For a software watchpoint, we must always check the
3920 watched value. */
3921 must_check_value = 1;
3922 else if (b->watchpoint_triggered == watch_triggered_yes)
3923 /* We have a hardware watchpoint (read, write, or access)
3924 and the target earlier reported an address watched by
3925 this watchpoint. */
3926 must_check_value = 1;
3927 else if (b->watchpoint_triggered == watch_triggered_unknown
3928 && b->type == bp_hardware_watchpoint)
3929 /* We were stopped by a hardware watchpoint, but the target could
3930 not report the data address. We must check the watchpoint's
3931 value. Access and read watchpoints are out of luck; without
3932 a data address, we can't figure it out. */
3933 must_check_value = 1;
3934
3935 if (must_check_value)
3936 {
3937 char *message
3938 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3939 b->number);
3940 struct cleanup *cleanups = make_cleanup (xfree, message);
3941 int e = catch_errors (watchpoint_check, bs, message,
3942 RETURN_MASK_ALL);
3943 do_cleanups (cleanups);
3944 switch (e)
3945 {
3946 case WP_DELETED:
3947 /* We've already printed what needs to be printed. */
3948 bs->print_it = print_it_done;
3949 /* Stop. */
3950 break;
3951 case WP_IGNORE:
3952 bs->print_it = print_it_noop;
3953 bs->stop = 0;
3954 break;
3955 case WP_VALUE_CHANGED:
3956 if (b->type == bp_read_watchpoint)
3957 {
3958 /* There are two cases to consider here:
3959
3960 1. We're watching the triggered memory for reads.
3961 In that case, trust the target, and always report
3962 the watchpoint hit to the user. Even though
3963 reads don't cause value changes, the value may
3964 have changed since the last time it was read, and
3965 since we're not trapping writes, we will not see
3966 those, and as such we should ignore our notion of
3967 old value.
3968
3969 2. We're watching the triggered memory for both
3970 reads and writes. There are two ways this may
3971 happen:
3972
3973 2.1. This is a target that can't break on data
3974 reads only, but can break on accesses (reads or
3975 writes), such as e.g., x86. We detect this case
3976 at the time we try to insert read watchpoints.
3977
3978 2.2. Otherwise, the target supports read
3979 watchpoints, but, the user set an access or write
3980 watchpoint watching the same memory as this read
3981 watchpoint.
3982
3983 If we're watching memory writes as well as reads,
3984 ignore watchpoint hits when we find that the
3985 value hasn't changed, as reads don't cause
3986 changes. This still gives false positives when
3987 the program writes the same value to memory as
3988 what there was already in memory (we will confuse
3989 it for a read), but it's much better than
3990 nothing. */
3991
3992 int other_write_watchpoint = 0;
3993
3994 if (bl->watchpoint_type == hw_read)
3995 {
3996 struct breakpoint *other_b;
3997
3998 ALL_BREAKPOINTS (other_b)
3999 if ((other_b->type == bp_hardware_watchpoint
4000 || other_b->type == bp_access_watchpoint)
4001 && (other_b->watchpoint_triggered
4002 == watch_triggered_yes))
4003 {
4004 other_write_watchpoint = 1;
4005 break;
4006 }
4007 }
4008
4009 if (other_write_watchpoint
4010 || bl->watchpoint_type == hw_access)
4011 {
4012 /* We're watching the same memory for writes,
4013 and the value changed since the last time we
4014 updated it, so this trap must be for a write.
4015 Ignore it. */
4016 bs->print_it = print_it_noop;
4017 bs->stop = 0;
4018 }
4019 }
4020 break;
4021 case WP_VALUE_NOT_CHANGED:
4022 if (b->type == bp_hardware_watchpoint
4023 || b->type == bp_watchpoint)
4024 {
4025 /* Don't stop: write watchpoints shouldn't fire if
4026 the value hasn't changed. */
4027 bs->print_it = print_it_noop;
4028 bs->stop = 0;
4029 }
4030 /* Stop. */
4031 break;
4032 default:
4033 /* Can't happen. */
4034 case 0:
4035 /* Error from catch_errors. */
4036 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
4037 if (b->related_breakpoint)
4038 b->related_breakpoint->disposition = disp_del_at_next_stop;
4039 b->disposition = disp_del_at_next_stop;
4040 /* We've already printed what needs to be printed. */
4041 bs->print_it = print_it_done;
4042 break;
4043 }
4044 }
4045 else /* must_check_value == 0 */
4046 {
4047 /* This is a case where some watchpoint(s) triggered, but
4048 not at the address of this watchpoint, or else no
4049 watchpoint triggered after all. So don't print
4050 anything for this watchpoint. */
4051 bs->print_it = print_it_noop;
4052 bs->stop = 0;
4053 }
4054 }
4055 }
4056
4057
4058 /* Check conditions (condition proper, frame, thread and ignore count)
4059 of breakpoint referred to by BS. If we should not stop for this
4060 breakpoint, set BS->stop to 0. */
4061
4062 static void
4063 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4064 {
4065 int thread_id = pid_to_thread_id (ptid);
4066 const struct bp_location *bl;
4067 struct breakpoint *b;
4068
4069 /* BS is built for existing struct breakpoint. */
4070 bl = bs->bp_location_at;
4071 gdb_assert (bl != NULL);
4072 b = bs->breakpoint_at;
4073 gdb_assert (b != NULL);
4074
4075 if (frame_id_p (b->frame_id)
4076 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
4077 bs->stop = 0;
4078 else if (bs->stop)
4079 {
4080 int value_is_zero = 0;
4081 struct expression *cond;
4082
4083 /* Evaluate Python breakpoints that have a "stop"
4084 method implemented. */
4085 if (b->py_bp_object)
4086 bs->stop = gdbpy_should_stop (b->py_bp_object);
4087
4088 if (is_watchpoint (b))
4089 cond = b->cond_exp;
4090 else
4091 cond = bl->cond;
4092
4093 if (cond && b->disposition != disp_del_at_next_stop)
4094 {
4095 int within_current_scope = 1;
4096
4097 /* We use value_mark and value_free_to_mark because it could
4098 be a long time before we return to the command level and
4099 call free_all_values. We can't call free_all_values
4100 because we might be in the middle of evaluating a
4101 function call. */
4102 struct value *mark = value_mark ();
4103
4104 /* Need to select the frame, with all that implies so that
4105 the conditions will have the right context. Because we
4106 use the frame, we will not see an inlined function's
4107 variables when we arrive at a breakpoint at the start
4108 of the inlined function; the current frame will be the
4109 call site. */
4110 if (!is_watchpoint (b) || b->cond_exp_valid_block == NULL)
4111 select_frame (get_current_frame ());
4112 else
4113 {
4114 struct frame_info *frame;
4115
4116 /* For local watchpoint expressions, which particular
4117 instance of a local is being watched matters, so we
4118 keep track of the frame to evaluate the expression
4119 in. To evaluate the condition however, it doesn't
4120 really matter which instantiation of the function
4121 where the condition makes sense triggers the
4122 watchpoint. This allows an expression like "watch
4123 global if q > 10" set in `func', catch writes to
4124 global on all threads that call `func', or catch
4125 writes on all recursive calls of `func' by a single
4126 thread. We simply always evaluate the condition in
4127 the innermost frame that's executing where it makes
4128 sense to evaluate the condition. It seems
4129 intuitive. */
4130 frame = block_innermost_frame (b->cond_exp_valid_block);
4131 if (frame != NULL)
4132 select_frame (frame);
4133 else
4134 within_current_scope = 0;
4135 }
4136 if (within_current_scope)
4137 value_is_zero
4138 = catch_errors (breakpoint_cond_eval, cond,
4139 "Error in testing breakpoint condition:\n",
4140 RETURN_MASK_ALL);
4141 else
4142 {
4143 warning (_("Watchpoint condition cannot be tested "
4144 "in the current scope"));
4145 /* If we failed to set the right context for this
4146 watchpoint, unconditionally report it. */
4147 value_is_zero = 0;
4148 }
4149 /* FIXME-someday, should give breakpoint #. */
4150 value_free_to_mark (mark);
4151 }
4152
4153 if (cond && value_is_zero)
4154 {
4155 bs->stop = 0;
4156 }
4157 else if (b->thread != -1 && b->thread != thread_id)
4158 {
4159 bs->stop = 0;
4160 }
4161 else if (b->ignore_count > 0)
4162 {
4163 b->ignore_count--;
4164 annotate_ignore_count_change ();
4165 bs->stop = 0;
4166 /* Increase the hit count even though we don't stop. */
4167 ++(b->hit_count);
4168 }
4169 }
4170 }
4171
4172
4173 /* Get a bpstat associated with having just stopped at address
4174 BP_ADDR in thread PTID.
4175
4176 Determine whether we stopped at a breakpoint, etc, or whether we
4177 don't understand this stop. Result is a chain of bpstat's such
4178 that:
4179
4180 if we don't understand the stop, the result is a null pointer.
4181
4182 if we understand why we stopped, the result is not null.
4183
4184 Each element of the chain refers to a particular breakpoint or
4185 watchpoint at which we have stopped. (We may have stopped for
4186 several reasons concurrently.)
4187
4188 Each element of the chain has valid next, breakpoint_at,
4189 commands, FIXME??? fields. */
4190
4191 bpstat
4192 bpstat_stop_status (struct address_space *aspace,
4193 CORE_ADDR bp_addr, ptid_t ptid)
4194 {
4195 struct breakpoint *b = NULL;
4196 struct bp_location *bl;
4197 struct bp_location *loc;
4198 /* First item of allocated bpstat's. */
4199 bpstat bs_head = NULL, *bs_link = &bs_head;
4200 /* Pointer to the last thing in the chain currently. */
4201 bpstat bs;
4202 int ix;
4203 int need_remove_insert;
4204 int removed_any;
4205
4206 /* First, build the bpstat chain with locations that explain a
4207 target stop, while being careful to not set the target running,
4208 as that may invalidate locations (in particular watchpoint
4209 locations are recreated). Resuming will happen here with
4210 breakpoint conditions or watchpoint expressions that include
4211 inferior function calls. */
4212
4213 ALL_BREAKPOINTS (b)
4214 {
4215 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4216 continue;
4217
4218 for (bl = b->loc; bl != NULL; bl = bl->next)
4219 {
4220 /* For hardware watchpoints, we look only at the first
4221 location. The watchpoint_check function will work on the
4222 entire expression, not the individual locations. For
4223 read watchpoints, the watchpoints_triggered function has
4224 checked all locations already. */
4225 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4226 break;
4227
4228 if (bl->shlib_disabled)
4229 continue;
4230
4231 if (!bpstat_check_location (bl, aspace, bp_addr))
4232 continue;
4233
4234 /* Come here if it's a watchpoint, or if the break address
4235 matches. */
4236
4237 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4238 explain stop. */
4239
4240 /* Assume we stop. Should we find a watchpoint that is not
4241 actually triggered, or if the condition of the breakpoint
4242 evaluates as false, we'll reset 'stop' to 0. */
4243 bs->stop = 1;
4244 bs->print = 1;
4245
4246 /* If this is a scope breakpoint, mark the associated
4247 watchpoint as triggered so that we will handle the
4248 out-of-scope event. We'll get to the watchpoint next
4249 iteration. */
4250 if (b->type == bp_watchpoint_scope)
4251 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
4252 }
4253 }
4254
4255 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4256 {
4257 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
4258 aspace, bp_addr))
4259 {
4260 bs = bpstat_alloc (loc, &bs_link);
4261 /* For hits of moribund locations, we should just proceed. */
4262 bs->stop = 0;
4263 bs->print = 0;
4264 bs->print_it = print_it_noop;
4265 }
4266 }
4267
4268 /* Now go through the locations that caused the target to stop, and
4269 check whether we're interested in reporting this stop to higher
4270 layers, or whether we should resume the target transparently. */
4271
4272 removed_any = 0;
4273
4274 for (bs = bs_head; bs != NULL; bs = bs->next)
4275 {
4276 if (!bs->stop)
4277 continue;
4278
4279 bpstat_check_watchpoint (bs);
4280 if (!bs->stop)
4281 continue;
4282
4283 b = bs->breakpoint_at;
4284
4285 if (b->type == bp_thread_event || b->type == bp_overlay_event
4286 || b->type == bp_longjmp_master
4287 || b->type == bp_std_terminate_master
4288 || b->type == bp_exception_master)
4289 /* We do not stop for these. */
4290 bs->stop = 0;
4291 else
4292 bpstat_check_breakpoint_conditions (bs, ptid);
4293
4294 if (bs->stop)
4295 {
4296 ++(b->hit_count);
4297
4298 /* We will stop here. */
4299 if (b->disposition == disp_disable)
4300 {
4301 if (b->enable_state != bp_permanent)
4302 b->enable_state = bp_disabled;
4303 removed_any = 1;
4304 }
4305 if (b->silent)
4306 bs->print = 0;
4307 bs->commands = b->commands;
4308 incref_counted_command_line (bs->commands);
4309 bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4310 if (bs->commands_left
4311 && (strcmp ("silent", bs->commands_left->line) == 0
4312 || (xdb_commands
4313 && strcmp ("Q",
4314 bs->commands_left->line) == 0)))
4315 {
4316 bs->commands_left = bs->commands_left->next;
4317 bs->print = 0;
4318 }
4319 }
4320
4321 /* Print nothing for this entry if we dont stop or dont print. */
4322 if (bs->stop == 0 || bs->print == 0)
4323 bs->print_it = print_it_noop;
4324 }
4325
4326 /* If we aren't stopping, the value of some hardware watchpoint may
4327 not have changed, but the intermediate memory locations we are
4328 watching may have. Don't bother if we're stopping; this will get
4329 done later. */
4330 need_remove_insert = 0;
4331 if (! bpstat_causes_stop (bs_head))
4332 for (bs = bs_head; bs != NULL; bs = bs->next)
4333 if (!bs->stop
4334 && bs->breakpoint_at
4335 && is_hardware_watchpoint (bs->breakpoint_at))
4336 {
4337 update_watchpoint (bs->breakpoint_at, 0 /* don't reparse. */);
4338 need_remove_insert = 1;
4339 }
4340
4341 if (need_remove_insert)
4342 update_global_location_list (1);
4343 else if (removed_any)
4344 update_global_location_list (0);
4345
4346 return bs_head;
4347 }
4348
4349 static void
4350 handle_jit_event (void)
4351 {
4352 struct frame_info *frame;
4353 struct gdbarch *gdbarch;
4354
4355 /* Switch terminal for any messages produced by
4356 breakpoint_re_set. */
4357 target_terminal_ours_for_output ();
4358
4359 frame = get_current_frame ();
4360 gdbarch = get_frame_arch (frame);
4361
4362 jit_event_handler (gdbarch);
4363
4364 target_terminal_inferior ();
4365 }
4366
4367 /* Prepare WHAT final decision for infrun. */
4368
4369 /* Decide what infrun needs to do with this bpstat. */
4370
4371 struct bpstat_what
4372 bpstat_what (bpstat bs)
4373 {
4374 struct bpstat_what retval;
4375 /* We need to defer calling `solib_add', as adding new symbols
4376 resets breakpoints, which in turn deletes breakpoint locations,
4377 and hence may clear unprocessed entries in the BS chain. */
4378 int shlib_event = 0;
4379 int jit_event = 0;
4380
4381 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
4382 retval.call_dummy = STOP_NONE;
4383 retval.is_longjmp = 0;
4384
4385 for (; bs != NULL; bs = bs->next)
4386 {
4387 /* Extract this BS's action. After processing each BS, we check
4388 if its action overrides all we've seem so far. */
4389 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4390 enum bptype bptype;
4391
4392 if (bs->breakpoint_at == NULL)
4393 {
4394 /* I suspect this can happen if it was a momentary
4395 breakpoint which has since been deleted. */
4396 bptype = bp_none;
4397 }
4398 else if (bs->breakpoint_at == NULL)
4399 bptype = bp_none;
4400 else
4401 bptype = bs->breakpoint_at->type;
4402
4403 switch (bptype)
4404 {
4405 case bp_none:
4406 break;
4407 case bp_breakpoint:
4408 case bp_hardware_breakpoint:
4409 case bp_until:
4410 case bp_finish:
4411 if (bs->stop)
4412 {
4413 if (bs->print)
4414 this_action = BPSTAT_WHAT_STOP_NOISY;
4415 else
4416 this_action = BPSTAT_WHAT_STOP_SILENT;
4417 }
4418 else
4419 this_action = BPSTAT_WHAT_SINGLE;
4420 break;
4421 case bp_watchpoint:
4422 case bp_hardware_watchpoint:
4423 case bp_read_watchpoint:
4424 case bp_access_watchpoint:
4425 if (bs->stop)
4426 {
4427 if (bs->print)
4428 this_action = BPSTAT_WHAT_STOP_NOISY;
4429 else
4430 this_action = BPSTAT_WHAT_STOP_SILENT;
4431 }
4432 else
4433 {
4434 /* There was a watchpoint, but we're not stopping.
4435 This requires no further action. */
4436 }
4437 break;
4438 case bp_longjmp:
4439 case bp_exception:
4440 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
4441 retval.is_longjmp = bptype == bp_longjmp;
4442 break;
4443 case bp_longjmp_resume:
4444 case bp_exception_resume:
4445 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
4446 retval.is_longjmp = bptype == bp_longjmp_resume;
4447 break;
4448 case bp_step_resume:
4449 if (bs->stop)
4450 this_action = BPSTAT_WHAT_STEP_RESUME;
4451 else
4452 {
4453 /* It is for the wrong frame. */
4454 this_action = BPSTAT_WHAT_SINGLE;
4455 }
4456 break;
4457 case bp_watchpoint_scope:
4458 case bp_thread_event:
4459 case bp_overlay_event:
4460 case bp_longjmp_master:
4461 case bp_std_terminate_master:
4462 case bp_exception_master:
4463 this_action = BPSTAT_WHAT_SINGLE;
4464 break;
4465 case bp_catchpoint:
4466 if (bs->stop)
4467 {
4468 if (bs->print)
4469 this_action = BPSTAT_WHAT_STOP_NOISY;
4470 else
4471 this_action = BPSTAT_WHAT_STOP_SILENT;
4472 }
4473 else
4474 {
4475 /* There was a catchpoint, but we're not stopping.
4476 This requires no further action. */
4477 }
4478 break;
4479 case bp_shlib_event:
4480 shlib_event = 1;
4481
4482 /* If requested, stop when the dynamic linker notifies GDB
4483 of events. This allows the user to get control and place
4484 breakpoints in initializer routines for dynamically
4485 loaded objects (among other things). */
4486 if (stop_on_solib_events)
4487 this_action = BPSTAT_WHAT_STOP_NOISY;
4488 else
4489 this_action = BPSTAT_WHAT_SINGLE;
4490 break;
4491 case bp_jit_event:
4492 jit_event = 1;
4493 this_action = BPSTAT_WHAT_SINGLE;
4494 break;
4495 case bp_call_dummy:
4496 /* Make sure the action is stop (silent or noisy),
4497 so infrun.c pops the dummy frame. */
4498 retval.call_dummy = STOP_STACK_DUMMY;
4499 this_action = BPSTAT_WHAT_STOP_SILENT;
4500 break;
4501 case bp_std_terminate:
4502 /* Make sure the action is stop (silent or noisy),
4503 so infrun.c pops the dummy frame. */
4504 retval.call_dummy = STOP_STD_TERMINATE;
4505 this_action = BPSTAT_WHAT_STOP_SILENT;
4506 break;
4507 case bp_tracepoint:
4508 case bp_fast_tracepoint:
4509 case bp_static_tracepoint:
4510 /* Tracepoint hits should not be reported back to GDB, and
4511 if one got through somehow, it should have been filtered
4512 out already. */
4513 internal_error (__FILE__, __LINE__,
4514 _("bpstat_what: tracepoint encountered"));
4515 default:
4516 internal_error (__FILE__, __LINE__,
4517 _("bpstat_what: unhandled bptype %d"), (int) bptype);
4518 }
4519
4520 retval.main_action = max (retval.main_action, this_action);
4521 }
4522
4523 if (shlib_event)
4524 {
4525 if (debug_infrun)
4526 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4527
4528 /* Check for any newly added shared libraries if we're supposed
4529 to be adding them automatically. */
4530
4531 /* Switch terminal for any messages produced by
4532 breakpoint_re_set. */
4533 target_terminal_ours_for_output ();
4534
4535 #ifdef SOLIB_ADD
4536 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4537 #else
4538 solib_add (NULL, 0, &current_target, auto_solib_add);
4539 #endif
4540
4541 target_terminal_inferior ();
4542 }
4543
4544 if (jit_event)
4545 {
4546 if (debug_infrun)
4547 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4548
4549 handle_jit_event ();
4550 }
4551
4552 return retval;
4553 }
4554
4555 /* Nonzero if we should step constantly (e.g. watchpoints on machines
4556 without hardware support). This isn't related to a specific bpstat,
4557 just to things like whether watchpoints are set. */
4558
4559 int
4560 bpstat_should_step (void)
4561 {
4562 struct breakpoint *b;
4563
4564 ALL_BREAKPOINTS (b)
4565 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4566 return 1;
4567 return 0;
4568 }
4569
4570 int
4571 bpstat_causes_stop (bpstat bs)
4572 {
4573 for (; bs != NULL; bs = bs->next)
4574 if (bs->stop)
4575 return 1;
4576
4577 return 0;
4578 }
4579
4580 \f
4581
4582 /* Compute a string of spaces suitable to indent the next line
4583 so it starts at the position corresponding to the table column
4584 named COL_NAME in the currently active table of UIOUT. */
4585
4586 static char *
4587 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4588 {
4589 static char wrap_indent[80];
4590 int i, total_width, width, align;
4591 char *text;
4592
4593 total_width = 0;
4594 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4595 {
4596 if (strcmp (text, col_name) == 0)
4597 {
4598 gdb_assert (total_width < sizeof wrap_indent);
4599 memset (wrap_indent, ' ', total_width);
4600 wrap_indent[total_width] = 0;
4601
4602 return wrap_indent;
4603 }
4604
4605 total_width += width + 1;
4606 }
4607
4608 return NULL;
4609 }
4610
4611 /* Print the LOC location out of the list of B->LOC locations. */
4612
4613 static void
4614 print_breakpoint_location (struct breakpoint *b,
4615 struct bp_location *loc)
4616 {
4617 struct cleanup *old_chain = save_current_program_space ();
4618
4619 if (loc != NULL && loc->shlib_disabled)
4620 loc = NULL;
4621
4622 if (loc != NULL)
4623 set_current_program_space (loc->pspace);
4624
4625 if (b->source_file && loc)
4626 {
4627 struct symbol *sym
4628 = find_pc_sect_function (loc->address, loc->section);
4629 if (sym)
4630 {
4631 ui_out_text (uiout, "in ");
4632 ui_out_field_string (uiout, "func",
4633 SYMBOL_PRINT_NAME (sym));
4634 ui_out_text (uiout, " ");
4635 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4636 ui_out_text (uiout, "at ");
4637 }
4638 ui_out_field_string (uiout, "file", b->source_file);
4639 ui_out_text (uiout, ":");
4640
4641 if (ui_out_is_mi_like_p (uiout))
4642 {
4643 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4644 char *fullname = symtab_to_fullname (sal.symtab);
4645
4646 if (fullname)
4647 ui_out_field_string (uiout, "fullname", fullname);
4648 }
4649
4650 ui_out_field_int (uiout, "line", b->line_number);
4651 }
4652 else if (loc)
4653 {
4654 struct ui_stream *stb = ui_out_stream_new (uiout);
4655 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4656
4657 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4658 demangle, "");
4659 ui_out_field_stream (uiout, "at", stb);
4660
4661 do_cleanups (stb_chain);
4662 }
4663 else
4664 ui_out_field_string (uiout, "pending", b->addr_string);
4665
4666 do_cleanups (old_chain);
4667 }
4668
4669 static const char *
4670 bptype_string (enum bptype type)
4671 {
4672 struct ep_type_description
4673 {
4674 enum bptype type;
4675 char *description;
4676 };
4677 static struct ep_type_description bptypes[] =
4678 {
4679 {bp_none, "?deleted?"},
4680 {bp_breakpoint, "breakpoint"},
4681 {bp_hardware_breakpoint, "hw breakpoint"},
4682 {bp_until, "until"},
4683 {bp_finish, "finish"},
4684 {bp_watchpoint, "watchpoint"},
4685 {bp_hardware_watchpoint, "hw watchpoint"},
4686 {bp_read_watchpoint, "read watchpoint"},
4687 {bp_access_watchpoint, "acc watchpoint"},
4688 {bp_longjmp, "longjmp"},
4689 {bp_longjmp_resume, "longjmp resume"},
4690 {bp_exception, "exception"},
4691 {bp_exception_resume, "exception resume"},
4692 {bp_step_resume, "step resume"},
4693 {bp_watchpoint_scope, "watchpoint scope"},
4694 {bp_call_dummy, "call dummy"},
4695 {bp_std_terminate, "std::terminate"},
4696 {bp_shlib_event, "shlib events"},
4697 {bp_thread_event, "thread events"},
4698 {bp_overlay_event, "overlay events"},
4699 {bp_longjmp_master, "longjmp master"},
4700 {bp_std_terminate_master, "std::terminate master"},
4701 {bp_exception_master, "exception master"},
4702 {bp_catchpoint, "catchpoint"},
4703 {bp_tracepoint, "tracepoint"},
4704 {bp_fast_tracepoint, "fast tracepoint"},
4705 {bp_static_tracepoint, "static tracepoint"},
4706 {bp_jit_event, "jit events"},
4707 };
4708
4709 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4710 || ((int) type != bptypes[(int) type].type))
4711 internal_error (__FILE__, __LINE__,
4712 _("bptypes table does not describe type #%d."),
4713 (int) type);
4714
4715 return bptypes[(int) type].description;
4716 }
4717
4718 /* Print B to gdb_stdout. */
4719
4720 static void
4721 print_one_breakpoint_location (struct breakpoint *b,
4722 struct bp_location *loc,
4723 int loc_number,
4724 struct bp_location **last_loc,
4725 int allflag)
4726 {
4727 struct command_line *l;
4728 static char bpenables[] = "nynny";
4729 struct cleanup *bkpt_chain;
4730
4731 int header_of_multiple = 0;
4732 int part_of_multiple = (loc != NULL);
4733 struct value_print_options opts;
4734
4735 get_user_print_options (&opts);
4736
4737 gdb_assert (!loc || loc_number != 0);
4738 /* See comment in print_one_breakpoint concerning treatment of
4739 breakpoints with single disabled location. */
4740 if (loc == NULL
4741 && (b->loc != NULL
4742 && (b->loc->next != NULL || !b->loc->enabled)))
4743 header_of_multiple = 1;
4744 if (loc == NULL)
4745 loc = b->loc;
4746
4747 annotate_record ();
4748 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
4749
4750 /* 1 */
4751 annotate_field (0);
4752 if (part_of_multiple)
4753 {
4754 char *formatted;
4755 formatted = xstrprintf ("%d.%d", b->number, loc_number);
4756 ui_out_field_string (uiout, "number", formatted);
4757 xfree (formatted);
4758 }
4759 else
4760 {
4761 ui_out_field_int (uiout, "number", b->number);
4762 }
4763
4764 /* 2 */
4765 annotate_field (1);
4766 if (part_of_multiple)
4767 ui_out_field_skip (uiout, "type");
4768 else
4769 ui_out_field_string (uiout, "type", bptype_string (b->type));
4770
4771 /* 3 */
4772 annotate_field (2);
4773 if (part_of_multiple)
4774 ui_out_field_skip (uiout, "disp");
4775 else
4776 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
4777
4778
4779 /* 4 */
4780 annotate_field (3);
4781 if (part_of_multiple)
4782 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
4783 else
4784 ui_out_field_fmt (uiout, "enabled", "%c",
4785 bpenables[(int) b->enable_state]);
4786 ui_out_spaces (uiout, 2);
4787
4788
4789 /* 5 and 6 */
4790 if (b->ops != NULL && b->ops->print_one != NULL)
4791 {
4792 /* Although the print_one can possibly print all locations,
4793 calling it here is not likely to get any nice result. So,
4794 make sure there's just one location. */
4795 gdb_assert (b->loc == NULL || b->loc->next == NULL);
4796 b->ops->print_one (b, last_loc);
4797 }
4798 else
4799 switch (b->type)
4800 {
4801 case bp_none:
4802 internal_error (__FILE__, __LINE__,
4803 _("print_one_breakpoint: bp_none encountered\n"));
4804 break;
4805
4806 case bp_watchpoint:
4807 case bp_hardware_watchpoint:
4808 case bp_read_watchpoint:
4809 case bp_access_watchpoint:
4810 /* Field 4, the address, is omitted (which makes the columns
4811 not line up too nicely with the headers, but the effect
4812 is relatively readable). */
4813 if (opts.addressprint)
4814 ui_out_field_skip (uiout, "addr");
4815 annotate_field (5);
4816 ui_out_field_string (uiout, "what", b->exp_string);
4817 break;
4818
4819 case bp_breakpoint:
4820 case bp_hardware_breakpoint:
4821 case bp_until:
4822 case bp_finish:
4823 case bp_longjmp:
4824 case bp_longjmp_resume:
4825 case bp_exception:
4826 case bp_exception_resume:
4827 case bp_step_resume:
4828 case bp_watchpoint_scope:
4829 case bp_call_dummy:
4830 case bp_std_terminate:
4831 case bp_shlib_event:
4832 case bp_thread_event:
4833 case bp_overlay_event:
4834 case bp_longjmp_master:
4835 case bp_std_terminate_master:
4836 case bp_exception_master:
4837 case bp_tracepoint:
4838 case bp_fast_tracepoint:
4839 case bp_static_tracepoint:
4840 case bp_jit_event:
4841 if (opts.addressprint)
4842 {
4843 annotate_field (4);
4844 if (header_of_multiple)
4845 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
4846 else if (b->loc == NULL || loc->shlib_disabled)
4847 ui_out_field_string (uiout, "addr", "<PENDING>");
4848 else
4849 ui_out_field_core_addr (uiout, "addr",
4850 loc->gdbarch, loc->address);
4851 }
4852 annotate_field (5);
4853 if (!header_of_multiple)
4854 print_breakpoint_location (b, loc);
4855 if (b->loc)
4856 *last_loc = b->loc;
4857 break;
4858 }
4859
4860
4861 /* For backward compatibility, don't display inferiors unless there
4862 are several. */
4863 if (loc != NULL
4864 && !header_of_multiple
4865 && (allflag
4866 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4867 && (number_of_program_spaces () > 1
4868 || number_of_inferiors () > 1)
4869 /* LOC is for existing B, it cannot be in
4870 moribund_locations and thus having NULL OWNER. */
4871 && loc->owner->type != bp_catchpoint)))
4872 {
4873 struct inferior *inf;
4874 int first = 1;
4875
4876 for (inf = inferior_list; inf != NULL; inf = inf->next)
4877 {
4878 if (inf->pspace == loc->pspace)
4879 {
4880 if (first)
4881 {
4882 first = 0;
4883 ui_out_text (uiout, " inf ");
4884 }
4885 else
4886 ui_out_text (uiout, ", ");
4887 ui_out_text (uiout, plongest (inf->num));
4888 }
4889 }
4890 }
4891
4892 if (!part_of_multiple)
4893 {
4894 if (b->thread != -1)
4895 {
4896 /* FIXME: This seems to be redundant and lost here; see the
4897 "stop only in" line a little further down. */
4898 ui_out_text (uiout, " thread ");
4899 ui_out_field_int (uiout, "thread", b->thread);
4900 }
4901 else if (b->task != 0)
4902 {
4903 ui_out_text (uiout, " task ");
4904 ui_out_field_int (uiout, "task", b->task);
4905 }
4906 }
4907
4908 ui_out_text (uiout, "\n");
4909
4910 if (!part_of_multiple && b->static_trace_marker_id)
4911 {
4912 gdb_assert (b->type == bp_static_tracepoint);
4913
4914 ui_out_text (uiout, "\tmarker id is ");
4915 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
4916 b->static_trace_marker_id);
4917 ui_out_text (uiout, "\n");
4918 }
4919
4920 if (part_of_multiple && frame_id_p (b->frame_id))
4921 {
4922 annotate_field (6);
4923 ui_out_text (uiout, "\tstop only in stack frame at ");
4924 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
4925 the frame ID. */
4926 ui_out_field_core_addr (uiout, "frame",
4927 b->gdbarch, b->frame_id.stack_addr);
4928 ui_out_text (uiout, "\n");
4929 }
4930
4931 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
4932 {
4933 /* We do not print the condition for Ada exception catchpoints
4934 because the condition is an internal implementation detail
4935 that we do not want to expose to the user. */
4936 annotate_field (7);
4937 if (is_tracepoint (b))
4938 ui_out_text (uiout, "\ttrace only if ");
4939 else
4940 ui_out_text (uiout, "\tstop only if ");
4941 ui_out_field_string (uiout, "cond", b->cond_string);
4942 ui_out_text (uiout, "\n");
4943 }
4944
4945 if (!part_of_multiple && b->thread != -1)
4946 {
4947 /* FIXME should make an annotation for this. */
4948 ui_out_text (uiout, "\tstop only in thread ");
4949 ui_out_field_int (uiout, "thread", b->thread);
4950 ui_out_text (uiout, "\n");
4951 }
4952
4953 if (!part_of_multiple && b->hit_count)
4954 {
4955 /* FIXME should make an annotation for this. */
4956 if (ep_is_catchpoint (b))
4957 ui_out_text (uiout, "\tcatchpoint");
4958 else
4959 ui_out_text (uiout, "\tbreakpoint");
4960 ui_out_text (uiout, " already hit ");
4961 ui_out_field_int (uiout, "times", b->hit_count);
4962 if (b->hit_count == 1)
4963 ui_out_text (uiout, " time\n");
4964 else
4965 ui_out_text (uiout, " times\n");
4966 }
4967
4968 /* Output the count also if it is zero, but only if this is mi.
4969 FIXME: Should have a better test for this. */
4970 if (ui_out_is_mi_like_p (uiout))
4971 if (!part_of_multiple && b->hit_count == 0)
4972 ui_out_field_int (uiout, "times", b->hit_count);
4973
4974 if (!part_of_multiple && b->ignore_count)
4975 {
4976 annotate_field (8);
4977 ui_out_text (uiout, "\tignore next ");
4978 ui_out_field_int (uiout, "ignore", b->ignore_count);
4979 ui_out_text (uiout, " hits\n");
4980 }
4981
4982 l = b->commands ? b->commands->commands : NULL;
4983 if (!part_of_multiple && l)
4984 {
4985 struct cleanup *script_chain;
4986
4987 annotate_field (9);
4988 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
4989 print_command_lines (uiout, l, 4);
4990 do_cleanups (script_chain);
4991 }
4992
4993 if (!part_of_multiple && b->pass_count)
4994 {
4995 annotate_field (10);
4996 ui_out_text (uiout, "\tpass count ");
4997 ui_out_field_int (uiout, "pass", b->pass_count);
4998 ui_out_text (uiout, " \n");
4999 }
5000
5001 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5002 {
5003 if (b->addr_string)
5004 ui_out_field_string (uiout, "original-location", b->addr_string);
5005 else if (b->exp_string)
5006 ui_out_field_string (uiout, "original-location", b->exp_string);
5007 }
5008
5009 do_cleanups (bkpt_chain);
5010 }
5011
5012 static void
5013 print_one_breakpoint (struct breakpoint *b,
5014 struct bp_location **last_loc,
5015 int allflag)
5016 {
5017 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
5018
5019 /* If this breakpoint has custom print function,
5020 it's already printed. Otherwise, print individual
5021 locations, if any. */
5022 if (b->ops == NULL || b->ops->print_one == NULL)
5023 {
5024 /* If breakpoint has a single location that is disabled, we
5025 print it as if it had several locations, since otherwise it's
5026 hard to represent "breakpoint enabled, location disabled"
5027 situation.
5028
5029 Note that while hardware watchpoints have several locations
5030 internally, that's not a property exposed to user. */
5031 if (b->loc
5032 && !is_hardware_watchpoint (b)
5033 && (b->loc->next || !b->loc->enabled)
5034 && !ui_out_is_mi_like_p (uiout))
5035 {
5036 struct bp_location *loc;
5037 int n = 1;
5038 for (loc = b->loc; loc; loc = loc->next, ++n)
5039 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5040 }
5041 }
5042 }
5043
5044 static int
5045 breakpoint_address_bits (struct breakpoint *b)
5046 {
5047 int print_address_bits = 0;
5048 struct bp_location *loc;
5049
5050 for (loc = b->loc; loc; loc = loc->next)
5051 {
5052 int addr_bit;
5053
5054 /* Software watchpoints that aren't watching memory don't have
5055 an address to print. */
5056 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5057 continue;
5058
5059 addr_bit = gdbarch_addr_bit (loc->gdbarch);
5060 if (addr_bit > print_address_bits)
5061 print_address_bits = addr_bit;
5062 }
5063
5064 return print_address_bits;
5065 }
5066
5067 struct captured_breakpoint_query_args
5068 {
5069 int bnum;
5070 };
5071
5072 static int
5073 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
5074 {
5075 struct captured_breakpoint_query_args *args = data;
5076 struct breakpoint *b;
5077 struct bp_location *dummy_loc = NULL;
5078
5079 ALL_BREAKPOINTS (b)
5080 {
5081 if (args->bnum == b->number)
5082 {
5083 print_one_breakpoint (b, &dummy_loc, 0);
5084 return GDB_RC_OK;
5085 }
5086 }
5087 return GDB_RC_NONE;
5088 }
5089
5090 enum gdb_rc
5091 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5092 char **error_message)
5093 {
5094 struct captured_breakpoint_query_args args;
5095
5096 args.bnum = bnum;
5097 /* For the moment we don't trust print_one_breakpoint() to not throw
5098 an error. */
5099 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5100 error_message, RETURN_MASK_ALL) < 0)
5101 return GDB_RC_FAIL;
5102 else
5103 return GDB_RC_OK;
5104 }
5105
5106 /* Return non-zero if B is user settable (breakpoints, watchpoints,
5107 catchpoints, et.al.). */
5108
5109 static int
5110 user_settable_breakpoint (const struct breakpoint *b)
5111 {
5112 return (b->type == bp_breakpoint
5113 || b->type == bp_catchpoint
5114 || b->type == bp_hardware_breakpoint
5115 || is_tracepoint (b)
5116 || is_watchpoint (b));
5117 }
5118
5119 /* Return true if this breakpoint was set by the user, false if it is
5120 internal or momentary. */
5121
5122 int
5123 user_breakpoint_p (struct breakpoint *b)
5124 {
5125 return user_settable_breakpoint (b) && b->number > 0;
5126 }
5127
5128 /* Print information on user settable breakpoint (watchpoint, etc)
5129 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5130 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5131 FILTER is non-NULL, call it on each breakpoint and only include the
5132 ones for which it returns non-zero. Return the total number of
5133 breakpoints listed. */
5134
5135 static int
5136 breakpoint_1 (char *args, int allflag,
5137 int (*filter) (const struct breakpoint *))
5138 {
5139 struct breakpoint *b;
5140 struct bp_location *last_loc = NULL;
5141 int nr_printable_breakpoints;
5142 struct cleanup *bkpttbl_chain;
5143 struct value_print_options opts;
5144 int print_address_bits = 0;
5145 int print_type_col_width = 14;
5146
5147 get_user_print_options (&opts);
5148
5149 /* Compute the number of rows in the table, as well as the size
5150 required for address fields. */
5151 nr_printable_breakpoints = 0;
5152 ALL_BREAKPOINTS (b)
5153 {
5154 /* If we have a filter, only list the breakpoints it accepts. */
5155 if (filter && !filter (b))
5156 continue;
5157
5158 /* If we have an "args" string, it is a list of breakpoints to
5159 accept. Skip the others. */
5160 if (args != NULL && *args != '\0')
5161 {
5162 if (allflag && parse_and_eval_long (args) != b->number)
5163 continue;
5164 if (!allflag && !number_is_in_list (args, b->number))
5165 continue;
5166 }
5167
5168 if (allflag || user_breakpoint_p (b))
5169 {
5170 int addr_bit, type_len;
5171
5172 addr_bit = breakpoint_address_bits (b);
5173 if (addr_bit > print_address_bits)
5174 print_address_bits = addr_bit;
5175
5176 type_len = strlen (bptype_string (b->type));
5177 if (type_len > print_type_col_width)
5178 print_type_col_width = type_len;
5179
5180 nr_printable_breakpoints++;
5181 }
5182 }
5183
5184 if (opts.addressprint)
5185 bkpttbl_chain
5186 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5187 nr_printable_breakpoints,
5188 "BreakpointTable");
5189 else
5190 bkpttbl_chain
5191 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5192 nr_printable_breakpoints,
5193 "BreakpointTable");
5194
5195 if (nr_printable_breakpoints > 0)
5196 annotate_breakpoints_headers ();
5197 if (nr_printable_breakpoints > 0)
5198 annotate_field (0);
5199 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
5200 if (nr_printable_breakpoints > 0)
5201 annotate_field (1);
5202 ui_out_table_header (uiout, print_type_col_width, ui_left,
5203 "type", "Type"); /* 2 */
5204 if (nr_printable_breakpoints > 0)
5205 annotate_field (2);
5206 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
5207 if (nr_printable_breakpoints > 0)
5208 annotate_field (3);
5209 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
5210 if (opts.addressprint)
5211 {
5212 if (nr_printable_breakpoints > 0)
5213 annotate_field (4);
5214 if (print_address_bits <= 32)
5215 ui_out_table_header (uiout, 10, ui_left,
5216 "addr", "Address"); /* 5 */
5217 else
5218 ui_out_table_header (uiout, 18, ui_left,
5219 "addr", "Address"); /* 5 */
5220 }
5221 if (nr_printable_breakpoints > 0)
5222 annotate_field (5);
5223 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5224 ui_out_table_body (uiout);
5225 if (nr_printable_breakpoints > 0)
5226 annotate_breakpoints_table ();
5227
5228 ALL_BREAKPOINTS (b)
5229 {
5230 QUIT;
5231 /* If we have a filter, only list the breakpoints it accepts. */
5232 if (filter && !filter (b))
5233 continue;
5234
5235 /* If we have an "args" string, it is a list of breakpoints to
5236 accept. Skip the others. */
5237
5238 if (args != NULL && *args != '\0')
5239 {
5240 if (allflag) /* maintenance info breakpoint */
5241 {
5242 if (parse_and_eval_long (args) != b->number)
5243 continue;
5244 }
5245 else /* all others */
5246 {
5247 if (!number_is_in_list (args, b->number))
5248 continue;
5249 }
5250 }
5251 /* We only print out user settable breakpoints unless the
5252 allflag is set. */
5253 if (allflag || user_breakpoint_p (b))
5254 print_one_breakpoint (b, &last_loc, allflag);
5255 }
5256
5257 do_cleanups (bkpttbl_chain);
5258
5259 if (nr_printable_breakpoints == 0)
5260 {
5261 /* If there's a filter, let the caller decide how to report
5262 empty list. */
5263 if (!filter)
5264 {
5265 if (args == NULL || *args == '\0')
5266 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5267 else
5268 ui_out_message (uiout, 0,
5269 "No breakpoint or watchpoint matching '%s'.\n",
5270 args);
5271 }
5272 }
5273 else
5274 {
5275 if (last_loc && !server_command)
5276 set_next_address (last_loc->gdbarch, last_loc->address);
5277 }
5278
5279 /* FIXME? Should this be moved up so that it is only called when
5280 there have been breakpoints? */
5281 annotate_breakpoints_table_end ();
5282
5283 return nr_printable_breakpoints;
5284 }
5285
5286 /* Display the value of default-collect in a way that is generally
5287 compatible with the breakpoint list. */
5288
5289 static void
5290 default_collect_info (void)
5291 {
5292 /* If it has no value (which is frequently the case), say nothing; a
5293 message like "No default-collect." gets in user's face when it's
5294 not wanted. */
5295 if (!*default_collect)
5296 return;
5297
5298 /* The following phrase lines up nicely with per-tracepoint collect
5299 actions. */
5300 ui_out_text (uiout, "default collect ");
5301 ui_out_field_string (uiout, "default-collect", default_collect);
5302 ui_out_text (uiout, " \n");
5303 }
5304
5305 static void
5306 breakpoints_info (char *args, int from_tty)
5307 {
5308 breakpoint_1 (args, 0, NULL);
5309
5310 default_collect_info ();
5311 }
5312
5313 static void
5314 watchpoints_info (char *args, int from_tty)
5315 {
5316 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
5317
5318 if (num_printed == 0)
5319 {
5320 if (args == NULL || *args == '\0')
5321 ui_out_message (uiout, 0, "No watchpoints.\n");
5322 else
5323 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
5324 }
5325 }
5326
5327 static void
5328 maintenance_info_breakpoints (char *args, int from_tty)
5329 {
5330 breakpoint_1 (args, 1, NULL);
5331
5332 default_collect_info ();
5333 }
5334
5335 static int
5336 breakpoint_has_pc (struct breakpoint *b,
5337 struct program_space *pspace,
5338 CORE_ADDR pc, struct obj_section *section)
5339 {
5340 struct bp_location *bl = b->loc;
5341
5342 for (; bl; bl = bl->next)
5343 {
5344 if (bl->pspace == pspace
5345 && bl->address == pc
5346 && (!overlay_debugging || bl->section == section))
5347 return 1;
5348 }
5349 return 0;
5350 }
5351
5352 /* Print a message describing any breakpoints set at PC. This
5353 concerns with logical breakpoints, so we match program spaces, not
5354 address spaces. */
5355
5356 static void
5357 describe_other_breakpoints (struct gdbarch *gdbarch,
5358 struct program_space *pspace, CORE_ADDR pc,
5359 struct obj_section *section, int thread)
5360 {
5361 int others = 0;
5362 struct breakpoint *b;
5363
5364 ALL_BREAKPOINTS (b)
5365 others += breakpoint_has_pc (b, pspace, pc, section);
5366 if (others > 0)
5367 {
5368 if (others == 1)
5369 printf_filtered (_("Note: breakpoint "));
5370 else /* if (others == ???) */
5371 printf_filtered (_("Note: breakpoints "));
5372 ALL_BREAKPOINTS (b)
5373 if (breakpoint_has_pc (b, pspace, pc, section))
5374 {
5375 others--;
5376 printf_filtered ("%d", b->number);
5377 if (b->thread == -1 && thread != -1)
5378 printf_filtered (" (all threads)");
5379 else if (b->thread != -1)
5380 printf_filtered (" (thread %d)", b->thread);
5381 printf_filtered ("%s%s ",
5382 ((b->enable_state == bp_disabled
5383 || b->enable_state == bp_call_disabled
5384 || b->enable_state == bp_startup_disabled)
5385 ? " (disabled)"
5386 : b->enable_state == bp_permanent
5387 ? " (permanent)"
5388 : ""),
5389 (others > 1) ? ","
5390 : ((others == 1) ? " and" : ""));
5391 }
5392 printf_filtered (_("also set at pc "));
5393 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
5394 printf_filtered (".\n");
5395 }
5396 }
5397 \f
5398 /* Set the default place to put a breakpoint
5399 for the `break' command with no arguments. */
5400
5401 void
5402 set_default_breakpoint (int valid, struct program_space *pspace,
5403 CORE_ADDR addr, struct symtab *symtab,
5404 int line)
5405 {
5406 default_breakpoint_valid = valid;
5407 default_breakpoint_pspace = pspace;
5408 default_breakpoint_address = addr;
5409 default_breakpoint_symtab = symtab;
5410 default_breakpoint_line = line;
5411 }
5412
5413 /* Return true iff it is meaningful to use the address member of
5414 BPT. For some breakpoint types, the address member is irrelevant
5415 and it makes no sense to attempt to compare it to other addresses
5416 (or use it for any other purpose either).
5417
5418 More specifically, each of the following breakpoint types will
5419 always have a zero valued address and we don't want to mark
5420 breakpoints of any of these types to be a duplicate of an actual
5421 breakpoint at address zero:
5422
5423 bp_watchpoint
5424 bp_catchpoint
5425
5426 */
5427
5428 static int
5429 breakpoint_address_is_meaningful (struct breakpoint *bpt)
5430 {
5431 enum bptype type = bpt->type;
5432
5433 return (type != bp_watchpoint && type != bp_catchpoint);
5434 }
5435
5436 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5437 true if LOC1 and LOC2 represent the same watchpoint location. */
5438
5439 static int
5440 watchpoint_locations_match (struct bp_location *loc1,
5441 struct bp_location *loc2)
5442 {
5443 /* Both of them must not be in moribund_locations. */
5444 gdb_assert (loc1->owner != NULL);
5445 gdb_assert (loc2->owner != NULL);
5446
5447 /* If the target can evaluate the condition expression in hardware,
5448 then we we need to insert both watchpoints even if they are at
5449 the same place. Otherwise the watchpoint will only trigger when
5450 the condition of whichever watchpoint was inserted evaluates to
5451 true, not giving a chance for GDB to check the condition of the
5452 other watchpoint. */
5453 if ((loc1->owner->cond_exp
5454 && target_can_accel_watchpoint_condition (loc1->address,
5455 loc1->length,
5456 loc1->watchpoint_type,
5457 loc1->owner->cond_exp))
5458 || (loc2->owner->cond_exp
5459 && target_can_accel_watchpoint_condition (loc2->address,
5460 loc2->length,
5461 loc2->watchpoint_type,
5462 loc2->owner->cond_exp)))
5463 return 0;
5464
5465 /* Note that this checks the owner's type, not the location's. In
5466 case the target does not support read watchpoints, but does
5467 support access watchpoints, we'll have bp_read_watchpoint
5468 watchpoints with hw_access locations. Those should be considered
5469 duplicates of hw_read locations. The hw_read locations will
5470 become hw_access locations later. */
5471 return (loc1->owner->type == loc2->owner->type
5472 && loc1->pspace->aspace == loc2->pspace->aspace
5473 && loc1->address == loc2->address
5474 && loc1->length == loc2->length);
5475 }
5476
5477 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5478 same breakpoint location. In most targets, this can only be true
5479 if ASPACE1 matches ASPACE2. On targets that have global
5480 breakpoints, the address space doesn't really matter. */
5481
5482 static int
5483 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5484 struct address_space *aspace2, CORE_ADDR addr2)
5485 {
5486 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5487 || aspace1 == aspace2)
5488 && addr1 == addr2);
5489 }
5490
5491 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
5492 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5493 represent the same location. */
5494
5495 static int
5496 breakpoint_locations_match (struct bp_location *loc1,
5497 struct bp_location *loc2)
5498 {
5499 int hw_point1, hw_point2;
5500
5501 /* Both of them must not be in moribund_locations. */
5502 gdb_assert (loc1->owner != NULL);
5503 gdb_assert (loc2->owner != NULL);
5504
5505 hw_point1 = is_hardware_watchpoint (loc1->owner);
5506 hw_point2 = is_hardware_watchpoint (loc2->owner);
5507
5508 if (hw_point1 != hw_point2)
5509 return 0;
5510 else if (hw_point1)
5511 return watchpoint_locations_match (loc1, loc2);
5512 else
5513 return breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5514 loc2->pspace->aspace, loc2->address);
5515 }
5516
5517 static void
5518 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5519 int bnum, int have_bnum)
5520 {
5521 /* The longest string possibly returned by hex_string_custom
5522 is 50 chars. These must be at least that big for safety. */
5523 char astr1[64];
5524 char astr2[64];
5525
5526 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5527 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
5528 if (have_bnum)
5529 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
5530 bnum, astr1, astr2);
5531 else
5532 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
5533 }
5534
5535 /* Adjust a breakpoint's address to account for architectural
5536 constraints on breakpoint placement. Return the adjusted address.
5537 Note: Very few targets require this kind of adjustment. For most
5538 targets, this function is simply the identity function. */
5539
5540 static CORE_ADDR
5541 adjust_breakpoint_address (struct gdbarch *gdbarch,
5542 CORE_ADDR bpaddr, enum bptype bptype)
5543 {
5544 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
5545 {
5546 /* Very few targets need any kind of breakpoint adjustment. */
5547 return bpaddr;
5548 }
5549 else if (bptype == bp_watchpoint
5550 || bptype == bp_hardware_watchpoint
5551 || bptype == bp_read_watchpoint
5552 || bptype == bp_access_watchpoint
5553 || bptype == bp_catchpoint)
5554 {
5555 /* Watchpoints and the various bp_catch_* eventpoints should not
5556 have their addresses modified. */
5557 return bpaddr;
5558 }
5559 else
5560 {
5561 CORE_ADDR adjusted_bpaddr;
5562
5563 /* Some targets have architectural constraints on the placement
5564 of breakpoint instructions. Obtain the adjusted address. */
5565 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
5566
5567 /* An adjusted breakpoint address can significantly alter
5568 a user's expectations. Print a warning if an adjustment
5569 is required. */
5570 if (adjusted_bpaddr != bpaddr)
5571 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5572
5573 return adjusted_bpaddr;
5574 }
5575 }
5576
5577 /* Allocate a struct bp_location. */
5578
5579 static struct bp_location *
5580 allocate_bp_location (struct breakpoint *bpt)
5581 {
5582 struct bp_location *loc;
5583
5584 loc = xmalloc (sizeof (struct bp_location));
5585 memset (loc, 0, sizeof (*loc));
5586
5587 loc->owner = bpt;
5588 loc->cond = NULL;
5589 loc->shlib_disabled = 0;
5590 loc->enabled = 1;
5591
5592 switch (bpt->type)
5593 {
5594 case bp_breakpoint:
5595 case bp_until:
5596 case bp_finish:
5597 case bp_longjmp:
5598 case bp_longjmp_resume:
5599 case bp_exception:
5600 case bp_exception_resume:
5601 case bp_step_resume:
5602 case bp_watchpoint_scope:
5603 case bp_call_dummy:
5604 case bp_std_terminate:
5605 case bp_shlib_event:
5606 case bp_thread_event:
5607 case bp_overlay_event:
5608 case bp_jit_event:
5609 case bp_longjmp_master:
5610 case bp_std_terminate_master:
5611 case bp_exception_master:
5612 loc->loc_type = bp_loc_software_breakpoint;
5613 break;
5614 case bp_hardware_breakpoint:
5615 loc->loc_type = bp_loc_hardware_breakpoint;
5616 break;
5617 case bp_hardware_watchpoint:
5618 case bp_read_watchpoint:
5619 case bp_access_watchpoint:
5620 loc->loc_type = bp_loc_hardware_watchpoint;
5621 break;
5622 case bp_watchpoint:
5623 case bp_catchpoint:
5624 case bp_tracepoint:
5625 case bp_fast_tracepoint:
5626 case bp_static_tracepoint:
5627 loc->loc_type = bp_loc_other;
5628 break;
5629 default:
5630 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
5631 }
5632
5633 loc->refc = 1;
5634 return loc;
5635 }
5636
5637 static void
5638 free_bp_location (struct bp_location *loc)
5639 {
5640 if (loc->cond)
5641 xfree (loc->cond);
5642
5643 if (loc->function_name)
5644 xfree (loc->function_name);
5645
5646 xfree (loc);
5647 }
5648
5649 /* Increment reference count. */
5650
5651 static void
5652 incref_bp_location (struct bp_location *bl)
5653 {
5654 ++bl->refc;
5655 }
5656
5657 /* Decrement reference count. If the reference count reaches 0,
5658 destroy the bp_location. Sets *BLP to NULL. */
5659
5660 static void
5661 decref_bp_location (struct bp_location **blp)
5662 {
5663 gdb_assert ((*blp)->refc > 0);
5664
5665 if (--(*blp)->refc == 0)
5666 free_bp_location (*blp);
5667 *blp = NULL;
5668 }
5669
5670 /* Helper to set_raw_breakpoint below. Creates a breakpoint that has
5671 type BPTYPE and has no locations as yet. */
5672 /* This function is used in gdbtk sources and thus can not be made
5673 static. */
5674
5675 static struct breakpoint *
5676 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
5677 enum bptype bptype)
5678 {
5679 struct breakpoint *b, *b1;
5680
5681 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
5682 memset (b, 0, sizeof (*b));
5683
5684 b->type = bptype;
5685 b->gdbarch = gdbarch;
5686 b->language = current_language->la_language;
5687 b->input_radix = input_radix;
5688 b->thread = -1;
5689 b->enable_state = bp_enabled;
5690 b->next = 0;
5691 b->silent = 0;
5692 b->ignore_count = 0;
5693 b->commands = NULL;
5694 b->frame_id = null_frame_id;
5695 b->forked_inferior_pid = null_ptid;
5696 b->exec_pathname = NULL;
5697 b->syscalls_to_be_caught = NULL;
5698 b->ops = NULL;
5699 b->condition_not_parsed = 0;
5700 b->py_bp_object = NULL;
5701
5702 /* Add this breakpoint to the end of the chain so that a list of
5703 breakpoints will come out in order of increasing numbers. */
5704
5705 b1 = breakpoint_chain;
5706 if (b1 == 0)
5707 breakpoint_chain = b;
5708 else
5709 {
5710 while (b1->next)
5711 b1 = b1->next;
5712 b1->next = b;
5713 }
5714 return b;
5715 }
5716
5717 /* Initialize loc->function_name. */
5718 static void
5719 set_breakpoint_location_function (struct bp_location *loc)
5720 {
5721 gdb_assert (loc->owner != NULL);
5722
5723 if (loc->owner->type == bp_breakpoint
5724 || loc->owner->type == bp_hardware_breakpoint
5725 || is_tracepoint (loc->owner))
5726 {
5727 find_pc_partial_function (loc->address, &(loc->function_name),
5728 NULL, NULL);
5729 if (loc->function_name)
5730 loc->function_name = xstrdup (loc->function_name);
5731 }
5732 }
5733
5734 /* Attempt to determine architecture of location identified by SAL. */
5735 static struct gdbarch *
5736 get_sal_arch (struct symtab_and_line sal)
5737 {
5738 if (sal.section)
5739 return get_objfile_arch (sal.section->objfile);
5740 if (sal.symtab)
5741 return get_objfile_arch (sal.symtab->objfile);
5742
5743 return NULL;
5744 }
5745
5746 /* set_raw_breakpoint is a low level routine for allocating and
5747 partially initializing a breakpoint of type BPTYPE. The newly
5748 created breakpoint's address, section, source file name, and line
5749 number are provided by SAL. The newly created and partially
5750 initialized breakpoint is added to the breakpoint chain and
5751 is also returned as the value of this function.
5752
5753 It is expected that the caller will complete the initialization of
5754 the newly created breakpoint struct as well as output any status
5755 information regarding the creation of a new breakpoint. In
5756 particular, set_raw_breakpoint does NOT set the breakpoint
5757 number! Care should be taken to not allow an error to occur
5758 prior to completing the initialization of the breakpoint. If this
5759 should happen, a bogus breakpoint will be left on the chain. */
5760
5761 struct breakpoint *
5762 set_raw_breakpoint (struct gdbarch *gdbarch,
5763 struct symtab_and_line sal, enum bptype bptype)
5764 {
5765 struct breakpoint *b = set_raw_breakpoint_without_location (gdbarch,
5766 bptype);
5767 CORE_ADDR adjusted_address;
5768 struct gdbarch *loc_gdbarch;
5769
5770 loc_gdbarch = get_sal_arch (sal);
5771 if (!loc_gdbarch)
5772 loc_gdbarch = b->gdbarch;
5773
5774 if (bptype != bp_catchpoint)
5775 gdb_assert (sal.pspace != NULL);
5776
5777 /* Adjust the breakpoint's address prior to allocating a location.
5778 Once we call allocate_bp_location(), that mostly uninitialized
5779 location will be placed on the location chain. Adjustment of the
5780 breakpoint may cause target_read_memory() to be called and we do
5781 not want its scan of the location chain to find a breakpoint and
5782 location that's only been partially initialized. */
5783 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5784 sal.pc, b->type);
5785
5786 b->loc = allocate_bp_location (b);
5787 b->loc->gdbarch = loc_gdbarch;
5788 b->loc->requested_address = sal.pc;
5789 b->loc->address = adjusted_address;
5790 b->loc->pspace = sal.pspace;
5791
5792 /* Store the program space that was used to set the breakpoint, for
5793 breakpoint resetting. */
5794 b->pspace = sal.pspace;
5795
5796 if (sal.symtab == NULL)
5797 b->source_file = NULL;
5798 else
5799 b->source_file = xstrdup (sal.symtab->filename);
5800 b->loc->section = sal.section;
5801 b->line_number = sal.line;
5802
5803 set_breakpoint_location_function (b->loc);
5804
5805 breakpoints_changed ();
5806
5807 return b;
5808 }
5809
5810
5811 /* Note that the breakpoint object B describes a permanent breakpoint
5812 instruction, hard-wired into the inferior's code. */
5813 void
5814 make_breakpoint_permanent (struct breakpoint *b)
5815 {
5816 struct bp_location *bl;
5817
5818 b->enable_state = bp_permanent;
5819
5820 /* By definition, permanent breakpoints are already present in the
5821 code. Mark all locations as inserted. For now,
5822 make_breakpoint_permanent is called in just one place, so it's
5823 hard to say if it's reasonable to have permanent breakpoint with
5824 multiple locations or not, but it's easy to implmement. */
5825 for (bl = b->loc; bl; bl = bl->next)
5826 bl->inserted = 1;
5827 }
5828
5829 /* Call this routine when stepping and nexting to enable a breakpoint
5830 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5831 initiated the operation. */
5832
5833 void
5834 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
5835 {
5836 struct breakpoint *b, *b_tmp;
5837 int thread = tp->num;
5838
5839 /* To avoid having to rescan all objfile symbols at every step,
5840 we maintain a list of continually-inserted but always disabled
5841 longjmp "master" breakpoints. Here, we simply create momentary
5842 clones of those and enable them for the requested thread. */
5843 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5844 if (b->pspace == current_program_space
5845 && (b->type == bp_longjmp_master
5846 || b->type == bp_exception_master))
5847 {
5848 struct breakpoint *clone = clone_momentary_breakpoint (b);
5849
5850 clone->type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5851 clone->thread = thread;
5852 }
5853
5854 tp->initiating_frame = frame;
5855 }
5856
5857 /* Delete all longjmp breakpoints from THREAD. */
5858 void
5859 delete_longjmp_breakpoint (int thread)
5860 {
5861 struct breakpoint *b, *b_tmp;
5862
5863 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5864 if (b->type == bp_longjmp || b->type == bp_exception)
5865 {
5866 if (b->thread == thread)
5867 delete_breakpoint (b);
5868 }
5869 }
5870
5871 void
5872 enable_overlay_breakpoints (void)
5873 {
5874 struct breakpoint *b;
5875
5876 ALL_BREAKPOINTS (b)
5877 if (b->type == bp_overlay_event)
5878 {
5879 b->enable_state = bp_enabled;
5880 update_global_location_list (1);
5881 overlay_events_enabled = 1;
5882 }
5883 }
5884
5885 void
5886 disable_overlay_breakpoints (void)
5887 {
5888 struct breakpoint *b;
5889
5890 ALL_BREAKPOINTS (b)
5891 if (b->type == bp_overlay_event)
5892 {
5893 b->enable_state = bp_disabled;
5894 update_global_location_list (0);
5895 overlay_events_enabled = 0;
5896 }
5897 }
5898
5899 /* Set an active std::terminate breakpoint for each std::terminate
5900 master breakpoint. */
5901 void
5902 set_std_terminate_breakpoint (void)
5903 {
5904 struct breakpoint *b, *b_tmp;
5905
5906 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5907 if (b->pspace == current_program_space
5908 && b->type == bp_std_terminate_master)
5909 {
5910 struct breakpoint *clone = clone_momentary_breakpoint (b);
5911 clone->type = bp_std_terminate;
5912 }
5913 }
5914
5915 /* Delete all the std::terminate breakpoints. */
5916 void
5917 delete_std_terminate_breakpoint (void)
5918 {
5919 struct breakpoint *b, *b_tmp;
5920
5921 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5922 if (b->type == bp_std_terminate)
5923 delete_breakpoint (b);
5924 }
5925
5926 struct breakpoint *
5927 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5928 {
5929 struct breakpoint *b;
5930
5931 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
5932
5933 b->enable_state = bp_enabled;
5934 /* addr_string has to be used or breakpoint_re_set will delete me. */
5935 b->addr_string
5936 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
5937
5938 update_global_location_list_nothrow (1);
5939
5940 return b;
5941 }
5942
5943 void
5944 remove_thread_event_breakpoints (void)
5945 {
5946 struct breakpoint *b, *b_tmp;
5947
5948 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5949 if (b->type == bp_thread_event
5950 && b->loc->pspace == current_program_space)
5951 delete_breakpoint (b);
5952 }
5953
5954 struct captured_parse_breakpoint_args
5955 {
5956 char **arg_p;
5957 struct symtabs_and_lines *sals_p;
5958 char ***addr_string_p;
5959 int *not_found_ptr;
5960 };
5961
5962 struct lang_and_radix
5963 {
5964 enum language lang;
5965 int radix;
5966 };
5967
5968 /* Create a breakpoint for JIT code registration and unregistration. */
5969
5970 struct breakpoint *
5971 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5972 {
5973 struct breakpoint *b;
5974
5975 b = create_internal_breakpoint (gdbarch, address, bp_jit_event);
5976 update_global_location_list_nothrow (1);
5977 return b;
5978 }
5979
5980 /* Remove JIT code registration and unregistration breakpoint(s). */
5981
5982 void
5983 remove_jit_event_breakpoints (void)
5984 {
5985 struct breakpoint *b, *b_tmp;
5986
5987 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5988 if (b->type == bp_jit_event
5989 && b->loc->pspace == current_program_space)
5990 delete_breakpoint (b);
5991 }
5992
5993 void
5994 remove_solib_event_breakpoints (void)
5995 {
5996 struct breakpoint *b, *b_tmp;
5997
5998 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5999 if (b->type == bp_shlib_event
6000 && b->loc->pspace == current_program_space)
6001 delete_breakpoint (b);
6002 }
6003
6004 struct breakpoint *
6005 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6006 {
6007 struct breakpoint *b;
6008
6009 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
6010 update_global_location_list_nothrow (1);
6011 return b;
6012 }
6013
6014 /* Disable any breakpoints that are on code in shared libraries. Only
6015 apply to enabled breakpoints, disabled ones can just stay disabled. */
6016
6017 void
6018 disable_breakpoints_in_shlibs (void)
6019 {
6020 struct bp_location *loc, **locp_tmp;
6021
6022 ALL_BP_LOCATIONS (loc, locp_tmp)
6023 {
6024 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6025 struct breakpoint *b = loc->owner;
6026
6027 /* We apply the check to all breakpoints, including disabled for
6028 those with loc->duplicate set. This is so that when breakpoint
6029 becomes enabled, or the duplicate is removed, gdb will try to
6030 insert all breakpoints. If we don't set shlib_disabled here,
6031 we'll try to insert those breakpoints and fail. */
6032 if (((b->type == bp_breakpoint)
6033 || (b->type == bp_jit_event)
6034 || (b->type == bp_hardware_breakpoint)
6035 || (is_tracepoint (b)))
6036 && loc->pspace == current_program_space
6037 && !loc->shlib_disabled
6038 #ifdef PC_SOLIB
6039 && PC_SOLIB (loc->address)
6040 #else
6041 && solib_name_from_address (loc->pspace, loc->address)
6042 #endif
6043 )
6044 {
6045 loc->shlib_disabled = 1;
6046 }
6047 }
6048 }
6049
6050 /* Disable any breakpoints that are in in an unloaded shared library.
6051 Only apply to enabled breakpoints, disabled ones can just stay
6052 disabled. */
6053
6054 static void
6055 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6056 {
6057 struct bp_location *loc, **locp_tmp;
6058 int disabled_shlib_breaks = 0;
6059
6060 /* SunOS a.out shared libraries are always mapped, so do not
6061 disable breakpoints; they will only be reported as unloaded
6062 through clear_solib when GDB discards its shared library
6063 list. See clear_solib for more information. */
6064 if (exec_bfd != NULL
6065 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6066 return;
6067
6068 ALL_BP_LOCATIONS (loc, locp_tmp)
6069 {
6070 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6071 struct breakpoint *b = loc->owner;
6072
6073 if ((loc->loc_type == bp_loc_hardware_breakpoint
6074 || loc->loc_type == bp_loc_software_breakpoint)
6075 && solib->pspace == loc->pspace
6076 && !loc->shlib_disabled
6077 && (b->type == bp_breakpoint
6078 || b->type == bp_jit_event
6079 || b->type == bp_hardware_breakpoint)
6080 && solib_contains_address_p (solib, loc->address))
6081 {
6082 loc->shlib_disabled = 1;
6083 /* At this point, we cannot rely on remove_breakpoint
6084 succeeding so we must mark the breakpoint as not inserted
6085 to prevent future errors occurring in remove_breakpoints. */
6086 loc->inserted = 0;
6087 if (!disabled_shlib_breaks)
6088 {
6089 target_terminal_ours_for_output ();
6090 warning (_("Temporarily disabling breakpoints "
6091 "for unloaded shared library \"%s\""),
6092 solib->so_name);
6093 }
6094 disabled_shlib_breaks = 1;
6095 }
6096 }
6097 }
6098
6099 /* FORK & VFORK catchpoints. */
6100
6101 /* Implement the "insert" breakpoint_ops method for fork
6102 catchpoints. */
6103
6104 static int
6105 insert_catch_fork (struct bp_location *bl)
6106 {
6107 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
6108 }
6109
6110 /* Implement the "remove" breakpoint_ops method for fork
6111 catchpoints. */
6112
6113 static int
6114 remove_catch_fork (struct bp_location *bl)
6115 {
6116 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6117 }
6118
6119 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
6120 catchpoints. */
6121
6122 static int
6123 breakpoint_hit_catch_fork (struct breakpoint *b)
6124 {
6125 return inferior_has_forked (inferior_ptid, &b->forked_inferior_pid);
6126 }
6127
6128 /* Implement the "print_it" breakpoint_ops method for fork
6129 catchpoints. */
6130
6131 static enum print_stop_action
6132 print_it_catch_fork (struct breakpoint *b)
6133 {
6134 annotate_catchpoint (b->number);
6135 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
6136 b->number, ptid_get_pid (b->forked_inferior_pid));
6137 return PRINT_SRC_AND_LOC;
6138 }
6139
6140 /* Implement the "print_one" breakpoint_ops method for fork
6141 catchpoints. */
6142
6143 static void
6144 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
6145 {
6146 struct value_print_options opts;
6147
6148 get_user_print_options (&opts);
6149
6150 /* Field 4, the address, is omitted (which makes the columns not
6151 line up too nicely with the headers, but the effect is relatively
6152 readable). */
6153 if (opts.addressprint)
6154 ui_out_field_skip (uiout, "addr");
6155 annotate_field (5);
6156 ui_out_text (uiout, "fork");
6157 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6158 {
6159 ui_out_text (uiout, ", process ");
6160 ui_out_field_int (uiout, "what",
6161 ptid_get_pid (b->forked_inferior_pid));
6162 ui_out_spaces (uiout, 1);
6163 }
6164 }
6165
6166 /* Implement the "print_mention" breakpoint_ops method for fork
6167 catchpoints. */
6168
6169 static void
6170 print_mention_catch_fork (struct breakpoint *b)
6171 {
6172 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6173 }
6174
6175 /* Implement the "print_recreate" breakpoint_ops method for fork
6176 catchpoints. */
6177
6178 static void
6179 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6180 {
6181 fprintf_unfiltered (fp, "catch fork");
6182 }
6183
6184 /* The breakpoint_ops structure to be used in fork catchpoints. */
6185
6186 static struct breakpoint_ops catch_fork_breakpoint_ops =
6187 {
6188 insert_catch_fork,
6189 remove_catch_fork,
6190 breakpoint_hit_catch_fork,
6191 NULL, /* resources_needed */
6192 print_it_catch_fork,
6193 print_one_catch_fork,
6194 print_mention_catch_fork,
6195 print_recreate_catch_fork
6196 };
6197
6198 /* Implement the "insert" breakpoint_ops method for vfork
6199 catchpoints. */
6200
6201 static int
6202 insert_catch_vfork (struct bp_location *bl)
6203 {
6204 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
6205 }
6206
6207 /* Implement the "remove" breakpoint_ops method for vfork
6208 catchpoints. */
6209
6210 static int
6211 remove_catch_vfork (struct bp_location *bl)
6212 {
6213 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6214 }
6215
6216 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6217 catchpoints. */
6218
6219 static int
6220 breakpoint_hit_catch_vfork (struct breakpoint *b)
6221 {
6222 return inferior_has_vforked (inferior_ptid, &b->forked_inferior_pid);
6223 }
6224
6225 /* Implement the "print_it" breakpoint_ops method for vfork
6226 catchpoints. */
6227
6228 static enum print_stop_action
6229 print_it_catch_vfork (struct breakpoint *b)
6230 {
6231 annotate_catchpoint (b->number);
6232 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
6233 b->number, ptid_get_pid (b->forked_inferior_pid));
6234 return PRINT_SRC_AND_LOC;
6235 }
6236
6237 /* Implement the "print_one" breakpoint_ops method for vfork
6238 catchpoints. */
6239
6240 static void
6241 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
6242 {
6243 struct value_print_options opts;
6244
6245 get_user_print_options (&opts);
6246 /* Field 4, the address, is omitted (which makes the columns not
6247 line up too nicely with the headers, but the effect is relatively
6248 readable). */
6249 if (opts.addressprint)
6250 ui_out_field_skip (uiout, "addr");
6251 annotate_field (5);
6252 ui_out_text (uiout, "vfork");
6253 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6254 {
6255 ui_out_text (uiout, ", process ");
6256 ui_out_field_int (uiout, "what",
6257 ptid_get_pid (b->forked_inferior_pid));
6258 ui_out_spaces (uiout, 1);
6259 }
6260 }
6261
6262 /* Implement the "print_mention" breakpoint_ops method for vfork
6263 catchpoints. */
6264
6265 static void
6266 print_mention_catch_vfork (struct breakpoint *b)
6267 {
6268 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6269 }
6270
6271 /* Implement the "print_recreate" breakpoint_ops method for vfork
6272 catchpoints. */
6273
6274 static void
6275 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6276 {
6277 fprintf_unfiltered (fp, "catch vfork");
6278 }
6279
6280 /* The breakpoint_ops structure to be used in vfork catchpoints. */
6281
6282 static struct breakpoint_ops catch_vfork_breakpoint_ops =
6283 {
6284 insert_catch_vfork,
6285 remove_catch_vfork,
6286 breakpoint_hit_catch_vfork,
6287 NULL, /* resources_needed */
6288 print_it_catch_vfork,
6289 print_one_catch_vfork,
6290 print_mention_catch_vfork,
6291 print_recreate_catch_vfork
6292 };
6293
6294 /* Implement the "insert" breakpoint_ops method for syscall
6295 catchpoints. */
6296
6297 static int
6298 insert_catch_syscall (struct bp_location *bl)
6299 {
6300 struct inferior *inf = current_inferior ();
6301
6302 ++inf->total_syscalls_count;
6303 if (!bl->owner->syscalls_to_be_caught)
6304 ++inf->any_syscall_count;
6305 else
6306 {
6307 int i, iter;
6308
6309 for (i = 0;
6310 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
6311 i++)
6312 {
6313 int elem;
6314
6315 if (iter >= VEC_length (int, inf->syscalls_counts))
6316 {
6317 int old_size = VEC_length (int, inf->syscalls_counts);
6318 uintptr_t vec_addr_offset
6319 = old_size * ((uintptr_t) sizeof (int));
6320 uintptr_t vec_addr;
6321 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6322 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6323 vec_addr_offset;
6324 memset ((void *) vec_addr, 0,
6325 (iter + 1 - old_size) * sizeof (int));
6326 }
6327 elem = VEC_index (int, inf->syscalls_counts, iter);
6328 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6329 }
6330 }
6331
6332 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6333 inf->total_syscalls_count != 0,
6334 inf->any_syscall_count,
6335 VEC_length (int, inf->syscalls_counts),
6336 VEC_address (int, inf->syscalls_counts));
6337 }
6338
6339 /* Implement the "remove" breakpoint_ops method for syscall
6340 catchpoints. */
6341
6342 static int
6343 remove_catch_syscall (struct bp_location *bl)
6344 {
6345 struct inferior *inf = current_inferior ();
6346
6347 --inf->total_syscalls_count;
6348 if (!bl->owner->syscalls_to_be_caught)
6349 --inf->any_syscall_count;
6350 else
6351 {
6352 int i, iter;
6353
6354 for (i = 0;
6355 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
6356 i++)
6357 {
6358 int elem;
6359 if (iter >= VEC_length (int, inf->syscalls_counts))
6360 /* Shouldn't happen. */
6361 continue;
6362 elem = VEC_index (int, inf->syscalls_counts, iter);
6363 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6364 }
6365 }
6366
6367 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6368 inf->total_syscalls_count != 0,
6369 inf->any_syscall_count,
6370 VEC_length (int, inf->syscalls_counts),
6371 VEC_address (int,
6372 inf->syscalls_counts));
6373 }
6374
6375 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6376 catchpoints. */
6377
6378 static int
6379 breakpoint_hit_catch_syscall (struct breakpoint *b)
6380 {
6381 /* We must check if we are catching specific syscalls in this
6382 breakpoint. If we are, then we must guarantee that the called
6383 syscall is the same syscall we are catching. */
6384 int syscall_number = 0;
6385
6386 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6387 return 0;
6388
6389 /* Now, checking if the syscall is the same. */
6390 if (b->syscalls_to_be_caught)
6391 {
6392 int i, iter;
6393
6394 for (i = 0;
6395 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6396 i++)
6397 if (syscall_number == iter)
6398 break;
6399 /* Not the same. */
6400 if (!iter)
6401 return 0;
6402 }
6403
6404 return 1;
6405 }
6406
6407 /* Implement the "print_it" breakpoint_ops method for syscall
6408 catchpoints. */
6409
6410 static enum print_stop_action
6411 print_it_catch_syscall (struct breakpoint *b)
6412 {
6413 /* These are needed because we want to know in which state a
6414 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6415 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6416 must print "called syscall" or "returned from syscall". */
6417 ptid_t ptid;
6418 struct target_waitstatus last;
6419 struct syscall s;
6420 struct cleanup *old_chain;
6421 char *syscall_id;
6422
6423 get_last_target_status (&ptid, &last);
6424
6425 get_syscall_by_number (last.value.syscall_number, &s);
6426
6427 annotate_catchpoint (b->number);
6428
6429 if (s.name == NULL)
6430 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6431 else
6432 syscall_id = xstrprintf ("'%s'", s.name);
6433
6434 old_chain = make_cleanup (xfree, syscall_id);
6435
6436 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6437 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6438 b->number, syscall_id);
6439 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6440 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6441 b->number, syscall_id);
6442
6443 do_cleanups (old_chain);
6444
6445 return PRINT_SRC_AND_LOC;
6446 }
6447
6448 /* Implement the "print_one" breakpoint_ops method for syscall
6449 catchpoints. */
6450
6451 static void
6452 print_one_catch_syscall (struct breakpoint *b,
6453 struct bp_location **last_loc)
6454 {
6455 struct value_print_options opts;
6456
6457 get_user_print_options (&opts);
6458 /* Field 4, the address, is omitted (which makes the columns not
6459 line up too nicely with the headers, but the effect is relatively
6460 readable). */
6461 if (opts.addressprint)
6462 ui_out_field_skip (uiout, "addr");
6463 annotate_field (5);
6464
6465 if (b->syscalls_to_be_caught
6466 && VEC_length (int, b->syscalls_to_be_caught) > 1)
6467 ui_out_text (uiout, "syscalls \"");
6468 else
6469 ui_out_text (uiout, "syscall \"");
6470
6471 if (b->syscalls_to_be_caught)
6472 {
6473 int i, iter;
6474 char *text = xstrprintf ("%s", "");
6475
6476 for (i = 0;
6477 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6478 i++)
6479 {
6480 char *x = text;
6481 struct syscall s;
6482 get_syscall_by_number (iter, &s);
6483
6484 if (s.name != NULL)
6485 text = xstrprintf ("%s%s, ", text, s.name);
6486 else
6487 text = xstrprintf ("%s%d, ", text, iter);
6488
6489 /* We have to xfree the last 'text' (now stored at 'x')
6490 because xstrprintf dinamically allocates new space for it
6491 on every call. */
6492 xfree (x);
6493 }
6494 /* Remove the last comma. */
6495 text[strlen (text) - 2] = '\0';
6496 ui_out_field_string (uiout, "what", text);
6497 }
6498 else
6499 ui_out_field_string (uiout, "what", "<any syscall>");
6500 ui_out_text (uiout, "\" ");
6501 }
6502
6503 /* Implement the "print_mention" breakpoint_ops method for syscall
6504 catchpoints. */
6505
6506 static void
6507 print_mention_catch_syscall (struct breakpoint *b)
6508 {
6509 if (b->syscalls_to_be_caught)
6510 {
6511 int i, iter;
6512
6513 if (VEC_length (int, b->syscalls_to_be_caught) > 1)
6514 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6515 else
6516 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6517
6518 for (i = 0;
6519 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6520 i++)
6521 {
6522 struct syscall s;
6523 get_syscall_by_number (iter, &s);
6524
6525 if (s.name)
6526 printf_filtered (" '%s' [%d]", s.name, s.number);
6527 else
6528 printf_filtered (" %d", s.number);
6529 }
6530 printf_filtered (")");
6531 }
6532 else
6533 printf_filtered (_("Catchpoint %d (any syscall)"),
6534 b->number);
6535 }
6536
6537 /* Implement the "print_recreate" breakpoint_ops method for syscall
6538 catchpoints. */
6539
6540 static void
6541 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6542 {
6543 fprintf_unfiltered (fp, "catch syscall");
6544
6545 if (b->syscalls_to_be_caught)
6546 {
6547 int i, iter;
6548
6549 for (i = 0;
6550 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6551 i++)
6552 {
6553 struct syscall s;
6554
6555 get_syscall_by_number (iter, &s);
6556 if (s.name)
6557 fprintf_unfiltered (fp, " %s", s.name);
6558 else
6559 fprintf_unfiltered (fp, " %d", s.number);
6560 }
6561 }
6562 }
6563
6564 /* The breakpoint_ops structure to be used in syscall catchpoints. */
6565
6566 static struct breakpoint_ops catch_syscall_breakpoint_ops =
6567 {
6568 insert_catch_syscall,
6569 remove_catch_syscall,
6570 breakpoint_hit_catch_syscall,
6571 NULL, /* resources_needed */
6572 print_it_catch_syscall,
6573 print_one_catch_syscall,
6574 print_mention_catch_syscall,
6575 print_recreate_catch_syscall
6576 };
6577
6578 /* Returns non-zero if 'b' is a syscall catchpoint. */
6579
6580 static int
6581 syscall_catchpoint_p (struct breakpoint *b)
6582 {
6583 return (b->ops == &catch_syscall_breakpoint_ops);
6584 }
6585
6586 /* Create a new breakpoint of the bp_catchpoint kind and return it,
6587 but does NOT mention it nor update the global location list.
6588 This is useful if you need to fill more fields in the
6589 struct breakpoint before calling mention.
6590
6591 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6592 If COND_STRING is not NULL, then store it in the breakpoint.
6593 OPS, if not NULL, is the breakpoint_ops structure associated
6594 to the catchpoint. */
6595
6596 static struct breakpoint *
6597 create_catchpoint_without_mention (struct gdbarch *gdbarch, int tempflag,
6598 char *cond_string,
6599 struct breakpoint_ops *ops)
6600 {
6601 struct symtab_and_line sal;
6602 struct breakpoint *b;
6603
6604 init_sal (&sal);
6605 sal.pspace = current_program_space;
6606
6607 b = set_raw_breakpoint (gdbarch, sal, bp_catchpoint);
6608 set_breakpoint_count (breakpoint_count + 1);
6609 b->number = breakpoint_count;
6610
6611 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
6612 b->thread = -1;
6613 b->addr_string = NULL;
6614 b->enable_state = bp_enabled;
6615 b->disposition = tempflag ? disp_del : disp_donttouch;
6616 b->ops = ops;
6617
6618 return b;
6619 }
6620
6621 /* Create a new breakpoint of the bp_catchpoint kind and return it.
6622
6623 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6624 If COND_STRING is not NULL, then store it in the breakpoint.
6625 OPS, if not NULL, is the breakpoint_ops structure associated
6626 to the catchpoint. */
6627
6628 static struct breakpoint *
6629 create_catchpoint (struct gdbarch *gdbarch, int tempflag,
6630 char *cond_string, struct breakpoint_ops *ops)
6631 {
6632 struct breakpoint *b =
6633 create_catchpoint_without_mention (gdbarch, tempflag, cond_string, ops);
6634
6635 mention (b);
6636 update_global_location_list (1);
6637
6638 return b;
6639 }
6640
6641 static void
6642 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6643 int tempflag, char *cond_string,
6644 struct breakpoint_ops *ops)
6645 {
6646 struct breakpoint *b
6647 = create_catchpoint (gdbarch, tempflag, cond_string, ops);
6648
6649 /* FIXME: We should put this information in a breakpoint private data
6650 area. */
6651 b->forked_inferior_pid = null_ptid;
6652 }
6653
6654 /* Exec catchpoints. */
6655
6656 static int
6657 insert_catch_exec (struct bp_location *bl)
6658 {
6659 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
6660 }
6661
6662 static int
6663 remove_catch_exec (struct bp_location *bl)
6664 {
6665 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6666 }
6667
6668 static int
6669 breakpoint_hit_catch_exec (struct breakpoint *b)
6670 {
6671 return inferior_has_execd (inferior_ptid, &b->exec_pathname);
6672 }
6673
6674 static enum print_stop_action
6675 print_it_catch_exec (struct breakpoint *b)
6676 {
6677 annotate_catchpoint (b->number);
6678 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
6679 b->exec_pathname);
6680 return PRINT_SRC_AND_LOC;
6681 }
6682
6683 static void
6684 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
6685 {
6686 struct value_print_options opts;
6687
6688 get_user_print_options (&opts);
6689
6690 /* Field 4, the address, is omitted (which makes the columns
6691 not line up too nicely with the headers, but the effect
6692 is relatively readable). */
6693 if (opts.addressprint)
6694 ui_out_field_skip (uiout, "addr");
6695 annotate_field (5);
6696 ui_out_text (uiout, "exec");
6697 if (b->exec_pathname != NULL)
6698 {
6699 ui_out_text (uiout, ", program \"");
6700 ui_out_field_string (uiout, "what", b->exec_pathname);
6701 ui_out_text (uiout, "\" ");
6702 }
6703 }
6704
6705 static void
6706 print_mention_catch_exec (struct breakpoint *b)
6707 {
6708 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6709 }
6710
6711 /* Implement the "print_recreate" breakpoint_ops method for exec
6712 catchpoints. */
6713
6714 static void
6715 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6716 {
6717 fprintf_unfiltered (fp, "catch exec");
6718 }
6719
6720 static struct breakpoint_ops catch_exec_breakpoint_ops =
6721 {
6722 insert_catch_exec,
6723 remove_catch_exec,
6724 breakpoint_hit_catch_exec,
6725 NULL, /* resources_needed */
6726 print_it_catch_exec,
6727 print_one_catch_exec,
6728 print_mention_catch_exec,
6729 print_recreate_catch_exec
6730 };
6731
6732 static void
6733 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6734 struct breakpoint_ops *ops)
6735 {
6736 struct gdbarch *gdbarch = get_current_arch ();
6737 struct breakpoint *b =
6738 create_catchpoint_without_mention (gdbarch, tempflag, NULL, ops);
6739
6740 b->syscalls_to_be_caught = filter;
6741
6742 /* Now, we have to mention the breakpoint and update the global
6743 location list. */
6744 mention (b);
6745 update_global_location_list (1);
6746 }
6747
6748 static int
6749 hw_breakpoint_used_count (void)
6750 {
6751 struct breakpoint *b;
6752 int i = 0;
6753
6754 ALL_BREAKPOINTS (b)
6755 {
6756 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
6757 i++;
6758 }
6759
6760 return i;
6761 }
6762
6763 static int
6764 hw_watchpoint_used_count (enum bptype type, int *other_type_used)
6765 {
6766 int i = 0;
6767 struct breakpoint *b;
6768 struct bp_location *bl;
6769
6770 *other_type_used = 0;
6771 ALL_BREAKPOINTS (b)
6772 {
6773 if (!breakpoint_enabled (b))
6774 continue;
6775
6776 if (b->type == type)
6777 for (bl = b->loc; bl; bl = bl->next)
6778 {
6779 /* Special types of hardware watchpoints may use more than
6780 one register. */
6781 if (b->ops && b->ops->resources_needed)
6782 i += b->ops->resources_needed (bl);
6783 else
6784 i++;
6785 }
6786 else if (is_hardware_watchpoint (b))
6787 *other_type_used = 1;
6788 }
6789
6790 return i;
6791 }
6792
6793 void
6794 disable_watchpoints_before_interactive_call_start (void)
6795 {
6796 struct breakpoint *b;
6797
6798 ALL_BREAKPOINTS (b)
6799 {
6800 if (is_watchpoint (b) && breakpoint_enabled (b))
6801 {
6802 b->enable_state = bp_call_disabled;
6803 update_global_location_list (0);
6804 }
6805 }
6806 }
6807
6808 void
6809 enable_watchpoints_after_interactive_call_stop (void)
6810 {
6811 struct breakpoint *b;
6812
6813 ALL_BREAKPOINTS (b)
6814 {
6815 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
6816 {
6817 b->enable_state = bp_enabled;
6818 update_global_location_list (1);
6819 }
6820 }
6821 }
6822
6823 void
6824 disable_breakpoints_before_startup (void)
6825 {
6826 struct breakpoint *b;
6827 int found = 0;
6828
6829 ALL_BREAKPOINTS (b)
6830 {
6831 if (b->pspace != current_program_space)
6832 continue;
6833
6834 if ((b->type == bp_breakpoint
6835 || b->type == bp_hardware_breakpoint)
6836 && breakpoint_enabled (b))
6837 {
6838 b->enable_state = bp_startup_disabled;
6839 found = 1;
6840 }
6841 }
6842
6843 if (found)
6844 update_global_location_list (0);
6845
6846 current_program_space->executing_startup = 1;
6847 }
6848
6849 void
6850 enable_breakpoints_after_startup (void)
6851 {
6852 struct breakpoint *b;
6853 int found = 0;
6854
6855 current_program_space->executing_startup = 0;
6856
6857 ALL_BREAKPOINTS (b)
6858 {
6859 if (b->pspace != current_program_space)
6860 continue;
6861
6862 if ((b->type == bp_breakpoint
6863 || b->type == bp_hardware_breakpoint)
6864 && b->enable_state == bp_startup_disabled)
6865 {
6866 b->enable_state = bp_enabled;
6867 found = 1;
6868 }
6869 }
6870
6871 if (found)
6872 breakpoint_re_set ();
6873 }
6874
6875
6876 /* Set a breakpoint that will evaporate an end of command
6877 at address specified by SAL.
6878 Restrict it to frame FRAME if FRAME is nonzero. */
6879
6880 struct breakpoint *
6881 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
6882 struct frame_id frame_id, enum bptype type)
6883 {
6884 struct breakpoint *b;
6885
6886 /* If FRAME_ID is valid, it should be a real frame, not an inlined
6887 one. */
6888 gdb_assert (!frame_id_inlined_p (frame_id));
6889
6890 b = set_raw_breakpoint (gdbarch, sal, type);
6891 b->enable_state = bp_enabled;
6892 b->disposition = disp_donttouch;
6893 b->frame_id = frame_id;
6894
6895 /* If we're debugging a multi-threaded program, then we want
6896 momentary breakpoints to be active in only a single thread of
6897 control. */
6898 if (in_thread_list (inferior_ptid))
6899 b->thread = pid_to_thread_id (inferior_ptid);
6900
6901 update_global_location_list_nothrow (1);
6902
6903 return b;
6904 }
6905
6906 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
6907 ORIG is NULL. */
6908
6909 struct breakpoint *
6910 clone_momentary_breakpoint (struct breakpoint *orig)
6911 {
6912 struct breakpoint *copy;
6913
6914 /* If there's nothing to clone, then return nothing. */
6915 if (orig == NULL)
6916 return NULL;
6917
6918 copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
6919 copy->loc = allocate_bp_location (copy);
6920 set_breakpoint_location_function (copy->loc);
6921
6922 copy->loc->gdbarch = orig->loc->gdbarch;
6923 copy->loc->requested_address = orig->loc->requested_address;
6924 copy->loc->address = orig->loc->address;
6925 copy->loc->section = orig->loc->section;
6926 copy->loc->pspace = orig->loc->pspace;
6927
6928 if (orig->source_file == NULL)
6929 copy->source_file = NULL;
6930 else
6931 copy->source_file = xstrdup (orig->source_file);
6932
6933 copy->line_number = orig->line_number;
6934 copy->frame_id = orig->frame_id;
6935 copy->thread = orig->thread;
6936 copy->pspace = orig->pspace;
6937
6938 copy->enable_state = bp_enabled;
6939 copy->disposition = disp_donttouch;
6940 copy->number = internal_breakpoint_number--;
6941
6942 update_global_location_list_nothrow (0);
6943 return copy;
6944 }
6945
6946 struct breakpoint *
6947 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
6948 enum bptype type)
6949 {
6950 struct symtab_and_line sal;
6951
6952 sal = find_pc_line (pc, 0);
6953 sal.pc = pc;
6954 sal.section = find_pc_overlay (pc);
6955 sal.explicit_pc = 1;
6956
6957 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
6958 }
6959 \f
6960
6961 /* Tell the user we have just set a breakpoint B. */
6962
6963 static void
6964 mention (struct breakpoint *b)
6965 {
6966 int say_where = 0;
6967 struct cleanup *ui_out_chain;
6968 struct value_print_options opts;
6969
6970 get_user_print_options (&opts);
6971
6972 /* FIXME: This is misplaced; mention() is called by things (like
6973 hitting a watchpoint) other than breakpoint creation. It should
6974 be possible to clean this up and at the same time replace the
6975 random calls to breakpoint_changed with this hook. */
6976 observer_notify_breakpoint_created (b->number);
6977
6978 if (b->ops != NULL && b->ops->print_mention != NULL)
6979 b->ops->print_mention (b);
6980 else
6981 switch (b->type)
6982 {
6983 case bp_none:
6984 printf_filtered (_("(apparently deleted?) Eventpoint %d: "),
6985 b->number);
6986 break;
6987 case bp_watchpoint:
6988 ui_out_text (uiout, "Watchpoint ");
6989 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
6990 ui_out_field_int (uiout, "number", b->number);
6991 ui_out_text (uiout, ": ");
6992 ui_out_field_string (uiout, "exp", b->exp_string);
6993 do_cleanups (ui_out_chain);
6994 break;
6995 case bp_hardware_watchpoint:
6996 ui_out_text (uiout, "Hardware watchpoint ");
6997 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
6998 ui_out_field_int (uiout, "number", b->number);
6999 ui_out_text (uiout, ": ");
7000 ui_out_field_string (uiout, "exp", b->exp_string);
7001 do_cleanups (ui_out_chain);
7002 break;
7003 case bp_read_watchpoint:
7004 ui_out_text (uiout, "Hardware read watchpoint ");
7005 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
7006 ui_out_field_int (uiout, "number", b->number);
7007 ui_out_text (uiout, ": ");
7008 ui_out_field_string (uiout, "exp", b->exp_string);
7009 do_cleanups (ui_out_chain);
7010 break;
7011 case bp_access_watchpoint:
7012 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
7013 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
7014 ui_out_field_int (uiout, "number", b->number);
7015 ui_out_text (uiout, ": ");
7016 ui_out_field_string (uiout, "exp", b->exp_string);
7017 do_cleanups (ui_out_chain);
7018 break;
7019 case bp_breakpoint:
7020 if (ui_out_is_mi_like_p (uiout))
7021 {
7022 say_where = 0;
7023 break;
7024 }
7025 if (b->disposition == disp_del)
7026 printf_filtered (_("Temporary breakpoint"));
7027 else
7028 printf_filtered (_("Breakpoint"));
7029 printf_filtered (_(" %d"), b->number);
7030 say_where = 1;
7031 break;
7032 case bp_hardware_breakpoint:
7033 if (ui_out_is_mi_like_p (uiout))
7034 {
7035 say_where = 0;
7036 break;
7037 }
7038 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
7039 say_where = 1;
7040 break;
7041 case bp_tracepoint:
7042 if (ui_out_is_mi_like_p (uiout))
7043 {
7044 say_where = 0;
7045 break;
7046 }
7047 printf_filtered (_("Tracepoint"));
7048 printf_filtered (_(" %d"), b->number);
7049 say_where = 1;
7050 break;
7051 case bp_fast_tracepoint:
7052 if (ui_out_is_mi_like_p (uiout))
7053 {
7054 say_where = 0;
7055 break;
7056 }
7057 printf_filtered (_("Fast tracepoint"));
7058 printf_filtered (_(" %d"), b->number);
7059 say_where = 1;
7060 break;
7061 case bp_static_tracepoint:
7062 if (ui_out_is_mi_like_p (uiout))
7063 {
7064 say_where = 0;
7065 break;
7066 }
7067 printf_filtered (_("Static tracepoint"));
7068 printf_filtered (_(" %d"), b->number);
7069 say_where = 1;
7070 break;
7071
7072 case bp_until:
7073 case bp_finish:
7074 case bp_longjmp:
7075 case bp_longjmp_resume:
7076 case bp_exception:
7077 case bp_exception_resume:
7078 case bp_step_resume:
7079 case bp_call_dummy:
7080 case bp_std_terminate:
7081 case bp_watchpoint_scope:
7082 case bp_shlib_event:
7083 case bp_thread_event:
7084 case bp_overlay_event:
7085 case bp_jit_event:
7086 case bp_longjmp_master:
7087 case bp_std_terminate_master:
7088 case bp_exception_master:
7089 break;
7090 }
7091
7092 if (say_where)
7093 {
7094 /* i18n: cagney/2005-02-11: Below needs to be merged into a
7095 single string. */
7096 if (b->loc == NULL)
7097 {
7098 printf_filtered (_(" (%s) pending."), b->addr_string);
7099 }
7100 else
7101 {
7102 if (opts.addressprint || b->source_file == NULL)
7103 {
7104 printf_filtered (" at ");
7105 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
7106 gdb_stdout);
7107 }
7108 if (b->source_file)
7109 printf_filtered (": file %s, line %d.",
7110 b->source_file, b->line_number);
7111
7112 if (b->loc->next)
7113 {
7114 struct bp_location *loc = b->loc;
7115 int n = 0;
7116 for (; loc; loc = loc->next)
7117 ++n;
7118 printf_filtered (" (%d locations)", n);
7119 }
7120
7121 }
7122 }
7123 if (ui_out_is_mi_like_p (uiout))
7124 return;
7125 printf_filtered ("\n");
7126 }
7127 \f
7128
7129 static struct bp_location *
7130 add_location_to_breakpoint (struct breakpoint *b,
7131 const struct symtab_and_line *sal)
7132 {
7133 struct bp_location *loc, **tmp;
7134
7135 loc = allocate_bp_location (b);
7136 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7137 ;
7138 *tmp = loc;
7139 loc->gdbarch = get_sal_arch (*sal);
7140 if (!loc->gdbarch)
7141 loc->gdbarch = b->gdbarch;
7142 loc->requested_address = sal->pc;
7143 loc->address = adjust_breakpoint_address (loc->gdbarch,
7144 loc->requested_address, b->type);
7145 loc->pspace = sal->pspace;
7146 gdb_assert (loc->pspace != NULL);
7147 loc->section = sal->section;
7148
7149 set_breakpoint_location_function (loc);
7150 return loc;
7151 }
7152 \f
7153
7154 /* Return 1 if LOC is pointing to a permanent breakpoint,
7155 return 0 otherwise. */
7156
7157 static int
7158 bp_loc_is_permanent (struct bp_location *loc)
7159 {
7160 int len;
7161 CORE_ADDR addr;
7162 const gdb_byte *brk;
7163 gdb_byte *target_mem;
7164 struct cleanup *cleanup;
7165 int retval = 0;
7166
7167 gdb_assert (loc != NULL);
7168
7169 addr = loc->address;
7170 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
7171
7172 /* Software breakpoints unsupported? */
7173 if (brk == NULL)
7174 return 0;
7175
7176 target_mem = alloca (len);
7177
7178 /* Enable the automatic memory restoration from breakpoints while
7179 we read the memory. Otherwise we could say about our temporary
7180 breakpoints they are permanent. */
7181 cleanup = save_current_space_and_thread ();
7182
7183 switch_to_program_space_and_thread (loc->pspace);
7184 make_show_memory_breakpoints_cleanup (0);
7185
7186 if (target_read_memory (loc->address, target_mem, len) == 0
7187 && memcmp (target_mem, brk, len) == 0)
7188 retval = 1;
7189
7190 do_cleanups (cleanup);
7191
7192 return retval;
7193 }
7194
7195
7196
7197 /* Create a breakpoint with SAL as location. Use ADDR_STRING
7198 as textual description of the location, and COND_STRING
7199 as condition expression. */
7200
7201 static void
7202 create_breakpoint_sal (struct gdbarch *gdbarch,
7203 struct symtabs_and_lines sals, char *addr_string,
7204 char *cond_string,
7205 enum bptype type, enum bpdisp disposition,
7206 int thread, int task, int ignore_count,
7207 struct breakpoint_ops *ops, int from_tty,
7208 int enabled, int internal)
7209 {
7210 struct breakpoint *b = NULL;
7211 int i;
7212
7213 if (type == bp_hardware_breakpoint)
7214 {
7215 int i = hw_breakpoint_used_count ();
7216 int target_resources_ok =
7217 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
7218 i + 1, 0);
7219 if (target_resources_ok == 0)
7220 error (_("No hardware breakpoint support in the target."));
7221 else if (target_resources_ok < 0)
7222 error (_("Hardware breakpoints used exceeds limit."));
7223 }
7224
7225 gdb_assert (sals.nelts > 0);
7226
7227 for (i = 0; i < sals.nelts; ++i)
7228 {
7229 struct symtab_and_line sal = sals.sals[i];
7230 struct bp_location *loc;
7231
7232 if (from_tty)
7233 {
7234 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7235 if (!loc_gdbarch)
7236 loc_gdbarch = gdbarch;
7237
7238 describe_other_breakpoints (loc_gdbarch,
7239 sal.pspace, sal.pc, sal.section, thread);
7240 }
7241
7242 if (i == 0)
7243 {
7244 b = set_raw_breakpoint (gdbarch, sal, type);
7245 set_breakpoint_number (internal, b);
7246 b->thread = thread;
7247 b->task = task;
7248
7249 b->cond_string = cond_string;
7250 b->ignore_count = ignore_count;
7251 b->enable_state = enabled ? bp_enabled : bp_disabled;
7252 b->disposition = disposition;
7253 b->pspace = sals.sals[0].pspace;
7254
7255 if (type == bp_static_tracepoint)
7256 {
7257 struct static_tracepoint_marker marker;
7258
7259 if (is_marker_spec (addr_string))
7260 {
7261 /* We already know the marker exists, otherwise, we
7262 wouldn't see a sal for it. */
7263 char *p = &addr_string[3];
7264 char *endp;
7265 char *marker_str;
7266 int i;
7267
7268 p = skip_spaces (p);
7269
7270 endp = skip_to_space (p);
7271
7272 marker_str = savestring (p, endp - p);
7273 b->static_trace_marker_id = marker_str;
7274
7275 printf_filtered (_("Probed static tracepoint "
7276 "marker \"%s\"\n"),
7277 b->static_trace_marker_id);
7278 }
7279 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7280 {
7281 b->static_trace_marker_id = xstrdup (marker.str_id);
7282 release_static_tracepoint_marker (&marker);
7283
7284 printf_filtered (_("Probed static tracepoint "
7285 "marker \"%s\"\n"),
7286 b->static_trace_marker_id);
7287 }
7288 else
7289 warning (_("Couldn't determine the static "
7290 "tracepoint marker to probe"));
7291 }
7292
7293 if (enabled && b->pspace->executing_startup
7294 && (b->type == bp_breakpoint
7295 || b->type == bp_hardware_breakpoint))
7296 b->enable_state = bp_startup_disabled;
7297
7298 loc = b->loc;
7299 }
7300 else
7301 {
7302 loc = add_location_to_breakpoint (b, &sal);
7303 }
7304
7305 if (bp_loc_is_permanent (loc))
7306 make_breakpoint_permanent (b);
7307
7308 if (b->cond_string)
7309 {
7310 char *arg = b->cond_string;
7311 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
7312 if (*arg)
7313 error (_("Garbage %s follows condition"), arg);
7314 }
7315 }
7316
7317 if (addr_string)
7318 b->addr_string = addr_string;
7319 else
7320 /* addr_string has to be used or breakpoint_re_set will delete
7321 me. */
7322 b->addr_string
7323 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7324
7325 b->ops = ops;
7326 if (internal)
7327 /* Do not mention breakpoints with a negative number, but do
7328 notify observers. */
7329 observer_notify_breakpoint_created (b->number);
7330 else
7331 mention (b);
7332 }
7333
7334 /* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7335 elements to fill the void space. */
7336 static void
7337 remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
7338 {
7339 int i = index_to_remove+1;
7340 int last_index = sal->nelts-1;
7341
7342 for (;i <= last_index; ++i)
7343 sal->sals[i-1] = sal->sals[i];
7344
7345 --(sal->nelts);
7346 }
7347
7348 /* If appropriate, obtains all sals that correspond to the same file
7349 and line as SAL, in all program spaces. Users debugging with IDEs,
7350 will want to set a breakpoint at foo.c:line, and not really care
7351 about program spaces. This is done only if SAL does not have
7352 explicit PC and has line and file information. If we got just a
7353 single expanded sal, return the original.
7354
7355 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7356 which the name of enclosing function is different from SAL. This
7357 makes sure that if we have breakpoint originally set in template
7358 instantiation, say foo<int>(), we won't expand SAL to locations at
7359 the same line in all existing instantiations of 'foo'. */
7360
7361 static struct symtabs_and_lines
7362 expand_line_sal_maybe (struct symtab_and_line sal)
7363 {
7364 struct symtabs_and_lines expanded;
7365 CORE_ADDR original_pc = sal.pc;
7366 char *original_function = NULL;
7367 int found;
7368 int i;
7369 struct cleanup *old_chain;
7370
7371 /* If we have explicit pc, don't expand.
7372 If we have no line number, we can't expand. */
7373 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7374 {
7375 expanded.nelts = 1;
7376 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7377 expanded.sals[0] = sal;
7378 return expanded;
7379 }
7380
7381 sal.pc = 0;
7382
7383 old_chain = save_current_space_and_thread ();
7384
7385 switch_to_program_space_and_thread (sal.pspace);
7386
7387 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
7388
7389 /* Note that expand_line_sal visits *all* program spaces. */
7390 expanded = expand_line_sal (sal);
7391
7392 if (expanded.nelts == 1)
7393 {
7394 /* We had one sal, we got one sal. Return that sal, adjusting it
7395 past the function prologue if necessary. */
7396 xfree (expanded.sals);
7397 expanded.nelts = 1;
7398 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7399 sal.pc = original_pc;
7400 expanded.sals[0] = sal;
7401 skip_prologue_sal (&expanded.sals[0]);
7402 do_cleanups (old_chain);
7403 return expanded;
7404 }
7405
7406 if (!sal.explicit_line)
7407 {
7408 CORE_ADDR func_addr, func_end;
7409 for (i = 0; i < expanded.nelts; ++i)
7410 {
7411 CORE_ADDR pc = expanded.sals[i].pc;
7412 char *this_function;
7413
7414 /* We need to switch threads as well since we're about to
7415 read memory. */
7416 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7417
7418 if (find_pc_partial_function (pc, &this_function,
7419 &func_addr, &func_end))
7420 {
7421 if (this_function
7422 && strcmp (this_function, original_function) != 0)
7423 {
7424 remove_sal (&expanded, i);
7425 --i;
7426 }
7427 }
7428 }
7429 }
7430
7431 /* Skip the function prologue if necessary. */
7432 for (i = 0; i < expanded.nelts; ++i)
7433 skip_prologue_sal (&expanded.sals[i]);
7434
7435 do_cleanups (old_chain);
7436
7437 if (expanded.nelts <= 1)
7438 {
7439 /* This is un ugly workaround. If we get zero expanded sals
7440 then something is really wrong. Fix that by returning the
7441 original sal. */
7442
7443 xfree (expanded.sals);
7444 expanded.nelts = 1;
7445 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7446 sal.pc = original_pc;
7447 expanded.sals[0] = sal;
7448 return expanded;
7449 }
7450
7451 if (original_pc)
7452 {
7453 found = 0;
7454 for (i = 0; i < expanded.nelts; ++i)
7455 if (expanded.sals[i].pc == original_pc)
7456 {
7457 found = 1;
7458 break;
7459 }
7460 gdb_assert (found);
7461 }
7462
7463 return expanded;
7464 }
7465
7466 /* Add SALS.nelts breakpoints to the breakpoint table. For each
7467 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7468 value. COND_STRING, if not NULL, specified the condition to be
7469 used for all breakpoints. Essentially the only case where
7470 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7471 function. In that case, it's still not possible to specify
7472 separate conditions for different overloaded functions, so
7473 we take just a single condition string.
7474
7475 NOTE: If the function succeeds, the caller is expected to cleanup
7476 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
7477 array contents). If the function fails (error() is called), the
7478 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
7479 COND and SALS arrays and each of those arrays contents. */
7480
7481 static void
7482 create_breakpoints_sal (struct gdbarch *gdbarch,
7483 struct symtabs_and_lines sals, char **addr_string,
7484 char *cond_string,
7485 enum bptype type, enum bpdisp disposition,
7486 int thread, int task, int ignore_count,
7487 struct breakpoint_ops *ops, int from_tty,
7488 int enabled, int internal)
7489 {
7490 int i;
7491
7492 for (i = 0; i < sals.nelts; ++i)
7493 {
7494 struct symtabs_and_lines expanded =
7495 expand_line_sal_maybe (sals.sals[i]);
7496
7497 create_breakpoint_sal (gdbarch, expanded, addr_string[i],
7498 cond_string, type, disposition,
7499 thread, task, ignore_count, ops,
7500 from_tty, enabled, internal);
7501 }
7502 }
7503
7504 /* Parse ADDRESS which is assumed to be a SAL specification possibly
7505 followed by conditionals. On return, SALS contains an array of SAL
7506 addresses found. ADDR_STRING contains a vector of (canonical)
7507 address strings. ADDRESS points to the end of the SAL.
7508
7509 The array and the line spec strings are allocated on the heap, it is
7510 the caller's responsibility to free them. */
7511
7512 static void
7513 parse_breakpoint_sals (char **address,
7514 struct symtabs_and_lines *sals,
7515 char ***addr_string,
7516 int *not_found_ptr)
7517 {
7518 char *addr_start = *address;
7519
7520 *addr_string = NULL;
7521 /* If no arg given, or if first arg is 'if ', use the default
7522 breakpoint. */
7523 if ((*address) == NULL
7524 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
7525 {
7526 if (default_breakpoint_valid)
7527 {
7528 struct symtab_and_line sal;
7529
7530 init_sal (&sal); /* Initialize to zeroes. */
7531 sals->sals = (struct symtab_and_line *)
7532 xmalloc (sizeof (struct symtab_and_line));
7533 sal.pc = default_breakpoint_address;
7534 sal.line = default_breakpoint_line;
7535 sal.symtab = default_breakpoint_symtab;
7536 sal.pspace = default_breakpoint_pspace;
7537 sal.section = find_pc_overlay (sal.pc);
7538
7539 /* "break" without arguments is equivalent to "break *PC"
7540 where PC is the default_breakpoint_address. So make sure
7541 to set sal.explicit_pc to prevent GDB from trying to
7542 expand the list of sals to include all other instances
7543 with the same symtab and line. */
7544 sal.explicit_pc = 1;
7545
7546 sals->sals[0] = sal;
7547 sals->nelts = 1;
7548 }
7549 else
7550 error (_("No default breakpoint address now."));
7551 }
7552 else
7553 {
7554 /* Force almost all breakpoints to be in terms of the
7555 current_source_symtab (which is decode_line_1's default).
7556 This should produce the results we want almost all of the
7557 time while leaving default_breakpoint_* alone.
7558
7559 ObjC: However, don't match an Objective-C method name which
7560 may have a '+' or '-' succeeded by a '[' */
7561
7562 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
7563
7564 if (default_breakpoint_valid
7565 && (!cursal.symtab
7566 || ((strchr ("+-", (*address)[0]) != NULL)
7567 && ((*address)[1] != '['))))
7568 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
7569 default_breakpoint_line, addr_string,
7570 not_found_ptr);
7571 else
7572 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
7573 addr_string, not_found_ptr);
7574 }
7575 /* For any SAL that didn't have a canonical string, fill one in. */
7576 if (sals->nelts > 0 && *addr_string == NULL)
7577 *addr_string = xcalloc (sals->nelts, sizeof (char **));
7578 if (addr_start != (*address))
7579 {
7580 int i;
7581
7582 for (i = 0; i < sals->nelts; i++)
7583 {
7584 /* Add the string if not present. */
7585 if ((*addr_string)[i] == NULL)
7586 (*addr_string)[i] = savestring (addr_start,
7587 (*address) - addr_start);
7588 }
7589 }
7590 }
7591
7592
7593 /* Convert each SAL into a real PC. Verify that the PC can be
7594 inserted as a breakpoint. If it can't throw an error. */
7595
7596 static void
7597 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
7598 {
7599 int i;
7600
7601 for (i = 0; i < sals->nelts; i++)
7602 resolve_sal_pc (&sals->sals[i]);
7603 }
7604
7605 /* Fast tracepoints may have restrictions on valid locations. For
7606 instance, a fast tracepoint using a jump instead of a trap will
7607 likely have to overwrite more bytes than a trap would, and so can
7608 only be placed where the instruction is longer than the jump, or a
7609 multi-instruction sequence does not have a jump into the middle of
7610 it, etc. */
7611
7612 static void
7613 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7614 struct symtabs_and_lines *sals)
7615 {
7616 int i, rslt;
7617 struct symtab_and_line *sal;
7618 char *msg;
7619 struct cleanup *old_chain;
7620
7621 for (i = 0; i < sals->nelts; i++)
7622 {
7623 sal = &sals->sals[i];
7624
7625 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7626 NULL, &msg);
7627 old_chain = make_cleanup (xfree, msg);
7628
7629 if (!rslt)
7630 error (_("May not have a fast tracepoint at 0x%s%s"),
7631 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7632
7633 do_cleanups (old_chain);
7634 }
7635 }
7636
7637 static void
7638 do_captured_parse_breakpoint (struct ui_out *ui, void *data)
7639 {
7640 struct captured_parse_breakpoint_args *args = data;
7641
7642 parse_breakpoint_sals (args->arg_p, args->sals_p, args->addr_string_p,
7643 args->not_found_ptr);
7644 }
7645
7646 /* Given TOK, a string specification of condition and thread, as
7647 accepted by the 'break' command, extract the condition
7648 string and thread number and set *COND_STRING and *THREAD.
7649 PC identifies the context at which the condition should be parsed.
7650 If no condition is found, *COND_STRING is set to NULL.
7651 If no thread is found, *THREAD is set to -1. */
7652 static void
7653 find_condition_and_thread (char *tok, CORE_ADDR pc,
7654 char **cond_string, int *thread, int *task)
7655 {
7656 *cond_string = NULL;
7657 *thread = -1;
7658 while (tok && *tok)
7659 {
7660 char *end_tok;
7661 int toklen;
7662 char *cond_start = NULL;
7663 char *cond_end = NULL;
7664
7665 tok = skip_spaces (tok);
7666
7667 end_tok = skip_to_space (tok);
7668
7669 toklen = end_tok - tok;
7670
7671 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7672 {
7673 struct expression *expr;
7674
7675 tok = cond_start = end_tok + 1;
7676 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7677 xfree (expr);
7678 cond_end = tok;
7679 *cond_string = savestring (cond_start,
7680 cond_end - cond_start);
7681 }
7682 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7683 {
7684 char *tmptok;
7685
7686 tok = end_tok + 1;
7687 tmptok = tok;
7688 *thread = strtol (tok, &tok, 0);
7689 if (tok == tmptok)
7690 error (_("Junk after thread keyword."));
7691 if (!valid_thread_id (*thread))
7692 error (_("Unknown thread %d."), *thread);
7693 }
7694 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7695 {
7696 char *tmptok;
7697
7698 tok = end_tok + 1;
7699 tmptok = tok;
7700 *task = strtol (tok, &tok, 0);
7701 if (tok == tmptok)
7702 error (_("Junk after task keyword."));
7703 if (!valid_task_id (*task))
7704 error (_("Unknown task %d."), *task);
7705 }
7706 else
7707 error (_("Junk at end of arguments."));
7708 }
7709 }
7710
7711 /* Decode a static tracepoint marker spec. */
7712
7713 static struct symtabs_and_lines
7714 decode_static_tracepoint_spec (char **arg_p)
7715 {
7716 VEC(static_tracepoint_marker_p) *markers = NULL;
7717 struct symtabs_and_lines sals;
7718 struct symtab_and_line sal;
7719 struct symbol *sym;
7720 struct cleanup *old_chain;
7721 char *p = &(*arg_p)[3];
7722 char *endp;
7723 char *marker_str;
7724 int i;
7725
7726 p = skip_spaces (p);
7727
7728 endp = skip_to_space (p);
7729
7730 marker_str = savestring (p, endp - p);
7731 old_chain = make_cleanup (xfree, marker_str);
7732
7733 markers = target_static_tracepoint_markers_by_strid (marker_str);
7734 if (VEC_empty(static_tracepoint_marker_p, markers))
7735 error (_("No known static tracepoint marker named %s"), marker_str);
7736
7737 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7738 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7739
7740 for (i = 0; i < sals.nelts; i++)
7741 {
7742 struct static_tracepoint_marker *marker;
7743
7744 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7745
7746 init_sal (&sals.sals[i]);
7747
7748 sals.sals[i] = find_pc_line (marker->address, 0);
7749 sals.sals[i].pc = marker->address;
7750
7751 release_static_tracepoint_marker (marker);
7752 }
7753
7754 do_cleanups (old_chain);
7755
7756 *arg_p = endp;
7757 return sals;
7758 }
7759
7760 /* Set a breakpoint. This function is shared between CLI and MI
7761 functions for setting a breakpoint. This function has two major
7762 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7763 parameter. If non-zero, the function will parse arg, extracting
7764 breakpoint location, address and thread. Otherwise, ARG is just
7765 the location of breakpoint, with condition and thread specified by
7766 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7767 the breakpoint number will be allocated from the internal
7768 breakpoint count. Returns true if any breakpoint was created;
7769 false otherwise. */
7770
7771 int
7772 create_breakpoint (struct gdbarch *gdbarch,
7773 char *arg, char *cond_string, int thread,
7774 int parse_condition_and_thread,
7775 int tempflag, enum bptype type_wanted,
7776 int ignore_count,
7777 enum auto_boolean pending_break_support,
7778 struct breakpoint_ops *ops,
7779 int from_tty, int enabled, int internal)
7780 {
7781 struct gdb_exception e;
7782 struct symtabs_and_lines sals;
7783 struct symtab_and_line pending_sal;
7784 char *copy_arg;
7785 char *addr_start = arg;
7786 char **addr_string;
7787 struct cleanup *old_chain;
7788 struct cleanup *bkpt_chain = NULL;
7789 struct captured_parse_breakpoint_args parse_args;
7790 int i;
7791 int pending = 0;
7792 int not_found = 0;
7793 int task = 0;
7794 int prev_bkpt_count = breakpoint_count;
7795
7796 sals.sals = NULL;
7797 sals.nelts = 0;
7798 addr_string = NULL;
7799
7800 parse_args.arg_p = &arg;
7801 parse_args.sals_p = &sals;
7802 parse_args.addr_string_p = &addr_string;
7803 parse_args.not_found_ptr = &not_found;
7804
7805 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7806 {
7807 int i;
7808
7809 sals = decode_static_tracepoint_spec (&arg);
7810
7811 copy_arg = savestring (addr_start, arg - addr_start);
7812 addr_string = xcalloc (sals.nelts, sizeof (char **));
7813 for (i = 0; i < sals.nelts; i++)
7814 addr_string[i] = xstrdup (copy_arg);
7815 goto done;
7816 }
7817
7818 e = catch_exception (uiout, do_captured_parse_breakpoint,
7819 &parse_args, RETURN_MASK_ALL);
7820
7821 /* If caller is interested in rc value from parse, set value. */
7822 switch (e.reason)
7823 {
7824 case RETURN_QUIT:
7825 throw_exception (e);
7826 case RETURN_ERROR:
7827 switch (e.error)
7828 {
7829 case NOT_FOUND_ERROR:
7830
7831 /* If pending breakpoint support is turned off, throw
7832 error. */
7833
7834 if (pending_break_support == AUTO_BOOLEAN_FALSE)
7835 throw_exception (e);
7836
7837 exception_print (gdb_stderr, e);
7838
7839 /* If pending breakpoint support is auto query and the user
7840 selects no, then simply return the error code. */
7841 if (pending_break_support == AUTO_BOOLEAN_AUTO
7842 && !nquery (_("Make breakpoint pending on "
7843 "future shared library load? ")))
7844 return 0;
7845
7846 /* At this point, either the user was queried about setting
7847 a pending breakpoint and selected yes, or pending
7848 breakpoint behavior is on and thus a pending breakpoint
7849 is defaulted on behalf of the user. */
7850 copy_arg = xstrdup (addr_start);
7851 addr_string = &copy_arg;
7852 sals.nelts = 1;
7853 sals.sals = &pending_sal;
7854 pending_sal.pc = 0;
7855 pending = 1;
7856 break;
7857 default:
7858 throw_exception (e);
7859 }
7860 break;
7861 default:
7862 if (!sals.nelts)
7863 return 0;
7864 }
7865
7866 done:
7867
7868 /* Create a chain of things that always need to be cleaned up. */
7869 old_chain = make_cleanup (null_cleanup, 0);
7870
7871 if (!pending)
7872 {
7873 /* Make sure that all storage allocated to SALS gets freed. */
7874 make_cleanup (xfree, sals.sals);
7875
7876 /* Cleanup the addr_string array but not its contents. */
7877 make_cleanup (xfree, addr_string);
7878 }
7879
7880 /* ----------------------------- SNIP -----------------------------
7881 Anything added to the cleanup chain beyond this point is assumed
7882 to be part of a breakpoint. If the breakpoint create succeeds
7883 then the memory is not reclaimed. */
7884 bkpt_chain = make_cleanup (null_cleanup, 0);
7885
7886 /* Mark the contents of the addr_string for cleanup. These go on
7887 the bkpt_chain and only occur if the breakpoint create fails. */
7888 for (i = 0; i < sals.nelts; i++)
7889 {
7890 if (addr_string[i] != NULL)
7891 make_cleanup (xfree, addr_string[i]);
7892 }
7893
7894 /* Resolve all line numbers to PC's and verify that the addresses
7895 are ok for the target. */
7896 if (!pending)
7897 breakpoint_sals_to_pc (&sals);
7898
7899 /* Fast tracepoints may have additional restrictions on location. */
7900 if (type_wanted == bp_fast_tracepoint)
7901 check_fast_tracepoint_sals (gdbarch, &sals);
7902
7903 /* Verify that condition can be parsed, before setting any
7904 breakpoints. Allocate a separate condition expression for each
7905 breakpoint. */
7906 if (!pending)
7907 {
7908 if (parse_condition_and_thread)
7909 {
7910 /* Here we only parse 'arg' to separate condition
7911 from thread number, so parsing in context of first
7912 sal is OK. When setting the breakpoint we'll
7913 re-parse it in context of each sal. */
7914 cond_string = NULL;
7915 thread = -1;
7916 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
7917 &thread, &task);
7918 if (cond_string)
7919 make_cleanup (xfree, cond_string);
7920 }
7921 else
7922 {
7923 /* Create a private copy of condition string. */
7924 if (cond_string)
7925 {
7926 cond_string = xstrdup (cond_string);
7927 make_cleanup (xfree, cond_string);
7928 }
7929 }
7930
7931 /* If the user is creating a static tracepoint by marker id
7932 (strace -m MARKER_ID), then store the sals index, so that
7933 breakpoint_re_set can try to match up which of the newly
7934 found markers corresponds to this one, and, don't try to
7935 expand multiple locations for each sal, given than SALS
7936 already should contain all sals for MARKER_ID. */
7937 if (type_wanted == bp_static_tracepoint
7938 && is_marker_spec (addr_string[0]))
7939 {
7940 int i;
7941
7942 for (i = 0; i < sals.nelts; ++i)
7943 {
7944 struct symtabs_and_lines expanded;
7945 struct breakpoint *tp;
7946 struct cleanup *old_chain;
7947
7948 expanded.nelts = 1;
7949 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7950 expanded.sals[0] = sals.sals[i];
7951 old_chain = make_cleanup (xfree, expanded.sals);
7952
7953 create_breakpoint_sal (gdbarch, expanded, addr_string[i],
7954 cond_string, type_wanted,
7955 tempflag ? disp_del : disp_donttouch,
7956 thread, task, ignore_count, ops,
7957 from_tty, enabled, internal);
7958
7959 do_cleanups (old_chain);
7960
7961 /* Get the tracepoint we just created. */
7962 if (internal)
7963 tp = get_breakpoint (internal_breakpoint_number);
7964 else
7965 tp = get_breakpoint (breakpoint_count);
7966 gdb_assert (tp != NULL);
7967
7968 /* Given that its possible to have multiple markers with
7969 the same string id, if the user is creating a static
7970 tracepoint by marker id ("strace -m MARKER_ID"), then
7971 store the sals index, so that breakpoint_re_set can
7972 try to match up which of the newly found markers
7973 corresponds to this one */
7974 tp->static_trace_marker_id_idx = i;
7975 }
7976 }
7977 else
7978 create_breakpoints_sal (gdbarch, sals, addr_string, cond_string,
7979 type_wanted,
7980 tempflag ? disp_del : disp_donttouch,
7981 thread, task, ignore_count, ops, from_tty,
7982 enabled, internal);
7983 }
7984 else
7985 {
7986 struct breakpoint *b;
7987
7988 make_cleanup (xfree, copy_arg);
7989
7990 b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
7991 set_breakpoint_number (internal, b);
7992 b->thread = -1;
7993 b->addr_string = addr_string[0];
7994 b->cond_string = NULL;
7995 b->ignore_count = ignore_count;
7996 b->disposition = tempflag ? disp_del : disp_donttouch;
7997 b->condition_not_parsed = 1;
7998 b->ops = ops;
7999 b->enable_state = enabled ? bp_enabled : bp_disabled;
8000 b->pspace = current_program_space;
8001 b->py_bp_object = NULL;
8002
8003 if (enabled && b->pspace->executing_startup
8004 && (b->type == bp_breakpoint
8005 || b->type == bp_hardware_breakpoint))
8006 b->enable_state = bp_startup_disabled;
8007
8008 if (internal)
8009 /* Do not mention breakpoints with a negative number,
8010 but do notify observers. */
8011 observer_notify_breakpoint_created (b->number);
8012 else
8013 mention (b);
8014 }
8015
8016 if (sals.nelts > 1)
8017 {
8018 warning (_("Multiple breakpoints were set.\nUse the "
8019 "\"delete\" command to delete unwanted breakpoints."));
8020 prev_breakpoint_count = prev_bkpt_count;
8021 }
8022
8023 /* That's it. Discard the cleanups for data inserted into the
8024 breakpoint. */
8025 discard_cleanups (bkpt_chain);
8026 /* But cleanup everything else. */
8027 do_cleanups (old_chain);
8028
8029 /* error call may happen here - have BKPT_CHAIN already discarded. */
8030 update_global_location_list (1);
8031
8032 return 1;
8033 }
8034
8035 /* Set a breakpoint.
8036 ARG is a string describing breakpoint address,
8037 condition, and thread.
8038 FLAG specifies if a breakpoint is hardware on,
8039 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8040 and BP_TEMPFLAG. */
8041
8042 static void
8043 break_command_1 (char *arg, int flag, int from_tty)
8044 {
8045 int tempflag = flag & BP_TEMPFLAG;
8046 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8047 ? bp_hardware_breakpoint
8048 : bp_breakpoint);
8049
8050 create_breakpoint (get_current_arch (),
8051 arg,
8052 NULL, 0, 1 /* parse arg */,
8053 tempflag, type_wanted,
8054 0 /* Ignore count */,
8055 pending_break_support,
8056 NULL /* breakpoint_ops */,
8057 from_tty,
8058 1 /* enabled */,
8059 0 /* internal */);
8060 }
8061
8062
8063 /* Helper function for break_command_1 and disassemble_command. */
8064
8065 void
8066 resolve_sal_pc (struct symtab_and_line *sal)
8067 {
8068 CORE_ADDR pc;
8069
8070 if (sal->pc == 0 && sal->symtab != NULL)
8071 {
8072 if (!find_line_pc (sal->symtab, sal->line, &pc))
8073 error (_("No line %d in file \"%s\"."),
8074 sal->line, sal->symtab->filename);
8075 sal->pc = pc;
8076
8077 /* If this SAL corresponds to a breakpoint inserted using a line
8078 number, then skip the function prologue if necessary. */
8079 if (sal->explicit_line)
8080 skip_prologue_sal (sal);
8081 }
8082
8083 if (sal->section == 0 && sal->symtab != NULL)
8084 {
8085 struct blockvector *bv;
8086 struct block *b;
8087 struct symbol *sym;
8088
8089 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
8090 if (bv != NULL)
8091 {
8092 sym = block_linkage_function (b);
8093 if (sym != NULL)
8094 {
8095 fixup_symbol_section (sym, sal->symtab->objfile);
8096 sal->section = SYMBOL_OBJ_SECTION (sym);
8097 }
8098 else
8099 {
8100 /* It really is worthwhile to have the section, so we'll
8101 just have to look harder. This case can be executed
8102 if we have line numbers but no functions (as can
8103 happen in assembly source). */
8104
8105 struct minimal_symbol *msym;
8106 struct cleanup *old_chain = save_current_space_and_thread ();
8107
8108 switch_to_program_space_and_thread (sal->pspace);
8109
8110 msym = lookup_minimal_symbol_by_pc (sal->pc);
8111 if (msym)
8112 sal->section = SYMBOL_OBJ_SECTION (msym);
8113
8114 do_cleanups (old_chain);
8115 }
8116 }
8117 }
8118 }
8119
8120 void
8121 break_command (char *arg, int from_tty)
8122 {
8123 break_command_1 (arg, 0, from_tty);
8124 }
8125
8126 void
8127 tbreak_command (char *arg, int from_tty)
8128 {
8129 break_command_1 (arg, BP_TEMPFLAG, from_tty);
8130 }
8131
8132 static void
8133 hbreak_command (char *arg, int from_tty)
8134 {
8135 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
8136 }
8137
8138 static void
8139 thbreak_command (char *arg, int from_tty)
8140 {
8141 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
8142 }
8143
8144 static void
8145 stop_command (char *arg, int from_tty)
8146 {
8147 printf_filtered (_("Specify the type of breakpoint to set.\n\
8148 Usage: stop in <function | address>\n\
8149 stop at <line>\n"));
8150 }
8151
8152 static void
8153 stopin_command (char *arg, int from_tty)
8154 {
8155 int badInput = 0;
8156
8157 if (arg == (char *) NULL)
8158 badInput = 1;
8159 else if (*arg != '*')
8160 {
8161 char *argptr = arg;
8162 int hasColon = 0;
8163
8164 /* Look for a ':'. If this is a line number specification, then
8165 say it is bad, otherwise, it should be an address or
8166 function/method name. */
8167 while (*argptr && !hasColon)
8168 {
8169 hasColon = (*argptr == ':');
8170 argptr++;
8171 }
8172
8173 if (hasColon)
8174 badInput = (*argptr != ':'); /* Not a class::method */
8175 else
8176 badInput = isdigit (*arg); /* a simple line number */
8177 }
8178
8179 if (badInput)
8180 printf_filtered (_("Usage: stop in <function | address>\n"));
8181 else
8182 break_command_1 (arg, 0, from_tty);
8183 }
8184
8185 static void
8186 stopat_command (char *arg, int from_tty)
8187 {
8188 int badInput = 0;
8189
8190 if (arg == (char *) NULL || *arg == '*') /* no line number */
8191 badInput = 1;
8192 else
8193 {
8194 char *argptr = arg;
8195 int hasColon = 0;
8196
8197 /* Look for a ':'. If there is a '::' then get out, otherwise
8198 it is probably a line number. */
8199 while (*argptr && !hasColon)
8200 {
8201 hasColon = (*argptr == ':');
8202 argptr++;
8203 }
8204
8205 if (hasColon)
8206 badInput = (*argptr == ':'); /* we have class::method */
8207 else
8208 badInput = !isdigit (*arg); /* not a line number */
8209 }
8210
8211 if (badInput)
8212 printf_filtered (_("Usage: stop at <line>\n"));
8213 else
8214 break_command_1 (arg, 0, from_tty);
8215 }
8216
8217 /* Return non-zero if EXP is verified as constant. Returned zero
8218 means EXP is variable. Also the constant detection may fail for
8219 some constant expressions and in such case still falsely return
8220 zero. */
8221 static int
8222 watchpoint_exp_is_const (const struct expression *exp)
8223 {
8224 int i = exp->nelts;
8225
8226 while (i > 0)
8227 {
8228 int oplenp, argsp;
8229
8230 /* We are only interested in the descriptor of each element. */
8231 operator_length (exp, i, &oplenp, &argsp);
8232 i -= oplenp;
8233
8234 switch (exp->elts[i].opcode)
8235 {
8236 case BINOP_ADD:
8237 case BINOP_SUB:
8238 case BINOP_MUL:
8239 case BINOP_DIV:
8240 case BINOP_REM:
8241 case BINOP_MOD:
8242 case BINOP_LSH:
8243 case BINOP_RSH:
8244 case BINOP_LOGICAL_AND:
8245 case BINOP_LOGICAL_OR:
8246 case BINOP_BITWISE_AND:
8247 case BINOP_BITWISE_IOR:
8248 case BINOP_BITWISE_XOR:
8249 case BINOP_EQUAL:
8250 case BINOP_NOTEQUAL:
8251 case BINOP_LESS:
8252 case BINOP_GTR:
8253 case BINOP_LEQ:
8254 case BINOP_GEQ:
8255 case BINOP_REPEAT:
8256 case BINOP_COMMA:
8257 case BINOP_EXP:
8258 case BINOP_MIN:
8259 case BINOP_MAX:
8260 case BINOP_INTDIV:
8261 case BINOP_CONCAT:
8262 case BINOP_IN:
8263 case BINOP_RANGE:
8264 case TERNOP_COND:
8265 case TERNOP_SLICE:
8266 case TERNOP_SLICE_COUNT:
8267
8268 case OP_LONG:
8269 case OP_DOUBLE:
8270 case OP_DECFLOAT:
8271 case OP_LAST:
8272 case OP_COMPLEX:
8273 case OP_STRING:
8274 case OP_BITSTRING:
8275 case OP_ARRAY:
8276 case OP_TYPE:
8277 case OP_NAME:
8278 case OP_OBJC_NSSTRING:
8279
8280 case UNOP_NEG:
8281 case UNOP_LOGICAL_NOT:
8282 case UNOP_COMPLEMENT:
8283 case UNOP_ADDR:
8284 case UNOP_HIGH:
8285 /* Unary, binary and ternary operators: We have to check
8286 their operands. If they are constant, then so is the
8287 result of that operation. For instance, if A and B are
8288 determined to be constants, then so is "A + B".
8289
8290 UNOP_IND is one exception to the rule above, because the
8291 value of *ADDR is not necessarily a constant, even when
8292 ADDR is. */
8293 break;
8294
8295 case OP_VAR_VALUE:
8296 /* Check whether the associated symbol is a constant.
8297
8298 We use SYMBOL_CLASS rather than TYPE_CONST because it's
8299 possible that a buggy compiler could mark a variable as
8300 constant even when it is not, and TYPE_CONST would return
8301 true in this case, while SYMBOL_CLASS wouldn't.
8302
8303 We also have to check for function symbols because they
8304 are always constant. */
8305 {
8306 struct symbol *s = exp->elts[i + 2].symbol;
8307
8308 if (SYMBOL_CLASS (s) != LOC_BLOCK
8309 && SYMBOL_CLASS (s) != LOC_CONST
8310 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8311 return 0;
8312 break;
8313 }
8314
8315 /* The default action is to return 0 because we are using
8316 the optimistic approach here: If we don't know something,
8317 then it is not a constant. */
8318 default:
8319 return 0;
8320 }
8321 }
8322
8323 return 1;
8324 }
8325
8326 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8327
8328 static int
8329 insert_watchpoint (struct bp_location *bl)
8330 {
8331 int length = bl->owner->exact? 1 : bl->length;
8332
8333 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
8334 bl->owner->cond_exp);
8335 }
8336
8337 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8338
8339 static int
8340 remove_watchpoint (struct bp_location *bl)
8341 {
8342 int length = bl->owner->exact? 1 : bl->length;
8343
8344 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
8345 bl->owner->cond_exp);
8346 }
8347
8348 /* Implement the "resources_needed" breakpoint_ops method for
8349 hardware watchpoints. */
8350
8351 static int
8352 resources_needed_watchpoint (const struct bp_location *bl)
8353 {
8354 int length = bl->owner->exact? 1 : bl->length;
8355
8356 return target_region_ok_for_hw_watchpoint (bl->address, length);
8357 }
8358
8359 /* The breakpoint_ops structure to be used in hardware watchpoints. */
8360
8361 static struct breakpoint_ops watchpoint_breakpoint_ops =
8362 {
8363 insert_watchpoint,
8364 remove_watchpoint,
8365 NULL, /* breakpoint_hit */
8366 resources_needed_watchpoint,
8367 NULL, /* print_it */
8368 NULL, /* print_one */
8369 NULL, /* print_mention */
8370 NULL /* print_recreate */
8371 };
8372
8373 /* accessflag: hw_write: watch write,
8374 hw_read: watch read,
8375 hw_access: watch access (read or write) */
8376 static void
8377 watch_command_1 (char *arg, int accessflag, int from_tty,
8378 int just_location, int internal)
8379 {
8380 struct breakpoint *b, *scope_breakpoint = NULL;
8381 struct expression *exp;
8382 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
8383 struct value *val, *mark, *result;
8384 struct frame_info *frame;
8385 char *exp_start = NULL;
8386 char *exp_end = NULL;
8387 char *tok, *id_tok_start, *end_tok;
8388 int toklen;
8389 char *cond_start = NULL;
8390 char *cond_end = NULL;
8391 int i, other_type_used, target_resources_ok = 0;
8392 enum bptype bp_type;
8393 int reg_cnt = 0;
8394 int thread = -1;
8395 int pc = 0;
8396
8397 /* Make sure that we actually have parameters to parse. */
8398 if (arg != NULL && arg[0] != '\0')
8399 {
8400 toklen = strlen (arg); /* Size of argument list. */
8401
8402 /* Points tok to the end of the argument list. */
8403 tok = arg + toklen - 1;
8404
8405 /* Go backwards in the parameters list. Skip the last
8406 parameter. If we're expecting a 'thread <thread_num>'
8407 parameter, this should be the thread identifier. */
8408 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8409 tok--;
8410 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8411 tok--;
8412
8413 /* Points end_tok to the beginning of the last token. */
8414 id_tok_start = tok + 1;
8415
8416 /* Go backwards in the parameters list. Skip one more
8417 parameter. If we're expecting a 'thread <thread_num>'
8418 parameter, we should reach a "thread" token. */
8419 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8420 tok--;
8421
8422 end_tok = tok;
8423
8424 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8425 tok--;
8426
8427 /* Move the pointer forward to skip the whitespace and
8428 calculate the length of the token. */
8429 tok++;
8430 toklen = end_tok - tok;
8431
8432 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8433 {
8434 /* At this point we've found a "thread" token, which means
8435 the user is trying to set a watchpoint that triggers
8436 only in a specific thread. */
8437 char *endp;
8438
8439 /* Extract the thread ID from the next token. */
8440 thread = strtol (id_tok_start, &endp, 0);
8441
8442 /* Check if the user provided a valid numeric value for the
8443 thread ID. */
8444 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
8445 error (_("Invalid thread ID specification %s."), id_tok_start);
8446
8447 /* Check if the thread actually exists. */
8448 if (!valid_thread_id (thread))
8449 error (_("Unknown thread %d."), thread);
8450
8451 /* Truncate the string and get rid of the thread <thread_num>
8452 parameter before the parameter list is parsed by the
8453 evaluate_expression() function. */
8454 *tok = '\0';
8455 }
8456 }
8457
8458 /* Parse the rest of the arguments. */
8459 innermost_block = NULL;
8460 exp_start = arg;
8461 exp = parse_exp_1 (&arg, 0, 0);
8462 exp_end = arg;
8463 /* Remove trailing whitespace from the expression before saving it.
8464 This makes the eventual display of the expression string a bit
8465 prettier. */
8466 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
8467 --exp_end;
8468
8469 /* Checking if the expression is not constant. */
8470 if (watchpoint_exp_is_const (exp))
8471 {
8472 int len;
8473
8474 len = exp_end - exp_start;
8475 while (len > 0 && isspace (exp_start[len - 1]))
8476 len--;
8477 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
8478 }
8479
8480 exp_valid_block = innermost_block;
8481 mark = value_mark ();
8482 fetch_subexp_value (exp, &pc, &val, &result, NULL);
8483
8484 if (just_location)
8485 {
8486 exp_valid_block = NULL;
8487 val = value_addr (result);
8488 release_value (val);
8489 value_free_to_mark (mark);
8490 }
8491 else if (val != NULL)
8492 release_value (val);
8493
8494 tok = skip_spaces (arg);
8495 end_tok = skip_to_space (tok);
8496
8497 toklen = end_tok - tok;
8498 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8499 {
8500 struct expression *cond;
8501
8502 innermost_block = NULL;
8503 tok = cond_start = end_tok + 1;
8504 cond = parse_exp_1 (&tok, 0, 0);
8505
8506 /* The watchpoint expression may not be local, but the condition
8507 may still be. E.g.: `watch global if local > 0'. */
8508 cond_exp_valid_block = innermost_block;
8509
8510 xfree (cond);
8511 cond_end = tok;
8512 }
8513 if (*tok)
8514 error (_("Junk at end of command."));
8515
8516 if (accessflag == hw_read)
8517 bp_type = bp_read_watchpoint;
8518 else if (accessflag == hw_access)
8519 bp_type = bp_access_watchpoint;
8520 else
8521 bp_type = bp_hardware_watchpoint;
8522
8523 reg_cnt = can_use_hardware_watchpoint (val, target_exact_watchpoints);
8524 if (reg_cnt == 0 && bp_type != bp_hardware_watchpoint)
8525 error (_("Expression cannot be implemented with read/access watchpoint."));
8526 if (reg_cnt != 0)
8527 {
8528 i = hw_watchpoint_used_count (bp_type, &other_type_used);
8529 target_resources_ok =
8530 target_can_use_hardware_watchpoint (bp_type, i + reg_cnt,
8531 other_type_used);
8532 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8533 error (_("Target does not support this type of hardware watchpoint."));
8534
8535 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
8536 error (_("Target can only support one kind "
8537 "of HW watchpoint at a time."));
8538 }
8539
8540 /* Change the type of breakpoint to an ordinary watchpoint if a
8541 hardware watchpoint could not be set. */
8542 if (!reg_cnt || target_resources_ok <= 0)
8543 bp_type = bp_watchpoint;
8544
8545 frame = block_innermost_frame (exp_valid_block);
8546
8547 /* If the expression is "local", then set up a "watchpoint scope"
8548 breakpoint at the point where we've left the scope of the watchpoint
8549 expression. Create the scope breakpoint before the watchpoint, so
8550 that we will encounter it first in bpstat_stop_status. */
8551 if (exp_valid_block && frame)
8552 {
8553 if (frame_id_p (frame_unwind_caller_id (frame)))
8554 {
8555 scope_breakpoint
8556 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
8557 frame_unwind_caller_pc (frame),
8558 bp_watchpoint_scope);
8559
8560 scope_breakpoint->enable_state = bp_enabled;
8561
8562 /* Automatically delete the breakpoint when it hits. */
8563 scope_breakpoint->disposition = disp_del;
8564
8565 /* Only break in the proper frame (help with recursion). */
8566 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
8567
8568 /* Set the address at which we will stop. */
8569 scope_breakpoint->loc->gdbarch
8570 = frame_unwind_caller_arch (frame);
8571 scope_breakpoint->loc->requested_address
8572 = frame_unwind_caller_pc (frame);
8573 scope_breakpoint->loc->address
8574 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
8575 scope_breakpoint->loc->requested_address,
8576 scope_breakpoint->type);
8577 }
8578 }
8579
8580 /* Now set up the breakpoint. */
8581 b = set_raw_breakpoint_without_location (NULL, bp_type);
8582 set_breakpoint_number (internal, b);
8583 b->thread = thread;
8584 b->disposition = disp_donttouch;
8585 b->exp = exp;
8586 b->exp_valid_block = exp_valid_block;
8587 b->cond_exp_valid_block = cond_exp_valid_block;
8588 if (just_location)
8589 {
8590 struct type *t = value_type (val);
8591 CORE_ADDR addr = value_as_address (val);
8592 char *name;
8593
8594 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
8595 name = type_to_string (t);
8596
8597 b->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
8598 core_addr_to_string (addr));
8599 xfree (name);
8600
8601 b->exp_string = xstrprintf ("-location: %.*s",
8602 (int) (exp_end - exp_start), exp_start);
8603
8604 /* The above expression is in C. */
8605 b->language = language_c;
8606 }
8607 else
8608 b->exp_string = savestring (exp_start, exp_end - exp_start);
8609 b->val = val;
8610 b->val_valid = 1;
8611 b->ops = &watchpoint_breakpoint_ops;
8612
8613 /* Use an exact watchpoint when there's only one memory region to be
8614 watched, and only one debug register is needed to watch it. */
8615 b->exact = target_exact_watchpoints && reg_cnt == 1;
8616
8617 if (cond_start)
8618 b->cond_string = savestring (cond_start, cond_end - cond_start);
8619 else
8620 b->cond_string = 0;
8621
8622 if (frame)
8623 {
8624 b->watchpoint_frame = get_frame_id (frame);
8625 b->watchpoint_thread = inferior_ptid;
8626 }
8627 else
8628 {
8629 b->watchpoint_frame = null_frame_id;
8630 b->watchpoint_thread = null_ptid;
8631 }
8632
8633 if (scope_breakpoint != NULL)
8634 {
8635 /* The scope breakpoint is related to the watchpoint. We will
8636 need to act on them together. */
8637 b->related_breakpoint = scope_breakpoint;
8638 scope_breakpoint->related_breakpoint = b;
8639 }
8640
8641 if (!just_location)
8642 value_free_to_mark (mark);
8643
8644 /* Finally update the new watchpoint. This creates the locations
8645 that should be inserted. */
8646 update_watchpoint (b, 1);
8647 if (internal)
8648 /* Do not mention breakpoints with a negative number, but do
8649 notify observers. */
8650 observer_notify_breakpoint_created (b->number);
8651 else
8652 mention (b);
8653 update_global_location_list (1);
8654 }
8655
8656 /* Return count of debug registers needed to watch the given expression.
8657 If EXACT_WATCHPOINTS is 1, then consider that only the address of
8658 the start of the watched region will be monitored (i.e., all accesses
8659 will be aligned). This uses less debug registers on some targets.
8660
8661 If the watchpoint cannot be handled in hardware return zero. */
8662
8663 static int
8664 can_use_hardware_watchpoint (struct value *v, int exact_watchpoints)
8665 {
8666 int found_memory_cnt = 0;
8667 struct value *head = v;
8668
8669 /* Did the user specifically forbid us to use hardware watchpoints? */
8670 if (!can_use_hw_watchpoints)
8671 return 0;
8672
8673 /* Make sure that the value of the expression depends only upon
8674 memory contents, and values computed from them within GDB. If we
8675 find any register references or function calls, we can't use a
8676 hardware watchpoint.
8677
8678 The idea here is that evaluating an expression generates a series
8679 of values, one holding the value of every subexpression. (The
8680 expression a*b+c has five subexpressions: a, b, a*b, c, and
8681 a*b+c.) GDB's values hold almost enough information to establish
8682 the criteria given above --- they identify memory lvalues,
8683 register lvalues, computed values, etcetera. So we can evaluate
8684 the expression, and then scan the chain of values that leaves
8685 behind to decide whether we can detect any possible change to the
8686 expression's final value using only hardware watchpoints.
8687
8688 However, I don't think that the values returned by inferior
8689 function calls are special in any way. So this function may not
8690 notice that an expression involving an inferior function call
8691 can't be watched with hardware watchpoints. FIXME. */
8692 for (; v; v = value_next (v))
8693 {
8694 if (VALUE_LVAL (v) == lval_memory)
8695 {
8696 if (v != head && value_lazy (v))
8697 /* A lazy memory lvalue in the chain is one that GDB never
8698 needed to fetch; we either just used its address (e.g.,
8699 `a' in `a.b') or we never needed it at all (e.g., `a'
8700 in `a,b'). This doesn't apply to HEAD; if that is
8701 lazy then it was not readable, but watch it anyway. */
8702 ;
8703 else
8704 {
8705 /* Ahh, memory we actually used! Check if we can cover
8706 it with hardware watchpoints. */
8707 struct type *vtype = check_typedef (value_type (v));
8708
8709 /* We only watch structs and arrays if user asked for it
8710 explicitly, never if they just happen to appear in a
8711 middle of some value chain. */
8712 if (v == head
8713 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
8714 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
8715 {
8716 CORE_ADDR vaddr = value_address (v);
8717 int len;
8718 int num_regs;
8719
8720 len = (exact_watchpoints
8721 && is_scalar_type_recursive (vtype))?
8722 1 : TYPE_LENGTH (value_type (v));
8723
8724 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
8725 if (!num_regs)
8726 return 0;
8727 else
8728 found_memory_cnt += num_regs;
8729 }
8730 }
8731 }
8732 else if (VALUE_LVAL (v) != not_lval
8733 && deprecated_value_modifiable (v) == 0)
8734 return 0; /* These are values from the history (e.g., $1). */
8735 else if (VALUE_LVAL (v) == lval_register)
8736 return 0; /* Cannot watch a register with a HW watchpoint. */
8737 }
8738
8739 /* The expression itself looks suitable for using a hardware
8740 watchpoint, but give the target machine a chance to reject it. */
8741 return found_memory_cnt;
8742 }
8743
8744 void
8745 watch_command_wrapper (char *arg, int from_tty, int internal)
8746 {
8747 watch_command_1 (arg, hw_write, from_tty, 0, internal);
8748 }
8749
8750 /* A helper function that looks for an argument at the start of a
8751 string. The argument must also either be at the end of the string,
8752 or be followed by whitespace. Returns 1 if it finds the argument,
8753 0 otherwise. If the argument is found, it updates *STR. */
8754
8755 static int
8756 check_for_argument (char **str, char *arg, int arg_len)
8757 {
8758 if (strncmp (*str, arg, arg_len) == 0
8759 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
8760 {
8761 *str += arg_len;
8762 return 1;
8763 }
8764 return 0;
8765 }
8766
8767 /* A helper function that looks for the "-location" argument and then
8768 calls watch_command_1. */
8769
8770 static void
8771 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
8772 {
8773 int just_location = 0;
8774
8775 if (arg
8776 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
8777 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
8778 {
8779 arg = skip_spaces (arg);
8780 just_location = 1;
8781 }
8782
8783 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8784 }
8785
8786 static void
8787 watch_command (char *arg, int from_tty)
8788 {
8789 watch_maybe_just_location (arg, hw_write, from_tty);
8790 }
8791
8792 void
8793 rwatch_command_wrapper (char *arg, int from_tty, int internal)
8794 {
8795 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8796 }
8797
8798 static void
8799 rwatch_command (char *arg, int from_tty)
8800 {
8801 watch_maybe_just_location (arg, hw_read, from_tty);
8802 }
8803
8804 void
8805 awatch_command_wrapper (char *arg, int from_tty, int internal)
8806 {
8807 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8808 }
8809
8810 static void
8811 awatch_command (char *arg, int from_tty)
8812 {
8813 watch_maybe_just_location (arg, hw_access, from_tty);
8814 }
8815 \f
8816
8817 /* Helper routines for the until_command routine in infcmd.c. Here
8818 because it uses the mechanisms of breakpoints. */
8819
8820 struct until_break_command_continuation_args
8821 {
8822 struct breakpoint *breakpoint;
8823 struct breakpoint *breakpoint2;
8824 int thread_num;
8825 };
8826
8827 /* This function is called by fetch_inferior_event via the
8828 cmd_continuation pointer, to complete the until command. It takes
8829 care of cleaning up the temporary breakpoints set up by the until
8830 command. */
8831 static void
8832 until_break_command_continuation (void *arg)
8833 {
8834 struct until_break_command_continuation_args *a = arg;
8835
8836 delete_breakpoint (a->breakpoint);
8837 if (a->breakpoint2)
8838 delete_breakpoint (a->breakpoint2);
8839 delete_longjmp_breakpoint (a->thread_num);
8840 }
8841
8842 void
8843 until_break_command (char *arg, int from_tty, int anywhere)
8844 {
8845 struct symtabs_and_lines sals;
8846 struct symtab_and_line sal;
8847 struct frame_info *frame = get_selected_frame (NULL);
8848 struct breakpoint *breakpoint;
8849 struct breakpoint *breakpoint2 = NULL;
8850 struct cleanup *old_chain;
8851 int thread;
8852 struct thread_info *tp;
8853
8854 clear_proceed_status ();
8855
8856 /* Set a breakpoint where the user wants it and at return from
8857 this function. */
8858
8859 if (default_breakpoint_valid)
8860 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
8861 default_breakpoint_line, (char ***) NULL, NULL);
8862 else
8863 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL,
8864 0, (char ***) NULL, NULL);
8865
8866 if (sals.nelts != 1)
8867 error (_("Couldn't get information on specified line."));
8868
8869 sal = sals.sals[0];
8870 xfree (sals.sals); /* malloc'd, so freed. */
8871
8872 if (*arg)
8873 error (_("Junk at end of arguments."));
8874
8875 resolve_sal_pc (&sal);
8876
8877 if (anywhere)
8878 /* If the user told us to continue until a specified location,
8879 we don't specify a frame at which we need to stop. */
8880 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
8881 null_frame_id, bp_until);
8882 else
8883 /* Otherwise, specify the selected frame, because we want to stop
8884 only at the very same frame. */
8885 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
8886 get_stack_frame_id (frame),
8887 bp_until);
8888
8889 old_chain = make_cleanup_delete_breakpoint (breakpoint);
8890
8891 tp = inferior_thread ();
8892 thread = tp->num;
8893
8894 /* Keep within the current frame, or in frames called by the current
8895 one. */
8896
8897 if (frame_id_p (frame_unwind_caller_id (frame)))
8898 {
8899 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
8900 sal.pc = frame_unwind_caller_pc (frame);
8901 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
8902 sal,
8903 frame_unwind_caller_id (frame),
8904 bp_until);
8905 make_cleanup_delete_breakpoint (breakpoint2);
8906
8907 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
8908 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
8909 }
8910
8911 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
8912
8913 /* If we are running asynchronously, and proceed call above has
8914 actually managed to start the target, arrange for breakpoints to
8915 be deleted when the target stops. Otherwise, we're already
8916 stopped and delete breakpoints via cleanup chain. */
8917
8918 if (target_can_async_p () && is_running (inferior_ptid))
8919 {
8920 struct until_break_command_continuation_args *args;
8921 args = xmalloc (sizeof (*args));
8922
8923 args->breakpoint = breakpoint;
8924 args->breakpoint2 = breakpoint2;
8925 args->thread_num = thread;
8926
8927 discard_cleanups (old_chain);
8928 add_continuation (inferior_thread (),
8929 until_break_command_continuation, args,
8930 xfree);
8931 }
8932 else
8933 do_cleanups (old_chain);
8934 }
8935
8936 /* This function attempts to parse an optional "if <cond>" clause
8937 from the arg string. If one is not found, it returns NULL.
8938
8939 Else, it returns a pointer to the condition string. (It does not
8940 attempt to evaluate the string against a particular block.) And,
8941 it updates arg to point to the first character following the parsed
8942 if clause in the arg string. */
8943
8944 static char *
8945 ep_parse_optional_if_clause (char **arg)
8946 {
8947 char *cond_string;
8948
8949 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
8950 return NULL;
8951
8952 /* Skip the "if" keyword. */
8953 (*arg) += 2;
8954
8955 /* Skip any extra leading whitespace, and record the start of the
8956 condition string. */
8957 *arg = skip_spaces (*arg);
8958 cond_string = *arg;
8959
8960 /* Assume that the condition occupies the remainder of the arg
8961 string. */
8962 (*arg) += strlen (cond_string);
8963
8964 return cond_string;
8965 }
8966
8967 /* Commands to deal with catching events, such as signals, exceptions,
8968 process start/exit, etc. */
8969
8970 typedef enum
8971 {
8972 catch_fork_temporary, catch_vfork_temporary,
8973 catch_fork_permanent, catch_vfork_permanent
8974 }
8975 catch_fork_kind;
8976
8977 static void
8978 catch_fork_command_1 (char *arg, int from_tty,
8979 struct cmd_list_element *command)
8980 {
8981 struct gdbarch *gdbarch = get_current_arch ();
8982 char *cond_string = NULL;
8983 catch_fork_kind fork_kind;
8984 int tempflag;
8985
8986 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
8987 tempflag = (fork_kind == catch_fork_temporary
8988 || fork_kind == catch_vfork_temporary);
8989
8990 if (!arg)
8991 arg = "";
8992 arg = skip_spaces (arg);
8993
8994 /* The allowed syntax is:
8995 catch [v]fork
8996 catch [v]fork if <cond>
8997
8998 First, check if there's an if clause. */
8999 cond_string = ep_parse_optional_if_clause (&arg);
9000
9001 if ((*arg != '\0') && !isspace (*arg))
9002 error (_("Junk at end of arguments."));
9003
9004 /* If this target supports it, create a fork or vfork catchpoint
9005 and enable reporting of such events. */
9006 switch (fork_kind)
9007 {
9008 case catch_fork_temporary:
9009 case catch_fork_permanent:
9010 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
9011 &catch_fork_breakpoint_ops);
9012 break;
9013 case catch_vfork_temporary:
9014 case catch_vfork_permanent:
9015 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
9016 &catch_vfork_breakpoint_ops);
9017 break;
9018 default:
9019 error (_("unsupported or unknown fork kind; cannot catch it"));
9020 break;
9021 }
9022 }
9023
9024 static void
9025 catch_exec_command_1 (char *arg, int from_tty,
9026 struct cmd_list_element *command)
9027 {
9028 struct gdbarch *gdbarch = get_current_arch ();
9029 int tempflag;
9030 char *cond_string = NULL;
9031
9032 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9033
9034 if (!arg)
9035 arg = "";
9036 arg = skip_spaces (arg);
9037
9038 /* The allowed syntax is:
9039 catch exec
9040 catch exec if <cond>
9041
9042 First, check if there's an if clause. */
9043 cond_string = ep_parse_optional_if_clause (&arg);
9044
9045 if ((*arg != '\0') && !isspace (*arg))
9046 error (_("Junk at end of arguments."));
9047
9048 /* If this target supports it, create an exec catchpoint
9049 and enable reporting of such events. */
9050 create_catchpoint (gdbarch, tempflag, cond_string,
9051 &catch_exec_breakpoint_ops);
9052 }
9053
9054 static enum print_stop_action
9055 print_exception_catchpoint (struct breakpoint *b)
9056 {
9057 int bp_temp, bp_throw;
9058
9059 annotate_catchpoint (b->number);
9060
9061 bp_throw = strstr (b->addr_string, "throw") != NULL;
9062 if (b->loc->address != b->loc->requested_address)
9063 breakpoint_adjustment_warning (b->loc->requested_address,
9064 b->loc->address,
9065 b->number, 1);
9066 bp_temp = b->disposition == disp_del;
9067 ui_out_text (uiout,
9068 bp_temp ? "Temporary catchpoint "
9069 : "Catchpoint ");
9070 if (!ui_out_is_mi_like_p (uiout))
9071 ui_out_field_int (uiout, "bkptno", b->number);
9072 ui_out_text (uiout,
9073 bp_throw ? " (exception thrown), "
9074 : " (exception caught), ");
9075 if (ui_out_is_mi_like_p (uiout))
9076 {
9077 ui_out_field_string (uiout, "reason",
9078 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9079 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9080 ui_out_field_int (uiout, "bkptno", b->number);
9081 }
9082 return PRINT_SRC_AND_LOC;
9083 }
9084
9085 static void
9086 print_one_exception_catchpoint (struct breakpoint *b,
9087 struct bp_location **last_loc)
9088 {
9089 struct value_print_options opts;
9090
9091 get_user_print_options (&opts);
9092 if (opts.addressprint)
9093 {
9094 annotate_field (4);
9095 if (b->loc == NULL || b->loc->shlib_disabled)
9096 ui_out_field_string (uiout, "addr", "<PENDING>");
9097 else
9098 ui_out_field_core_addr (uiout, "addr",
9099 b->loc->gdbarch, b->loc->address);
9100 }
9101 annotate_field (5);
9102 if (b->loc)
9103 *last_loc = b->loc;
9104 if (strstr (b->addr_string, "throw") != NULL)
9105 ui_out_field_string (uiout, "what", "exception throw");
9106 else
9107 ui_out_field_string (uiout, "what", "exception catch");
9108 }
9109
9110 static void
9111 print_mention_exception_catchpoint (struct breakpoint *b)
9112 {
9113 int bp_temp;
9114 int bp_throw;
9115
9116 bp_temp = b->disposition == disp_del;
9117 bp_throw = strstr (b->addr_string, "throw") != NULL;
9118 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9119 : _("Catchpoint "));
9120 ui_out_field_int (uiout, "bkptno", b->number);
9121 ui_out_text (uiout, bp_throw ? _(" (throw)")
9122 : _(" (catch)"));
9123 }
9124
9125 /* Implement the "print_recreate" breakpoint_ops method for throw and
9126 catch catchpoints. */
9127
9128 static void
9129 print_recreate_exception_catchpoint (struct breakpoint *b,
9130 struct ui_file *fp)
9131 {
9132 int bp_temp;
9133 int bp_throw;
9134
9135 bp_temp = b->disposition == disp_del;
9136 bp_throw = strstr (b->addr_string, "throw") != NULL;
9137 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9138 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
9139 }
9140
9141 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
9142 NULL, /* insert */
9143 NULL, /* remove */
9144 NULL, /* breakpoint_hit */
9145 NULL, /* resources_needed */
9146 print_exception_catchpoint,
9147 print_one_exception_catchpoint,
9148 print_mention_exception_catchpoint,
9149 print_recreate_exception_catchpoint
9150 };
9151
9152 static int
9153 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9154 enum exception_event_kind ex_event, int from_tty)
9155 {
9156 char *trigger_func_name;
9157
9158 if (ex_event == EX_EVENT_CATCH)
9159 trigger_func_name = "__cxa_begin_catch";
9160 else
9161 trigger_func_name = "__cxa_throw";
9162
9163 create_breakpoint (get_current_arch (),
9164 trigger_func_name, cond_string, -1,
9165 0 /* condition and thread are valid. */,
9166 tempflag, bp_breakpoint,
9167 0,
9168 AUTO_BOOLEAN_TRUE /* pending */,
9169 &gnu_v3_exception_catchpoint_ops, from_tty,
9170 1 /* enabled */,
9171 0 /* internal */);
9172
9173 return 1;
9174 }
9175
9176 /* Deal with "catch catch" and "catch throw" commands. */
9177
9178 static void
9179 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9180 int tempflag, int from_tty)
9181 {
9182 char *cond_string = NULL;
9183
9184 if (!arg)
9185 arg = "";
9186 arg = skip_spaces (arg);
9187
9188 cond_string = ep_parse_optional_if_clause (&arg);
9189
9190 if ((*arg != '\0') && !isspace (*arg))
9191 error (_("Junk at end of arguments."));
9192
9193 if (ex_event != EX_EVENT_THROW
9194 && ex_event != EX_EVENT_CATCH)
9195 error (_("Unsupported or unknown exception event; cannot catch it"));
9196
9197 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9198 return;
9199
9200 warning (_("Unsupported with this platform/compiler combination."));
9201 }
9202
9203 /* Implementation of "catch catch" command. */
9204
9205 static void
9206 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9207 {
9208 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9209
9210 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9211 }
9212
9213 /* Implementation of "catch throw" command. */
9214
9215 static void
9216 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9217 {
9218 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9219
9220 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9221 }
9222
9223 /* Create a breakpoint struct for Ada exception catchpoints. */
9224
9225 static void
9226 create_ada_exception_breakpoint (struct gdbarch *gdbarch,
9227 struct symtab_and_line sal,
9228 char *addr_string,
9229 char *exp_string,
9230 char *cond_string,
9231 struct expression *cond,
9232 struct breakpoint_ops *ops,
9233 int tempflag,
9234 int from_tty)
9235 {
9236 struct breakpoint *b;
9237
9238 if (from_tty)
9239 {
9240 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9241 if (!loc_gdbarch)
9242 loc_gdbarch = gdbarch;
9243
9244 describe_other_breakpoints (loc_gdbarch,
9245 sal.pspace, sal.pc, sal.section, -1);
9246 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9247 version for exception catchpoints, because two catchpoints
9248 used for different exception names will use the same address.
9249 In this case, a "breakpoint ... also set at..." warning is
9250 unproductive. Besides, the warning phrasing is also a bit
9251 inapropriate, we should use the word catchpoint, and tell
9252 the user what type of catchpoint it is. The above is good
9253 enough for now, though. */
9254 }
9255
9256 b = set_raw_breakpoint (gdbarch, sal, bp_breakpoint);
9257 set_breakpoint_count (breakpoint_count + 1);
9258
9259 b->enable_state = bp_enabled;
9260 b->disposition = tempflag ? disp_del : disp_donttouch;
9261 b->number = breakpoint_count;
9262 b->ignore_count = 0;
9263 b->loc->cond = cond;
9264 b->addr_string = addr_string;
9265 b->language = language_ada;
9266 b->cond_string = cond_string;
9267 b->exp_string = exp_string;
9268 b->thread = -1;
9269 b->ops = ops;
9270
9271 mention (b);
9272 update_global_location_list (1);
9273 }
9274
9275 /* Implement the "catch exception" command. */
9276
9277 static void
9278 catch_ada_exception_command (char *arg, int from_tty,
9279 struct cmd_list_element *command)
9280 {
9281 struct gdbarch *gdbarch = get_current_arch ();
9282 int tempflag;
9283 struct symtab_and_line sal;
9284 char *addr_string = NULL;
9285 char *exp_string = NULL;
9286 char *cond_string = NULL;
9287 struct expression *cond = NULL;
9288 struct breakpoint_ops *ops = NULL;
9289
9290 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9291
9292 if (!arg)
9293 arg = "";
9294 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
9295 &cond_string, &cond, &ops);
9296 create_ada_exception_breakpoint (gdbarch, sal, addr_string, exp_string,
9297 cond_string, cond, ops, tempflag,
9298 from_tty);
9299 }
9300
9301 /* Cleanup function for a syscall filter list. */
9302 static void
9303 clean_up_filters (void *arg)
9304 {
9305 VEC(int) *iter = *(VEC(int) **) arg;
9306 VEC_free (int, iter);
9307 }
9308
9309 /* Splits the argument using space as delimiter. Returns an xmalloc'd
9310 filter list, or NULL if no filtering is required. */
9311 static VEC(int) *
9312 catch_syscall_split_args (char *arg)
9313 {
9314 VEC(int) *result = NULL;
9315 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9316
9317 while (*arg != '\0')
9318 {
9319 int i, syscall_number;
9320 char *endptr;
9321 char cur_name[128];
9322 struct syscall s;
9323
9324 /* Skip whitespace. */
9325 while (isspace (*arg))
9326 arg++;
9327
9328 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9329 cur_name[i] = arg[i];
9330 cur_name[i] = '\0';
9331 arg += i;
9332
9333 /* Check if the user provided a syscall name or a number. */
9334 syscall_number = (int) strtol (cur_name, &endptr, 0);
9335 if (*endptr == '\0')
9336 get_syscall_by_number (syscall_number, &s);
9337 else
9338 {
9339 /* We have a name. Let's check if it's valid and convert it
9340 to a number. */
9341 get_syscall_by_name (cur_name, &s);
9342
9343 if (s.number == UNKNOWN_SYSCALL)
9344 /* Here we have to issue an error instead of a warning,
9345 because GDB cannot do anything useful if there's no
9346 syscall number to be caught. */
9347 error (_("Unknown syscall name '%s'."), cur_name);
9348 }
9349
9350 /* Ok, it's valid. */
9351 VEC_safe_push (int, result, s.number);
9352 }
9353
9354 discard_cleanups (cleanup);
9355 return result;
9356 }
9357
9358 /* Implement the "catch syscall" command. */
9359
9360 static void
9361 catch_syscall_command_1 (char *arg, int from_tty,
9362 struct cmd_list_element *command)
9363 {
9364 int tempflag;
9365 VEC(int) *filter;
9366 struct syscall s;
9367 struct gdbarch *gdbarch = get_current_arch ();
9368
9369 /* Checking if the feature if supported. */
9370 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9371 error (_("The feature 'catch syscall' is not supported on \
9372 this architecture yet."));
9373
9374 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9375
9376 arg = skip_spaces (arg);
9377
9378 /* We need to do this first "dummy" translation in order
9379 to get the syscall XML file loaded or, most important,
9380 to display a warning to the user if there's no XML file
9381 for his/her architecture. */
9382 get_syscall_by_number (0, &s);
9383
9384 /* The allowed syntax is:
9385 catch syscall
9386 catch syscall <name | number> [<name | number> ... <name | number>]
9387
9388 Let's check if there's a syscall name. */
9389
9390 if (arg != NULL)
9391 filter = catch_syscall_split_args (arg);
9392 else
9393 filter = NULL;
9394
9395 create_syscall_event_catchpoint (tempflag, filter,
9396 &catch_syscall_breakpoint_ops);
9397 }
9398
9399 /* Implement the "catch assert" command. */
9400
9401 static void
9402 catch_assert_command (char *arg, int from_tty,
9403 struct cmd_list_element *command)
9404 {
9405 struct gdbarch *gdbarch = get_current_arch ();
9406 int tempflag;
9407 struct symtab_and_line sal;
9408 char *addr_string = NULL;
9409 struct breakpoint_ops *ops = NULL;
9410
9411 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9412
9413 if (!arg)
9414 arg = "";
9415 sal = ada_decode_assert_location (arg, &addr_string, &ops);
9416 create_ada_exception_breakpoint (gdbarch, sal, addr_string, NULL, NULL, NULL,
9417 ops, tempflag, from_tty);
9418 }
9419
9420 static void
9421 catch_command (char *arg, int from_tty)
9422 {
9423 error (_("Catch requires an event name."));
9424 }
9425 \f
9426
9427 static void
9428 tcatch_command (char *arg, int from_tty)
9429 {
9430 error (_("Catch requires an event name."));
9431 }
9432
9433 /* Delete breakpoints by address or line. */
9434
9435 static void
9436 clear_command (char *arg, int from_tty)
9437 {
9438 struct breakpoint *b;
9439 VEC(breakpoint_p) *found = 0;
9440 int ix;
9441 int default_match;
9442 struct symtabs_and_lines sals;
9443 struct symtab_and_line sal;
9444 int i;
9445
9446 if (arg)
9447 {
9448 sals = decode_line_spec (arg, 1);
9449 default_match = 0;
9450 }
9451 else
9452 {
9453 sals.sals = (struct symtab_and_line *)
9454 xmalloc (sizeof (struct symtab_and_line));
9455 make_cleanup (xfree, sals.sals);
9456 init_sal (&sal); /* Initialize to zeroes. */
9457 sal.line = default_breakpoint_line;
9458 sal.symtab = default_breakpoint_symtab;
9459 sal.pc = default_breakpoint_address;
9460 sal.pspace = default_breakpoint_pspace;
9461 if (sal.symtab == 0)
9462 error (_("No source file specified."));
9463
9464 sals.sals[0] = sal;
9465 sals.nelts = 1;
9466
9467 default_match = 1;
9468 }
9469
9470 /* We don't call resolve_sal_pc here. That's not as bad as it
9471 seems, because all existing breakpoints typically have both
9472 file/line and pc set. So, if clear is given file/line, we can
9473 match this to existing breakpoint without obtaining pc at all.
9474
9475 We only support clearing given the address explicitly
9476 present in breakpoint table. Say, we've set breakpoint
9477 at file:line. There were several PC values for that file:line,
9478 due to optimization, all in one block.
9479
9480 We've picked one PC value. If "clear" is issued with another
9481 PC corresponding to the same file:line, the breakpoint won't
9482 be cleared. We probably can still clear the breakpoint, but
9483 since the other PC value is never presented to user, user
9484 can only find it by guessing, and it does not seem important
9485 to support that. */
9486
9487 /* For each line spec given, delete bps which correspond to it. Do
9488 it in two passes, solely to preserve the current behavior that
9489 from_tty is forced true if we delete more than one
9490 breakpoint. */
9491
9492 found = NULL;
9493 for (i = 0; i < sals.nelts; i++)
9494 {
9495 /* If exact pc given, clear bpts at that pc.
9496 If line given (pc == 0), clear all bpts on specified line.
9497 If defaulting, clear all bpts on default line
9498 or at default pc.
9499
9500 defaulting sal.pc != 0 tests to do
9501
9502 0 1 pc
9503 1 1 pc _and_ line
9504 0 0 line
9505 1 0 <can't happen> */
9506
9507 sal = sals.sals[i];
9508
9509 /* Find all matching breakpoints and add them to 'found'. */
9510 ALL_BREAKPOINTS (b)
9511 {
9512 int match = 0;
9513 /* Are we going to delete b? */
9514 if (b->type != bp_none && !is_watchpoint (b))
9515 {
9516 struct bp_location *loc = b->loc;
9517 for (; loc; loc = loc->next)
9518 {
9519 int pc_match = sal.pc
9520 && (loc->pspace == sal.pspace)
9521 && (loc->address == sal.pc)
9522 && (!section_is_overlay (loc->section)
9523 || loc->section == sal.section);
9524 int line_match = ((default_match || (0 == sal.pc))
9525 && b->source_file != NULL
9526 && sal.symtab != NULL
9527 && sal.pspace == loc->pspace
9528 && filename_cmp (b->source_file,
9529 sal.symtab->filename) == 0
9530 && b->line_number == sal.line);
9531 if (pc_match || line_match)
9532 {
9533 match = 1;
9534 break;
9535 }
9536 }
9537 }
9538
9539 if (match)
9540 VEC_safe_push(breakpoint_p, found, b);
9541 }
9542 }
9543 /* Now go thru the 'found' chain and delete them. */
9544 if (VEC_empty(breakpoint_p, found))
9545 {
9546 if (arg)
9547 error (_("No breakpoint at %s."), arg);
9548 else
9549 error (_("No breakpoint at this line."));
9550 }
9551
9552 if (VEC_length(breakpoint_p, found) > 1)
9553 from_tty = 1; /* Always report if deleted more than one. */
9554 if (from_tty)
9555 {
9556 if (VEC_length(breakpoint_p, found) == 1)
9557 printf_unfiltered (_("Deleted breakpoint "));
9558 else
9559 printf_unfiltered (_("Deleted breakpoints "));
9560 }
9561 breakpoints_changed ();
9562
9563 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
9564 {
9565 if (from_tty)
9566 printf_unfiltered ("%d ", b->number);
9567 delete_breakpoint (b);
9568 }
9569 if (from_tty)
9570 putchar_unfiltered ('\n');
9571 }
9572 \f
9573 /* Delete breakpoint in BS if they are `delete' breakpoints and
9574 all breakpoints that are marked for deletion, whether hit or not.
9575 This is called after any breakpoint is hit, or after errors. */
9576
9577 void
9578 breakpoint_auto_delete (bpstat bs)
9579 {
9580 struct breakpoint *b, *b_tmp;
9581
9582 for (; bs; bs = bs->next)
9583 if (bs->breakpoint_at
9584 && bs->breakpoint_at->disposition == disp_del
9585 && bs->stop)
9586 delete_breakpoint (bs->breakpoint_at);
9587
9588 ALL_BREAKPOINTS_SAFE (b, b_tmp)
9589 {
9590 if (b->disposition == disp_del_at_next_stop)
9591 delete_breakpoint (b);
9592 }
9593 }
9594
9595 /* A comparison function for bp_location AP and BP being interfaced to
9596 qsort. Sort elements primarily by their ADDRESS (no matter what
9597 does breakpoint_address_is_meaningful say for its OWNER),
9598 secondarily by ordering first bp_permanent OWNERed elements and
9599 terciarily just ensuring the array is sorted stable way despite
9600 qsort being an instable algorithm. */
9601
9602 static int
9603 bp_location_compare (const void *ap, const void *bp)
9604 {
9605 struct bp_location *a = *(void **) ap;
9606 struct bp_location *b = *(void **) bp;
9607 /* A and B come from existing breakpoints having non-NULL OWNER. */
9608 int a_perm = a->owner->enable_state == bp_permanent;
9609 int b_perm = b->owner->enable_state == bp_permanent;
9610
9611 if (a->address != b->address)
9612 return (a->address > b->address) - (a->address < b->address);
9613
9614 /* Sort permanent breakpoints first. */
9615 if (a_perm != b_perm)
9616 return (a_perm < b_perm) - (a_perm > b_perm);
9617
9618 /* Make the user-visible order stable across GDB runs. Locations of
9619 the same breakpoint can be sorted in arbitrary order. */
9620
9621 if (a->owner->number != b->owner->number)
9622 return (a->owner->number > b->owner->number)
9623 - (a->owner->number < b->owner->number);
9624
9625 return (a > b) - (a < b);
9626 }
9627
9628 /* Set bp_location_placed_address_before_address_max and
9629 bp_location_shadow_len_after_address_max according to the current
9630 content of the bp_location array. */
9631
9632 static void
9633 bp_location_target_extensions_update (void)
9634 {
9635 struct bp_location *bl, **blp_tmp;
9636
9637 bp_location_placed_address_before_address_max = 0;
9638 bp_location_shadow_len_after_address_max = 0;
9639
9640 ALL_BP_LOCATIONS (bl, blp_tmp)
9641 {
9642 CORE_ADDR start, end, addr;
9643
9644 if (!bp_location_has_shadow (bl))
9645 continue;
9646
9647 start = bl->target_info.placed_address;
9648 end = start + bl->target_info.shadow_len;
9649
9650 gdb_assert (bl->address >= start);
9651 addr = bl->address - start;
9652 if (addr > bp_location_placed_address_before_address_max)
9653 bp_location_placed_address_before_address_max = addr;
9654
9655 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
9656
9657 gdb_assert (bl->address < end);
9658 addr = end - bl->address;
9659 if (addr > bp_location_shadow_len_after_address_max)
9660 bp_location_shadow_len_after_address_max = addr;
9661 }
9662 }
9663
9664 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
9665 into the inferior, only remove already-inserted locations that no
9666 longer should be inserted. Functions that delete a breakpoint or
9667 breakpoints should pass false, so that deleting a breakpoint
9668 doesn't have the side effect of inserting the locations of other
9669 breakpoints that are marked not-inserted, but should_be_inserted
9670 returns true on them.
9671
9672 This behaviour is useful is situations close to tear-down -- e.g.,
9673 after an exec, while the target still has execution, but breakpoint
9674 shadows of the previous executable image should *NOT* be restored
9675 to the new image; or before detaching, where the target still has
9676 execution and wants to delete breakpoints from GDB's lists, and all
9677 breakpoints had already been removed from the inferior. */
9678
9679 static void
9680 update_global_location_list (int should_insert)
9681 {
9682 struct breakpoint *b;
9683 struct bp_location **locp, *loc;
9684 struct cleanup *cleanups;
9685
9686 /* Used in the duplicates detection below. When iterating over all
9687 bp_locations, points to the first bp_location of a given address.
9688 Breakpoints and watchpoints of different types are never
9689 duplicates of each other. Keep one pointer for each type of
9690 breakpoint/watchpoint, so we only need to loop over all locations
9691 once. */
9692 struct bp_location *bp_loc_first; /* breakpoint */
9693 struct bp_location *wp_loc_first; /* hardware watchpoint */
9694 struct bp_location *awp_loc_first; /* access watchpoint */
9695 struct bp_location *rwp_loc_first; /* read watchpoint */
9696
9697 /* Saved former bp_location array which we compare against the newly
9698 built bp_location from the current state of ALL_BREAKPOINTS. */
9699 struct bp_location **old_location, **old_locp;
9700 unsigned old_location_count;
9701
9702 old_location = bp_location;
9703 old_location_count = bp_location_count;
9704 bp_location = NULL;
9705 bp_location_count = 0;
9706 cleanups = make_cleanup (xfree, old_location);
9707
9708 ALL_BREAKPOINTS (b)
9709 for (loc = b->loc; loc; loc = loc->next)
9710 bp_location_count++;
9711
9712 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
9713 locp = bp_location;
9714 ALL_BREAKPOINTS (b)
9715 for (loc = b->loc; loc; loc = loc->next)
9716 *locp++ = loc;
9717 qsort (bp_location, bp_location_count, sizeof (*bp_location),
9718 bp_location_compare);
9719
9720 bp_location_target_extensions_update ();
9721
9722 /* Identify bp_location instances that are no longer present in the
9723 new list, and therefore should be freed. Note that it's not
9724 necessary that those locations should be removed from inferior --
9725 if there's another location at the same address (previously
9726 marked as duplicate), we don't need to remove/insert the
9727 location.
9728
9729 LOCP is kept in sync with OLD_LOCP, each pointing to the current
9730 and former bp_location array state respectively. */
9731
9732 locp = bp_location;
9733 for (old_locp = old_location; old_locp < old_location + old_location_count;
9734 old_locp++)
9735 {
9736 struct bp_location *old_loc = *old_locp;
9737 struct bp_location **loc2p;
9738
9739 /* Tells if 'old_loc' is found amoung the new locations. If
9740 not, we have to free it. */
9741 int found_object = 0;
9742 /* Tells if the location should remain inserted in the target. */
9743 int keep_in_target = 0;
9744 int removed = 0;
9745
9746 /* Skip LOCP entries which will definitely never be needed.
9747 Stop either at or being the one matching OLD_LOC. */
9748 while (locp < bp_location + bp_location_count
9749 && (*locp)->address < old_loc->address)
9750 locp++;
9751
9752 for (loc2p = locp;
9753 (loc2p < bp_location + bp_location_count
9754 && (*loc2p)->address == old_loc->address);
9755 loc2p++)
9756 {
9757 if (*loc2p == old_loc)
9758 {
9759 found_object = 1;
9760 break;
9761 }
9762 }
9763
9764 /* If this location is no longer present, and inserted, look if
9765 there's maybe a new location at the same address. If so,
9766 mark that one inserted, and don't remove this one. This is
9767 needed so that we don't have a time window where a breakpoint
9768 at certain location is not inserted. */
9769
9770 if (old_loc->inserted)
9771 {
9772 /* If the location is inserted now, we might have to remove
9773 it. */
9774
9775 if (found_object && should_be_inserted (old_loc))
9776 {
9777 /* The location is still present in the location list,
9778 and still should be inserted. Don't do anything. */
9779 keep_in_target = 1;
9780 }
9781 else
9782 {
9783 /* The location is either no longer present, or got
9784 disabled. See if there's another location at the
9785 same address, in which case we don't need to remove
9786 this one from the target. */
9787
9788 /* OLD_LOC comes from existing struct breakpoint. */
9789 if (breakpoint_address_is_meaningful (old_loc->owner))
9790 {
9791 for (loc2p = locp;
9792 (loc2p < bp_location + bp_location_count
9793 && (*loc2p)->address == old_loc->address);
9794 loc2p++)
9795 {
9796 struct bp_location *loc2 = *loc2p;
9797
9798 if (breakpoint_locations_match (loc2, old_loc))
9799 {
9800 /* For the sake of should_be_inserted.
9801 Duplicates check below will fix up this
9802 later. */
9803 loc2->duplicate = 0;
9804
9805 /* Read watchpoint locations are switched to
9806 access watchpoints, if the former are not
9807 supported, but the latter are. */
9808 if (is_hardware_watchpoint (old_loc->owner))
9809 {
9810 gdb_assert (is_hardware_watchpoint (loc2->owner));
9811 loc2->watchpoint_type = old_loc->watchpoint_type;
9812 }
9813
9814 if (loc2 != old_loc && should_be_inserted (loc2))
9815 {
9816 loc2->inserted = 1;
9817 loc2->target_info = old_loc->target_info;
9818 keep_in_target = 1;
9819 break;
9820 }
9821 }
9822 }
9823 }
9824 }
9825
9826 if (!keep_in_target)
9827 {
9828 if (remove_breakpoint (old_loc, mark_uninserted))
9829 {
9830 /* This is just about all we can do. We could keep
9831 this location on the global list, and try to
9832 remove it next time, but there's no particular
9833 reason why we will succeed next time.
9834
9835 Note that at this point, old_loc->owner is still
9836 valid, as delete_breakpoint frees the breakpoint
9837 only after calling us. */
9838 printf_filtered (_("warning: Error removing "
9839 "breakpoint %d\n"),
9840 old_loc->owner->number);
9841 }
9842 removed = 1;
9843 }
9844 }
9845
9846 if (!found_object)
9847 {
9848 if (removed && non_stop
9849 && breakpoint_address_is_meaningful (old_loc->owner)
9850 && !is_hardware_watchpoint (old_loc->owner))
9851 {
9852 /* This location was removed from the target. In
9853 non-stop mode, a race condition is possible where
9854 we've removed a breakpoint, but stop events for that
9855 breakpoint are already queued and will arrive later.
9856 We apply an heuristic to be able to distinguish such
9857 SIGTRAPs from other random SIGTRAPs: we keep this
9858 breakpoint location for a bit, and will retire it
9859 after we see some number of events. The theory here
9860 is that reporting of events should, "on the average",
9861 be fair, so after a while we'll see events from all
9862 threads that have anything of interest, and no longer
9863 need to keep this breakpoint location around. We
9864 don't hold locations forever so to reduce chances of
9865 mistaking a non-breakpoint SIGTRAP for a breakpoint
9866 SIGTRAP.
9867
9868 The heuristic failing can be disastrous on
9869 decr_pc_after_break targets.
9870
9871 On decr_pc_after_break targets, like e.g., x86-linux,
9872 if we fail to recognize a late breakpoint SIGTRAP,
9873 because events_till_retirement has reached 0 too
9874 soon, we'll fail to do the PC adjustment, and report
9875 a random SIGTRAP to the user. When the user resumes
9876 the inferior, it will most likely immediately crash
9877 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
9878 corrupted, because of being resumed e.g., in the
9879 middle of a multi-byte instruction, or skipped a
9880 one-byte instruction. This was actually seen happen
9881 on native x86-linux, and should be less rare on
9882 targets that do not support new thread events, like
9883 remote, due to the heuristic depending on
9884 thread_count.
9885
9886 Mistaking a random SIGTRAP for a breakpoint trap
9887 causes similar symptoms (PC adjustment applied when
9888 it shouldn't), but then again, playing with SIGTRAPs
9889 behind the debugger's back is asking for trouble.
9890
9891 Since hardware watchpoint traps are always
9892 distinguishable from other traps, so we don't need to
9893 apply keep hardware watchpoint moribund locations
9894 around. We simply always ignore hardware watchpoint
9895 traps we can no longer explain. */
9896
9897 old_loc->events_till_retirement = 3 * (thread_count () + 1);
9898 old_loc->owner = NULL;
9899
9900 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
9901 }
9902 else
9903 {
9904 old_loc->owner = NULL;
9905 decref_bp_location (&old_loc);
9906 }
9907 }
9908 }
9909
9910 /* Rescan breakpoints at the same address and section, marking the
9911 first one as "first" and any others as "duplicates". This is so
9912 that the bpt instruction is only inserted once. If we have a
9913 permanent breakpoint at the same place as BPT, make that one the
9914 official one, and the rest as duplicates. Permanent breakpoints
9915 are sorted first for the same address.
9916
9917 Do the same for hardware watchpoints, but also considering the
9918 watchpoint's type (regular/access/read) and length. */
9919
9920 bp_loc_first = NULL;
9921 wp_loc_first = NULL;
9922 awp_loc_first = NULL;
9923 rwp_loc_first = NULL;
9924 ALL_BP_LOCATIONS (loc, locp)
9925 {
9926 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
9927 non-NULL. */
9928 struct breakpoint *b = loc->owner;
9929 struct bp_location **loc_first_p;
9930
9931 if (b->enable_state == bp_disabled
9932 || b->enable_state == bp_call_disabled
9933 || b->enable_state == bp_startup_disabled
9934 || !loc->enabled
9935 || loc->shlib_disabled
9936 || !breakpoint_address_is_meaningful (b)
9937 || is_tracepoint (b))
9938 continue;
9939
9940 /* Permanent breakpoint should always be inserted. */
9941 if (b->enable_state == bp_permanent && ! loc->inserted)
9942 internal_error (__FILE__, __LINE__,
9943 _("allegedly permanent breakpoint is not "
9944 "actually inserted"));
9945
9946 if (b->type == bp_hardware_watchpoint)
9947 loc_first_p = &wp_loc_first;
9948 else if (b->type == bp_read_watchpoint)
9949 loc_first_p = &rwp_loc_first;
9950 else if (b->type == bp_access_watchpoint)
9951 loc_first_p = &awp_loc_first;
9952 else
9953 loc_first_p = &bp_loc_first;
9954
9955 if (*loc_first_p == NULL
9956 || (overlay_debugging && loc->section != (*loc_first_p)->section)
9957 || !breakpoint_locations_match (loc, *loc_first_p))
9958 {
9959 *loc_first_p = loc;
9960 loc->duplicate = 0;
9961 continue;
9962 }
9963
9964 loc->duplicate = 1;
9965
9966 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
9967 && b->enable_state != bp_permanent)
9968 internal_error (__FILE__, __LINE__,
9969 _("another breakpoint was inserted on top of "
9970 "a permanent breakpoint"));
9971 }
9972
9973 if (breakpoints_always_inserted_mode () && should_insert
9974 && (have_live_inferiors ()
9975 || (gdbarch_has_global_breakpoints (target_gdbarch))))
9976 insert_breakpoint_locations ();
9977
9978 do_cleanups (cleanups);
9979 }
9980
9981 void
9982 breakpoint_retire_moribund (void)
9983 {
9984 struct bp_location *loc;
9985 int ix;
9986
9987 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
9988 if (--(loc->events_till_retirement) == 0)
9989 {
9990 decref_bp_location (&loc);
9991 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
9992 --ix;
9993 }
9994 }
9995
9996 static void
9997 update_global_location_list_nothrow (int inserting)
9998 {
9999 struct gdb_exception e;
10000
10001 TRY_CATCH (e, RETURN_MASK_ERROR)
10002 update_global_location_list (inserting);
10003 }
10004
10005 /* Clear BKP from a BPS. */
10006
10007 static void
10008 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10009 {
10010 bpstat bs;
10011
10012 for (bs = bps; bs; bs = bs->next)
10013 if (bs->breakpoint_at == bpt)
10014 {
10015 bs->breakpoint_at = NULL;
10016 bs->old_val = NULL;
10017 /* bs->commands will be freed later. */
10018 }
10019 }
10020
10021 /* Callback for iterate_over_threads. */
10022 static int
10023 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10024 {
10025 struct breakpoint *bpt = data;
10026
10027 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10028 return 0;
10029 }
10030
10031 /* Delete a breakpoint and clean up all traces of it in the data
10032 structures. */
10033
10034 void
10035 delete_breakpoint (struct breakpoint *bpt)
10036 {
10037 struct breakpoint *b;
10038
10039 gdb_assert (bpt != NULL);
10040
10041 /* Has this bp already been deleted? This can happen because
10042 multiple lists can hold pointers to bp's. bpstat lists are
10043 especial culprits.
10044
10045 One example of this happening is a watchpoint's scope bp. When
10046 the scope bp triggers, we notice that the watchpoint is out of
10047 scope, and delete it. We also delete its scope bp. But the
10048 scope bp is marked "auto-deleting", and is already on a bpstat.
10049 That bpstat is then checked for auto-deleting bp's, which are
10050 deleted.
10051
10052 A real solution to this problem might involve reference counts in
10053 bp's, and/or giving them pointers back to their referencing
10054 bpstat's, and teaching delete_breakpoint to only free a bp's
10055 storage when no more references were extent. A cheaper bandaid
10056 was chosen. */
10057 if (bpt->type == bp_none)
10058 return;
10059
10060 /* At least avoid this stale reference until the reference counting
10061 of breakpoints gets resolved. */
10062 if (bpt->related_breakpoint != NULL)
10063 {
10064 gdb_assert (bpt->related_breakpoint->related_breakpoint == bpt);
10065 bpt->related_breakpoint->disposition = disp_del_at_next_stop;
10066 bpt->related_breakpoint->related_breakpoint = NULL;
10067 bpt->related_breakpoint = NULL;
10068 }
10069
10070 observer_notify_breakpoint_deleted (bpt->number);
10071
10072 if (breakpoint_chain == bpt)
10073 breakpoint_chain = bpt->next;
10074
10075 ALL_BREAKPOINTS (b)
10076 if (b->next == bpt)
10077 {
10078 b->next = bpt->next;
10079 break;
10080 }
10081
10082 decref_counted_command_line (&bpt->commands);
10083 xfree (bpt->cond_string);
10084 xfree (bpt->cond_exp);
10085 xfree (bpt->addr_string);
10086 xfree (bpt->exp);
10087 xfree (bpt->exp_string);
10088 xfree (bpt->exp_string_reparse);
10089 value_free (bpt->val);
10090 xfree (bpt->source_file);
10091 xfree (bpt->exec_pathname);
10092 clean_up_filters (&bpt->syscalls_to_be_caught);
10093
10094
10095 /* Be sure no bpstat's are pointing at the breakpoint after it's
10096 been freed. */
10097 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
10098 in all threeds for now. Note that we cannot just remove bpstats
10099 pointing at bpt from the stop_bpstat list entirely, as breakpoint
10100 commands are associated with the bpstat; if we remove it here,
10101 then the later call to bpstat_do_actions (&stop_bpstat); in
10102 event-top.c won't do anything, and temporary breakpoints with
10103 commands won't work. */
10104
10105 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
10106
10107 /* Now that breakpoint is removed from breakpoint list, update the
10108 global location list. This will remove locations that used to
10109 belong to this breakpoint. Do this before freeing the breakpoint
10110 itself, since remove_breakpoint looks at location's owner. It
10111 might be better design to have location completely
10112 self-contained, but it's not the case now. */
10113 update_global_location_list (0);
10114
10115
10116 /* On the chance that someone will soon try again to delete this
10117 same bp, we mark it as deleted before freeing its storage. */
10118 bpt->type = bp_none;
10119
10120 xfree (bpt);
10121 }
10122
10123 static void
10124 do_delete_breakpoint_cleanup (void *b)
10125 {
10126 delete_breakpoint (b);
10127 }
10128
10129 struct cleanup *
10130 make_cleanup_delete_breakpoint (struct breakpoint *b)
10131 {
10132 return make_cleanup (do_delete_breakpoint_cleanup, b);
10133 }
10134
10135 /* A callback for map_breakpoint_numbers that calls
10136 delete_breakpoint. */
10137
10138 static void
10139 do_delete_breakpoint (struct breakpoint *b, void *ignore)
10140 {
10141 delete_breakpoint (b);
10142 }
10143
10144 void
10145 delete_command (char *arg, int from_tty)
10146 {
10147 struct breakpoint *b, *b_tmp;
10148
10149 dont_repeat ();
10150
10151 if (arg == 0)
10152 {
10153 int breaks_to_delete = 0;
10154
10155 /* Delete all breakpoints if no argument.
10156 Do not delete internal or call-dummy breakpoints, these have
10157 to be deleted with an explicit breakpoint number argument. */
10158 ALL_BREAKPOINTS (b)
10159 {
10160 if (b->type != bp_call_dummy
10161 && b->type != bp_std_terminate
10162 && b->type != bp_shlib_event
10163 && b->type != bp_jit_event
10164 && b->type != bp_thread_event
10165 && b->type != bp_overlay_event
10166 && b->type != bp_longjmp_master
10167 && b->type != bp_std_terminate_master
10168 && b->type != bp_exception_master
10169 && b->number >= 0)
10170 {
10171 breaks_to_delete = 1;
10172 break;
10173 }
10174 }
10175
10176 /* Ask user only if there are some breakpoints to delete. */
10177 if (!from_tty
10178 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
10179 {
10180 ALL_BREAKPOINTS_SAFE (b, b_tmp)
10181 {
10182 if (b->type != bp_call_dummy
10183 && b->type != bp_std_terminate
10184 && b->type != bp_shlib_event
10185 && b->type != bp_thread_event
10186 && b->type != bp_jit_event
10187 && b->type != bp_overlay_event
10188 && b->type != bp_longjmp_master
10189 && b->type != bp_std_terminate_master
10190 && b->type != bp_exception_master
10191 && b->number >= 0)
10192 delete_breakpoint (b);
10193 }
10194 }
10195 }
10196 else
10197 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
10198 }
10199
10200 static int
10201 all_locations_are_pending (struct bp_location *loc)
10202 {
10203 for (; loc; loc = loc->next)
10204 if (!loc->shlib_disabled)
10205 return 0;
10206 return 1;
10207 }
10208
10209 /* Subroutine of update_breakpoint_locations to simplify it.
10210 Return non-zero if multiple fns in list LOC have the same name.
10211 Null names are ignored. */
10212
10213 static int
10214 ambiguous_names_p (struct bp_location *loc)
10215 {
10216 struct bp_location *l;
10217 htab_t htab = htab_create_alloc (13, htab_hash_string,
10218 (int (*) (const void *,
10219 const void *)) streq,
10220 NULL, xcalloc, xfree);
10221
10222 for (l = loc; l != NULL; l = l->next)
10223 {
10224 const char **slot;
10225 const char *name = l->function_name;
10226
10227 /* Allow for some names to be NULL, ignore them. */
10228 if (name == NULL)
10229 continue;
10230
10231 slot = (const char **) htab_find_slot (htab, (const void *) name,
10232 INSERT);
10233 /* NOTE: We can assume slot != NULL here because xcalloc never
10234 returns NULL. */
10235 if (*slot != NULL)
10236 {
10237 htab_delete (htab);
10238 return 1;
10239 }
10240 *slot = name;
10241 }
10242
10243 htab_delete (htab);
10244 return 0;
10245 }
10246
10247 /* When symbols change, it probably means the sources changed as well,
10248 and it might mean the static tracepoint markers are no longer at
10249 the same address or line numbers they used to be at last we
10250 checked. Losing your static tracepoints whenever you rebuild is
10251 undesirable. This function tries to resync/rematch gdb static
10252 tracepoints with the markers on the target, for static tracepoints
10253 that have not been set by marker id. Static tracepoint that have
10254 been set by marker id are reset by marker id in breakpoint_re_set.
10255 The heuristic is:
10256
10257 1) For a tracepoint set at a specific address, look for a marker at
10258 the old PC. If one is found there, assume to be the same marker.
10259 If the name / string id of the marker found is different from the
10260 previous known name, assume that means the user renamed the marker
10261 in the sources, and output a warning.
10262
10263 2) For a tracepoint set at a given line number, look for a marker
10264 at the new address of the old line number. If one is found there,
10265 assume to be the same marker. If the name / string id of the
10266 marker found is different from the previous known name, assume that
10267 means the user renamed the marker in the sources, and output a
10268 warning.
10269
10270 3) If a marker is no longer found at the same address or line, it
10271 may mean the marker no longer exists. But it may also just mean
10272 the code changed a bit. Maybe the user added a few lines of code
10273 that made the marker move up or down (in line number terms). Ask
10274 the target for info about the marker with the string id as we knew
10275 it. If found, update line number and address in the matching
10276 static tracepoint. This will get confused if there's more than one
10277 marker with the same ID (possible in UST, although unadvised
10278 precisely because it confuses tools). */
10279
10280 static struct symtab_and_line
10281 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
10282 {
10283 struct static_tracepoint_marker marker;
10284 CORE_ADDR pc;
10285 int i;
10286
10287 pc = sal.pc;
10288 if (sal.line)
10289 find_line_pc (sal.symtab, sal.line, &pc);
10290
10291 if (target_static_tracepoint_marker_at (pc, &marker))
10292 {
10293 if (strcmp (b->static_trace_marker_id, marker.str_id) != 0)
10294 warning (_("static tracepoint %d changed probed marker from %s to %s"),
10295 b->number,
10296 b->static_trace_marker_id, marker.str_id);
10297
10298 xfree (b->static_trace_marker_id);
10299 b->static_trace_marker_id = xstrdup (marker.str_id);
10300 release_static_tracepoint_marker (&marker);
10301
10302 return sal;
10303 }
10304
10305 /* Old marker wasn't found on target at lineno. Try looking it up
10306 by string ID. */
10307 if (!sal.explicit_pc
10308 && sal.line != 0
10309 && sal.symtab != NULL
10310 && b->static_trace_marker_id != NULL)
10311 {
10312 VEC(static_tracepoint_marker_p) *markers;
10313
10314 markers
10315 = target_static_tracepoint_markers_by_strid (b->static_trace_marker_id);
10316
10317 if (!VEC_empty(static_tracepoint_marker_p, markers))
10318 {
10319 struct symtab_and_line sal;
10320 struct symbol *sym;
10321 struct static_tracepoint_marker *marker;
10322
10323 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
10324
10325 xfree (b->static_trace_marker_id);
10326 b->static_trace_marker_id = xstrdup (marker->str_id);
10327
10328 warning (_("marker for static tracepoint %d (%s) not "
10329 "found at previous line number"),
10330 b->number, b->static_trace_marker_id);
10331
10332 init_sal (&sal);
10333
10334 sal.pc = marker->address;
10335
10336 sal = find_pc_line (marker->address, 0);
10337 sym = find_pc_sect_function (marker->address, NULL);
10338 ui_out_text (uiout, "Now in ");
10339 if (sym)
10340 {
10341 ui_out_field_string (uiout, "func",
10342 SYMBOL_PRINT_NAME (sym));
10343 ui_out_text (uiout, " at ");
10344 }
10345 ui_out_field_string (uiout, "file", sal.symtab->filename);
10346 ui_out_text (uiout, ":");
10347
10348 if (ui_out_is_mi_like_p (uiout))
10349 {
10350 char *fullname = symtab_to_fullname (sal.symtab);
10351
10352 if (fullname)
10353 ui_out_field_string (uiout, "fullname", fullname);
10354 }
10355
10356 ui_out_field_int (uiout, "line", sal.line);
10357 ui_out_text (uiout, "\n");
10358
10359 b->line_number = sal.line;
10360
10361 xfree (b->source_file);
10362 if (sym)
10363 b->source_file = xstrdup (sal.symtab->filename);
10364 else
10365 b->source_file = NULL;
10366
10367 xfree (b->addr_string);
10368 b->addr_string = xstrprintf ("%s:%d",
10369 sal.symtab->filename, b->line_number);
10370
10371 /* Might be nice to check if function changed, and warn if
10372 so. */
10373
10374 release_static_tracepoint_marker (marker);
10375 }
10376 }
10377 return sal;
10378 }
10379
10380 static void
10381 update_breakpoint_locations (struct breakpoint *b,
10382 struct symtabs_and_lines sals)
10383 {
10384 int i;
10385 char *s;
10386 struct bp_location *existing_locations = b->loc;
10387
10388 /* If there's no new locations, and all existing locations are
10389 pending, don't do anything. This optimizes the common case where
10390 all locations are in the same shared library, that was unloaded.
10391 We'd like to retain the location, so that when the library is
10392 loaded again, we don't loose the enabled/disabled status of the
10393 individual locations. */
10394 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
10395 return;
10396
10397 b->loc = NULL;
10398
10399 for (i = 0; i < sals.nelts; ++i)
10400 {
10401 struct bp_location *new_loc =
10402 add_location_to_breakpoint (b, &(sals.sals[i]));
10403
10404 /* Reparse conditions, they might contain references to the
10405 old symtab. */
10406 if (b->cond_string != NULL)
10407 {
10408 struct gdb_exception e;
10409
10410 s = b->cond_string;
10411 TRY_CATCH (e, RETURN_MASK_ERROR)
10412 {
10413 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
10414 0);
10415 }
10416 if (e.reason < 0)
10417 {
10418 warning (_("failed to reevaluate condition "
10419 "for breakpoint %d: %s"),
10420 b->number, e.message);
10421 new_loc->enabled = 0;
10422 }
10423 }
10424
10425 if (b->source_file != NULL)
10426 xfree (b->source_file);
10427 if (sals.sals[i].symtab == NULL)
10428 b->source_file = NULL;
10429 else
10430 b->source_file = xstrdup (sals.sals[i].symtab->filename);
10431
10432 if (b->line_number == 0)
10433 b->line_number = sals.sals[i].line;
10434 }
10435
10436 /* Update locations of permanent breakpoints. */
10437 if (b->enable_state == bp_permanent)
10438 make_breakpoint_permanent (b);
10439
10440 /* If possible, carry over 'disable' status from existing
10441 breakpoints. */
10442 {
10443 struct bp_location *e = existing_locations;
10444 /* If there are multiple breakpoints with the same function name,
10445 e.g. for inline functions, comparing function names won't work.
10446 Instead compare pc addresses; this is just a heuristic as things
10447 may have moved, but in practice it gives the correct answer
10448 often enough until a better solution is found. */
10449 int have_ambiguous_names = ambiguous_names_p (b->loc);
10450
10451 for (; e; e = e->next)
10452 {
10453 if (!e->enabled && e->function_name)
10454 {
10455 struct bp_location *l = b->loc;
10456 if (have_ambiguous_names)
10457 {
10458 for (; l; l = l->next)
10459 if (breakpoint_address_match (e->pspace->aspace, e->address,
10460 l->pspace->aspace, l->address))
10461 {
10462 l->enabled = 0;
10463 break;
10464 }
10465 }
10466 else
10467 {
10468 for (; l; l = l->next)
10469 if (l->function_name
10470 && strcmp (e->function_name, l->function_name) == 0)
10471 {
10472 l->enabled = 0;
10473 break;
10474 }
10475 }
10476 }
10477 }
10478 }
10479
10480 update_global_location_list (1);
10481 }
10482
10483 /* Reset a breakpoint given it's struct breakpoint * BINT.
10484 The value we return ends up being the return value from catch_errors.
10485 Unused in this case. */
10486
10487 static int
10488 breakpoint_re_set_one (void *bint)
10489 {
10490 /* Get past catch_errs. */
10491 struct breakpoint *b = (struct breakpoint *) bint;
10492 int not_found = 0;
10493 int *not_found_ptr = &not_found;
10494 struct symtabs_and_lines sals = {0};
10495 struct symtabs_and_lines expanded = {0};
10496 char *s;
10497 struct gdb_exception e;
10498 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
10499 int marker_spec = 0;
10500
10501 switch (b->type)
10502 {
10503 case bp_none:
10504 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
10505 b->number);
10506 return 0;
10507 case bp_breakpoint:
10508 case bp_hardware_breakpoint:
10509 case bp_tracepoint:
10510 case bp_fast_tracepoint:
10511 case bp_static_tracepoint:
10512 /* Do not attempt to re-set breakpoints disabled during startup. */
10513 if (b->enable_state == bp_startup_disabled)
10514 return 0;
10515
10516 if (b->addr_string == NULL)
10517 {
10518 /* Anything without a string can't be re-set. */
10519 delete_breakpoint (b);
10520 return 0;
10521 }
10522
10523 input_radix = b->input_radix;
10524 s = b->addr_string;
10525
10526 save_current_space_and_thread ();
10527 switch_to_program_space_and_thread (b->pspace);
10528
10529 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
10530
10531 set_language (b->language);
10532 TRY_CATCH (e, RETURN_MASK_ERROR)
10533 {
10534 if (marker_spec)
10535 {
10536 sals = decode_static_tracepoint_spec (&s);
10537 if (sals.nelts > b->static_trace_marker_id_idx)
10538 {
10539 sals.sals[0] = sals.sals[b->static_trace_marker_id_idx];
10540 sals.nelts = 1;
10541 }
10542 else
10543 error (_("marker %s not found"), b->static_trace_marker_id);
10544 }
10545 else
10546 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0,
10547 (char ***) NULL, not_found_ptr);
10548 }
10549 if (e.reason < 0)
10550 {
10551 int not_found_and_ok = 0;
10552 /* For pending breakpoints, it's expected that parsing will
10553 fail until the right shared library is loaded. User has
10554 already told to create pending breakpoints and don't need
10555 extra messages. If breakpoint is in bp_shlib_disabled
10556 state, then user already saw the message about that
10557 breakpoint being disabled, and don't want to see more
10558 errors. */
10559 if (not_found
10560 && (b->condition_not_parsed
10561 || (b->loc && b->loc->shlib_disabled)
10562 || b->enable_state == bp_disabled))
10563 not_found_and_ok = 1;
10564
10565 if (!not_found_and_ok)
10566 {
10567 /* We surely don't want to warn about the same breakpoint
10568 10 times. One solution, implemented here, is disable
10569 the breakpoint on error. Another solution would be to
10570 have separate 'warning emitted' flag. Since this
10571 happens only when a binary has changed, I don't know
10572 which approach is better. */
10573 b->enable_state = bp_disabled;
10574 throw_exception (e);
10575 }
10576 }
10577
10578 if (!not_found)
10579 {
10580 gdb_assert (sals.nelts == 1);
10581
10582 resolve_sal_pc (&sals.sals[0]);
10583 if (b->condition_not_parsed && s && s[0])
10584 {
10585 char *cond_string = 0;
10586 int thread = -1;
10587 int task = 0;
10588
10589 find_condition_and_thread (s, sals.sals[0].pc,
10590 &cond_string, &thread, &task);
10591 if (cond_string)
10592 b->cond_string = cond_string;
10593 b->thread = thread;
10594 b->task = task;
10595 b->condition_not_parsed = 0;
10596 }
10597
10598 if (b->type == bp_static_tracepoint && !marker_spec)
10599 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
10600
10601 expanded = expand_line_sal_maybe (sals.sals[0]);
10602 }
10603
10604 make_cleanup (xfree, sals.sals);
10605 update_breakpoint_locations (b, expanded);
10606 break;
10607
10608 case bp_watchpoint:
10609 case bp_hardware_watchpoint:
10610 case bp_read_watchpoint:
10611 case bp_access_watchpoint:
10612 /* Watchpoint can be either on expression using entirely global
10613 variables, or it can be on local variables.
10614
10615 Watchpoints of the first kind are never auto-deleted, and
10616 even persist across program restarts. Since they can use
10617 variables from shared libraries, we need to reparse
10618 expression as libraries are loaded and unloaded.
10619
10620 Watchpoints on local variables can also change meaning as
10621 result of solib event. For example, if a watchpoint uses
10622 both a local and a global variables in expression, it's a
10623 local watchpoint, but unloading of a shared library will make
10624 the expression invalid. This is not a very common use case,
10625 but we still re-evaluate expression, to avoid surprises to
10626 the user.
10627
10628 Note that for local watchpoints, we re-evaluate it only if
10629 watchpoints frame id is still valid. If it's not, it means
10630 the watchpoint is out of scope and will be deleted soon. In
10631 fact, I'm not sure we'll ever be called in this case.
10632
10633 If a local watchpoint's frame id is still valid, then
10634 b->exp_valid_block is likewise valid, and we can safely use it.
10635
10636 Don't do anything about disabled watchpoints, since they will
10637 be reevaluated again when enabled. */
10638 update_watchpoint (b, 1 /* reparse */);
10639 break;
10640 /* We needn't really do anything to reset these, since the mask
10641 that requests them is unaffected by e.g., new libraries being
10642 loaded. */
10643 case bp_catchpoint:
10644 break;
10645
10646 default:
10647 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
10648 /* fall through */
10649 /* Delete overlay event and longjmp master breakpoints; they will be
10650 reset later by breakpoint_re_set. */
10651 case bp_overlay_event:
10652 case bp_longjmp_master:
10653 case bp_std_terminate_master:
10654 case bp_exception_master:
10655 delete_breakpoint (b);
10656 break;
10657
10658 /* This breakpoint is special, it's set up when the inferior
10659 starts and we really don't want to touch it. */
10660 case bp_shlib_event:
10661
10662 /* Like bp_shlib_event, this breakpoint type is special.
10663 Once it is set up, we do not want to touch it. */
10664 case bp_thread_event:
10665
10666 /* Keep temporary breakpoints, which can be encountered when we
10667 step over a dlopen call and SOLIB_ADD is resetting the
10668 breakpoints. Otherwise these should have been blown away via
10669 the cleanup chain or by breakpoint_init_inferior when we
10670 rerun the executable. */
10671 case bp_until:
10672 case bp_finish:
10673 case bp_watchpoint_scope:
10674 case bp_call_dummy:
10675 case bp_std_terminate:
10676 case bp_step_resume:
10677 case bp_longjmp:
10678 case bp_longjmp_resume:
10679 case bp_exception:
10680 case bp_exception_resume:
10681 case bp_jit_event:
10682 break;
10683 }
10684
10685 do_cleanups (cleanups);
10686 return 0;
10687 }
10688
10689 /* Re-set all breakpoints after symbols have been re-loaded. */
10690 void
10691 breakpoint_re_set (void)
10692 {
10693 struct breakpoint *b, *b_tmp;
10694 enum language save_language;
10695 int save_input_radix;
10696 struct cleanup *old_chain;
10697
10698 save_language = current_language->la_language;
10699 save_input_radix = input_radix;
10700 old_chain = save_current_program_space ();
10701
10702 ALL_BREAKPOINTS_SAFE (b, b_tmp)
10703 {
10704 /* Format possible error msg. */
10705 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
10706 b->number);
10707 struct cleanup *cleanups = make_cleanup (xfree, message);
10708 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
10709 do_cleanups (cleanups);
10710 }
10711 set_language (save_language);
10712 input_radix = save_input_radix;
10713
10714 jit_breakpoint_re_set ();
10715
10716 do_cleanups (old_chain);
10717
10718 create_overlay_event_breakpoint ();
10719 create_longjmp_master_breakpoint ();
10720 create_std_terminate_master_breakpoint ();
10721 create_exception_master_breakpoint ();
10722 }
10723 \f
10724 /* Reset the thread number of this breakpoint:
10725
10726 - If the breakpoint is for all threads, leave it as-is.
10727 - Else, reset it to the current thread for inferior_ptid. */
10728 void
10729 breakpoint_re_set_thread (struct breakpoint *b)
10730 {
10731 if (b->thread != -1)
10732 {
10733 if (in_thread_list (inferior_ptid))
10734 b->thread = pid_to_thread_id (inferior_ptid);
10735
10736 /* We're being called after following a fork. The new fork is
10737 selected as current, and unless this was a vfork will have a
10738 different program space from the original thread. Reset that
10739 as well. */
10740 b->loc->pspace = current_program_space;
10741 }
10742 }
10743
10744 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
10745 If from_tty is nonzero, it prints a message to that effect,
10746 which ends with a period (no newline). */
10747
10748 void
10749 set_ignore_count (int bptnum, int count, int from_tty)
10750 {
10751 struct breakpoint *b;
10752
10753 if (count < 0)
10754 count = 0;
10755
10756 ALL_BREAKPOINTS (b)
10757 if (b->number == bptnum)
10758 {
10759 if (is_tracepoint (b))
10760 {
10761 if (from_tty && count != 0)
10762 printf_filtered (_("Ignore count ignored for tracepoint %d."),
10763 bptnum);
10764 return;
10765 }
10766
10767 b->ignore_count = count;
10768 if (from_tty)
10769 {
10770 if (count == 0)
10771 printf_filtered (_("Will stop next time "
10772 "breakpoint %d is reached."),
10773 bptnum);
10774 else if (count == 1)
10775 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
10776 bptnum);
10777 else
10778 printf_filtered (_("Will ignore next %d "
10779 "crossings of breakpoint %d."),
10780 count, bptnum);
10781 }
10782 breakpoints_changed ();
10783 observer_notify_breakpoint_modified (b->number);
10784 return;
10785 }
10786
10787 error (_("No breakpoint number %d."), bptnum);
10788 }
10789
10790 /* Command to set ignore-count of breakpoint N to COUNT. */
10791
10792 static void
10793 ignore_command (char *args, int from_tty)
10794 {
10795 char *p = args;
10796 int num;
10797
10798 if (p == 0)
10799 error_no_arg (_("a breakpoint number"));
10800
10801 num = get_number (&p);
10802 if (num == 0)
10803 error (_("bad breakpoint number: '%s'"), args);
10804 if (*p == 0)
10805 error (_("Second argument (specified ignore-count) is missing."));
10806
10807 set_ignore_count (num,
10808 longest_to_int (value_as_long (parse_and_eval (p))),
10809 from_tty);
10810 if (from_tty)
10811 printf_filtered ("\n");
10812 }
10813 \f
10814 /* Call FUNCTION on each of the breakpoints
10815 whose numbers are given in ARGS. */
10816
10817 static void
10818 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
10819 void *),
10820 void *data)
10821 {
10822 int num;
10823 struct breakpoint *b, *tmp;
10824 int match;
10825 struct get_number_or_range_state state;
10826
10827 if (args == 0)
10828 error_no_arg (_("one or more breakpoint numbers"));
10829
10830 init_number_or_range (&state, args);
10831
10832 while (!state.finished)
10833 {
10834 char *p = state.string;
10835
10836 match = 0;
10837
10838 num = get_number_or_range (&state);
10839 if (num == 0)
10840 {
10841 warning (_("bad breakpoint number at or near '%s'"), p);
10842 }
10843 else
10844 {
10845 ALL_BREAKPOINTS_SAFE (b, tmp)
10846 if (b->number == num)
10847 {
10848 struct breakpoint *related_breakpoint = b->related_breakpoint;
10849 match = 1;
10850 function (b, data);
10851 if (related_breakpoint)
10852 function (related_breakpoint, data);
10853 break;
10854 }
10855 if (match == 0)
10856 printf_unfiltered (_("No breakpoint number %d.\n"), num);
10857 }
10858 }
10859 }
10860
10861 static struct bp_location *
10862 find_location_by_number (char *number)
10863 {
10864 char *dot = strchr (number, '.');
10865 char *p1;
10866 int bp_num;
10867 int loc_num;
10868 struct breakpoint *b;
10869 struct bp_location *loc;
10870
10871 *dot = '\0';
10872
10873 p1 = number;
10874 bp_num = get_number (&p1);
10875 if (bp_num == 0)
10876 error (_("Bad breakpoint number '%s'"), number);
10877
10878 ALL_BREAKPOINTS (b)
10879 if (b->number == bp_num)
10880 {
10881 break;
10882 }
10883
10884 if (!b || b->number != bp_num)
10885 error (_("Bad breakpoint number '%s'"), number);
10886
10887 p1 = dot+1;
10888 loc_num = get_number (&p1);
10889 if (loc_num == 0)
10890 error (_("Bad breakpoint location number '%s'"), number);
10891
10892 --loc_num;
10893 loc = b->loc;
10894 for (;loc_num && loc; --loc_num, loc = loc->next)
10895 ;
10896 if (!loc)
10897 error (_("Bad breakpoint location number '%s'"), dot+1);
10898
10899 return loc;
10900 }
10901
10902
10903 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
10904 If from_tty is nonzero, it prints a message to that effect,
10905 which ends with a period (no newline). */
10906
10907 void
10908 disable_breakpoint (struct breakpoint *bpt)
10909 {
10910 /* Never disable a watchpoint scope breakpoint; we want to
10911 hit them when we leave scope so we can delete both the
10912 watchpoint and its scope breakpoint at that time. */
10913 if (bpt->type == bp_watchpoint_scope)
10914 return;
10915
10916 /* You can't disable permanent breakpoints. */
10917 if (bpt->enable_state == bp_permanent)
10918 return;
10919
10920 bpt->enable_state = bp_disabled;
10921
10922 update_global_location_list (0);
10923
10924 observer_notify_breakpoint_modified (bpt->number);
10925 }
10926
10927 /* A callback for map_breakpoint_numbers that calls
10928 disable_breakpoint. */
10929
10930 static void
10931 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
10932 {
10933 disable_breakpoint (b);
10934 }
10935
10936 static void
10937 disable_command (char *args, int from_tty)
10938 {
10939 struct breakpoint *bpt;
10940
10941 if (args == 0)
10942 ALL_BREAKPOINTS (bpt)
10943 switch (bpt->type)
10944 {
10945 case bp_none:
10946 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
10947 bpt->number);
10948 break;
10949 case bp_breakpoint:
10950 case bp_tracepoint:
10951 case bp_fast_tracepoint:
10952 case bp_static_tracepoint:
10953 case bp_catchpoint:
10954 case bp_hardware_breakpoint:
10955 case bp_watchpoint:
10956 case bp_hardware_watchpoint:
10957 case bp_read_watchpoint:
10958 case bp_access_watchpoint:
10959 disable_breakpoint (bpt);
10960 break;
10961 default:
10962 break;
10963 }
10964 else if (strchr (args, '.'))
10965 {
10966 struct bp_location *loc = find_location_by_number (args);
10967 if (loc)
10968 loc->enabled = 0;
10969 update_global_location_list (0);
10970 }
10971 else
10972 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
10973 }
10974
10975 static void
10976 do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
10977 {
10978 int target_resources_ok;
10979
10980 if (bpt->type == bp_hardware_breakpoint)
10981 {
10982 int i;
10983 i = hw_breakpoint_used_count ();
10984 target_resources_ok =
10985 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10986 i + 1, 0);
10987 if (target_resources_ok == 0)
10988 error (_("No hardware breakpoint support in the target."));
10989 else if (target_resources_ok < 0)
10990 error (_("Hardware breakpoints used exceeds limit."));
10991 }
10992
10993 if (is_watchpoint (bpt))
10994 {
10995 struct gdb_exception e;
10996
10997 TRY_CATCH (e, RETURN_MASK_ALL)
10998 {
10999 update_watchpoint (bpt, 1 /* reparse */);
11000 }
11001 if (e.reason < 0)
11002 {
11003 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
11004 bpt->number);
11005 return;
11006 }
11007 }
11008
11009 if (bpt->enable_state != bp_permanent)
11010 bpt->enable_state = bp_enabled;
11011 bpt->disposition = disposition;
11012 update_global_location_list (1);
11013 breakpoints_changed ();
11014
11015 observer_notify_breakpoint_modified (bpt->number);
11016 }
11017
11018
11019 void
11020 enable_breakpoint (struct breakpoint *bpt)
11021 {
11022 do_enable_breakpoint (bpt, bpt->disposition);
11023 }
11024
11025 /* A callback for map_breakpoint_numbers that calls
11026 enable_breakpoint. */
11027
11028 static void
11029 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
11030 {
11031 enable_breakpoint (b);
11032 }
11033
11034 /* The enable command enables the specified breakpoints (or all defined
11035 breakpoints) so they once again become (or continue to be) effective
11036 in stopping the inferior. */
11037
11038 static void
11039 enable_command (char *args, int from_tty)
11040 {
11041 struct breakpoint *bpt;
11042
11043 if (args == 0)
11044 ALL_BREAKPOINTS (bpt)
11045 switch (bpt->type)
11046 {
11047 case bp_none:
11048 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
11049 bpt->number);
11050 break;
11051 case bp_breakpoint:
11052 case bp_tracepoint:
11053 case bp_fast_tracepoint:
11054 case bp_static_tracepoint:
11055 case bp_catchpoint:
11056 case bp_hardware_breakpoint:
11057 case bp_watchpoint:
11058 case bp_hardware_watchpoint:
11059 case bp_read_watchpoint:
11060 case bp_access_watchpoint:
11061 enable_breakpoint (bpt);
11062 break;
11063 default:
11064 break;
11065 }
11066 else if (strchr (args, '.'))
11067 {
11068 struct bp_location *loc = find_location_by_number (args);
11069 if (loc)
11070 loc->enabled = 1;
11071 update_global_location_list (1);
11072 }
11073 else
11074 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
11075 }
11076
11077 static void
11078 enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
11079 {
11080 do_enable_breakpoint (bpt, disp_disable);
11081 }
11082
11083 static void
11084 enable_once_command (char *args, int from_tty)
11085 {
11086 map_breakpoint_numbers (args, enable_once_breakpoint, NULL);
11087 }
11088
11089 static void
11090 enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
11091 {
11092 do_enable_breakpoint (bpt, disp_del);
11093 }
11094
11095 static void
11096 enable_delete_command (char *args, int from_tty)
11097 {
11098 map_breakpoint_numbers (args, enable_delete_breakpoint, NULL);
11099 }
11100 \f
11101 static void
11102 set_breakpoint_cmd (char *args, int from_tty)
11103 {
11104 }
11105
11106 static void
11107 show_breakpoint_cmd (char *args, int from_tty)
11108 {
11109 }
11110
11111 /* Invalidate last known value of any hardware watchpoint if
11112 the memory which that value represents has been written to by
11113 GDB itself. */
11114
11115 static void
11116 invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
11117 const bfd_byte *data)
11118 {
11119 struct breakpoint *bp;
11120
11121 ALL_BREAKPOINTS (bp)
11122 if (bp->enable_state == bp_enabled
11123 && bp->type == bp_hardware_watchpoint
11124 && bp->val_valid && bp->val)
11125 {
11126 struct bp_location *loc;
11127
11128 for (loc = bp->loc; loc != NULL; loc = loc->next)
11129 if (loc->loc_type == bp_loc_hardware_watchpoint
11130 && loc->address + loc->length > addr
11131 && addr + len > loc->address)
11132 {
11133 value_free (bp->val);
11134 bp->val = NULL;
11135 bp->val_valid = 0;
11136 }
11137 }
11138 }
11139
11140 /* Use default_breakpoint_'s, or nothing if they aren't valid. */
11141
11142 struct symtabs_and_lines
11143 decode_line_spec_1 (char *string, int funfirstline)
11144 {
11145 struct symtabs_and_lines sals;
11146
11147 if (string == 0)
11148 error (_("Empty line specification."));
11149 if (default_breakpoint_valid)
11150 sals = decode_line_1 (&string, funfirstline,
11151 default_breakpoint_symtab,
11152 default_breakpoint_line,
11153 (char ***) NULL, NULL);
11154 else
11155 sals = decode_line_1 (&string, funfirstline,
11156 (struct symtab *) NULL, 0, (char ***) NULL, NULL);
11157 if (*string)
11158 error (_("Junk at end of line specification: %s"), string);
11159 return sals;
11160 }
11161
11162 /* Create and insert a raw software breakpoint at PC. Return an
11163 identifier, which should be used to remove the breakpoint later.
11164 In general, places which call this should be using something on the
11165 breakpoint chain instead; this function should be eliminated
11166 someday. */
11167
11168 void *
11169 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
11170 struct address_space *aspace, CORE_ADDR pc)
11171 {
11172 struct bp_target_info *bp_tgt;
11173
11174 bp_tgt = XZALLOC (struct bp_target_info);
11175
11176 bp_tgt->placed_address_space = aspace;
11177 bp_tgt->placed_address = pc;
11178
11179 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
11180 {
11181 /* Could not insert the breakpoint. */
11182 xfree (bp_tgt);
11183 return NULL;
11184 }
11185
11186 return bp_tgt;
11187 }
11188
11189 /* Remove a breakpoint BP inserted by
11190 deprecated_insert_raw_breakpoint. */
11191
11192 int
11193 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
11194 {
11195 struct bp_target_info *bp_tgt = bp;
11196 int ret;
11197
11198 ret = target_remove_breakpoint (gdbarch, bp_tgt);
11199 xfree (bp_tgt);
11200
11201 return ret;
11202 }
11203
11204 /* One (or perhaps two) breakpoints used for software single
11205 stepping. */
11206
11207 static void *single_step_breakpoints[2];
11208 static struct gdbarch *single_step_gdbarch[2];
11209
11210 /* Create and insert a breakpoint for software single step. */
11211
11212 void
11213 insert_single_step_breakpoint (struct gdbarch *gdbarch,
11214 struct address_space *aspace,
11215 CORE_ADDR next_pc)
11216 {
11217 void **bpt_p;
11218
11219 if (single_step_breakpoints[0] == NULL)
11220 {
11221 bpt_p = &single_step_breakpoints[0];
11222 single_step_gdbarch[0] = gdbarch;
11223 }
11224 else
11225 {
11226 gdb_assert (single_step_breakpoints[1] == NULL);
11227 bpt_p = &single_step_breakpoints[1];
11228 single_step_gdbarch[1] = gdbarch;
11229 }
11230
11231 /* NOTE drow/2006-04-11: A future improvement to this function would
11232 be to only create the breakpoints once, and actually put them on
11233 the breakpoint chain. That would let us use set_raw_breakpoint.
11234 We could adjust the addresses each time they were needed. Doing
11235 this requires corresponding changes elsewhere where single step
11236 breakpoints are handled, however. So, for now, we use this. */
11237
11238 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
11239 if (*bpt_p == NULL)
11240 error (_("Could not insert single-step breakpoint at %s"),
11241 paddress (gdbarch, next_pc));
11242 }
11243
11244 /* Check if the breakpoints used for software single stepping
11245 were inserted or not. */
11246
11247 int
11248 single_step_breakpoints_inserted (void)
11249 {
11250 return (single_step_breakpoints[0] != NULL
11251 || single_step_breakpoints[1] != NULL);
11252 }
11253
11254 /* Remove and delete any breakpoints used for software single step. */
11255
11256 void
11257 remove_single_step_breakpoints (void)
11258 {
11259 gdb_assert (single_step_breakpoints[0] != NULL);
11260
11261 /* See insert_single_step_breakpoint for more about this deprecated
11262 call. */
11263 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
11264 single_step_breakpoints[0]);
11265 single_step_gdbarch[0] = NULL;
11266 single_step_breakpoints[0] = NULL;
11267
11268 if (single_step_breakpoints[1] != NULL)
11269 {
11270 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
11271 single_step_breakpoints[1]);
11272 single_step_gdbarch[1] = NULL;
11273 single_step_breakpoints[1] = NULL;
11274 }
11275 }
11276
11277 /* Delete software single step breakpoints without removing them from
11278 the inferior. This is intended to be used if the inferior's address
11279 space where they were inserted is already gone, e.g. after exit or
11280 exec. */
11281
11282 void
11283 cancel_single_step_breakpoints (void)
11284 {
11285 int i;
11286
11287 for (i = 0; i < 2; i++)
11288 if (single_step_breakpoints[i])
11289 {
11290 xfree (single_step_breakpoints[i]);
11291 single_step_breakpoints[i] = NULL;
11292 single_step_gdbarch[i] = NULL;
11293 }
11294 }
11295
11296 /* Detach software single-step breakpoints from INFERIOR_PTID without
11297 removing them. */
11298
11299 static void
11300 detach_single_step_breakpoints (void)
11301 {
11302 int i;
11303
11304 for (i = 0; i < 2; i++)
11305 if (single_step_breakpoints[i])
11306 target_remove_breakpoint (single_step_gdbarch[i],
11307 single_step_breakpoints[i]);
11308 }
11309
11310 /* Check whether a software single-step breakpoint is inserted at
11311 PC. */
11312
11313 static int
11314 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
11315 CORE_ADDR pc)
11316 {
11317 int i;
11318
11319 for (i = 0; i < 2; i++)
11320 {
11321 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
11322 if (bp_tgt
11323 && breakpoint_address_match (bp_tgt->placed_address_space,
11324 bp_tgt->placed_address,
11325 aspace, pc))
11326 return 1;
11327 }
11328
11329 return 0;
11330 }
11331
11332 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
11333 non-zero otherwise. */
11334 static int
11335 is_syscall_catchpoint_enabled (struct breakpoint *bp)
11336 {
11337 if (syscall_catchpoint_p (bp)
11338 && bp->enable_state != bp_disabled
11339 && bp->enable_state != bp_call_disabled)
11340 return 1;
11341 else
11342 return 0;
11343 }
11344
11345 int
11346 catch_syscall_enabled (void)
11347 {
11348 struct inferior *inf = current_inferior ();
11349
11350 return inf->total_syscalls_count != 0;
11351 }
11352
11353 int
11354 catching_syscall_number (int syscall_number)
11355 {
11356 struct breakpoint *bp;
11357
11358 ALL_BREAKPOINTS (bp)
11359 if (is_syscall_catchpoint_enabled (bp))
11360 {
11361 if (bp->syscalls_to_be_caught)
11362 {
11363 int i, iter;
11364 for (i = 0;
11365 VEC_iterate (int, bp->syscalls_to_be_caught, i, iter);
11366 i++)
11367 if (syscall_number == iter)
11368 return 1;
11369 }
11370 else
11371 return 1;
11372 }
11373
11374 return 0;
11375 }
11376
11377 /* Complete syscall names. Used by "catch syscall". */
11378 static char **
11379 catch_syscall_completer (struct cmd_list_element *cmd,
11380 char *text, char *word)
11381 {
11382 const char **list = get_syscall_names ();
11383 char **retlist
11384 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
11385
11386 xfree (list);
11387 return retlist;
11388 }
11389
11390 /* Tracepoint-specific operations. */
11391
11392 /* Set tracepoint count to NUM. */
11393 static void
11394 set_tracepoint_count (int num)
11395 {
11396 tracepoint_count = num;
11397 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
11398 }
11399
11400 void
11401 trace_command (char *arg, int from_tty)
11402 {
11403 if (create_breakpoint (get_current_arch (),
11404 arg,
11405 NULL, 0, 1 /* parse arg */,
11406 0 /* tempflag */,
11407 bp_tracepoint /* type_wanted */,
11408 0 /* Ignore count */,
11409 pending_break_support,
11410 NULL,
11411 from_tty,
11412 1 /* enabled */,
11413 0 /* internal */))
11414 set_tracepoint_count (breakpoint_count);
11415 }
11416
11417 void
11418 ftrace_command (char *arg, int from_tty)
11419 {
11420 if (create_breakpoint (get_current_arch (),
11421 arg,
11422 NULL, 0, 1 /* parse arg */,
11423 0 /* tempflag */,
11424 bp_fast_tracepoint /* type_wanted */,
11425 0 /* Ignore count */,
11426 pending_break_support,
11427 NULL,
11428 from_tty,
11429 1 /* enabled */,
11430 0 /* internal */))
11431 set_tracepoint_count (breakpoint_count);
11432 }
11433
11434 /* strace command implementation. Creates a static tracepoint. */
11435
11436 void
11437 strace_command (char *arg, int from_tty)
11438 {
11439 if (create_breakpoint (get_current_arch (),
11440 arg,
11441 NULL, 0, 1 /* parse arg */,
11442 0 /* tempflag */,
11443 bp_static_tracepoint /* type_wanted */,
11444 0 /* Ignore count */,
11445 pending_break_support,
11446 NULL,
11447 from_tty,
11448 1 /* enabled */,
11449 0 /* internal */))
11450 set_tracepoint_count (breakpoint_count);
11451 }
11452
11453 /* Set up a fake reader function that gets command lines from a linked
11454 list that was acquired during tracepoint uploading. */
11455
11456 static struct uploaded_tp *this_utp;
11457 static int next_cmd;
11458
11459 static char *
11460 read_uploaded_action (void)
11461 {
11462 char *rslt;
11463
11464 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
11465
11466 next_cmd++;
11467
11468 return rslt;
11469 }
11470
11471 /* Given information about a tracepoint as recorded on a target (which
11472 can be either a live system or a trace file), attempt to create an
11473 equivalent GDB tracepoint. This is not a reliable process, since
11474 the target does not necessarily have all the information used when
11475 the tracepoint was originally defined. */
11476
11477 struct breakpoint *
11478 create_tracepoint_from_upload (struct uploaded_tp *utp)
11479 {
11480 char *addr_str, small_buf[100];
11481 struct breakpoint *tp;
11482
11483 if (utp->at_string)
11484 addr_str = utp->at_string;
11485 else
11486 {
11487 /* In the absence of a source location, fall back to raw
11488 address. Since there is no way to confirm that the address
11489 means the same thing as when the trace was started, warn the
11490 user. */
11491 warning (_("Uploaded tracepoint %d has no "
11492 "source location, using raw address"),
11493 utp->number);
11494 sprintf (small_buf, "*%s", hex_string (utp->addr));
11495 addr_str = small_buf;
11496 }
11497
11498 /* There's not much we can do with a sequence of bytecodes. */
11499 if (utp->cond && !utp->cond_string)
11500 warning (_("Uploaded tracepoint %d condition "
11501 "has no source form, ignoring it"),
11502 utp->number);
11503
11504 if (!create_breakpoint (get_current_arch (),
11505 addr_str,
11506 utp->cond_string, -1, 0 /* parse cond/thread */,
11507 0 /* tempflag */,
11508 utp->type /* type_wanted */,
11509 0 /* Ignore count */,
11510 pending_break_support,
11511 NULL,
11512 0 /* from_tty */,
11513 utp->enabled /* enabled */,
11514 0 /* internal */))
11515 return NULL;
11516
11517 set_tracepoint_count (breakpoint_count);
11518
11519 /* Get the tracepoint we just created. */
11520 tp = get_tracepoint (tracepoint_count);
11521 gdb_assert (tp != NULL);
11522
11523 if (utp->pass > 0)
11524 {
11525 sprintf (small_buf, "%d %d", utp->pass, tp->number);
11526
11527 trace_pass_command (small_buf, 0);
11528 }
11529
11530 /* If we have uploaded versions of the original commands, set up a
11531 special-purpose "reader" function and call the usual command line
11532 reader, then pass the result to the breakpoint command-setting
11533 function. */
11534 if (!VEC_empty (char_ptr, utp->cmd_strings))
11535 {
11536 struct command_line *cmd_list;
11537
11538 this_utp = utp;
11539 next_cmd = 0;
11540
11541 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
11542
11543 breakpoint_set_commands (tp, cmd_list);
11544 }
11545 else if (!VEC_empty (char_ptr, utp->actions)
11546 || !VEC_empty (char_ptr, utp->step_actions))
11547 warning (_("Uploaded tracepoint %d actions "
11548 "have no source form, ignoring them"),
11549 utp->number);
11550
11551 return tp;
11552 }
11553
11554 /* Print information on tracepoint number TPNUM_EXP, or all if
11555 omitted. */
11556
11557 static void
11558 tracepoints_info (char *args, int from_tty)
11559 {
11560 int num_printed;
11561
11562 num_printed = breakpoint_1 (args, 0, is_tracepoint);
11563
11564 if (num_printed == 0)
11565 {
11566 if (args == NULL || *args == '\0')
11567 ui_out_message (uiout, 0, "No tracepoints.\n");
11568 else
11569 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
11570 }
11571
11572 default_collect_info ();
11573 }
11574
11575 /* The 'enable trace' command enables tracepoints.
11576 Not supported by all targets. */
11577 static void
11578 enable_trace_command (char *args, int from_tty)
11579 {
11580 enable_command (args, from_tty);
11581 }
11582
11583 /* The 'disable trace' command disables tracepoints.
11584 Not supported by all targets. */
11585 static void
11586 disable_trace_command (char *args, int from_tty)
11587 {
11588 disable_command (args, from_tty);
11589 }
11590
11591 /* Remove a tracepoint (or all if no argument). */
11592 static void
11593 delete_trace_command (char *arg, int from_tty)
11594 {
11595 struct breakpoint *b, *b_tmp;
11596
11597 dont_repeat ();
11598
11599 if (arg == 0)
11600 {
11601 int breaks_to_delete = 0;
11602
11603 /* Delete all breakpoints if no argument.
11604 Do not delete internal or call-dummy breakpoints, these
11605 have to be deleted with an explicit breakpoint number
11606 argument. */
11607 ALL_TRACEPOINTS (b)
11608 {
11609 if (b->number >= 0)
11610 {
11611 breaks_to_delete = 1;
11612 break;
11613 }
11614 }
11615
11616 /* Ask user only if there are some breakpoints to delete. */
11617 if (!from_tty
11618 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
11619 {
11620 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11621 {
11622 if (is_tracepoint (b)
11623 && b->number >= 0)
11624 delete_breakpoint (b);
11625 }
11626 }
11627 }
11628 else
11629 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
11630 }
11631
11632 /* Helper function for trace_pass_command. */
11633
11634 static void
11635 trace_pass_set_count (struct breakpoint *bp, int count, int from_tty)
11636 {
11637 bp->pass_count = count;
11638 observer_notify_tracepoint_modified (bp->number);
11639 if (from_tty)
11640 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
11641 bp->number, count);
11642 }
11643
11644 /* Set passcount for tracepoint.
11645
11646 First command argument is passcount, second is tracepoint number.
11647 If tracepoint number omitted, apply to most recently defined.
11648 Also accepts special argument "all". */
11649
11650 static void
11651 trace_pass_command (char *args, int from_tty)
11652 {
11653 struct breakpoint *t1;
11654 unsigned int count;
11655
11656 if (args == 0 || *args == 0)
11657 error (_("passcount command requires an "
11658 "argument (count + optional TP num)"));
11659
11660 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
11661
11662 while (*args && isspace ((int) *args))
11663 args++;
11664
11665 if (*args && strncasecmp (args, "all", 3) == 0)
11666 {
11667 args += 3; /* Skip special argument "all". */
11668 if (*args)
11669 error (_("Junk at end of arguments."));
11670
11671 ALL_TRACEPOINTS (t1)
11672 {
11673 trace_pass_set_count (t1, count, from_tty);
11674 }
11675 }
11676 else if (*args == '\0')
11677 {
11678 t1 = get_tracepoint_by_number (&args, NULL, 1);
11679 if (t1)
11680 trace_pass_set_count (t1, count, from_tty);
11681 }
11682 else
11683 {
11684 struct get_number_or_range_state state;
11685
11686 init_number_or_range (&state, args);
11687 while (!state.finished)
11688 {
11689 t1 = get_tracepoint_by_number (&args, &state, 1);
11690 if (t1)
11691 trace_pass_set_count (t1, count, from_tty);
11692 }
11693 }
11694 }
11695
11696 struct breakpoint *
11697 get_tracepoint (int num)
11698 {
11699 struct breakpoint *t;
11700
11701 ALL_TRACEPOINTS (t)
11702 if (t->number == num)
11703 return t;
11704
11705 return NULL;
11706 }
11707
11708 /* Find the tracepoint with the given target-side number (which may be
11709 different from the tracepoint number after disconnecting and
11710 reconnecting). */
11711
11712 struct breakpoint *
11713 get_tracepoint_by_number_on_target (int num)
11714 {
11715 struct breakpoint *t;
11716
11717 ALL_TRACEPOINTS (t)
11718 if (t->number_on_target == num)
11719 return t;
11720
11721 return NULL;
11722 }
11723
11724 /* Utility: parse a tracepoint number and look it up in the list.
11725 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
11726 If OPTIONAL_P is true, then if the argument is missing, the most
11727 recent tracepoint (tracepoint_count) is returned. */
11728 struct breakpoint *
11729 get_tracepoint_by_number (char **arg,
11730 struct get_number_or_range_state *state,
11731 int optional_p)
11732 {
11733 extern int tracepoint_count;
11734 struct breakpoint *t;
11735 int tpnum;
11736 char *instring = arg == NULL ? NULL : *arg;
11737
11738 if (state)
11739 {
11740 gdb_assert (!state->finished);
11741 tpnum = get_number_or_range (state);
11742 }
11743 else if (arg == NULL || *arg == NULL || ! **arg)
11744 {
11745 if (optional_p)
11746 tpnum = tracepoint_count;
11747 else
11748 error_no_arg (_("tracepoint number"));
11749 }
11750 else
11751 tpnum = get_number (arg);
11752
11753 if (tpnum <= 0)
11754 {
11755 if (instring && *instring)
11756 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
11757 instring);
11758 else
11759 printf_filtered (_("Tracepoint argument missing "
11760 "and no previous tracepoint\n"));
11761 return NULL;
11762 }
11763
11764 ALL_TRACEPOINTS (t)
11765 if (t->number == tpnum)
11766 {
11767 return t;
11768 }
11769
11770 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
11771 return NULL;
11772 }
11773
11774 /* Save information on user settable breakpoints (watchpoints, etc) to
11775 a new script file named FILENAME. If FILTER is non-NULL, call it
11776 on each breakpoint and only include the ones for which it returns
11777 non-zero. */
11778
11779 static void
11780 save_breakpoints (char *filename, int from_tty,
11781 int (*filter) (const struct breakpoint *))
11782 {
11783 struct breakpoint *tp;
11784 int any = 0;
11785 char *pathname;
11786 struct cleanup *cleanup;
11787 struct ui_file *fp;
11788 int extra_trace_bits = 0;
11789
11790 if (filename == 0 || *filename == 0)
11791 error (_("Argument required (file name in which to save)"));
11792
11793 /* See if we have anything to save. */
11794 ALL_BREAKPOINTS (tp)
11795 {
11796 /* Skip internal and momentary breakpoints. */
11797 if (!user_breakpoint_p (tp))
11798 continue;
11799
11800 /* If we have a filter, only save the breakpoints it accepts. */
11801 if (filter && !filter (tp))
11802 continue;
11803
11804 any = 1;
11805
11806 if (is_tracepoint (tp))
11807 {
11808 extra_trace_bits = 1;
11809
11810 /* We can stop searching. */
11811 break;
11812 }
11813 }
11814
11815 if (!any)
11816 {
11817 warning (_("Nothing to save."));
11818 return;
11819 }
11820
11821 pathname = tilde_expand (filename);
11822 cleanup = make_cleanup (xfree, pathname);
11823 fp = gdb_fopen (pathname, "w");
11824 if (!fp)
11825 error (_("Unable to open file '%s' for saving (%s)"),
11826 filename, safe_strerror (errno));
11827 make_cleanup_ui_file_delete (fp);
11828
11829 if (extra_trace_bits)
11830 save_trace_state_variables (fp);
11831
11832 ALL_BREAKPOINTS (tp)
11833 {
11834 /* Skip internal and momentary breakpoints. */
11835 if (!user_breakpoint_p (tp))
11836 continue;
11837
11838 /* If we have a filter, only save the breakpoints it accepts. */
11839 if (filter && !filter (tp))
11840 continue;
11841
11842 if (tp->ops != NULL)
11843 (tp->ops->print_recreate) (tp, fp);
11844 else
11845 {
11846 if (tp->type == bp_fast_tracepoint)
11847 fprintf_unfiltered (fp, "ftrace");
11848 if (tp->type == bp_static_tracepoint)
11849 fprintf_unfiltered (fp, "strace");
11850 else if (tp->type == bp_tracepoint)
11851 fprintf_unfiltered (fp, "trace");
11852 else if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11853 fprintf_unfiltered (fp, "tbreak");
11854 else if (tp->type == bp_breakpoint)
11855 fprintf_unfiltered (fp, "break");
11856 else if (tp->type == bp_hardware_breakpoint
11857 && tp->disposition == disp_del)
11858 fprintf_unfiltered (fp, "thbreak");
11859 else if (tp->type == bp_hardware_breakpoint)
11860 fprintf_unfiltered (fp, "hbreak");
11861 else if (tp->type == bp_watchpoint)
11862 fprintf_unfiltered (fp, "watch");
11863 else if (tp->type == bp_hardware_watchpoint)
11864 fprintf_unfiltered (fp, "watch");
11865 else if (tp->type == bp_read_watchpoint)
11866 fprintf_unfiltered (fp, "rwatch");
11867 else if (tp->type == bp_access_watchpoint)
11868 fprintf_unfiltered (fp, "awatch");
11869 else
11870 internal_error (__FILE__, __LINE__,
11871 _("unhandled breakpoint type %d"), (int) tp->type);
11872
11873 if (tp->exp_string)
11874 fprintf_unfiltered (fp, " %s", tp->exp_string);
11875 else if (tp->addr_string)
11876 fprintf_unfiltered (fp, " %s", tp->addr_string);
11877 else
11878 {
11879 char tmp[40];
11880
11881 sprintf_vma (tmp, tp->loc->address);
11882 fprintf_unfiltered (fp, " *0x%s", tmp);
11883 }
11884 }
11885
11886 if (tp->thread != -1)
11887 fprintf_unfiltered (fp, " thread %d", tp->thread);
11888
11889 if (tp->task != 0)
11890 fprintf_unfiltered (fp, " task %d", tp->task);
11891
11892 fprintf_unfiltered (fp, "\n");
11893
11894 /* Note, we can't rely on tp->number for anything, as we can't
11895 assume the recreated breakpoint numbers will match. Use $bpnum
11896 instead. */
11897
11898 if (tp->cond_string)
11899 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
11900
11901 if (tp->ignore_count)
11902 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
11903
11904 if (tp->pass_count)
11905 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
11906
11907 if (tp->commands)
11908 {
11909 volatile struct gdb_exception ex;
11910
11911 fprintf_unfiltered (fp, " commands\n");
11912
11913 ui_out_redirect (uiout, fp);
11914 TRY_CATCH (ex, RETURN_MASK_ALL)
11915 {
11916 print_command_lines (uiout, tp->commands->commands, 2);
11917 }
11918 ui_out_redirect (uiout, NULL);
11919
11920 if (ex.reason < 0)
11921 throw_exception (ex);
11922
11923 fprintf_unfiltered (fp, " end\n");
11924 }
11925
11926 if (tp->enable_state == bp_disabled)
11927 fprintf_unfiltered (fp, "disable\n");
11928
11929 /* If this is a multi-location breakpoint, check if the locations
11930 should be individually disabled. Watchpoint locations are
11931 special, and not user visible. */
11932 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
11933 {
11934 struct bp_location *loc;
11935 int n = 1;
11936
11937 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
11938 if (!loc->enabled)
11939 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
11940 }
11941 }
11942
11943 if (extra_trace_bits && *default_collect)
11944 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
11945
11946 do_cleanups (cleanup);
11947 if (from_tty)
11948 printf_filtered (_("Saved to file '%s'.\n"), filename);
11949 }
11950
11951 /* The `save breakpoints' command. */
11952
11953 static void
11954 save_breakpoints_command (char *args, int from_tty)
11955 {
11956 save_breakpoints (args, from_tty, NULL);
11957 }
11958
11959 /* The `save tracepoints' command. */
11960
11961 static void
11962 save_tracepoints_command (char *args, int from_tty)
11963 {
11964 save_breakpoints (args, from_tty, is_tracepoint);
11965 }
11966
11967 /* Create a vector of all tracepoints. */
11968
11969 VEC(breakpoint_p) *
11970 all_tracepoints (void)
11971 {
11972 VEC(breakpoint_p) *tp_vec = 0;
11973 struct breakpoint *tp;
11974
11975 ALL_TRACEPOINTS (tp)
11976 {
11977 VEC_safe_push (breakpoint_p, tp_vec, tp);
11978 }
11979
11980 return tp_vec;
11981 }
11982
11983 \f
11984 /* This help string is used for the break, hbreak, tbreak and thbreak
11985 commands. It is defined as a macro to prevent duplication.
11986 COMMAND should be a string constant containing the name of the
11987 command. */
11988 #define BREAK_ARGS_HELP(command) \
11989 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
11990 LOCATION may be a line number, function name, or \"*\" and an address.\n\
11991 If a line number is specified, break at start of code for that line.\n\
11992 If a function is specified, break at start of code for that function.\n\
11993 If an address is specified, break at that exact address.\n\
11994 With no LOCATION, uses current execution address of the selected\n\
11995 stack frame. This is useful for breaking on return to a stack frame.\n\
11996 \n\
11997 THREADNUM is the number from \"info threads\".\n\
11998 CONDITION is a boolean expression.\n\
11999 \n\
12000 Multiple breakpoints at one place are permitted, and useful if their\n\
12001 conditions are different.\n\
12002 \n\
12003 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
12004
12005 /* List of subcommands for "catch". */
12006 static struct cmd_list_element *catch_cmdlist;
12007
12008 /* List of subcommands for "tcatch". */
12009 static struct cmd_list_element *tcatch_cmdlist;
12010
12011 /* Like add_cmd, but add the command to both the "catch" and "tcatch"
12012 lists, and pass some additional user data to the command function. */
12013 static void
12014 add_catch_command (char *name, char *docstring,
12015 void (*sfunc) (char *args, int from_tty,
12016 struct cmd_list_element *command),
12017 char **(*completer) (struct cmd_list_element *cmd,
12018 char *text, char *word),
12019 void *user_data_catch,
12020 void *user_data_tcatch)
12021 {
12022 struct cmd_list_element *command;
12023
12024 command = add_cmd (name, class_breakpoint, NULL, docstring,
12025 &catch_cmdlist);
12026 set_cmd_sfunc (command, sfunc);
12027 set_cmd_context (command, user_data_catch);
12028 set_cmd_completer (command, completer);
12029
12030 command = add_cmd (name, class_breakpoint, NULL, docstring,
12031 &tcatch_cmdlist);
12032 set_cmd_sfunc (command, sfunc);
12033 set_cmd_context (command, user_data_tcatch);
12034 set_cmd_completer (command, completer);
12035 }
12036
12037 static void
12038 clear_syscall_counts (struct inferior *inf)
12039 {
12040 inf->total_syscalls_count = 0;
12041 inf->any_syscall_count = 0;
12042 VEC_free (int, inf->syscalls_counts);
12043 }
12044
12045 static void
12046 save_command (char *arg, int from_tty)
12047 {
12048 printf_unfiltered (_("\"save\" must be followed by "
12049 "the name of a save subcommand.\n"));
12050 help_list (save_cmdlist, "save ", -1, gdb_stdout);
12051 }
12052
12053 struct breakpoint *
12054 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
12055 void *data)
12056 {
12057 struct breakpoint *b, *b_tmp;
12058
12059 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12060 {
12061 if ((*callback) (b, data))
12062 return b;
12063 }
12064
12065 return NULL;
12066 }
12067
12068 void
12069 _initialize_breakpoint (void)
12070 {
12071 struct cmd_list_element *c;
12072
12073 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
12074 observer_attach_inferior_exit (clear_syscall_counts);
12075 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
12076
12077 breakpoint_objfile_key = register_objfile_data ();
12078
12079 breakpoint_chain = 0;
12080 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
12081 before a breakpoint is set. */
12082 breakpoint_count = 0;
12083
12084 tracepoint_count = 0;
12085
12086 add_com ("ignore", class_breakpoint, ignore_command, _("\
12087 Set ignore-count of breakpoint number N to COUNT.\n\
12088 Usage is `ignore N COUNT'."));
12089 if (xdb_commands)
12090 add_com_alias ("bc", "ignore", class_breakpoint, 1);
12091
12092 add_com ("commands", class_breakpoint, commands_command, _("\
12093 Set commands to be executed when a breakpoint is hit.\n\
12094 Give breakpoint number as argument after \"commands\".\n\
12095 With no argument, the targeted breakpoint is the last one set.\n\
12096 The commands themselves follow starting on the next line.\n\
12097 Type a line containing \"end\" to indicate the end of them.\n\
12098 Give \"silent\" as the first line to make the breakpoint silent;\n\
12099 then no output is printed when it is hit, except what the commands print."));
12100
12101 add_com ("condition", class_breakpoint, condition_command, _("\
12102 Specify breakpoint number N to break only if COND is true.\n\
12103 Usage is `condition N COND', where N is an integer and COND is an\n\
12104 expression to be evaluated whenever breakpoint N is reached."));
12105
12106 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
12107 Set a temporary breakpoint.\n\
12108 Like \"break\" except the breakpoint is only temporary,\n\
12109 so it will be deleted when hit. Equivalent to \"break\" followed\n\
12110 by using \"enable delete\" on the breakpoint number.\n\
12111 \n"
12112 BREAK_ARGS_HELP ("tbreak")));
12113 set_cmd_completer (c, location_completer);
12114
12115 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
12116 Set a hardware assisted breakpoint.\n\
12117 Like \"break\" except the breakpoint requires hardware support,\n\
12118 some target hardware may not have this support.\n\
12119 \n"
12120 BREAK_ARGS_HELP ("hbreak")));
12121 set_cmd_completer (c, location_completer);
12122
12123 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
12124 Set a temporary hardware assisted breakpoint.\n\
12125 Like \"hbreak\" except the breakpoint is only temporary,\n\
12126 so it will be deleted when hit.\n\
12127 \n"
12128 BREAK_ARGS_HELP ("thbreak")));
12129 set_cmd_completer (c, location_completer);
12130
12131 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
12132 Enable some breakpoints.\n\
12133 Give breakpoint numbers (separated by spaces) as arguments.\n\
12134 With no subcommand, breakpoints are enabled until you command otherwise.\n\
12135 This is used to cancel the effect of the \"disable\" command.\n\
12136 With a subcommand you can enable temporarily."),
12137 &enablelist, "enable ", 1, &cmdlist);
12138 if (xdb_commands)
12139 add_com ("ab", class_breakpoint, enable_command, _("\
12140 Enable some breakpoints.\n\
12141 Give breakpoint numbers (separated by spaces) as arguments.\n\
12142 With no subcommand, breakpoints are enabled until you command otherwise.\n\
12143 This is used to cancel the effect of the \"disable\" command.\n\
12144 With a subcommand you can enable temporarily."));
12145
12146 add_com_alias ("en", "enable", class_breakpoint, 1);
12147
12148 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
12149 Enable some breakpoints.\n\
12150 Give breakpoint numbers (separated by spaces) as arguments.\n\
12151 This is used to cancel the effect of the \"disable\" command.\n\
12152 May be abbreviated to simply \"enable\".\n"),
12153 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
12154
12155 add_cmd ("once", no_class, enable_once_command, _("\
12156 Enable breakpoints for one hit. Give breakpoint numbers.\n\
12157 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
12158 &enablebreaklist);
12159
12160 add_cmd ("delete", no_class, enable_delete_command, _("\
12161 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12162 If a breakpoint is hit while enabled in this fashion, it is deleted."),
12163 &enablebreaklist);
12164
12165 add_cmd ("delete", no_class, enable_delete_command, _("\
12166 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12167 If a breakpoint is hit while enabled in this fashion, it is deleted."),
12168 &enablelist);
12169
12170 add_cmd ("once", no_class, enable_once_command, _("\
12171 Enable breakpoints for one hit. Give breakpoint numbers.\n\
12172 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
12173 &enablelist);
12174
12175 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
12176 Disable some breakpoints.\n\
12177 Arguments are breakpoint numbers with spaces in between.\n\
12178 To disable all breakpoints, give no argument.\n\
12179 A disabled breakpoint is not forgotten, but has no effect until reenabled."),
12180 &disablelist, "disable ", 1, &cmdlist);
12181 add_com_alias ("dis", "disable", class_breakpoint, 1);
12182 add_com_alias ("disa", "disable", class_breakpoint, 1);
12183 if (xdb_commands)
12184 add_com ("sb", class_breakpoint, disable_command, _("\
12185 Disable some breakpoints.\n\
12186 Arguments are breakpoint numbers with spaces in between.\n\
12187 To disable all breakpoints, give no argument.\n\
12188 A disabled breakpoint is not forgotten, but has no effect until reenabled."));
12189
12190 add_cmd ("breakpoints", class_alias, disable_command, _("\
12191 Disable some breakpoints.\n\
12192 Arguments are breakpoint numbers with spaces in between.\n\
12193 To disable all breakpoints, give no argument.\n\
12194 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
12195 This command may be abbreviated \"disable\"."),
12196 &disablelist);
12197
12198 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
12199 Delete some breakpoints or auto-display expressions.\n\
12200 Arguments are breakpoint numbers with spaces in between.\n\
12201 To delete all breakpoints, give no argument.\n\
12202 \n\
12203 Also a prefix command for deletion of other GDB objects.\n\
12204 The \"unset\" command is also an alias for \"delete\"."),
12205 &deletelist, "delete ", 1, &cmdlist);
12206 add_com_alias ("d", "delete", class_breakpoint, 1);
12207 add_com_alias ("del", "delete", class_breakpoint, 1);
12208 if (xdb_commands)
12209 add_com ("db", class_breakpoint, delete_command, _("\
12210 Delete some breakpoints.\n\
12211 Arguments are breakpoint numbers with spaces in between.\n\
12212 To delete all breakpoints, give no argument.\n"));
12213
12214 add_cmd ("breakpoints", class_alias, delete_command, _("\
12215 Delete some breakpoints or auto-display expressions.\n\
12216 Arguments are breakpoint numbers with spaces in between.\n\
12217 To delete all breakpoints, give no argument.\n\
12218 This command may be abbreviated \"delete\"."),
12219 &deletelist);
12220
12221 add_com ("clear", class_breakpoint, clear_command, _("\
12222 Clear breakpoint at specified line or function.\n\
12223 Argument may be line number, function name, or \"*\" and an address.\n\
12224 If line number is specified, all breakpoints in that line are cleared.\n\
12225 If function is specified, breakpoints at beginning of function are cleared.\n\
12226 If an address is specified, breakpoints at that address are cleared.\n\
12227 \n\
12228 With no argument, clears all breakpoints in the line that the selected frame\n\
12229 is executing in.\n\
12230 \n\
12231 See also the \"delete\" command which clears breakpoints by number."));
12232 add_com_alias ("cl", "clear", class_breakpoint, 1);
12233
12234 c = add_com ("break", class_breakpoint, break_command, _("\
12235 Set breakpoint at specified line or function.\n"
12236 BREAK_ARGS_HELP ("break")));
12237 set_cmd_completer (c, location_completer);
12238
12239 add_com_alias ("b", "break", class_run, 1);
12240 add_com_alias ("br", "break", class_run, 1);
12241 add_com_alias ("bre", "break", class_run, 1);
12242 add_com_alias ("brea", "break", class_run, 1);
12243
12244 if (xdb_commands)
12245 add_com_alias ("ba", "break", class_breakpoint, 1);
12246
12247 if (dbx_commands)
12248 {
12249 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
12250 Break in function/address or break at a line in the current file."),
12251 &stoplist, "stop ", 1, &cmdlist);
12252 add_cmd ("in", class_breakpoint, stopin_command,
12253 _("Break in function or address."), &stoplist);
12254 add_cmd ("at", class_breakpoint, stopat_command,
12255 _("Break at a line in the current file."), &stoplist);
12256 add_com ("status", class_info, breakpoints_info, _("\
12257 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
12258 The \"Type\" column indicates one of:\n\
12259 \tbreakpoint - normal breakpoint\n\
12260 \twatchpoint - watchpoint\n\
12261 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12262 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12263 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
12264 address and file/line number respectively.\n\
12265 \n\
12266 Convenience variable \"$_\" and default examine address for \"x\"\n\
12267 are set to the address of the last breakpoint listed unless the command\n\
12268 is prefixed with \"server \".\n\n\
12269 Convenience variable \"$bpnum\" contains the number of the last\n\
12270 breakpoint set."));
12271 }
12272
12273 add_info ("breakpoints", breakpoints_info, _("\
12274 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
12275 The \"Type\" column indicates one of:\n\
12276 \tbreakpoint - normal breakpoint\n\
12277 \twatchpoint - watchpoint\n\
12278 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12279 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12280 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
12281 address and file/line number respectively.\n\
12282 \n\
12283 Convenience variable \"$_\" and default examine address for \"x\"\n\
12284 are set to the address of the last breakpoint listed unless the command\n\
12285 is prefixed with \"server \".\n\n\
12286 Convenience variable \"$bpnum\" contains the number of the last\n\
12287 breakpoint set."));
12288
12289 add_info_alias ("b", "breakpoints", 1);
12290
12291 if (xdb_commands)
12292 add_com ("lb", class_breakpoint, breakpoints_info, _("\
12293 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
12294 The \"Type\" column indicates one of:\n\
12295 \tbreakpoint - normal breakpoint\n\
12296 \twatchpoint - watchpoint\n\
12297 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12298 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12299 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
12300 address and file/line number respectively.\n\
12301 \n\
12302 Convenience variable \"$_\" and default examine address for \"x\"\n\
12303 are set to the address of the last breakpoint listed unless the command\n\
12304 is prefixed with \"server \".\n\n\
12305 Convenience variable \"$bpnum\" contains the number of the last\n\
12306 breakpoint set."));
12307
12308 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
12309 Status of all breakpoints, or breakpoint number NUMBER.\n\
12310 The \"Type\" column indicates one of:\n\
12311 \tbreakpoint - normal breakpoint\n\
12312 \twatchpoint - watchpoint\n\
12313 \tlongjmp - internal breakpoint used to step through longjmp()\n\
12314 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
12315 \tuntil - internal breakpoint used by the \"until\" command\n\
12316 \tfinish - internal breakpoint used by the \"finish\" command\n\
12317 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12318 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12319 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
12320 address and file/line number respectively.\n\
12321 \n\
12322 Convenience variable \"$_\" and default examine address for \"x\"\n\
12323 are set to the address of the last breakpoint listed unless the command\n\
12324 is prefixed with \"server \".\n\n\
12325 Convenience variable \"$bpnum\" contains the number of the last\n\
12326 breakpoint set."),
12327 &maintenanceinfolist);
12328
12329 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
12330 Set catchpoints to catch events."),
12331 &catch_cmdlist, "catch ",
12332 0/*allow-unknown*/, &cmdlist);
12333
12334 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
12335 Set temporary catchpoints to catch events."),
12336 &tcatch_cmdlist, "tcatch ",
12337 0/*allow-unknown*/, &cmdlist);
12338
12339 /* Add catch and tcatch sub-commands. */
12340 add_catch_command ("catch", _("\
12341 Catch an exception, when caught.\n\
12342 With an argument, catch only exceptions with the given name."),
12343 catch_catch_command,
12344 NULL,
12345 CATCH_PERMANENT,
12346 CATCH_TEMPORARY);
12347 add_catch_command ("throw", _("\
12348 Catch an exception, when thrown.\n\
12349 With an argument, catch only exceptions with the given name."),
12350 catch_throw_command,
12351 NULL,
12352 CATCH_PERMANENT,
12353 CATCH_TEMPORARY);
12354 add_catch_command ("fork", _("Catch calls to fork."),
12355 catch_fork_command_1,
12356 NULL,
12357 (void *) (uintptr_t) catch_fork_permanent,
12358 (void *) (uintptr_t) catch_fork_temporary);
12359 add_catch_command ("vfork", _("Catch calls to vfork."),
12360 catch_fork_command_1,
12361 NULL,
12362 (void *) (uintptr_t) catch_vfork_permanent,
12363 (void *) (uintptr_t) catch_vfork_temporary);
12364 add_catch_command ("exec", _("Catch calls to exec."),
12365 catch_exec_command_1,
12366 NULL,
12367 CATCH_PERMANENT,
12368 CATCH_TEMPORARY);
12369 add_catch_command ("syscall", _("\
12370 Catch system calls by their names and/or numbers.\n\
12371 Arguments say which system calls to catch. If no arguments\n\
12372 are given, every system call will be caught.\n\
12373 Arguments, if given, should be one or more system call names\n\
12374 (if your system supports that), or system call numbers."),
12375 catch_syscall_command_1,
12376 catch_syscall_completer,
12377 CATCH_PERMANENT,
12378 CATCH_TEMPORARY);
12379 add_catch_command ("exception", _("\
12380 Catch Ada exceptions, when raised.\n\
12381 With an argument, catch only exceptions with the given name."),
12382 catch_ada_exception_command,
12383 NULL,
12384 CATCH_PERMANENT,
12385 CATCH_TEMPORARY);
12386 add_catch_command ("assert", _("\
12387 Catch failed Ada assertions, when raised.\n\
12388 With an argument, catch only exceptions with the given name."),
12389 catch_assert_command,
12390 NULL,
12391 CATCH_PERMANENT,
12392 CATCH_TEMPORARY);
12393
12394 c = add_com ("watch", class_breakpoint, watch_command, _("\
12395 Set a watchpoint for an expression.\n\
12396 Usage: watch [-l|-location] EXPRESSION\n\
12397 A watchpoint stops execution of your program whenever the value of\n\
12398 an expression changes.\n\
12399 If -l or -location is given, this evaluates EXPRESSION and watches\n\
12400 the memory to which it refers."));
12401 set_cmd_completer (c, expression_completer);
12402
12403 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
12404 Set a read watchpoint for an expression.\n\
12405 Usage: rwatch [-l|-location] EXPRESSION\n\
12406 A watchpoint stops execution of your program whenever the value of\n\
12407 an expression is read.\n\
12408 If -l or -location is given, this evaluates EXPRESSION and watches\n\
12409 the memory to which it refers."));
12410 set_cmd_completer (c, expression_completer);
12411
12412 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
12413 Set a watchpoint for an expression.\n\
12414 Usage: awatch [-l|-location] EXPRESSION\n\
12415 A watchpoint stops execution of your program whenever the value of\n\
12416 an expression is either read or written.\n\
12417 If -l or -location is given, this evaluates EXPRESSION and watches\n\
12418 the memory to which it refers."));
12419 set_cmd_completer (c, expression_completer);
12420
12421 add_info ("watchpoints", watchpoints_info, _("\
12422 Status of specified watchpoints (all watchpoints if no argument)."));
12423
12424 /* XXX: cagney/2005-02-23: This should be a boolean, and should
12425 respond to changes - contrary to the description. */
12426 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
12427 &can_use_hw_watchpoints, _("\
12428 Set debugger's willingness to use watchpoint hardware."), _("\
12429 Show debugger's willingness to use watchpoint hardware."), _("\
12430 If zero, gdb will not use hardware for new watchpoints, even if\n\
12431 such is available. (However, any hardware watchpoints that were\n\
12432 created before setting this to nonzero, will continue to use watchpoint\n\
12433 hardware.)"),
12434 NULL,
12435 show_can_use_hw_watchpoints,
12436 &setlist, &showlist);
12437
12438 can_use_hw_watchpoints = 1;
12439
12440 /* Tracepoint manipulation commands. */
12441
12442 c = add_com ("trace", class_breakpoint, trace_command, _("\
12443 Set a tracepoint at specified line or function.\n\
12444 \n"
12445 BREAK_ARGS_HELP ("trace") "\n\
12446 Do \"help tracepoints\" for info on other tracepoint commands."));
12447 set_cmd_completer (c, location_completer);
12448
12449 add_com_alias ("tp", "trace", class_alias, 0);
12450 add_com_alias ("tr", "trace", class_alias, 1);
12451 add_com_alias ("tra", "trace", class_alias, 1);
12452 add_com_alias ("trac", "trace", class_alias, 1);
12453
12454 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
12455 Set a fast tracepoint at specified line or function.\n\
12456 \n"
12457 BREAK_ARGS_HELP ("ftrace") "\n\
12458 Do \"help tracepoints\" for info on other tracepoint commands."));
12459 set_cmd_completer (c, location_completer);
12460
12461 c = add_com ("strace", class_breakpoint, strace_command, _("\
12462 Set a static tracepoint at specified line, function or marker.\n\
12463 \n\
12464 strace [LOCATION] [if CONDITION]\n\
12465 LOCATION may be a line number, function name, \"*\" and an address,\n\
12466 or -m MARKER_ID.\n\
12467 If a line number is specified, probe the marker at start of code\n\
12468 for that line. If a function is specified, probe the marker at start\n\
12469 of code for that function. If an address is specified, probe the marker\n\
12470 at that exact address. If a marker id is specified, probe the marker\n\
12471 with that name. With no LOCATION, uses current execution address of\n\
12472 the selected stack frame.\n\
12473 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
12474 This collects arbitrary user data passed in the probe point call to the\n\
12475 tracing library. You can inspect it when analyzing the trace buffer,\n\
12476 by printing the $_sdata variable like any other convenience variable.\n\
12477 \n\
12478 CONDITION is a boolean expression.\n\
12479 \n\
12480 Multiple tracepoints at one place are permitted, and useful if their\n\
12481 conditions are different.\n\
12482 \n\
12483 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
12484 Do \"help tracepoints\" for info on other tracepoint commands."));
12485 set_cmd_completer (c, location_completer);
12486
12487 add_info ("tracepoints", tracepoints_info, _("\
12488 Status of specified tracepoints (all tracepoints if no argument).\n\
12489 Convenience variable \"$tpnum\" contains the number of the\n\
12490 last tracepoint set."));
12491
12492 add_info_alias ("tp", "tracepoints", 1);
12493
12494 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
12495 Delete specified tracepoints.\n\
12496 Arguments are tracepoint numbers, separated by spaces.\n\
12497 No argument means delete all tracepoints."),
12498 &deletelist);
12499
12500 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
12501 Disable specified tracepoints.\n\
12502 Arguments are tracepoint numbers, separated by spaces.\n\
12503 No argument means disable all tracepoints."),
12504 &disablelist);
12505 deprecate_cmd (c, "disable");
12506
12507 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
12508 Enable specified tracepoints.\n\
12509 Arguments are tracepoint numbers, separated by spaces.\n\
12510 No argument means enable all tracepoints."),
12511 &enablelist);
12512 deprecate_cmd (c, "enable");
12513
12514 add_com ("passcount", class_trace, trace_pass_command, _("\
12515 Set the passcount for a tracepoint.\n\
12516 The trace will end when the tracepoint has been passed 'count' times.\n\
12517 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
12518 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
12519
12520 add_prefix_cmd ("save", class_breakpoint, save_command,
12521 _("Save breakpoint definitions as a script."),
12522 &save_cmdlist, "save ",
12523 0/*allow-unknown*/, &cmdlist);
12524
12525 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
12526 Save current breakpoint definitions as a script.\n\
12527 This includes all types of breakpoints (breakpoints, watchpoints,\n\
12528 catchpoints, tracepoints). Use the 'source' command in another debug\n\
12529 session to restore them."),
12530 &save_cmdlist);
12531 set_cmd_completer (c, filename_completer);
12532
12533 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
12534 Save current tracepoint definitions as a script.\n\
12535 Use the 'source' command in another debug session to restore them."),
12536 &save_cmdlist);
12537 set_cmd_completer (c, filename_completer);
12538
12539 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
12540 deprecate_cmd (c, "save tracepoints");
12541
12542 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
12543 Breakpoint specific settings\n\
12544 Configure various breakpoint-specific variables such as\n\
12545 pending breakpoint behavior"),
12546 &breakpoint_set_cmdlist, "set breakpoint ",
12547 0/*allow-unknown*/, &setlist);
12548 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
12549 Breakpoint specific settings\n\
12550 Configure various breakpoint-specific variables such as\n\
12551 pending breakpoint behavior"),
12552 &breakpoint_show_cmdlist, "show breakpoint ",
12553 0/*allow-unknown*/, &showlist);
12554
12555 add_setshow_auto_boolean_cmd ("pending", no_class,
12556 &pending_break_support, _("\
12557 Set debugger's behavior regarding pending breakpoints."), _("\
12558 Show debugger's behavior regarding pending breakpoints."), _("\
12559 If on, an unrecognized breakpoint location will cause gdb to create a\n\
12560 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
12561 an error. If auto, an unrecognized breakpoint location results in a\n\
12562 user-query to see if a pending breakpoint should be created."),
12563 NULL,
12564 show_pending_break_support,
12565 &breakpoint_set_cmdlist,
12566 &breakpoint_show_cmdlist);
12567
12568 pending_break_support = AUTO_BOOLEAN_AUTO;
12569
12570 add_setshow_boolean_cmd ("auto-hw", no_class,
12571 &automatic_hardware_breakpoints, _("\
12572 Set automatic usage of hardware breakpoints."), _("\
12573 Show automatic usage of hardware breakpoints."), _("\
12574 If set, the debugger will automatically use hardware breakpoints for\n\
12575 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
12576 a warning will be emitted for such breakpoints."),
12577 NULL,
12578 show_automatic_hardware_breakpoints,
12579 &breakpoint_set_cmdlist,
12580 &breakpoint_show_cmdlist);
12581
12582 add_setshow_enum_cmd ("always-inserted", class_support,
12583 always_inserted_enums, &always_inserted_mode, _("\
12584 Set mode for inserting breakpoints."), _("\
12585 Show mode for inserting breakpoints."), _("\
12586 When this mode is off, breakpoints are inserted in inferior when it is\n\
12587 resumed, and removed when execution stops. When this mode is on,\n\
12588 breakpoints are inserted immediately and removed only when the user\n\
12589 deletes the breakpoint. When this mode is auto (which is the default),\n\
12590 the behaviour depends on the non-stop setting (see help set non-stop).\n\
12591 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
12592 behaves as if always-inserted mode is on; if gdb is controlling the\n\
12593 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
12594 NULL,
12595 &show_always_inserted_mode,
12596 &breakpoint_set_cmdlist,
12597 &breakpoint_show_cmdlist);
12598
12599 automatic_hardware_breakpoints = 1;
12600
12601 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
12602 }