For test1.c
, I get:
5
133
1
0
It means that the child process firstly get SIGTRAP
(5), cause by execl
.
The last three lines indicate that the child process dies due to the SIGSTRAP
signal from the parent.
// test1.c
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/reg.h>
#include <stdio.h>
#include <pthread.h>
int main() {
pid_t child;
int status = 0;
child = fork();
if(child == 0) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execl("/bin/ls", "ls", NULL);
} else {
wait(&status);
printf("%d\n", status >> 8);
ptrace(PTRACE_CONT, child, NULL, SIGTRAP);
wait(&status);
printf("%d\n", status);
printf("%d\n", WIFSIGNALED(status));
printf("%d\n", WIFEXITED(status));
}
return 0;
}
For test2.c
, I get:
19
1029
0
0
1
19 is SIGSTOP
, and 1029
is (SIGTRAP | (PTRACE_EVENT_EXEC<<8))
, but the
last threes lines are beyond me. Why does the child process exit normally? What happend to the SIGTRAP
signal from the parent?
// test2.c
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/reg.h>
#include <stdio.h>
#include <pthread.h>
int main() {
pid_t child;
int status = 0;
child = fork();
if(child == 0) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
raise(SIGSTOP);
execl("/bin/ls", "ls", NULL);
} else {
wait(&status);
ptrace(PTRACE_SETOPTIONS, child, 0, PTRACE_O_TRACEEXEC);
printf("%d\n", status >> 8);
ptrace(PTRACE_CONT, child, NULL, 0);
wait(&status);
printf("%d\n", status >> 8);
ptrace(PTRACE_CONT, child, NULL, SIGTRAP);
wait(&status);
printf("%d\n", status);
printf("%d\n", WIFSIGNALED(status));
printf("%d\n", WIFEXITED(status));
}
return 0;
}