在Ubuntu10.10上升级内核到2.6.36使用systemtap

     一直在 linux 平台上做开发工作已经几年了,但对 linux 内核都没有认真研究过,个人对 linux 的了解都停留在系统 api 的使用层面,对 linux 的底层设计了解甚少。在身边的同事,不凡有熟悉 linux 内核的高手,在他们的耳濡目染下,也激发了自己研究 linux 内核的激情,所以在自己的小黑上装上了 vmware Ubuntu10.10 下面我将分享一下个人在搭建环境和学习 linux 内核的一些初步准备工作。

 

        我学习 linux 内核分成两个方面,理论学习和实践。理论学习主要偏重理论书籍的阅读,短时间内,我囫囵吞枣的扫描了如下的介绍 linux 内核的书籍,《 linux 内核完全注释》,《 linux 内核修炼之道》,《 linux 内核设计与实现》第二版,《深入理解 linux 内核》第三版,《 独辟蹊径品内核 -- LINUX内核源代码导读 》,还有一本与内核关系不是太大,《 程序员的自我修养— 链接、装载与库》,这些书真的挺理论的,看完了,脑子居然没有留下多少东西,唯一留下的就是感觉 liunx 内核好深奥,真 NB 。这肯定不是我想要的学习效果,我需要的是在学习理论的同时,不断地实践,尽量多的理解自己感兴趣的一些技术设计细节。虽然看这些理论书籍比较枯燥,但通过读这些书籍,我宏观上了解了 linux 内核的架构,知道了内核的学习方法,避免在实践时走弯路。个人对文件系统和网络系统比较感兴趣,但经历有限,只会深入研究其中一个系统,兼顾整个内核系统其它模块的学习。我的案头摆着几本以后要重点学习的书籍,《深入理解 linux 内核》第三版, 《深入理解 linux 架构》,《内核网络栈源代码情景分析》,希望这些理论书籍能提升自己对 linux 内核的了解。

 

        下面谈谈我在内核学习中的一些初期实践工作,碰到不少问题,但还是耐心的一个个解决了。首先去 http://www.kernel.org/ 下载了最新的 linux 内核 2.6.36 让后就开始风风火火的准备看代码了,真的是初生牛犊不怕虎,啃了两天代码,也没有看明白一个小模块,看来盲目的学习不起作用,还是得补习点理论知识,于是借来几本理论书籍开始啃,在理论学习过程中,了解了 x86 的架构,汇编相关知识,内存管理,进程调度,文件系统等初步知识,似乎理解得更多一点了,前期学习的理论知识应该准备得差不多了,应该开始详细分析一些感兴趣的代码了。万事开头难,还得从搭建学习环境开始。

 

        一番准备后,在小黑上装上了 vmware ,并装上了 Ubuntu10.10 Ubuntu10.10 自带的内核是 2.6.35.22 ,我的学习目标是 2.6.36 所以首先是要升级内核。编译内核前对编译选项做了一些简单裁剪配置,主要打开了一些用于调试的开关。然后就是一番漫长的等待编译过程,最后 make install 。重启,选择 2.6.36 内核启动, oops ,启动不成功,哪里出问题了?重新进入原来的 2.6.35 内核启动系统,查看 /boot/ 目录,有 initrd.img-2.6.35-22-generic 这个 image ,但却没有 initrd.img-2.6. 36 这个 image ,难道 vmware 需要内核支持 ramdisk 才能启动?我查看了编译内核的配置,“

Initial RAM filesystem and RAM disk (initramfs/initrd) support ”这个选项也选上了,那为什么编译的时候没有生成 initrd.img-2.6. 36 这个 image 呢?经过一番研究,没找到原因,但发现用 make-kpkg 可以编译出这个 image ,于是安装 make-kpkg

sudo apt-get install make-kpkg 。

     然后进入 linux 源代码的文件夹运行:

make -kpkg --initrd binary-arch

 

经过一段时间后,在源代码的上层目录生成了三个 deb 的安装文件。

linux-headers-2.6.36_2.6.36-10.00.Custom_i386.deb
linux-image-2.6.36_2.6.36-10.00.Custom_i386.deb
linux-image-2.6.36-dbg_2.6.36-10.00.Custom_i386.deb

分别安装这个三个包:

sudo dpkg -i l inux-headers-2.6.36_2.6.36-10.00.Custom_i386.deb
sudo dpkg -i l inux-image-2.6.36_2.6.36-10.00.Custom_i386.deb
sudo dpkg -i linux-image-2.6.36-dbg_2.6.36-10.00.Custom_i386.deb 
 

        安装完成后重启系统,选择 2.6.36 内核进入系统,啊,成功启动,兴奋一把。尝试运行系统一些命令,似乎也正常。升级内核该告一个段落了吧,好戏还在后头。

 

       Systemtap 是学习内核一个必不可少的工具,它不仅可以侦测内核空间的相关信息,还可以侦测用户空间的信息,是研究内核源代码,调试系统性能的一个必备工具。在 Ubuntu 下如何安装 Systemtap 请参考:

http://sourceware.org/systemtap/wiki/SystemtapOnUbuntu

       

        运行 sudo apt-get install systemta p 安装 Systemtap 。由于在升级内核的时候已经安装了相应的 debug 信息,

至此内核追踪已经可以执行,但module的信息还需要多做些工作

sudo apt-get install elfutils

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

 

