This repository contains C and C++ libraries and tools for collecting and decoding
Linux Tracepoint
events and for generating Tracepoint events from user mode using the
user_events facility. This
includes support for eventheader events and perf.data files.
Related repositories:
- LinuxTracepoints-Net -
.NET libraries and tools for decoding Linux Tracepoint events and for generating
Tracepoint events from user mode using the
user_eventsfacility. This includes support foreventheaderevents andperf.datafiles. - LinuxTracepoints-Rust -
Rust libraries for generating Tracepoint events from user mode using the
user_eventsfacility. This includes support foreventheaderevents.
-
libtracepoint - low-level C/C++ tracing interface. Designed to support replacement at link-time if a different implementation is needed (e.g. for testing).
- tracepoint-provider.h -
a developer-friendly C/C++ API for writing tracepoint events to any
implementation of the
tracepoint.hinterface. - tracepoint.h - low-level interface for writing tracepoint events.
- libtracepoint.a -
default implementation that writes directly to the Linux
user_eventsfacility.
- tracepoint-provider.h -
a developer-friendly C/C++ API for writing tracepoint events to any
implementation of the
-
libtracepoint-control-cpp - C++ library for controlling a tracepoint event collection session.
perf-collectis a tool that collects tracepoint events into aperf.datafile using thelibtracepoint-control-cpplibrary. This tool is similar to theperf recordcommand, but it includes special "pre-register" support to simplify collection ofuser_eventstracepoints that are not yet registered when trace collection begins.TracepointSession.himplements an event collection session that can collect tracepoint events and enumerate the events that the session has collected.TracepointPath.hhas functions for finding the/sys/kernel/tracingmount point and readingformatfiles.TracepointName.hrepresents a tracepoint name (system name + event name); for instance,user_events:eventName.TracepointSpec.hrepresents a tracepoint specification (system name + event name + field definitions); for instance,user_events:eventName int field1.TracepointCache.himplements a cache for tracking parsedformatfiles and locating cached data byTracepointNameor bycommon_typeid.
-
libtracepoint-decode-cpp - C++ library for decoding tracepoints and reading/writing
perf.datafiles. Works on both Linux and Windows.PerfDataFile.hdefines thePerfDataFileclass that decodesperf.datafiles.PerfEventInfo.hdefines thePerfSampleEventInfoandPerfNonSampleEventInfostructures for raw event information.PerfEventMetadata.hdefines classes for parsing ftrace event metadata information.
-
libeventheader-tracepoint -
eventheaderenvelope that supports extended attributes including severity level and optional field information (field types and field names).- TraceLoggingProvider.h -
a developer-friendly C/C++ API for writing
eventheader-encapsulated events to any implementation of the tracepoint interface. - EventHeaderDynamic.h -
C++ API for writing runtime-defined
eventheader-encapsulated events, intended for use as an implementation layer for a higher-level API like OpenTelemetry.
- TraceLoggingProvider.h -
a developer-friendly C/C++ API for writing
-
libeventheader-decode-cpp - C++ library for decoding events that use the
eventheaderenvelope. Works on both Linux and Windows.EventEnumeratorclass parses an event into fields.EventFormatterclass converts event data into a string.perf-decodetool that decodesperf.datafiles to JSON with support foreventheaderevents.
-
Configure a Linux system with the
user_eventsfeature enabled.- Supported on Linux kernel 6.4 and later.
- Kernel must be built with
user_eventssupport (CONFIG_USER_EVENTS=y). - Must have either
tracefsordebugfsmounted. For example, you might add the following line to your/etc/fstabfile:tracefs /sys/kernel/tracing tracefs defaults 0 0 - The user that will generate events must have
xaccess to thetracingdirectory andwaccess to thetracing/user_events_datafile. One possible implementation is to create atracersgroup, then:chgrp tracers /sys/kernel/tracingchgrp tracers /sys/kernel/tracing/user_events_datachmod g+x /sys/kernel/tracingchmod g+w /sys/kernel/tracing/user_events_data
-
Use one of the event generation APIs to write a program that generates events.
- C/C++ programs can use
tracepoint-provider.h
to generate regular Linux Tracepoint events that are defined at compile-time.
(Link with
libtracepoint.) - C/C++ programs can use
TraceLoggingProvider.h
to generate eventheader-enabled Tracepoint events that are defined at
compile-time. (Link with
libtracepointandlibeventheader-tracepoint.) - C++ middle-layer APIs (e.g. an OpenTelemetry exporter) can use
EventHeaderDynamic.h
to generate eventheader-enabled Tracepoint events that are runtime-dynamic.
(Link with
libtracepointandlibeventheader-tracepoint.) - Rust or .NET programs can use LinuxTracepoints-Rust or LinuxTracepoints-Net to generate Tracepoint events.
- C/C++ programs can use
tracepoint-provider.h
to generate regular Linux Tracepoint events that are defined at compile-time.
(Link with
-
To collect events in a C++ program, use libtracepoint-control-cpp. Note that your program must run as a privileged user (
CAP_PERFMONcapability plus read access to/sys/kernel/tracing/events) because access to the event collection system is restricted by default. -
To collect events without writing C++ code, use the included perf-collect tool or the Linux
perftool to collect events to aperf.datafile, e.g.perf-collect -o File.perf user_events:MyEvent1 user_events:MyEvent2orperf record -o File.perf -k monotonic -e user_events:MyEvent1,user_events:MyEvent2. Note that you must run the tool as a privileged user to collect events (CAP_PERFMONcapability plus read access to/sys/kernel/tracing/events).- The
perftool binary is typically available as part of thelinux-perfpackage (e.g. can be installed byapt install linux-perf). However, this package installs aperf_VERSIONbinary rather than aperfbinary, so you will need to add an appropriate VERSION suffix to yourperfcommands or use a wrapper script. - To capture tracepoints using
perf, you'll also need to installlibtraceevent, e.g.apt install libtraceevent1. - The
linux-basepackage installs aperfwrapper script that redirects to the version ofperfthat matches your current kernel (if present) so that you can run the appropriate version ofperfwithout the VERSION suffix. This frequently doesn't work because the latestperfbinary fromaptdoesn't always match the running kernel, so you may want to make your own wrapper script instead. - Note that for purposes of collecting events, it is usually not important
for the version of the
perftool to match the kernel version, so it's ok to use e.g.perf_5.10even if you are running a newer kernel.
- The
-
Note that tracepoints must be registered before you can start collecting them. The
perf-collecttool has facilities to pre-register a user_events tracepoint. Theperfcommand will report an error if the tracepoint is not yet registered.- You can usually register tracepoints by starting the program that generates them. Most programs will register all of their tracepoints when they start running. (They will usually unregister when they stop running.)
- You can also use the
tracepoint-registertool to pre-register an event so you can start collecting it before starting the program that generates it. - If writing your own event collection tool, you might do something similar
in your tool to pre-register the events that you need to collect. For
example, you might use the
PreregisterTracepointorPreregisterEventHeaderTracepointmethods of theTracepointCacheclass inlibtracepoint=control.
-
Use the
perf-decodetool to decode theperf.datafile to JSON text, or write your own decoding tool using libtracepoint-decode-cpp andlibeventheader-decode-cpp.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.