ktracepoint
A Rust tracepoint library for kernel scenarios, designed with goals similar to Linux tracepoints:
- Define events and fields with macros
- Manage tracepoints by subsystem and event 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:
TracingEventsManager -> subsystem -> event - 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
write_kernel_text is used for static key instruction patching.
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 manager = ?;
6. Enable, filter, and consume output
let subsystem = manager.get_subsystem.unwrap;
let event = subsystem.get_event.unwrap;
event.enable_file.write;
event.tracepoint.enable_event;
event.filter_file.write.unwrap;
// Read format and ID
let fmt = event.format_file.read;
let id = event.id_file.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/TracePointMapTracingEventsManager/EventsSubsystem/EventInfoTracePipeRaw/TracePipeSnapshot/TracePipeOpsTraceCmdLineCache/TraceEntryParser