[DTrace-devel] [PATCH 2/2] bpf: generate bpf_asm_helpers.h and use it

Kris Van Hees kris.van.hees at oracle.com
Fri Feb 2 16:50:20 UTC 2024


The bpf_asm_helpers.h file is generated from bpf/bpf_helper_defs.h so
that BPF assembler source files no longer need to specify hardcoded
BPF helper ids.

Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
---
 bpf/Build           |  2 +-
 bpf/basename.S      |  4 ++--
 bpf/cleanpath.S     |  5 ++---
 bpf/dirname.S       |  4 ++--
 bpf/index.S         |  5 ++---
 bpf/inet_ntoa.S     |  4 ++--
 bpf/inet_ntoa6.S    |  4 ++--
 bpf/link_ntop.S     |  4 ++--
 bpf/lltostr.S       |  4 ++--
 bpf/mutex_owned.S   |  5 ++---
 bpf/mutex_owner.S   |  4 ++--
 bpf/probe_error.h   |  2 +-
 bpf/progenyof.S     |  5 ++---
 bpf/rindex.S        |  5 ++---
 bpf/rw_iswriter.S   |  4 ++--
 bpf/rw_read_held.S  |  4 ++--
 bpf/rw_write_held.S |  4 ++--
 bpf/strchr.S        |  5 ++---
 bpf/strcmp.S        |  5 ++---
 bpf/strjoin.S       |  4 ++--
 bpf/strrchr.S       |  5 ++---
 bpf/strtok.S        |  5 ++---
 bpf/substr.S        |  4 ++--
 include/Build       |  3 +++
 include/mkHelpers   | 23 +++++++++++++++++++++++
 25 files changed, 70 insertions(+), 53 deletions(-)
 create mode 100755 include/mkHelpers

diff --git a/bpf/Build b/bpf/Build
index 62812942..3e43f4b6 100644
--- a/bpf/Build
+++ b/bpf/Build
@@ -19,7 +19,7 @@ BPFLIBS += bpf_dlib
 bpf_dlib_CPPFLAGS = -Ilibdtrace -Iinclude -I$(objdir)/include -idirafter /usr/include
 bpf_dlib_TARGET = dlibs/bpf_dlib
 bpf_dlib_DIR := $(current-dir)
-bpf_dlib_SRCDEPS = $(objdir)/include/.dir.stamp
+bpf_dlib_SRCDEPS = $(objdir)/include/.dir.stamp $(objdir)/include/bpf_asm_helpers.h
 bpf_dlib_SOURCES = \
 	agg_lqbin.c agg_qbin.c \
 	basename.S \
diff --git a/bpf/basename.S b/bpf/basename.S
index 0c87828d..571f2526 100644
--- a/bpf/basename.S
+++ b/bpf/basename.S
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2021, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read_str	45
+#include <bpf_asm_helpers.h>
 
 /*
  * void dt_basename(const dt_dctx_t *dctx, char *src, char *dst);
diff --git a/bpf/cleanpath.S b/bpf/cleanpath.S
index fb4be1cd..d64fe53f 100644
--- a/bpf/cleanpath.S
+++ b/bpf/cleanpath.S
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2024, Oracle and/or its affiliates.
  */
 
 /*
@@ -132,8 +132,7 @@
  *      dst[strlen(dst) - 1] = '\0';
  */
 
-#define BPF_FUNC_probe_read	4
-#define BPF_FUNC_probe_read_str	45
+#include <bpf_asm_helpers.h>
 
 /*
  * void dt_cleanpath(const dt_dctx_t *dctx, char *src, char *dst);
diff --git a/bpf/dirname.S b/bpf/dirname.S
index 3c07f884..0eb70096 100644
--- a/bpf/dirname.S
+++ b/bpf/dirname.S
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2021, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read_str	45
+#include <bpf_asm_helpers.h>
 
 /*
  * void dt_dirname(const dt_dctx_t *dctx, char *src, char *dst);
diff --git a/bpf/index.S b/bpf/index.S
index c8b1d7cd..4441631e 100644
--- a/bpf/index.S
+++ b/bpf/index.S
@@ -1,10 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2021, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
-#define BPF_FUNC_probe_read_str	45
+#include <bpf_asm_helpers.h>
 
 	.text
 /*
diff --git a/bpf/inet_ntoa.S b/bpf/inet_ntoa.S
index b59674bf..486398ba 100644
--- a/bpf/inet_ntoa.S
+++ b/bpf/inet_ntoa.S
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2022, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
+#include <bpf_asm_helpers.h>
 
 	.text
 
diff --git a/bpf/inet_ntoa6.S b/bpf/inet_ntoa6.S
index a559c1dd..fb4cf9de 100644
--- a/bpf/inet_ntoa6.S
+++ b/bpf/inet_ntoa6.S
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2023, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read		4
+#include <bpf_asm_helpers.h>
 
 #define OUTPUT_LEN			40
 #define INPUT_LEN			16
diff --git a/bpf/link_ntop.S b/bpf/link_ntop.S
index 8f8e5a99..79f82f6c 100644
--- a/bpf/link_ntop.S
+++ b/bpf/link_ntop.S
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2023, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
+#include <bpf_asm_helpers.h>
 
 #define ARPHRD_ETHER		1	/* ARP protocol hw identifier <linux/if_arp.h> */
 #define ARPHRD_INFINIBAND	32	/* ARP protocol hw identifier <linux/if_arp.h> */
