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: 代表的是傳入參數的名稱