Add constructor to fork_catchpoint
[binutils-gdb.git] / gdb / break-catch-fork.c
1 /* Everything about vfork catchpoints, for GDB.
2
3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21
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"
30
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. */
34
35 struct fork_catchpoint : public breakpoint
36 {
37 explicit fork_catchpoint (bool is_vfork_)
38 : is_vfork (is_vfork_)
39 {
40 }
41
42 int insert_location (struct bp_location *) override;
43 int remove_location (struct bp_location *,
44 enum remove_bp_reason reason) override;
45 int breakpoint_hit (const struct bp_location *bl,
46 const address_space *aspace,
47 CORE_ADDR bp_addr,
48 const target_waitstatus &ws) override;
49 enum print_stop_action print_it (const bpstat *bs) const override;
50 bool print_one (bp_location **) const override;
51 void print_mention () const override;
52 void print_recreate (struct ui_file *fp) const override;
53
54 /* True if the breakpoint is for vfork, false for fork. */
55 bool is_vfork;
56
57 /* Process id of a child process whose forking triggered this
58 catchpoint. This field is only valid immediately after this
59 catchpoint has triggered. */
60 ptid_t forked_inferior_pid = null_ptid;
61 };
62
63 /* Implement the "insert" method for fork catchpoints. */
64
65 int
66 fork_catchpoint::insert_location (struct bp_location *bl)
67 {
68 if (is_vfork)
69 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
70 else
71 return target_insert_fork_catchpoint (inferior_ptid.pid ());
72 }
73
74 /* Implement the "remove" method for fork catchpoints. */
75
76 int
77 fork_catchpoint::remove_location (struct bp_location *bl,
78 enum remove_bp_reason reason)
79 {
80 if (is_vfork)
81 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
82 else
83 return target_remove_fork_catchpoint (inferior_ptid.pid ());
84 }
85
86 /* Implement the "breakpoint_hit" method for fork catchpoints. */
87
88 int
89 fork_catchpoint::breakpoint_hit (const struct bp_location *bl,
90 const address_space *aspace,
91 CORE_ADDR bp_addr,
92 const target_waitstatus &ws)
93 {
94 if (ws.kind () != (is_vfork
95 ? TARGET_WAITKIND_VFORKED
96 : TARGET_WAITKIND_FORKED))
97 return 0;
98
99 forked_inferior_pid = ws.child_ptid ();
100 return 1;
101 }
102
103 /* Implement the "print_it" method for fork catchpoints. */
104
105 enum print_stop_action
106 fork_catchpoint::print_it (const bpstat *bs) const
107 {
108 struct ui_out *uiout = current_uiout;
109
110 annotate_catchpoint (number);
111 maybe_print_thread_hit_breakpoint (uiout);
112 if (disposition == disp_del)
113 uiout->text ("Temporary catchpoint ");
114 else
115 uiout->text ("Catchpoint ");
116 if (uiout->is_mi_like_p ())
117 {
118 uiout->field_string ("reason",
119 async_reason_lookup (is_vfork
120 ? EXEC_ASYNC_VFORK
121 : EXEC_ASYNC_FORK));
122 uiout->field_string ("disp", bpdisp_text (disposition));
123 }
124 uiout->field_signed ("bkptno", number);
125 if (is_vfork)
126 uiout->text (" (vforked process ");
127 else
128 uiout->text (" (forked process ");
129 uiout->field_signed ("newpid", forked_inferior_pid.pid ());
130 uiout->text ("), ");
131 return PRINT_SRC_AND_LOC;
132 }
133
134 /* Implement the "print_one" method for fork catchpoints. */
135
136 bool
137 fork_catchpoint::print_one (bp_location **last_loc) const
138 {
139 struct value_print_options opts;
140 struct ui_out *uiout = current_uiout;
141
142 get_user_print_options (&opts);
143
144 /* Field 4, the address, is omitted (which makes the columns not
145 line up too nicely with the headers, but the effect is relatively
146 readable). */
147 if (opts.addressprint)
148 uiout->field_skip ("addr");
149 annotate_field (5);
150 const char *name = is_vfork ? "vfork" : "fork";
151 uiout->text (name);
152 if (forked_inferior_pid != null_ptid)
153 {
154 uiout->text (", process ");
155 uiout->field_signed ("what", forked_inferior_pid.pid ());
156 uiout->spaces (1);
157 }
158
159 if (uiout->is_mi_like_p ())
160 uiout->field_string ("catch-type", name);
161
162 return true;
163 }
164
165 /* Implement the "print_mention" method for fork catchpoints. */
166
167 void
168 fork_catchpoint::print_mention () const
169 {
170 gdb_printf (_("Catchpoint %d (%s)"), number,
171 is_vfork ? "vfork" : "fork");
172 }
173
174 /* Implement the "print_recreate" method for fork catchpoints. */
175
176 void
177 fork_catchpoint::print_recreate (struct ui_file *fp) const
178 {
179 gdb_printf (fp, "catch %s", is_vfork ? "vfork" : "fork");
180 print_recreate_thread (fp);
181 }
182
183 static void
184 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
185 bool temp, const char *cond_string,
186 bool is_vfork)
187 {
188 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint (is_vfork));
189
190 init_catchpoint (c.get (), gdbarch, temp, cond_string);
191
192 install_breakpoint (0, std::move (c), 1);
193 }
194
195 enum catch_fork_kind
196 {
197 catch_fork_temporary, catch_vfork_temporary,
198 catch_fork_permanent, catch_vfork_permanent
199 };
200
201 static void
202 catch_fork_command_1 (const char *arg, int from_tty,
203 struct cmd_list_element *command)
204 {
205 struct gdbarch *gdbarch = get_current_arch ();
206 const char *cond_string = NULL;
207 catch_fork_kind fork_kind;
208
209 fork_kind = (catch_fork_kind) (uintptr_t) command->context ();
210 bool temp = (fork_kind == catch_fork_temporary
211 || fork_kind == catch_vfork_temporary);
212
213 if (!arg)
214 arg = "";
215 arg = skip_spaces (arg);
216
217 /* The allowed syntax is:
218 catch [v]fork
219 catch [v]fork if <cond>
220
221 First, check if there's an if clause. */
222 cond_string = ep_parse_optional_if_clause (&arg);
223
224 if ((*arg != '\0') && !isspace (*arg))
225 error (_("Junk at end of arguments."));
226
227 /* If this target supports it, create a fork or vfork catchpoint
228 and enable reporting of such events. */
229 switch (fork_kind)
230 {
231 case catch_fork_temporary:
232 case catch_fork_permanent:
233 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string, false);
234 break;
235 case catch_vfork_temporary:
236 case catch_vfork_permanent:
237 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string, true);
238 break;
239 default:
240 error (_("unsupported or unknown fork kind; cannot catch it"));
241 break;
242 }
243 }
244
245 void _initialize_break_catch_fork ();
246 void
247 _initialize_break_catch_fork ()
248 {
249 add_catch_command ("fork", _("Catch calls to fork."),
250 catch_fork_command_1,
251 NULL,
252 (void *) (uintptr_t) catch_fork_permanent,
253 (void *) (uintptr_t) catch_fork_temporary);
254 add_catch_command ("vfork", _("Catch calls to vfork."),
255 catch_fork_command_1,
256 NULL,
257 (void *) (uintptr_t) catch_vfork_permanent,
258 (void *) (uintptr_t) catch_vfork_temporary);
259 }