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;
110 found = STATE_INSTABLE;
111 while (errno = 0, fgets (line, sizeof (line), f) != NULL)
113 const char *const string = "State:\t";
114 const size_t length = sizeof "State:\t" - 1;
116 if (strncmp (line, string, length) != 0)
118 if (strcmp (line + length, "S (sleeping)\n") == 0)
119 found = STATE_SLEEPING;
120 else if (strcmp (line + length, "R (running)\n") == 0)
121 found = STATE_RUNNING;
122 else if (strcmp (line + length, "T (stopped)\n") == 0)
123 found = STATE_STOPPED;
124 else if (strcmp (line + length, "T (tracing stop)\n") == 0)
125 found = STATE_PTRACED;
126 else if (strcmp (line + length, "Z (zombie)\n") == 0)
127 found = STATE_ZOMBIE;
130 fprintf (stderr, "Found an unknown state: %s", line + length);
136 assert (found != STATE_INSTABLE);
143 #define STATE(pid, expect_mask) state ((pid), (expect_mask), #expect_mask )
145 static enum state state (pid_t pid, unsigned expect_mask, const char *expect_mask_string)
148 time_t timeout = time (NULL) + TIMEOUT_SECS;
150 /* Sanity check `1 << enum state' was not misplaced with `enum state'. */
151 assert (1 << (STATE_FIRST + 1) >= STATE_LAST);
152 assert (expect_mask != 0);
153 #define MASK_UNDER_EXCLUSIVE(bit) ((1 << (bit)) - 1)
154 #define MASK_ABOVE_INCLUSIVE(bit) (~MASK_UNDER_EXCLUSIVE (bit))
155 assert ((expect_mask & MASK_UNDER_EXCLUSIVE (STATE_FIRST + 1)) == 0);
156 assert ((expect_mask & MASK_ABOVE_INCLUSIVE (STATE_LAST)) == 0);
157 #undef MASK_ABOVE_INCLUSIVE
158 #undef MASK_UNDER_EXCLUSIVE
162 found = state_get (pid);
164 if (((1 << found) & expect_mask) != 0)
167 while (time (NULL) < timeout);
169 fprintf (stderr, "Found for PID %d state %s but expecting (%s)\n",
170 (int) pid, state_to_name (found), expect_mask_string);
174 /* Debugging only: Number of signal needing redelivery on PTRACE_ATTACH. */
175 int attach_redelivered;
177 int attach (pid_t pid)
183 attach_redelivered = 0;
188 stopped = (STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING)
189 | (1 << STATE_STOPPED)) == STATE_STOPPED);
192 i = ptrace (PTRACE_ATTACH, pid, NULL, NULL);
195 /* FIXME: Why it does not work?
196 Disable also the STATE () call above. */
200 i = ptrace (PTRACE_CONT, pid, (void *) 1, (void *) SIGSTOP);
201 /* `STOPPED == 1' may be false, even if the process was not stopped. */
204 else if (errno == ESRCH)
214 i = waitpid (pid, &status, 0);
216 if (!WIFSTOPPED (status))
218 /* Process may have exited. */
219 fprintf (stderr, "PID %d waitpid(2) status %d\n", (int) pid,
223 if (WSTOPSIG (status) == SIGSTOP)
226 if (attach_redelivered == 0)
227 attach_redelivered = WSTOPSIG (status);
229 attach_redelivered = -1;
233 /* Re-deliver the signal received before SIGSTOP.
234 It happens with about only 1:200000 probability. */
235 i = ptrace (PTRACE_CONT, pid, (void *) 1,
236 (void *) (unsigned long) WSTOPSIG (status));
243 void detach (pid_t pid, int stopped)
249 i = ptrace (PTRACE_DETACH, pid, NULL,
250 (void *) (unsigned long) (stopped ? SIGSTOP : 0));
258 int main (int argc, char **argv)
265 fprintf (stderr, "Usage: %s <PID>\n", argv[0]);
269 pid = atoi (argv[1]);
271 stopped = attach (pid);
273 detach (pid, stopped);
278 #endif /* !LIBRARY */