备注
AI Translation Notice
This document was automatically translated by Qwen/Qwen3-8B
model, for reference only.
Source document: kernel/trace/tracepoint.md
Translation time: 2025-06-14 09:36:42
Translation model:
Qwen/Qwen3-8B
Please report issues via Community Channel
Tracepoints
Author: Chen Linfeng
Email: chenlinfeng25@outlook.com
Overview
Tracepoints are a tracing mechanism provided by the Linux kernel, allowing developers to insert probe points at specific locations in the kernel code to collect runtime information. Unlike kprobes, tracepoints are predefined and are typically used for collecting performance data or debugging information without modifying the kernel code.
Workflow
Define Tracepoint: Kernel developers define tracepoints in the code using the macro
define_event_trace
.Trigger Tracepoint: Developers can trigger the defined tracepoints from other parts of the code by using the functions defined with
define_event_trace
, and pass relevant context information. For example, if the defined tracepoint is namedmy_tracepoint
, it can be triggered usingtrace_my_tracepoint()
.Collect Data: When a tracepoint is triggered, the kernel records the related data. This data can be analyzed using user-space tools (such as
trace-cmd
orperf
). Currently, DragonOS only supports viewing the data files created by the kernel for these data.Reading the
/sys/kernel/debug/tracing/trace
file can view the tracepoint collected data. The buffer content will not be cleared.Reading the
/sys/kernel/debug/tracing/trace_pipe
file can view the tracepoint collected data. The buffer content will be cleared. If there is no content in the buffer, it will block until new data is available.Reading the
/sys/kernel/debug/tracing/events/
directory can view all available tracepoints.Reading the
/sys/kernel/debug/tracing/events/<event_name>/format
file can view the data format of a specific tracepoint.Writing to the
/sys/kernel/debug/tracing/events/<event_name>/enable
file can enable a specific tracepoint.Writing an empty value to
/sys/kernel/debug/tracing/trace
can clear the current trace data.
Analyze Data: User-space tools can read the data collected by tracepoints and generate reports or charts to help developers understand kernel behavior.
Interface
define_event_trace!() // 定义一个 tracepoint
// example
define_event_trace!(
sys_enter_openat,
TP_system(syscalls),
TP_PROTO(dfd: i32, path:*const u8, o_flags: u32, mode: u32),
TP_STRUCT__entry{
dfd: i32,
path: u64,
o_flags: u32,
mode: u32,
},
TP_fast_assign{
dfd: dfd,
path: path as u64,
o_flags: o_flags,
mode: mode,
},
TP_ident(__entry),
TP_printk({
format!(
"dfd: {}, path: {:#x}, o_flags: {:?}, mode: {:?}",
__entry.dfd,
__entry.path,
__entry.o_flags,
__entry.mode
)
})
);
Tracepoints For eBPF
In DragonOS, tracepoints can also be used in conjunction with eBPF to collect richer data within the kernel. eBPF programs can be attached to tracepoints to execute custom logic when a tracepoint is triggered.