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 20
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. */
75 static const char *state_to_name (enum state state)
79 case STATE_INSTABLE: return "STATE_INSTABLE";
80 case STATE_ENOENT: return "STATE_ENOENT";
81 case STATE_SLEEPING: return "STATE_SLEEPING";
82 case STATE_RUNNING: return "STATE_RUNNING";
83 case STATE_STOPPED: return "STATE_STOPPED";
84 case STATE_PTRACED: return "STATE_PTRACED";
85 case STATE_ZOMBIE: return "STATE_ZOMBIE";
86 case STATE_DEAD: return "STATE_DEAD";
93 static enum state state_get (pid_t pid)
102 snprintf (status_name, sizeof (status_name), "/proc/%d/status", (int) pid);
103 f = fopen (status_name, "r");
104 if (f == NULL && errno == ENOENT)
105 found = STATE_ENOENT;
108 fprintf (stderr, "errno = %d\n", errno);
115 found = STATE_INSTABLE;
116 while (errno = 0, fgets (line, sizeof (line), f) != NULL)
118 const char *const string = "State:\t";
119 const size_t length = sizeof "State:\t" - 1;
121 if (strncmp (line, string, length) != 0)
123 if (strcmp (line + length, "S (sleeping)\n") == 0)
124 found = STATE_SLEEPING;
125 else if (strcmp (line + length, "R (running)\n") == 0)
126 found = STATE_RUNNING;
127 else if (strcmp (line + length, "T (stopped)\n") == 0)
128 found = STATE_STOPPED;
129 else if (strcmp (line + length, "T (tracing stop)\n") == 0)
130 found = STATE_PTRACED;
131 else if (strcmp (line + length, "Z (zombie)\n") == 0)
132 found = STATE_ZOMBIE;
133 /* FIXME: What does it mean? */
134 else if (strcmp (line + length, "X (dead)\n") == 0)
138 fprintf (stderr, "Found an unknown state: %s", line + length);
142 assert (found != STATE_INSTABLE || errno == ESRCH);
149 #define STATE(pid, expect_mask) state ((pid), (expect_mask), #expect_mask )
151 static enum state state (pid_t pid, unsigned expect_mask, const char *expect_mask_string)
154 time_t timeout = time (NULL) + TIMEOUT_SECS;
156 /* Sanity check `1 << enum state' was not misplaced with `enum state'. */
157 assert (1 << (STATE_FIRST + 1) >= STATE_LAST);
158 assert (expect_mask != 0);
159 #define MASK_UNDER_EXCLUSIVE(bit) ((1 << (bit)) - 1)
160 #define MASK_ABOVE_INCLUSIVE(bit) (~MASK_UNDER_EXCLUSIVE (bit))
161 assert ((expect_mask & MASK_UNDER_EXCLUSIVE (STATE_FIRST + 1)) == 0);
162 assert ((expect_mask & MASK_ABOVE_INCLUSIVE (STATE_LAST)) == 0);
163 #undef MASK_ABOVE_INCLUSIVE
164 #undef MASK_UNDER_EXCLUSIVE
168 found = state_get (pid);
170 if (((1 << found) & expect_mask) != 0)
173 while (time (NULL) < timeout);
175 fprintf (stderr, "Found for PID %d state %s but expecting (%s)\n",
176 (int) pid, state_to_name (found), expect_mask_string);
180 /* Debugging only: Number of signal needing redelivery on PTRACE_ATTACH. */
181 int attach_redelivered;
183 int attach (pid_t pid)
189 attach_redelivered = 0;
194 stopped = (STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING)
195 | (1 << STATE_STOPPED)) == STATE_STOPPED);
198 i = ptrace (PTRACE_ATTACH, pid, NULL, NULL);
201 /* FIXME: Why it does not work?
202 Disable also the STATE () call above. */
206 i = ptrace (PTRACE_CONT, pid, (void *) 1, (void *) SIGSTOP);
207 /* `STOPPED == 1' may be false, even if the process was not stopped. */
210 else if (errno == ESRCH)
220 i = waitpid (pid, &status, 0);
222 if (!WIFSTOPPED (status))
224 /* Process may have exited. */
225 fprintf (stderr, "PID %d waitpid(2) status %d\n", (int) pid,
229 if (WSTOPSIG (status) == SIGSTOP)
232 if (attach_redelivered == 0)
233 attach_redelivered = WSTOPSIG (status);
235 attach_redelivered = -1;
239 /* Re-deliver the signal received before SIGSTOP.
240 It happens with about only 1:200000 probability. */
241 i = ptrace (PTRACE_CONT, pid, (void *) 1,
242 (void *) (unsigned long) WSTOPSIG (status));
249 void detach (pid_t pid, int stopped)
255 i = ptrace (PTRACE_DETACH, pid, NULL,
256 (void *) (unsigned long) (stopped ? SIGSTOP : 0));
264 int main (int argc, char **argv)
271 fprintf (stderr, "Usage: %s <PID>\n", argv[0]);
275 pid = atoi (argv[1]);
277 stopped = attach (pid);
279 detach (pid, stopped);
284 #endif /* !LIBRARY */