]> Git Repo - binutils.git/blob - gdb/break-catch-fork.c
gdb: remove SYMBOL_CLASS macro, add getter
[binutils.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   /* True if the breakpoint is for vfork, false for fork.  */
38   bool is_vfork;
39
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;
44 };
45
46 /* Implement the "insert" breakpoint_ops method for fork
47    catchpoints.  */
48
49 static int
50 insert_catch_fork (struct bp_location *bl)
51 {
52   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
53
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 ());
58 }
59
60 /* Implement the "remove" breakpoint_ops method for fork
61    catchpoints.  */
62
63 static int
64 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
65 {
66   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
67
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 ());
72 }
73
74 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
75    catchpoints.  */
76
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)
81 {
82   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
83
84   if (ws.kind () != (c->is_vfork
85                      ? TARGET_WAITKIND_VFORKED
86                      : TARGET_WAITKIND_FORKED))
87     return 0;
88
89   c->forked_inferior_pid = ws.child_ptid ();
90   return 1;
91 }
92
93 /* Implement the "print_it" breakpoint_ops method for fork
94    catchpoints.  */
95
96 static enum print_stop_action
97 print_it_catch_fork (bpstat *bs)
98 {
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;
102
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 ())
110     {
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));
116     }
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;
125 }
126
127 /* Implement the "print_one" breakpoint_ops method for fork
128    catchpoints.  */
129
130 static void
131 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
132 {
133   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
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 = c->is_vfork ? "vfork" : "fork";
146   uiout->text (name);
147   if (c->forked_inferior_pid != null_ptid)
148     {
149       uiout->text (", process ");
150       uiout->field_signed ("what", c->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
158 /* Implement the "print_mention" breakpoint_ops method for fork
159    catchpoints.  */
160
161 static void
162 print_mention_catch_fork (struct breakpoint *b)
163 {
164   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
165   printf_filtered (_("Catchpoint %d (%s)"), c->number,
166                    c->is_vfork ? "vfork" : "fork");
167 }
168
169 /* Implement the "print_recreate" breakpoint_ops method for fork
170    catchpoints.  */
171
172 static void
173 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
174 {
175   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
176   fprintf_unfiltered (fp, "catch %s",
177                       c->is_vfork ? "vfork" : "fork");
178   print_recreate_thread (b, fp);
179 }
180
181 /* The breakpoint_ops structure to be used in fork catchpoints.  */
182
183 static struct breakpoint_ops catch_fork_breakpoint_ops;
184
185 static void
186 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
187                                     bool temp, const char *cond_string,
188                                     bool is_vfork)
189 {
190   std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
191
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;
196
197   install_breakpoint (0, std::move (c), 1);
198 }
199
200 typedef enum
201 {
202   catch_fork_temporary, catch_vfork_temporary,
203   catch_fork_permanent, catch_vfork_permanent
204 }
205 catch_fork_kind;
206
207 static void
208 catch_fork_command_1 (const char *arg, int from_tty,
209                       struct cmd_list_element *command)
210 {
211   struct gdbarch *gdbarch = get_current_arch ();
212   const char *cond_string = NULL;
213   catch_fork_kind fork_kind;
214
215   fork_kind = (catch_fork_kind) (uintptr_t) command->context ();
216   bool temp = (fork_kind == catch_fork_temporary
217                || fork_kind == catch_vfork_temporary);
218
219   if (!arg)
220     arg = "";
221   arg = skip_spaces (arg);
222
223   /* The allowed syntax is:
224      catch [v]fork
225      catch [v]fork if <cond>
226
227      First, check if there's an if clause.  */
228   cond_string = ep_parse_optional_if_clause (&arg);
229
230   if ((*arg != '\0') && !isspace (*arg))
231     error (_("Junk at end of arguments."));
232
233   /* If this target supports it, create a fork or vfork catchpoint
234      and enable reporting of such events.  */
235   switch (fork_kind)
236     {
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;
248     }
249 }
250
251 static void
252 initialize_ops ()
253 {
254   struct breakpoint_ops *ops;
255
256   initialize_breakpoint_ops ();
257
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;
268 }
269
270 void _initialize_break_catch_fork ();
271 void
272 _initialize_break_catch_fork ()
273 {
274   initialize_ops ();
275
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);
286 }
This page took 0.040807 seconds and 4 git commands to generate.