[DTrace-devel] [PATCH 05/15 v2] Implement first set of builtin variables

Kris Van Hees kris.van.hees at oracle.com
Fri May 29 10:59:26 PDT 2020


Variables that are currently implemented:

	arg0 .. arg9
	curthread
	pid
	tid
	uid
	gid

Orabug: 31221984
Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
---
 bpf/Build         |  2 +-
 bpf/get_bvar.c    | 46 +++++++++++++++++++++++++++++++++++++++++++---
 libdtrace/dt_cg.c |  6 +++++-
 3 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/bpf/Build b/bpf/Build
index b24b71e0..bc77dcf3 100644
--- a/bpf/Build
+++ b/bpf/Build
@@ -12,7 +12,7 @@ $(objdir)/include/.dir.stamp:
 	@touch $(objdir)/include/.dir.stamp
 
 BPFLIBS += bpf_dlib
-bpf_dlib_CPPFLAGS = -Ilibdtrace -I$(objdir)/include
+bpf_dlib_CPPFLAGS = -Ilibdtrace -Iinclude -I$(objdir)/include
 bpf_dlib_TARGET = dlibs/bpf_dlib
 bpf_dlib_DIR := $(current-dir)
 bpf_dlib_SRCDEPS = $(objdir)/include/.dir.stamp
diff --git a/bpf/get_bvar.c b/bpf/get_bvar.c
index 6b0b354a..4891b144 100644
--- a/bpf/get_bvar.c
+++ b/bpf/get_bvar.c
@@ -5,13 +5,53 @@
 #include <linux/bpf.h>
 #include <stdint.h>
 #include <bpf-helpers.h>
+#include <dtrace/dif_defines.h>
+#include <dt_bpf_ctx.h>
 
 #ifndef noinline
 # define noinline	__attribute__((noinline))
 #endif
 
-noinline uint64_t dt_get_bvar(uint32_t id)
+noinline uint64_t dt_get_bvar(struct dt_bpf_context *dctx, uint32_t id)
 {
-	/* Not implemented yet. */
-	return (uint64_t)-1;
+	switch (id) {
+	case DIF_VAR_EPID:
+		return dctx->epid;
+	case DIF_VAR_ARG0: case DIF_VAR_ARG1: case DIF_VAR_ARG2:
+	case DIF_VAR_ARG3: case DIF_VAR_ARG4: case DIF_VAR_ARG5:
+	case DIF_VAR_ARG6: case DIF_VAR_ARG7: case DIF_VAR_ARG8:
+	case DIF_VAR_ARG9:
+		return dctx->argv[id - DIF_VAR_ARG0];
+	case DIF_VAR_CURTHREAD:
+		return bpf_get_current_task();
+	case DIF_VAR_TIMESTAMP:
+		return bpf_ktime_get_ns();
+	case DIF_VAR_PID: {
+		uint64_t	val = bpf_get_current_pid_tgid();
+
+		return val >> 32;
+	}
+	case DIF_VAR_TID: {
+		uint64_t	val = bpf_get_current_pid_tgid();
+
+		return val & 0x00000000ffffffffUL;
+	}
+	case DIF_VAR_UID: {
+		uint64_t	val = bpf_get_current_uid_gid();
+
+		return val & 0x00000000ffffffffUL;
+	}
+	case DIF_VAR_GID: {
+		uint64_t	val = bpf_get_current_uid_gid();
+
+		return val >> 32;
+	}
+	default:
+		/* Not implemented yet. */
+#if 1
+		return (uint64_t)-1;
+#else
+		return (uint64_t)id;
+#endif
+	}
 }
diff --git a/libdtrace/dt_cg.c b/libdtrace/dt_cg.c
index d916ee68..28f9bb3a 100644
--- a/libdtrace/dt_cg.c
+++ b/libdtrace/dt_cg.c
@@ -895,7 +895,11 @@ dt_cg_load_var(dt_node_t *dst, dt_irlist_t *dlp, dt_regset_t *drp)
 		if (dt_regset_xalloc_args(drp) == -1)
 			longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
 		if (idp->di_id < DIF_VAR_OTHER_UBASE) {	/* built-in var */
-			instr = BPF_MOV_IMM(BPF_REG_1, idp->di_id);
+			instr = BPF_LOAD(BPF_DW, BPF_REG_1,
+					 BPF_REG_FP, DT_STK_DCTX);
+			dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE,
+					 instr));
+			instr = BPF_MOV_IMM(BPF_REG_2, idp->di_id);
 			dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE,
 					 instr));
 			idp = dt_dlib_get_func(yypcb->pcb_hdl, "dt_get_bvar");
-- 
2.26.0




More information about the DTrace-devel mailing list