[DTrace-devel] [PATCH 1/3] options: add -xbtfpath option

Kris Van Hees kris.van.hees at oracle.com
Mon Jun 23 23:37:17 UTC 2025


Also move the initialization of BPF and BTF to dtrace_init() to ensure
that the -xbtfpath= option is processed before we try to access BTF
data.

Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
---
 libdtrace/dt_btf.c     | 22 ++++++++++++++++++++--
 libdtrace/dt_impl.h    |  1 +
 libdtrace/dt_open.c    | 10 +++++++---
 libdtrace/dt_options.c | 21 +++++++++++++++++++++
 4 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/libdtrace/dt_btf.c b/libdtrace/dt_btf.c
index ad87bd83..e9ead435 100644
--- a/libdtrace/dt_btf.c
+++ b/libdtrace/dt_btf.c
@@ -337,6 +337,8 @@ dt_btf_load_file(dtrace_hdl_t *dtp, const char *fn)
 		return NULL;
 	}
 
+	dt_dprintf("BTF file %s: %d types\n", fn, btf->type_cnt);
+
 	return btf;
 }
 
@@ -765,14 +767,30 @@ out:
 dt_btf_t *
 dt_btf_load_module(dtrace_hdl_t *dtp, dt_module_t *dmp)
 {
-	char		fn[PATH_MAX + 1];
+	char		*fn = NULL;
+	int		rc = 0;
 	dt_btf_t	*btf;
 
 	if (dmp->dm_btf)
 		return dmp->dm_btf;
 
-	snprintf(fn, sizeof(fn), "/sys/kernel/btf/%s", dmp->dm_name);
+	/*
+	 * Default: /sys/kernel/btf/<module>
+	 * If "none", disable BTF.
+	 * Otherwise: <BTF path>/<module>
+	 */
+	if (dtp->dt_btf_path == NULL)
+		rc = asprintf(&fn, "/sys/kernel/btf/%s", dmp->dm_name);
+	else if (strcmp(dtp->dt_btf_path, "none") == 0)
+		return NULL;
+	else
+		rc = asprintf(&fn, "%s/%s", dtp->dt_btf_path, dmp->dm_name);
+
+	if (rc == -1)
+		return dt_btf_set_load_errno(dtp, ENOMEM);
+
 	btf = dt_btf_load_file(dtp, fn);
+	free(fn);
 
 	if (btf && !dtp->dt_shared_btf && strcmp(dmp->dm_name, "vmlinux") == 0)
 		dtp->dt_shared_btf = btf;
diff --git a/libdtrace/dt_impl.h b/libdtrace/dt_impl.h
index 3bb7c344..2adc1252 100644
--- a/libdtrace/dt_impl.h
+++ b/libdtrace/dt_impl.h
@@ -301,6 +301,7 @@ struct dtrace_hdl {
 	dt_htab_t *dt_kernsyms; /* htab of kernel symbol names */
 	char *dt_ctfa_path;	/* path to vmlinux.ctfa */
 	ctf_archive_t *dt_ctfa; /* ctf archive for the entire kernel tree */
+	char *dt_btf_path;	/* path to vmlinux.btf */
 	struct dt_btf *dt_shared_btf; /* BTF data for the kernel (shared) */
 	ctf_file_t *dt_shared_ctf; /* Handle to the shared CTF */
 	dt_htab_t *dt_kernpaths; /* hash table of dt_kern_path_t's */
diff --git a/libdtrace/dt_open.c b/libdtrace/dt_open.c
index 01c36a76..c67455e7 100644
--- a/libdtrace/dt_open.c
+++ b/libdtrace/dt_open.c
@@ -910,6 +910,11 @@ dt_vopen(int version, int flags, int *errp,
 			return set_open_errno(dtp, errp, EDT_NOMEM);
 	}
 
+	/* If DTRACE_OPT_BTFPATH is set, use it.  */
+	dtp->dt_btf_path = getenv("DTRACE_OPT_BTFPATH");
+	if (dtp->dt_btf_path)
+		dtp->dt_btf_path = strdup(dtp->dt_btf_path);
+
 	/*
 	 * Update the module list and load the values for the macro variable
 	 * definitions according to the current process.
@@ -1146,9 +1151,6 @@ dt_vopen(int version, int flags, int *errp,
 	if (dtrace_setopt(dtp, "libdir", _dtrace_libdir) != 0)
 		return set_open_errno(dtp, errp, dtp->dt_errno);
 
-	dt_bpf_init(dtp);
-	dt_btf_get_module_ids(dtp);
-
 	return dtp;
 }
 
@@ -1181,6 +1183,8 @@ dtrace_init(dtrace_hdl_t *dtp)
 	/*
 	 * Initialize the BPF library handling.
 	 */
+	dt_bpf_init(dtp);
+	dt_btf_get_module_ids(dtp);
 	dt_dlib_init(dtp);
 
 	/*
diff --git a/libdtrace/dt_options.c b/libdtrace/dt_options.c
index 377b396b..25e79979 100644
--- a/libdtrace/dt_options.c
+++ b/libdtrace/dt_options.c
@@ -303,6 +303,26 @@ dt_opt_ld_path(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
 	return 0;
 }
 
+static int
+dt_opt_btf_path(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
+{
+	char *btf;
+
+	if (arg == NULL)
+		return dt_set_errno(dtp, EDT_BADOPTVAL);
+
+	if (dtp->dt_pcb != NULL)
+		return dt_set_errno(dtp, EDT_BADOPTCTX);
+
+	if ((btf = strdup(arg)) == NULL)
+		return dt_set_errno(dtp, EDT_NOMEM);
+
+	free(dtp->dt_btf_path);
+	dtp->dt_btf_path = btf;
+
+	return 0;
+}
+
 static int
 dt_opt_ctfa_path(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
 {
@@ -1067,6 +1087,7 @@ static const dt_option_t _dtrace_ctoptions[] = {
 	{ "aggpercpu", dt_opt_agg, DTRACE_A_PERCPU },
 	{ "amin", dt_opt_amin },
 	{ "argref", dt_opt_cflags, DTRACE_C_ARGREF },
+	{ "btfpath", dt_opt_btf_path },
 	{ "core", dt_opt_core },
 	{ "cpp", dt_opt_cflags, DTRACE_C_CPP },
 	{ "cppargs", dt_opt_cpp_args },
-- 
2.43.5




More information about the DTrace-devel mailing list