aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Rostedt (Google) <rostedt@goodmis.org>2022-12-08 16:36:44 -0500
committerSteven Rostedt (Google) <rostedt@goodmis.org>2022-12-08 19:40:13 -0500
commit5f2aa8497c5b7f9aa7ef0a35b85cc6528d6c2afc (patch)
treed267f821bcba10a745b6f812729447f21bd16fec
parent27e45d9c1b7d22dc7202d8a1dae5512ef3cfad66 (diff)
downloadlibtraceevent-5f2aa8497c5b7f9aa7ef0a35b85cc6528d6c2afc.tar.gz
libtraceevent kbuffer: Add SAME_AS_HOST for endian and size
Add a SAME_AS_HOST to the endian and size parameters of kbuffer_alloc() that makes it easier for the applications that simply read the ring buffer. If the host has 4 byte word, it will call uname() and check the machine value. If machine contains the string "64", it will assume that the kernel is 64 bit and that is what the host will be considered. Link: https://lore.kernel.org/linux-trace-devel/20221208163644.185bf199@gandalf.local.home Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
-rw-r--r--include/traceevent/kbuffer.h2
-rw-r--r--src/kbuffer-parse.c31
2 files changed, 32 insertions, 1 deletions
diff --git a/include/traceevent/kbuffer.h b/include/traceevent/kbuffer.h
index a2b5220..ca638bc 100644
--- a/include/traceevent/kbuffer.h
+++ b/include/traceevent/kbuffer.h
@@ -13,11 +13,13 @@
enum kbuffer_endian {
KBUFFER_ENDIAN_BIG,
KBUFFER_ENDIAN_LITTLE,
+ KBUFFER_ENDIAN_SAME_AS_HOST,
};
enum kbuffer_long_size {
KBUFFER_LSIZE_4,
KBUFFER_LSIZE_8,
+ KBUFFER_LSIZE_SAME_AS_HOST,
};
enum {
diff --git a/src/kbuffer-parse.c b/src/kbuffer-parse.c
index d174e72..390a789 100644
--- a/src/kbuffer-parse.c
+++ b/src/kbuffer-parse.c
@@ -6,6 +6,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdbool.h>
+
+#include <sys/utsname.h>
#include "kbuffer.h"
@@ -159,6 +162,24 @@ static int calc_index(struct kbuffer *kbuf, void *ptr)
static int __next_event(struct kbuffer *kbuf);
+/*
+ * Just because sizeof(long) is 4 bytes, doesn't mean the OS isn't
+ * 64bits
+ */
+static bool host_is_32bit(void)
+{
+ struct utsname buf;
+ int ret;
+
+ ret = uname(&buf);
+ if (ret < 0) {
+ /* Oh well, just assume it is 32 bit */
+ return true;
+ }
+ /* If the uname machine value contains 64, assume the kernel is 64 bit */
+ return strstr(buf.machine, "64") == NULL;
+}
+
/**
* kbuffer_alloc - allocat a new kbuffer
* @size; enum to denote size of word
@@ -175,6 +196,10 @@ kbuffer_alloc(enum kbuffer_long_size size, enum kbuffer_endian endian)
switch (size) {
case KBUFFER_LSIZE_4:
break;
+ case KBUFFER_LSIZE_SAME_AS_HOST:
+ if (sizeof(long) != 8 && host_is_32bit())
+ break;
+ /* fallthrough */
case KBUFFER_LSIZE_8:
flags |= KBUFFER_FL_LONG_8;
break;
@@ -184,6 +209,7 @@ kbuffer_alloc(enum kbuffer_long_size size, enum kbuffer_endian endian)
switch (endian) {
case KBUFFER_ENDIAN_LITTLE:
+ case KBUFFER_ENDIAN_SAME_AS_HOST:
break;
case KBUFFER_ENDIAN_BIG:
flags |= KBUFFER_FL_BIG_ENDIAN;
@@ -198,8 +224,11 @@ kbuffer_alloc(enum kbuffer_long_size size, enum kbuffer_endian endian)
kbuf->flags = flags;
- if (host_is_bigendian())
+ if (host_is_bigendian()) {
+ if (endian == KBUFFER_ENDIAN_SAME_AS_HOST)
+ flags |= KBUFFER_FL_BIG_ENDIAN;
kbuf->flags |= KBUFFER_FL_HOST_BIG_ENDIAN;
+ }
if (do_swap(kbuf)) {
kbuf->read_8 = __read_8_sw;