Linux ftrace 内核跟踪入门
文章目录
- ftrace介绍
- 开启ftrace
- ftrace使用
- ftrace跟踪指定内核函数
- ftrace跟踪指定pid
- ftrace原理
- ftrace与strace
- trace-cmd 工具
- KernelShark
- 参考
ftrace介绍
Ftrace is an internal tracer designed to help out developers and designers of systems to find what is going on inside the kernel. It can be used for debugging or analyzing latencies and performance issues that take place outside of user-space.
ftrace 是内建于 Linux 内核的跟踪工具,从 2.6.27 开始加入主流内核。使用 ftrace 可以调试或者分析内核中发生的事情。ftrace 提供了不同的跟踪器,以用于不同的场合,比如跟踪内核函数调用、对上下文切换进行跟踪、查看中断被关闭的时长、跟踪内核态中的延迟以及性能问题等。系统开发人员可以使用 ftrace 对内核进行跟踪调试,以找到内核中出现的问题的根源,方便对其进行修复。
使用环境:Linux linuxdev 6.8.0-52-generic #53-Ubuntu SMP PREEMPT_DYNAMIC Sat Jan 11 00:06:25 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
开启ftrace
一般的linux发行版都已经开启了ftrace支持,

最基础的是这几个选项:
- CONFIG_FTRACE --> “Tracers”
- CONFIG_FUNCTION_TRACER --> Kernel Function Tracer
- CONFIG_FUNCTION_GRAPH_TRACER --> Kernel Function Graph Tracer
- CONFIG_DYNAMIC_FTRACE --> enable/disable function tracing dynamically
更多的选项可以查看内核trace模块的makefile和kconfig文件:kernel/trace/Makefile、kernel/trace/Kconfig。
ftrace 使用 tracefs 文件系统来保存控制文件以及用于显示输出的文件,启用ftrace功能后,默认会挂载出来,目前的环境上是在:tracefs /sys/kernel/tracing tracefs rw,nosuid,nodev,noexec,relatime 0 0
查看tracefs挂载点下面的内容:
$ sudo ls /sys/kernel/tracing
available_events current_tracer hwlat_detector printk_formats set_event_pid stack_max_size trace_marker tracing_thresh
available_filter_functions dynamic_events instances README set_ftrace_filter stack_trace trace_marker_raw uprobe_events
available_filter_functions_addrs dyn_ftrace_total_info kprobe_events rv set_ftrace_notrace stack_trace_filter trace_options uprobe_profile
available_tracers enabled_functions kprobe_profile saved_cmdlines set_ftrace_notrace_pid synthetic_events trace_pipe user_events_data
buffer_percent error_log max_graph_depth saved_cmdlines_size set_ftrace_pid timestamp_mode trace_stat user_events_status
buffer_size_kb events options saved_tgids set_graph_function touched_functions tracing_cpumask
buffer_subbuf_size_kb free_buffer osnoise set_event set_graph_notrace trace tracing_max_latency
buffer_total_size_kb function_profile_enabled per_cpu set_event_notrace_pid snapshot trace_clock tracing_on
tracing目录(/sys/kernel/tracing)中的文件控制着跟踪的能力。根据你在内核配置时的选项的不同,这里列的文件可能稍有差异。你可以在内核源代码目录下Documentation/trace目录中找到这些文件的信息。
下面介绍几个重要的文件:
- available_tracers
该文件列出所有当前内核支持的tracer
# cat available_tracers
timerlat osnoise hwlat blk mmiotrace function_graph wakeup_dl wakeup_rt wakeup function nop
- current_tracer
该文件指出当前正在运行的tracer
# cat current_tracer
nop
- trace -> Contains the tracing data in human readable format
该文件包含可阅读的tracing数据
# cat trace
# tracer: nop
#
# entries-in-buffer/entries-written: 0/0 #P:4
#
# _-----=> irqs-off/BH-disabled
# / _----=> need-resched
# | / _---=> hardirq/softirq
# || / _--=> preempt-depth
# ||| / _-=> migrate-disable
# |||| / delay
# TASK-PID CPU# ||||| TIMESTAMP FUNCTION
# | | | ||||| | |
- tracing_on
该文件开启/关闭输出tracing数据到ring buffer(ftrace使用单独的ring buffer来存储tracing数据)
# cat tracing_on
1
ftrace使用
ftrace一般使用步骤:
- 写入一些特定文件以启用 / 禁用tracing。
- 写入一些特定文件以设置 / 取消设置过滤器以微调tracing。
- 根据步骤 1 和 2 从文件中读取生成的tracing输出。
- 清除文件中的早期输出或缓冲区。
- 缩小到你的特定用例(要跟踪的内核函数)并重复步骤 1、2、3、4。
指定某个tracer,我们只要将该tracer的名称写入current_tracer文件。
# echo function > current_tracer
随后我们可以通过trace或者trace_pipe文件读取输出
# cat trace | head -20
# tracer: function
#
# entries-in-buffer/entries-written: 205023/41961107 #P:4
#
# _-----=> irqs-off/BH-disabled
# / _----=> need-resched
# | / _---=> hardirq/softirq
# || / _--=> preempt-depth
# ||| / _-=> migrate-disable
# |||| / delay
# TASK-PID CPU# ||||| TIMESTAMP FUNCTION
# | | | ||||| | |mintreport-tray-6561 [003] ...1. 23204.688260: seq_put_decimal_ull_width <-seq_put_decimal_ullmintreport-tray-6561 [003] ...1. 23204.688260: seq_put_decimal_ull <-do_task_statmintreport-tray-6561 [003] ...1. 23204.688260: seq_put_decimal_ull_width <-seq_put_decimal_ullmintreport-tray-6561 [003] ...1. 23204.688260: seq_put_decimal_ull <-do_task_stat
##### CPU 2 buffer started ####<idle>-0 [002] d.h2. 23204.749361: __sysvec_apic_timer_interrupt <-sysvec_apic_timer_interrupt<idle>-0 [002] d.h2. 23204.749362: hrtimer_interrupt <-__sysvec_apic_timer_interrupt<idle>-0 [002] d.h2. 23204.749362: _raw_spin_lock_irqsave <-hrtimer_interrupt
如果你想关闭该tracer,直接将nop写入current_tracer文件即可。
function_graph是一种替代的函数跟踪器,它不仅跟踪函数入口,还跟踪函数的返回,允许你创建函数流的调用图,并以类似 C 语言的风格输出跟踪数据,其中包含每个函数的持续时间信息。
# cat trace | head -20
# tracer: function_graph
#
# CPU DURATION FUNCTION CALLS
# | | | | | | |2) 0.151 us | } /* seq_printf */2) 0.152 us | seq_printf();2) 0.152 us | seq_printf();2) 0.149 us | seq_printf();2) 0.112 us | _raw_spin_lock_irqsave();2) 0.153 us | seq_printf();2) 0.160 us | seq_printf();2) 0.159 us | seq_printf();2) 0.153 us | seq_printf();2) 0.156 us | seq_printf();2) 0.111 us | seq_putc();2) 0.112 us | _raw_spin_unlock_irqrestore();2) 0.117 us | __rcu_read_unlock();2) 4.759 us | } /* show_interrupts */2) 0.109 us | int_seq_next();2) 0.109 us | int_seq_stop();
ftrace跟踪指定内核函数
available_filter_functions文件展示了ftrace支持的跟踪内核函数的集合,我们可以从这里寻找需要跟踪的内核函数,或者自己指定。
# grep fork available_filter_functions
ret_from_fork
__do_sys_fork
__do_sys_vfork
tsk_fork_get_node
__traceiter_sched_process_fork
__probestub_sched_process_fork
__sched_fork
sched_fork
sched_cgroup_fork
sched_post_fork
sched_mm_cid_fork
task_fork_fair
task_fork_dl
sched_core_fork
sched_autogroup_fork
timens_on_fork
cgroup_css_set_put_fork
cgroup_fork
cgroup_cancel_fork
cgroup_post_fork
cgroup_css_set_fork
cgroup_can_fork
freezer_fork
pids_cancel_fork
pids_can_fork
cpuset_cancel_fork
cpuset_can_fork
cpuset_fork
perf_event_fork
anon_vma_fork
mem_cgroup_fork
tty_audit_fork
register_random_vmfork_notifier
unregister_random_vmfork_notifier
add_vmfork_randomness
proc_fork_connector
尝试跟踪__do_sys_fork函数,很遗憾目前环境中的内核在创建进程时不使用该函数,而是使用kernel_clone这个函数
#ifdef __ARCH_WANT_SYS_FORK
SYSCALL_DEFINE0(fork)
{
#ifdef CONFIG_MMUstruct kernel_clone_args args = {.exit_signal = SIGCHLD,};return kernel_clone(&args);
#else/* can not support in nommu mode */return -EINVAL;
#endif
}
#endif
查找该函数:
# grep kernel_clone /sys/kernel/tracing/available_filter_functions
kernel_clone
跟踪该函数:
root@linuxdev:/sys/kernel/tracing# echo nop >current_tracer
root@linuxdev:/sys/kernel/tracing# echo kernel_clone>set_graph_function
root@linuxdev:/sys/kernel/tracing# echo function_graph >current_tracer
root@linuxdev:/sys/kernel/tracing# cat trace
# tracer: function_graph
#
# CPU DURATION FUNCTION CALLS
# | | | | | | |0) | kernel_clone() {0) # 1520.654 us | copy_process();0) 2.568 us | add_device_randomness();0) 1.289 us | get_task_pid();0) 0.757 us | pid_vnr();0) 0.673 us | _raw_spin_lock();0) 3.660 us | lru_gen_add_mm();0) 0.690 us | _raw_spin_unlock();0) + 47.696 us | wake_up_new_task();0) 1.016 us | put_pid();0) # 1592.533 us | }1) | ret_from_fork() {1) ! 122.284 us | schedule_tail();1) 0.859 us | syscall_exit_to_user_mode_prepare();1) 0.638 us | mem_cgroup_handle_over_high();1) 0.666 us | blkcg_maybe_throttle_current();1) + 60.708 us | __rseq_handle_notify_resume();1) 0.768 us | fpregs_assert_state_consistent();1) 1.831 us | switch_fpu_return();1) ! 197.731 us | }3) | kernel_clone() {3) # 1391.121 us | copy_process();3) 2.695 us | add_device_randomness();3) 1.232 us | get_task_pid();3) 0.783 us | pid_vnr();3) 0.669 us | _raw_spin_lock();3) 3.709 us | lru_gen_add_mm();3) 0.654 us | _raw_spin_unlock();3) + 54.441 us | wake_up_new_task();3) 1.083 us | put_pid();3) # 1469.054 us | }------------------------------------------0) bash-7769 => cat-8888 ------------------------------------------0) | ret_from_fork() {0) + 69.115 us | schedule_tail();0) 0.804 us | syscall_exit_to_user_mode_prepare();0) 0.689 us | mem_cgroup_handle_over_high();0) 0.677 us | blkcg_maybe_throttle_current();0) + 37.261 us | __rseq_handle_notify_resume();0) 0.756 us | fpregs_assert_state_consistent();0) 1.950 us | switch_fpu_return();0) ! 120.873 us | }
ftrace跟踪指定pid
# echo $PID > set_ftrace_pid
以监控top进程为例:
# pidof top
8963
# echo 8963 > set_ftrace_pid
# cat trace | head -30
# tracer: function
#
# entries-in-buffer/entries-written: 13655/8126400 #P:4
#
# _-----=> irqs-off/BH-disabled
# / _----=> need-resched
# | / _---=> hardirq/softirq
# || / _--=> preempt-depth
# ||| / _-=> migrate-disable
# |||| / delay
# TASK-PID CPU# ||||| TIMESTAMP FUNCTION
# | | | ||||| | |top-8963 [001] d..2. 27383.603413: <stack trace>=> 0xffffffffc12840ad=> _raw_spin_unlock=> finish_task_switch.isra.0=> __schedule=> __cond_resched=> mutex_lock=> process_output_block=> n_tty_write=> iterate_tty_write=> file_tty_write.isra.0=> tty_write=> vfs_write=> ksys_write=> __x64_sys_write=> x64_sys_call=> do_syscall_64=> entry_SYSCALL_64_after_hwframe
需要注意的是每次进行新的tracing的时候需要清除下上一次tracing的配置,如果上次设置了某些过滤条件,那么可能会对本次的tracing结果产生影响
更进一步的使用建议参考Debugging Linux Kernel using ftrace着一系列文章。
ftrace原理
参考Ftrace 实现原理与开发实践

