用Ftrace跟踪内核模块
来源:互联网 发布:js base64转换成file 编辑:程序博客网 时间:2024/06/05 22:52
创建时间:2010-05-23
作者:杨民强 Steven Yang
E-mail:mqyoung@gmail.com
转载请注明出处:http://blog.csdn.net/defeattroy/archive/2010/05/23/5618099.aspx
如果你需要分析一个内核模块的函数调用数或者希望得到各语句段的执行时间,那么这篇文章很适合您。
Ftrace抛开内核代码跟踪领域的对手如KFT等进入Linux官方内核,正说明其功能强大性能稳定。Ftrace 的作用是帮助开发人员了解 Linux 内核的运行时行为,以便进行故障调试或性能分析。网上有很多关于ftrace的介绍及使用的文章,在此就不赘述,本文主要就使用ftrace跟踪分析内核模块,说明其详细操作过程和一些技巧。
对一个名为epl.ko的内核模块进行trace的过程如下:
1 首先确保你打开相应的内核选项: kernel_hacking/Trace下面。如果需要重编内核。
注意重编内核后重编你的模块。
2 设置ftrace
mount -t debugfs debugfs /sys/kernel/debug #要使用 ftrace首先需要mount这个debugfs
ln -s /sys/kernel/debug /debug #方便期间做个链接
cd /debug
echo function_graph > current_tracer #输出函数调用图,需要打开内核选项FUNCTION_GRAPH_TRACER
3 提取模块里的函数列表
如果没有设置过滤表(默认加载debugfs后),available_filter_functions文件包含了所有可追踪的函数名称,默认情况下这个链表覆盖了所有内核的可trace函数。如果我们插入一个模块,那么ftrace会自动把我们我们这个模块的函数符号加到available_filter_functions中。提取一个内核模块的函数可以有两种方法,一种是用nm命令,一种是比较插入模块前后available_filter_functions的差异,两种方法都提供给读者如下:
1)比较插入模块前后available_filter_functions的差异
cat available_filter_functions > /tmp/funcs_without_epl
insmod epl.ko
cat available_filter_functions > /tmp/funcs_with_epl
diff /tmp/funcs_without_epl /tmp/funcs_with_epl > epl_funcs
删除 epl_funcs文件里由diff产生的无用信息。
2) 使用nm命令导出epl.ko里的符号
参考 [附表:nm基本用法]
nm导出的是ko里的所有符号,我们只需要函数符号
nm -s epl.ko | grep " T " | awk '{ print $3 }' > nm_funcs
nm -s epl.ko | grep " t " | awk '{ print $3 }' >> nm_funcs
为了比较两者产生的差别,首先排下序
sort nm_funcs > nm_funcs_sorted
sort epl_funcs > epl_funcs_sorted
diff -Nur nm_funcs_sorted epl_funcs_sorted
我们可能发现nm_funcs包含了一些epl_funcs没有的函数,这些函数正式kernel里面本来有的内核函数,所以如果你只想trace你的模块的函数,那么推荐使用方法1。
4 开始trace
insmod epl.ko
cat /tmp/epl_funcs > set_ftrace_filter
echo 1 >tracing_enabled #开始trace
cat trace_pipe > /tmp/ftrace_result.txt & #设置一个管道重定向,防止溢出
tracing & waiting....
echo 0 >tracing_enabled #结束trace
说明:一个ko插入kernel以后才可以给 set_ftrace_filter设置函数过滤表,因为没插入以前内核没有epl.ko的函数符号,故设置set_ftrace_filter失败,所以需要先插入模块以后再enable trace,但是这样又有一个问题:我们也想trace模块插入时候的执行情况时,我们还不能在插入模块前设置过滤表,那么怎么办呢?需要得到这个答案需要首先确认:在默认情况下,也就是设置ftrace为trace所有可trace的函数后,我们插入模块时候ftrace是不是马上把这个ko的函数表加进可trace列表,同时也能够trace这些函数?如果可以,那我们可以先预先准备好filter list(即我们文中所述的epl_funcs),插入模块后然后再设置set_ftrace_filter过滤表,这样会在ftrace的结果的前一段会有我们不希望trace的函数(内核函数),或者删除,或者提取出我们模块初始化部分即可。作者在实践过程中发现有时候ftrace的结果输出没有显示函数符号表,这个问题还有待进一步确认...希望对这方便有深入了解的网友不吝赐教,mqyoung at gmail.com。
如下是我在学习使用过程中做的一些笔记和备忘,也许对您有用
需要了解的几个文件:
===============
set_ftrace_filter:
limit the trace to only those functions.
set_ftrace_notrace:
An effect opposite to that of set_ftrace_filter. Any function that is
added here will not be traced. If a function exists in both files,
the function will _not_ be traced.
set_ftrace_pid:
Only trace a single thread.
available_filter_functions:
This lists the funtions that ftrace has processed and can trace. These
are the function names that you can pass to "set_ftrace_filter" or
"set_ftrace_notrace".
Ftrace 提供的几种Tracer
=================
"function"
"function_graph"
"sched_switch"
context switches and wakeups between tasks
"irqsoff"
disable intrrupts and saves the trace with the longest max latency.
See tracing_max_latency.
"preemptoff"
"preemptirqsoff"
"wakeup"
Records the max latency that it takes for the highest priority task to
get scheduled after it has been woken up.
"hw-branch-tracer"
"nop"
"trace nothing" tracer. To remove all tracers from tracing simply echo
"nop" into current_tracer.
参考资料:
[1] nm http://hi.baidu.com/zzgmtv/blog/item/d021d7437d38d91f72f05d57.html
[2] ftrace kernel_src/Documentation/trace/ftrace.txt
附表:nm基本用法
对于每一个符号,nm列出其值(the symbol value),类型(the symbol type)和其名字(the symbol name)。
- 用Ftrace跟踪内核模块
- 用Ftrace跟踪内核模块
- How to use ftrace to trace your kernel module(使用Ftrace跟踪你的内核模块)
- ftrace 跟踪函数调用
- 利用ftrace跟踪内核static tracepoint——实例writeback event
- 利用ftrace跟踪内核static tracepoint——实例writeback event
- linux跟踪利器之Ftrace
- Linux内核调试Ftrace
- 利用ftrace调试内核
- 配置内核支持Ftrace
- 用 lttng 跟踪内核
- 使用 ftrace 调试 Linux 内核
- 使用 ftrace 调试 Linux 内核
- 使用 ftrace 调试 Linux 内核
- 使用 ftrace 调试 Linux 内核
- 使用 ftrace 调试 Linux 内核
- 使用 ftrace 调试 Linux 内核
- 内核性能调试–ftrace
- 第一次
- [OE]iis php环境配置完整版
- SQL 连接查询(内链接----外连接)
- Symbian Thread and Process 进程和线程
- linux-crontab 命令
- 用Ftrace跟踪内核模块
- 一个简单问题的遗传算法的实现
- C#中父窗口和子窗口之间实现控件互操作
- 用getch实现控制
- HttpServletRequest接口的getSession方法
- Ubuntu开启root用户及其它登录问题的解决
- 【WM】Phone Canvas重复创建视图的原因
- Ubuntu 10.04 安装配置手记
- [OE]IIS配置全能空间