1 /* Everything about vfork catchpoints, for GDB.
3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "arch-utils.h"
24 #include "breakpoint.h"
25 #include "cli/cli-decode.h"
27 #include "mi/mi-common.h"
31 /* An instance of this type is used to represent a fork or vfork
32 catchpoint. A breakpoint is really of this type iff its ops pointer points
33 to CATCH_FORK_BREAKPOINT_OPS. */
35 struct fork_catchpoint
: public breakpoint
37 int insert_location (struct bp_location
*) override
;
38 int remove_location (struct bp_location
*,
39 enum remove_bp_reason reason
) override
;
40 int breakpoint_hit (const struct bp_location
*bl
,
41 const address_space
*aspace
,
43 const target_waitstatus
&ws
) override
;
44 enum print_stop_action
print_it (struct bpstat
*bs
) override
;
45 bool print_one (struct bp_location
**) override
;
46 void print_mention () override
;
47 void print_recreate (struct ui_file
*fp
) override
;
49 /* True if the breakpoint is for vfork, false for fork. */
52 /* Process id of a child process whose forking triggered this
53 catchpoint. This field is only valid immediately after this
54 catchpoint has triggered. */
55 ptid_t forked_inferior_pid
;
58 /* Implement the "insert" method for fork catchpoints. */
61 fork_catchpoint::insert_location (struct bp_location
*bl
)
64 return target_insert_vfork_catchpoint (inferior_ptid
.pid ());
66 return target_insert_fork_catchpoint (inferior_ptid
.pid ());
69 /* Implement the "remove" method for fork catchpoints. */
72 fork_catchpoint::remove_location (struct bp_location
*bl
,
73 enum remove_bp_reason reason
)
76 return target_remove_vfork_catchpoint (inferior_ptid
.pid ());
78 return target_remove_fork_catchpoint (inferior_ptid
.pid ());
81 /* Implement the "breakpoint_hit" method for fork catchpoints. */
84 fork_catchpoint::breakpoint_hit (const struct bp_location
*bl
,
85 const address_space
*aspace
,
87 const target_waitstatus
&ws
)
89 if (ws
.kind () != (is_vfork
90 ? TARGET_WAITKIND_VFORKED
91 : TARGET_WAITKIND_FORKED
))
94 forked_inferior_pid
= ws
.child_ptid ();
98 /* Implement the "print_it" method for fork catchpoints. */
100 enum print_stop_action
101 fork_catchpoint::print_it (bpstat
*bs
)
103 struct ui_out
*uiout
= current_uiout
;
105 annotate_catchpoint (number
);
106 maybe_print_thread_hit_breakpoint (uiout
);
107 if (disposition
== disp_del
)
108 uiout
->text ("Temporary catchpoint ");
110 uiout
->text ("Catchpoint ");
111 if (uiout
->is_mi_like_p ())
113 uiout
->field_string ("reason",
114 async_reason_lookup (is_vfork
117 uiout
->field_string ("disp", bpdisp_text (disposition
));
119 uiout
->field_signed ("bkptno", number
);
121 uiout
->text (" (vforked process ");
123 uiout
->text (" (forked process ");
124 uiout
->field_signed ("newpid", forked_inferior_pid
.pid ());
126 return PRINT_SRC_AND_LOC
;
129 /* Implement the "print_one" method for fork catchpoints. */
132 fork_catchpoint::print_one (struct bp_location
**last_loc
)
134 struct value_print_options opts
;
135 struct ui_out
*uiout
= current_uiout
;
137 get_user_print_options (&opts
);
139 /* Field 4, the address, is omitted (which makes the columns not
140 line up too nicely with the headers, but the effect is relatively
142 if (opts
.addressprint
)
143 uiout
->field_skip ("addr");
145 const char *name
= is_vfork
? "vfork" : "fork";
147 if (forked_inferior_pid
!= null_ptid
)
149 uiout
->text (", process ");
150 uiout
->field_signed ("what", forked_inferior_pid
.pid ());
154 if (uiout
->is_mi_like_p ())
155 uiout
->field_string ("catch-type", name
);
160 /* Implement the "print_mention" method for fork catchpoints. */
163 fork_catchpoint::print_mention ()
165 gdb_printf (_("Catchpoint %d (%s)"), number
,
166 is_vfork
? "vfork" : "fork");
169 /* Implement the "print_recreate" method for fork catchpoints. */
172 fork_catchpoint::print_recreate (struct ui_file
*fp
)
174 gdb_printf (fp
, "catch %s", is_vfork
? "vfork" : "fork");
175 print_recreate_thread (this, fp
);
179 create_fork_vfork_event_catchpoint (struct gdbarch
*gdbarch
,
180 bool temp
, const char *cond_string
,
183 std::unique_ptr
<fork_catchpoint
> c (new fork_catchpoint ());
185 init_catchpoint (c
.get (), gdbarch
, temp
, cond_string
);
186 c
->is_vfork
= is_vfork
;
187 c
->forked_inferior_pid
= null_ptid
;
189 install_breakpoint (0, std::move (c
), 1);
194 catch_fork_temporary
, catch_vfork_temporary
,
195 catch_fork_permanent
, catch_vfork_permanent
199 catch_fork_command_1 (const char *arg
, int from_tty
,
200 struct cmd_list_element
*command
)
202 struct gdbarch
*gdbarch
= get_current_arch ();
203 const char *cond_string
= NULL
;
204 catch_fork_kind fork_kind
;
206 fork_kind
= (catch_fork_kind
) (uintptr_t) command
->context ();
207 bool temp
= (fork_kind
== catch_fork_temporary
208 || fork_kind
== catch_vfork_temporary
);
212 arg
= skip_spaces (arg
);
214 /* The allowed syntax is:
216 catch [v]fork if <cond>
218 First, check if there's an if clause. */
219 cond_string
= ep_parse_optional_if_clause (&arg
);
221 if ((*arg
!= '\0') && !isspace (*arg
))
222 error (_("Junk at end of arguments."));
224 /* If this target supports it, create a fork or vfork catchpoint
225 and enable reporting of such events. */
228 case catch_fork_temporary
:
229 case catch_fork_permanent
:
230 create_fork_vfork_event_catchpoint (gdbarch
, temp
, cond_string
, false);
232 case catch_vfork_temporary
:
233 case catch_vfork_permanent
:
234 create_fork_vfork_event_catchpoint (gdbarch
, temp
, cond_string
, true);
237 error (_("unsupported or unknown fork kind; cannot catch it"));
242 void _initialize_break_catch_fork ();
244 _initialize_break_catch_fork ()
246 add_catch_command ("fork", _("Catch calls to fork."),
247 catch_fork_command_1
,
249 (void *) (uintptr_t) catch_fork_permanent
,
250 (void *) (uintptr_t) catch_fork_temporary
);
251 add_catch_command ("vfork", _("Catch calls to vfork."),
252 catch_fork_command_1
,
254 (void *) (uintptr_t) catch_vfork_permanent
,
255 (void *) (uintptr_t) catch_vfork_temporary
);