---
name: "tracecmd_open_fd"
section: "3"
platform: "linux"
package: "trace-cmd"
version: "3.3.1-1+b1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/tracecmd_open"
---

# tracecmd\_open\_fd(3)

Open and close a trace file.

Part of [tracecmd\_open(3)](/tracecmd_open/3.md).

## 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

This set of APIs can be used to open and close a trace file recorded by **[trace-cmd(1)](/trace-cmd/1.md)** and containing tracing information from ftrace, the official Linux kernel tracer. The opened file is represented by a *tracecmd\_input* 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 *flag* input parameter, which controls how the file will be opened and parsed. The *flag* is a combination of these options:

```
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's home directory.
```

The **tracecmd\_open()** function opens a given trace *file*, parses the metadata headers from the file, allocates and initializes а *tracecmd\_input* 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 *tracecmd\_read\_* APIs.

The **tracecmd\_open\_fd()** function does the same as **tracecmd\_open()**, but works with a file descriptor to a trace file, opened for reading.

The **tracecmd\_open\_head()** function is the same as **tracecmd\_open()**, but does not initialize the handler for reading trace data. It reads and parses the metadata headers only. The **tracecmd\_init\_data()** should be used before using the *tracecmd\_read\_* APIs.

The **tracecmd\_init\_data()** function initializes a *handle*, 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.

The **tracecmd\_close()** function frees a *handle*, pointer to tracecmd\_input structure, previously allocated with **tracecmd\_open()**, **tracecmd\_open\_fd()** or **tracecmd\_open\_head()** APIs.

The **tracecmd\_set\_private()** function allows to add specific *data* to the *handle* that can be retrieved later.

The **tracecmd\_get\_private()** function will retrieve the *data* set by **tracecmd\_set\_private()** for the given *handle*.

## RETURN VALUE

The **tracecmd\_open()**, **tracecmd\_open\_fd()** and **tracecmd\_open\_head()** functions return a pointer to tracecmd\_input structure or NULL in case of an error. The returned structure must be free with **tracecmd\_close()**. Note that if **tracecmd\_open\_fd()** is used to allocate a tracecmd\_input handler, when **tracecmd\_close()** is called to close it, that fd will be closed also.

The **tracecmd\_init\_data()** function returns -1 in case of an error or 0 otherwise.

The **tracecmd\_get\_private()** returns the *data* set by **tracecmd\_set\_private()**.

## EXAMPLE

```
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 <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);
}
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's metadata
and 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
        Header file to include in order to have access to the library APIs.
-ltracecmd
        Linker switch to add when building a program that uses the library.
```

## SEE ALSO

**[libtracefs(3)](/libtracefs/3.md)**, **[libtraceevent(3)](/libtraceevent/3.md)**, **[trace-cmd(1)](/trace-cmd/1.md)** **[trace-cmd.dat(5)](/trace-cmd.dat/5.md)**

## 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

- 1\.

  rostedt@goodmis.org

  mailto:rostedt@goodmis.org

- 2\.

  tz.stoyanov@gmail.com

  mailto:tz.stoyanov@gmail.com

- 3\.

  linux-trace-devel@vger.kernel.org

  mailto:linux-trace-devel@vger.kernel.org
