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