1 /* Tests for MINIX3 ptrace(2) - by D.C. van Moolenbroek */
10 #include <sys/select.h>
11 #include <sys/ptrace.h>
18 if (child) exit(n); printf("Attach type %d, ", attach); e(n); }
21 #define _WIFSTOPPED(s) (WIFSTOPPED(s) && !WIFSIGNALED(s) && !WIFEXITED(s))
22 #define _WIFSIGNALED(s) (!WIFSTOPPED(s) && WIFSIGNALED(s) && !WIFEXITED(s))
23 #define _WIFEXITED(s) (!WIFSTOPPED(s) && !WIFSIGNALED(s) && WIFEXITED(s))
25 #define timed_test(func) (timed_test_func(#func, func));
27 int main(int argc
, char **argv
);
28 void test(int m
, int a
);
29 void timed_test_func(const char *s
, void (* func
)(void));
30 void timed_test_timeout(int signum
);
31 pid_t
traced_fork(void (*c
) (void));
32 pid_t
traced_pfork(void (*c
) (void));
33 void WRITE(int value
);
35 void traced_wait(void);
36 void detach_running(pid_t pid
);
37 void dummy_handler(int sig
);
38 void exit_handler(int sig
);
39 void count_handler(int sig
);
40 void catch_handler(int sig
);
41 void test_wait_child(void);
43 void test_exec_child(void);
45 void test_step_child(void);
47 void test_sig_child(void);
49 void test_exit_child(void);
51 void test_term_child(void);
53 void test_catch_child(void);
54 void test_catch(void);
55 void test_kill_child(void);
57 void test_attach_child(void);
58 void test_attach(void);
59 void test_detach_child(void);
60 void test_detach(void);
61 void test_death_child(void);
62 void test_death(void);
63 void test_zdeath_child(void);
64 void test_zdeath(void);
65 void test_syscall_child(void);
66 void test_syscall(void);
67 void test_tracefork_child(void);
68 void test_tracefork(void);
69 void sigexec(int setflag
, int opt
, int *traps
, int *stop
);
70 void test_trapexec(void);
71 void test_altexec(void);
72 void test_noexec(void);
73 void test_defexec(void);
74 void test_reattach_child(void);
75 void test_reattach(void);
77 static char *executable
;
78 static int child
= 0, attach
;
81 static int sigs
, caught
;
87 int i
, m
= 0xFFFFFF, n
= 0xF;
88 char cp_cmd
[NAME_MAX
+ 10];
90 if (strcmp(argv
[0], "DO CHECK") == 0) {
98 snprintf(cp_cmd
, sizeof(cp_cmd
), "cp ../%s .", executable
);
101 if (argc
>= 2) m
= atoi(argv
[1]);
102 if (argc
>= 3) n
= atoi(argv
[2]);
104 for (i
= 0; i
< ITERATIONS
; i
++) {
105 if (n
& 001) test(m
, 0);
106 if (n
& 002) test(m
, 1);
107 if (n
& 004) test(m
, 2);
108 if (n
& 010) test(m
, 3);
112 return(-1); /* impossible */
121 if (m
& 00000001) timed_test(test_wait
);
122 if (m
& 00000002) timed_test(test_exec
);
123 #if !defined(__arm__)
124 /* BJG: single-stepping isn't implemented on ARM */
125 if (m
& 00000004) timed_test(test_step
);
127 if (m
& 00000010) timed_test(test_sig
);
128 if (m
& 00000020) timed_test(test_exit
);
129 if (m
& 00000040) timed_test(test_term
);
130 if (m
& 00000100) timed_test(test_catch
);
131 if (m
& 00000200) timed_test(test_kill
);
132 if (m
& 00000400) timed_test(test_attach
);
133 if (m
& 00001000) timed_test(test_detach
);
134 if (m
& 00002000) timed_test(test_death
);
135 if (m
& 00004000) timed_test(test_zdeath
);
136 if (m
& 00010000) timed_test(test_syscall
);
137 if (m
& 00020000) timed_test(test_tracefork
);
138 if (m
& 00040000) timed_test(test_trapexec
);
139 if (m
& 00100000) timed_test(test_altexec
);
140 if (m
& 00200000) timed_test(test_noexec
);
141 if (m
& 00400000) timed_test(test_defexec
);
142 if (m
& 01000000) test_reattach(); /* not timed, catches SIGALRM */
145 static jmp_buf timed_test_context
;
147 void timed_test_timeout(int signum
)
149 longjmp(timed_test_context
, -1);
155 void timed_test_func(const char *s
, void (* func
)(void))
157 if (setjmp(timed_test_context
) == 0)
159 /* the function gets 60 seconds to complete */
160 if (signal(SIGALRM
, timed_test_timeout
) == SIG_ERR
) { my_e(701); return; }
167 /* report timeout as error */
168 printf("timeout in %s\n", s
);
179 if (pipe(pfd
) != 0) my_e(200);
180 if (pipe(&pfd
[2]) != 0) my_e(201);
183 case 0: /* let child volunteer to be traced */
186 if (pid
< 0) my_e(202);
191 if (ptrace(T_OK
, 0, 0, 0) != 0) my_e(203);
200 if (READ() != 0) my_e(205);
204 case 1: /* attach to child process */
207 if (pid
< 0) my_e(206);
212 if (READ() != 0) my_e(207);
219 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) my_e(209);
221 if (waitpid(pid
, &status
, 0) != pid
) my_e(210);
222 if (!_WIFSTOPPED(status
)) my_e(211);
223 if (WSTOPSIG(status
) != SIGSTOP
) my_e(212);
225 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(213);
231 case 2: /* attach to non-child process */
234 if (ppid
< 0) my_e(214);
239 if (pid
< 0) exit(215);
244 if (READ() != 0) my_e(216);
255 if (waitpid(pid
, &status
, 0) != pid
) my_e(218);
256 if (_WIFSTOPPED(status
)) my_e(219);
257 if (_WIFEXITED(status
) && (r
= WEXITSTATUS(status
)) != 42) my_e(r
);
264 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) my_e(220);
266 if (waitpid(pid
, &status
, 0) != pid
) my_e(221);
267 if (!_WIFSTOPPED(status
)) my_e(222);
268 if (WSTOPSIG(status
) != SIGSTOP
) my_e(223);
270 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(224);
276 case 3: /* attach by forking from child */
279 if (ppid
< 0) my_e(225);
284 if (ptrace(T_OK
, 0, 0, 0) != 0) my_e(226);
288 if (READ() != 0) my_e(227);
292 if (pid
< 0) my_e(228);
302 if (waitpid(pid
, &status
, 0) != pid
) my_e(230);
303 if (_WIFSTOPPED(status
)) my_e(231);
304 if (_WIFEXITED(status
) && (r
= WEXITSTATUS(status
)) != 42) my_e(r
);
309 if (READ() != 0) my_e(232);
311 if (kill(ppid
, SIGSTOP
) != 0) my_e(233);
313 if (waitpid(ppid
, &status
, 0) != ppid
) my_e(234);
314 if (!_WIFSTOPPED(status
)) my_e(235);
315 if (WSTOPSIG(status
) != SIGSTOP
) my_e(236);
317 if (ptrace(T_SETOPT
, ppid
, 0, TO_TRACEFORK
) != 0) my_e(237);
319 if (ptrace(T_RESUME
, ppid
, 0, 0) != 0) my_e(238);
325 if (waitpid(pid
, &status
, 0) != pid
) my_e(239);
326 if (!_WIFSTOPPED(status
)) my_e(240);
327 if (WSTOPSIG(status
) != SIGSTOP
) my_e(241);
329 if (ptrace(T_SETOPT
, pid
, 0, 0) != 0) my_e(242);
330 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(243);
332 detach_running(ppid
);
340 pid_t
traced_pfork(c
)
345 if (pipe(pfd
) != 0) my_e(300);
346 if (pipe(&pfd
[2]) != 0) my_e(301);
350 if (pid
< 0) my_e(302);
366 if (write(pfd
[child
*2+1], &value
, sizeof(value
)) != sizeof(value
)) my_e(400);
373 if (read(pfd
[2-child
*2], &value
, sizeof(value
)) != sizeof(value
)) my_e(401);
383 if (waitpid(ppid
, &status
, 0) != ppid
) my_e(500);
384 if (!_WIFEXITED(status
)) my_e(501);
385 if ((r
= WEXITSTATUS(status
)) != 0) my_e(r
);
388 /* Quick hack to clean up detached children */
389 waitpid(-1, NULL
, WNOHANG
);
398 void detach_running(pid
)
401 /* Detach from a process that is not already stopped. This is the way to do it.
402 * We have to stop the child in order to detach from it, but as the child may
403 * have other signals pending for the tracer, we cannot assume we get our own
404 * signal back immediately. However, because we know that the kill is instant
405 * and resuming with pending signals will only stop the process immediately
406 * again, we can use T_RESUME for all the signals until we get our own signal,
407 * and then detach. A complicating factor is that anywhere during this
408 * procedure, the child may die (e.g. by getting a SIGKILL). In our tests, this
413 if (kill(pid
, SIGSTOP
) != 0) my_e(600);
415 if (waitpid(pid
, &status
, 0) != pid
) my_e(601);
417 while (_WIFSTOPPED(status
)) {
418 if (WSTOPSIG(status
) == SIGSTOP
) {
419 if (ptrace(T_DETACH
, pid
, 0, 0) != 0) my_e(602);
424 if (ptrace(T_RESUME
, pid
, 0, WSTOPSIG(status
)) != 0) my_e(603);
426 if (waitpid(pid
, &status
, 0) != pid
) my_e(604);
429 /* Apparently the process exited. */
430 if (!_WIFEXITED(status
) && !_WIFSIGNALED(status
)) my_e(605);
432 /* In our tests, that should not happen. */
436 void dummy_handler(sig
)
441 void exit_handler(sig
)
447 void count_handler(sig
)
453 void catch_handler(sig
)
460 case SIGUSR1
: bit
= 1; break;
461 case SIGUSR2
: bit
= 2; break;
462 case SIGTERM
: bit
= 4; break;
467 sigprocmask(SIG_SETMASK
, &set
, NULL
);
469 if (caught
& bit
) my_e(101);
473 void test_wait_child()
485 pid
= traced_fork(test_wait_child
);
487 if (waitpid(pid
, &status
, 0) != pid
) my_e(1);
488 if (!_WIFEXITED(status
)) my_e(2);
489 if (WEXITSTATUS(status
) != 42) my_e(3);
494 void test_exec_child()
496 if (READ() != 0) my_e(100);
498 execl(executable
, "DO CHECK", NULL
);
508 /* This test covers the T_OK case. */
509 if (attach
!= 0) return;
513 pid
= traced_fork(test_exec_child
);
517 /* An exec() should result in a trap signal. */
518 if (waitpid(pid
, &status
, 0) != pid
) my_e(1);
519 if (!_WIFSTOPPED(status
)) my_e(2);
520 if (WSTOPSIG(status
) != SIGTRAP
) my_e(3);
522 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(4);
524 if (waitpid(pid
, &status
, 0) != pid
) my_e(5);
525 if (!_WIFEXITED(status
)) my_e(6);
526 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
531 void test_step_child()
535 signal(SIGUSR1
, SIG_IGN
);
539 if (READ() != 0) my_e(100);
541 /* It must not be possible for the child to stop the single-step signal. */
542 signal(SIGTRAP
, SIG_IGN
);
544 sigprocmask(SIG_SETMASK
, &set
, NULL
);
552 int r
, status
, count
;
556 pid
= traced_fork(test_step_child
);
558 if (READ() != 0) my_e(1);
560 /* While the child is running, neither waitpid() nor ptrace() should work. */
561 if (waitpid(pid
, &status
, WNOHANG
) != 0) my_e(2);
562 if (ptrace(T_RESUME
, pid
, 0, 0) != -1) my_e(3);
563 if (errno
!= EBUSY
) my_e(4);
565 if (kill(pid
, SIGUSR1
) != 0) my_e(5);
569 /* A kill() signal (other than SIGKILL) should be delivered to the tracer. */
570 if (waitpid(pid
, &status
, 0) != pid
) my_e(6);
571 if (!_WIFSTOPPED(status
)) my_e(7);
572 if (WSTOPSIG(status
) != SIGUSR1
) my_e(8);
574 /* ptrace(T_STEP) should result in instruction-wise progress. */
575 for (count
= 0; ; count
++) {
576 if (ptrace(T_STEP
, pid
, 0, 0) != 0) my_e(9);
578 if (waitpid(pid
, &status
, 0) != pid
) my_e(10);
579 if (_WIFEXITED(status
)) break;
580 if (!_WIFSTOPPED(status
)) my_e(11);
581 if (WSTOPSIG(status
) != SIGTRAP
) my_e(12);
584 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
586 if (count
< 10) my_e(13); /* in practice: hundreds */
591 void test_sig_child()
593 signal(SIGUSR1
, exit_handler
);
595 if (READ() != 0) my_e(100);
609 pid
= traced_fork(test_sig_child
);
613 /* allow the child to enter the pause */
616 if (kill(pid
, SIGUSR1
) != 0) my_e(1);
617 if (kill(pid
, SIGUSR2
) != 0) my_e(2);
619 /* All signals should arrive at the tracer, although in "random" order. */
620 if (waitpid(pid
, &status
, 0) != pid
) my_e(3);
621 if (!_WIFSTOPPED(status
)) my_e(4);
622 if (WSTOPSIG(status
) != SIGUSR1
&& WSTOPSIG(status
) != SIGUSR2
) my_e(5);
624 /* The tracer should see kills arriving while the tracee is stopped. */
625 if (kill(pid
, WSTOPSIG(status
)) != 0) my_e(6);
627 if (waitpid(pid
, &status
, WNOHANG
) != pid
) my_e(7);
628 if (!_WIFSTOPPED(status
)) my_e(8);
629 if (WSTOPSIG(status
) != SIGUSR1
&& WSTOPSIG(status
) != SIGUSR2
) my_e(9);
630 sig
= (WSTOPSIG(status
) == SIGUSR1
) ? SIGUSR2
: SIGUSR1
;
632 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(10);
634 if (waitpid(pid
, &status
, 0) != pid
) my_e(11);
635 if (!_WIFSTOPPED(status
)) my_e(12);
636 if (WSTOPSIG(status
) != sig
) my_e(13);
638 if (waitpid(pid
, &status
, WNOHANG
) != 0) my_e(14);
640 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(15);
642 /* Ignored signals passed via ptrace() should be ignored. */
643 if (kill(pid
, SIGUSR1
) != 0) my_e(16);
645 if (waitpid(pid
, &status
, 0) != pid
) my_e(17);
646 if (!_WIFSTOPPED(status
)) my_e(18);
647 if (WSTOPSIG(status
) != SIGUSR1
) my_e(19);
649 if (ptrace(T_RESUME
, pid
, 0, SIGCHLD
) != 0) my_e(20);
651 /* if the pause has been aborted (shouldn't happen!), let the child exit */
654 if (waitpid(pid
, &status
, WNOHANG
) != 0) my_e(21);
656 /* Caught signals passed via ptrace() should invoke their signal handlers. */
657 if (kill(pid
, SIGUSR1
) != 0) my_e(22);
659 if (waitpid(pid
, &status
, 0) != pid
) my_e(23);
660 if (!_WIFSTOPPED(status
)) my_e(24);
661 if (WSTOPSIG(status
) != SIGUSR1
) my_e(25);
663 if (ptrace(T_RESUME
, pid
, 0, SIGUSR1
) != 0) my_e(26);
665 if (waitpid(pid
, &status
, 0) != pid
) my_e(27);
666 if (!_WIFEXITED(status
)) my_e(28);
667 if ((r
= WEXITSTATUS(status
)) != 42) my_e(29);
672 void test_exit_child()
686 pid
= traced_fork(test_exit_child
);
688 if (READ() != 0) my_e(1);
692 if (kill(pid
, SIGSTOP
) != 0) my_e(2);
694 if (waitpid(pid
, &status
, 0) != pid
) my_e(3);
695 if (!_WIFSTOPPED(status
)) my_e(4);
696 if (WSTOPSIG(status
) != SIGSTOP
) my_e(5);
698 /* There should be no more signals pending for the tracer now. */
699 if (waitpid(pid
, &status
, WNOHANG
) != 0) my_e(6);
701 /* ptrace(T_EXIT) should terminate the process with the given exit value. */
702 if (ptrace(T_EXIT
, pid
, 0, 42) != 0) my_e(7);
704 if (waitpid(pid
, &status
, 0) != pid
) my_e(8);
705 if (!_WIFEXITED(status
)) my_e(9);
706 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
711 void test_term_child()
713 signal(SIGUSR1
, SIG_DFL
);
714 signal(SIGUSR2
, dummy_handler
);
730 pid
= traced_fork(test_term_child
);
732 if (READ() != 0) my_e(1);
734 /* If the first of two signals terminates the traced child, the second signal
735 * may or may not be delivered to the tracer - this is merely a policy issue.
736 * However, nothing unexpected should happen.
738 if (kill(pid
, SIGUSR1
) != 0) my_e(2);
739 if (kill(pid
, SIGUSR2
) != 0) my_e(3);
741 if (waitpid(pid
, &status
, 0) != pid
) my_e(4);
742 if (!_WIFSTOPPED(status
)) my_e(5);
744 if (ptrace(T_RESUME
, pid
, 0, SIGUSR1
) != 0) my_e(6);
746 if (waitpid(pid
, &status
, 0) != pid
) my_e(7);
748 if (_WIFSTOPPED(status
)) {
749 if (ptrace(T_RESUME
, pid
, 0, SIGUSR1
) != 0) my_e(8);
751 if (waitpid(pid
, &status
, 0) != pid
) my_e(9);
754 if (!_WIFSIGNALED(status
)) my_e(10);
755 if (WTERMSIG(status
) != SIGUSR1
) my_e(11);
760 void test_catch_child()
765 sa
.sa_handler
= catch_handler
;
766 sigemptyset(&sa
.sa_mask
);
767 sa
.sa_flags
= SA_NODEFER
;
769 sigaction(SIGUSR1
, &sa
, NULL
);
770 sigaction(SIGUSR2
, &sa
, NULL
);
771 sigaction(SIGTERM
, &sa
, NULL
);
774 sigprocmask(SIG_SETMASK
, &set
, &oset
);
780 while (caught
!= 7) sigsuspend(&oset
);
792 pid
= traced_fork(test_catch_child
);
794 if (READ() != 0) my_e(1);
796 if (kill(pid
, SIGUSR1
) != 0) my_e(2);
797 if (kill(pid
, SIGUSR2
) != 0) my_e(3);
799 if (waitpid(pid
, &status
, 0) != pid
) my_e(4);
800 if (!_WIFSTOPPED(status
)) my_e(5);
801 if (WSTOPSIG(status
) != SIGUSR1
&& WSTOPSIG(status
) != SIGUSR2
) my_e(6);
802 sig
= (WSTOPSIG(status
) == SIGUSR1
) ? SIGUSR2
: SIGUSR1
;
804 if (ptrace(T_RESUME
, pid
, 0, WSTOPSIG(status
)) != 0) my_e(7);
806 if (kill(pid
, SIGTERM
) != 0) my_e(8);
808 if (waitpid(pid
, &status
, 0) != pid
) my_e(9);
809 if (!_WIFSTOPPED(status
)) my_e(10);
810 if (WSTOPSIG(status
) != sig
&& WSTOPSIG(status
) != SIGTERM
) my_e(11);
811 if (WSTOPSIG(status
) == sig
) sig
= SIGTERM
;
813 if (ptrace(T_RESUME
, pid
, 0, WSTOPSIG(status
)) != 0) my_e(12);
815 if (kill(pid
, SIGBUS
) != 0) my_e(13);
817 if (waitpid(pid
, &status
, 0) != pid
) my_e(14);
818 if (!_WIFSTOPPED(status
)) my_e(15);
819 if (WSTOPSIG(status
) != sig
&& WSTOPSIG(status
) != SIGBUS
) my_e(16);
821 if (ptrace(T_RESUME
, pid
, 0, sig
) != 0) my_e(17);
823 if (WSTOPSIG(status
) == sig
) sig
= SIGBUS
;
825 if (waitpid(pid
, &status
, 0) != pid
) my_e(18);
826 if (!_WIFSTOPPED(status
)) my_e(19);
827 if (WSTOPSIG(status
) != sig
) my_e(20);
829 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(21);
831 if (waitpid(pid
, &status
, 0) != pid
) my_e(22);
832 if (!_WIFEXITED(status
)) my_e(23);
833 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
838 void test_kill_child()
842 signal(SIGKILL
, SIG_IGN
);
844 sigprocmask(SIG_SETMASK
, &set
, NULL
);
860 pid
= traced_fork(test_kill_child
);
862 if (READ() != 0) my_e(1);
864 /* SIGKILL must be unstoppable in every way. */
865 if (kill(pid
, SIGKILL
) != 0) my_e(2);
867 if (waitpid(pid
, &status
, 0) != pid
) my_e(3);
868 if (!_WIFSIGNALED(status
)) my_e(4);
869 if (WTERMSIG(status
) != SIGKILL
) my_e(5);
871 /* After termination, the child must no longer be visible to the tracer. */
872 if (waitpid(pid
, &status
, WNOHANG
) != -1) my_e(6);
873 if (errno
!= ECHILD
) my_e(7);
878 void test_attach_child()
880 if (ptrace(T_OK
, 0, 0, 0) != -1) my_e(100);
881 if (errno
!= EBUSY
) my_e(101);
885 if (READ() != 0) my_e(102);
896 /* Attaching to kernel processes is not allowed. */
897 if (ptrace(T_ATTACH
, -1, 0, 0) != -1) my_e(1);
898 if (errno
!= ESRCH
) my_e(2);
900 /* Attaching to self is not allowed. */
901 if (ptrace(T_ATTACH
, getpid(), 0, 0) != -1) my_e(3);
902 if (errno
!= EPERM
) my_e(4);
904 /* Attaching to PM is not allowed. */
906 /* FIXME: disabled until we can reliably determine PM's pid */
907 if (ptrace(T_ATTACH
, 0, 0, 0) != -1) my_e(5);
908 if (errno
!= EPERM
) my_e(6);
911 pid
= traced_fork(test_attach_child
);
913 /* Attaching more than once is not allowed. */
914 if (ptrace(T_ATTACH
, pid
, 0, 0) != -1) my_e(7);
915 if (errno
!= EBUSY
) my_e(8);
917 if (READ() != 0) my_e(9);
919 /* Detaching a running child should not succeed. */
920 if (ptrace(T_DETACH
, pid
, 0, 0) == 0) my_e(10);
921 if (errno
!= EBUSY
) my_e(11);
930 void test_detach_child()
933 sigset_t set
, sset
, oset
;
935 sa
.sa_handler
= catch_handler
;
936 sigemptyset(&sa
.sa_mask
);
937 sa
.sa_flags
= SA_NODEFER
;
939 sigaction(SIGUSR1
, &sa
, NULL
);
940 sigaction(SIGUSR2
, &sa
, NULL
);
941 sigaction(SIGTERM
, &sa
, NULL
);
944 sigprocmask(SIG_SETMASK
, &set
, &oset
);
947 sigdelset(&sset
, SIGUSR1
);
953 if (sigsuspend(&sset
) != -1) my_e(102);
954 if (errno
!= EINTR
) my_e(103);
956 if (caught
!= 1) my_e(104);
958 if (READ() != 0) my_e(105);
960 while (caught
!= 7) sigsuspend(&oset
);
970 /* Can't use traced_fork(), so simplify a bit */
971 if (attach
!= 0) return;
975 pid
= traced_pfork(test_detach_child
);
977 if (READ() != 0) my_e(1);
979 /* The tracer should not see signals sent to the process before attaching. */
980 if (kill(pid
, SIGUSR2
) != 0) my_e(2);
982 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) my_e(3);
984 if (waitpid(pid
, &status
, 0) != pid
) my_e(4);
985 if (!_WIFSTOPPED(status
)) my_e(5);
986 if (WSTOPSIG(status
) != SIGSTOP
) my_e(6);
988 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(7);
990 if (kill(pid
, SIGUSR1
) != 0) my_e(8);
992 if (waitpid(pid
, &status
, 0) != pid
) my_e(9);
993 if (!_WIFSTOPPED(status
)) my_e(10);
994 if (WSTOPSIG(status
) != SIGUSR1
) my_e(11);
996 /* Signals pending at the tracer should be passed on after detaching. */
997 if (kill(pid
, SIGTERM
) != 0) my_e(12);
999 /* A signal may be passed with the detach request. */
1000 if (ptrace(T_DETACH
, pid
, 0, SIGUSR1
) != 0) my_e(13);
1004 if (waitpid(pid
, &status
, 0) != pid
) my_e(14);
1005 if (!_WIFEXITED(status
)) my_e(15);
1006 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
1011 void test_death_child()
1017 if (pid
< 0) my_e(100);
1020 ptrace(T_OK
, 0, 0, 0);
1027 if (READ() != 0) my_e(101);
1029 kill(getpid(), SIGKILL
);
1041 pid
= traced_fork(test_death_child
);
1045 if (kill(cpid
, 0) != 0) my_e(1);
1049 if (waitpid(pid
, &status
, 0) != pid
) my_e(2);
1050 if (!_WIFSIGNALED(status
)) my_e(3);
1051 if (WTERMSIG(status
) != SIGKILL
) my_e(4);
1053 /* The children of killed tracers should be terminated. */
1054 while (kill(cpid
, 0) == 0) sleep(1);
1055 if (errno
!= ESRCH
) my_e(5);
1060 void test_zdeath_child()
1062 if (READ() != 0) my_e(100);
1072 /* Can't use traced_fork(), so simplify a bit */
1073 if (attach
!= 0) return;
1077 pid
= traced_pfork(test_zdeath_child
);
1081 if (tpid
< 0) my_e(1);
1084 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) exit(101);
1086 if (waitpid(pid
, &status
, 0) != pid
) exit(102);
1087 if (!_WIFSTOPPED(status
)) exit(103);
1088 if (WSTOPSIG(status
) != SIGSTOP
) exit(104);
1090 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) exit(105);
1094 /* Unwaited-for traced zombies should be passed to their parent. */
1102 /* However, that should only happen once the tracer has actually died. */
1103 if (waitpid(pid
, &status
, WNOHANG
) != 0) my_e(2);
1105 if (waitpid(tpid
, &status
, 0) != tpid
) my_e(3);
1106 if (!_WIFEXITED(status
)) my_e(4);
1107 if ((r
= WEXITSTATUS(status
)) != 84) my_e(r
);
1109 if (waitpid(pid
, &status
, 0) != pid
) my_e(5);
1110 if (!_WIFEXITED(status
)) my_e(6);
1111 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
1116 void test_syscall_child()
1118 signal(SIGUSR1
, count_handler
);
1119 signal(SIGUSR2
, count_handler
);
1125 if (READ() != 0) my_e(100);
1127 /* Three calls (may fail) */
1132 if (sigs
!= 2) my_e(101);
1140 int i
, r
, sig
, status
;
1144 pid
= traced_fork(test_syscall_child
);
1146 if (READ() != 0) my_e(1);
1148 if (kill(pid
, SIGSTOP
) != 0) my_e(2);
1150 if (waitpid(pid
, &status
, 0) != pid
) my_e(3);
1151 if (!_WIFSTOPPED(status
)) my_e(4);
1152 if (WSTOPSIG(status
) != SIGSTOP
) my_e(5);
1156 /* Upon resuming a first system call, no syscall leave event must be sent. */
1157 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(6);
1159 if (waitpid(pid
, &status
, 0) != pid
) my_e(7);
1161 for (i
= 0; _WIFSTOPPED(status
); i
++) {
1162 if (WSTOPSIG(status
) != SIGTRAP
) my_e(8);
1164 /* Signals passed via T_SYSCALL should arrive, on enter and exit. */
1165 if (i
== 3) sig
= SIGUSR1
;
1166 else if (i
== 6) sig
= SIGUSR2
;
1169 if (ptrace(T_SYSCALL
, pid
, 0, sig
) != 0) my_e(9);
1171 if (waitpid(pid
, &status
, 0) != pid
) my_e(10);
1174 if (!_WIFEXITED(status
)) my_e(11);
1175 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
1177 /* The number of events seen is deterministic but libc-dependent. */
1178 if (i
< 10 || i
> 100) my_e(12);
1180 /* The last system call event must be for entering exit(). */
1181 if (!(i
% 2)) my_e(13);
1186 void test_tracefork_child()
1190 signal(SIGHUP
, SIG_IGN
);
1196 if (READ() != 0) my_e(100);
1198 if ((pid
= fork()) < 0) my_e(101);
1200 exit(pid
> 0 ? 42 : 84);
1203 void test_tracefork()
1205 pid_t pgrp
, ppid
, cpid
, wpid
;
1206 int r
, status
, gotstop
, ptraps
, ctraps
;
1210 ppid
= traced_fork(test_tracefork_child
);
1212 if ((pgrp
= READ()) <= 0) my_e(1);
1214 if (kill(ppid
, SIGSTOP
) != 0) my_e(2);
1216 if (waitpid(ppid
, &status
, 0) != ppid
) my_e(3);
1217 if (!_WIFSTOPPED(status
)) my_e(4);
1218 if (WSTOPSIG(status
) != SIGSTOP
) my_e(5);
1220 if (ptrace(T_SETOPT
, ppid
, 0, TO_TRACEFORK
) != 0) my_e(6);
1224 if (ptrace(T_SYSCALL
, ppid
, 0, 0) != 0) my_e(7);
1229 /* Count how many traps we get for parent and child, until they both exit. */
1230 for (ptraps
= ctraps
= 0; ppid
|| cpid
; ) {
1231 wpid
= waitpid(-pgrp
, &status
, 0);
1233 if (wpid
<= 0) my_e(8);
1234 if (cpid
< 0 && wpid
!= ppid
) {
1238 if (wpid
!= ppid
&& wpid
!= cpid
) my_e(9);
1240 if (_WIFEXITED(status
)) {
1242 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
1246 if ((r
= WEXITSTATUS(status
)) != 84) my_e(r
);
1251 if (!_WIFSTOPPED(status
)) my_e(10);
1253 switch (WSTOPSIG(status
)) {
1258 if (wpid
!= cpid
) my_e(11);
1259 if (gotstop
) my_e(12);
1263 if (wpid
== ppid
) ptraps
++;
1270 if (ptrace(T_SYSCALL
, wpid
, 0, 0) != 0) my_e(14);
1274 /* The parent should get an odd number of traps: the first one is a syscall
1275 * enter trap (typically for the fork()), the last one is the syscall enter
1276 * trap for its exit().
1278 if (ptraps
< 3) my_e(15);
1279 if (!(ptraps
% 2)) my_e(16);
1281 /* The child should get an even number of traps: the first one is a syscall
1282 * leave trap from the fork(), the last one is the syscall enter trap for
1285 if (ctraps
< 2) my_e(17);
1286 if (ctraps
% 2) my_e(18);
1291 void sigexec(setflag
, opt
, traps
, stop
)
1300 pid
= traced_fork(test_exec_child
);
1302 if (kill(pid
, SIGSTOP
) != 0) my_e(1);
1304 if (waitpid(pid
, &status
, 0) != pid
) my_e(2);
1305 if (!_WIFSTOPPED(status
)) my_e(3);
1306 if (WSTOPSIG(status
) != SIGSTOP
) my_e(4);
1308 if (setflag
&& ptrace(T_SETOPT
, pid
, 0, opt
) != 0) my_e(5);
1312 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(6);
1318 if (waitpid(pid
, &status
, 0) != pid
) my_e(7);
1320 if (_WIFEXITED(status
)) break;
1322 if (!_WIFSTOPPED(status
)) my_e(8);
1324 switch (WSTOPSIG(status
)) {
1329 if (*stop
>= 0) my_e(9);
1336 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(11);
1339 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
1344 void test_trapexec()
1350 sigexec(1, 0, &traps
, &stop
);
1352 /* The exec does not cause a SIGSTOP. This gives us an even number of traps;
1353 * as above, but plus the exec()'s extra SIGTRAP. This trap is
1354 * indistinguishable from a syscall trap, especially when considering failed
1355 * exec() calls and immediately following signal handler invocations.
1357 if (traps
< 4) my_e(12);
1358 if (traps
% 2) my_e(13);
1359 if (stop
>= 0) my_e(14);
1368 sigexec(1, TO_ALTEXEC
, &traps
, &stop
);
1370 /* The exec causes a SIGSTOP. This gives us an odd number of traps: a pair
1371 * for each system call, plus one for the final exit(). The stop must have
1372 * taken place after a syscall enter event, i.e. must be odd as well.
1374 if (traps
< 3) my_e(12);
1375 if (!(traps
% 2)) my_e(13);
1376 if (stop
< 0) my_e(14);
1377 if (!(stop
% 2)) my_e(15);
1386 sigexec(1, TO_NOEXEC
, &traps
, &stop
);
1388 /* The exec causes no signal at all. As above, but without the SIGSTOPs. */
1389 if (traps
< 3) my_e(12);
1390 if (!(traps
% 2)) my_e(13);
1391 if (stop
>= 0) my_e(14);
1398 /* We want to test the default of T_OK (0) and T_ATTACH (TO_NOEXEC). */
1399 if (attach
!= 0 && attach
!= 1) return;
1403 /* Do not set any options this time. */
1404 sigexec(0, 0, &traps
, &stop
);
1408 if (traps
< 4) my_e(12);
1409 if (traps
% 2) my_e(13);
1410 if (stop
>= 0) my_e(14);
1413 if (traps
< 3) my_e(15);
1414 if (!(traps
% 2)) my_e(16);
1415 if (stop
>= 0) my_e(17);
1419 void test_reattach_child()
1423 if (READ() != 0) my_e(100);
1427 if (select(0, NULL
, NULL
, NULL
, &tv
) != 0) my_e(101);
1432 void test_reattach()
1435 int r
, status
, count
;
1439 pid
= traced_fork(test_reattach_child
);
1441 if (kill(pid
, SIGSTOP
) != 0) my_e(1);
1443 if (waitpid(pid
, &status
, 0) != pid
) my_e(2);
1444 if (!_WIFSTOPPED(status
)) my_e(3);
1445 if (WSTOPSIG(status
) != SIGSTOP
) my_e(4);
1449 signal(SIGALRM
, dummy_handler
);
1452 /* Start tracing system calls. We don't know how many there will be until
1453 * we reach the child's select(), so we have to interrupt ourselves.
1454 * The hard assumption here is that the child is able to enter the select()
1455 * within a second, despite being traced. If this is not the case, the test
1456 * may hang or fail, and the child may die from a SIGTRAP.
1458 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(5);
1460 for (count
= 0; (r
= waitpid(pid
, &status
, 0)) == pid
; count
++) {
1461 if (!_WIFSTOPPED(status
)) my_e(6);
1462 if (WSTOPSIG(status
) != SIGTRAP
) my_e(7);
1464 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(8);
1467 if (r
!= -1 || errno
!= EINTR
) my_e(9);
1469 /* We always start with syscall enter event; the last event we should have
1470 * seen before the alarm was entering the select() call.
1472 if (!(count
% 2)) my_e(10);
1474 /* Detach, and immediately attach again. */
1475 detach_running(pid
);
1477 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) my_e(11);
1479 if (waitpid(pid
, &status
, 0) != pid
) my_e(12);
1480 if (!_WIFSTOPPED(status
)) my_e(13);
1481 if (WSTOPSIG(status
) != SIGSTOP
) my_e(14);
1483 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(15);
1485 if (waitpid(pid
, &status
, 0) != pid
) my_e(16);
1487 for (count
= 0; _WIFSTOPPED(status
); count
++) {
1488 if (WSTOPSIG(status
) != SIGTRAP
) my_e(17);
1490 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(18);
1492 if (waitpid(pid
, &status
, 0) != pid
) my_e(19);
1495 if (!_WIFEXITED(status
)) my_e(20);
1496 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
1498 /* We must not have seen the select()'s syscall leave event, and the last
1499 * event will be the syscall enter for the exit().
1501 if (!(count
% 2)) my_e(21);