ftrace(一)

Ftrace相对于其他的工具,比如Dtrace/systemTap来说,是一种轻量级的trace工具,利用静态代码插装技术来实现,实现更加可靠,以一种友好 的可视化方式来进行输出,支持ASCII。相对于其他的工具,它更加受内核开发者推崇,目前已经合入的内核主线。它的实现也依赖很多其他内核特性,比如 tracepoint,debugfs,kprobe等。

(1)debugfs

debugfs编译:

CONFIG_DEBUG_FS=y

Ftrace使用debugfs来操作tracer和更新trace信息,所以内核必须配置使能debugfs,上面kernel 配置项会把debugfs的支持编译进内核中,那么光编译还不行,还要在系统起来之后挂载debugfs。

debugfs挂载,配置etc/fstable文件:

	debugfs       /sys/kernel/debug          debugfs defaults        0       0

或者使用命令:

	mount -t debugfs nodev /sys/kernel/debug

(2)ftrace

make menuconfig后选择如下:

	  Kernel hacking ---> Tracers

当以上选项被选上之后可以进一步选择需要编译到内核的Tracer:

--- Tracers                                          
[ ]   Kernel Function Tracer                         
[ ]   Interrupts-off Latency Tracer                  
[ ]   Preemption-off Latency Tracer                  
[ ]   Scheduling Latency Tracer                      
[ ]   Trace process context switches and events (NEW)
[ ]   Trace syscalls                                 
[ ]   Create a snapshot trace buffer                 
      Branch Profiling (No branch profiling)  --->   
[ ]   Trace max stack                                
[ ]   Support for tracing block IO actions           
[ ]   Enable uprobes-based dynamic events            
[ ]   Memory mapped IO tracing                       
[ ]   Add tracepoint that benchmarks tracepoints  

当我们配置了上面的Tracer后,重新编译内核并运行,会在 debugfs 下创建一个 tracing 目录:/sys/kernel/debug/tracing。

下面是我的内核编译后生成的tracing目录:

/sys/kernel/debug/tracing # ls
README                      saved_cmdlines_size
available_events            set_event
available_filter_functions  set_ftrace_filter
available_tracers           set_ftrace_notrace
buffer_size_kb              set_ftrace_pid
buffer_total_size_kb        set_graph_function
current_tracer              set_graph_notrace
dyn_ftrace_total_info       snapshot
enabled_functions           trace
events                      trace_clock
free_buffer                 trace_marker
instances                   trace_options
max_graph_depth             trace_pipe
options                     tracing_cpumask
per_cpu                     tracing_max_latency
printk_formats              tracing_on
saved_cmdlines              tracing_thresh

可以通过cat README获取mini HOWTO使用说明。下面也简单介绍一下关键的节点介绍:

  • current_tracer
    用于设置和显示当前的tracer,系统默认为nop

  • available_tracers
    用于显示当前编译到内核中的tracer,可以配置到current_tracer来使用

  • tracing_on
    用于使能和关闭tracing的开关:
    echo 0 > tracing_on : 关闭tracing
    echo 1 > tracing_on : 使能tracing

  • trace
    trace信息输出接口,从trace ring buffer中获取

  • trace_pipe
    也是trace信息输出接口,是一种流式输出格式,和上面的区别是读取的信息会被丢掉,不会每次读取重复输出。

  • trace_options
    这个文件节点是用来设置trace特性的,比如输出格式或者trace方式,其中的选项是以使能和禁止方式可选的。
    如果想要禁止某个属性,只需要在属性名前加"no"前缀,并且echo到trace_options中即可设置成功。
    下面列了几个比较常用的options的说明:

 markers - When set, the trace_marker is writable (only by root).
       When disabled, the trace_marker will error with EINVAL
       on write.
 function-trace - The latency tracers will enable function tracing
       if this option is enabled (default it is). When
       it is disabled, the latency tracers do not trace
       functions. This keeps the overhead of the tracer down
       when performing latency tests.
Note: Some tracers have their own options. They only appear
      when the tracer is active.
   ```
- **options**
和上面的功能一样,但是是一种目录的形式来体现的,包含的属性都会在此目录中生成一个文件节点,使能和禁止的方式,是直接echo 1或者echo 0到对应属性文件中即可,设置效果和上面的一样。

- **buffer_size_kb**
查看和设置per cpu buffer大小

- **buffer_total_size_kb**
 查看所有的 all cpu buffers大小

- **tracing_cpumask**
设置指定的trace CPU,是以HEX输出的一种格式,一个bit代表一个CPU。

- **set_ftrace_filter**
写函数名到此文件,将只会trace对应function的功能。

- **set_ftrace_notrace**
和上面的相反,写函数名进去会屏蔽特定函数的trace功能。

- **available_filter_functions**
显示可以设置给set_ftrace_filter或者set_ftrace_notrace的函数名


- **trace_marker**
 这是一个非常常用的一个节点功能,目的就是可以写一个标记到trace buff中,这样可以通过查找对应的标记来确定发生的事件开始和结束时间。

- **set_ftrace_pid**	
  写入pids来只追踪特定pids的trace信息。


- **tracing_max_latency**
  最大延迟(us),一些tracers会记录最大延迟信息,当然这个信息会在"trace"文件的输出中也会体现出来。
  需要注意的是,这个文件只会记录最大值,所以每次我们在开始一次trace时都要echo 0进去清空之间的记录。   

- **tracing_thresh**
 一些延迟的tracers类型会记录延迟时间信息 ,这个可设置时间阈值,时间大于此值才会被记录(us)

- **trace_clock**
用于event记录的clock类型,一个event的记录都会带有timestamp,那么此时间是取自那种clock就是在该文件中配置的。

             local:   Per cpu clock but may not be synced across CPUs
             global:   Synced across CPUs but slows tracing down.
             counter:   Not a clock, but just an increment
             uptime:   Jiffy counter from time of boot
             perf:   Same clock that perf events use
- **triggers**
当function执行时会触发一个命令。

Format: :[:count](count代表function命中多少次才触发该命令)

trigger: traceon, traceoff
enable_event::
disable_event::
stacktrace
snapshot
dump
cpudump


- **set_graph_function**
 Trace the nested calls of a function (function_graph)

- **set_graph_notrace**	
  Do not trace the nested calls of a function (function_graph)

- **events**
是一个目录,它包含了event trace所要配置的相关信息,包括各个子系统,以及enable/trigger等等的配置选项。
以上就是tracing相关的各个目录的介绍,可能不够全面,因为随着内核的不断更新,会不停的有新的trace特性加入进来。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ftrace和ptrace是两个不同的工具,它们在功能和用途上有所区别。 引用中提到的ftrace是 Linux 内核的一个内建跟踪工具,用于跟踪和分析内核函数调用、上下文切换、延迟和性能问题等。它可以通过配置内核和 debugfs 来使用,并包含多个跟踪器,可以方便地跟踪不同类型的信息。 而引用中提到的ptrace是一个系统调用,用于在用户空间中跟踪和控制进程的执行。通过ptrace,用户可以监视和修改目标进程的内存、寄存器和执行状态,实现调试和跟踪进程的功能。 因此,ftrace主要用于内核级别的跟踪和性能分析,而ptrace主要用于用户空间进程的调试和跟踪。它们各自具有不同的功能和应用场景,但都能提供有助于问题排查和性能优化的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Linux内核调试方法总结之strace ,ltrace, ptrace, ftrace, sysrq](https://blog.csdn.net/zmjames2000/article/details/88410484)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Linux内核学习(十):内核追踪必备技能--ftrace](https://blog.csdn.net/weixin_45264425/article/details/125955998)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值