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 /* True if the breakpoint is for vfork, false for fork. */
40 /* Process id of a child process whose forking triggered this
41 catchpoint. This field is only valid immediately after this
42 catchpoint has triggered. */
43 ptid_t forked_inferior_pid
;
46 /* Implement the "insert" breakpoint_ops method for fork
50 insert_catch_fork (struct bp_location
*bl
)
52 struct fork_catchpoint
*c
= (struct fork_catchpoint
*) bl
->owner
;
55 return target_insert_vfork_catchpoint (inferior_ptid
.pid ());
57 return target_insert_fork_catchpoint (inferior_ptid
.pid ());
60 /* Implement the "remove" breakpoint_ops method for fork
64 remove_catch_fork (struct bp_location
*bl
, enum remove_bp_reason reason
)
66 struct fork_catchpoint
*c
= (struct fork_catchpoint
*) bl
->owner
;
69 return target_remove_vfork_catchpoint (inferior_ptid
.pid ());
71 return target_remove_fork_catchpoint (inferior_ptid
.pid ());
74 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
78 breakpoint_hit_catch_fork (const struct bp_location
*bl
,
79 const address_space
*aspace
, CORE_ADDR bp_addr
,
80 const target_waitstatus
&ws
)
82 struct fork_catchpoint
*c
= (struct fork_catchpoint
*) bl
->owner
;
84 if (ws
.kind () != (c
->is_vfork
85 ? TARGET_WAITKIND_VFORKED
86 : TARGET_WAITKIND_FORKED
))
89 c
->forked_inferior_pid
= ws
.child_ptid ();
93 /* Implement the "print_it" breakpoint_ops method for fork
96 static enum print_stop_action
97 print_it_catch_fork (bpstat
*bs
)
99 struct ui_out
*uiout
= current_uiout
;
100 struct breakpoint
*b
= bs
->breakpoint_at
;
101 struct fork_catchpoint
*c
= (struct fork_catchpoint
*) bs
->breakpoint_at
;
103 annotate_catchpoint (b
->number
);
104 maybe_print_thread_hit_breakpoint (uiout
);
105 if (b
->disposition
== disp_del
)
106 uiout
->text ("Temporary catchpoint ");
108 uiout
->text ("Catchpoint ");
109 if (uiout
->is_mi_like_p ())
111 uiout
->field_string ("reason",
112 async_reason_lookup (c
->is_vfork
115 uiout
->field_string ("disp", bpdisp_text (b
->disposition
));
117 uiout
->field_signed ("bkptno", b
->number
);
119 uiout
->text (" (vforked process ");
121 uiout
->text (" (forked process ");
122 uiout
->field_signed ("newpid", c
->forked_inferior_pid
.pid ());
124 return PRINT_SRC_AND_LOC
;
127 /* Implement the "print_one" breakpoint_ops method for fork
131 print_one_catch_fork (struct breakpoint
*b
, struct bp_location
**last_loc
)
133 struct fork_catchpoint
*c
= (struct fork_catchpoint
*) b
;
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
= c
->is_vfork
? "vfork" : "fork";
147 if (c
->forked_inferior_pid
!= null_ptid
)
149 uiout
->text (", process ");
150 uiout
->field_signed ("what", c
->forked_inferior_pid
.pid ());
154 if (uiout
->is_mi_like_p ())
155 uiout
->field_string ("catch-type", name
);
158 /* Implement the "print_mention" breakpoint_ops method for fork
162 print_mention_catch_fork (struct breakpoint
*b
)
164 struct fork_catchpoint
*c
= (struct fork_catchpoint
*) b
;
165 gdb_printf (_("Catchpoint %d (%s)"), c
->number
,
166 c
->is_vfork
? "vfork" : "fork");
169 /* Implement the "print_recreate" breakpoint_ops method for fork
173 print_recreate_catch_fork (struct breakpoint
*b
, struct ui_file
*fp
)
175 struct fork_catchpoint
*c
= (struct fork_catchpoint
*) b
;
176 gdb_printf (fp
, "catch %s",
177 c
->is_vfork
? "vfork" : "fork");
178 print_recreate_thread (b
, fp
);
181 /* The breakpoint_ops structure to be used in fork catchpoints. */
183 static struct breakpoint_ops catch_fork_breakpoint_ops
;
186 create_fork_vfork_event_catchpoint (struct gdbarch
*gdbarch
,
187 bool temp
, const char *cond_string
,
190 std::unique_ptr
<fork_catchpoint
> c (new fork_catchpoint ());
192 init_catchpoint (c
.get (), gdbarch
, temp
, cond_string
,
193 &catch_fork_breakpoint_ops
);
194 c
->is_vfork
= is_vfork
;
195 c
->forked_inferior_pid
= null_ptid
;
197 install_breakpoint (0, std::move (c
), 1);
202 catch_fork_temporary
, catch_vfork_temporary
,
203 catch_fork_permanent
, catch_vfork_permanent
208 catch_fork_command_1 (const char *arg
, int from_tty
,
209 struct cmd_list_element
*command
)
211 struct gdbarch
*gdbarch
= get_current_arch ();
212 const char *cond_string
= NULL
;
213 catch_fork_kind fork_kind
;
215 fork_kind
= (catch_fork_kind
) (uintptr_t) command
->context ();
216 bool temp
= (fork_kind
== catch_fork_temporary
217 || fork_kind
== catch_vfork_temporary
);
221 arg
= skip_spaces (arg
);
223 /* The allowed syntax is:
225 catch [v]fork if <cond>
227 First, check if there's an if clause. */
228 cond_string
= ep_parse_optional_if_clause (&arg
);
230 if ((*arg
!= '\0') && !isspace (*arg
))
231 error (_("Junk at end of arguments."));
233 /* If this target supports it, create a fork or vfork catchpoint
234 and enable reporting of such events. */
237 case catch_fork_temporary
:
238 case catch_fork_permanent
:
239 create_fork_vfork_event_catchpoint (gdbarch
, temp
, cond_string
, false);
241 case catch_vfork_temporary
:
242 case catch_vfork_permanent
:
243 create_fork_vfork_event_catchpoint (gdbarch
, temp
, cond_string
, true);
246 error (_("unsupported or unknown fork kind; cannot catch it"));
254 struct breakpoint_ops
*ops
;
256 initialize_breakpoint_ops ();
258 /* Fork catchpoints. */
259 ops
= &catch_fork_breakpoint_ops
;
260 *ops
= base_breakpoint_ops
;
261 ops
->insert_location
= insert_catch_fork
;
262 ops
->remove_location
= remove_catch_fork
;
263 ops
->breakpoint_hit
= breakpoint_hit_catch_fork
;
264 ops
->print_it
= print_it_catch_fork
;
265 ops
->print_one
= print_one_catch_fork
;
266 ops
->print_mention
= print_mention_catch_fork
;
267 ops
->print_recreate
= print_recreate_catch_fork
;
270 void _initialize_break_catch_fork ();
272 _initialize_break_catch_fork ()
276 add_catch_command ("fork", _("Catch calls to fork."),
277 catch_fork_command_1
,
279 (void *) (uintptr_t) catch_fork_permanent
,
280 (void *) (uintptr_t) catch_fork_temporary
);
281 add_catch_command ("vfork", _("Catch calls to vfork."),
282 catch_fork_command_1
,
284 (void *) (uintptr_t) catch_vfork_permanent
,
285 (void *) (uintptr_t) catch_vfork_temporary
);