Remove breakpoint_ops from init_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 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,
42 CORE_ADDR bp_addr,
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;
48
49 /* True if the breakpoint is for vfork, false for fork. */
50 bool is_vfork;
51
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;
56 };
57
58 /* Implement the "insert" method for fork catchpoints. */
59
60 int
61 fork_catchpoint::insert_location (struct bp_location *bl)
62 {
63 if (is_vfork)
64 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
65 else
66 return target_insert_fork_catchpoint (inferior_ptid.pid ());
67 }
68
69 /* Implement the "remove" method for fork catchpoints. */
70
71 int
72 fork_catchpoint::remove_location (struct bp_location *bl,
73 enum remove_bp_reason reason)
74 {
75 if (is_vfork)
76 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
77 else
78 return target_remove_fork_catchpoint (inferior_ptid.pid ());
79 }
80
81 /* Implement the "breakpoint_hit" method for fork catchpoints. */
82
83 int
84 fork_catchpoint::breakpoint_hit (const struct bp_location *bl,
85 const address_space *aspace,
86 CORE_ADDR bp_addr,
87 const target_waitstatus &ws)
88 {
89 if (ws.kind () != (is_vfork
90 ? TARGET_WAITKIND_VFORKED
91 : TARGET_WAITKIND_FORKED))
92 return 0;
93
94 forked_inferior_pid = ws.child_ptid ();
95 return 1;
96 }
97
98 /* Implement the "print_it" method for fork catchpoints. */
99
100 enum print_stop_action
101 fork_catchpoint::print_it (bpstat *bs)
102 {
103 struct ui_out *uiout = current_uiout;
104
105 annotate_catchpoint (number);
106 maybe_print_thread_hit_breakpoint (uiout);
107 if (disposition == disp_del)
108 uiout->text ("Temporary catchpoint ");
109 else
110 uiout->text ("Catchpoint ");
111 if (uiout->is_mi_like_p ())
112 {
113 uiout->field_string ("reason",
114 async_reason_lookup (is_vfork
115 ? EXEC_ASYNC_VFORK
116 : EXEC_ASYNC_FORK));
117 uiout->field_string ("disp", bpdisp_text (disposition));
118 }
119 uiout->field_signed ("bkptno", number);
120 if (is_vfork)
121 uiout->text (" (vforked process ");
122 else
123 uiout->text (" (forked process ");
124 uiout->field_signed ("newpid", forked_inferior_pid.pid ());
125 uiout->text ("), ");
126 return PRINT_SRC_AND_LOC;
127 }
128
129 /* Implement the "print_one" method for fork catchpoints. */
130
131 bool
132 fork_catchpoint::print_one (struct bp_location **last_loc)
133 {
134 struct value_print_options opts;
135 struct ui_out *uiout = current_uiout;
136
137 get_user_print_options (&opts);
138
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 = is_vfork ? "vfork" : "fork";
146 uiout->text (name);
147 if (forked_inferior_pid != null_ptid)
148 {
149 uiout->text (", process ");
150 uiout->field_signed ("what", forked_inferior_pid.pid ());
151 uiout->spaces (1);
152 }
153
154 if (uiout->is_mi_like_p ())
155 uiout->field_string ("catch-type", name);
156
157 return true;
158 }
159
160 /* Implement the "print_mention" method for fork catchpoints. */
161
162 void
163 fork_catchpoint::print_mention ()
164 {
165 gdb_printf (_("Catchpoint %d (%s)"), number,
166 is_vfork ? "vfork" : "fork");
167 }
168
169 /* Implement the "print_recreate" method for fork catchpoints. */
170
171 void
172 fork_catchpoint::print_recreate (struct ui_file *fp)
173 {
174 gdb_printf (fp, "catch %s", is_vfork ? "vfork" : "fork");
175 print_recreate_thread (this, fp);
176 }
177
178 static void
179 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
180 bool temp, const char *cond_string,
181 bool is_vfork)
182 {
183 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
184
185 init_catchpoint (c.get (), gdbarch, temp, cond_string);
186 c->is_vfork = is_vfork;
187 c->forked_inferior_pid = null_ptid;
188
189 install_breakpoint (0, std::move (c), 1);
190 }
191
192 enum catch_fork_kind
193 {
194 catch_fork_temporary, catch_vfork_temporary,
195 catch_fork_permanent, catch_vfork_permanent
196 };
197
198 static void
199 catch_fork_command_1 (const char *arg, int from_tty,
200 struct cmd_list_element *command)
201 {
202 struct gdbarch *gdbarch = get_current_arch ();
203 const char *cond_string = NULL;
204 catch_fork_kind fork_kind;
205
206 fork_kind = (catch_fork_kind) (uintptr_t) command->context ();
207 bool temp = (fork_kind == catch_fork_temporary
208 || fork_kind == catch_vfork_temporary);
209
210 if (!arg)
211 arg = "";
212 arg = skip_spaces (arg);
213
214 /* The allowed syntax is:
215 catch [v]fork
216 catch [v]fork if <cond>
217
218 First, check if there's an if clause. */
219 cond_string = ep_parse_optional_if_clause (&arg);
220
221 if ((*arg != '\0') && !isspace (*arg))
222 error (_("Junk at end of arguments."));
223
224 /* If this target supports it, create a fork or vfork catchpoint
225 and enable reporting of such events. */
226 switch (fork_kind)
227 {
228 case catch_fork_temporary:
229 case catch_fork_permanent:
230 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string, false);
231 break;
232 case catch_vfork_temporary:
233 case catch_vfork_permanent:
234 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string, true);
235 break;
236 default:
237 error (_("unsupported or unknown fork kind; cannot catch it"));
238 break;
239 }
240 }
241
242 void _initialize_break_catch_fork ();
243 void
244 _initialize_break_catch_fork ()
245 {
246 add_catch_command ("fork", _("Catch calls to fork."),
247 catch_fork_command_1,
248 NULL,
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,
253 NULL,
254 (void *) (uintptr_t) catch_vfork_permanent,
255 (void *) (uintptr_t) catch_vfork_temporary);
256 }