systemtap引用自定义头文件的手艺精简版

先看上一篇:
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

嗯,这样更好。


浙江温州皮鞋湿,下雨进水不会胖。

原文链接: https://blog.csdn.net/dog250/article/details/108234728

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    systemtap引用自定义头文件的手艺精简版

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/405843

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月26日 上午9:28
下一篇 2023年4月26日 上午9:28

相关推荐