diff --git a/bpf/lltostr.S b/bpf/lltostr.S
index 6d3aca05..ef1e2cb1 100644
--- a/bpf/lltostr.S
+++ b/bpf/lltostr.S
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2021, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
+#include <bpf_asm_helpers.h>
 #define MAXCHARS		19
 
 	.text
diff --git a/bpf/mutex_owned.S b/bpf/mutex_owned.S
index 19c61bc5..a3c95247 100644
--- a/bpf/mutex_owned.S
+++ b/bpf/mutex_owned.S
@@ -1,10 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2022, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
-#define BPF_FUNC_get_current_task	35
+#include <bpf_asm_helpers.h>
 #define MUTEX_FLAGS		0x07
 
 	.text
diff --git a/bpf/mutex_owner.S b/bpf/mutex_owner.S
index 3668d643..0afcaa62 100644
--- a/bpf/mutex_owner.S
+++ b/bpf/mutex_owner.S
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2022, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
+#include <bpf_asm_helpers.h>
 #define MUTEX_FLAGS		0x07
 
 	.text
diff --git a/bpf/probe_error.h b/bpf/probe_error.h
index 3dda932e..d3ae079b 100644
--- a/bpf/probe_error.h
+++ b/bpf/probe_error.h
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2021, 2024, Oracle and/or its affiliates.
  */
 #ifndef BPF_PROBE_ERRROR_H
 #define BPF_PROBE_ERRROR_H
diff --git a/bpf/progenyof.S b/bpf/progenyof.S
index e17eaa98..5b4ce60f 100644
--- a/bpf/progenyof.S
+++ b/bpf/progenyof.S
@@ -1,10 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2022, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
-#define BPF_FUNC_get_current_task	35
+#include <bpf_asm_helpers.h>
 
 	.text
 	.align	4
diff --git a/bpf/rindex.S b/bpf/rindex.S
index bd5e4574..586228f9 100644
--- a/bpf/rindex.S
+++ b/bpf/rindex.S
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2021, 2024, Oracle and/or its affiliates.
  */
 
 /*
@@ -17,8 +17,7 @@
  *   - In the call to rindex.S, start<0 means to use the default.
  */
 
