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