[DTrace-devel] [PATCH 5/6 v2] Add disassembler annotations for assoc arrays

Kris Van Hees kris.van.hees at oracle.com
Wed Mar 9 21:43:55 UTC 2022


Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
---
 libdtrace/dt_dis.c                         | 44 ++++++++++++++++++++++
 test/unittest/disasm/tst.ann-gvar-assoc.r  |  2 +
 test/unittest/disasm/tst.ann-gvar-assoc.sh | 22 +++++++++++
 test/unittest/disasm/tst.ann-tvar-assoc.r  |  2 +
 test/unittest/disasm/tst.ann-tvar-assoc.sh | 22 +++++++++++
 5 files changed, 92 insertions(+)
 create mode 100644 test/unittest/disasm/tst.ann-gvar-assoc.r
 create mode 100755 test/unittest/disasm/tst.ann-gvar-assoc.sh
 create mode 100644 test/unittest/disasm/tst.ann-tvar-assoc.r
 create mode 100755 test/unittest/disasm/tst.ann-tvar-assoc.sh

diff --git a/libdtrace/dt_dis.c b/libdtrace/dt_dis.c
index d911dbf5..37c5059e 100644
--- a/libdtrace/dt_dis.c
+++ b/libdtrace/dt_dis.c
@@ -337,6 +337,50 @@ dt_dis_bpf_args(const dtrace_difo_t *dp, const char *fn,
 			 dt_dis_varname_id(dp, in->imm + DIF_VAR_OTHER_UBASE,
 					DIFV_SCOPE_THREAD, addr));
 		return buf;
+	} else if (strcmp(fn, "dt_get_assoc") == 0) {
+		uint_t		varid;
+		dof_relodesc_t	*rp = dp->dtdo_breltab;
+		int		cnt = dp->dtdo_brelen;
+		const char	*rname = NULL;
+
+		/*
+		 * We know that the previous four instructions exist and
+		 * move the variable id to a register in the first instruction
+		 * of that seqeuence (because we wrote the code generator to
+		 * emit the instructions in this exact order.)
+		 */
+		in -= 4;
+		varid = in->imm + DIF_VAR_OTHER_UBASE;
+
+		/*
+		 * We need to determine whether this is a associative TLS array
+		 * or a global array.  We know that for a TLS assoc, a call to
+		 * dt_tlskey must be present 5 instructions earlier (actually
+		 * 9 instructions before the call to dt_get_assoc).
+		 */
+		if (addr < 9 || in[-5].code != (BPF_JMP | BPF_CALL) ||
+				in[-5].src_reg != BPF_PSEUDO_CALL)
+			goto gvar_assoc;
+
+		addr -= 9;
+		for (; cnt; cnt--, rp++) {
+			if (rp->dofr_offset > addr * sizeof(uint64_t))
+				break;
+			if (rp->dofr_offset == addr * sizeof(uint64_t))
+				rname = dt_difo_getstr(dp, rp->dofr_name);
+		}
+
+		if (strcmp(rname, "dt_tlskey") != 0)
+			goto gvar_assoc;
+
+		snprintf(buf, len, "self->%s[]",
+			 dt_dis_varname_id(dp, varid, DIFV_SCOPE_THREAD, addr));
+		return buf;
+
+gvar_assoc:
+		snprintf(buf, len, "%s[]",
+			 dt_dis_varname_id(dp, varid, DIFV_SCOPE_GLOBAL, addr));
+		return buf;
 	}
 
 	return NULL;
diff --git a/test/unittest/disasm/tst.ann-gvar-assoc.r b/test/unittest/disasm/tst.ann-gvar-assoc.r
new file mode 100644
index 00000000..f7563a51
--- /dev/null
+++ b/test/unittest/disasm/tst.ann-gvar-assoc.r
@@ -0,0 +1,2 @@
+85 0 1 0000 ffffffff    call dt_get_assoc             ! st[]
+85 0 1 0000 ffffffff    call dt_get_assoc             ! ld[]
diff --git a/test/unittest/disasm/tst.ann-gvar-assoc.sh b/test/unittest/disasm/tst.ann-gvar-assoc.sh
new file mode 100755
index 00000000..6690967d
--- /dev/null
+++ b/test/unittest/disasm/tst.ann-gvar-assoc.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+#
+# Oracle Linux DTrace.
+# Copyright (c) 2022, 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.
+#
+
+dtrace=$1
+
+$dtrace $dt_flags -Sen '
+int st[int], ld[int];
+
+BEGIN
+{
+	st[2] = 42;
+	trace(ld[5]);
+	exit(0);
+}
+' 2>&1 | awk '/ call dt_get_assoc/ { sub(/^[^:]+: /, ""); print; }'
+
+exit $?
diff --git a/test/unittest/disasm/tst.ann-tvar-assoc.r b/test/unittest/disasm/tst.ann-tvar-assoc.r
new file mode 100644
index 00000000..573c93e2
--- /dev/null
+++ b/test/unittest/disasm/tst.ann-tvar-assoc.r
@@ -0,0 +1,2 @@
+85 0 1 0000 ffffffff    call dt_get_assoc             ! self->st[]
+85 0 1 0000 ffffffff    call dt_get_assoc             ! self->ld[]
diff --git a/test/unittest/disasm/tst.ann-tvar-assoc.sh b/test/unittest/disasm/tst.ann-tvar-assoc.sh
new file mode 100755
index 00000000..354d8fbe
--- /dev/null
+++ b/test/unittest/disasm/tst.ann-tvar-assoc.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+#
+# Oracle Linux DTrace.
+# Copyright (c) 2022, 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.
+#
+
+dtrace=$1
+
+$dtrace $dt_flags -Sen '
+self int st[int], ld[int];
+
+BEGIN
+{
+	self->st[2] = 42;
+	trace(self->ld[5]);
+	exit(0);
+}
+' 2>&1 | awk '/ call dt_get_assoc/ { sub(/^[^:]+: /, ""); print; }'
+
+exit $?
-- 
2.34.1




More information about the DTrace-devel mailing list