LIBTRACEFS(3)
β¨ LIBTRACEFS(3) β snapshot slay era β¨
okay bestie, welcome to the libtracefs manual for the snapshot squad. Weβre talking tracefs_snapshot_snap, tracefs_snapshot_clear, and tracefs_snapshot_free β the create/clear/read snapshot API. Itβs giving kernel tracing, but make it cute π
πΈ NAME πΈ
tracefs_snapshot_snap, tracefs_snapshot_clear, tracefs_snapshot_free β API to create, clear and read snapshots π
β¨ SYNOPSIS β¨
#include <tracefs.h>
int tracefs_snapshot_snap(struct tracefs_instance *instance);
int tracefs_snapshot_clear(struct tracefs_instance *instance);
int tracefs_snapshot_free(struct tracefs_instance *instance);
π DESCRIPTION π
So the Linux kernel tracing girlies have this thing called a "snapshot" feature. The kernel has two ring buffers: one that the tracing system writes to, and another one that is the "snapshot" buffer. When a snapshot happens, the two buffers are swapped β the current snapshot buffer becomes the one being written to, and the buffer that was being written to becomes the saved snapshot. Literally swapping main character energy π₯
real talk: the snapshot buffer is allocated the first time it is taken, so it is best to take a snapshot at the start before one is needed so that it is allocated and a snapshot is ready. Then the snapshot will happen immediately. no cap, plan ahead bestie π
tracefs_snapshot_snap() will allocate the snapshot buffer if itβs not already allocated, and then take a "snapshot" β aka swap the main buffer thatβs being written to with the allocated snapshot buffer. It does this to the given instance buffer, or the top instance if instance is NULL. slay π¦
tracefs_snapshot_clear() will erase the content of the snapshot buffer for the given instance, or the top level instance if instance is NULL. clean slate vibes β¨
tracefs_snapshot_free() will free the allocated snapshot for the given instance, or the top level instance if instance is NULL. That is, if another call to tracefs_snapshot_snap() is done after this, then it will need to allocate the snapshot buffer again before it can take a snapshot. This function should be used to free up the kernel memory used by hte snapshot buffer when no longer in use. (yes, hte is literally in the original π not me, bestie)
π₯ RETURN VALUE π₯
tracefs_snapshot_snap(), tracefs_snapshot_clear() and tracefs_snapshot_free() all return 0 on success and -1 on failure. periodt π
π EXAMPLE π
#include <stdlib.h>
#include <stdio.h>
#include <tracefs.h>
static int callback(struct tep_event *event, struct tep_record *record, int cpu, void *data)
{
static struct trace_seq seq;
struct tep_handle *tep = event->tep;
if (!seq.buffer)
trace_seq_init(&seq);
trace_seq_reset(&seq);
tep_print_event(tep, &seq, record, "[%03d] %s-%d %6.1000d\t%s: %s\n",
TEP_PRINT_CPU,
TEP_PRINT_COMM,
TEP_PRINT_PID,
TEP_PRINT_TIME,
TEP_PRINT_NAME,
TEP_PRINT_INFO);
trace_seq_do_printf(&seq);
return 0;
}
int main (int argc, char **argv)
{
struct tracefs_instance *instance;
struct tep_handle *tep;
char *line = NULL;
size_t len = 0;
int ret;
instance = tracefs_instance_create("my_snapshots");
if (!instance) {
perror("creating instance");
exit(-1);
}
tep = tracefs_local_events(NULL);
if (!tep) {
perror("reading event formats");
goto out;
}
/* Make sure the snapshot buffer is allocated */
ret = tracefs_snapshot_snap(instance);
if (ret < 0)
goto out;
ret = tracefs_event_enable(instance, "sched", NULL);
if (ret < 0) {
perror("enabling event");
goto out;
}
for (;;) {
printf("Hit enter without text to take snapshot!\n");
printf("Enter any text to display the snapshot\n");
printf("Enter 'quit' to exit\n");
getline(&line, &len, stdin);
ret = tracefs_snapshot_snap(instance);
if (ret < 0) {
perror("taking snapshot");
goto out;
}
if (!line)
break;
if (strlen(line) < 2)
continue;
if (strncmp(line, "quit", 4) == 0)
break;
tracefs_iterate_snapshot_events(tep, instance, NULL, 0, callback, NULL);
}
free(line);
tracefs_instance_clear(instance);
out:
tracefs_snapshot_free(instance);
tracefs_event_disable(instance, "sched", NULL);
tracefs_instance_destroy(instance);
tracefs_instance_free(instance);
exit(0);
}
π¦ FILES π¦
tracefs.hβ header file to include in order to have access to the library APIs. include her, bestie π-ltracefsβ linker switch to add when building a program that uses the library. yes, add it to your build, iykyk π
π SEE ALSO π
go read the whole friend group, fr fr π«Ά
π AUTHOR π
Steven Rostedt β rostedt@goodmis.org [1] β icon behavior π
π REPORTING BUGS π
Report bugs to linux-trace-devel@vger.kernel.org [2]. please be nice and specific, bestie π
β LICENSE β
libtracefs is Free Software licensed under the GNU LGPL 2.1. slay and respect the license π
π§ RESOURCES π§
https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/
the source is right there, go off π₯
π COPYING π
Copyright (C) 2023 Google, LLC. Free use of this software is granted under the terms of the GNU Public License (GPL). π
π NOTES π
rostedt@goodmis.orgmailto:rostedt@goodmis.orglinux-trace-devel@vger.kernel.orgmailto:linux-trace-devel@vger.kernel.org
signing off from libtracefs 1.8.4, dated 10/21/2026 β go take that snapshot, bestie π β¨
π¦ where this page comes from
- arch rolling: libtracefs 1.8.4-1 Β· GPL-2.0-only AND LGPL-2.1-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms