本文共 2916 字,大约阅读时间需要 9 分钟。
http://blog.yufeng.info/archives/1229
有时候在看系统代码的时候,我们很难从源码中看出我们感兴趣的函数是如何被调用的,因为调用路径有可能太多。用户空间的程序gdb设断点是个好的方法,内核的就麻烦了。这时候systemtap可以帮忙, 比如:
SystemTap translator/driver (version 1.5/0.137 non-git sources) |
Copyright (C) 2005-2011 Red Hat, Inc. and others |
This is free software; see the source for copying conditions. |
enabled features: LIBRPM LIBSQLITE3 NSS BOOST_SHARED_PTR TR1_UNORDERED_MAP NLS |
$ sudo stap -e 'probe vfs.add_to_page_cache {print_backtrace();println("")}' |
0xffffffff8000c633 : add_to_page_cache+0x0/0xc1 [kernel] |
0xffffffff800c4565 : add_to_page_cache_lru+0xe/0x22 [kernel] |
0xffffffff80025a10 : find_or_create_page+0x4b/0x72 [kernel] |
0xffffffff80019b72 : __getblk+0x105/0x236 [kernel] |
0xffff811838293600 (inexact) |
但是有时候我们并没能得到全部的符号信息, 比如# 0xffffffff8803826a 这行是谁没打印出来吧。 原因是我们的模块可能被未知的模块所调用,这些模块的符号信息没有自动加载,所以systemtap当然就不知道谁是谁了!
解决方法 man stap 文档写的很清楚:
-d MODULE
Add symbol/unwind information for the given module into the kernel object module. This may enable symbolic tracebacks from those mod- ules/programs, even if they do not have an explicit probe placed into them. –ldd Add symbol/unwind information for all shared libraries suspected by ldd to be necessary for user-space binaries being probe or listed
with the -d option. Caution: this can make the probe modules considerably larger. –all-modules
Equivalent to specifying “-dkernel” and a “-d” for each kernel module that is currently loaded. Caution: this can make the probe modules considerably larger.
$ sudo stap --all-modules -e 'probe vfs.add_to_page_cache {print_backtrace();println("")}' |
0xffffffff8000c633 : add_to_page_cache+0x0/0xc1 [kernel] |
0xffffffff800c4565 : add_to_page_cache_lru+0xe/0x22 [kernel] |
0xffffffff80025a10 : find_or_create_page+0x4b/0x72 [kernel] |
0xffffffff80019b72 : __getblk+0x105/0x236 [kernel] |
0xffffffff8803826a : journal_get_descriptor_buffer+0x30/0x0 [jbd] |
0xffff81183f50c000 (inexact) |
0xffffffff8803826a 这行我们知道是谁了把, jbd. 知道了这个模块我们就可以这样:
$ sudo stap -d jbd -e 'probe vfs.add_to_page_cache {print_backtrace();println("")}' |
0xffffffff8000c633 : add_to_page_cache+0x0/0xc1 [kernel] |
0xffffffff800c4565 : add_to_page_cache_lru+0xe/0x22 [kernel] |
0xffffffff80025a10 : find_or_create_page+0x4b/0x72 [kernel] |
0xffffffff80019b72 : __getblk+0x105/0x236 [kernel] |
0xffffffff8803826a : journal_get_descriptor_buffer+0x30/0x0 [jbd] |
0xffff81183f50c000 (inexact) |
二个一样的的运行效果,只是生成的模块大小不一样,我们来看下:
$ sudo stap -d jbd -m demo -p4 -e 'probe vfs.add_to_page_cache {print_backtrace();println("")}' |
-rw-r--r-- 1 root root 2943293 Mar 26 22:14 demo.ko |
$ sudo stap --all-modules -m demo -p4 -e 'probe vfs.add_to_page_cache {print_backtrace();println("")}' |
-rw-r--r-- 1 root root 3890409 Mar 26 22:15 demo.ko |
–all-modules的生成要大不少,不过很适合我们这样的懒人!
用户空间的程序用–ldd来搞定!
玩得开心!
Post Footer automatically generated by for wordpress.
转载地址:http://trobi.baihongyu.com/