linux syscall

来源:互联网 发布:als矩阵分解推荐模型 编辑:程序博客网 时间:2024/05/16 17:03

2011-4-18

这部分是在学习电源管理程序时记录的一些碎片

linux内核跟踪

syscall tracer(1)

syscall tracer是用于跟踪系统调用的,它会检测所有系统调用的入口和出口,再将相关的信息保存到ring buffer

比如,echo syscall > current_tracer

     Cat trace | tail

 

1.

Syscall函数打开并写文件

#include<sys/syscall.h>

#include<unistd.h>

 

Int mian(int argc,char **argv)

{

  Int fd;

  Fd=syscall(SYS_open,”destfile”,O_WRONLY);

  Static char buf[]=”abcd”;

  Syscall(SYS_write,fd,buf,4);

  Syscall(SYS_close,fd);

  Return 0;

}

2.

参考下图,

                                  

                  

               

                      

添加自己的系统调用,(参考上面两个图)

(1)   Include/asm/unistd.h

#define __NR_myfunc 222

(2) include/bits/syscall.h

#define SYS_myfunc __NR_myfunc

 

syscall_table.S中添加

.long SYMBOL_NAME(sys_myfunc)

 

(3) kernel/sys.c 添加调用实现

       Asmlinkageintsys_myfunc(int input){

       Printk(“”<1> input value is %d “”,input);

       Return input*10;

}

重新编译内核

 

(4) user space 程式

 #include

Static inline _syscall1(int,myfunc,int,a)

Int main(void)

{

       Printf(“return value %d”,myfunc(10));

}

_syscall1是一个macro指令,事实上是_syscallN的指令,N表示系统调用所需要用到的参数个数,_syscall1就是用到一个参数

_syscallN(arg1,arg2,arg3,arg4)

Arg1: 代表传回值

Arg2: syscall name

Arg3: 传入参数的类型

Arg4: 代表的是传入参数的名称

 

 

原创粉丝点击