请问这是什么情况
pid_t pid = fork();
if (pid == 0) {
if(ptrace(PTRACE_TRACEME, 0, NULL, NULL) == -1) {
printf("error in child: %s\n", strerror(errno));
exit(2);
}
write(2, "hello\n", 6); //情况A
//execlp("/home/bman/HelloWolrd", ""); //情况B
} else {
while(1) {
int status = 0;
wait(&status);
if (WIFEXITED(status)) {
break;
}
user_regs_struct reg;
int ret = ptrace(PTRACE_GETREGS, pid, NULL, ® );
if (ret == -1) {
printf("%s\n", strerror(errno));
exit(2);
}
printf("ret %d\n", ret);
printf("syscall %d\n", (int)reg.orig_eax);
getchar();
ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
}
}
情况A的子进程直接输出hello后退出
情况B的子进程会在每次系统调用前中断,知道父进程执行ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
后继续运行。
请问这是什么情况
pid_t pid = fork();
if (pid == 0) {
if(ptrace(PTRACE_TRACEME, 0, NULL, NULL) == -1) {
printf("error in child: %s\n", strerror(errno));
exit(2);
}
write(2, "hello\n", 6); //情况A
//execlp("/home/bman/HelloWolrd", ""); //情况B
} else {
while(1) {
int status = 0;
wait(&status);
if (WIFEXITED(status)) {
break;
}
user_regs_struct reg;
int ret = ptrace(PTRACE_GETREGS, pid, NULL, ® );
if (ret == -1) {
printf("%s\n", strerror(errno));
exit(2);
}
printf("ret %d\n", ret);
printf("syscall %d\n", (int)reg.orig_eax);
getchar();
ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
}
}
情况A的子进程直接输出hello后退出
情况B的子进程会在每次系统调用前中断,知道父进程执行ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
后继续运行。
参考:
http://thorstenball.com/blog/2014/06/13/where-did-fork-go/
ptrace
fork
execve