今天调试ptrace的一个用例,里面用到了excel这个函数:
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/reg.h> /* For constants ORIG_RAX etc */
#include <stdio.h>
int main()
{
pid_t child;
long orig_rax;
child = fork();
if(child == 0)
{
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
printf("Try to call: execl\n");
execl("/bin/ls", "ls", NULL);
printf("child exit\n");
}
else
{
wait(NULL);
orig_rax = ptrace(PTRACE_PEEKUSER,
child, 8 * ORIG_RAX,
NULL);
printf("The child made a "
"system call %ld\n", orig_rax);
ptrace(PTRACE_CONT, child, NULL, NULL);
printf("Try to call:ptrace\n");
}
return 0;
}
上面的代码来自:https://blog.csdn.net/litost000/article/details/82813641
运行的时候发现,child exit 永远打印不出来,尝试了各种办法,因为之前从来没用过excel函数,对其不了解,所以也没向它考虑,后来通过最笨的方式进行尝试,验证出就是excel的问题,然后一查,果然。。。。。
excel的一个特点:
当进程调用一种exec函数时,该进程完全由新程序代换,而新程序则从其main函数开始执行。因为调用exec并不创建新进程,所以前后的进程ID并未改变。exec只是用另一个新程序替换了当前进程的正文、数据、堆和栈段。
用另一个新程序替换了当前进程的正文、数据、堆和栈段。
当前进程的正文都被替换了,那么execl后的语句,即便execl退出了,都不会被执行
上面的话来自:https://blog.csdn.net/bao_bei/article/details/48287945
但是对其参数也不了解,也就顺便查了一下:
Linux下头文件
#include <unistd.h>
函数定义
int execl(const char *path, const char *arg, ...)
实例1
/*
* execl函数使用实例1
*功能:执行/bin/ls -al /etc/passwd
* */
#include <unistd.h>
int main(void)
{
//执行/bin目录下的ls
//第一个参数为程序名ls,第二个参数为-al,第三个参数为/etc/passwd
execl("/bin/ls", "ls","-al","/etc/passwd",(char*)0);
return 0;
}
实例2
include <unistd.h>
int
main(void)
{
char args[] = "-l";
/*
* 执行/bin目录下的ls,第一参数为程序名ls,
* 第二个参数为-al,第三个参数为/etc/
*/
execl("/bin/ls","ls","-al","/etc/",NULL);
return 0;
}
上面的2个例子来自:https://blog.csdn.net/ahuang1900/article/details/38169769
excel的第一个参数是关键,第二个参数只是个名称而已,可以随便写——当然,为了代码的功能清楚,乱写也没有必要;