LIBTRACECMD(3)
π¦ LIBTRACECMD(3) π¦
okay so bestie, welcome to the libtracefs manual page for LIBTRACECMD(3) β this is the one about opening and closing trace files, and I promise it's gonna make sense π
β¨ NAME β¨
tracecmd_open, tracecmd_open_fd, tracecmd_open_head, tracecmd_init_data, tracecmd_close, tracecmd_set_private, tracecmd_get_private β open and close a trace file π
π SYNOPSIS π
#include <trace-cmd.h>
struct tracecmd_input *tracecmd_open(const char *file, int flags);
struct tracecmd_input *tracecmd_open_fd(int fd, int flags);
struct tracecmd_input *tracecmd_open_head(const char *file, int flags);
int tracecmd_init_data(struct tracecmd_input *handle);
void tracecmd_close(struct tracecmd_input *handle);
void tracecmd_set_private(struct tracecmd_input *handle, void *data);
void *tracecmd_get_private(struct tracecmd_input *handle);
π DESCRIPTION π
So this lil family of APIs is literally here to open and close a trace file that was recorded by trace-cmd(1) and that's carrying all the tracing info from ftrace β the official Linux kernel tracer, no cap π
The file you open gets represented by a tracecmd_input structure, and bestie, every other library API that touches that file needs a pointer to that structure. Non-negotiable.
The APIs that open a trace file all take a flags input parameter, which controls how the file gets opened and parsed. That flag is a combination of these options:
TRACECMD_FL_LOAD_NO_PLUGINSβ do not load any plugins, periodt πTRACECMD_FL_LOAD_NO_SYSTEM_PLUGINSβ do not load system wide plugins; load only "local only" plugins from the user's home directory π
tracecmd_open() opens the trace file you give it, parses the metadata headers out of that file, and allocates + initializes a tracecmd_input handler structure representing the file. It also initializes the handler for reading trace data from the file. The handler it hands back is ready to go with the tracecmd_read_ APIs β¨
tracecmd_open_fd() does exactly the same thing as tracecmd_open(), except it works with a file descriptor to a trace file β one that's opened for reading, okay? π«Ά
tracecmd_open_head() is the same as tracecmd_open() too, BUT it does not initialize the handler for reading trace data. It only reads and parses the metadata headers. So you gotta call tracecmd_init_data() before you go anywhere near the tracecmd_read_ APIs, iykyk.
tracecmd_init_data() initializes a handle β one that was allocated with tracecmd_open_head() β for reading trace data from the file associated with it. This API must be called before any of the tracecmd_read_ APIs, no skipping π
tracecmd_close() frees a handle (a pointer to a tracecmd_input structure) that was previously allocated with the tracecmd_open(), tracecmd_open_fd() or tracecmd_open_head() APIs. Cleanup era π
tracecmd_set_private() lets you attach specific data to the handle, so you can retrieve it later. Little secret stash, love that for you π
tracecmd_get_private() retrieves the data that tracecmd_set_private() set, for the given handle. Stash retrieved, iconic π
β RETURN VALUE β
tracecmd_open(), tracecmd_open_fd() and tracecmd_open_head() return a pointer to a tracecmd_input structure, or NULL if something went wrong π The structure that comes back must be freed with tracecmd_close().
Heads up bestie, this one matters: iftracecmd_open_fd()is what allocated thetracecmd_inputhandler, then whentracecmd_close()is called to close it, that fd will be closed also. So don't be out here double-closing it, fr fr π
tracecmd_init_data() returns -1 on error, or 0 otherwise.
tracecmd_get_private() returns the data that was set by tracecmd_set_private().
π₯ EXAMPLE π₯
So there are two different use patterns for opening and reading trace data from a trace file, and you pick whichever fits your use case π¦
- Open and initialise the trace file in Π° single step:
#include <stdlib.h>
#include <trace-cmd.h>
static int print_events(struct tracecmd_input *handle, struct tep_record *record, int cpu, void *data)
{
static struct trace_seq seq;
struct tep_handle *tep = tracecmd_get_tep(handle);
const char *file = tracecmd_get_private(handle);
if (!seq.buffer)
trace_seq_init(&seq);
trace_seq_reset(&seq);
trace_seq_printf(&seq, "%s: ", file);
tep_print_event(tep, &seq, record, "%6.1000d [%03d] %s-%d %s: %s\n",
TEP_PRINT_TIME, TEP_PRINT_CPU, TEP_PRINT_COMM, TEP_PRINT_PID,
TEP_PRINT_NAME, TEP_PRINT_INFO);
trace_seq_terminate(&seq);
trace_seq_do_printf(&seq);
return 0;
}
int main(int argc, char **argv)
{
struct tracecmd_input *handle;
if (argc < 2) {
printf("usage: %s trace.dat\n", argv[0]);
exit(-1);
}
handle = tracecmd_open(argv[i], 0);
if (!handle)
exit(-1);
tracecmd_set_private(handles[nr_handles], argv[i]);
tracecmd_iterate_events(handles, NULL, 0, print_events, NULL);
tracecmd_close(handle);
}
- Open and initialise the trace file in two steps. This lets you do some processing based on metadata read from the file, before initialising the trace data for reading. A classic use case for this is opening multiple trace files that were recorded in a same trace session πΈ In that case the timestamps of all the trace events have to be adjusted based on the information from the file's metadata, and that has to happen before reading the trace data.
#include <trace-cmd.h>
...
struct tracecmd_input *handle = tracecmd_open_head("trace.dat");
if (!handle) {
/* Failed to open trace.dat file */
}
...
/* do some processing, before initialising the trace data for reading */
...
if (tracecmd_init_data(handle) < 0) {
/* Failed to initialize hadle for reading the trace data */
}
...
/* Read tracing data from the file, using the handle */
...
tracecmd_close(handle);
...
π FILES π
trace-cmd.hβ the header file you include to get access to the library APIs π-ltracecmdβ the linker switch you add when building a program that uses the library π
π SEE ALSO π
libtracefs(3), libtraceevent(3), trace-cmd(1), trace-cmd.dat(5)
π AUTHOR π
- Steven Rostedt
<rostedt@goodmis.org[1]> - Tzvetomir Stoyanov
<tz.stoyanov@gmail.com[2]>
π REPORTING BUGS π
Report bugs to <linux-trace-devel@vger.kernel.org[3]> π
π LICENSE π
libtracecmd is Free Software licensed under the GNU LGPL 2.1 β¨
π¦ RESOURCES π¦
https://git.kernel.org/pub/scm/utils/trace-cmd/trace-cmd.git/
π COPYING π
Copyright (C) 2020 VMware, Inc. Free use of this software is granted under the terms of the GNU Public License (GPL).
π NOTES π
rostedt@goodmis.orgmailto:rostedt@goodmis.orgtz.stoyanov@gmail.commailto:tz.stoyanov@gmail.comlinux-trace-devel@vger.kernel.orgmailto:linux-trace-devel@vger.kernel.org
and that's the whole page bestie π
this was the libtracefs manual, dated 03/09/2026, and it's LIBTRACECMD(3) β go open some trace files, you got this π«Άπ
π¦ where this page comes from
- arch rolling: trace-cmd 3.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