1 /* Tests for MINIX3 ptrace(2) - by D.C. van Moolenbroek */
10 #include <sys/select.h>
11 #include <sys/ptrace.h>
12 #include <sys/syslimits.h>
19 if (child) exit(n); printf("Attach type %d, ", attach); e(n); }
22 #define _WIFSTOPPED(s) (WIFSTOPPED(s) && !WIFSIGNALED(s) && !WIFEXITED(s))
23 #define _WIFSIGNALED(s) (!WIFSTOPPED(s) && WIFSIGNALED(s) && !WIFEXITED(s))
24 #define _WIFEXITED(s) (!WIFSTOPPED(s) && !WIFSIGNALED(s) && WIFEXITED(s))
26 #define timed_test(func) (timed_test_func(#func, func));
28 int main(int argc
, char **argv
);
29 void test(int m
, int a
);
30 void timed_test_func(const char *s
, void (* func
)(void));
31 void timed_test_timeout(int signum
);
32 pid_t
traced_fork(void (*c
) (void));
33 pid_t
traced_pfork(void (*c
) (void));
34 void WRITE(int value
);
36 void traced_wait(void);
37 void detach_running(pid_t pid
);
38 void dummy_handler(int sig
);
39 void exit_handler(int sig
);
40 void count_handler(int sig
);
41 void catch_handler(int sig
);
42 void test_wait_child(void);
44 void test_exec_child(void);
46 void test_step_child(void);
48 void test_sig_child(void);
50 void test_exit_child(void);
52 void test_term_child(void);
54 void test_catch_child(void);
55 void test_catch(void);
56 void test_kill_child(void);
58 void test_attach_child(void);
59 void test_attach(void);
60 void test_detach_child(void);
61 void test_detach(void);
62 void test_death_child(void);
63 void test_death(void);
64 void test_zdeath_child(void);
65 void test_zdeath(void);
66 void test_syscall_child(void);
67 void test_syscall(void);
68 void test_tracefork_child(void);
69 void test_tracefork(void);
70 void sigexec(int setflag
, int opt
, int *traps
, int *stop
);
71 void test_trapexec(void);
72 void test_altexec(void);
73 void test_noexec(void);
74 void test_defexec(void);
75 void test_reattach_child(void);
76 void test_reattach(void);
78 static char *executable
;
79 static int child
= 0, attach
;
82 static int sigs
, caught
;
88 int i
, m
= 0xFFFFFF, n
= 0xF;
89 char cp_cmd
[NAME_MAX
+ 10];
91 if (strcmp(argv
[0], "DO CHECK") == 0) {
99 snprintf(cp_cmd
, sizeof(cp_cmd
), "cp ../%s .", executable
);
102 if (argc
>= 2) m
= atoi(argv
[1]);
103 if (argc
>= 3) n
= atoi(argv
[2]);
105 for (i
= 0; i
< ITERATIONS
; i
++) {
106 if (n
& 001) test(m
, 0);
107 if (n
& 002) test(m
, 1);
108 if (n
& 004) test(m
, 2);
109 if (n
& 010) test(m
, 3);
113 return(-1); /* impossible */
122 if (m
& 00000001) timed_test(test_wait
);
123 if (m
& 00000002) timed_test(test_exec
);
124 #if !defined(__arm__)
125 /* BJG: single-stepping isn't implemented on ARM */
126 if (m
& 00000004) timed_test(test_step
);
128 if (m
& 00000010) timed_test(test_sig
);
129 if (m
& 00000020) timed_test(test_exit
);
130 if (m
& 00000040) timed_test(test_term
);
131 if (m
& 00000100) timed_test(test_catch
);
132 if (m
& 00000200) timed_test(test_kill
);
133 if (m
& 00000400) timed_test(test_attach
);
134 if (m
& 00001000) timed_test(test_detach
);
135 if (m
& 00002000) timed_test(test_death
);
136 if (m
& 00004000) timed_test(test_zdeath
);
137 if (m
& 00010000) timed_test(test_syscall
);
138 if (m
& 00020000) timed_test(test_tracefork
);
139 if (m
& 00040000) timed_test(test_trapexec
);
140 if (m
& 00100000) timed_test(test_altexec
);
141 if (m
& 00200000) timed_test(test_noexec
);
142 if (m
& 00400000) timed_test(test_defexec
);
143 if (m
& 01000000) test_reattach(); /* not timed, catches SIGALRM */
146 static jmp_buf timed_test_context
;
148 void timed_test_timeout(int signum
)
150 longjmp(timed_test_context
, -1);
156 void timed_test_func(const char *s
, void (* func
)(void))
158 if (setjmp(timed_test_context
) == 0)
160 /* the function gets 60 seconds to complete */
161 if (signal(SIGALRM
, timed_test_timeout
) == SIG_ERR
) { my_e(701); return; }
168 /* report timeout as error */
169 printf("timeout in %s\n", s
);
180 if (pipe(pfd
) != 0) my_e(200);
181 if (pipe(&pfd
[2]) != 0) my_e(201);
184 case 0: /* let child volunteer to be traced */
187 if (pid
< 0) my_e(202);
192 if (ptrace(T_OK
, 0, 0, 0) != 0) my_e(203);
201 if (READ() != 0) my_e(205);
205 case 1: /* attach to child process */
208 if (pid
< 0) my_e(206);
213 if (READ() != 0) my_e(207);
220 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) my_e(209);
222 if (waitpid(pid
, &status
, 0) != pid
) my_e(210);
223 if (!_WIFSTOPPED(status
)) my_e(211);
224 if (WSTOPSIG(status
) != SIGSTOP
) my_e(212);
226 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(213);
232 case 2: /* attach to non-child process */
235 if (ppid
< 0) my_e(214);
240 if (pid
< 0) exit(215);
245 if (READ() != 0) my_e(216);
256 if (waitpid(pid
, &status
, 0) != pid
) my_e(218);
257 if (_WIFSTOPPED(status
)) my_e(219);
258 if (_WIFEXITED(status
) && (r
= WEXITSTATUS(status
)) != 42) my_e(r
);
265 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) my_e(220);
267 if (waitpid(pid
, &status
, 0) != pid
) my_e(221);
268 if (!_WIFSTOPPED(status
)) my_e(222);
269 if (WSTOPSIG(status
) != SIGSTOP
) my_e(223);
271 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(224);
277 case 3: /* attach by forking from child */
280 if (ppid
< 0) my_e(225);
285 if (ptrace(T_OK
, 0, 0, 0) != 0) my_e(226);
289 if (READ() != 0) my_e(227);
293 if (pid
< 0) my_e(228);
303 if (waitpid(pid
, &status
, 0) != pid
) my_e(230);
304 if (_WIFSTOPPED(status
)) my_e(231);
305 if (_WIFEXITED(status
) && (r
= WEXITSTATUS(status
)) != 42) my_e(r
);
310 if (READ() != 0) my_e(232);
312 if (kill(ppid
, SIGSTOP
) != 0) my_e(233);
314 if (waitpid(ppid
, &status
, 0) != ppid
) my_e(234);
315 if (!_WIFSTOPPED(status
)) my_e(235);
316 if (WSTOPSIG(status
) != SIGSTOP
) my_e(236);
318 if (ptrace(T_SETOPT
, ppid
, 0, TO_TRACEFORK
) != 0) my_e(237);
320 if (ptrace(T_RESUME
, ppid
, 0, 0) != 0) my_e(238);
326 if (waitpid(pid
, &status
, 0) != pid
) my_e(239);
327 if (!_WIFSTOPPED(status
)) my_e(240);
328 if (WSTOPSIG(status
) != SIGSTOP
) my_e(241);
330 if (ptrace(T_SETOPT
, pid
, 0, 0) != 0) my_e(242);
331 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(243);
333 detach_running(ppid
);
341 pid_t
traced_pfork(c
)
346 if (pipe(pfd
) != 0) my_e(300);
347 if (pipe(&pfd
[2]) != 0) my_e(301);
351 if (pid
< 0) my_e(302);
367 if (write(pfd
[child
*2+1], &value
, sizeof(value
)) != sizeof(value
)) my_e(400);
374 if (read(pfd
[2-child
*2], &value
, sizeof(value
)) != sizeof(value
)) my_e(401);
384 if (waitpid(ppid
, &status
, 0) != ppid
) my_e(500);
385 if (!_WIFEXITED(status
)) my_e(501);
386 if ((r
= WEXITSTATUS(status
)) != 0) my_e(r
);
389 /* Quick hack to clean up detached children */
390 waitpid(-1, NULL
, WNOHANG
);
399 void detach_running(pid
)
402 /* Detach from a process that is not already stopped. This is the way to do it.
403 * We have to stop the child in order to detach from it, but as the child may
404 * have other signals pending for the tracer, we cannot assume we get our own
405 * signal back immediately. However, because we know that the kill is instant
406 * and resuming with pending signals will only stop the process immediately
407 * again, we can use T_RESUME for all the signals until we get our own signal,
408 * and then detach. A complicating factor is that anywhere during this
409 * procedure, the child may die (e.g. by getting a SIGKILL). In our tests, this
414 if (kill(pid
, SIGSTOP
) != 0) my_e(600);
416 if (waitpid(pid
, &status
, 0) != pid
) my_e(601);
418 while (_WIFSTOPPED(status
)) {
419 if (WSTOPSIG(status
) == SIGSTOP
) {
420 if (ptrace(T_DETACH
, pid
, 0, 0) != 0) my_e(602);
425 if (ptrace(T_RESUME
, pid
, 0, WSTOPSIG(status
)) != 0) my_e(603);
427 if (waitpid(pid
, &status
, 0) != pid
) my_e(604);
430 /* Apparently the process exited. */
431 if (!_WIFEXITED(status
) && !_WIFSIGNALED(status
)) my_e(605);
433 /* In our tests, that should not happen. */
437 void dummy_handler(sig
)
442 void exit_handler(sig
)
448 void count_handler(sig
)
454 void catch_handler(sig
)
461 case SIGUSR1
: bit
= 1; break;
462 case SIGUSR2
: bit
= 2; break;
463 case SIGTERM
: bit
= 4; break;
468 sigprocmask(SIG_SETMASK
, &set
, NULL
);
470 if (caught
& bit
) my_e(101);
474 void test_wait_child()
486 pid
= traced_fork(test_wait_child
);
488 if (waitpid(pid
, &status
, 0) != pid
) my_e(1);
489 if (!_WIFEXITED(status
)) my_e(2);
490 if (WEXITSTATUS(status
) != 42) my_e(3);
495 void test_exec_child()
497 if (READ() != 0) my_e(100);
499 execl(executable
, "DO CHECK", NULL
);
509 /* This test covers the T_OK case. */
510 if (attach
!= 0) return;
514 pid
= traced_fork(test_exec_child
);
518 /* An exec() should result in a trap signal. */
519 if (waitpid(pid
, &status
, 0) != pid
) my_e(1);
520 if (!_WIFSTOPPED(status
)) my_e(2);
521 if (WSTOPSIG(status
) != SIGTRAP
) my_e(3);
523 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(4);
525 if (waitpid(pid
, &status
, 0) != pid
) my_e(5);
526 if (!_WIFEXITED(status
)) my_e(6);
527 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
532 void test_step_child()
536 signal(SIGUSR1
, SIG_IGN
);
540 if (READ() != 0) my_e(100);
542 /* It must not be possible for the child to stop the single-step signal. */
543 signal(SIGTRAP
, SIG_IGN
);
545 sigprocmask(SIG_SETMASK
, &set
, NULL
);
553 int r
, status
, count
;
557 pid
= traced_fork(test_step_child
);
559 if (READ() != 0) my_e(1);
561 /* While the child is running, neither waitpid() nor ptrace() should work. */
562 if (waitpid(pid
, &status
, WNOHANG
) != 0) my_e(2);
563 if (ptrace(T_RESUME
, pid
, 0, 0) != -1) my_e(3);
564 if (errno
!= EBUSY
) my_e(4);
566 if (kill(pid
, SIGUSR1
) != 0) my_e(5);
570 /* A kill() signal (other than SIGKILL) should be delivered to the tracer. */
571 if (waitpid(pid
, &status
, 0) != pid
) my_e(6);
572 if (!_WIFSTOPPED(status
)) my_e(7);
573 if (WSTOPSIG(status
) != SIGUSR1
) my_e(8);
575 /* ptrace(T_STEP) should result in instruction-wise progress. */
576 for (count
= 0; ; count
++) {
577 if (ptrace(T_STEP
, pid
, 0, 0) != 0) my_e(9);
579 if (waitpid(pid
, &status
, 0) != pid
) my_e(10);
580 if (_WIFEXITED(status
)) break;
581 if (!_WIFSTOPPED(status
)) my_e(11);
582 if (WSTOPSIG(status
) != SIGTRAP
) my_e(12);
585 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
587 if (count
< 10) my_e(13); /* in practice: hundreds */
592 void test_sig_child()
594 signal(SIGUSR1
, exit_handler
);
596 if (READ() != 0) my_e(100);
610 pid
= traced_fork(test_sig_child
);
614 /* allow the child to enter the pause */
617 if (kill(pid
, SIGUSR1
) != 0) my_e(1);
618 if (kill(pid
, SIGUSR2
) != 0) my_e(2);
620 /* All signals should arrive at the tracer, although in "random" order. */
621 if (waitpid(pid
, &status
, 0) != pid
) my_e(3);
622 if (!_WIFSTOPPED(status
)) my_e(4);
623 if (WSTOPSIG(status
) != SIGUSR1
&& WSTOPSIG(status
) != SIGUSR2
) my_e(5);
625 /* The tracer should see kills arriving while the tracee is stopped. */
626 if (kill(pid
, WSTOPSIG(status
)) != 0) my_e(6);
628 if (waitpid(pid
, &status
, WNOHANG
) != pid
) my_e(7);
629 if (!_WIFSTOPPED(status
)) my_e(8);
630 if (WSTOPSIG(status
) != SIGUSR1
&& WSTOPSIG(status
) != SIGUSR2
) my_e(9);
631 sig
= (WSTOPSIG(status
) == SIGUSR1
) ? SIGUSR2
: SIGUSR1
;
633 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(10);
635 if (waitpid(pid
, &status
, 0) != pid
) my_e(11);
636 if (!_WIFSTOPPED(status
)) my_e(12);
637 if (WSTOPSIG(status
) != sig
) my_e(13);
639 if (waitpid(pid
, &status
, WNOHANG
) != 0) my_e(14);
641 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(15);
643 /* Ignored signals passed via ptrace() should be ignored. */
644 if (kill(pid
, SIGUSR1
) != 0) my_e(16);
646 if (waitpid(pid
, &status
, 0) != pid
) my_e(17);
647 if (!_WIFSTOPPED(status
)) my_e(18);
648 if (WSTOPSIG(status
) != SIGUSR1
) my_e(19);
650 if (ptrace(T_RESUME
, pid
, 0, SIGCHLD
) != 0) my_e(20);
652 /* if the pause has been aborted (shouldn't happen!), let the child exit */
655 if (waitpid(pid
, &status
, WNOHANG
) != 0) my_e(21);
657 /* Caught signals passed via ptrace() should invoke their signal handlers. */
658 if (kill(pid
, SIGUSR1
) != 0) my_e(22);
660 if (waitpid(pid
, &status
, 0) != pid
) my_e(23);
661 if (!_WIFSTOPPED(status
)) my_e(24);
662 if (WSTOPSIG(status
) != SIGUSR1
) my_e(25);
664 if (ptrace(T_RESUME
, pid
, 0, SIGUSR1
) != 0) my_e(26);
666 if (waitpid(pid
, &status
, 0) != pid
) my_e(27);
667 if (!_WIFEXITED(status
)) my_e(28);
668 if ((r
= WEXITSTATUS(status
)) != 42) my_e(29);
673 void test_exit_child()
687 pid
= traced_fork(test_exit_child
);
689 if (READ() != 0) my_e(1);
693 if (kill(pid
, SIGSTOP
) != 0) my_e(2);
695 if (waitpid(pid
, &status
, 0) != pid
) my_e(3);
696 if (!_WIFSTOPPED(status
)) my_e(4);
697 if (WSTOPSIG(status
) != SIGSTOP
) my_e(5);
699 /* There should be no more signals pending for the tracer now. */
700 if (waitpid(pid
, &status
, WNOHANG
) != 0) my_e(6);
702 /* ptrace(T_EXIT) should terminate the process with the given exit value. */
703 if (ptrace(T_EXIT
, pid
, 0, 42) != 0) my_e(7);
705 if (waitpid(pid
, &status
, 0) != pid
) my_e(8);
706 if (!_WIFEXITED(status
)) my_e(9);
707 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
712 void test_term_child()
714 signal(SIGUSR1
, SIG_DFL
);
715 signal(SIGUSR2
, dummy_handler
);
731 pid
= traced_fork(test_term_child
);
733 if (READ() != 0) my_e(1);
735 /* If the first of two signals terminates the traced child, the second signal
736 * may or may not be delivered to the tracer - this is merely a policy issue.
737 * However, nothing unexpected should happen.
739 if (kill(pid
, SIGUSR1
) != 0) my_e(2);
740 if (kill(pid
, SIGUSR2
) != 0) my_e(3);
742 if (waitpid(pid
, &status
, 0) != pid
) my_e(4);
743 if (!_WIFSTOPPED(status
)) my_e(5);
745 if (ptrace(T_RESUME
, pid
, 0, SIGUSR1
) != 0) my_e(6);
747 if (waitpid(pid
, &status
, 0) != pid
) my_e(7);
749 if (_WIFSTOPPED(status
)) {
750 if (ptrace(T_RESUME
, pid
, 0, SIGUSR1
) != 0) my_e(8);
752 if (waitpid(pid
, &status
, 0) != pid
) my_e(9);
755 if (!_WIFSIGNALED(status
)) my_e(10);
756 if (WTERMSIG(status
) != SIGUSR1
) my_e(11);
761 void test_catch_child()
766 sa
.sa_handler
= catch_handler
;
767 sigemptyset(&sa
.sa_mask
);
768 sa
.sa_flags
= SA_NODEFER
;
770 sigaction(SIGUSR1
, &sa
, NULL
);
771 sigaction(SIGUSR2
, &sa
, NULL
);
772 sigaction(SIGTERM
, &sa
, NULL
);
775 sigprocmask(SIG_SETMASK
, &set
, &oset
);
781 while (caught
!= 7) sigsuspend(&oset
);
793 pid
= traced_fork(test_catch_child
);
795 if (READ() != 0) my_e(1);
797 if (kill(pid
, SIGUSR1
) != 0) my_e(2);
798 if (kill(pid
, SIGUSR2
) != 0) my_e(3);
800 if (waitpid(pid
, &status
, 0) != pid
) my_e(4);
801 if (!_WIFSTOPPED(status
)) my_e(5);
802 if (WSTOPSIG(status
) != SIGUSR1
&& WSTOPSIG(status
) != SIGUSR2
) my_e(6);
803 sig
= (WSTOPSIG(status
) == SIGUSR1
) ? SIGUSR2
: SIGUSR1
;
805 if (ptrace(T_RESUME
, pid
, 0, WSTOPSIG(status
)) != 0) my_e(7);
807 if (kill(pid
, SIGTERM
) != 0) my_e(8);
809 if (waitpid(pid
, &status
, 0) != pid
) my_e(9);
810 if (!_WIFSTOPPED(status
)) my_e(10);
811 if (WSTOPSIG(status
) != sig
&& WSTOPSIG(status
) != SIGTERM
) my_e(11);
812 if (WSTOPSIG(status
) == sig
) sig
= SIGTERM
;
814 if (ptrace(T_RESUME
, pid
, 0, WSTOPSIG(status
)) != 0) my_e(12);
816 if (kill(pid
, SIGBUS
) != 0) my_e(13);
818 if (waitpid(pid
, &status
, 0) != pid
) my_e(14);
819 if (!_WIFSTOPPED(status
)) my_e(15);
820 if (WSTOPSIG(status
) != sig
&& WSTOPSIG(status
) != SIGBUS
) my_e(16);
822 if (ptrace(T_RESUME
, pid
, 0, sig
) != 0) my_e(17);
824 if (WSTOPSIG(status
) == sig
) sig
= SIGBUS
;
826 if (waitpid(pid
, &status
, 0) != pid
) my_e(18);
827 if (!_WIFSTOPPED(status
)) my_e(19);
828 if (WSTOPSIG(status
) != sig
) my_e(20);
830 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(21);
832 if (waitpid(pid
, &status
, 0) != pid
) my_e(22);
833 if (!_WIFEXITED(status
)) my_e(23);
834 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
839 void test_kill_child()
843 signal(SIGKILL
, SIG_IGN
);
845 sigprocmask(SIG_SETMASK
, &set
, NULL
);
861 pid
= traced_fork(test_kill_child
);
863 if (READ() != 0) my_e(1);
865 /* SIGKILL must be unstoppable in every way. */
866 if (kill(pid
, SIGKILL
) != 0) my_e(2);
868 if (waitpid(pid
, &status
, 0) != pid
) my_e(3);
869 if (!_WIFSIGNALED(status
)) my_e(4);
870 if (WTERMSIG(status
) != SIGKILL
) my_e(5);
872 /* After termination, the child must no longer be visible to the tracer. */
873 if (waitpid(pid
, &status
, WNOHANG
) != -1) my_e(6);
874 if (errno
!= ECHILD
) my_e(7);
879 void test_attach_child()
881 if (ptrace(T_OK
, 0, 0, 0) != -1) my_e(100);
882 if (errno
!= EBUSY
) my_e(101);
886 if (READ() != 0) my_e(102);
897 /* Attaching to kernel processes is not allowed. */
898 if (ptrace(T_ATTACH
, -1, 0, 0) != -1) my_e(1);
899 if (errno
!= ESRCH
) my_e(2);
901 /* Attaching to self is not allowed. */
902 if (ptrace(T_ATTACH
, getpid(), 0, 0) != -1) my_e(3);
903 if (errno
!= EPERM
) my_e(4);
905 /* Attaching to PM is not allowed. */
907 /* FIXME: disabled until we can reliably determine PM's pid */
908 if (ptrace(T_ATTACH
, 0, 0, 0) != -1) my_e(5);
909 if (errno
!= EPERM
) my_e(6);
912 pid
= traced_fork(test_attach_child
);
914 /* Attaching more than once is not allowed. */
915 if (ptrace(T_ATTACH
, pid
, 0, 0) != -1) my_e(7);
916 if (errno
!= EBUSY
) my_e(8);
918 if (READ() != 0) my_e(9);
920 /* Detaching a running child should not succeed. */
921 if (ptrace(T_DETACH
, pid
, 0, 0) == 0) my_e(10);
922 if (errno
!= EBUSY
) my_e(11);
931 void test_detach_child()
934 sigset_t set
, sset
, oset
;
936 sa
.sa_handler
= catch_handler
;
937 sigemptyset(&sa
.sa_mask
);
938 sa
.sa_flags
= SA_NODEFER
;
940 sigaction(SIGUSR1
, &sa
, NULL
);
941 sigaction(SIGUSR2
, &sa
, NULL
);
942 sigaction(SIGTERM
, &sa
, NULL
);
945 sigprocmask(SIG_SETMASK
, &set
, &oset
);
948 sigdelset(&sset
, SIGUSR1
);
954 if (sigsuspend(&sset
) != -1) my_e(102);
955 if (errno
!= EINTR
) my_e(103);
957 if (caught
!= 1) my_e(104);
959 if (READ() != 0) my_e(105);
961 while (caught
!= 7) sigsuspend(&oset
);
971 /* Can't use traced_fork(), so simplify a bit */
972 if (attach
!= 0) return;
976 pid
= traced_pfork(test_detach_child
);
978 if (READ() != 0) my_e(1);
980 /* The tracer should not see signals sent to the process before attaching. */
981 if (kill(pid
, SIGUSR2
) != 0) my_e(2);
983 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) my_e(3);
985 if (waitpid(pid
, &status
, 0) != pid
) my_e(4);
986 if (!_WIFSTOPPED(status
)) my_e(5);
987 if (WSTOPSIG(status
) != SIGSTOP
) my_e(6);
989 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) my_e(7);
991 if (kill(pid
, SIGUSR1
) != 0) my_e(8);
993 if (waitpid(pid
, &status
, 0) != pid
) my_e(9);
994 if (!_WIFSTOPPED(status
)) my_e(10);
995 if (WSTOPSIG(status
) != SIGUSR1
) my_e(11);
997 /* Signals pending at the tracer should be passed on after detaching. */
998 if (kill(pid
, SIGTERM
) != 0) my_e(12);
1000 /* A signal may be passed with the detach request. */
1001 if (ptrace(T_DETACH
, pid
, 0, SIGUSR1
) != 0) my_e(13);
1005 if (waitpid(pid
, &status
, 0) != pid
) my_e(14);
1006 if (!_WIFEXITED(status
)) my_e(15);
1007 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
1012 void test_death_child()
1018 if (pid
< 0) my_e(100);
1021 ptrace(T_OK
, 0, 0, 0);
1028 if (READ() != 0) my_e(101);
1030 kill(getpid(), SIGKILL
);
1042 pid
= traced_fork(test_death_child
);
1046 if (kill(cpid
, 0) != 0) my_e(1);
1050 if (waitpid(pid
, &status
, 0) != pid
) my_e(2);
1051 if (!_WIFSIGNALED(status
)) my_e(3);
1052 if (WTERMSIG(status
) != SIGKILL
) my_e(4);
1054 /* The children of killed tracers should be terminated. */
1055 while (kill(cpid
, 0) == 0) sleep(1);
1056 if (errno
!= ESRCH
) my_e(5);
1061 void test_zdeath_child()
1063 if (READ() != 0) my_e(100);
1073 /* Can't use traced_fork(), so simplify a bit */
1074 if (attach
!= 0) return;
1078 pid
= traced_pfork(test_zdeath_child
);
1082 if (tpid
< 0) my_e(1);
1085 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) exit(101);
1087 if (waitpid(pid
, &status
, 0) != pid
) exit(102);
1088 if (!_WIFSTOPPED(status
)) exit(103);
1089 if (WSTOPSIG(status
) != SIGSTOP
) exit(104);
1091 if (ptrace(T_RESUME
, pid
, 0, 0) != 0) exit(105);
1095 /* Unwaited-for traced zombies should be passed to their parent. */
1103 /* However, that should only happen once the tracer has actually died. */
1104 if (waitpid(pid
, &status
, WNOHANG
) != 0) my_e(2);
1106 if (waitpid(tpid
, &status
, 0) != tpid
) my_e(3);
1107 if (!_WIFEXITED(status
)) my_e(4);
1108 if ((r
= WEXITSTATUS(status
)) != 84) my_e(r
);
1110 if (waitpid(pid
, &status
, 0) != pid
) my_e(5);
1111 if (!_WIFEXITED(status
)) my_e(6);
1112 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
1117 void test_syscall_child()
1119 signal(SIGUSR1
, count_handler
);
1120 signal(SIGUSR2
, count_handler
);
1126 if (READ() != 0) my_e(100);
1128 /* Three calls (may fail) */
1133 if (sigs
!= 2) my_e(101);
1141 int i
, r
, sig
, status
;
1145 pid
= traced_fork(test_syscall_child
);
1147 if (READ() != 0) my_e(1);
1149 if (kill(pid
, SIGSTOP
) != 0) my_e(2);
1151 if (waitpid(pid
, &status
, 0) != pid
) my_e(3);
1152 if (!_WIFSTOPPED(status
)) my_e(4);
1153 if (WSTOPSIG(status
) != SIGSTOP
) my_e(5);
1157 /* Upon resuming a first system call, no syscall leave event must be sent. */
1158 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(6);
1160 if (waitpid(pid
, &status
, 0) != pid
) my_e(7);
1162 for (i
= 0; _WIFSTOPPED(status
); i
++) {
1163 if (WSTOPSIG(status
) != SIGTRAP
) my_e(8);
1165 /* Signals passed via T_SYSCALL should arrive, on enter and exit. */
1166 if (i
== 3) sig
= SIGUSR1
;
1167 else if (i
== 6) sig
= SIGUSR2
;
1170 if (ptrace(T_SYSCALL
, pid
, 0, sig
) != 0) my_e(9);
1172 if (waitpid(pid
, &status
, 0) != pid
) my_e(10);
1175 if (!_WIFEXITED(status
)) my_e(11);
1176 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
1178 /* The number of events seen is deterministic but libc-dependent. */
1179 if (i
< 10 || i
> 100) my_e(12);
1181 /* The last system call event must be for entering exit(). */
1182 if (!(i
% 2)) my_e(13);
1187 void test_tracefork_child()
1191 signal(SIGHUP
, SIG_IGN
);
1197 if (READ() != 0) my_e(100);
1199 if ((pid
= fork()) < 0) my_e(101);
1201 exit(pid
> 0 ? 42 : 84);
1204 void test_tracefork()
1206 pid_t pgrp
, ppid
, cpid
, wpid
;
1207 int r
, status
, gotstop
, ptraps
, ctraps
;
1211 ppid
= traced_fork(test_tracefork_child
);
1213 if ((pgrp
= READ()) <= 0) my_e(1);
1215 if (kill(ppid
, SIGSTOP
) != 0) my_e(2);
1217 if (waitpid(ppid
, &status
, 0) != ppid
) my_e(3);
1218 if (!_WIFSTOPPED(status
)) my_e(4);
1219 if (WSTOPSIG(status
) != SIGSTOP
) my_e(5);
1221 if (ptrace(T_SETOPT
, ppid
, 0, TO_TRACEFORK
) != 0) my_e(6);
1225 if (ptrace(T_SYSCALL
, ppid
, 0, 0) != 0) my_e(7);
1230 /* Count how many traps we get for parent and child, until they both exit. */
1231 for (ptraps
= ctraps
= 0; ppid
|| cpid
; ) {
1232 wpid
= waitpid(-pgrp
, &status
, 0);
1234 if (wpid
<= 0) my_e(8);
1235 if (cpid
< 0 && wpid
!= ppid
) {
1239 if (wpid
!= ppid
&& wpid
!= cpid
) my_e(9);
1241 if (_WIFEXITED(status
)) {
1243 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
1247 if ((r
= WEXITSTATUS(status
)) != 84) my_e(r
);
1252 if (!_WIFSTOPPED(status
)) my_e(10);
1254 switch (WSTOPSIG(status
)) {
1259 if (wpid
!= cpid
) my_e(11);
1260 if (gotstop
) my_e(12);
1264 if (wpid
== ppid
) ptraps
++;
1271 if (ptrace(T_SYSCALL
, wpid
, 0, 0) != 0) my_e(14);
1275 /* The parent should get an odd number of traps: the first one is a syscall
1276 * enter trap (typically for the fork()), the last one is the syscall enter
1277 * trap for its exit().
1279 if (ptraps
< 3) my_e(15);
1280 if (!(ptraps
% 2)) my_e(16);
1282 /* The child should get an even number of traps: the first one is a syscall
1283 * leave trap from the fork(), the last one is the syscall enter trap for
1286 if (ctraps
< 2) my_e(17);
1287 if (ctraps
% 2) my_e(18);
1292 void sigexec(setflag
, opt
, traps
, stop
)
1301 pid
= traced_fork(test_exec_child
);
1303 if (kill(pid
, SIGSTOP
) != 0) my_e(1);
1305 if (waitpid(pid
, &status
, 0) != pid
) my_e(2);
1306 if (!_WIFSTOPPED(status
)) my_e(3);
1307 if (WSTOPSIG(status
) != SIGSTOP
) my_e(4);
1309 if (setflag
&& ptrace(T_SETOPT
, pid
, 0, opt
) != 0) my_e(5);
1313 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(6);
1319 if (waitpid(pid
, &status
, 0) != pid
) my_e(7);
1321 if (_WIFEXITED(status
)) break;
1323 if (!_WIFSTOPPED(status
)) my_e(8);
1325 switch (WSTOPSIG(status
)) {
1330 if (*stop
>= 0) my_e(9);
1337 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(11);
1340 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
1345 void test_trapexec()
1351 sigexec(1, 0, &traps
, &stop
);
1353 /* The exec does not cause a SIGSTOP. This gives us an even number of traps;
1354 * as above, but plus the exec()'s extra SIGTRAP. This trap is
1355 * indistinguishable from a syscall trap, especially when considering failed
1356 * exec() calls and immediately following signal handler invocations.
1358 if (traps
< 4) my_e(12);
1359 if (traps
% 2) my_e(13);
1360 if (stop
>= 0) my_e(14);
1369 sigexec(1, TO_ALTEXEC
, &traps
, &stop
);
1371 /* The exec causes a SIGSTOP. This gives us an odd number of traps: a pair
1372 * for each system call, plus one for the final exit(). The stop must have
1373 * taken place after a syscall enter event, i.e. must be odd as well.
1375 if (traps
< 3) my_e(12);
1376 if (!(traps
% 2)) my_e(13);
1377 if (stop
< 0) my_e(14);
1378 if (!(stop
% 2)) my_e(15);
1387 sigexec(1, TO_NOEXEC
, &traps
, &stop
);
1389 /* The exec causes no signal at all. As above, but without the SIGSTOPs. */
1390 if (traps
< 3) my_e(12);
1391 if (!(traps
% 2)) my_e(13);
1392 if (stop
>= 0) my_e(14);
1399 /* We want to test the default of T_OK (0) and T_ATTACH (TO_NOEXEC). */
1400 if (attach
!= 0 && attach
!= 1) return;
1404 /* Do not set any options this time. */
1405 sigexec(0, 0, &traps
, &stop
);
1409 if (traps
< 4) my_e(12);
1410 if (traps
% 2) my_e(13);
1411 if (stop
>= 0) my_e(14);
1414 if (traps
< 3) my_e(15);
1415 if (!(traps
% 2)) my_e(16);
1416 if (stop
>= 0) my_e(17);
1420 void test_reattach_child()
1424 if (READ() != 0) my_e(100);
1428 if (select(0, NULL
, NULL
, NULL
, &tv
) != 0) my_e(101);
1433 void test_reattach()
1436 int r
, status
, count
;
1440 pid
= traced_fork(test_reattach_child
);
1442 if (kill(pid
, SIGSTOP
) != 0) my_e(1);
1444 if (waitpid(pid
, &status
, 0) != pid
) my_e(2);
1445 if (!_WIFSTOPPED(status
)) my_e(3);
1446 if (WSTOPSIG(status
) != SIGSTOP
) my_e(4);
1450 signal(SIGALRM
, dummy_handler
);
1453 /* Start tracing system calls. We don't know how many there will be until
1454 * we reach the child's select(), so we have to interrupt ourselves.
1455 * The hard assumption here is that the child is able to enter the select()
1456 * within a second, despite being traced. If this is not the case, the test
1457 * may hang or fail, and the child may die from a SIGTRAP.
1459 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(5);
1461 for (count
= 0; (r
= waitpid(pid
, &status
, 0)) == pid
; count
++) {
1462 if (!_WIFSTOPPED(status
)) my_e(6);
1463 if (WSTOPSIG(status
) != SIGTRAP
) my_e(7);
1465 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(8);
1468 if (r
!= -1 || errno
!= EINTR
) my_e(9);
1470 /* We always start with syscall enter event; the last event we should have
1471 * seen before the alarm was entering the select() call.
1473 if (!(count
% 2)) my_e(10);
1475 /* Detach, and immediately attach again. */
1476 detach_running(pid
);
1478 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) my_e(11);
1480 if (waitpid(pid
, &status
, 0) != pid
) my_e(12);
1481 if (!_WIFSTOPPED(status
)) my_e(13);
1482 if (WSTOPSIG(status
) != SIGSTOP
) my_e(14);
1484 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(15);
1486 if (waitpid(pid
, &status
, 0) != pid
) my_e(16);
1488 for (count
= 0; _WIFSTOPPED(status
); count
++) {
1489 if (WSTOPSIG(status
) != SIGTRAP
) my_e(17);
1491 if (ptrace(T_SYSCALL
, pid
, 0, 0) != 0) my_e(18);
1493 if (waitpid(pid
, &status
, 0) != pid
) my_e(19);
1496 if (!_WIFEXITED(status
)) my_e(20);
1497 if ((r
= WEXITSTATUS(status
)) != 42) my_e(r
);
1499 /* We must not have seen the select()'s syscall leave event, and the last
1500 * event will be the syscall enter for the exit().
1502 if (!(count
% 2)) my_e(21);