Cent OS 5.4 final下Systemtap的安裝
2012-2-22 最新安裝步驟參考
http://blog.csdn.net/zklth/article/details/7281958
一、Systemtap運行環境需求
(1)linux kernel with kprobes (mainline 2.6.11+ or backport);
(2)kernel module build environment (kernel-devel or kernel-smp-devel rpm);
(3)kernel debugging information (kernel-debuginfo rpm);
(4)C compiler (same as what kernel was compiled with);
(5)elfutils with libdwfl for debugging informatin parsing;
(6)root privileges;
即:
(1)核心支援並配置了kprobe(核心2.6.11和以上)
(2)核心模組編譯環境(即編譯核心模組所需的核心標頭檔以及模組配置資訊,對於Fedora core或Redhat指kernel-devel或kernel-smp-devel RPM包)
(3)核心調試資訊(對於Fedora core或Redhat指kernel-debuginfo RPM包)
(4)C編譯環境
(5)支援libdwfl的elfutils(只有支援libwdfl的elfutils,systemtap才能正常工作,如果您的系統的elfutils較舊,您必須下載elfutils源碼包來編譯,systemtap能夠和elfutils一塊編譯)
(6)root許可權(為了運行Systemtap,您必須具有root許可權)
二、CentOS 5.4 final下安裝步驟
核心版本
[root@localhost opt]# uname -r
2.6.18-164.el5 <假若是2.6.18-164.el5xen是表示核心對虛擬化的支援>
作業系統版本
[root@localhost opt]# cat /etc/issue
CentOS release 5.4 (Final) Kernel /r on an /m
Cent OS 5.4發行光碟片中包含了下面所列出的大部分包,
可以通過 rpm -q 命令查看,
或者
在圖形介面的“應用程式”—>“添加/刪除軟體”—>“搜尋”—>輸入軟體包名來查詢軟體包的相關資訊。
注意:下面列出的kernel-debuginfo-common,kernel-debuginfo,kernel-xen-debuginfo這三個包一般在發行光碟片中都不內建,
需要到網上去下載,下面列出幾個:
(i) http://debuginfo.centos.org/5/i386/ 這裡可以下載 Cent OS 5 的kernrl-debuginfo的rpm包。(我的機器是 i386)
(ii) http://rpm.pbone.net/ 選擇系統版本號碼,可以下載到相應的rpm安裝包。
(注意) 如果是xen核心應該下載kernel-xen-debuginfo包;
此外kernel-debuginfo-common和kernel-debuginfo版本應該相同,後者依賴於前者,安裝時先安裝前者;
我在Cent OS 5.4 Final下使用SystemTap時最開始沒有安裝這兩個包,SystemTap仍能正常工作,監測系統調用,不知何故,建議安裝上,最後我下載安裝的是kernel-debuginfo-2.6.18-194.11.4.el5.i686.rpm和kernel-debuginfo-common-2.6.18-194.11.4.el5.i686;
(1)安裝gcc,libcap-devel
gcc是編譯器(c、c++),libcap-devel是libcap的開發檔案;
查看系統中是否安裝相應包,這些包在Cent OS 5.4 final發行光碟片都能找到;
[root@localhost opt]# rpm -q gcc
gcc-4.1.2-46.el5
[root@localhost opt]# rpm -q libcap
libcap-1.10-26
(2)安裝kernel-headers
kernel-headers是被glibc使用的linux核心的標頭檔;
查看系統中是否安裝相應包,該包在Cent OS 5.4 final發行光碟片中能找到;
[root@localhost opt]# rpm -q kernel-headers
kernel-headers-2.6.18-164.el5
(3)安裝kernel-devel
kernel-devel是用來構建與核心匹配的核心模組的開發軟體包;
如果是Xen核心,需要安裝kernel-xen-devel包,如果核心版本是smp的,則需安裝kernel-[smp-]devel包;
該包在Cent OS 5.4 final發行光碟片中能找到;
[root@localhost opt]# rpm -q kernel-devel
kernel-devel-2.6.18-164.el5
安裝了 kernel-devel 包時會在 /usr/src/kernels/`uname -r` 目錄下新增內容,這是核心目錄,systemtap在執行時會調用核心源碼目錄中的相應模組。若不安裝此包,則在執行指令碼時將會出現
“missing x86_64 kernel/module debuginfo under '/lib/modules/`uname -r`/build'” 錯誤,而'/lib/modules/`uname -r`/build'”是一個連結,指向核心源碼目錄/usr/src/kernels/`uname -r`。注意:核心和kerner-devel的版本應對應一致。
(4)安裝kernel-debuginfo-common
該包在Cent OS 5.4 final發行光碟片中沒有,需要自己下載安裝,在前面已經給出。
[root@localhost opt]# rpm -q kernel-debuginfo-common
package kernel-deguginfo-common is not installed
(5)安裝kernel-debuginfo
如果是Xen核心,需要下載並安裝kernel-xen-debuginfo;
SystemTap需要通過核心調試資訊來定位核心功能和變數的位置。
該包在Cent OS 5.4 final發行光碟片中沒有,需要自己下載安裝,在前面已經給出。
[root@localhost opt]# rpm -q kernel-xen-debuginfo
package kernel-xen-deguginfo is not installed
--------------------------------------------------------------------
安裝kernel-debuginfo步驟:
安裝kernel-debuginfo(xen核心是kernel-xen-debuginfo)時必須首先安裝kernel-debuginfo-common, 否則會提示依賴錯誤,因為kernel-debuginfo依賴於kernel-debuginfo-common,此外,兩者版本應該一致,我下載安裝的是:
kernel-debuginfo-2.6.18-194.11.4.el5.i686.rpm ( 核心是xen,則kernel-xen-debuginfo-2.6.18-194.11.4.el5.i686.rpm)
kernel-debuginfo-common-2.6.18-194.11.4.el5.i686
: http://debuginfo.centos.org/5/i386/
安裝 : rpm -ivh *.rpm
查詢 : rpm -q kernel-debuginfo
卸載 : rpm -e kernel-debuginfo
安裝步驟:
[root@localhost kernel-debuginfo]# ls
kernel-debuginfo-2.6.18-194.11.4.el5.i686.rpm
kernel-debuginfo-common-2.6.18-194.11.4.el5.i686.rpm
[root@localhost kernel-debuginfo]# rpm -ivh kernel-debuginfo-2.6.18-194.11.4.el5.i686.rpm
error: Failed dependencies:
kernel-debuginfo-common-i686 = 2.6.18-194.11.4.el5 is needed by kernel-debuginfo-2.6.18-194.11.4.el5.i686
[root@localhost kernel-debuginfo]# rpm -ivh kernel-debuginfo-common-2.6.18-194.11.4.el5.i686.rpm
Preparing... ########################################### [100%]
1:kernel-debuginfo-common########################################### [100%]
[root@localhost kernel-debuginfo]# rpm -q kernel-debuginfo-common
kernel-debuginfo-common-2.6.18-194.11.4.el5
[root@localhost kernel-debuginfo]# rpm -ivh kernel-debuginfo-2.6.18-194.11.4.el5.i686.rpm
Preparing... ########################################### [100%]
1:kernel-debuginfo ########################################### [100%]
[root@localhost kernel-debuginfo]# rpm -q kernel-debuginfo
kernel-debuginfo-2.6.18-194.11.4.el5
----------------------------------------------------------------------------
(6)安裝elfutils
(i)Centos5.4 final預設安裝了elfutils-0.137
#rpm –q elfutils
elfutils-0.137-3.e15
最好將Centos5.4發行光碟片裡面所有elfutils*的rpm包都裝上;
Centos5.4安裝盤中有此軟體包, 共有如下7個:
預設已安裝的有以下5個rpm包
elfutils-0.137-3.el5.i386.rpm
一組用來處理編譯的對象的工具集合
elfutils-libelf-0.137-3.el5.i386.rpm
讀取和寫入ELF檔案(Executable and Linking Format. 大意為可執行、可關聯的檔案格式)的庫
elfutils-libelf-devel-0.137-3.el5.i386.rpm
libelf的開發支援
elfutils-libelf-devel-static-0.137-3.el5.i386.rpm
static archive of libelf
elfutils-libs-0.137-3.el5.i386.rpm
用於處理編譯的對象的庫
還未安裝的有以下幾個
elfutils-devel-0.137-3.el5.i386.rpm
處理編譯對象的開發庫
elfutils-devel-static-0.137-3.el5.i386.rpm
處理編譯對象的靜態archive
(ii)下載elfutils源碼包進行安裝
SystemTap需要elfutils軟體包提供的庫函數來分析調試資訊。
目前的SystemTap要求安裝elfutils-0.123以上版本。
我們可以從SystemTap的網站下載RPM或者源碼來升級。
elfutils老版本的是:
ftp://sources.redhat.com/pub/systemtap/elfutils.old/
其他版本也可以在 https://fedorahosted.org/releases/e/l/elfutils/ 下載
或者 http://kojipkgs.fedoraproject.org/packages/elfutils/
elfutils官網在 https://fedorahosted.org/elfutils/
(7)安裝Systemtap
(i)Centos5.4預設安裝了systemtap-0.9.7-5.e15
#rpm -q systemtap
systemtap-0.9.7-5.e15
最好將Centos5.4發行光碟片裡的Systemtap*包都裝上;
Centos5.4預設安裝的有如下2個:
systemtap-0.9.7-5.e15.i386
測試系統
systemtap-runtime-0.9.7-5.e15.i386
系統運行時測試裝置
還未安裝的有:
在圖形介面的“應用程式”—>“添加/刪除軟體”—>“搜尋”—>輸入軟體包名systemtap能夠查詢軟體包的相關資訊。
(ii)下載systemtap源碼進行安裝
從SystemTap的FTP網站下載最新的源碼
ftp://sources.redhat.com/pub/SystemTap/snapshots/SystemTap-20100925.tar.bz2
或者
ftp://sources.redhat.com/pub/systemtap/releases/
或者也可以下載 systemtap 的 rpm 包進行安裝: 在 http://rpm.pbone.net/ 上搜尋systemtap.
systemtap官網在
http://sourceware.org/systemtap/documentation.html
安裝請參見
Linux下安裝使用SystemTap源碼安裝SystemTap
我在Cent OS 5.4下未安裝 kernel-debuginfo和kernel-debuginfo-common,使用系統預設安裝的SystemTap也能夠正常工作,但建議安裝上 debuginfo 包.
(8)運行簡單的Systemtap測試程式;
測試指令碼systemtap.stp監控java進程的系統調用:
--------------------------------------------------------------------
global syscalllist
global mpname="java" /* attention : java process'name is lower-case "java" */
global debug=0 /*print middle info*/
global all=0
probe begin {
printf("%s process monitoring started .../n",mpname)
}
probe syscall.read
{
if(all) printf("current process : %s /n",execname());
/* execname() can get the current process's name */
if (execname() ==mpname) {
/* variable name is syscall's name, it is defined in kernel function */
namex=name
if(debug) printf("%s/n",namex)
/* pid() can get the current process's PID */
pidx=pid()
if(debug) printf("%d/n",pidx)
syscalllist[pidx,namex]++
}
}
probe syscall.write
{
pname=execname()
syscallname=name
count(pname,syscallname)
}
probe kernel.function("sys_read"){
pname=execname()
objname="kf_sys_read"
count(pname,objname)
}
probe kernel.function("sys_write"){
pname=execname()
objname="kf_sys_write"
count(pname,objname)
}
//
//Remarks: Count process's some obj, such syscall,kernel.function
//
//Parameters:
//// pname : process'name
//// objname : syscall name
//
//Return values :
//// null
//
function count(pname,objname){
if (pname==mpname) {
if(debug) printf("%s ",objname)
/* current process' id */
pidx=pid()
if(debug) printf("%d/n",pidx)
syscalllist[pidx,objname]++
}
}
/*
probe timer.ms(5000) {
}
*/
function print_info(){
foreach ( [pidx,namex] in syscalllist ) {
printf("%d %s = %d/n", pidx,namex, syscalllist[pidx,namex] )
}
}
probe end {
print_info()
printf("java process monitoring finished/n")
}
---------------------------------------------------------------------
運行 stap -v systemtap.stp 命令,若運行正常則表示systemtap安裝成功
[root@localhost systemtap_study]# stap -v monitorJava.stap
Pass 1: parsed user script and 52 library script(s) in 280usr/0sys/293real ms.
Pass 2: analyzed script: 6 probe(s), 4 function(s), 1 embed(s), 4 global(s) in 310usr/350sys/654real ms.
Pass 3: using cached /root/.systemtap/cache/a3/stap_a3128d43c0908f80778e75f469858507_2665.c
Pass 4: using cached /root/.systemtap/cache/a3/stap_a3128d43c0908f80778e75f469858507_2665.ko
Pass 5: starting run.
java process monitoring started ...
4151 kf_sys_read = 14
4151 read = 14
4151 kf_sys_write = 5
4151 write = 5
4049 kf_sys_read = 43
4049 read = 43
4049 kf_sys_write = 13
4049 write = 13
5873 kf_sys_read = 864
5873 read = 864
5873 kf_sys_write = 2
5873 write = 2
5861 kf_sys_read = 2561
5861 read = 2561
5861 kf_sys_write = 29
5861 write = 29
5921 kf_sys_read = 864
5921 read = 864
5921 kf_sys_write = 2
5921 write = 2
5909 kf_sys_read = 2561
5909 read = 2561
5909 kf_sys_write = 29
5909 write = 29
java process monitoring finished
Pass 5: run completed in 0usr/10sys/14408real ms.
[root@localhost systemtap_study]#
三、Cent OS 5.4 Final特殊性說明以及在一般linux系統中systemtap安裝步驟
Cent OS 5.4 final在安裝作業系統時只要選擇了相關軟體包即會預設安裝上elfutils和systemtap,
並且不需要重新編譯核心,因為其核心預設支援相關的選項,這些選項再下面說明。
假若系統沒有安裝上面敘述的(1)~(5)的一些rpm包以及elfutils和systemtap,
則需要首先下載這些rpm包完成(1)~(5)的安裝,再重新編譯核心,用新核心啟動後再安裝elfutils和systemtap
編譯核心時必須保證核心支援以下選項:
make menuconfig;配置核心編譯選項,按如下方式選擇
General setup --->
[*] Kernel->user space relay support (formerly relayfs)
Kernel hacking --->
[*] Kernel debugging
[*] Compile the kernel with debug info
[*] Debug Filesystem
Instrumentation Support --->
[*] Kprobes (EXPERIMENTAL)
Security Options --->
[*] Default Linux Capabilities
退出時,vi .config 查看核心編譯設定檔,
確保CONFIG_DEBUG_INFO, CONFIG_KPROBES, CONFIG_RELAY,CONFIG_DEBUG_FS這四項選中,使用如下命令
[root@localhost linux-2.6.25]# grep CONFIG_DEBUG_INFO .config
CONFIG_DEBUG_INFO=y
[root@localhost linux-2.6.25]# grep CONFIG_KPROBES .config
CONFIG_KPROBES=y
[root@localhost linux-2.6.25]# grep CONFIG_RELAY .config
CONFIG_RELAY=y
[root@localhost linux-2.6.25]# grep CONFIG_DEBUG_FS .config
CONFIG_DEBUG_FS=y
編譯核心
make clean
make vmlinux modules
make modules_install
make bzImage
make install
用新核心重新啟動系統後,安裝elfutils*和Systemtap*包
四、參考資料
Linux 自檢和 SystemTap
http://www.ibm.com/developerworks/cn/linux/l-systemtap/index.html
Linux 下的一個全新的效能測量和調式診斷工具 Systemtap
http://www.ibm.com/developerworks/cn/linux/l-cn-systemtap3/#N1005B
SystemTap官網
http://sourceware.org/systemtap/documentation.html
elfutils官網
https://fedorahosted.org/elfutils/
使用 SystemTap 調試核心
http://linux.chinaunix.net/docs/2006-12-15/3479.shtml