LIBTRACEEVENT(3)
π¦ NAME π¦
okay bestie, welcome to the manual page for libtraceevent β and this specific page is the kbuffer sub-buffer read squad π
fr fr, these are the functions that read through the kbuffer sub buffer:
kbuffer_read_eventkbuffer_next_eventkbuffer_missed_eventskbuffer_event_sizekbuffer_curr_sizekbuffer_curr_offsetkbuffer_curr_indexkbuffer_read_buffer
no cap, that's the lineup π
β¨ SYNOPSIS β¨
#include <kbuffer.h>
void *kbuffer_read_event(struct kbuffer *kbuf, unsigned long long *ts);
void *kbuffer_next_event(struct kbuffer *kbuf, unsigned long long *ts);
void *kbuffer_read_at_offset(struct kbuffer *kbuf, int offset, unsigned long long *ts);
int kbuffer_missed_events(struct kbuffer *kbuf);
int kbuffer_event_size(struct kbuffer *kbuf);
int kbuffer_curr_size(struct kbuffer *kbuf);
int kbuffer_curr_offset(struct kbuffer *kbuf);
int kbuffer_curr_index(struct kbuffer *kbuf);
int kbuffer_read_buffer(struct kbuffer *kbuf, void *buffer, int len);
πΈ DESCRIPTION πΈ
okay so here's the tea on each one π«
kbuffer_read_event() reads the next event in the kbuf descriptor, and if ts is non NULL, it'll drop its timestamp in there for you. The important part: this does not modify the kbuf descriptor, so if you call this function multiple times you'll get the same result every time. iconic, honestly π
kbuffer_next_event() returns the next event in the kbuf descriptor. It also sets ts to the timestamp of the returned event. NULL is returned if there are no more events, and in that case ts will be undefined π.
β οΈ heads up bestie, this is the trap: if you call this directly after akbuffer_load_subbuffer(), it will likely give an unexpected result, because it will return the second event and not the first one π. usually this function is only used to move on to the next event and to check whether there's any more events left to read β andkbuffer_read_event()is always called first. always. periodt.
kbuffer_read_at_offset() returns the event living at a given offset from the beginning of the sub-buffer π
. You can get that offset from kbuffer_curr_offset(). If ts points to an unsigned long long, then it gets set to the timestamp of the event at that given offset. cute right?
so about missed events π β if the sub-buffer had missed events before it, then kbuffer_missed_events() returns non zero. If it returns -1, that means there were missed events but the exact number is unknown π. If it returns a positive number, then that return value is the number of missed events. slay, data.
kbuffer_event_size() returns the size of the data portion of the current event (the one that kbuffer_read_event() would hand you).
kbuffer_curr_size() returns the entire record size of the current event (again, the one kbuffer_read_event() would return). The difference between these two? The return value here includes the size of the event record meta data, which is not part of what kbuffer_read_event() gives you. the way that meta data is just there being counted... π
kbuffer_curr_offset() returns the offset from the beginning of the sub-buffer of where the current event's meta data for the record begins. Note: the first event will not be at offset zero π. You can use this offset to grab the event with kbuffer_read_at_offset().
kbuffer_curr_index() returns the index from the beginning of the data portion of the sub-buffer where the current event's meta data is located. The first event will likely be zero, but may not be if there's a timestamp attached to it. she contains multitudes π
kbuffer_read_buffer() fills the given buffer from the kbuf the same way the kernel would do a read system call. Like, if the length len is less than the sub buffer size, or the kbuffer current index is non-zero, it will start copying from the kbuf current event and create buffer as a new sub buffer (with a timestamp and commit header) with that event that was found, including all events after it that can fit within len π.
The len must include the size of the sub buffer header as well as the events to include β like, len is the allocated size of buffer that can be filled. The return from this function is the index of the end of the last event that was added. If there are no more events, then zero is returned, and if the buffer can not copy any events because len was too small, then -1 is returned π«Ά
π RETURN VALUE π
kbuffer_read_event()β returns the event that thekbufdescriptor is currently at, orNULLif the last event was passed (bykbuffer_next_event()).kbuffer_next_event()β returns the next event after the current event, orNULLif there are no more events.kbuffer_read_at_offset()β returns the event at a given offset from the start of the sub-buffer stored inkbuf, orNULLif there exists no event. Note:offsetonly needs to be an offset that lands on the record, or is at the start of it. It does not need to be exactly at the beginning of the record πkbuffer_missed_events()β returns0if there were no missed events before the loaded sub-buffer. Returns-1if there were an unknown number of missed events, or β if the number of missed events is known β that number will be returned.kbuffer_event_size()β returns the size of the data payload of the current event ofkbuf.kbuffer_curr_size()β returns the size of the entire record of the current event ofkbuf. This includes the size of the meta data for that record.kbuf_curr_offset()β returns the offset of the current record from the beginning of thekbufsub-buffer.kbuf_curr_index()β returns the index of the current record from the beginning of thekbufdata section.kbuf_read_buffer()β returns the index of the end of the last event that was filled inbuffer. If there are no more events to copy from start then0is returned. Iflenis not big enough to hold any events, then-1is returned.
π EXAMPLE π
here's a whole snack of a demo, bestie β eat it up π
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <kbuffer.h>
int main (int argc, char **argv)
{
unsigned long long ts;
struct kbuffer *kbuf;
struct stat st;
char *buf;
void *event;
int save_offset = -1;
int record_size;
int offset;
int index;
int size;
int ret;
int fd;
int i = 0;
if (argc < 2) {
printf("usage: %s raw-subbuffer-page\n", argv[0]);
printf(" Try: dd count=1 bs=4096 if=/sys/kernel/tracing/per_cpu/cpu0/trace_pipe_raw of=/tmp/file\n");
exit(0);
}
if (stat(argv[1], &st) < 0) {
perror("stat");
exit(-1);
}
buf = malloc(st.st_size);
if (!buf) {
perror("Allocating buffer");
exit(-1);
}
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror(argv[1]);
exit(-1);
}
ret = read(fd, buf, st.st_size);
if (ret < 0) {
perror("Reading buffer");
exit(-1);
}
close(fd);
kbuf = kbuffer_alloc(KBUFFER_ENDIAN_SAME_AS_HOST,
KBUFFER_LSIZE_SAME_AS_HOST);
if (!kbuf) {
perror("Creating kbuffer");
exit(-1);
}
ret = kbuffer_load_subbuffer(kbuf, buf);
if (ret < 0) {
perror("Loading sub bufer");
exit(-1);
}
if (kbuffer_subbuffer_size(kbuf) > st.st_size) {
fprintf(stderr, "kbuffer is bigger than raw size %d > %ld\n",
kbuffer_subbuffer_size(kbuf), st.st_size);
exit(-1);
}
ret = kbuffer_missed_events(kbuf);
if (ret) {
if (ret > 0)
printf("Missed %d events before this buffer\n", ret);
else
printf("Missed unknown number of events before this buffer\n");
}
do {
event = kbuffer_read_event(kbuf, &ts);
if (event) {
record_size = kbuffer_curr_size(kbuf);
offset = kbuffer_curr_offset(kbuf);
index = kbuffer_curr_index(kbuf);
size = kbuffer_event_size(kbuf);
if (i == 20)
save_offset = offset;
printf(" event %3d ts:%lld\trecord_size:%d size:%d\tindex:%d offset:%d\n",
i++, ts, record_size, size, index, offset);
event = kbuffer_next_event(kbuf, NULL);
}
} while (event);
if (!event)
printf("Finished sub buffer\n");
if (save_offset > 0) {
event = kbuffer_read_at_offset(kbuf, save_offset, &ts);
if (!event) {
fprintf(stderr, "Funny, can't find event 20 at offset %d\n", save_offset);
exit(-1);
}
record_size = kbuffer_curr_size(kbuf);
offset = kbuffer_curr_offset(kbuf);
index = kbuffer_curr_index(kbuf);
size = kbuffer_event_size(kbuf);
printf("\n saved event 20 ts:%lld\trecord_size:%d size:%d\tindex:%d offset:%d\n\n",
ts, record_size, size, index, offset);
}
kbuffer_free(kbuf);
return 0;
}
π FILES π
event-parse.hβ header file to include so you have access to the library APIs π-ltraceeventβ the linker switch you add when building a program that uses the library β¨
π SEE ALSO π
libtraceevent(3), trace-cmd(1)
π AUTHOR π
Steven Rostedt <rostedt@goodmis.org[1]>, author of libtraceevent. king behaviour, honestly π«Ά
π REPORTING BUGS π
Report bugs to <linux-trace-devel@vger.kernel.org[2]> β they wanna hear it bestie, fr fr π
β LICENSE β
libtraceevent is Free Software licensed under the GNU LGPL 2.1 π
π§ RESOURCES π§
https://git.kernel.org/pub/scm/libs/libtrace/libtraceevent.git/
πΈ NOTES πΈ
rostedt@goodmis.orgβmailto:rostedt@goodmis.orglinux-trace-devel@vger.kernel.orgβmailto:linux-trace-devel@vger.kernel.org
and that's the whole page, bestie π this was libtraceevent 1.9.0, dated 06/15/2026, and it ate. go read some sub buffers, you've got this π
β¨
π¦ where this page comes from
- ubuntu stonking: libtraceevent-doc 1:1.9.0-2 Β· LGPL-2.1-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms