ktracepoint
A Rust tracepoint library for kernel scenarios, designed with goals similar to Linux tracepoints:
- Define events and fields with macros
- Manage tracepoints by unique event ID at runtime
- Support enable/disable, filter expressions, and callbacks
- Provide both raw event buffering and human-readable output
- no_std compatible
Repository: https://github.com/Starry-OS/tracepoint
Core Capabilities
- Event definition: use
define_event_trace!to generate event metadata, call functions, and register functions in one place - Event management:
TracePointMapindexed by tracepoint ID - Event control: enable/disable, format/id/filter
- Filter expressions: compiled and evaluated against schema via
tp-lexer - Output pipeline:
TracePipeRaw+TraceEntryParser
Quick Start
1. Add dependencies
[]
= "*"
= "0.8"
2. Keep the .tracepoint section in your linker script
This library scans event metadata through __start_tracepoint / __stop_tracepoint.
Merge the content of my_section.ld into your linker script and ensure the .tracepoint section is kept with KEEP.
3. Implement KernelTraceOps
You need to provide:
time_nowcpu_idcurrent_pidtrace_pipe_push_raw_recordtrace_cmdline_pushwrite_kernel_text- tracepoint state registry hooks:
read_tracepoint_state,write_tracepoint_state
write_kernel_text is used for static key instruction patching.
The state registry hooks let your OS choose its own synchronization strategy for callbacks and filters.
Callback restrictions
read_tracepoint_state may hold a read-side lock while the tracing fast path executes callbacks. If your implementation uses a non-reentrant lock such as RwLock, callbacks must not:
- register or unregister tracepoint callbacks
- update tracepoint filters
- call other APIs that require
write_tracepoint_state - recursively trigger tracepoints backed by the same state registry
Violating these rules may deadlock. Hosts that implement read_tracepoint_state with RCU, snapshots, or another non-blocking read-side mechanism may provide weaker restrictions.
4. Define and invoke events
use ;
define_event_trace!;
// Generated functions: trace_TEST / register_trace_TEST / unregister_trace_TEST
trace_TEST;
Note: TP_STRUCT__entry participates in byte layout. Ensure your field layout matches expectations (think in C layout terms).
5. Initialize the manager
use global_init_events;
global_init;
let = ?;
// Install `ext_tracepoints` into the registry used by
// Kops::read_tracepoint_state and Kops::write_tracepoint_state.
6. Enable, filter, and consume output
use ;
let event_id = 0;
let tracepoint = tracepoints.get.unwrap;
new.write;
tracepoint.enable_event;
write_tracepoint_state;
// Read format and ID
let fmt = new.read;
let id = new.read;
Run the example
Example code is in examples/usage.rs, covering:
- Event definition and triggering
- Event enabling and filtering
- Registering event/raw callbacks
- Reading
TracePipeRawsnapshots and parsing them into text
Main Public Types
KernelTraceOpsTracePoint/ExtTracePoint/TracePointMapTracePipeRaw/TracePipeSnapshot/TracePipeOpsTraceCmdLineCache/TraceEntryParser