xref: /aosp_15_r20/external/libtraceevent/Documentation/libtraceevent-kbuffer-read.txt (revision 436bf2bcd5202612ffffe471bbcc1f277cc8d28e)
1*436bf2bcSAndroid Build Coastguard Workerlibtraceevent(3)
2*436bf2bcSAndroid Build Coastguard Worker================
3*436bf2bcSAndroid Build Coastguard Worker
4*436bf2bcSAndroid Build Coastguard WorkerNAME
5*436bf2bcSAndroid Build Coastguard Worker----
6*436bf2bcSAndroid Build Coastguard Workerkbuffer_read_event, kbuffer_next_event, kbuffer_missed_events, kbuffer_event_size, kbuffer_curr_size,
7*436bf2bcSAndroid Build Coastguard Workerkbuffer_curr_offset, kbuffer_curr_index, kbuffer_read_buffer -
8*436bf2bcSAndroid Build Coastguard WorkerFunctions to read through the kbuffer sub buffer.
9*436bf2bcSAndroid Build Coastguard Worker
10*436bf2bcSAndroid Build Coastguard WorkerSYNOPSIS
11*436bf2bcSAndroid Build Coastguard Worker--------
12*436bf2bcSAndroid Build Coastguard Worker[verse]
13*436bf2bcSAndroid Build Coastguard Worker--
14*436bf2bcSAndroid Build Coastguard Worker*#include <kbuffer.h>*
15*436bf2bcSAndroid Build Coastguard Worker
16*436bf2bcSAndroid Build Coastguard Workervoid pass:[*]*kbuffer_read_event*(struct kbuffer pass:[*]_kbuf_, unsigned long long pass:[*]_ts_);
17*436bf2bcSAndroid Build Coastguard Workervoid pass:[*]*kbuffer_next_event*(struct kbuffer pass:[*]_kbuf_, unsigned long long pass:[*]_ts_);
18*436bf2bcSAndroid Build Coastguard Workervoid pass:[*]*kbuffer_read_at_offset*(struct kbuffer pass:[*]_kbuf_, int _offset_, unsigned long long pass:[*]_ts_);
19*436bf2bcSAndroid Build Coastguard Workerint *kbuffer_missed_events*(struct kbuffer pass:[*]_kbuf_);
20*436bf2bcSAndroid Build Coastguard Workerint *kbuffer_event_size*(struct kbuffer pass:[*]_kbuf_);
21*436bf2bcSAndroid Build Coastguard Workerint *kbuffer_curr_size*(struct kbuffer pass:[*]_kbuf_);
22*436bf2bcSAndroid Build Coastguard Workerint *kbuffer_curr_offset*(struct kbuffer pass:[*]_kbuf_);
23*436bf2bcSAndroid Build Coastguard Workerint *kbuffer_curr_index*(struct kbuffer pass:[*]_kbuf_);
24*436bf2bcSAndroid Build Coastguard Workerint *kbuffer_read_buffer*(struct kbuffer pass:[*]_kbuf_, void pass:[*]_buffer_, int _len_);
25*436bf2bcSAndroid Build Coastguard Worker--
26*436bf2bcSAndroid Build Coastguard Worker
27*436bf2bcSAndroid Build Coastguard WorkerDESCRIPTION
28*436bf2bcSAndroid Build Coastguard Worker-----------
29*436bf2bcSAndroid Build Coastguard WorkerThe function *kbuffer_read_event()* reads the next event in the _kbuf_ descriptor
30*436bf2bcSAndroid Build Coastguard Workerand if _ts_ is non NULL, will place its timestamp into it. This does not modify the _kbuf_
31*436bf2bcSAndroid Build Coastguard Workerdescriptor, and calling this function mulitple times will return the same result.
32*436bf2bcSAndroid Build Coastguard Worker
33*436bf2bcSAndroid Build Coastguard WorkerThe function *kbuffer_next_event()* will return the next event in the _kbuf_ descriptor.
34*436bf2bcSAndroid Build Coastguard WorkerIt will also set the _ts_ to the timestamp of the returned event. NULL is returned
35*436bf2bcSAndroid Build Coastguard Workerif there are no more events and _ts_ will be undefined. Note, if this is called directly
36*436bf2bcSAndroid Build Coastguard Workerafter a *kbuffer_load_subbuffer()* then it will likely give an unexpected result, as it
37*436bf2bcSAndroid Build Coastguard Workerwill return the second event and not the first event. Usually this function is only used
38*436bf2bcSAndroid Build Coastguard Workerto move to the next event and to know if there's any more events to read, and
39*436bf2bcSAndroid Build Coastguard Worker*kbuffer_read_event()* is always called first.
40*436bf2bcSAndroid Build Coastguard Worker
41*436bf2bcSAndroid Build Coastguard WorkerThe function *kbuffer_read_at_offset()* returns the event located at a given _offset_ from
42*436bf2bcSAndroid Build Coastguard Workerthe beginning of the sub-buffer. This offset can be retrieved by *kbuffer_curr_offset()*.
43*436bf2bcSAndroid Build Coastguard WorkerIf _ts_ points to an unsigned long long, then it will be set to the event at the given
44*436bf2bcSAndroid Build Coastguard Workeroffset's timestamp.
45*436bf2bcSAndroid Build Coastguard Worker
46*436bf2bcSAndroid Build Coastguard WorkerIf the sub-buffer had missed events before it, then *kbuffer_missed_events()* will return
47*436bf2bcSAndroid Build Coastguard Workerthe non zero. If it returns -1, that means there were missed events, but the exact number
48*436bf2bcSAndroid Build Coastguard Workerof missed events is unknown. If it returns a positive number, then the number of missed events
49*436bf2bcSAndroid Build Coastguard Workeris the return value.
50*436bf2bcSAndroid Build Coastguard Worker
51*436bf2bcSAndroid Build Coastguard WorkerThe *kbuffer_event_size()* function returns the size of the data portion of the current event
52*436bf2bcSAndroid Build Coastguard Worker(the one that would be returned by *kbuffer_read_event()*.
53*436bf2bcSAndroid Build Coastguard Worker
54*436bf2bcSAndroid Build Coastguard WorkerThe *kbuffer_curr_size()* function returns the entire record size of the current event
55*436bf2bcSAndroid Build Coastguard Worker(the one that would be returned by *kbuffer_read_event()*. The difference here is that the
56*436bf2bcSAndroid Build Coastguard Workerreturn value includes the size of the event record meta data that is not part of what
57*436bf2bcSAndroid Build Coastguard Workeris returned by *kbuffer_read_event()*.
58*436bf2bcSAndroid Build Coastguard Worker
59*436bf2bcSAndroid Build Coastguard WorkerThe *kbuffer_curr_offset()* function returns the offset from the beginning of the sub-buffer
60*436bf2bcSAndroid Build Coastguard Workerof where the current event's meta data for the record begins. The first event will
61*436bf2bcSAndroid Build Coastguard Workernot be at offset zero. This offset can be used to retrieve the event with
62*436bf2bcSAndroid Build Coastguard Worker*kbuffer_read_at_offset()*.
63*436bf2bcSAndroid Build Coastguard Worker
64*436bf2bcSAndroid Build Coastguard WorkerThe *kbuffer_curr_index()* function returns the index from the beginning of the data
65*436bf2bcSAndroid Build Coastguard Workerportion of the sub-buffer where the current evnet's meta data is located.
66*436bf2bcSAndroid Build Coastguard WorkerThe first event will likely be zero, but may not be if there's a timestamp attached to it.
67*436bf2bcSAndroid Build Coastguard Worker
68*436bf2bcSAndroid Build Coastguard WorkerThe *kbuffer_read_buffer()* function will fill the given _buffer_ from the _kbuf_ the same
69*436bf2bcSAndroid Build Coastguard Workerway the kernel would do a read system call. That is, if the length _len_ is less than the
70*436bf2bcSAndroid Build Coastguard Workersub buffer size, or the kbuffer current index is non-zero, it will start copying from the
71*436bf2bcSAndroid Build Coastguard Worker_kbuf_ current event and create _buffer_ as a new sub buffer (with a timestamp
72*436bf2bcSAndroid Build Coastguard Workerand commit header) with that event that was found and including all events after that can
73*436bf2bcSAndroid Build Coastguard Workerfit within _len_. The _len_ must include the size of the sub buffer header as well as the
74*436bf2bcSAndroid Build Coastguard Workerevents to include. That is, _len_ is the allocate size of _buffer_ that can be filled.
75*436bf2bcSAndroid Build Coastguard WorkerThe return from this function is the index of the end of the last event that was added.
76*436bf2bcSAndroid Build Coastguard WorkerIf there are no more events then zero is returned, and if the buffer can not
77*436bf2bcSAndroid Build Coastguard Workercopy any events because _len_ was too small, then -1 is returned.
78*436bf2bcSAndroid Build Coastguard Worker
79*436bf2bcSAndroid Build Coastguard Worker
80*436bf2bcSAndroid Build Coastguard WorkerRETURN VALUE
81*436bf2bcSAndroid Build Coastguard Worker------------
82*436bf2bcSAndroid Build Coastguard Worker*kbuffer_read_event()* returns the event that the _kbuf_ descriptor is currently at,
83*436bf2bcSAndroid Build Coastguard Workeror NULL if the last event was passed (by *kbuffer_next_event()*).
84*436bf2bcSAndroid Build Coastguard Worker
85*436bf2bcSAndroid Build Coastguard Worker*kbuffer_next_event()* returns the next event after the current event or NULL if there
86*436bf2bcSAndroid Build Coastguard Workerare no more events.
87*436bf2bcSAndroid Build Coastguard Worker
88*436bf2bcSAndroid Build Coastguard Worker*kbuffer_read_at_offset()* returns the event at a given _offset_ from the start of
89*436bf2bcSAndroid Build Coastguard Workerthe sub-buffer stored in _kbuf_, or NULL if there exists no event. Note, _offset_
90*436bf2bcSAndroid Build Coastguard Workeronly needs to be an offset that lands on the record, or is at the start of it. It does
91*436bf2bcSAndroid Build Coastguard Workernot need to be exactly at the beginning of the record.
92*436bf2bcSAndroid Build Coastguard Worker
93*436bf2bcSAndroid Build Coastguard Worker*kbuffer_missed_events()* returns 0 if there were no missed events before loaded sub-buffer.
94*436bf2bcSAndroid Build Coastguard WorkerReturns -1 if there were an unknown number of missed events, or if the number of missed events
95*436bf2bcSAndroid Build Coastguard Workeris known, that number will be returned.
96*436bf2bcSAndroid Build Coastguard Worker
97*436bf2bcSAndroid Build Coastguard Worker*kbuffer_event_size()* returns the size of the data payload of the current event of _kbuf_.
98*436bf2bcSAndroid Build Coastguard Worker
99*436bf2bcSAndroid Build Coastguard Worker*kbuffer_curr_size()* returns the size of the entire record of the current event of _kbuf_.
100*436bf2bcSAndroid Build Coastguard WorkerThis includes the size of the meta data for that record.
101*436bf2bcSAndroid Build Coastguard Worker
102*436bf2bcSAndroid Build Coastguard Worker*kbuf_curr_offset()* returns the offset of the current record from the beginning of the _kbuf_
103*436bf2bcSAndroid Build Coastguard Workersub-buffer.
104*436bf2bcSAndroid Build Coastguard Worker
105*436bf2bcSAndroid Build Coastguard Worker*kbuf_curr_index()* returns the index of the current record from the beginning of the _kbuf_
106*436bf2bcSAndroid Build Coastguard Workerdata section.
107*436bf2bcSAndroid Build Coastguard Worker
108*436bf2bcSAndroid Build Coastguard Worker*kbuf_read_buffer()* returns the index of the end of the last event that was filled in
109*436bf2bcSAndroid Build Coastguard Worker_buffer_. If there are no more events to copy from _start_ then 0 is returned. If _len_
110*436bf2bcSAndroid Build Coastguard Workeris not big enough to hold any events, then -1 is returned.
111*436bf2bcSAndroid Build Coastguard Worker
112*436bf2bcSAndroid Build Coastguard WorkerEXAMPLE
113*436bf2bcSAndroid Build Coastguard Worker-------
114*436bf2bcSAndroid Build Coastguard Worker[source,c]
115*436bf2bcSAndroid Build Coastguard Worker--
116*436bf2bcSAndroid Build Coastguard Worker#include <stdio.h>
117*436bf2bcSAndroid Build Coastguard Worker#include <stdlib.h>
118*436bf2bcSAndroid Build Coastguard Worker#include <fcntl.h>
119*436bf2bcSAndroid Build Coastguard Worker#include <unistd.h>
120*436bf2bcSAndroid Build Coastguard Worker#include <sys/stat.h>
121*436bf2bcSAndroid Build Coastguard Worker
122*436bf2bcSAndroid Build Coastguard Worker#include <kbuffer.h>
123*436bf2bcSAndroid Build Coastguard Worker
124*436bf2bcSAndroid Build Coastguard Workerint main (int argc, char **argv)
125*436bf2bcSAndroid Build Coastguard Worker{
126*436bf2bcSAndroid Build Coastguard Worker	unsigned long long ts;
127*436bf2bcSAndroid Build Coastguard Worker	struct kbuffer *kbuf;
128*436bf2bcSAndroid Build Coastguard Worker	struct stat st;
129*436bf2bcSAndroid Build Coastguard Worker	char *buf;
130*436bf2bcSAndroid Build Coastguard Worker	void *event;
131*436bf2bcSAndroid Build Coastguard Worker	int save_offset = -1;
132*436bf2bcSAndroid Build Coastguard Worker	int record_size;
133*436bf2bcSAndroid Build Coastguard Worker	int offset;
134*436bf2bcSAndroid Build Coastguard Worker	int index;
135*436bf2bcSAndroid Build Coastguard Worker	int size;
136*436bf2bcSAndroid Build Coastguard Worker	int ret;
137*436bf2bcSAndroid Build Coastguard Worker	int fd;
138*436bf2bcSAndroid Build Coastguard Worker	int i = 0;
139*436bf2bcSAndroid Build Coastguard Worker
140*436bf2bcSAndroid Build Coastguard Worker	if (argc < 2) {
141*436bf2bcSAndroid Build Coastguard Worker		printf("usage: %s raw-subbuffer-page\n", argv[0]);
142*436bf2bcSAndroid Build Coastguard Worker		printf(" Try: dd count=1 bs=4096 if=/sys/kernel/tracing/per_cpu/cpu0/trace_pipe_raw of=/tmp/file\n");
143*436bf2bcSAndroid Build Coastguard Worker		exit(0);
144*436bf2bcSAndroid Build Coastguard Worker	}
145*436bf2bcSAndroid Build Coastguard Worker
146*436bf2bcSAndroid Build Coastguard Worker	if (stat(argv[1], &st) < 0) {
147*436bf2bcSAndroid Build Coastguard Worker		perror("stat");
148*436bf2bcSAndroid Build Coastguard Worker		exit(-1);
149*436bf2bcSAndroid Build Coastguard Worker	}
150*436bf2bcSAndroid Build Coastguard Worker
151*436bf2bcSAndroid Build Coastguard Worker	buf = malloc(st.st_size);
152*436bf2bcSAndroid Build Coastguard Worker	if (!buf) {
153*436bf2bcSAndroid Build Coastguard Worker		perror("Allocating buffer");
154*436bf2bcSAndroid Build Coastguard Worker		exit(-1);
155*436bf2bcSAndroid Build Coastguard Worker	}
156*436bf2bcSAndroid Build Coastguard Worker
157*436bf2bcSAndroid Build Coastguard Worker	fd = open(argv[1], O_RDONLY);
158*436bf2bcSAndroid Build Coastguard Worker	if (fd < 0) {
159*436bf2bcSAndroid Build Coastguard Worker		perror(argv[1]);
160*436bf2bcSAndroid Build Coastguard Worker		exit(-1);
161*436bf2bcSAndroid Build Coastguard Worker	}
162*436bf2bcSAndroid Build Coastguard Worker
163*436bf2bcSAndroid Build Coastguard Worker	ret = read(fd, buf, st.st_size);
164*436bf2bcSAndroid Build Coastguard Worker	if (ret < 0) {
165*436bf2bcSAndroid Build Coastguard Worker		perror("Reading buffer");
166*436bf2bcSAndroid Build Coastguard Worker		exit(-1);
167*436bf2bcSAndroid Build Coastguard Worker	}
168*436bf2bcSAndroid Build Coastguard Worker	close(fd);
169*436bf2bcSAndroid Build Coastguard Worker
170*436bf2bcSAndroid Build Coastguard Worker	kbuf = kbuffer_alloc(KBUFFER_ENDIAN_SAME_AS_HOST,
171*436bf2bcSAndroid Build Coastguard Worker			     KBUFFER_LSIZE_SAME_AS_HOST);
172*436bf2bcSAndroid Build Coastguard Worker	if (!kbuf) {
173*436bf2bcSAndroid Build Coastguard Worker		perror("Creating kbuffer");
174*436bf2bcSAndroid Build Coastguard Worker		exit(-1);
175*436bf2bcSAndroid Build Coastguard Worker	}
176*436bf2bcSAndroid Build Coastguard Worker	ret = kbuffer_load_subbuffer(kbuf, buf);
177*436bf2bcSAndroid Build Coastguard Worker	if (ret < 0) {
178*436bf2bcSAndroid Build Coastguard Worker		perror("Loading sub bufer");
179*436bf2bcSAndroid Build Coastguard Worker		exit(-1);
180*436bf2bcSAndroid Build Coastguard Worker	}
181*436bf2bcSAndroid Build Coastguard Worker
182*436bf2bcSAndroid Build Coastguard Worker	if (kbuffer_subbuffer_size(kbuf) > st.st_size) {
183*436bf2bcSAndroid Build Coastguard Worker		fprintf(stderr, "kbuffer is bigger than raw size %d > %ld\n",
184*436bf2bcSAndroid Build Coastguard Worker			kbuffer_subbuffer_size(kbuf), st.st_size);
185*436bf2bcSAndroid Build Coastguard Worker		exit(-1);
186*436bf2bcSAndroid Build Coastguard Worker	}
187*436bf2bcSAndroid Build Coastguard Worker
188*436bf2bcSAndroid Build Coastguard Worker	ret = kbuffer_missed_events(kbuf);
189*436bf2bcSAndroid Build Coastguard Worker	if (ret) {
190*436bf2bcSAndroid Build Coastguard Worker		if (ret > 0)
191*436bf2bcSAndroid Build Coastguard Worker			printf("Missed %d events before this buffer\n", ret);
192*436bf2bcSAndroid Build Coastguard Worker		else
193*436bf2bcSAndroid Build Coastguard Worker			printf("Missed unknown number of events before this buffer\n");
194*436bf2bcSAndroid Build Coastguard Worker	}
195*436bf2bcSAndroid Build Coastguard Worker	do {
196*436bf2bcSAndroid Build Coastguard Worker		event = kbuffer_read_event(kbuf, &ts);
197*436bf2bcSAndroid Build Coastguard Worker		if (event) {
198*436bf2bcSAndroid Build Coastguard Worker			record_size = kbuffer_curr_size(kbuf);
199*436bf2bcSAndroid Build Coastguard Worker			offset = kbuffer_curr_offset(kbuf);
200*436bf2bcSAndroid Build Coastguard Worker			index = kbuffer_curr_index(kbuf);
201*436bf2bcSAndroid Build Coastguard Worker			size = kbuffer_event_size(kbuf);
202*436bf2bcSAndroid Build Coastguard Worker
203*436bf2bcSAndroid Build Coastguard Worker			if (i == 20)
204*436bf2bcSAndroid Build Coastguard Worker				save_offset = offset;
205*436bf2bcSAndroid Build Coastguard Worker			printf(" event %3d ts:%lld\trecord_size:%d size:%d\tindex:%d offset:%d\n",
206*436bf2bcSAndroid Build Coastguard Worker			       i++, ts, record_size, size, index, offset);
207*436bf2bcSAndroid Build Coastguard Worker			event = kbuffer_next_event(kbuf, NULL);
208*436bf2bcSAndroid Build Coastguard Worker		}
209*436bf2bcSAndroid Build Coastguard Worker	} while (event);
210*436bf2bcSAndroid Build Coastguard Worker
211*436bf2bcSAndroid Build Coastguard Worker	if (!event)
212*436bf2bcSAndroid Build Coastguard Worker		printf("Finished sub buffer\n");
213*436bf2bcSAndroid Build Coastguard Worker
214*436bf2bcSAndroid Build Coastguard Worker	if (save_offset > 0) {
215*436bf2bcSAndroid Build Coastguard Worker		event = kbuffer_read_at_offset(kbuf, save_offset, &ts);
216*436bf2bcSAndroid Build Coastguard Worker		if (!event) {
217*436bf2bcSAndroid Build Coastguard Worker			fprintf(stderr, "Funny, can't find event 20 at offset %d\n", save_offset);
218*436bf2bcSAndroid Build Coastguard Worker			exit(-1);
219*436bf2bcSAndroid Build Coastguard Worker		}
220*436bf2bcSAndroid Build Coastguard Worker		record_size = kbuffer_curr_size(kbuf);
221*436bf2bcSAndroid Build Coastguard Worker		offset = kbuffer_curr_offset(kbuf);
222*436bf2bcSAndroid Build Coastguard Worker		index = kbuffer_curr_index(kbuf);
223*436bf2bcSAndroid Build Coastguard Worker		size = kbuffer_event_size(kbuf);
224*436bf2bcSAndroid Build Coastguard Worker
225*436bf2bcSAndroid Build Coastguard Worker		printf("\n saved event 20 ts:%lld\trecord_size:%d size:%d\tindex:%d offset:%d\n\n",
226*436bf2bcSAndroid Build Coastguard Worker		       ts, record_size, size, index, offset);
227*436bf2bcSAndroid Build Coastguard Worker	}
228*436bf2bcSAndroid Build Coastguard Worker	kbuffer_free(kbuf);
229*436bf2bcSAndroid Build Coastguard Worker
230*436bf2bcSAndroid Build Coastguard Worker	return 0;
231*436bf2bcSAndroid Build Coastguard Worker}
232*436bf2bcSAndroid Build Coastguard Worker--
233*436bf2bcSAndroid Build Coastguard WorkerFILES
234*436bf2bcSAndroid Build Coastguard Worker-----
235*436bf2bcSAndroid Build Coastguard Worker[verse]
236*436bf2bcSAndroid Build Coastguard Worker--
237*436bf2bcSAndroid Build Coastguard Worker*event-parse.h*
238*436bf2bcSAndroid Build Coastguard Worker	Header file to include in order to have access to the library APIs.
239*436bf2bcSAndroid Build Coastguard Worker*-ltraceevent*
240*436bf2bcSAndroid Build Coastguard Worker	Linker switch to add when building a program that uses the library.
241*436bf2bcSAndroid Build Coastguard Worker--
242*436bf2bcSAndroid Build Coastguard Worker
243*436bf2bcSAndroid Build Coastguard WorkerSEE ALSO
244*436bf2bcSAndroid Build Coastguard Worker--------
245*436bf2bcSAndroid Build Coastguard Worker*libtraceevent*(3), *trace-cmd*(1)
246*436bf2bcSAndroid Build Coastguard Worker
247*436bf2bcSAndroid Build Coastguard WorkerAUTHOR
248*436bf2bcSAndroid Build Coastguard Worker------
249*436bf2bcSAndroid Build Coastguard Worker[verse]
250*436bf2bcSAndroid Build Coastguard Worker--
251*436bf2bcSAndroid Build Coastguard Worker*Steven Rostedt* <[email protected]>, author of *libtraceevent*.
252*436bf2bcSAndroid Build Coastguard Worker--
253*436bf2bcSAndroid Build Coastguard WorkerREPORTING BUGS
254*436bf2bcSAndroid Build Coastguard Worker--------------
255*436bf2bcSAndroid Build Coastguard WorkerReport bugs to  <[email protected]>
256*436bf2bcSAndroid Build Coastguard Worker
257*436bf2bcSAndroid Build Coastguard WorkerLICENSE
258*436bf2bcSAndroid Build Coastguard Worker-------
259*436bf2bcSAndroid Build Coastguard Workerlibtraceevent is Free Software licensed under the GNU LGPL 2.1
260*436bf2bcSAndroid Build Coastguard Worker
261*436bf2bcSAndroid Build Coastguard WorkerRESOURCES
262*436bf2bcSAndroid Build Coastguard Worker---------
263*436bf2bcSAndroid Build Coastguard Workerhttps://git.kernel.org/pub/scm/libs/libtrace/libtraceevent.git/
264