1LIBTRACEEVENT(3)             libtraceevent Manual             LIBTRACEEVENT(3)
2
3
4

NAME

6       kbuffer_timestamp, kbuffer_subbuf_timestamp - Functions that read
7       various data of a kbuffer descriptor
8

SYNOPSIS

10       #include <kbuffer.h>
11
12       unsigned long long kbuffer_timestamp(struct kbuffer *kbuf);
13       unsigned long long kbuffer_subbuf_timestamp(struct kbuffer *kbuf, void *subbuf);
14

DESCRIPTION

16       The function kbuffer_timestamp() returns the timestamp of the current
17       event of kbuf.
18
19       The function kbuffer_subbuf_timestamp() returns the timestamp for the
20       sub-buffer that was loaded in kbuf. This usually is (but not guaranteed
21       to be) the timestamp of the first event on the sub-buffer.
22
23       The function kbuffer_start_of_data() returns the offset of where the
24       delta
25

RETURN VALUE

27       kbuffer_read_event() returns the event that the kbuf descriptor is
28       currently at, or NULL if the last event was passed (by
29       kbuffer_next_event()).
30
31       kbuffer_next_event() returns the next event after the current event or
32       NULL if there are no more events.
33
34       kbuffer_read_at_offset() returns the event at a given offset from the
35       start of the sub-buffer stored in kbuf, or NULL if there exists no
36       event. Note, offset only needs to be an offset that lands on the
37       record, or is at the start of it. It does not need to be exactly at the
38       beginning of the record.
39
40       kbuffer_missed_events() returns 0 if there were no missed events before
41       loaded sub-buffer. Returns -1 if there were an unknown number of missed
42       events, or if the number of missed events is known, that number will be
43       returned.
44
45       kbuffer_event_size() returns the size of the data payload of the
46       current event of kbuf.
47
48       kbuffer_curr_size() returns the size of the entire record of the
49       current event of kbuf. This includes the size of the meta data for that
50       record.
51
52       kbuf_curr_offset() returns the offset of the current record from the
53       beginning of the kbuf sub-buffer.
54
55       kbuf_curr_index() returns the index of the current record from the
56       beginning of the kbuf data section.
57

EXAMPLE

59           #include <stdio.h>
60           #include <stdlib.h>
61           #include <fcntl.h>
62           #include <unistd.h>
63           #include <sys/stat.h>
64
65           #include <kbuffer.h>
66
67           int main (int argc, char **argv)
68           {
69                   unsigned long long ts;
70                   struct kbuffer *kbuf;
71                   struct stat st;
72                   char *buf;
73                   void *event;
74                   int save_offset = -1;
75                   int record_size;
76                   int offset;
77                   int index;
78                   int size;
79                   int ret;
80                   int fd;
81                   int i = 0;
82
83                   if (argc < 2) {
84                           printf("usage: %s raw-subbuffer-page\n", argv[0]);
85                           printf(" Try: dd count=1 bs=4096 if=/sys/kernel/tracing/per_cpu/cpu0/trace_pipe_raw of=/tmp/file\n");
86                           exit(0);
87                   }
88
89                   if (stat(argv[1], &st) < 0) {
90                           perror("stat");
91                           exit(-1);
92                   }
93
94                   buf = malloc(st.st_size);
95                   if (!buf) {
96                           perror("Allocating buffer");
97                           exit(-1);
98                   }
99
100                   fd = open(argv[1], O_RDONLY);
101                   if (fd < 0) {
102                           perror(argv[1]);
103                           exit(-1);
104                   }
105
106                   ret = read(fd, buf, st.st_size);
107                   if (ret < 0) {
108                           perror("Reading buffer");
109                           exit(-1);
110                   }
111                   close(fd);
112
113                   kbuf = kbuffer_alloc(KBUFFER_ENDIAN_SAME_AS_HOST,
114                                        KBUFFER_LSIZE_SAME_AS_HOST);
115                   if (!kbuf) {
116                           perror("Creating kbuffer");
117                           exit(-1);
118                   }
119                   ret = kbuffer_load_subbuffer(kbuf, buf);
120                   if (ret < 0) {
121                           perror("Loading sub bufer");
122                           exit(-1);
123                   }
124
125                   if (kbuffer_subbuffer_size(kbuf) > st.st_size) {
126                           fprintf(stderr, "kbuffer is bigger than raw size %d > %ld\n",
127                                   kbuffer_subbuffer_size(kbuf), st.st_size);
128                           exit(-1);
129                   }
130
131                   ret = kbuffer_missed_events(kbuf);
132                   if (ret) {
133                           if (ret > 0)
134                                   printf("Missed %d events before this buffer\n", ret);
135                           else
136                                   printf("Missed unknown number of events before this buffer\n");
137                   }
138                   do {
139                           event = kbuffer_read_event(kbuf, &ts);
140                           if (event) {
141                                   record_size = kbuffer_curr_size(kbuf);
142                                   offset = kbuffer_curr_offset(kbuf);
143                                   index = kbuffer_curr_index(kbuf);
144                                   size = kbuffer_event_size(kbuf);
145
146                                   if (i == 20)
147                                           save_offset = offset;
148                                   printf(" event %3d ts:%lld\trecord_size:%d size:%d\tindex:%d offset:%d\n",
149                                          i++, ts, record_size, size, index, offset);
150                                   event = kbuffer_next_event(kbuf, NULL);
151                           }
152                   } while (event);
153
154                   if (!event)
155                           printf("Finished sub buffer\n");
156
157                   if (save_offset > 0) {
158                           event = kbuffer_read_at_offset(kbuf, save_offset, &ts);
159                           if (!event) {
160                                   fprintf(stderr, "Funny, can't find event 20 at offset %d\n", save_offset);
161                                   exit(-1);
162                           }
163                           record_size = kbuffer_curr_size(kbuf);
164                           offset = kbuffer_curr_offset(kbuf);
165                           index = kbuffer_curr_index(kbuf);
166                           size = kbuffer_event_size(kbuf);
167
168                           printf("\n saved event 20 ts:%lld\trecord_size:%d size:%d\tindex:%d offset:%d\n\n",
169                                  ts, record_size, size, index, offset);
170                   }
171                   kbuffer_free(kbuf);
172
173                   return 0;
174           }
175

FILES

177           event-parse.h
178                   Header file to include in order to have access to the library APIs.
179           -ltraceevent
180                   Linker switch to add when building a program that uses the library.
181

SEE ALSO

183       libtraceevent(3), trace-cmd(1)
184

AUTHOR

186           Steven Rostedt <rostedt@goodmis.org[1]>, author of libtraceevent.
187

REPORTING BUGS

189       Report bugs to <linux-trace-devel@vger.kernel.org[2]>
190

LICENSE

192       libtraceevent is Free Software licensed under the GNU LGPL 2.1
193

RESOURCES

195       https://git.kernel.org/pub/scm/libs/libtrace/libtraceevent.git/
196

NOTES

198        1. rostedt@goodmis.org
199           mailto:rostedt@goodmis.org
200
201        2. linux-trace-devel@vger.kernel.org
202           mailto:linux-trace-devel@vger.kernel.org
203
204
205
206libtraceevent 1.7.2               04/05/2023                  LIBTRACEEVENT(3)
Impressum