[PATCH v2] libtraceevent plugins: Add plugin_net to handle byte swap during print

Petr Malat <[email protected]> Thu, 17 Apr 2025 16:41:27 +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()
or by a combination of __builtin_constant_p() and __fswapXX().
Support them in the print format string.

The __builtin_constant_p() is used in conditionals, which have an
optimized version for constant argument like this:
  __builtin_constant_p(X) ? constant_expression(X) : func(X)
Always assume __builtin_constant_p() is false, as the false branch
should be generic and work even with constant.

Signed-off-by: Petr Malat <[email protected]>
---
 plugins/Makefile     |  1 +
 plugins/plugin_net.c | 94 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 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..4aae7fb
--- /dev/null
+++ b/plugins/plugin_net.c
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: LGPL-2.1
+#include <stdint.h>
+
+#include "event-parse.h"
+#include "trace-seq.h"
+
+static unsigned long long
+process_builtin_constant_p(struct trace_seq *s, unsigned long long *args)
+{
+	return 0;
+}
+
+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_constant_p,
+				    TEP_FUNC_ARG_INT,
+				    "__builtin_constant_p",
+				    TEP_FUNC_ARG_INT,
+				    TEP_FUNC_ARG_VOID);
+	tep_register_print_function(tep,
+				    process_builtin_bswap16,
+				    TEP_FUNC_ARG_INT,
+				    "__fswab16",
+				    TEP_FUNC_ARG_INT,
+				    TEP_FUNC_ARG_VOID);
+	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,
+				    "__fswab32",
+				    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,
+				    "__fswab64",
+				    TEP_FUNC_ARG_LONG,
+				    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_constant_p,
+				      "__builtin_constant_p");
+	tep_unregister_print_function(tep, process_builtin_bswap16,
+				      "__fswab16");
+	tep_unregister_print_function(tep, process_builtin_bswap16,
+				      "__builtin_bswap16");
+	tep_unregister_print_function(tep, process_builtin_bswap32,
+				      "__fswab32");
+	tep_unregister_print_function(tep, process_builtin_bswap32,
+				      "__builtin_bswap32");
+	tep_unregister_print_function(tep, process_builtin_bswap64,
+				      "__fswab64");
+	tep_unregister_print_function(tep, process_builtin_bswap64,
+				      "__builtin_bswap64");
+}
-- 
2.39.5