81a3bd48ac070ebc76b44bdae701471c7bc580cd
[debugger.git] / debugger.c
1 /* Copyright 2007, Red Hat Inc.  */
2
3 #include <unistd.h>
4 #include <assert.h>
5 #include <stdlib.h>
6 #include <signal.h>
7 #include <errno.h>
8 #include <stdio.h>
9 #include <sys/ptrace.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
12 #include <limits.h>
13 #include <string.h>
14 #include <time.h>
15
16 #include "debugger.h"
17
18
19 #if 0
20 #define USLEEP (1000000 / 2)
21 #endif
22 #define TIMEOUT_SECS 10
23
24
25 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
26
27 void delay (void)
28 {
29 #ifdef USLEEP
30   int i;
31
32   i = usleep (USLEEP);
33   assert (i == 0);
34 #endif
35 }
36
37 static __attribute__((__noreturn__)) void crash (void)
38 {
39 #if 0
40   void *array[0x100];
41   int count;
42 #endif
43   char command[256];
44
45   fputs (">>> CRASH START\n", stderr);
46 #if 0
47   count = backtrace (array, ARRAY_SIZE (array));
48   backtrace_symbols_fd (array, count, STDERR_FILENO);
49 #endif
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(),
54             (int) getpid());
55   system (command);
56   fputs (">>> CRASH FINISH\n", stderr);
57   abort ();
58 }
59
60 enum state
61   {
62     /* Separate the valid `1 << enum state' and `enum state' ranges.  */
63     STATE_FIRST = 4,
64     STATE_INSTABLE,
65     STATE_ENOENT,
66     STATE_SLEEPING,
67     STATE_RUNNING,
68     STATE_STOPPED,
69     STATE_PTRACED,
70     STATE_ZOMBIE,
71     STATE_LAST
72   };
73
74 static const char *state_to_name (enum state state)
75 {
76   switch (state)
77     {
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";
85       default: crash ();
86     }
87   /* NOTREACHED */
88   crash ();
89 }
90
91 static enum state state_get (pid_t pid)
92 {
93   char status_name[32];
94   char line[LINE_MAX];
95   FILE *f;
96   enum state found;
97
98   delay ();
99
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;
104   else if (f == NULL)
105     crash ();
106   else
107     {
108       int i;
109
110       found = STATE_INSTABLE;
111       while (errno = 0, fgets (line, sizeof (line), f) != NULL)
112         {
113           const char *const string = "State:\t";
114           const size_t length = sizeof "State:\t" - 1;
115
116           if (strncmp (line, string, length) != 0)
117             continue;
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;
128           else
129             {
130               fprintf (stderr, "Found an unknown state: %s", line + length);
131               crash ();
132             }
133         }
134       if (errno == ESRCH)
135         return found;
136       assert (found != STATE_INSTABLE);
137       i = fclose (f);
138       assert (i == 0);
139     }
140   return found;
141 }
142
143 #define STATE(pid, expect_mask) state ((pid), (expect_mask), #expect_mask )
144
145 static enum state state (pid_t pid, unsigned expect_mask, const char *expect_mask_string)
146 {
147   enum state found;
148   time_t timeout = time (NULL) + TIMEOUT_SECS;
149
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
159
160   do
161     {
162       found = state_get (pid);
163
164       if (((1 << found) & expect_mask) != 0)
165         return found;
166     }
167   while (time (NULL) < timeout);
168
169   fprintf (stderr, "Found for PID %d state %s but expecting (%s)\n",
170            (int) pid, state_to_name (found), expect_mask_string);
171   crash ();
172 }
173
174 /* Debugging only: Number of signal needing redelivery on PTRACE_ATTACH.  */
175 int attach_redelivered;
176
177 int attach (pid_t pid)
178 {
179   int i;
180   int status;
181   int stopped;
182
183   attach_redelivered = 0;
184
185   delay ();
186
187 #if 1
188   stopped = (STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING)
189                          | (1 << STATE_STOPPED)) == STATE_STOPPED);
190 #endif
191
192   i = ptrace (PTRACE_ATTACH, pid, NULL, NULL);
193   assert (i == 0);
194
195   /* FIXME: Why it does not work?
196      Disable also the STATE () call above.  */
197 #if 0
198   delay();
199
200   i = ptrace (PTRACE_CONT, pid, (void *) 1, (void *) SIGSTOP);
201   /* `STOPPED == 1' may be false, even if the process was not stopped.  */
202   if (i == 0)
203     stopped = 1;
204   else if (errno == ESRCH)
205     stopped = 0;
206   else
207     crash ();
208 #endif
209
210   for (;;)
211     {
212       delay ();
213
214       i = waitpid (pid, &status, 0);
215       assert (i == pid);
216       if (!WIFSTOPPED (status))
217         {
218           /* Process may have exited.  */
219           fprintf (stderr, "PID %d waitpid(2) status %d\n", (int) pid,
220                    status);
221           exit (EXIT_FAILURE);
222         }
223       if (WSTOPSIG (status) == SIGSTOP)
224         break;
225
226       if (attach_redelivered == 0)
227         attach_redelivered = WSTOPSIG (status);
228       else
229         attach_redelivered = -1;
230
231       delay ();
232
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));
237       assert (i == 0);
238     }
239
240   return stopped;
241 }
242
243 void detach (pid_t pid, int stopped)
244 {
245   int i;
246
247   delay ();
248
249   i = ptrace (PTRACE_DETACH, pid, NULL,
250               (void *) (unsigned long) (stopped ? SIGSTOP : 0));
251   assert (i == 0);
252
253   delay ();
254 }
255
256 #ifndef LIBRARY
257
258 int main (int argc, char **argv)
259 {
260   pid_t pid;
261   int stopped;
262
263   if (argc != 2)
264     {
265       fprintf (stderr, "Usage: %s <PID>\n", argv[0]);
266       exit (EXIT_FAILURE);
267     }
268
269   pid = atoi (argv[1]);
270
271   stopped = attach (pid);
272
273   detach (pid, stopped);
274
275   return EXIT_SUCCESS;
276 }
277
278 #endif /* !LIBRARY */