Make tkill() compatible with FC6+.
[debugger.git] / testsuite.c
1 /* Copyright 2007, Red Hat Inc.  */
2
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <assert.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <unistd.h>
9 #include <limits.h>
10 #include <string.h>
11 #include <pthread.h>
12 #include <sys/syscall.h>
13
14 #include "debugger.h"
15
16 #define LIBRARY 1
17 #include "debugger.c"
18
19
20 #define tkill(tid, sig) syscall (SYS_tkill, (tid), (sig))
21
22
23 static int attach_checked (pid_t pid, int redelivered_expect)
24 {
25   int stopped;
26
27   STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING) | (1 << STATE_STOPPED));
28   stopped = attach (pid);
29   if (attach_redelivered != redelivered_expect)
30     {
31       fprintf (stderr, "Expecting redelivery of %d but found %d\n",
32                redelivered_expect, attach_redelivered);
33       abort ();
34     }
35   /* FIXME: Why also STATE_STOPPED?  */
36   STATE (pid, (1 << STATE_PTRACED) | (1 << STATE_STOPPED));
37   return stopped;
38 }
39
40 static void detach_checked (pid_t pid, int stopped)
41 {
42   /* FIXME: Why STATE_STOPPED?  */
43   STATE (pid, (stopped ? 1 << STATE_STOPPED : 1 << STATE_PTRACED));
44   detach (pid, stopped);
45   STATE (pid, (stopped ? 1 << STATE_STOPPED : (1 << STATE_SLEEPING) | (1 << STATE_RUNNING)));
46 }
47
48 struct registry
49   {
50     struct registry *next;
51     pid_t pid;
52   };
53 struct registry *registry_list;
54
55 static void registry_add (pid_t pid)
56 {
57   struct registry *new;
58
59   new = malloc (sizeof (*new));
60   assert (new != NULL);
61   new->pid = pid;
62   new->next = registry_list;
63   registry_list = new;
64 }
65
66 static void registry_remove (pid_t pid)
67 {
68   struct registry **iter_pointer;
69
70   for (iter_pointer = &registry_list; *iter_pointer != NULL;
71        iter_pointer = &(*iter_pointer)->next)
72     {
73       struct registry *found = *iter_pointer;
74       if (found->pid != pid)
75         continue;
76
77       *iter_pointer = found->next;
78       free (found);
79       return;
80     }
81   abort ();
82 }
83
84 static void registry_atexit (void)
85 {
86   struct registry *iter;
87
88   for (iter = registry_list; iter != NULL; iter = iter->next)
89     {
90       tkill (iter->pid, SIGCONT);
91       tkill (iter->pid, SIGKILL);
92       kill (iter->pid, SIGKILL);
93     }
94 }
95
96 static void registry_cleanup (void)
97 {
98   struct registry *iter;
99   pid_t pid;
100
101   registry_atexit ();
102   while ((pid = wait (NULL)) != -1)
103     {
104       for (iter = registry_list; iter != NULL; iter = iter->next)
105         if (iter->pid == pid)
106           break;
107       assert (iter != NULL);
108     }
109   assert (errno == ECHILD);
110   while (registry_list)
111     {
112       iter = registry_list;
113       registry_list = iter->next;
114       free (iter);
115     }
116 }
117
118 static void registry_handler (int signo)
119 {
120   signal (signo, SIG_DFL);
121   registry_atexit ();
122   raise (signo);
123 }
124
125 static void child_pause (void)
126 {
127   for (;;)
128     pause ();
129   /* NOTREACHED */
130   abort ();
131 }
132
133 static void child_alrm_handler (int signo)
134 {
135   assert (signo == SIGALRM);
136   raise (SIGALRM);
137 }
138
139 static void child_alrm (void)
140 {
141   void (*handler_orig) (int signo);
142 #if 0
143   int i;
144   sigset_t oldset;
145 #endif
146
147   handler_orig = signal (SIGALRM, child_alrm_handler);
148   assert (handler_orig == SIG_DFL);
149
150 #if 0
151   i = sigprocmask (SIG_BLOCK, NULL, &oldset);
152   assert (i == 0);
153   printf ("sigprocmask () -> sigismember (SIGALRM) == %d\n", sigismember (&oldset, SIGALRM));
154 #endif
155
156   for (;;)
157     pause ();
158   /* NOTREACHED */
159   abort ();
160 }
161
162 static pid_t spawn (void *(*child) (void *data, void *input), void *data,
163                     void *input, int fd_close)
164 {
165   pid_t pid;
166
167   pid = fork();
168   switch (pid)
169     {
170       case -1:
171         perror ("fork()");
172         exit (EXIT_FAILURE);
173         /* NOTREACHED */
174       case 0:
175         if (fd_close != -1)
176           {
177             int i;
178
179             i = close (fd_close);
180             assert (i == 0);
181           }
182         child (data, input);
183         /* NOTREACHED */
184         abort ();
185       default:;
186         /* PASSTHRU */
187     }
188   /* Parent.  */
189
190   registry_add (pid);
191   STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING));
192   return pid;
193 }
194
195 static void murder (pid_t pid)
196 {
197   int i;
198   pid_t pid_got;
199   int status;
200
201   if (pid == 0)
202     return;
203
204   i = kill (pid, SIGKILL);
205   assert (i == 0);
206
207   pid_got = waitpid (pid, &status, 0);
208   if (!(pid_got == -1 && errno == ECHILD))
209     {
210       assert (pid_got == pid);
211       assert ((WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL)
212               || (WIFEXITED (status) && WEXITSTATUS (status) == 0));
213       STATE (pid, 1 << STATE_ENOENT);
214     }
215   else
216     STATE (pid, (1 << STATE_ENOENT) | (1 << STATE_ZOMBIE));
217
218   registry_remove (pid);
219 }
220
221
222 struct child_spawner
223   {
224     void *(*child) (void *data, void *input);
225     void *data;
226     int fd;
227   };
228
229 static void *child_spawner (void *param_voidpointer, void *input)
230 {
231   struct child_spawner *param = param_voidpointer;
232   pid_t inferior;
233   ssize_t inferior_size;
234   int i;
235
236   inferior = spawn (param->child, param->data, input, param->fd);
237
238   inferior_size = write (param->fd, &inferior, sizeof (inferior));
239   assert (inferior_size == sizeof (inferior));
240   i = close (param->fd);
241   assert (i == 0);
242
243   waitpid (inferior, NULL, 0);
244   _exit (EXIT_SUCCESS);
245   /* NOTREACHED */
246   abort ();
247 }
248
249 struct spawner
250   {
251     void *(*func) (void *data, void *input);
252     void *data;
253   };
254
255 static void *spawn_with_waiter (void *data, void *input)
256 {
257   pid_t waiter;
258   struct child_spawner param_local;
259   int pipefds[2];
260   int i;
261   pid_t inferior;
262   ssize_t inferior_size;
263   unsigned char buf;
264   ssize_t buf_size;
265   struct spawner *param = data;
266
267   assert (data != NULL);
268
269   i = pipe (pipefds);
270   assert (i == 0);
271
272   param_local.child = param->func;
273   param_local.data = param->data;
274   param_local.fd = pipefds[1];
275   waiter = spawn (child_spawner, &param_local, input, pipefds[0]);
276
277   i = close (pipefds[1]);
278   assert (i == 0);
279   inferior_size = read (pipefds[0], &inferior, sizeof (inferior));
280   assert (inferior_size == sizeof (inferior));
281   buf_size = read (pipefds[0], &buf, sizeof (buf));
282   assert (buf_size == 0);
283   i = close (pipefds[0]);
284   assert (i == 0);
285
286   registry_add (inferior);
287
288   return (void *) (unsigned long) inferior;
289 }
290
291 static void *spawn_without_waiter (void *data, void *input)
292 {
293   struct spawner *param = data;
294
295   return (void *) (unsigned long) spawn (param->func, param->data, input, -1);
296 }
297
298 static void body_spawner (void *(*child) (void *data, void *input), void *data,
299                           void *input)
300 {
301   pid_t inferior;
302   int stopped;
303   int i;
304
305   assert (input == NULL);
306
307   /* Plain attach/detach.  */
308   inferior = (unsigned long) (*child) (data, child_pause);
309   stopped = attach_checked (inferior, 0);
310   assert (stopped == 0);
311   detach_checked (inferior, stopped);
312   murder (inferior);
313
314   /* Attach to a process stopped by standard kill(2).  */
315   inferior = (unsigned long) (*child) (data, child_pause);
316   delay ();
317   i = kill (inferior, SIGSTOP);
318   assert (i == 0);
319   STATE (inferior, 1 << STATE_STOPPED);
320   stopped = attach_checked (inferior, 0);
321   assert (stopped == 1);
322   detach_checked (inferior, stopped);
323   murder (inferior);
324
325   /* Attach to a process stopped by Linux specific tkill(2).  */
326   inferior = (unsigned long) (*child) (data, child_pause);
327   delay ();
328   i = tkill (inferior, SIGSTOP);
329   assert (i == 0);
330   STATE (inferior, 1 << STATE_STOPPED);
331   stopped = attach_checked (inferior, 0);
332   assert (stopped == 1);
333   detach_checked (inferior, stopped);
334   murder (inferior);
335
336   /* Attach to a stopped process with already pending SIGALRM.  */
337   inferior = (unsigned long) (*child) (data, child_alrm);
338   STATE (inferior, 1 << STATE_SLEEPING);
339   delay ();
340   i = tkill (inferior, SIGSTOP);
341   assert (i == 0);
342   /* Wait till it gets stopped otherwise we may get STATE_ENOENT below.  */
343   STATE (inferior, 1 << STATE_STOPPED);
344   delay ();
345   i = tkill (inferior, SIGALRM);
346   assert (i == 0);
347   STATE (inferior, 1 << STATE_STOPPED);
348   /* FIXME: SIGALRM did not get redelivered?  */
349 #if 0
350   stopped = attach_checked (inferior, SIGALRM);
351 #else
352   stopped = attach_checked (inferior, 0);
353 #endif
354   assert (stopped == 1);
355   detach_checked (inferior, stopped);
356   STATE (inferior, 1 << STATE_STOPPED);
357   delay ();
358   i = tkill (inferior, SIGCONT);
359   assert (i == 0);
360   /* This is a race, we may not prove the successful SIGALRM delivery by it.  */
361   STATE (inferior, 1 << STATE_RUNNING);
362   murder (inferior);
363 }
364
365 static void *pass (void *data, void *input)
366 {
367   struct spawner *param = data;
368   void (*input_func) (void) = input;
369
370   if (param != NULL)
371     return (*param->func) (param->data, input);
372
373   assert (input_func != NULL);
374   (*input_func) ();
375   /* NOTREACHED */
376   abort ();
377 }
378
379 static void *spawn_singlethreaded (void *data, void *input)
380 {
381   return pass (data, input);
382 }
383
384 static void *spawn_threaded_parent_start (void *arg)
385 {
386   for (;;)
387     pause ();
388   /* NOTREACHED */
389   abort ();
390 }
391
392 static void *spawn_threaded_parent (void *data, void *input)
393 {
394   pthread_t thread;
395   int i;
396
397   i = pthread_create (&thread, NULL, spawn_threaded_parent_start, NULL);
398   assert (i == 0);
399   return pass (data, input);
400 }
401
402 struct spawn_threaded_child_start
403   {
404     void *data;
405     void *input;
406   };
407
408 static void *spawn_threaded_child_start (void *arg_voidpointer)
409 {
410   struct spawn_threaded_child_start *arg = arg_voidpointer;
411
412   return pass (arg->data, arg->input);
413   /* NOTREACHED */
414 }
415
416 static void *spawn_threaded_child (void *data, void *input)
417 {
418   pthread_t thread;
419   int i;
420   struct spawn_threaded_child_start arg_local;
421
422   arg_local.data = data;
423   arg_local.input = input;
424   i = pthread_create (&thread, NULL, spawn_threaded_child_start, &arg_local);
425   assert (i == 0);
426   i = pthread_join (thread, NULL);
427   assert (i == 0);
428
429   _exit (EXIT_SUCCESS);
430 }
431
432 static void body_maywaiter (void *(*child) (void *data, void *input),
433                             void *data, void *input)
434 {
435   struct spawner param_local;
436
437   param_local.func = child;
438   param_local.data = data;
439   body_spawner (spawn_without_waiter, &param_local, NULL);
440   body_spawner (spawn_with_waiter, &param_local, NULL);
441 }
442
443 static volatile unsigned long loops = 0;
444 static volatile int loops_print = 0;
445
446 static void handler_sigusr1 (int signo)
447 {
448   assert (signo == SIGUSR1);
449
450   loops_print++;
451 }
452
453 int main (int argc, char **argv)
454 {
455   int loop = 0;
456   int i;
457
458   if (argc == 1)
459     ;
460   else if (argc == 2 && strcmp (argv[1], "-l") == 0)
461     loop = 1;
462   else
463     abort ();
464
465   i = nice (10);
466   assert (i != -1);
467
468   atexit (registry_atexit);
469   signal (SIGINT, registry_handler);
470   signal (SIGABRT, registry_handler);
471
472   signal (SIGUSR1, handler_sigusr1);
473
474   do
475     {
476       while (loops_print > 0)
477         {
478           printf ("%lu\n", loops);
479           loops_print--;
480         }
481       body_maywaiter (spawn_singlethreaded, NULL, NULL);
482       body_maywaiter (spawn_threaded_parent, NULL, NULL);
483       body_maywaiter (spawn_threaded_child, NULL, NULL);
484
485       registry_cleanup ();
486       loops++;
487     }
488   while (loop != 0);
489
490   return EXIT_SUCCESS;
491 }