-#define BPF_FUNC_probe_read	4
-#define BPF_FUNC_probe_read_str	45
+#include <bpf_asm_helpers.h>
 
 	.text
 /*
diff --git a/bpf/rw_iswriter.S b/bpf/rw_iswriter.S
index e324ad56..475e5150 100644
--- a/bpf/rw_iswriter.S
+++ b/bpf/rw_iswriter.S
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2022, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
+#include <bpf_asm_helpers.h>
 #define _QW_WMASK		0x1ff
 
 	.text
diff --git a/bpf/rw_read_held.S b/bpf/rw_read_held.S
index efe1467c..75a055e6 100644
--- a/bpf/rw_read_held.S
+++ b/bpf/rw_read_held.S
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2022, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
+#include <bpf_asm_helpers.h>
 #define _QW_WMASK		0x1ff
 
 	.text
diff --git a/bpf/rw_write_held.S b/bpf/rw_write_held.S
index 9fdf09cd..f109b022 100644
--- a/bpf/rw_write_held.S
+++ b/bpf/rw_write_held.S
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2022, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
+#include <bpf_asm_helpers.h>
 #define _QW_LOCKED		0x0ff
 
 	.text
diff --git a/bpf/strchr.S b/bpf/strchr.S
index 2777f3a7..8365d9ce 100644
--- a/bpf/strchr.S
+++ b/bpf/strchr.S
@@ -1,10 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2021, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
-#define BPF_FUNC_probe_read_str	45
+#include <bpf_asm_helpers.h>
 
 /*
  * uint64_t dt_strchr(char *src, uint64_t c, char *dst, char *tmp) {
diff --git a/bpf/strcmp.S b/bpf/strcmp.S
index 8147e257..61c8a87c 100644
--- a/bpf/strcmp.S
+++ b/bpf/strcmp.S
@@ -1,10 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2021, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
-#define BPF_FUNC_probe_read_str	45
+#include <bpf_asm_helpers.h>
 
 	.text
 
diff --git a/bpf/strjoin.S b/bpf/strjoin.S
index efeab029..b9ad4dcf 100644
--- a/bpf/strjoin.S
+++ b/bpf/strjoin.S
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2021, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read_str	45
+#include <bpf_asm_helpers.h>
 
 /*
  * void dt_strjoin(char *dst, const char *s1, const char *s2)
diff --git a/bpf/strrchr.S b/bpf/strrchr.S
index 05ccecb9..c3d740ca 100644
--- a/bpf/strrchr.S
+++ b/bpf/strrchr.S
@@ -1,10 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2021, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
-#define BPF_FUNC_probe_read_str	45
+#include <bpf_asm_helpers.h>
 
 /*
  * uint64_t dt_strrchr(char *src, uint64_t c, char *dst) {
diff --git a/bpf/strtok.S b/bpf/strtok.S
index b745c2a3..2cea773d 100644
--- a/bpf/strtok.S
+++ b/bpf/strtok.S
@@ -1,10 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2021, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read	4
-#define BPF_FUNC_probe_read_str	45
+#include <bpf_asm_helpers.h>
 
 	.text
 
diff --git a/bpf/substr.S b/bpf/substr.S
index 8feba79c..a6494f7f 100644
--- a/bpf/substr.S
+++ b/bpf/substr.S
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2021, 2024, Oracle and/or its affiliates.
  */
 
-#define BPF_FUNC_probe_read_str	45
+#include <bpf_asm_helpers.h>
 
 /*
  * char *dt_substr(char *dst, const char *src, int32_t idx, int32_t cnt,
diff --git a/include/Build b/include/Build
index 1a75e583..3d1b5bb1 100644
--- a/include/Build
+++ b/include/Build
@@ -7,3 +7,6 @@ dt_include_DIR := $(current-dir)
 
 $(objdir)/dt_git_version.h: $(dt_include_DIR)/dt_git_version.h.in .git-version
 	sed 's, at GIT-VERSION@,'"`cat .git-version`", < $(dt_include_DIR)/dt_git_version.h.in > $(objdir)/dt_git_version.h
+
+$(objdir)/include/bpf_asm_helpers.h: /usr/include/bpf/bpf_helper_defs.h
+	./include/mkHelpers $< > $@
diff --git a/include/mkHelpers b/include/mkHelpers
new file mode 100755
index 00000000..1a7537ca
--- /dev/null
+++ b/include/mkHelpers
@@ -0,0 +1,23 @@
+#!/usr/bin/awk -f
+#
+# Make a header file associating BPF_FUNC_* symbolic names with BPF helper ids.
+
+BEGIN {
+	print "#ifndef _BPF_ASM_HELPERS_H\n" \
+	      "#define _BPF_ASM_HELPERS_H\n";
+}
+
+$1 == "static" && /=/ && /[ \t]+[1-9][0-9]*[ \t]*;/ {
+	match($0, /[ \t]+[1-9][0-9]*[ \t]*;/);
+	id = substr($0, RSTART, RLENGTH - 1);
+	gsub(/[ \t]+/,"", id);
+	match($0, /\([ \t]*\*[ \t]*bpf_[_A-Za-z0-9]+/);
+	fn = substr($0, RSTART + 1, RLENGTH - 1);
+	sub(/[ \t]*\*[ \t]*bpf_/, "", fn);
+
+	print "#define BPF_FUNC_"fn " " id;
+}
+
+END {
+	print "\n#endif /* _BPF_ASM_HELPERS_H */";
+}
-- 
2.43.0




More information about the DTrace-devel mailing list