IBM zSystems: Add support for z16 as CPU name.
[binutils-gdb.git] / gdb / break-catch-fork.c
blobc2c87e8936181f839361b50c01550b6f24ef9931
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/>. */
20 #include "defs.h"
22 #include "annotate.h"
23 #include "arch-utils.h"
24 #include "breakpoint.h"
25 #include "cli/cli-decode.h"
26 #include "inferior.h"
27 #include "mi/mi-common.h"
28 #include "target.h"
29 #include "valprint.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. */
38 bool is_vfork;
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
47 catchpoints. */
49 static int
50 insert_catch_fork (struct bp_location *bl)
52 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
54 if (c->is_vfork)
55 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
56 else
57 return target_insert_fork_catchpoint (inferior_ptid.pid ());
60 /* Implement the "remove" breakpoint_ops method for fork
61 catchpoints. */
63 static int
64 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
66 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
68 if (c->is_vfork)
69 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
70 else
71 return target_remove_fork_catchpoint (inferior_ptid.pid ());
74 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
75 catchpoints. */
77 static int
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))
87 return 0;
89 c->forked_inferior_pid = ws.child_ptid ();
90 return 1;
93 /* Implement the "print_it" breakpoint_ops method for fork
94 catchpoints. */
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 ");
107 else
108 uiout->text ("Catchpoint ");
109 if (uiout->is_mi_like_p ())
111 uiout->field_string ("reason",
112 async_reason_lookup (c->is_vfork
113 ? EXEC_ASYNC_VFORK
114 : EXEC_ASYNC_FORK));
115 uiout->field_string ("disp", bpdisp_text (b->disposition));
117 uiout->field_signed ("bkptno", b->number);
118 if (c->is_vfork)
119 uiout->text (" (vforked process ");
120 else
121 uiout->text (" (forked process ");
122 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
123 uiout->text ("), ");
124 return PRINT_SRC_AND_LOC;
127 /* Implement the "print_one" breakpoint_ops method for fork
128 catchpoints. */
130 static void
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
141 readable). */
142 if (opts.addressprint)
143 uiout->field_skip ("addr");
144 annotate_field (5);
145 const char *name = c->is_vfork ? "vfork" : "fork";
146 uiout->text (name);
147 if (c->forked_inferior_pid != null_ptid)
149 uiout->text (", process ");
150 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
151 uiout->spaces (1);
154 if (uiout->is_mi_like_p ())
155 uiout->field_string ("catch-type", name);
158 /* Implement the "print_mention" breakpoint_ops method for fork
159 catchpoints. */
161 static void
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
170 catchpoints. */
172 static void
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;
185 static void
186 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
187 bool temp, const char *cond_string,
188 bool is_vfork)
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);
200 typedef enum
202 catch_fork_temporary, catch_vfork_temporary,
203 catch_fork_permanent, catch_vfork_permanent
205 catch_fork_kind;
207 static void
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);
219 if (!arg)
220 arg = "";
221 arg = skip_spaces (arg);
223 /* The allowed syntax is:
224 catch [v]fork
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. */
235 switch (fork_kind)
237 case catch_fork_temporary:
238 case catch_fork_permanent:
239 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string, false);
240 break;
241 case catch_vfork_temporary:
242 case catch_vfork_permanent:
243 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string, true);
244 break;
245 default:
246 error (_("unsupported or unknown fork kind; cannot catch it"));
247 break;
251 static void
252 initialize_ops ()
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 ();
271 void
272 _initialize_break_catch_fork ()
274 initialize_ops ();
276 add_catch_command ("fork", _("Catch calls to fork."),
277 catch_fork_command_1,
278 NULL,
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,
283 NULL,
284 (void *) (uintptr_t) catch_vfork_permanent,
285 (void *) (uintptr_t) catch_vfork_temporary);