文章目錄
相關技術:utrace, probe, ftrace, dtrace, pstrace
- centos安裝systemtap
- 使用執行個體
- 參考
原文串連
參考串連
重點是需要核心的匹配
1.首先先查看你的核心版本
uname -a
2.6.18-194.el5
如果你的核心版本比較老的話,你需要去尋找你需要的kernel-devel 的版本.
如果你想用yum去安裝,你可以用 yum install kernel-devel
注意:在最新的yum裡面安裝的總是最新的kernel-devel的版本,kernel-devel 需要匹配核心版本
比如: CentOS release 5.5 (Final)用的是核心版本 2.6.18-194.el5
先下載 和核心版本一樣的對應的
然後安裝
rpm-ivh kernel-devel-2.6.18-194.el5-i686.rpm
安裝好kernel-devel會有核心的目錄在
/usr/src/kernels/2.6.18-194.el5-i686/
2.安裝systemtap
yum install systemtap (stap -V 1.3/0.137 請安裝elfutils-devel 才能提示你需要安裝哪個版本的debuginfo)
3. 安裝debuginfo 去
http://debuginfo.centos.org
尋找和你核心完全符合
(例如本機uname -r 2.6.18-194.el5 只能選
kernel-debuginfo-2.6.18-194.el5.i686.rpm
kernel-debuginfo-common-2.6.18-194.el5.i686.rpm
)
kernel-debuginfo-common-xxxxx
kernel-debuginfo-xxxx
如果你找不到對應的核心版本的rpm,你可以去google 去尋找,可以使用redhat的debuginfo rpm 包
redhat核心參考下載
http://rpm.pbone.net/index.php3/stat/4/idpl/13968571/dir/redhat_el_5/com/kernel-debuginfo-common-2.6.18-128.el5.x86_64.rpm.html
ftp://ftp.pbone.net/mirror/ftp.redhat.com/pub/redhat/linux/enterprise/5Server/en/os/x86_64/Debuginfo/kernel-debuginfo-2.6.18-128.el5.x86_64.rpm
安裝rpm -ivh kernel-debuginfo*.rpm
如何測試:
stap -ve 'probe begin { log("hello world") exit() }'
備忘
官方推薦的安裝命令
2 yum install systemtap kernel-devel yum-utils
debuginfo-install kernel 由於我的Centos yum repository上沒有kernel-debuginfo包,於是得手工添加有這個包的repository。當然也可以把這個包及依賴包rpm檔案下載到本地安裝。
在/etc/yum.repos.d建立一個檔案,我把他命令為debuginfo
然後在debuginfo中加入
1
2
3
4
5
6 [debuginfo]
name=CentOS-$releasever - debuginfo
baseurl=http://debuginfo.centos.org/5/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5 然後在執行官方推薦的安裝命令的命令就可以了。整個過程要下載300多M檔案,因此需要很長的時間。
官方wiki:
http://sourceware.org/systemtap/wiki/SystemTapOnCentOS
systemtap如何跟蹤libc.so
下午和周忱同學折騰複雜程式的記憶體流失問題,用了valgrind, gogle perftools等工具都不大好用,很容易把應用程式搞死,於是打算用systemtap來在libc.so層面瞭解記憶體的使用方式。主要思路就是看 malloc/realloc和free的調用次數的平衡。
首先準備下環境,系統是標準的RHEL 5u4:
$ uname -r2.6.18-164.el5$ stap -VSystemTap translator/driver (version 1.3/0.137 non-git sources)Copyright (C) 2005-2010 Red Hat, Inc. and othersThis is free software; see the source for copying conditions.enabled features: LIBRPM LIBSQLITE3 NSS BOOST_SHARED_PTR TR1_UNORDERED_MAP$stap -L 'kernel.function("printk")'kernel.function("printk@kernel/printk.c:533") $fmt:char const* $args:va_list$ stap -L 'process("/lib64/libc.so.6").function("malloc")'Missing separate debuginfos, use: debuginfo-install glibc-2.5-42.x86_64
核心的符號是OK的,glibc沒有安裝符號。系統提示用 debuginfo-install glibc-2.5-42.x86_64 命令安裝符號資訊,但是RHEL 5不交錢不能用這個服務的,只能自己下載包安裝。
$ wget -c ftp.redhat.com/pub/redhat/linux/enterprise/5Server/en/os/x86_64/Debuginfo/glibc-debuginfo-2.5-42.x86_64.rpm$ sudo rpm -i glibc-debuginfo-2.5-42.x86_64.rpm$ stap -L 'process("/lib64/libc.so.6").function("malloc")'process("/lib64/libc-2.5.so").function("__libc_malloc@/usr/src/debug/glibc-2.5-20061008T1257/malloc/malloc.c:3560") $bytes:size_t
這次有了glibc的符號了,可以方便的跟蹤libc.so中malloc的使用方式。
接著我們來簡單的寫個c程式調用malloc, 同時寫個stap指令碼來跟蹤malloc的呼叫堆疊:
$ cat t.c#include <stdlib.h>void fun() { malloc(1000);}int main(int argc, char *argv[]) { fun(); return 0;}$cat m.stp probe process("/lib64/libc.so.6").function("malloc") {if (target()== pid()) {print_ubacktrace();exit();}}probe begin {println("~");}$ gcc -g t.c$ stap -L 'process("./a.out").function("*")'process("/home/chuba/a.out").function("fun@/home/chuba/t.c:3")process("/home/chuba/a.out").function("main@/home/chuba/t.c:7") $argc:int $argv:char**
現在程式準備好了,那麼我們來執行下看記憶體流失在那裡:
$sudo stap m.stp -c ./a.out ~ 0x33d5e74b96 : malloc+0x16/0x230 [libc-2.5.so] 0x4004a6 [a.out+0x4a6/0x1000]
我們看到在a.out的0x4004a6的地方地方調用了malloc, 但是具體在程式裡面是哪行呢? 用add2line就很容易找出來:
$ addr2line -e ./a.out 0x4004a6 /home/chuba/t.c:5$ nl t.c 1 #include <stdlib.h> 2 void fun() { 3 malloc(1000); 4 } 5 int main(int argc, char *argv[]) { 6 fun(); 7 return 0; 8 }
參考串連
erlang 非業餘研究
ibm Linux 自檢和 SystemTap
Linux 下的一個全新的效能測量和調式診斷工具 Systemtap 系列
systemtap manual
redhat systemtap introduce(pdf) 文庫連結
ftp://ftp.redhat.com/pub/redhat/linux/enterprise/5Server/en/os/i386/Debuginfo