[DTrace-devel] [PATCH] Add 'id' builtin D variable and a test for it

David Mc Lean david.mclean at oracle.com
Wed Sep 16 22:08:32 PDT 2020


Signed-off-by: David Mc Lean <david.mclean at oracle.com>
---
 bpf/get_bvar.c                         |  2 ++
 libdtrace/dt_bpf.h                     |  1 +
 libdtrace/dt_cc.c                      |  5 ++-
 libdtrace/dt_cg.c                      |  6 ++++
 libdtrace/dt_dctx.h                    |  2 ++
 libdtrace/dt_dlibs.c                   |  1 +
 test/unittest/variables/bvar/tst.id.sh | 59 ++++++++++++++++++++++++++++++++++
 7 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100755 test/unittest/variables/bvar/tst.id.sh

diff --git a/bpf/get_bvar.c b/bpf/get_bvar.c
index 9f09108..52ec186 100644
--- a/bpf/get_bvar.c
+++ b/bpf/get_bvar.c
@@ -27,6 +27,8 @@ noinline uint64_t dt_get_bvar(dt_mstate_t *mst, uint32_t id)
 		return mst->tstamp;
 	case DIF_VAR_EPID:
 		return mst->epid;
+	case DIF_VAR_ID:
+		return mst->prid;
 	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:
