手机版
你好,游客 登录 注册
背景:
阅读新闻

在Ubuntu上安装使用Systemtap

[日期:2016-02-28] 来源:Linux社区  作者:wtb2012 [字体: ]

因为最近开始在Ubuntu上学习Nginx,在网上看到别人介绍了一款强大的内核探测工具Systemtap,于是便准备学习下这款探测工具为以后代码分析做准备。

第一步便是安装。在自己电脑上安装的时候,也是费了一番劲儿。因此,为防止以后需要重新安装,也希望可以帮助后来学习的人,在这里准备写一篇博客做一个记录。

  以下便是安装步骤:

  (0)、安装elfutils,提供分析调试信息的库函数,及libcap-dev。

  借助于Ubuntu方便强大的包管理器,可以很方便进行安装,如下:

sudo apt-get install elfutils

sudo apt-get install  libcap-dev

  (1)、安装systemtap。

  借助于Ubuntu方便强大的包管理器,可以很方便进行安装,如下:    

sudo apt-get install systemtap

  后续如果需要卸载,可执行如下命令:

sudo apt-get remove systemtap

  也可以通过源码进行安装,下载地址:https://sourceware.org/systemtap/ftp/releases/ 。解压然后进入根目录,执行如下命令:

./configure
make
sudo make instal

  如果后续需要卸载,可进入根目录,执行如下命令:

sudo make uninstall

  (2)、安装debug symbols。

   1)、配置ddeb repository。

sudo cat > /etc/apt/sources.list.d/ddebs.list << EOF
deb http://ddebs.ubuntu.com/ precise main restricted universe multiverse
EOF

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ECDCAD72428D7C01
sudo apt-get update

  上面添加仓库地址也可以直接在ddebs.list文件后面添加相应地址。

    2)、添加完repository之后,便是下载和你当前内核版本相对应的debug symbols。在这里推荐一位外国朋友写的脚本,写的非常好,博文链接(http://www.domaigne.com/blog/random/getting-debug-kernel-on-ubuntu/),感兴趣的可以去学习学习。因此为了减少错误发生,这里采用这里的脚本进行下载和安装:

wget http://www.domaigne.com/download/tools/get-dbgsym

chmod +x get-dbgsym

sudo ./get-dbgsy

  执行脚本后,可以去做些其他事情,因为这里可能需要等待较长的时间。

  3)、生成systemtap/libelf所需的模块信息。将如下命令放入debug_ko.sh:

for file in `find /usr/lib/debug -name '*.ko' -print`
do
        buildid=`eu-readelf -n $file| grep Build.ID: | awk '{print $3}'`
        dir=`echo $buildid | cut -c1-2`
        fn=`echo $buildid | cut -c3-`
        mkdir -p /usr/lib/debug/.build-id/$dir
        ln -s $file /usr/lib/debug/.build-id/$dir/$fn
        ln -s $file /usr/lib/debug/.build-id/$dir/${fn}.debug
done

    然后执行该文件:

sudo ./debug_ko.sh

  (4)、测试安装是否成功。执行如下命令:

stap -e 'probe kernel.function("sys_open") {log("hello world") exit()}'

  如果在终端打印出“hello world”说明安装成功。如果没有,继续往下看。

  (5)、如果按照上述步骤安装完之后,仍然不能使用,那么请参考下面的场景分别进行补充。

  1)、如果执行(4)中的命令后,终端打印如下信息:

stap: Symbol `SSL_ImplementedCiphers' has different size in shared object, consider re-linking
In file included from include/linux/mutex.h:15:0,
                from /tmp/staphH2yQD/stap_6e022ad97cbe9c6f46b582f7a0eac81d_1242_src.c:25:
include/linux/spinlock_types.h:55:14: error: ‘__ARCH_SPIN_LOCK_UNLOCKED’ undeclared here (not in a function)
  .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
              ^
include/linux/spinlock_types.h:79:15: note: in expansion of macro ‘__RAW_SPIN_LOCK_INITIALIZER’
  { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
              ^
include/linux/spinlock_types.h:82:16: note: in expansion of macro ‘__SPIN_LOCK_INITIALIZER’
  (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
                ^
include/linux/mutex.h:111:18: note: in expansion of macro ‘__SPIN_LOCK_UNLOCKED’
  , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
                  ^
include/linux/mutex.h:117:27: note: in expansion of macro ‘__MUTEX_INITIALIZER’
  struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
                          ^
/tmp/staphH2yQD/stap_6e022ad97cbe9c6f46b582f7a0eac81d_1242_src.c:26:8: note: in expansion of macro ‘DEFINE_MUTEX’
 static DEFINE_MUTEX(module_refresh_mutex);
        ^
scripts/Makefile.build:258: recipe for target '/tmp/staphH2yQD/stap_6e022ad97cbe9c6f46b582f7a0eac81d_1242_src.o' failed
make[1]: *** [/tmp/staphH2yQD/stap_6e022ad97cbe9c6f46b582f7a0eac81d_1242_src.o] Error 1
Makefile:1398: recipe for target '_module_/tmp/staphH2yQD' failed
make: *** [_module_/tmp/staphH2yQD] Error 2
WARNING: kbuild exited with status: 2
Pass 4: compilation failed.  [man error::pass4]

说明有些共享库需要重新readlink,执行如下命令:

readlink /lib/modules/`uname -r`/build/

更多Ubuntu相关信息见Ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-02/128727.htm

linux
相关资讯       SystemTap 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意���容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款