'\" t .\" Title: libtracecmd .\" Author: [see the "AUTHOR" section] .\" Generator: DocBook XSL Stylesheets vsnapshot .\" Date: 02/10/2024 .\" Manual: libtracefs Manual .\" Source: libtracefs .\" Language: English .\" .TH "LIBTRACECMD" "3" "02/10/2024" "libtracefs" "libtracefs Manual" .\" ----------------------------------------------------------------- .\" * Define some portability stuff .\" ----------------------------------------------------------------- .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .\" http://bugs.debian.org/507673 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" ----------------------------------------------------------------- .\" * set default formatting .\" ----------------------------------------------------------------- .\" disable hyphenation .nh .\" disable justification (adjust text to left margin only) .ad l .\" ----------------------------------------------------------------- .\" * MAIN CONTENT STARTS HERE * .\" ----------------------------------------------------------------- .SH "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\&. .SH "SYNOPSIS" .sp .nf \fB#include \fR struct tracecmd_input *\fBtracecmd_open\fR(const char *\fIfile\fR, int \fIflags\fR); struct tracecmd_input *\fBtracecmd_open_fd\fR(int \fIfd\fR, int \fIflags\fR); struct tracecmd_input *\fBtracecmd_open_head\fR(const char *\fIfile\fR, int \fIflags\fR); int \fBtracecmd_init_data\fR(struct tracecmd_input *\fIhandle\fR); void \fBtracecmd_close\fR(struct tracecmd_input *\fIhandle\fR); void \fBtracecmd_set_private\fR(struct tracecmd_input *\fIhandle\fR, void *\fIdata\fR); void *\fBtracecmd_get_private\fR(struct tracecmd_input *\fIhandle\fR); .fi .SH "DESCRIPTION" .sp This set of APIs can be used to open and close a trace file recorded by \fBtrace\-cmd(1)\fR and containing tracing information from ftrace, the official Linux kernel tracer\&. The opened file is represented by a \fItracecmd_input\fR structure, all other library APIs that work with the file require a pointer to the structure\&. The APIs for opening a trace file have a \fIflag\fR input parameter, which controls how the file will be opened and parsed\&. The \fIflag\fR is a combination of these options: .sp .if n \{\ .RS 4 .\} .nf TRACECMD_FL_LOAD_NO_PLUGINS \- Do not load any plugins TRACECMD_FL_LOAD_NO_SYSTEM_PLUGINS \- Do not load system wide plugins, load only "local only" plugins from user\*(Aqs home directory\&. .fi .if n \{\ .RE .\} .sp The \fBtracecmd_open()\fR function opens a given trace \fIfile\fR, parses the metadata headers from the file, allocates and initializes а \fItracecmd_input\fR handler structure representing the file\&. It also initializes the handler for reading trace data from the file\&. The returned handler is ready to be used with \fItracecmd_read_\fR APIs\&. .sp The \fBtracecmd_open_fd()\fR function does the same as \fBtracecmd_open()\fR, but works with a file descriptor to a trace file, opened for reading\&. .sp The \fBtracecmd_open_head()\fR function is the same as \fBtracecmd_open()\fR, but does not initialize the handler for reading trace data\&. It reads and parses the metadata headers only\&. The \fBtracecmd_init_data()\fR should be used before using the \fItracecmd_read_\fR APIs\&. .sp The \fBtracecmd_init_data()\fR function initializes a \fIhandle\fR, allocated with \fBtracecmd_open_head()\fR, for reading trace data from the file associated with it\&. This API must be called before any of the \fItracecmd_read_\fR APIs\&. .sp The \fBtracecmd_close()\fR function frees a \fIhandle\fR, pointer to tracecmd_input structure, previously allocated with \fBtracecmd_open()\fR, \fBtracecmd_open_fd()\fR or \fBtracecmd_open_head()\fR APIs\&. .sp The \fBtracecmd_set_private()\fR function allows to add specific \fIdata\fR to the \fIhandle\fR that can be retrieved later\&. .sp The \fBtracecmd_get_private()\fR function will retrieve the \fIdata\fR set by \fBtracecmd_set_private()\fR for the given \fIhandle\fR\&. .SH "RETURN VALUE" .sp The \fBtracecmd_open()\fR, \fBtracecmd_open_fd()\fR and \fBtracecmd_open_head()\fR functions return a pointer to tracecmd_input structure or NULL in case of an error\&. The returned structure must be free with \fBtracecmd_close()\fR\&. Note that if \fBtracecmd_open_fd()\fR is used to allocate a tracecmd_input handler, when \fBtracecmd_close()\fR is called to close it, that fd will be closed also\&. .sp The \fBtracecmd_init_data()\fR function returns \-1 in case of an error or 0 otherwise\&. .sp The \fBtracecmd_get_private()\fR returns the \fIdata\fR set by \fBtracecmd_set_private()\fR\&. .SH "EXAMPLE" .sp .if n \{\ .RS 4 .\} .nf The are two different use patterns for opening and reading trace data from a trace file, which can be used depending on the use case\&. 1\&. Open and initialise the trace file in а single step: #include #include 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\en", 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\en", 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); } 2\&. Open and initialise the trace file in two steps\&. This allows to perform some processing based on metadata, read from the file, before initialising the trace data for reading\&. Example for such use case is when opening multiple trace files recorded in a same trace session\&. In that case timestamps of all trace events must be adjusted based on the information from the file\*(Aqs metadata and before reading the trace data\&. #include \&.\&.\&. 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); \&.\&.\&. .fi .if n \{\ .RE .\} .SH "FILES" .sp .if n \{\ .RS 4 .\} .nf \fBtrace\-cmd\&.h\fR Header file to include in order to have access to the library APIs\&. \fB\-ltracecmd\fR Linker switch to add when building a program that uses the library\&. .fi .if n \{\ .RE .\} .SH "SEE ALSO" .sp \fBlibtracefs(3)\fR, \fBlibtraceevent(3)\fR, \fBtrace\-cmd(1)\fR \fBtrace\-cmd\&.dat(5)\fR .SH "AUTHOR" .sp .if n \{\ .RS 4 .\} .nf \fBSteven Rostedt\fR <\m[blue]\fBrostedt@goodmis\&.org\fR\m[]\&\s-2\u[1]\d\s+2> \fBTzvetomir Stoyanov\fR <\m[blue]\fBtz\&.stoyanov@gmail\&.com\fR\m[]\&\s-2\u[2]\d\s+2> .fi .if n \{\ .RE .\} .SH "REPORTING BUGS" .sp Report bugs to <\m[blue]\fBlinux\-trace\-devel@vger\&.kernel\&.org\fR\m[]\&\s-2\u[3]\d\s+2> .SH "LICENSE" .sp libtracecmd is Free Software licensed under the GNU LGPL 2\&.1 .SH "RESOURCES" .sp \m[blue]\fBhttps://git\&.kernel\&.org/pub/scm/utils/trace\-cmd/trace\-cmd\&.git/\fR\m[] .SH "COPYING" .sp Copyright (C) 2020 VMware, Inc\&. Free use of this software is granted under the terms of the GNU Public License (GPL)\&. .SH "NOTES" .IP " 1." 4 rostedt@goodmis.org .RS 4 \%mailto:rostedt@goodmis.org .RE .IP " 2." 4 tz.stoyanov@gmail.com .RS 4 \%mailto:tz.stoyanov@gmail.com .RE .IP " 3." 4 linux-trace-devel@vger.kernel.org .RS 4 \%mailto:linux-trace-devel@vger.kernel.org .RE