1 /* GNU/Linux native-dependent code common to multiple platforms.
2 Copyright (C) 2003, 2004 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
43 #define PTRACE_O_TRACEVFORKDONE 0x00000020
44 #define PTRACE_O_TRACEEXIT 0x00000040
46 /* Wait extended result codes for the above trace options. */
47 #define PTRACE_EVENT_FORK 1
48 #define PTRACE_EVENT_VFORK 2
49 #define PTRACE_EVENT_CLONE 3
50 #define PTRACE_EVENT_EXEC 4
51 #define PTRACE_EVENT_VFORKDONE 5
52 #define PTRACE_EVENT_EXIT 6
54 #endif /* PTRACE_EVENT_FORK */
56 /* We can't always assume that this flag is available, but all systems
57 with the ptrace event handlers also have __WALL, so it's safe to use
60 #define __WALL 0x40000000 /* Wait for any child. */
63 extern struct target_ops child_ops;
65 static int linux_parent_pid;
67 struct simple_pid_list
70 struct simple_pid_list *next;
72 struct simple_pid_list *stopped_pids;
74 /* This variable is a tri-state flag: -1 for unknown, 0 if PTRACE_O_TRACEFORK
75 can not be used, 1 if it can. */
77 static int linux_supports_tracefork_flag = -1;
79 /* If we have PTRACE_O_TRACEFORK, this flag indicates whether we also have
80 PTRACE_O_TRACEVFORKDONE. */
82 static int linux_supports_tracevforkdone_flag = -1;
85 /* Trivial list manipulation functions to keep track of a list of
86 new stopped processes. */
88 add_to_pid_list (struct simple_pid_list **listp, int pid)
90 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
92 new_pid->next = *listp;
97 pull_pid_from_list (struct simple_pid_list **listp, int pid)
99 struct simple_pid_list **p;
101 for (p = listp; *p != NULL; p = &(*p)->next)
102 if ((*p)->pid == pid)
104 struct simple_pid_list *next = (*p)->next;
113 linux_record_stopped_pid (int pid)
115 add_to_pid_list (&stopped_pids, pid);
119 /* A helper function for linux_test_for_tracefork, called after fork (). */
122 linux_tracefork_child (void)
126 ptrace (PTRACE_TRACEME, 0, 0, 0);
127 kill (getpid (), SIGSTOP);
132 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. We
133 create a child process, attach to it, use PTRACE_SETOPTIONS to enable
134 fork tracing, and let it fork. If the process exits, we assume that
135 we can't use TRACEFORK; if we get the fork notification, and we can
136 extract the new child's PID, then we assume that we can. */
139 linux_test_for_tracefork (void)
141 int child_pid, ret, status;
146 perror_with_name ("linux_test_for_tracefork: fork");
149 linux_tracefork_child ();
151 ret = waitpid (child_pid, &status, 0);
153 perror_with_name ("linux_test_for_tracefork: waitpid");
154 else if (ret != child_pid)
155 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret);
156 if (! WIFSTOPPED (status))
157 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
159 linux_supports_tracefork_flag = 0;
161 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
164 ptrace (PTRACE_KILL, child_pid, 0, 0);
165 waitpid (child_pid, &status, 0);
169 /* Check whether PTRACE_O_TRACEVFORKDONE is available. */
170 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0,
171 PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE);
172 linux_supports_tracevforkdone_flag = (ret == 0);
174 ptrace (PTRACE_CONT, child_pid, 0, 0);
175 ret = waitpid (child_pid, &status, 0);
176 if (ret == child_pid && WIFSTOPPED (status)
177 && status >> 16 == PTRACE_EVENT_FORK)
180 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
181 if (ret == 0 && second_pid != 0)
185 linux_supports_tracefork_flag = 1;
186 waitpid (second_pid, &second_status, 0);
187 ptrace (PTRACE_DETACH, second_pid, 0, 0);
191 if (WIFSTOPPED (status))
193 ptrace (PTRACE_DETACH, child_pid, 0, 0);
194 waitpid (child_pid, &status, 0);
198 /* Return non-zero iff we have tracefork functionality available.
199 This function also sets linux_supports_tracefork_flag. */
202 linux_supports_tracefork (void)
204 if (linux_supports_tracefork_flag == -1)
205 linux_test_for_tracefork ();
206 return linux_supports_tracefork_flag;
210 linux_supports_tracevforkdone (void)
212 if (linux_supports_tracefork_flag == -1)
213 linux_test_for_tracefork ();
214 return linux_supports_tracevforkdone_flag;
219 linux_enable_event_reporting (ptid_t ptid)
221 int pid = ptid_get_pid (ptid);
224 if (! linux_supports_tracefork ())
227 options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC
228 | PTRACE_O_TRACECLONE;
229 if (linux_supports_tracevforkdone ())
230 options |= PTRACE_O_TRACEVFORKDONE;
232 /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
233 read-only process state. */
235 ptrace (PTRACE_SETOPTIONS, pid, 0, options);
239 child_post_attach (int pid)
241 linux_enable_event_reporting (pid_to_ptid (pid));
245 linux_child_post_startup_inferior (ptid_t ptid)
247 linux_enable_event_reporting (ptid);
250 #ifndef LINUX_CHILD_POST_STARTUP_INFERIOR
252 child_post_startup_inferior (ptid_t ptid)
254 linux_child_post_startup_inferior (ptid);
259 child_follow_fork (int follow_child)
262 struct target_waitstatus last_status;
264 int parent_pid, child_pid;
266 get_last_target_status (&last_ptid, &last_status);
267 has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED);
268 parent_pid = ptid_get_pid (last_ptid);
269 child_pid = last_status.value.related_pid;
273 /* We're already attached to the parent, by default. */
275 /* Before detaching from the child, remove all breakpoints from
276 it. (This won't actually modify the breakpoint list, but will
277 physically remove the breakpoints from the child.) */
278 /* If we vforked this will remove the breakpoints from the parent
279 also, but they'll be reinserted below. */
280 detach_breakpoints (child_pid);
282 fprintf_filtered (gdb_stdout,
283 "Detaching after fork from child process %d.\n",
286 ptrace (PTRACE_DETACH, child_pid, 0, 0);
290 if (linux_supports_tracevforkdone ())
294 ptrace (PTRACE_CONT, parent_pid, 0, 0);
295 waitpid (parent_pid, &status, __WALL);
296 if ((status >> 16) != PTRACE_EVENT_VFORKDONE)
297 warning ("Unexpected waitpid result %06x when waiting for "
298 "vfork-done", status);
302 /* We can't insert breakpoints until the child has
303 finished with the shared memory region. We need to
304 wait until that happens. Ideal would be to just
306 - ptrace (PTRACE_SYSCALL, parent_pid, 0, 0);
307 - waitpid (parent_pid, &status, __WALL);
308 However, most architectures can't handle a syscall
309 being traced on the way out if it wasn't traced on
312 We might also think to loop, continuing the child
313 until it exits or gets a SIGTRAP. One problem is
314 that the child might call ptrace with PTRACE_TRACEME.
316 There's no simple and reliable way to figure out when
317 the vforked child will be done with its copy of the
318 shared memory. We could step it out of the syscall,
319 two instructions, let it go, and then single-step the
320 parent once. When we have hardware single-step, this
321 would work; with software single-step it could still
322 be made to work but we'd have to be able to insert
323 single-step breakpoints in the child, and we'd have
324 to insert -just- the single-step breakpoint in the
325 parent. Very awkward.
327 In the end, the best we can do is to make sure it
328 runs for a little while. Hopefully it will be out of
329 range of any breakpoints we reinsert. Usually this
330 is only the single-step breakpoint at vfork's return
336 /* Since we vforked, breakpoints were removed in the parent
337 too. Put them back. */
338 reattach_breakpoints (parent_pid);
343 char child_pid_spelling[40];
345 /* Needed to keep the breakpoint lists in sync. */
347 detach_breakpoints (child_pid);
349 /* Before detaching from the parent, remove all breakpoints from it. */
350 remove_breakpoints ();
352 fprintf_filtered (gdb_stdout,
353 "Attaching after fork to child process %d.\n",
356 /* If we're vforking, we may want to hold on to the parent until
357 the child exits or execs. At exec time we can remove the old
358 breakpoints from the parent and detach it; at exit time we
359 could do the same (or even, sneakily, resume debugging it - the
360 child's exec has failed, or something similar).
362 This doesn't clean up "properly", because we can't call
363 target_detach, but that's OK; if the current target is "child",
364 then it doesn't need any further cleanups, and lin_lwp will
365 generally not encounter vfork (vfork is defined to fork
368 The holding part is very easy if we have VFORKDONE events;
369 but keeping track of both processes is beyond GDB at the
370 moment. So we don't expose the parent to the rest of GDB.
371 Instead we quietly hold onto it until such time as we can
375 linux_parent_pid = parent_pid;
377 target_detach (NULL, 0);
379 inferior_ptid = pid_to_ptid (child_pid);
380 push_target (&child_ops);
382 /* Reset breakpoints in the child as appropriate. */
383 follow_inferior_reset_breakpoints ();
390 linux_handle_extended_wait (int pid, int status,
391 struct target_waitstatus *ourstatus)
393 int event = status >> 16;
395 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
396 || event == PTRACE_EVENT_CLONE)
398 unsigned long new_pid;
401 ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
403 /* If we haven't already seen the new PID stop, wait for it now. */
404 if (! pull_pid_from_list (&stopped_pids, new_pid))
406 /* The new child has a pending SIGSTOP. We can't affect it until it
407 hits the SIGSTOP, but we're already attached. */
409 ret = waitpid (new_pid, &status,
410 (event == PTRACE_EVENT_CLONE) ? __WCLONE : 0);
411 } while (ret == -1 && errno == EINTR);
413 perror_with_name ("waiting for new child");
414 else if (ret != new_pid)
415 internal_error (__FILE__, __LINE__,
416 "wait returned unexpected PID %d", ret);
417 else if (!WIFSTOPPED (status) || WSTOPSIG (status) != SIGSTOP)
418 internal_error (__FILE__, __LINE__,
419 "wait returned unexpected status 0x%x", status);
422 if (event == PTRACE_EVENT_FORK)
423 ourstatus->kind = TARGET_WAITKIND_FORKED;
424 else if (event == PTRACE_EVENT_VFORK)
425 ourstatus->kind = TARGET_WAITKIND_VFORKED;
427 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
429 ourstatus->value.related_pid = new_pid;
430 return inferior_ptid;
433 if (event == PTRACE_EVENT_EXEC)
435 ourstatus->kind = TARGET_WAITKIND_EXECD;
436 ourstatus->value.execd_pathname
437 = xstrdup (child_pid_to_exec_file (pid));
439 if (linux_parent_pid)
441 detach_breakpoints (linux_parent_pid);
442 ptrace (PTRACE_DETACH, linux_parent_pid, 0, 0);
444 linux_parent_pid = 0;
447 return inferior_ptid;
450 internal_error (__FILE__, __LINE__,
451 "unknown ptrace event %d", event);
456 child_insert_fork_catchpoint (int pid)
458 if (! linux_supports_tracefork ())
459 error ("Your system does not support fork catchpoints.");
465 child_insert_vfork_catchpoint (int pid)
467 if (!linux_supports_tracefork ())
468 error ("Your system does not support vfork catchpoints.");
474 child_insert_exec_catchpoint (int pid)
476 if (!linux_supports_tracefork ())
477 error ("Your system does not support exec catchpoints.");
486 int pid = PIDGET (inferior_ptid);
487 struct target_waitstatus last;
494 /* If we're stopped while forking and we haven't followed yet, kill the
495 other task. We need to do this first because the parent will be
496 sleeping if this is a vfork. */
498 get_last_target_status (&last_ptid, &last);
500 if (last.kind == TARGET_WAITKIND_FORKED
501 || last.kind == TARGET_WAITKIND_VFORKED)
503 ptrace (PT_KILL, last.value.related_pid);
504 ptrace_wait (null_ptid, &status);
507 /* Kill the current process. */
508 ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
509 ret = ptrace_wait (null_ptid, &status);
511 /* We might get a SIGCHLD instead of an exit status. This is
512 aggravated by the first kill above - a child has just died. */
514 while (ret == pid && WIFSTOPPED (status))
516 ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
517 ret = ptrace_wait (null_ptid, &status);
520 target_mourn_inferior ();