我编写了一个脚本,该脚本将使用ftrace跟踪由特定命令执行的所有内核命令:
#!/bin/bash
# Mount the tracefs
mount -t tracefs nodev /sys/kernel/tracing
# Disable tracing
echo "0" > /sys/kernel/tracing/tracing_on
# set the current tracer to nop
echo "nop" > /sys/kernel/tracing/current_tracer
# set the current shell PID as a filter
echo $$ > /sys/kernel/tracing/set_ftrace_pid
# set the current tracer to function_graph
echo "function_graph" > /sys/kernel/tracing/current_tracer
# Enable tracing
echo "1" > /sys/kernel/tracing/tracing_on
# Execute the command
ls
# Disable tracing
echo "0" > /sys/kernel/tracing/tracing_on
# Cat the trace file into temporary file
cat /sys/kernel/tracing/trace > /tmp/mycommand_trace
使用 function_graph
跟踪器运行此脚本时,只有很少的条目,而使用 function
跟踪器运行的条目很多。
使用 function
跟踪器:
$ cat /tmp/mycommand_trace | head -n 20
# tracer: function
#
# _-----=> irqs-off
# / _----=> need-resched
# | / _---=> hardirq/softirq
# || / _--=> preempt-depth
# ||| / delay
# TASK-PID CPU# |||| TIMESTAMP FUNCTION
# | | | |||| | |
kernel_function-31784 [003] .... 5769.020705: mutex_unlock <-rb_simple_write
kernel_function-31784 [003] .... 5769.020706: __fsnotify_parent <-vfs_write
kernel_function-31784 [003] .... 5769.020706: fsnotify <-vfs_write
kernel_function-31784 [003] .... 5769.020707: __sb_end_write <-vfs_write
kernel_function-31784 [003] d... 5769.020730: do_syscall_64 <-entry_SYSCALL_64_after_hwframe
kernel_function-31784 [003] .... 5769.020730: __x64_sys_dup2 <-do_syscall_64
kernel_function-31784 [003] .... 5769.020730: ksys_dup3 <-__x64_sys_dup2
kernel_function-31784 [003] .... 5769.020730: _raw_spin_lock <-ksys_dup3
kernel_function-31784 [003] .... 5769.020731: expand_files <-ksys_dup3
kernel_function-31784 [003] .... 5769.020731: do_dup2 <-__x64_sys_dup2
kernel_function-31784 [003] .... 5769.020732: filp_close <-do_dup2
使用 function_graph
跟踪器:
$ cat /tmp/mycommand_trace
# tracer: function_graph
#
# CPU DURATION FUNCTION CALLS
# | | | | | | |
3) # 1485.764 us | } /* schedule */
3) # 1494.689 us | } /* do_wait */
3) # 1495.325 us | } /* kernel_wait4 */
3) # 1495.565 us | } /* __x64_sys_wait4 */
3) # 1495.848 us | } /* do_syscall_64 */
但是据我对 ftrace 的了解, function_graph
跟踪器应该跟踪内核函数调用的退出和进入,但是我看不到它在上面的输出中。
我的脚本有问题吗?