1 /* GNU/Linux native-dependent code common to multiple platforms.
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 #include <sys/ptrace.h>
28 #include "linux-nat.h"
30 /* If the system headers did not provide the constants, hard-code the normal
32 #ifndef PTRACE_EVENT_FORK
34 #define PTRACE_SETOPTIONS 0x4200
35 #define PTRACE_GETEVENTMSG 0x4201
37 /* options set using PTRACE_SETOPTIONS */
38 #define PTRACE_O_TRACESYSGOOD 0x00000001
39 #define PTRACE_O_TRACEFORK 0x00000002
40 #define PTRACE_O_TRACEVFORK 0x00000004
41 #define PTRACE_O_TRACECLONE 0x00000008
42 #define PTRACE_O_TRACEEXEC 0x00000010
44 /* Wait extended result codes for the above trace options. */
45 #define PTRACE_EVENT_FORK 1
46 #define PTRACE_EVENT_VFORK 2
47 #define PTRACE_EVENT_CLONE 3
48 #define PTRACE_EVENT_EXEC 4
50 #endif /* PTRACE_EVENT_FORK */
52 /* We can't always assume that this flag is available, but all systems
53 with the ptrace event handlers also have __WALL, so it's safe to use
56 #define __WALL 0x40000000 /* Wait for any child. */
59 extern struct target_ops child_ops;
61 struct simple_pid_list
64 struct simple_pid_list *next;
66 struct simple_pid_list *stopped_pids;
68 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
69 can not be used, 1 if it can. */
71 static int linux_supports_tracefork_flag = -1;
74 /* Trivial list manipulation functions to keep track of a list of
75 new stopped processes. */
77 add_to_pid_list (struct simple_pid_list **listp, int pid)
79 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
81 new_pid->next = *listp;
86 pull_pid_from_list (struct simple_pid_list **listp, int pid)
88 struct simple_pid_list **p;
90 for (p = listp; *p != NULL; p = &(*p)->next)
93 struct simple_pid_list *next = (*p)->next;
102 linux_record_stopped_pid (int pid)
104 add_to_pid_list (&stopped_pids, pid);
108 /* A helper function for linux_test_for_tracefork, called after fork (). */
111 linux_tracefork_child (void)
115 ptrace (PTRACE_TRACEME, 0, 0, 0);
116 kill (getpid (), SIGSTOP);
121 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. We
122 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
123 fork tracing, and let it fork. If the process exits, we assume that
124 we can't use TRACEFORK; if we get the fork notification, and we can
125 extract the new child's PID, then we assume that we can. */
128 linux_test_for_tracefork (void)
130 int child_pid, ret, status;
135 perror_with_name ("linux_test_for_tracefork: fork");
138 linux_tracefork_child ();
140 ret = waitpid (child_pid, &status, 0);
142 perror_with_name ("linux_test_for_tracefork: waitpid");
143 else if (ret != child_pid)
144 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret);
145 if (! WIFSTOPPED (status))
146 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
148 linux_supports_tracefork_flag = 0;
150 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
153 ptrace (PTRACE_KILL, child_pid, 0, 0);
154 waitpid (child_pid, &status, 0);
158 ptrace (PTRACE_CONT, child_pid, 0, 0);
159 ret = waitpid (child_pid, &status, 0);
160 if (ret == child_pid && WIFSTOPPED (status)
161 && status >> 16 == PTRACE_EVENT_FORK)
164 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
165 if (ret == 0 && second_pid != 0)
169 linux_supports_tracefork_flag = 1;
170 waitpid (second_pid, &second_status, 0);
171 ptrace (PTRACE_DETACH, second_pid, 0, 0);
175 if (WIFSTOPPED (status))
177 ptrace (PTRACE_DETACH, child_pid, 0, 0);
178 waitpid (child_pid, &status, 0);
182 /* Return non-zero iff we have tracefork functionality available.
183 This function also sets linux_supports_tracefork_flag. */
186 linux_supports_tracefork (void)
188 if (linux_supports_tracefork_flag == -1)
189 linux_test_for_tracefork ();
190 return linux_supports_tracefork_flag;
195 linux_enable_event_reporting (ptid_t ptid)
197 int pid = ptid_get_pid (ptid);
200 if (! linux_supports_tracefork ())
203 options = PTRACE_O_TRACEFORK;
205 ptrace (PTRACE_SETOPTIONS, pid, 0, options);
209 child_post_attach (int pid)
211 linux_enable_event_reporting (pid_to_ptid (pid));
215 linux_child_post_startup_inferior (ptid_t ptid)
217 linux_enable_event_reporting (ptid);
220 #ifndef LINUX_CHILD_POST_STARTUP_INFERIOR
222 child_post_startup_inferior (ptid_t ptid)
224 linux_child_post_startup_inferior (ptid);
229 child_follow_fork (int follow_child)
232 struct target_waitstatus last_status;
233 int parent_pid, child_pid;
235 get_last_target_status (&last_ptid, &last_status);
236 parent_pid = ptid_get_pid (last_ptid);
237 child_pid = last_status.value.related_pid;
241 /* We're already attached to the parent, by default. */
243 /* Before detaching from the child, remove all breakpoints from
244 it. (This won't actually modify the breakpoint list, but will
245 physically remove the breakpoints from the child.) */
246 detach_breakpoints (child_pid);
248 fprintf_filtered (gdb_stdout,
249 "Detaching after fork from child process %d.\n",
252 ptrace (PTRACE_DETACH, child_pid, 0, 0);
256 char child_pid_spelling[40];
258 /* Needed to keep the breakpoint lists in sync. */
259 detach_breakpoints (child_pid);
261 /* Before detaching from the parent, remove all breakpoints from it. */
262 remove_breakpoints ();
264 fprintf_filtered (gdb_stdout,
265 "Attaching after fork to child process %d.\n",
268 target_detach (NULL, 0);
270 inferior_ptid = pid_to_ptid (child_pid);
271 push_target (&child_ops);
273 /* Reset breakpoints in the child as appropriate. */
274 follow_inferior_reset_breakpoints ();
281 linux_handle_extended_wait (int pid, int status,
282 struct target_waitstatus *ourstatus)
284 int event = status >> 16;
286 if (event == PTRACE_EVENT_CLONE)
287 internal_error (__FILE__, __LINE__,
288 "unexpected clone event");
290 if (event == PTRACE_EVENT_FORK)
292 unsigned long new_pid;
295 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
297 /* If we haven't already seen the new PID stop, wait for it now. */
298 if (! pull_pid_from_list (&stopped_pids, new_pid))
300 /* The new child has a pending SIGSTOP. We can't affect it until it
301 hits the SIGSTOP, but we're already attached.
303 It won't be a clone (we didn't ask for clones in the event mask)
304 so we can just call waitpid and wait for the SIGSTOP. */
306 ret = waitpid (new_pid, &status, 0);
307 } while (ret == -1 && errno == EINTR);
309 perror_with_name ("waiting for new child");
310 else if (ret != new_pid)
311 internal_error (__FILE__, __LINE__,
312 "wait returned unexpected PID %d", ret);
313 else if (!WIFSTOPPED (status) || WSTOPSIG (status) != SIGSTOP)
314 internal_error (__FILE__, __LINE__,
315 "wait returned unexpected status 0x%x", status);
318 ourstatus->kind = TARGET_WAITKIND_FORKED;
319 ourstatus->value.related_pid = new_pid;
320 return inferior_ptid;
323 internal_error (__FILE__, __LINE__,
324 "unknown ptrace event %d", event);
329 child_insert_fork_catchpoint (int pid)
331 if (! linux_supports_tracefork ())
332 error ("Your system does not support fork catchpoints.");
338 child_insert_vfork_catchpoint (int pid)
340 if (linux_supports_tracefork ())
341 error ("Vfork catchpoints have not been implemented yet.");
343 error ("Your system does not support vfork catchpoints.");
347 child_insert_exec_catchpoint (int pid)
349 if (linux_supports_tracefork ())
350 error ("Exec catchpoints have not been implemented yet.");
352 error ("Your system does not support exec catchpoints.");
359 int pid = PIDGET (inferior_ptid);
360 struct target_waitstatus last;
367 /* If we're stopped while forking and we haven't followed yet, kill the
368 other task. We need to do this first because the parent will be
369 sleeping if this is a vfork. */
371 get_last_target_status (&last_ptid, &last);
373 if (last.kind == TARGET_WAITKIND_FORKED
374 || last.kind == TARGET_WAITKIND_VFORKED)
376 ptrace (PT_KILL, last.value.related_pid);
377 ptrace_wait (null_ptid, &status);
380 /* Kill the current process. */
381 ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
382 ret = ptrace_wait (null_ptid, &status);
384 /* We might get a SIGCHLD instead of an exit status. This is
385 aggravated by the first kill above - a child has just died. */
387 while (ret == pid && WIFSTOPPED (status))
389 ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
390 ret = ptrace_wait (null_ptid, &status);
393 target_mourn_inferior ();