ftrace与strace
参考Ftrace 实现原理与开发实践

他们的底层原理不同,表现出来的区别是ftrace可以跟踪内核中的函数,而strace只能跟踪到系统函数调用。
strace is a utility which allows you to trace the system calls that an application makes. When an application makes a system call, it is basically asking the kernel to do something, eg file access. Use the command man strace to get strace documentation and man syscalls to get information on system calls.
ftrace is a tool used during kernel development and allows the developer to see what functions are being called within the kernel.
参考这张著名的linux性能分析工具图:

trace-cmd 工具
trace-cmd工具是Steven Rostedt创建的用于ftrace的命令行工具。
KernelShark
KernelShark是一个图形工具,作为一个前端来处理trace-cmd工具生成的tracing数据——trace.dat。
参考
ftrace - Function Tracer
Debugging Linux Kernel using ftrace
Analyze the Linux kernel with ftrace
从Ftrace开始内核探索之旅
Tracing the Linux kernel with ftrace
Debugging the kernel using Ftrace - part 1
Debugging the kernel using Ftrace - part 2
Secrets of the Ftrace function tracer
Using KernelShark to analyze the real-time scheduler
Ftrace Kernel Hooks: More than just tracing
ftrace系统实现原理
Linux性能工具(二)ftrace基础篇
linux性能分析工具–ftrace的原理与使用
Linux内核性能调试工具之ftrace
Ftrace 实现原理与开发实践
相关文章:
Linux ftrace 内核跟踪入门
文章目录 ftrace介绍开启ftraceftrace使用ftrace跟踪指定内核函数ftrace跟踪指定pid ftrace原理ftrace与stracetrace-cmd 工具KernelShark参考 ftrace介绍 Ftrace is an internal tracer designed to help out developers and designers of systems to find what is going on i…...
1Panel应用推荐:WordPress开源博客软件和内容管理系统
1Panel(github.com/1Panel-dev/1Panel)是一款现代化、开源的Linux服务器运维管理面板,它致力于通过开源的方式,帮助用户简化建站与运维管理流程。为了方便广大用户快捷安装部署相关软件应用,1Panel特别开通应用商店&am…...
【数据结构-C语言】绪论
文章目录 一、前言二、基本概念和术语2.1 数据元素、数据项和数据对象2.2 数据结构2.2.1 逻辑结构2.2.2 存储结构 2.3 时间复杂度 一、前言 数据结构部分是根据严蔚敏老师的《数据结构-C语言版第2版》书中内容整理的。 二、基本概念和术语 2.1 数据元素、数据项和数据对象 …...
java poi Excel 文件导入导出常见错误及解决方案
在使用 Apache POI 进行 Excel 文件的导入导出操作时,可能会遇到各种问题。以下是一些常见的错误及其解决方案: 一、文件格式相关问题 1. 文件格式不兼容 问题描述:尝试使用 HSSFWorkbook 读取 .xlsx 文件,或者使用 XSSFWorkbo…...
深入浅出DeepSeek LLM 以长远主义拓展开源语言模型
深入浅出地讲解DeepSeek LLM 以长远主义拓展开源语言模型 🌟 1. 什么是 DeepSeek LLM? 大家想象一下,你在游戏里要打造一个超级英雄角色,选择最强的装备、技能点和升级策略。那么,DeepSeek LLM 就是 AI 界的“超级英雄…...
【Leetcode 每日一题】59. 螺旋矩阵 II
问题背景 给你一个正整数 n n n,生成一个包含 1 1 1 到 n 2 n ^ 2 n2 所有元素,且元素按顺时针顺序螺旋排列的 n n n \times n nn 正方形矩阵 m a t r i x matrix matrix。 数据约束 1 n 20 1 \times n \times 20 1n20 解题过程 定义方向数组…...
回退 android studio emulator 的版本
前情提要 最近用 frida 需要一个完全跑 arm64 的手机 os,因为雷电实时转义 arm 到 x64 的方案本质上还是 x64,会导致 frida 有 bug。查了一下有帖子说 android studio 自带的模拟器支持直接跑 arm64 的镜像 (Other Images) 直接跑跑不通,调…...
数据资产的管理与价值释放
引言:从 “黑金” 到 “数据” 的文明跃迁 在探讨数字资产的未来之前,我们不妨先回顾一下黄金在人类历史长河中的角色。黄金,这种闪耀着独特光芒的金属,从远古时代起就与人类文明紧密相连。在古埃及,黄金被视为太阳神…...
部署夜景增强模型Learning to See in the Dark以及gradio UI编程方法
前面我们已经把Learning to See in the Dark的paper和原理进行了解读,现在把Learning to See in the Dark(后续简称SID模型)部署看一下效果。 这篇文章选择的部署方式是gradio 本地pytorch直接推理。先看一下效果: 对单个文件进…...
【报错解决】MySQL报错:sql_mode=only_full_group_by
文章目录 报错信息 DataGrip 报错还原Navicat 报错还原 报错原因解决方案 查看当前 sql mode方案一:临时解决方案二:永久解决方案三:使用 any_value() 或 group_concat()方案四:调整实现思路,避开 GROUP BY 使用 我…...
【大数据技术】用户行为日志分析(python+hadoop+mapreduce+yarn+hive)
用户行为日志分析(pythonhadoopmapreduceyarnhive) 搭建完全分布式高可用大数据集群(VMwareCentOSFinalShell) 搭建完全分布式高可用大数据集群(HadoopMapReduceYarn) 本机PyCharm远程连接虚拟机Python …...
[Day 16]螺旋遍历二维数组
今天我们看一下力扣上的这个题目:146.螺旋遍历二维数组 题目描述: 给定一个二维数组 array,请返回「螺旋遍历」该数组的结果。 螺旋遍历:从左上角开始,按照 向右、向下、向左、向上 的顺序 依次 提取元素,…...
大模型的底层逻辑及Transformer架构
一、大模型的底层逻辑 1.数据驱动 大模型依赖海量的数据进行训练,数据的质量和数量直接影响模型的性能。通过大量的数据,模型能够学习到丰富的模式和规律,从而更好地处理各种任务。 2.深度学习架构 大模型基于深度学习技术,通常采用多层神经网络进行特征学习与抽象。其中…...
数据结构-基础
1、概念: 程序 数据结构 算法 2、程序的好坏 可读性,稳定性,扩展性,时间复杂度,空间复杂度。 3、数据结构 是指存储、组织数据的方式,以便高效地进行访问和修改。通过选择适当的数据结构, 能…...
SystemUI中NavigationBar分析
需求 SystemUI是一个与系统组件显示紧密相关的应用,包含快捷中心、消息通知、状态栏、导航栏、任务中心等诸多模块,本文介绍NavigationBar模块。SystemUI源码位于/frameworks/base/packages/SystemUI,Android13平台。NavigationBar显示如下&…...
MySQL的底层原理与架构
前言 了解MySQL的架构和原理对于很多的后续很多的操作会有很大的帮助与理解。并且很多知识都与底层架构相关联。 了解MySQL架构 通过上面的架构图可以得知,Server层中主要由 连接器、查询缓存、解析器/分析器、优化器、执行器 几部分组成的,下面将主要…...
三极管的截止、放大、饱和区
三极管的几个区,都有什么用: 截止区:晶体管不导通,用于开关电路的“关”状态。 放大区:晶体管用于信号放大,集电极电流与基极电流成正比。 饱和区:晶体管完全导通,用于开关电路的“…...
2025-2-7-算法学习(一) 动态规划-习题1 300.最长递增子序列
文章目录 算法学习(一) 动态规划-习题1 300.最长递增子序列(1)题目(2)举例:(3)提示(4)分析(5)动态规划代码:&a…...
学习日记-250207
一.论文 1.Prompt Learning for News Recommendation 任务不一致(LLM与实际任务)产生prompt提示。 Prompt Learning for News Recommendation 论文阅读 SIGIR2023-CSDN博客 2.GPT4Rec: A Generative Framework for Personalized Recommendation and…...
【Block总结】PSA,金字塔挤压注意力,解决传统注意力机制在捕获多尺度特征时的局限性
论文信息 标题: EPSANet: An Efficient Pyramid Squeeze Attention Block on Convolutional Neural Network论文链接: arXivGitHub链接: https://github.com/murufeng/EPSANet 创新点 EPSANet提出了一种新颖的金字塔挤压注意力(PSA)模块,旨…...
音乐播放器界面定制指南:foobar2000美化方案与体验提升
音乐播放器界面定制指南:foobar2000美化方案与体验提升 【免费下载链接】foobox-cn DUI 配置 for foobar2000 项目地址: https://gitcode.com/GitHub_Trending/fo/foobox-cn 在数字音乐时代,播放器已不仅是播放工具,更是个人音乐品味的…...
GPT-OSS-20B参数调优实战:如何设置才能获得最佳生成效果
GPT-OSS-20B参数调优实战:如何设置才能获得最佳生成效果 1. 模型特性与调优基础 1.1 GPT-OSS-20B核心架构 GPT-OSS-20B作为OpenAI开源的重量级模型,采用混合专家架构(MoE)设计,总参数量210亿,其中活跃参数36亿。这种设计使其在…...
CSS动画播放状态控制终极指南:掌握交互式动画实现技巧
CSS动画播放状态控制终极指南:掌握交互式动画实现技巧 【免费下载链接】css-reference CSS Reference: a free visual guide to the most popular CSS properties 项目地址: https://gitcode.com/gh_mirrors/cs/css-reference CSS动画播放状态控制是网页交互…...
Generalized Mask-aware IoU for Anchor Assignment for Real-time Instance Segmentation—面向实时实例分割的锚点分配方法
《广义掩膜感知IoU:面向实时实例分割的锚点分配方法》主要研究并解决实时实例分割任务中锚点分配不准确的问题。其核心创新在于提出了一种新的度量标准——广义掩膜感知交并比,并将其应用于锚点的正负样本分配,从而显著提升了模型的性能与效率…...
3步颠覆传统下载体验:百度网盘直链解析工具让你告别会员枷锁
3步颠覆传统下载体验:百度网盘直链解析工具让你告别会员枷锁 【免费下载链接】baidu-wangpan-parse 获取百度网盘分享文件的下载地址 项目地址: https://gitcode.com/gh_mirrors/ba/baidu-wangpan-parse 从200KB/s到5MB/s的蜕变 你是否也曾遇到这样的困境&a…...
前端开发者必看:5个提升AI提示词效果的实战技巧(附代码示例)
前端开发者必看:5个提升AI提示词效果的实战技巧(附代码示例) 当ChatGPT帮你生成React组件却总跑偏,当Copilot给出的代码建议总差那么点意思——作为前端开发者,你可能已经意识到:AI工具的表现力,…...
DeepSeek技术解析:如何利用128K上下文窗口提升代码生成效率
1. 128K上下文窗口的技术革命 第一次看到DeepSeek支持128K上下文窗口时,我的反应和大多数开发者一样:"这数字是不是多打了个0?"毕竟在主流大模型还停留在32K上下文的时候,这个参数直接翻了四倍。但实测下来才发现&#…...
Gemma-3-12b-it镜像免配置实战:单命令启动多模态服务并集成Flask API
Gemma-3-12b-it镜像免配置实战:单命令启动多模态服务并集成Flask API 1. 快速了解Gemma-3-12b-it多模态能力 Gemma-3-12b-it是Google推出的轻量级多模态模型,它最大的特点就是能同时理解文字和图片。想象一下,你给它一张照片,它…...
Gradio界面定制化:为DAMO-YOLO WebUI添加导出检测结果CSV功能
Gradio界面定制化:为DAMO-YOLO WebUI添加导出检测结果CSV功能 1. 项目背景与需求 如果你用过那个基于DAMO-YOLO的手机检测WebUI,可能会发现一个问题:检测结果只能看,不能存。 每次上传图片,系统会告诉你检测到了几个…...
隐式建模的革新:GemPy如何重新定义三维地质结构可视化
隐式建模的革新:GemPy如何重新定义三维地质结构可视化 【免费下载链接】gempy GemPy is an open-source, Python-based 3-D structural geological modeling software, which allows the implicit (i.e. automatic) creation of complex geological models from int…...
