1 /* Copyright 2007, Red Hat Inc. */
9 #include <sys/ptrace.h>
10 #include <sys/types.h>
20 #define USLEEP (1000000 / 2)
22 #define TIMEOUT_SECS 10
25 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
37 static __attribute__((__noreturn__)) void crash (void)
45 fputs (">>> CRASH START\n", stderr);
47 count = backtrace (array, ARRAY_SIZE (array));
48 backtrace_symbols_fd (array, count, STDERR_FILENO);
50 snprintf (command, sizeof (command), "echo -e \"bt\\nquit\""
51 " >/tmp/debugger.%d; gdb --batch -nx --command=/tmp/debugger.%d"
52 " /proc/%d/exe %d </dev/null;rm -f /tmp/debugger.%d",
53 (int) getpid(), (int) getpid(), (int) getpid(), (int) getpid(),
56 fputs (">>> CRASH FINISH\n", stderr);
62 /* Separate the valid `1 << enum state' and `enum state' ranges. */
74 static const char *state_to_name (enum state state)
78 case STATE_INSTABLE: return "STATE_INSTABLE";
79 case STATE_ENOENT: return "STATE_ENOENT";
80 case STATE_SLEEPING: return "STATE_SLEEPING";
81 case STATE_RUNNING: return "STATE_RUNNING";
82 case STATE_STOPPED: return "STATE_STOPPED";
83 case STATE_PTRACED: return "STATE_PTRACED";
84 case STATE_ZOMBIE: return "STATE_ZOMBIE";
91 static enum state state_get (pid_t pid)
100 snprintf (status_name, sizeof (status_name), "/proc/%d/status", (int) pid);
101 f = fopen (status_name, "r");
102 if (f == NULL && errno == ENOENT)
103 found = STATE_ENOENT;
106 fprintf (stderr, "errno = %d\n", errno);
113 found = STATE_INSTABLE;
114 while (errno = 0, fgets (line, sizeof (line), f) != NULL)
116 const char *const string = "State:\t";
117 const size_t length = sizeof "State:\t" - 1;
119 if (strncmp (line, string, length) != 0)
121 if (strcmp (line + length, "S (sleeping)\n") == 0)
122 found = STATE_SLEEPING;
123 else if (strcmp (line + length, "R (running)\n") == 0)
124 found = STATE_RUNNING;
125 else if (strcmp (line + length, "T (stopped)\n") == 0)
126 found = STATE_STOPPED;
127 else if (strcmp (line + length, "T (tracing stop)\n") == 0)
128 found = STATE_PTRACED;
129 else if (strcmp (line + length, "Z (zombie)\n") == 0)
130 found = STATE_ZOMBIE;
133 fprintf (stderr, "Found an unknown state: %s", line + length);
137 assert (found != STATE_INSTABLE || errno == ESRCH);
144 #define STATE(pid, expect_mask) state ((pid), (expect_mask), #expect_mask )
146 static enum state state (pid_t pid, unsigned expect_mask, const char *expect_mask_string)
149 time_t timeout = time (NULL) + TIMEOUT_SECS;
151 /* Sanity check `1 << enum state' was not misplaced with `enum state'. */
152 assert (1 << (STATE_FIRST + 1) >= STATE_LAST);
153 assert (expect_mask != 0);
154 #define MASK_UNDER_EXCLUSIVE(bit) ((1 << (bit)) - 1)
155 #define MASK_ABOVE_INCLUSIVE(bit) (~MASK_UNDER_EXCLUSIVE (bit))
156 assert ((expect_mask & MASK_UNDER_EXCLUSIVE (STATE_FIRST + 1)) == 0);
157 assert ((expect_mask & MASK_ABOVE_INCLUSIVE (STATE_LAST)) == 0);
158 #undef MASK_ABOVE_INCLUSIVE
159 #undef MASK_UNDER_EXCLUSIVE
163 found = state_get (pid);
165 if (((1 << found) & expect_mask) != 0)
168 while (time (NULL) < timeout);
170 fprintf (stderr, "Found for PID %d state %s but expecting (%s)\n",
171 (int) pid, state_to_name (found), expect_mask_string);
175 /* Debugging only: Number of signal needing redelivery on PTRACE_ATTACH. */
176 int attach_redelivered;
178 int attach (pid_t pid)
184 attach_redelivered = 0;
189 stopped = (STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING)
190 | (1 << STATE_STOPPED)) == STATE_STOPPED);
193 i = ptrace (PTRACE_ATTACH, pid, NULL, NULL);
196 /* FIXME: Why it does not work?
197 Disable also the STATE () call above. */
201 i = ptrace (PTRACE_CONT, pid, (void *) 1, (void *) SIGSTOP);
202 /* `STOPPED == 1' may be false, even if the process was not stopped. */
205 else if (errno == ESRCH)
215 i = waitpid (pid, &status, 0);
217 if (!WIFSTOPPED (status))
219 /* Process may have exited. */
220 fprintf (stderr, "PID %d waitpid(2) status %d\n", (int) pid,
224 if (WSTOPSIG (status) == SIGSTOP)
227 if (attach_redelivered == 0)
228 attach_redelivered = WSTOPSIG (status);
230 attach_redelivered = -1;
234 /* Re-deliver the signal received before SIGSTOP.
235 It happens with about only 1:200000 probability. */
236 i = ptrace (PTRACE_CONT, pid, (void *) 1,
237 (void *) (unsigned long) WSTOPSIG (status));
244 void detach (pid_t pid, int stopped)
250 i = ptrace (PTRACE_DETACH, pid, NULL,
251 (void *) (unsigned long) (stopped ? SIGSTOP : 0));
259 int main (int argc, char **argv)
266 fprintf (stderr, "Usage: %s <PID>\n", argv[0]);
270 pid = atoi (argv[1]);
272 stopped = attach (pid);
274 detach (pid, stopped);
279 #endif /* !LIBRARY */