Hack some races.
[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     {
106       fprintf (stderr, "errno = %d\n", errno);
107       crash ();
108     }
109   else
110     {
111       int i;
112
113       found = STATE_INSTABLE;
114       while (errno = 0, fgets (line, sizeof (line), f) != NULL)
115         {
116           const char *const string = "State:\t";
117           const size_t length = sizeof "State:\t" - 1;
118
119           if (strncmp (line, string, length) != 0)
120             continue;
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;
131           else
132             {
133               fprintf (stderr, "Found an unknown state: %s", line + length);
134               crash ();
135             }
136         }
137       assert (found != STATE_INSTABLE || errno == ESRCH);
138       i = fclose (f);
139       assert (i == 0);
140     }
141   return found;
142 }
143
144 #define STATE(pid, expect_mask) state ((pid), (expect_mask), #expect_mask )
145
146 static enum state state (pid_t pid, unsigned expect_mask, const char *expect_mask_string)
147 {
148   enum state found;
149   time_t timeout = time (NULL) + TIMEOUT_SECS;
150
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
160
161   do
162     {
163       found = state_get (pid);
164
165       if (((1 << found) & expect_mask) != 0)
166         return found;
167     }
168   while (time (NULL) < timeout);
169
170   fprintf (stderr, "Found for PID %d state %s but expecting (%s)\n",
171            (int) pid, state_to_name (found), expect_mask_string);
172   crash ();
173 }
174
175 /* Debugging only: Number of signal needing redelivery on PTRACE_ATTACH.  */
176 int attach_redelivered;
177
178 int attach (pid_t pid)
179 {
180   int i;
181   int status;
182   int stopped;
183
184   attach_redelivered = 0;
185
186   delay ();
187
188 #if 1
189   stopped = (STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING)
190                          | (1 << STATE_STOPPED)) == STATE_STOPPED);
191 #endif
192
193   i = ptrace (PTRACE_ATTACH, pid, NULL, NULL);
194   assert (i == 0);
195
196   /* FIXME: Why it does not work?
197      Disable also the STATE () call above.  */
198 #if 0
199   delay();
200
201   i = ptrace (PTRACE_CONT, pid, (void *) 1, (void *) SIGSTOP);
202   /* `STOPPED == 1' may be false, even if the process was not stopped.  */
203   if (i == 0)
204     stopped = 1;
205   else if (errno == ESRCH)
206     stopped = 0;
207   else
208     crash ();
209 #endif
210
211   for (;;)
212     {
213       delay ();
214
215       i = waitpid (pid, &status, 0);
216       assert (i == pid);
217       if (!WIFSTOPPED (status))
218         {
219           /* Process may have exited.  */
220           fprintf (stderr, "PID %d waitpid(2) status %d\n", (int) pid,
221                    status);
222           exit (EXIT_FAILURE);
223         }
224       if (WSTOPSIG (status) == SIGSTOP)
225         break;
226
227       if (attach_redelivered == 0)
228         attach_redelivered = WSTOPSIG (status);
229       else
230         attach_redelivered = -1;
231
232       delay ();
233
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));
238       assert (i == 0);
239     }
240
241   return stopped;
242 }
243
244 void detach (pid_t pid, int stopped)
245 {
246   int i;
247
248   delay ();
249
250   i = ptrace (PTRACE_DETACH, pid, NULL,
251               (void *) (unsigned long) (stopped ? SIGSTOP : 0));
252   assert (i == 0);
253
254   delay ();
255 }
256
257 #ifndef LIBRARY
258
259 int main (int argc, char **argv)
260 {
261   pid_t pid;
262   int stopped;
263
264   if (argc != 2)
265     {
266       fprintf (stderr, "Usage: %s <PID>\n", argv[0]);
267       exit (EXIT_FAILURE);
268     }
269
270   pid = atoi (argv[1]);
271
272   stopped = attach (pid);
273
274   detach (pid, stopped);
275
276   return EXIT_SUCCESS;
277 }
278
279 #endif /* !LIBRARY */