1. systemtap安装

systemtap是一个强大的内核开发调试工具。

stap 执行编写的脚本动作。流程如下:

  • 1.parse:分析脚本语法
  • 2.elaborate:展开脚本 中定义的探针和连接预定义脚本库,分析内核和内核模块的调试信息
  • 3.translate:.将脚本编译成c语言内核模块文件放 在$HOME/xxx.c 缓存起来,避免同一脚本多次编译
  • 4.build:将c语言模块文件编译成.ko的内核模块,也缓存起来。
    1. 把模块交给staprun,staprun加载内核模块到内核空间,stapio连接内核模块和用户空间,提供交互IO通道,采集数据。

systemtap 运行需要内核的调试信息支撑,默认发行版的内核在配置时这些调试开关没有打开,所以安装完systemtap也是无法去探测内核信息的

安装systemtap:

1.1. 方法一:

编译内核以支持systemtap //不合适

1.2. 方法二:

安装调试内核镜像

如果先安装了systemtap,有stap-prep命令,可以快速安装内核镜像,prep是通过调用yum来安装,如果yum中没有相应的内核镜像软件包,也是无法安装的

stap-prep

如果prep不能安装则需要手动安装,必须安装内核对应的内核镜像版本,内核信息包

[root@VM_11_7_centos systemtap-3.0]# uname  -a
Linux VM_11_7_centos 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

kernel-devel.x86_64 0:3.10.0-327.el7
kernel-debuginfo-3.10.0-327.el7.x86_64

systemtap运行还需要elfutils的支持,最新的systemtap需要使用最新的elfutils

elfutils

部署 SystemTap,需要安装以下两个 RPM 包:

systemtap
systemtap-runtime

TEST

root@localhost ~]# stap -ve 'probe begin { log("hello world") exit() }'
Pass 1: parsed user script and 115 library script(s) using 230072virt/47472res/3092shr/44892data kb, in 80usr/120sys/197real ms.
Pass 2: analyzed script: 1 probe(s), 2 function(s), 0 embed(s), 0 global(s) using 231128virt/48792res/3212shr/45948data kb, in 0usr/10sys/8real ms.
Pass 3: using cached /root/.systemtap/cache/7f/stap_7f72777f8257e9a432ad3115abd8362f_1030.c
Pass 4: using cached /root/.systemtap/cache/7f/stap_7f72777f8257e9a432ad3115abd8362f_1030.ko
Pass 5: starting run.
hello world
Pass 5: run completed in 10usr/80sys/819real ms.

正常情况下能都看到hello world,单这个测试只能测试软件都安装正常,正常使用还需要一些涉及到内核模块的测试

#!/usr/bin/stap
probe begin 
{
    log("begin to probe")
}

probe syscall.open
{
    printf ("%s(%d) open (%s)\n", execname(), pid(), argstr)
}

probe timer.ms(4000) # after 4 seconds
{
    exit ()
}

probe end
{
    log("end to probe")
}

这个脚本检测4秒内,调用open的进程

[root@localhost vinny]# stap open.stp 
begin to probe
gnome-shell(12291) open ("/proc/self/stat", O_RDONLY)
gnome-shell(12291) open ("/proc/self/stat", O_RDONLY)
gnome-shell(12291) open ("/proc/self/stat", O_RDONLY)
gnome-shell(12291) open ("/proc/self/stat", O_RDONLY)
gnome-shell(12291) open ("/proc/self/stat", O_RDONLY)
gnome-shell(12291) open ("/proc/self/stat", O_RDONLY)
gnome-shell(12291) open ("/proc/self/stat", O_RDONLY)
gnome-shell(12291) open ("/proc/self/stat", O_RDONLY)
gnome-shell(12291) open ("/proc/self/stat", O_RDONLY)
gnome-shell(12291) open ("/proc/self/stat", O_RDONLY)
gnome-shell(12291) open ("/proc/self/stat", O_RDONLY)
^Cgnome-shell(12291) open ("/proc/self/stat", O_RDONLY)
vmtoolsd(866) open ("/proc/net/dev", O_RDONLY)
vmtoolsd(866) open ("/proc/net/if_inet6", O_RDONLY)
vmtoolsd(866) open ("/proc/net/if_inet6", O_RDONLY)
vmtoolsd(866) open ("/proc/net/if_inet6", O_RDONLY)
vmtoolsd(866) open ("/proc/net/if_inet6", O_RDONLY)
gnome-shell(12291) open ("/proc/self/stat", O_RDONLY)
end to probe

正常的输入如上,但是在tlinux下面,总会出现如下保存

Tlinux

ERROR: Couldn't insert module '/tmp/stapKOJEJH/stap_53172c9aef79113568db5a2ab948f356_1228.ko': Required key not available

这是由于公司安全考虑,在tlinux系统上,所有加载到内核的模块都要通过公司认证 http://sign.tlinux.oa.com/cgi-bin/sign_svr/cgi-bin/login.cgi?url=http://sign.tlinux.oa.com:80/cgi-bin/sign_svr/cgi-bin/index.cgi

1.3. 参考

http://www.cnblogs.com/hazir/p/systemtap_introduction.html https://dirtysalt.github.io/systemtap.html

Copyright © 温玉 2021 | 浙ICP备2020032454号 all right reserved,powered by Gitbook该文件修订时间: 2023-06-19 08:59:50

results matching ""

    No results matching ""