#include "defs.h"
#include "inferior.h"
#include "target.h"
#include "gdb_wait.h"
#include <sys/ptrace.h>
#include "linux-nat.h"
#ifndef PTRACE_EVENT_FORK
#define PTRACE_SETOPTIONS 0x4200
#define PTRACE_GETEVENTMSG 0x4201
#define PTRACE_O_TRACESYSGOOD 0x00000001
#define PTRACE_O_TRACEFORK 0x00000002
#define PTRACE_O_TRACEVFORK 0x00000004
#define PTRACE_O_TRACECLONE 0x00000008
#define PTRACE_O_TRACEEXEC 0x00000010
#define PTRACE_O_TRACEVFORKDONE 0x00000020
#define PTRACE_O_TRACEEXIT 0x00000040
#define PTRACE_EVENT_FORK 1
#define PTRACE_EVENT_VFORK 2
#define PTRACE_EVENT_CLONE 3
#define PTRACE_EVENT_EXEC 4
#define PTRACE_EVENT_VFORKDONE 5
#define PTRACE_EVENT_EXIT 6
#endif
#ifndef __WALL
#define __WALL 0x40000000
#endif
extern struct target_ops child_ops;
static int linux_parent_pid;
struct simple_pid_list
{
int pid;
struct simple_pid_list *next;
};
struct simple_pid_list *stopped_pids;
static int linux_supports_tracefork_flag = -1;
static int linux_supports_tracevforkdone_flag = -1;
static void
add_to_pid_list (struct simple_pid_list **listp, int pid)
{
struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
new_pid->pid = pid;
new_pid->next = *listp;
*listp = new_pid;
}
static int
pull_pid_from_list (struct simple_pid_list **listp, int pid)
{
struct simple_pid_list **p;
for (p = listp; *p != NULL; p = &(*p)->next)
if ((*p)->pid == pid)
{
struct simple_pid_list *next = (*p)->next;
xfree (*p);
*p = next;
return 1;
}
return 0;
}
void
linux_record_stopped_pid (int pid)
{
add_to_pid_list (&stopped_pids, pid);
}
static void
linux_tracefork_child (void)
{
int ret;
ptrace (PTRACE_TRACEME, 0, 0, 0);
kill (getpid (), SIGSTOP);
fork ();
exit (0);
}
static void
linux_test_for_tracefork (void)
{
int child_pid, ret, status;
long second_pid;
child_pid = fork ();
if (child_pid == -1)
perror_with_name ("linux_test_for_tracefork: fork");
if (child_pid == 0)
linux_tracefork_child ();
ret = waitpid (child_pid, &status, 0);
if (ret == -1)
perror_with_name ("linux_test_for_tracefork: waitpid");
else if (ret != child_pid)
error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret);
if (! WIFSTOPPED (status))
error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
linux_supports_tracefork_flag = 0;
ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
if (ret != 0)
{
ptrace (PTRACE_KILL, child_pid, 0, 0);
waitpid (child_pid, &status, 0);
return;
}
ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0,
PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE);
linux_supports_tracevforkdone_flag = (ret == 0);
ptrace (PTRACE_CONT, child_pid, 0, 0);
ret = waitpid (child_pid, &status, 0);
if (ret == child_pid && WIFSTOPPED (status)
&& status >> 16 == PTRACE_EVENT_FORK)
{
second_pid = 0;
ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
if (ret == 0 && second_pid != 0)
{
int second_status;
linux_supports_tracefork_flag = 1;
waitpid (second_pid, &second_status, 0);
ptrace (PTRACE_DETACH, second_pid, 0, 0);
}
}
if (WIFSTOPPED (status))
{
ptrace (PTRACE_DETACH, child_pid, 0, 0);
waitpid (child_pid, &status, 0);
}
}
static int
linux_supports_tracefork (void)
{
if (linux_supports_tracefork_flag == -1)
linux_test_for_tracefork ();
return linux_supports_tracefork_flag;
}
static int
linux_supports_tracevforkdone (void)
{
if (linux_supports_tracefork_flag == -1)
linux_test_for_tracefork ();
return linux_supports_tracevforkdone_flag;
}
void
linux_enable_event_reporting (ptid_t ptid)
{
int pid = ptid_get_pid (ptid);
int options;
if (! linux_supports_tracefork ())
return;
options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC;
if (linux_supports_tracevforkdone ())
options |= PTRACE_O_TRACEVFORKDONE;
ptrace (PTRACE_SETOPTIONS, pid, 0, options);
}
void
child_post_attach (int pid)
{
linux_enable_event_reporting (pid_to_ptid (pid));
}
void
linux_child_post_startup_inferior (ptid_t ptid)
{
linux_enable_event_reporting (ptid);
}
#ifndef LINUX_CHILD_POST_STARTUP_INFERIOR
void
child_post_startup_inferior (ptid_t ptid)
{
linux_child_post_startup_inferior (ptid);
}
#endif
int
child_follow_fork (int follow_child)
{
ptid_t last_ptid;
struct target_waitstatus last_status;
int has_vforked;
int parent_pid, child_pid;
get_last_target_status (&last_ptid, &last_status);
has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED);
parent_pid = ptid_get_pid (last_ptid);
child_pid = last_status.value.related_pid;
if (! follow_child)
{
detach_breakpoints (child_pid);
fprintf_filtered (gdb_stdout,
"Detaching after fork from child process %d.\n",
child_pid);
ptrace (PTRACE_DETACH, child_pid, 0, 0);
if (has_vforked)
{
if (linux_supports_tracevforkdone ())
{
int status;
ptrace (PTRACE_CONT, parent_pid, 0, 0);
waitpid (parent_pid, &status, __WALL);
if ((status >> 16) != PTRACE_EVENT_VFORKDONE)
warning ("Unexpected waitpid result %06x when waiting for "
"vfork-done", status);
}
else
{
usleep (10000);
}
reattach_breakpoints (parent_pid);
}
}
else
{
char child_pid_spelling[40];
if (! has_vforked)
detach_breakpoints (child_pid);
remove_breakpoints ();
fprintf_filtered (gdb_stdout,
"Attaching after fork to child process %d.\n",
child_pid);
if (has_vforked)
linux_parent_pid = parent_pid;
else
target_detach (NULL, 0);
inferior_ptid = pid_to_ptid (child_pid);
push_target (&child_ops);
follow_inferior_reset_breakpoints ();
}
return 0;
}
ptid_t
linux_handle_extended_wait (int pid, int status,
struct target_waitstatus *ourstatus)
{
int event = status >> 16;
if (event == PTRACE_EVENT_CLONE)
internal_error (__FILE__, __LINE__,
"unexpected clone event");
if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
{
unsigned long new_pid;
int ret;
ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
if (! pull_pid_from_list (&stopped_pids, new_pid))
{
do {
ret = waitpid (new_pid, &status, 0);
} while (ret == -1 && errno == EINTR);
if (ret == -1)
perror_with_name ("waiting for new child");
else if (ret != new_pid)
internal_error (__FILE__, __LINE__,
"wait returned unexpected PID %d", ret);
else if (!WIFSTOPPED (status) || WSTOPSIG (status) != SIGSTOP)
internal_error (__FILE__, __LINE__,
"wait returned unexpected status 0x%x", status);
}
ourstatus->kind = (event == PTRACE_EVENT_FORK)
? TARGET_WAITKIND_FORKED : TARGET_WAITKIND_VFORKED;
ourstatus->value.related_pid = new_pid;
return inferior_ptid;
}
if (event == PTRACE_EVENT_EXEC)
{
ourstatus->kind = TARGET_WAITKIND_EXECD;
ourstatus->value.execd_pathname
= xstrdup (child_pid_to_exec_file (pid));
if (linux_parent_pid)
{
detach_breakpoints (linux_parent_pid);
ptrace (PTRACE_DETACH, linux_parent_pid, 0, 0);
linux_parent_pid = 0;
}
return inferior_ptid;
}
internal_error (__FILE__, __LINE__,
"unknown ptrace event %d", event);
}
int
child_insert_fork_catchpoint (int pid)
{
if (! linux_supports_tracefork ())
error ("Your system does not support fork catchpoints.");
return 0;
}
int
child_insert_vfork_catchpoint (int pid)
{
if (!linux_supports_tracefork ())
error ("Your system does not support vfork catchpoints.");
return 0;
}
int
child_insert_exec_catchpoint (int pid)
{
if (!linux_supports_tracefork ())
error ("Your system does not support exec catchpoints.");
return 0;
}
void
kill_inferior (void)
{
int status;
int pid = PIDGET (inferior_ptid);
struct target_waitstatus last;
ptid_t last_ptid;
int ret;
if (pid == 0)
return;
get_last_target_status (&last_ptid, &last);
if (last.kind == TARGET_WAITKIND_FORKED
|| last.kind == TARGET_WAITKIND_VFORKED)
{
ptrace (PT_KILL, last.value.related_pid);
ptrace_wait (null_ptid, &status);
}
ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
ret = ptrace_wait (null_ptid, &status);
while (ret == pid && WIFSTOPPED (status))
{
ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0);
ret = ptrace_wait (null_ptid, &status);
}
target_mourn_inferior ();
}