1 /* Copyright 2007, Red Hat Inc. */
9 #include <sys/ptrace.h>
10 #include <sys/types.h>
18 #define USLEEP (1000000 / 2)
21 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
33 static __attribute__((__noreturn__)) void crash (void)
41 fputs (">>> CRASH START\n", stderr);
43 count = backtrace (array, ARRAY_SIZE (array));
44 backtrace_symbols_fd (array, count, STDERR_FILENO);
46 snprintf (command, sizeof (command), "echo -e \"bt\\nquit\""
47 " >/tmp/debugger.%d; gdb --batch -nx --command=/tmp/debugger.%d"
48 " /proc/%d/exe %d </dev/null;rm -f /tmp/debugger.%d",
49 (int) getpid(), (int) getpid(), (int) getpid(), (int) getpid(),
52 fputs (">>> CRASH FINISH\n", stderr);
58 /* Separate the valid `1 << enum state' and `enum state' ranges. */
69 static const char *state_to_name (enum state state)
73 case STATE_ENOENT: return "STATE_ENOENT";
74 case STATE_SLEEPING: return "STATE_SLEEPING";
75 case STATE_RUNNING: return "STATE_RUNNING";
76 case STATE_STOPPED: return "STATE_STOPPED";
77 case STATE_PTRACED: return "STATE_PTRACED";
78 case STATE_ZOMBIE: return "STATE_ZOMBIE";
85 #define STATE(pid, expect_mask) state ((pid), (expect_mask), #expect_mask )
87 static enum state state (pid_t pid, unsigned expect_mask, const char *expect_mask_string)
96 /* Sanity check `1 << enum state' was not misplaced with `enum state'. */
97 assert (1 << (STATE_FIRST + 1) >= STATE_LAST);
98 assert (expect_mask != 0);
99 #define MASK_UNDER_EXCLUSIVE(bit) ((1 << (bit)) - 1)
100 #define MASK_ABOVE_INCLUSIVE(bit) (~MASK_UNDER_EXCLUSIVE (bit))
101 assert ((expect_mask & MASK_UNDER_EXCLUSIVE (STATE_FIRST + 1)) == 0);
102 assert ((expect_mask & MASK_ABOVE_INCLUSIVE (STATE_LAST)) == 0);
103 #undef MASK_ABOVE_INCLUSIVE
104 #undef MASK_UNDER_EXCLUSIVE
106 snprintf (status_name, sizeof (status_name), "/proc/%d/status", (int) pid);
107 f = fopen (status_name, "r");
108 if (f == NULL && errno == ENOENT)
109 found = STATE_ENOENT;
116 found = STATE_ENOENT;
117 while (errno = 0, fgets (line, sizeof (line), f) != NULL)
119 const char *const string = "State:\t";
120 const size_t length = sizeof "State:\t" - 1;
122 if (strncmp (line, string, length) != 0)
124 if (strcmp (line + length, "S (sleeping)\n") == 0)
125 found = STATE_SLEEPING;
126 else if (strcmp (line + length, "R (running)\n") == 0)
127 found = STATE_RUNNING;
128 else if (strcmp (line + length, "T (stopped)\n") == 0)
129 found = STATE_STOPPED;
130 else if (strcmp (line + length, "T (tracing stop)\n") == 0)
131 found = STATE_PTRACED;
132 else if (strcmp (line + length, "Z (zombie)\n") == 0)
133 found = STATE_ZOMBIE;
136 fprintf (stderr, "Found an unknown state: %s", line + length);
140 assert (found != STATE_ENOENT);
144 if (((1 << found) & expect_mask) == 0)
146 fprintf (stderr, "Found for PID %d state %s but expecting (%s)\n",
147 (int) pid, state_to_name (found), expect_mask_string);
153 /* Debugging only: Number of signal needing redelivery on PTRACE_ATTACH. */
154 int attach_redelivered;
156 int attach (pid_t pid)
162 attach_redelivered = 0;
167 stopped = (STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING)
168 | (1 << STATE_STOPPED)) == STATE_STOPPED);
171 i = ptrace (PTRACE_ATTACH, pid, NULL, NULL);
174 /* FIXME: Why it does not work?
175 Disable also the STATE () call above. */
179 i = ptrace (PTRACE_CONT, pid, (void *) 1, (void *) SIGSTOP);
180 /* `STOPPED == 1' may be false, even if the process was not stopped. */
183 else if (errno == ESRCH)
193 i = waitpid (pid, &status, 0);
195 if (!WIFSTOPPED (status))
197 /* Process may have exited. */
198 fprintf (stderr, "PID %d waitpid(2) status %d\n", (int) pid,
202 if (WSTOPSIG (status) == SIGSTOP)
205 if (attach_redelivered == 0)
206 attach_redelivered = WSTOPSIG (status);
208 attach_redelivered = -1;
212 /* Re-deliver the signal received before SIGSTOP.
213 It happens with about only 1:200000 probability. */
214 i = ptrace (PTRACE_CONT, pid, (void *) 1,
215 (void *) (unsigned long) WSTOPSIG (status));
222 void detach (pid_t pid, int stopped)
228 i = ptrace (PTRACE_DETACH, pid, NULL,
229 (void *) (unsigned long) (stopped ? SIGSTOP : 0));
237 int main (int argc, char **argv)
244 fprintf (stderr, "Usage: %s <PID>\n", argv[0]);
248 pid = atoi (argv[1]);
250 stopped = attach (pid);
252 detach (pid, stopped);
257 #endif /* !LIBRARY */