1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/patches/gdb/6.3/730-debian_gdb-fix-tracefork-check.patch Wed May 23 21:08:24 2007 +0000
1.3 @@ -0,0 +1,225 @@
1.4 +Status: submitted for comments
1.5 +
1.6 +2004-11-12 Daniel Jacobowitz <dan@debian.org>
1.7 +
1.8 + * linux-nat.c (my_waitpid): New function.
1.9 + (linux_test_for_tracefork): Make more robust and verbose. Take
1.10 + an ORIGINAL_PID argument and test for PTRACE_SETOPTIONS first.
1.11 + (linux_supports_tracefork, linux_supports_tracevforkdone): Take a PID
1.12 + argument. Update calls to linux_test_for_tracefork.
1.13 + (linux_enable_event_reporting, child_follow_fork)
1.14 + (child_insert_fork_catchpoint, child_insert_vfork_catchpoint)
1.15 + (child_insert_exec_catchpoint): Update calls to
1.16 + linux_supports_tracefork and linux_supports_tracevforkdone.
1.17 +
1.18 +Index: gdb-6.3/gdb/linux-nat.c
1.19 +===================================================================
1.20 +--- gdb-6.3.orig/gdb/linux-nat.c 2004-10-08 16:29:47.000000000 -0400
1.21 ++++ gdb-6.3/gdb/linux-nat.c 2004-11-13 16:41:51.368720845 -0500
1.22 +@@ -150,18 +150,47 @@ linux_tracefork_child (void)
1.23 + exit (0);
1.24 + }
1.25 +
1.26 +-/* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. We
1.27 ++/* Wrapper function for waitpid which handles EINTR. */
1.28 ++
1.29 ++static int
1.30 ++my_waitpid (int pid, int *status, int flags)
1.31 ++{
1.32 ++ int ret;
1.33 ++ do
1.34 ++ {
1.35 ++ ret = waitpid (pid, status, flags);
1.36 ++ }
1.37 ++ while (ret == -1 && errno == EINTR);
1.38 ++
1.39 ++ return ret;
1.40 ++}
1.41 ++
1.42 ++/* Determine if PTRACE_O_TRACEFORK can be used to follow fork events.
1.43 ++
1.44 ++ First, we try to enable fork tracing on ORIGINAL_PID. If this fails,
1.45 ++ we know that the feature is not available. This may change the tracing
1.46 ++ options for ORIGINAL_PID, but we'll be setting them shortly anyway.
1.47 ++
1.48 ++ However, if it succeeds, we don't know for sure that the feature is
1.49 ++ available; old versions of PTRACE_SETOPTIONS ignored unknown options. We
1.50 + create a child process, attach to it, use PTRACE_SETOPTIONS to enable
1.51 +- fork tracing, and let it fork. If the process exits, we assume that
1.52 +- we can't use TRACEFORK; if we get the fork notification, and we can
1.53 +- extract the new child's PID, then we assume that we can. */
1.54 ++ fork tracing, and let it fork. If the process exits, we assume that we
1.55 ++ can't use TRACEFORK; if we get the fork notification, and we can extract
1.56 ++ the new child's PID, then we assume that we can. */
1.57 +
1.58 + static void
1.59 +-linux_test_for_tracefork (void)
1.60 ++linux_test_for_tracefork (int original_pid)
1.61 + {
1.62 + int child_pid, ret, status;
1.63 + long second_pid;
1.64 +
1.65 ++ linux_supports_tracefork_flag = 0;
1.66 ++ linux_supports_tracevforkdone_flag = 0;
1.67 ++
1.68 ++ ret = ptrace (PTRACE_SETOPTIONS, original_pid, 0, PTRACE_O_TRACEFORK);
1.69 ++ if (ret != 0)
1.70 ++ return;
1.71 ++
1.72 + child_pid = fork ();
1.73 + if (child_pid == -1)
1.74 + perror_with_name ("linux_test_for_tracefork: fork");
1.75 +@@ -169,7 +198,7 @@ linux_test_for_tracefork (void)
1.76 + if (child_pid == 0)
1.77 + linux_tracefork_child ();
1.78 +
1.79 +- ret = waitpid (child_pid, &status, 0);
1.80 ++ ret = my_waitpid (child_pid, &status, 0);
1.81 + if (ret == -1)
1.82 + perror_with_name ("linux_test_for_tracefork: waitpid");
1.83 + else if (ret != child_pid)
1.84 +@@ -177,13 +206,23 @@ linux_test_for_tracefork (void)
1.85 + if (! WIFSTOPPED (status))
1.86 + error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
1.87 +
1.88 +- linux_supports_tracefork_flag = 0;
1.89 +-
1.90 + ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
1.91 + if (ret != 0)
1.92 + {
1.93 +- ptrace (PTRACE_KILL, child_pid, 0, 0);
1.94 +- waitpid (child_pid, &status, 0);
1.95 ++ ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
1.96 ++ if (ret != 0)
1.97 ++ {
1.98 ++ warning ("linux_test_for_tracefork: failed to kill child");
1.99 ++ return;
1.100 ++ }
1.101 ++
1.102 ++ ret = my_waitpid (child_pid, &status, 0);
1.103 ++ if (ret != child_pid)
1.104 ++ warning ("linux_test_for_tracefork: failed to wait for killed child");
1.105 ++ else if (!WIFSIGNALED (status))
1.106 ++ warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
1.107 ++ "killed child", status);
1.108 ++
1.109 + return;
1.110 + }
1.111 +
1.112 +@@ -192,8 +231,12 @@ linux_test_for_tracefork (void)
1.113 + PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORKDONE);
1.114 + linux_supports_tracevforkdone_flag = (ret == 0);
1.115 +
1.116 +- ptrace (PTRACE_CONT, child_pid, 0, 0);
1.117 +- ret = waitpid (child_pid, &status, 0);
1.118 ++ ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
1.119 ++ if (ret != 0)
1.120 ++ warning ("linux_test_for_tracefork: failed to resume child");
1.121 ++
1.122 ++ ret = my_waitpid (child_pid, &status, 0);
1.123 ++
1.124 + if (ret == child_pid && WIFSTOPPED (status)
1.125 + && status >> 16 == PTRACE_EVENT_FORK)
1.126 + {
1.127 +@@ -204,34 +247,38 @@ linux_test_for_tracefork (void)
1.128 + int second_status;
1.129 +
1.130 + linux_supports_tracefork_flag = 1;
1.131 +- waitpid (second_pid, &second_status, 0);
1.132 +- ptrace (PTRACE_DETACH, second_pid, 0, 0);
1.133 ++ my_waitpid (second_pid, &second_status, 0);
1.134 ++ ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
1.135 ++ if (ret != 0)
1.136 ++ warning ("linux_test_for_tracefork: failed to kill second child");
1.137 + }
1.138 + }
1.139 ++ else
1.140 ++ warning ("linux_test_for_tracefork: unexpected result from waitpid "
1.141 ++ "(%d, status 0x%x)", ret, status);
1.142 +
1.143 +- if (WIFSTOPPED (status))
1.144 +- {
1.145 +- ptrace (PTRACE_DETACH, child_pid, 0, 0);
1.146 +- waitpid (child_pid, &status, 0);
1.147 +- }
1.148 ++ ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
1.149 ++ if (ret != 0)
1.150 ++ warning ("linux_test_for_tracefork: failed to kill child");
1.151 ++ my_waitpid (child_pid, &status, 0);
1.152 + }
1.153 +
1.154 + /* Return non-zero iff we have tracefork functionality available.
1.155 + This function also sets linux_supports_tracefork_flag. */
1.156 +
1.157 + static int
1.158 +-linux_supports_tracefork (void)
1.159 ++linux_supports_tracefork (int pid)
1.160 + {
1.161 + if (linux_supports_tracefork_flag == -1)
1.162 +- linux_test_for_tracefork ();
1.163 ++ linux_test_for_tracefork (pid);
1.164 + return linux_supports_tracefork_flag;
1.165 + }
1.166 +
1.167 + static int
1.168 +-linux_supports_tracevforkdone (void)
1.169 ++linux_supports_tracevforkdone (int pid)
1.170 + {
1.171 + if (linux_supports_tracefork_flag == -1)
1.172 +- linux_test_for_tracefork ();
1.173 ++ linux_test_for_tracefork (pid);
1.174 + return linux_supports_tracevforkdone_flag;
1.175 + }
1.176 +
1.177 +@@ -242,12 +289,12 @@ linux_enable_event_reporting (ptid_t pti
1.178 + int pid = ptid_get_pid (ptid);
1.179 + int options;
1.180 +
1.181 +- if (! linux_supports_tracefork ())
1.182 ++ if (! linux_supports_tracefork (pid))
1.183 + return;
1.184 +
1.185 + options = PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC
1.186 + | PTRACE_O_TRACECLONE;
1.187 +- if (linux_supports_tracevforkdone ())
1.188 ++ if (linux_supports_tracevforkdone (pid))
1.189 + options |= PTRACE_O_TRACEVFORKDONE;
1.190 +
1.191 + /* Do not enable PTRACE_O_TRACEEXIT until GDB is more prepared to support
1.192 +@@ -308,7 +355,8 @@ child_follow_fork (int follow_child)
1.193 +
1.194 + if (has_vforked)
1.195 + {
1.196 +- if (linux_supports_tracevforkdone ())
1.197 ++ gdb_assert (linux_supports_tracefork_flag >= 0);
1.198 ++ if (linux_supports_tracevforkdone (0))
1.199 + {
1.200 + int status;
1.201 +
1.202 +@@ -476,7 +524,7 @@ linux_handle_extended_wait (int pid, int
1.203 + int
1.204 + child_insert_fork_catchpoint (int pid)
1.205 + {
1.206 +- if (! linux_supports_tracefork ())
1.207 ++ if (! linux_supports_tracefork (pid))
1.208 + error ("Your system does not support fork catchpoints.");
1.209 +
1.210 + return 0;
1.211 +@@ -485,7 +533,7 @@ child_insert_fork_catchpoint (int pid)
1.212 + int
1.213 + child_insert_vfork_catchpoint (int pid)
1.214 + {
1.215 +- if (!linux_supports_tracefork ())
1.216 ++ if (!linux_supports_tracefork (pid))
1.217 + error ("Your system does not support vfork catchpoints.");
1.218 +
1.219 + return 0;
1.220 +@@ -494,7 +542,7 @@ child_insert_vfork_catchpoint (int pid)
1.221 + int
1.222 + child_insert_exec_catchpoint (int pid)
1.223 + {
1.224 +- if (!linux_supports_tracefork ())
1.225 ++ if (!linux_supports_tracefork (pid))
1.226 + error ("Your system does not support exec catchpoints.");
1.227 +
1.228 + return 0;