CentOS 5.4 final下Systemtap的安装

CentOS 5.4 final下Systemtap的安装 
时间:2015-02-11来源:linux网站 作者:zklth 

 一、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   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 

    (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 
        然后安装如下: 
         tar -jxvf SystemTap-20100925.tar.bz2 
         cd src 
         ./configure 
         make 
         make install 

       我在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 ... ",mpname) 
 } 

 probe syscall.read 
 { 
   if(all) printf("current process : %s  ",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 ",namex) 
     /* pid() can get the current process's PID */ 
     pidx=pid() 
     if(debug) printf("%d ",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 ",pidx) 
     syscalllist[pidx,objname]++ 
   }   } 

 /* 
 probe timer.ms(5000) { 

 } 
 */ 

 function print_info(){ 
   foreach ( [pidx,namex] in syscalllist ) { 
     printf("%d %s = %d ", pidx,namex, syscalllist[pidx,namex] ) 
   }   } 

 probe end {     print_info() 
   printf("java process monitoring finished ") 
 } 

 运行 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*包

原文地址:https://www.cnblogs.com/zengkefu/p/6351703.html