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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章