https://elixir.bootlin.com/linux/latest/source/Documentation/trace/ftrace.rst
【转】linux ftrace原理
From: http://www.360doc.com/content/16/0126/13/10366845_530659813.shtml
Tested on Linux kernel 4.8.17
ftrace 系列文章的一个索引
最后,这里是我找到的一些 ftrace 方面的文章。它们大部分在 LWN (Linux 新闻周刊)上,它是 Linux 的一个极好的资源(你可以购买一个 订阅!)
使用 Ftrace 调试内核 - part 1 (Dec 2009, Steven Rostedt)
使用 Ftrace 调试内核 - part 2 (Dec 2009, Steven Rostedt)
Linux 函数跟踪器的秘密 (Jan 2010, Steven Rostedt)
trace-cmd:Ftrace 的一个前端 (Oct 2010, Steven Rostedt)
使用 KernelShark 去分析实时调试器 (2011, Steven Rostedt)
Ftrace: 神秘的开关 (2014, Brendan Gregg)
内核文档:(它十分有用) Documentation/ftrace.txt
你能跟踪的事件的文档 Documentation/events.txt
linux 内核开发上的一些 ftrace 设计文档 (不是有用,而是有趣!) Documentation/ftrace-design.txt
作者:
链接:https://www.imooc.com/article/29575?block_id=tuijian_wz
来源:慕课网
指定函数深度
From: http://terenceli.github.io/技术/2017/12/05/tracing1
echo 2 > max_graph_depth
function graph tracer
Flags::
'$' - greater than 1 second
'@' - greater than 100 milisecond
'*' - greater than 10 milisecond
'#' - greater than 1000 microsecond
'!' - greater than 100 microsecond
'+' - greater than 10 microsecond
' ' - less than or equal to 10 microsecond.
xy@ubuntu:/boot$ ag 'CONFIG_DYNAMIC_FTRACE' -i /boot/config-4.8.17
8411:CONFIG_DYNAMIC_FTRACE=y
8412:CONFIG_DYNAMIC_FTRACE_WITH_REGS=y
- script
# 2. function graph tracer (valified)
# use root with sudo -s
echo function_graph > /sys/kernel/debug/tracing/current_tracer
cat -n /sys/kernel/debug/tracing/current_tracer
echo
echo do_mount copy_mnt_ns > /sys/kernel/debug/tracing/set_graph_function
cat -n /sys/kernel/debug/tracing/set_graph_function
# run test process and then
cat -n /sys/kernel/debug/tracing/trace
Schedule switch tracer 的输出
From: https://www.cnblogs.com/jefree/p/4438975.html
2、 一旦将函数追踪器启动,ftrace会记录所有函数的运行情况,但是我们只想查看schedule函数的调用情况,因此先设置函数过滤器,仅记录schedule:
root@thinker:/sys/kernel/debug/tracing#echo schedule > set_ftrace_filter
Schedule switch tracer 记录系统中的进程切换信息。在其输出文件 trace 中 , 输出行的格式有两种:
第一种表示进程切换信息:
Context switches:
Previous task Next Task
<pid>:<prio>:<state> ==> <pid>:<prio>:<state>
第二种表示进程 wakeup 的信息:
Wake ups:
Current task Task waking up
<pid>:<prio>:<state> + <pid>:<prio>:<state>
这里举一个实例:
# tracer: sched_switch
#
# TASK_PID CPU# TIMESTAMP FUNCTION
# | | | |
fon-6263 [000] 4154504638.932214: 6263:120:R + 2717:120:S
fon-6263 [000] 4154504638.932214: 6263:120:? ==> 2717:120:R
bash-2717 [000] 4154504638.932214: 2717:120:S + 2714:120:S
第一行表示进程 fon 进程 wakeup 了 bash 进程。其中 fon 进程的 pid 为 6263,优先级为 120,进程状态为 Ready 。她将进程 ID 为 2717 的 bash 进程唤醒。
第二行表示进程切换发生,从 fon 切换到 bash 。
Stack trace
Since the kernel has a fixed sized stack, it is important not to
waste it in functions. A kernel developer must be conscience of
what they allocate on the stack. If they add too much, the system
can be in danger of a stack overflow, and corruption will occur,
usually leading to a system panic.
There are some tools that check this, usually with interrupts
periodically checking usage. But if you can perform a check
at every function call that will become very useful. As ftrace provides
a function tracer, it makes it convenient to check the stack size
at every function call. This is enabled via the stack tracer.
CONFIG_STACK_TRACER enables the ftrace stack tracing functionality.