trace系列0 - 概述【转】

转自:https://blog.csdn.net/jasonactions/article/details/122250033?spm=1001.2014.3001.5502

1.前言
本文主要是根据阅码场 《Linux内核tracers的实现原理与应用》视频课程在aarch64上的实践。这是整个系列文章的第一篇,本篇主要解决如下几个问题:

ftrace是什么?
ftrace框架包含哪些tracer?
这些tracer的基本使用方法有哪些?
kernel版本:5.10
平台:arm64

2. ftrace是什么?
ftrace是一个内核跟踪工具,旨在帮助系统开发人员和设计人员了解内核内部的情况。从2008年由Steven Rostedt 开发并在合并到2.6.27主线内核, 它主要源于两个工具:来自Ingo Molnar的延迟追踪器和来自Steven的logdev工具。

ftrace 是 Function Trace 的简写,它可以让你观察和记录内核函数的执行流,但它能做的远不止这些:
记录函数的执行流程;
测量函数执行时间,并发现瓶颈和性能问题;
测量实时进程花费的时间并发现延迟问题;
测试内核栈使用情况并发现可能的栈溢出;
检查禁用和启用中断之间发生了什么
抢占和从唤醒任务到实际调度任务的时间

3. ftrace如何实现?

 

 

Ftrace uses the tracefs file system to hold the control files as well as the files to display output.

Ftrace 有两大组成部分,一是 framework,另外就是一系列的 tracer 。
ftrace框架是整个ftrace功能的纽带,包括对内核的修改,Tracer的注册,RingBuffer的控制等等。
每个 tracer 完成不同的功能,它们统一由 framework 管理。 ftrace 的 trace 信息保存在 ring buffer 中,由 framework 负责管理。 Framework 利用 debugfs 系统在 debugfs 下建立 tracing 目录,并提供了一系列的控制文件。
Tracer有很多种,主要几大类:

函数类:function, function_graph, stack
延时类:irqsoff, preemptoff, preemptirqsoff, wakeup, wakeup_rt, waktup_dl
其他类:nop, mmiotrace, blk
注: trace event和kprobe是特殊的traer,他们不同于function tracer和function graph tracer,而只是借用了tracefs的框架,以及用到了ftrace的ring buffer

4. ftrace如何工作?
There are two main types of tracing: static tracing and dynamic tracing.

Static tracing is implemented through static probes added in the source code. They have a low processing load, but the traced code is limited and defined at build time.
Dynamic tracing is implemented through dynamic probes injected into code, allowing to define at runtime the code to be traced. It has a certain processing load, but the range of source code to be traced is much larger.
Ftrace uses a combination of both static probes (function tracing, event tracing, etc) and dynamic probes (kprobes, uprobes, etc).

5. 几种tracer的概要说明
function tracer
通过在编译阶段在函数入口插入空指令,运行时对于指定的跟踪函数,将空指令替换为要执行的钩子回调,在钩子回调函数中记录函数调用关系,并写入ftrace的ring buffer
function graph tracer
编译阶段在要跟踪函数的入口处和返回处分别放置了钩子函数,这两个钩子函数会记录函数的执行时间,同时也会记录函数的调用栈,并写入ftrace的ring buffer:
trace event
trace event可以理解成为一种特殊的tracer,它没有像function tracer和function graph tracer那样向trace子系统注册trace, 它通过TRACE_EVENT宏静态定义了一系列的trace event,每个 trace event都会与一个回调关联,一般用于打印一些信息到ring buffer。trace event 由内核在初始化时批量执行trace event的创建及初始化。函数中通过trace_xxx来执行相应trace event的回调。
kprobe
是一种特殊的trace event,trace event是静态定义,kprobe可实现动态定义,它主要通过动态注册kprobe,kprobe中包含了相关的回调函数,通过提前在探测处插入一条断点指令,当函数执行到探测处触发断点异常,从而通过断点异常执行到注册的kprobe回调,默认回调函数将会打印函数的参数。
kretprobe
同kprobe,不同之处是在函数返回处插入断点指令
参考文档
https://docs.kernel.org/trace/index.html
Welcome to ftrace & the Start of Your Journey to Understanding the Linux Kernel!
https://blogs.vmware.com/opensource/author/steven-rostedt/
https://blogs.vmware.com/opensource/2018/10/11/embedded-recipes-kernel-recipes/
https://blogs.vmware.com/opensource/2018/10/11/embedded-recipes-kernel-recipes/
Analyze the Linux kernel with ftrace
ftrace tutorial
https://alex.dzyoba.com/blog/ftrace/
https://andreasch.com/2017/12/06/ftrace/
https://www.brendangregg.com/blog/2014-08-30/ftrace-the-hidden-light-switch.html
https://lwn.net/Articles/608497/

https://www.kernel.org/doc/Documentation/trace/ftrace.txt
https://www.kernel.org/doc/html/latest/trace/ftrace-design.html

https://linux.cn/article-9838-1.html
https://www.cnblogs.com/arnoldlu/p/7211249.html
————————————————
版权声明:本文为CSDN博主「HZero.chen」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jasonactions/article/details/122250033

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章