1LIBTRACECMD(3) libtracefs Manual LIBTRACECMD(3)
2
3
4
6 tracecmd_get_first_ts, tracecmd_add_ts_offset - Handle time stamps from
7 a trace file.
8
10 #include <trace-cmd.h>
11
12 unsigned long long tracecmd_get_first_ts(struct tracecmd_input *handle);
13 void tracecmd_add_ts_offset(struct tracecmd_input *handle, long long offset);
14
16 This set of APIs can be used to read tracing data from a trace file
17 opened with tracecmd_open()(3), tracecmd_open_fd()(3) or
18 tracecmd_open_head()(3).
19
20 The tracecmd_get_first_ts() function returns the time stamp of the
21 first record in the handle.
22
23 The tracecmd_add_ts_offset() function adds an offset to each of the
24 records in the handle that represents a trace file. This is useful for
25 associating two different tracing files by their offset (for example a
26 trace file from a host and a trace file from a guest that were not
27 synchronized when created).
28
30 The tracecmd_get_first_ts() function returns the timestamp of the first
31 record in a trace file for the given handle.
32
34 #include <stdlib.h>
35 #include <trace-cmd.h>
36
37 static int print_events(struct tracecmd_input *handle, struct tep_record *record, int cpu, void *data)
38 {
39 static struct trace_seq seq;
40 struct tep_handle *tep = tracecmd_get_tep(handle);
41 const char *file = tracecmd_get_private(handle);
42
43 if (!seq.buffer)
44 trace_seq_init(&seq);
45
46 trace_seq_reset(&seq);
47 trace_seq_printf(&seq, "%s: ", file);
48 tep_print_event(tep, &seq, record, "%6.1000d [%03d] %s-%d %s: %s\n",
49 TEP_PRINT_TIME, TEP_PRINT_CPU, TEP_PRINT_COMM, TEP_PRINT_PID,
50 TEP_PRINT_NAME, TEP_PRINT_INFO);
51 trace_seq_terminate(&seq);
52 trace_seq_do_printf(&seq);
53 return 0;
54 }
55
56 int main(int argc, char **argv)
57 {
58 struct tracecmd_input **handles = NULL;
59 unsigned long long ts, first_ts = 0;
60 int nr_handles = 0;
61 int i;
62
63 if (argc < 2) {
64 printf("usage: %s trace.dat [trace.dat ...]\n", argv[0]);
65 exit(-1);
66 }
67
68 for (i = 1; i < argc; i++) {
69 handles = realloc(handles, sizeof(*handles) * (nr_handles + 1));
70 if (!handles)
71 exit(-1);
72 handles[nr_handles] = tracecmd_open(argv[i], 0);
73 if (!handles[nr_handles])
74 exit(-1);
75 tracecmd_set_private(handles[nr_handles], argv[i]);
76 ts = tracecmd_get_first_ts(handles[nr_handles]);
77 if (!first_ts || ts < first_ts)
78 first_ts = ts;
79 nr_handles++;
80 }
81
82 /* Set the time stamp to start at the first record found */
83 for (i = 0; i < nr_handles; i++)
84 tracecmd_add_ts_offset(handles[i], -first_ts);
85
86 tracecmd_iterate_events_multi(handles, nr_handles, print_events, NULL);
87
88 for (i = 0; i < nr_handles; i++)
89 tracecmd_close(handles[i]);
90 free(handles);
91 }
92
94 trace-cmd.h
95 Header file to include in order to have access to the library APIs.
96 -ltracecmd
97 Linker switch to add when building a program that uses the library.
98
100 libtracefs(3), libtraceevent(3), trace-cmd(1) trace-cmd.dat(5)
101
103 Steven Rostedt <rostedt@goodmis.org[1]>
104 Tzvetomir Stoyanov <tz.stoyanov@gmail.com[2]>
105
107 Report bugs to <linux-trace-devel@vger.kernel.org[3]>
108
110 libtracecmd is Free Software licensed under the GNU LGPL 2.1
111
113 https://git.kernel.org/pub/scm/utils/trace-cmd/trace-cmd.git/
114
116 Copyright (C) 2020 VMware, Inc. Free use of this software is granted
117 under the terms of the GNU Public License (GPL).
118
120 1. rostedt@goodmis.org
121 mailto:rostedt@goodmis.org
122
123 2. tz.stoyanov@gmail.com
124 mailto:tz.stoyanov@gmail.com
125
126 3. linux-trace-devel@vger.kernel.org
127 mailto:linux-trace-devel@vger.kernel.org
128
129
130
131libtracefs 07/20/2023 LIBTRACECMD(3)