[PATCH] libtraceevent plugins: Add plugin_net to handle bswap during print

Petr Malat <[email protected]> Wed, 9 Apr 2025 13:07:05 +0200
Newsgroups org.kernel.vger.linux-trace-devel
Message-ID <[email protected]>
Traced data my be stored in the network order and transformed to the
host order at print time by ntohs() or similar functions. These
functions end up being implemented by one of __builtin_bswapXX()
functions. Support them in the print format string.

Signed-off-by: Petr Malat <[email protected]>
---
 plugins/Makefile     |  1 +
 plugins/plugin_net.c | 56 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)
 create mode 100644 plugins/plugin_net.c

diff --git a/plugins/Makefile b/plugins/Makefile
index 4c8cb17..c07315e 100644
--- a/plugins/Makefile
+++ b/plugins/Makefile
@@ -91,6 +91,7 @@ PLUGINS += plugin_hrtimer.so
 PLUGINS += plugin_kmem.so
 PLUGINS += plugin_kvm.so
 PLUGINS += plugin_mac80211.so
+PLUGINS += plugin_net.so
 PLUGINS += plugin_sched_switch.so
 PLUGINS += plugin_function.so
 PLUGINS += plugin_futex.so
diff --git a/plugins/plugin_net.c b/plugins/plugin_net.c
new file mode 100644
index 0000000..c5eddb5
--- /dev/null
+++ b/plugins/plugin_net.c
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: LGPL-2.1
+#include <stdint.h>
+
+#include "event-parse.h"
+#include "trace-seq.h"
+
+static unsigned long long
+process_builtin_bswap16(struct trace_seq *s, unsigned long long *args)
+{
+	return __builtin_bswap16((uint16_t)args[0]);
+}
+
+static unsigned long long
+process_builtin_bswap32(struct trace_seq *s, unsigned long long *args)
+{
+	return __builtin_bswap32((uint32_t)args[0]);
+}
+
+static unsigned long long
+process_builtin_bswap64(struct trace_seq *s, unsigned long long *args)
+{
+	return __builtin_bswap64(args[0]);
+}
+
+int TEP_PLUGIN_LOADER(struct tep_handle *tep)
+{
+	tep_register_print_function(tep,
+				    process_builtin_bswap16,
+				    TEP_FUNC_ARG_INT,
+				    "__builtin_bswap16",
+				    TEP_FUNC_ARG_INT,
+				    TEP_FUNC_ARG_VOID);
+	tep_register_print_function(tep,
+				    process_builtin_bswap32,
+				    TEP_FUNC_ARG_INT,
+				    "__builtin_bswap32",
+				    TEP_FUNC_ARG_INT,
+				    TEP_FUNC_ARG_VOID);
+	tep_register_print_function(tep,
+				    process_builtin_bswap64,
+				    TEP_FUNC_ARG_LONG,
+				    "__builtin_bswap64",
+				    TEP_FUNC_ARG_LONG,
+				    TEP_FUNC_ARG_VOID);
+	return 0;
+}
+
+void TEP_PLUGIN_UNLOADER(struct tep_handle *tep)
+{
+	tep_unregister_print_function(tep, process_builtin_bswap16,
+				      "__builtin_bswap16");
+	tep_unregister_print_function(tep, process_builtin_bswap32,
+				      "__builtin_bswap32");
+	tep_unregister_print_function(tep, process_builtin_bswap64,
+				      "__builtin_bswap64");
+}
-- 
2.39.5