centos7 systemtap 介绍

0. 查看环境:

# uname -a
Linux hw005 3.10.0-327.10.1.el7.x86_64 #1 SMP Tue Feb 16 17:03:50 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

1. yum install kernel-devel

2. yum install systemtap

3. install kernel/glib debug:

glibc-debuginfo-2.17-106.el7_2.4.x86_64.rpm
glibc-debuginfo-common-2.17-106.el7_2.4.x86_64.rpm
kernel-debuginfo-3.10.0-327.10.1.el7.x86_64.rpm
kernel-debuginfo-common-x86_64-3.10.0-327.10.1.el7.x86_64.rpm

ref: http://debuginfo.centos.org/7

4. 测试是否安装ok:

# # 测试stap
# stap -V
Systemtap translator/driver (version 2.8/0.163, rpm 2.8-10.el7)
Copyright (C) 2005-2015 Red Hat, Inc. and others
This is free software; see the source for copying conditions.
enabled features: AVAHI LIBRPM LIBSQLITE3 NSS BOOST_SHARED_PTR TR1_UNORDERED_MAP NLS DYNINST JAVA LIBVIRT LIBXML2
#
# # 测试kernel-debuginfo
# stap -L 'kernel.function("printk")'
kernel.function("printk@kernel/printk.c:1693") $fmt:char const* $args:va_list
# 
# # 测试glibc-debuginfo
# stap -L 'process("/lib64/libc.so.6").function("malloc")'
process("/usr/lib64/libc-2.17.so").function("malloc"

5. 一个测试例子:查找内存泄露

# # C 源文件:
# cat t.c
#include <stdlib.h>

void fun() {
    malloc(1000);
}

int main(int argc, char *argv[]) {
    fun();
    return 0;
}
#
# # systemtap 脚本:
# cat m.stp 
probe process("/lib64/libc.so.6").function("malloc") {
    if (target()== pid()) {
        print_ubacktrace();
        exit();
    }
}
probe begin {
    println("~");
}
#
# # 编译t.cat:
# gcc -g t.c
#
# # 查看 a.out 的函数调用栈:
# stap -L 'process("./a.out").function("*")'
process("/root/systemtap/test/a.out").function("__do_global_dtors_aux")
process("/root/systemtap/test/a.out").function("__libc_csu_fini")
process("/root/systemtap/test/a.out").function("__libc_csu_init")
process("/root/systemtap/test/a.out").function("_fini")
process("/root/systemtap/test/a.out").function("_init")
process("/root/systemtap/test/a.out").function("_start")
process("/root/systemtap/test/a.out").function("deregister_tm_clones")
process("/root/systemtap/test/a.out").function("frame_dummy")
process("/root/systemtap/test/a.out").function("fun@/root/systemtap/test/t.c:3")
process("/root/systemtap/test/a.out").function("main@/root/systemtap/test/t.c:7") $argc:int $argv:char**
process("/root/systemtap/test/a.out").function("register_tm_clones")
#
# # 查看 malloc 调用情况:
# stap m.stp -c ./a.out 
~
 0x7fed3b435890 : __libc_malloc+0x0/0xe0 [/usr/lib64/libc-2.17.so]
 0x40053e [/root/systemtap/test/a.out+0x53e/0x1000]
WARNING: Missing unwind data for module, rerun with 'stap -d /root/systemtap/test/a.out'
#
# # 查看调用 malloc 的代码
# addr2line ./a.out 0x40053e
??:0
/root/systemtap/test/t.c:5

你可能感兴趣的:(linux,centos,systemtap)