- 查看环境:
- uname -r
- yum install kernel-devel
- yum install systemtap
- install kernel/glib debug:
glibc-debuginfo-2.17-106.el7_2.8.x86_64.rpm
glibc-debuginfo-common-2.17-106.el7_2.8.x86_64.rpm
kernel-debuginfo-3.10.0-327.28.3.el7.x86_64.rpm
kernel-debuginfo-common-x86_64-3.10.0-327.28.3.el7.x86_64.rpm
ref: http://debuginfo.centos.org/7 - 测试是否安装ok:
6.1 测试stap
stap -V
Systemtap translator/driver (version 3.0/0.163/0.166, rpm 3.0-7.el7)
Copyright (C) 2005-2015 Red Hat, Inc. and others
This is free software; see the source for copying conditions.
enabled features: AVAHI BOOST_SHARED_PTR BOOST_STRING_REF DYNINST JAVA LIBRPM LIBSQLITE3 LIBVIRT LIBXML2 NLS NSS TR1_UNORDERED_MAP READLINE
6.2 测试kernel-debuginfo
stap -L ‘kernel.function(“printk”)’
kernel.function(“printk@kernel/printk.c:1717”)
fmt:charconst∗
args:va_list
6.3 测试glibc-debuginfo
stap -L ‘process(“/lib64/libc.so.6”).function(“malloc”)’
process(“/usr/lib64/libc2.17.so”).function(“__libc_malloc@/usr/src/debug/glibc-2.17-c758a686/malloc/malloc.c:2883”) $bytes:size_t - 一个测试例子:查找内存泄露
# # 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