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