diff --git a/libdtrace/dt_bpf.h b/libdtrace/dt_bpf.h
index c4a4a99..7a7fe87 100644
--- a/libdtrace/dt_bpf.h
+++ b/libdtrace/dt_bpf.h
@@ -17,6 +17,7 @@ extern "C" {
 
 #define DT_CONST_EPID	1
 #define DT_CONST_ARGC	2
+#define DT_CONST_PRID	3
 
 extern int perf_event_open(struct perf_event_attr *attr, pid_t pid, int cpu,
 			   int group_fd, unsigned long flags);
diff --git a/libdtrace/dt_cc.c b/libdtrace/dt_cc.c
index f29a21f..ceea19c 100644
--- a/libdtrace/dt_cc.c
+++ b/libdtrace/dt_cc.c
@@ -2412,7 +2412,10 @@ dt_link_construct(dtrace_hdl_t *dtp, const dt_probe_t *prp, dtrace_difo_t *dp,
 		case DT_IDENT_SCALAR:			/* constant */
 			switch (idp->di_id) {
 			case DT_CONST_EPID:
-				nrp->dofr_data = epid;	/* set vakue */
+				nrp->dofr_data = epid;
+				break;
+			case DT_CONST_PRID:
+				nrp->dofr_data = prp->desc->id;
 				break;
 			case DT_CONST_ARGC:
 				nrp->dofr_data = 0;	/* FIXME */
diff --git a/libdtrace/dt_cg.c b/libdtrace/dt_cg.c
index 0d3463c..14e543b 100644
--- a/libdtrace/dt_cg.c
+++ b/libdtrace/dt_cg.c
@@ -175,9 +175,11 @@ dt_cg_prologue(dt_pcb_t *pcb, dt_node_t *pred)
 {
 	dt_irlist_t	*dlp = &pcb->pcb_ir;
 	dt_ident_t	*epid = dt_dlib_get_var(pcb->pcb_hdl, "EPID");
+	dt_ident_t	*prid = dt_dlib_get_var(pcb->pcb_hdl, "PRID");
 	struct bpf_insn	instr;
 
 	assert(epid != NULL);
+	assert(prid != NULL);
 
 	/*
 	 * void dt_program(dt_dctx_t *dctx)
@@ -206,6 +208,7 @@ dt_cg_prologue(dt_pcb_t *pcb, dt_node_t *pred)
 	 *				// stdw [%r0 + DMST_FAULT], 0
 	 *	dctx->mst->tstamp = 0;	// stdw [%r0 + DMST_TSTAMP], 0
 	 *	dctx->mst->epid = EPID;	// stw [%r0 + DMST_EPID], EPID
+	 *	dctx->mst->prid = PRID;	// stw [%r0 + DMST_PRID], PRID
 	 *	*((uint32_t *)&buf[0]) = EPID;
 	 *				// stw [%r9 + 0], EPID
 	 */
@@ -218,6 +221,9 @@ dt_cg_prologue(dt_pcb_t *pcb, dt_node_t *pred)
 	instr = BPF_STORE_IMM(BPF_W, BPF_REG_0, DMST_EPID, -1);
 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
 	dlp->dl_last->di_extern = epid;
+	instr = BPF_STORE_IMM(BPF_W, BPF_REG_0, DMST_PRID, -1);
+	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
+	dlp->dl_last->di_extern = prid;
 	instr = BPF_STORE_IMM(BPF_W, BPF_REG_9, 0, -1);
 	dt_irlist_append(dlp, dt_cg_node_alloc(DT_LBL_NONE, instr));
 	dlp->dl_last->di_extern = epid;
diff --git a/libdtrace/dt_dctx.h b/libdtrace/dt_dctx.h
index 79ef710..48b1470 100644
--- a/libdtrace/dt_dctx.h
+++ b/libdtrace/dt_dctx.h
@@ -15,6 +15,7 @@
  */
 typedef struct dt_mstate {
 	uint32_t	epid;		/* Enabled probe ID */
+	uint32_t	prid;		/* Probe ID */
 	uint32_t	tag;		/* Tag (for future use) */
 	uint64_t	fault;		/* DTrace fault flags */
 	uint64_t	tstamp;		/* cached timestamp value */
@@ -45,6 +46,7 @@ typedef struct dt_dctx {
 #define DCTX_FP(off)	(-(ushort_t)DCTX_SIZE + (ushort_t)(off))
 
 #define DMST_EPID	offsetof(dt_mstate_t, epid)
+#define DMST_PRID	offsetof(dt_mstate_t, prid)
 #define DMST_TAG	offsetof(dt_mstate_t, tag)
 #define DMST_FAULT	offsetof(dt_mstate_t, fault)
 #define DMST_TSTAMP	offsetof(dt_mstate_t, tstamp)
diff --git a/libdtrace/dt_dlibs.c b/libdtrace/dt_dlibs.c
index 83eb4a1..2875364 100644
--- a/libdtrace/dt_dlibs.c
+++ b/libdtrace/dt_dlibs.c
@@ -69,6 +69,7 @@ static const dt_ident_t		dt_bpf_symbols[] = {
 	DT_BPF_SYMBOL(tvars, DT_IDENT_PTR),
 	/* BPF internal identifiers */
 	DT_BPF_SYMBOL_ID(EPID, DT_IDENT_SCALAR, DT_CONST_EPID),
+	DT_BPF_SYMBOL_ID(PRID, DT_IDENT_SCALAR, DT_CONST_PRID),
 	DT_BPF_SYMBOL_ID(ARGC, DT_IDENT_SCALAR, DT_CONST_ARGC),
 	/* End-of-list marker */
 	{ NULL, }
diff --git a/test/unittest/variables/bvar/tst.id.sh b/test/unittest/variables/bvar/tst.id.sh
new file mode 100755
index 0000000..c34679f
--- /dev/null
+++ b/test/unittest/variables/bvar/tst.id.sh
@@ -0,0 +1,59 @@
+#!/bin/bash
+#
+# Oracle Linux DTrace.
+# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
+# Licensed under the Universal Permissive License v 1.0 as shown at
+# http://oss.oracle.com/licenses/upl.
+#
+
+##
+#
+# ASSERTION:
+# The id built-in variable matches the ID.
+#
+# SECTION: Variables/Built-in Variables/id
+#
+##
+
+dtrace=$1
+
+# Testing turns off default ID output.
+# For this test, however, we want that ID output.
+unset _DTRACE_TESTING
+
+# Feed DTrace output into awk script to process results.
+$dtrace $dt_flags -n '
+profile-3 {
+	trace(id);
+}
+
+tick-2 {
+	exit(0);
+}
+' | awk '
+BEGIN {
+        nevents = 0;
+        nerrors = 0;
+        ID = -1;
+}
+
+{ print }
+
+$3 == ":profile-3" {
+        nevents++;
+        if (NF != 4) { nerrors++; print; print "ERROR: not 4 fields" };
+        if ($2 != $4) { nerrors++; print; print "ERROR: ID and id differ" };
+        if (ID == -1) { ID = $2 };
+        if (ID != $2) { nerrors++; print; print "ERROR: probe ID changed" };
+}
+
+END {
+        if (nevents == 0) { print "NO EVENTS"; exit(1) };
+        if (nerrors != 0) { print "FOUND ERRORS"; exit(1) };
+        print "success";
+        exit(0);
+}
+'
+
+exit $?
+
-- 
1.8.3.1




More information about the DTrace-devel mailing list