到这里应该能运行 stap 执行 hello Word 的程序了吧,先试运行一个简单的

sudo stap -ve 'probe begin { log("hello world") exit() }'

运行出错了, error:‘param_ops_int64_t’ undeclared here (not in a function)

这是什么情况,先不管,再试运行一个脚本

sudo stap -c df -e 'probe syscall.* { if (target()==pid()) log(name." ".argstr) }'

          任然遇到上面的错误,在网上搜了一番,原来这是 Systemtap 2.6.36 内核支持不好,有这个 bug Systemtap 官方网站说这个 bug 已经修复,使用最新的 Systemtap 就好了。于是下面的网站下载最新的 Systemtap 的源代码,编译安装。

http://sources.redhat.com/systemtap/ftp/snapshots/

 

然后运行 hello Word 脚本,

Pass 1: parsed user script and 75 library script(s) using 16300virt/12368res/1628shr kb, in 920usr/3670sys/4627real ms.

Pass 2: analyzed script: 1 probe(s), 2 function(s), 0 embed(s), 0 global(s) using 16564virt/12892res/1688shr kb, in 170usr/260sys/526real ms.

Pass 3: translated to C into "/tmp/stapa7GBqo/stap_5d4a1f5ec0c40981d6d84405569c6127_669.c" using 16564virt/13116res/1896shr kb, in 150usr/130sys/277real ms.

Pass 4: compiled C into "stap_5d4a1f5ec0c40981d6d84405569c6127_669.ko" in 8880usr/18060sys/27864real ms.

Pass 5: starting run.

hello world

Pass 5: run completed in 60usr/2650sys/2871real ms.

 

试运行一下能否侦测系统函数的调用堆栈,这个功能在看源代码的时候用得着,脚本如下:

#!/usr/bin/stap

probe kernel.function("do_timer").return {
    print_backtrace();
    printf("\n");
    exit();
}
 

运行输出如下:

Returning from:  0xc0155e60 : do_timer+0x0/0x30 [kernel]
Returning to  :  0xc017574d : tick_do_update_jiffies64+0xed/0x170 [kernel]
0xc017597c : tick_sched_timer+0xbc/0xd0 [kernel]
0xc0169a0a : __run_hrtimer+0x7a/0x1c0 [kernel]
0xc0169df0 : hrtimer_interrupt+0x130/0x2a0 [kernel]
0xc05d08e6 : smp_apic_timer_interrupt+0x56/0x8a [kernel]
0xc05ca5d9 : apic_timer_interrupt+0x31/0x38 [kernel]
0xc012ab4a : native_safe_halt+0xa/0x10 [kernel] (inexact)
0xc0109e93 : default_idle+0x53/0xb0 [kernel] (inexact)
0xc0101e4a : cpu_idle+0x8a/0xf0 [kernel] (inexact)
0xc05c4242 : start_secondary+0x1ec/0x1f2 [kernel] (inexact)
 

           不错, Systemtap 对内核的侦测功能应该运行正常了,下面我们来尝试一下 Systemtap 对用户空间的侦测功能,在开发过程中,我们也不时用 Systemtap 来侦测函数的调用堆栈,调用频率,性能如何等等。先写个简单的程序 test.c 试试看。

#include <stdlib.h>
#include <stdio.h>

void func()
{
    int i = 0;
    int count = 0;

    for (i = 0; i < 1000000; ++i)
    {
        ++count;
    }
}


int main(int argc, char** argv)
{
    func();
    return 0;
}
 

          编译 gcc -g  test.c -o test ,用户程序要使用 Systemtap 需要在编译时加 -g 选项。写个简单的脚本 calltime.stp 来测试“ func ”的执行时间。

#!/usr/bin/stap

global tm
global call_time

probe begin {
    printf(":\)\n");
}

probe process("./test").function("func") {
    tm = gettimeofday_ns();
}

probe process("./test").function("func").return {
    call_time <<< (gettimeofday_ns() - tm);
    printf("func call_time: %d\n", @avg(call_time));
}

probe end {
    if (@count(call_time) > 0) {
        printf("func call_time: %d\n", @avg(call_time));
    }
}
 

          运行 sudo ./calltime.stp 运行出错了, SystemTap不支持process(" ./test ")的用户 空间 的调试的方法 ,在网上搜罗一番,原来是 Systemtap 对用户空间的调试需要 utrace 支持, utrace 只是在 redhat 系统做了支持, Ubuntu 系统中默认不支持,内核也是默认不支持 utrace 。问题找到了,先给内核打上 utrace 的补丁,到如下网站下载 utrace 2.6.36 内核的补丁。

http://people.redhat.com/roland/utrace/

补丁的顺序如下:

  >cd /src/linux2 .6.36
  >patch – p1 <tracehook.patch
  >patch – p1 <utrace.patch
  >patch – p1 <utrace-ptrace.patch 
 

 

           打好补丁之后,重新编译内核,注意需要选中 utrace 选项 General setup --> Infrastructure for tracing and debugging user processes 然后就是按照上面的步骤重新升级内核,内核升级完成后,先运行

sudo ./calltime.stp
 

等待出现“ :) ”,然后运行 ./test

结果是这样的

:)
func call_time: 10291909
 

至此,终于配置好了,可以在系统中作更多的测试学习了。

猜你喜欢

转载自mqzhuang.iteye.com/blog/840223