先看上一篇:
https://blog.csdn.net/dog250/article/details/108230157
不够精简是不是?
那是因为我的水平还不够6,其实stap是可以直接调用system来执行外部命令的,如此就不需要再进入guru模式来stap自己了。
脚本如下:
#!/usr/local/bin/stap
// selftap
probe process("/usr/local/bin/stap").function("make_any_make_cmd")
{
ext = "\'EXTRA_CFLAGS += -I$(STAP_INCLUDE)\'";
file = sprintf("%s/Makefile", user_string($dir->_M_local_buf));
cmd = sprintf("/usr/bin/echo %s \>>%s", ext, file);
// 有system命令干嘛还在内核空间写文件...
system(cmd);
}
够简单吧。来来来,看效果:
[root@localhost test]# cat aa.stp
#!/usr/local/bin/stap -g
%{
#include "common.h"
%}
function func(who:long)
%{
STAP_PRINTF("%d %lu\n", VAR, STAP_ARG_who);
%}
probe begin
{
func($1);
exit();
}
[root@localhost test]# /usr/local/bin/stap -g ./aa.stp 191
/tmp/stapKTguZf/stap_7a62a2c6930488205b2f13dbba9d80c5_1310_src.c:29:20: 致命错误:common.h:没有那个文件或目录
#include "common.h"
^
编译中断。
make[1]: *** [/tmp/stapKTguZf/stap_7a62a2c6930488205b2f13dbba9d80c5_1310_src.o] 错误 1
make: *** [_module_/tmp/stapKTguZf] 错误 2
WARNING: kbuild exited with status: 2
Pass 4: compilation failed. [man error::pass4]
[root@localhost test]# ./selftap -c '/usr/local/bin/stap -g ./aa.stp 191'
100 191
[root@localhost test]#
附说一句,其实,我在脚本里的cmd有点硬编码了,正确的做法应该是:
file = sprintf("%s/Makefile", user_string($dir->_M_local_buf));
cmd = sprintf("./mycmd %s", file);
我只需要将Makefile的全路径传进去,我就可以往Makefile里写更多的东西了,比如再加一个obj-m += XXX.o
嗯,这样更好。
浙江温州皮鞋湿,下雨进水不会胖。