[DTrace-devel] [PATCH v3 02/19] cg: add dt_cg_trace

Nick Alcock nick.alcock at oracle.com
Thu Mar 24 18:24:28 UTC 2022


A call to

   dt_cg_trace(dlp, drp, 1, 666, foo);

will trace the value of reg FOO into
/sys/kernel/debug/tracing/trace_pipe when dtrace is compiled with
debugging=yes.  '666' above is a counter value which is also logged in
the resulting trace output, to distinguish trace calls from each other.

A call to

  dt_cg_trace(dlp, drp, 0, 666, foo);

will trace the immediate value of FOO at codegen time.

Under debugging=no this call has no effect and is compiled out.

Signed-off-by: Nick Alcock <nick.alcock at oracle.com>
---
 Makeoptions       |  3 ++-
 bpf/Build         |  3 ++-
 bpf/trace_ptr.c   | 23 +++++++++++++++++++++++
 libdtrace/dt_cg.c | 30 ++++++++++++++++++++++++++++++
 4 files changed, 57 insertions(+), 2 deletions(-)
 create mode 100644 bpf/trace_ptr.c

diff --git a/Makeoptions b/Makeoptions
index 011440a31d83..dba38641154f 100644
--- a/Makeoptions
+++ b/Makeoptions
@@ -17,7 +17,8 @@ help::
 	@printf "\n" >&2
 
 ifneq ($(debugging),no)
-override CFLAGS += -O0 -g
+override CFLAGS += -O0 -g -DDEBUGGING
+override BPFCFLAGS += -DDEBUGGING
 endif
 
 ifneq ($(coverage),no)
diff --git a/bpf/Build b/bpf/Build
index d986fd7e089b..1540382bc322 100644
--- a/bpf/Build
+++ b/bpf/Build
@@ -38,7 +38,8 @@ bpf_dlib_SOURCES = \
 	strlen.c \
 	strrchr.S \
 	strtok.S \
-	substr.S
+	substr.S \
+	trace_ptr.c
 
 bpf-check: $(objdir)/include/.dir.stamp
 	$(BPFC) $(BPFCPPFLAGS) $(bpf_dlib_CPPFLAGS) $(BPFCFLAGS) -S \
diff --git a/bpf/trace_ptr.c b/bpf/trace_ptr.c
new file mode 100644
index 000000000000..9dcbc720a115
--- /dev/null
+++ b/bpf/trace_ptr.c
@@ -0,0 +1,23 @@
+#ifdef DEBUGGING
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
+ */
+#include <linux/bpf.h>
+#include <stdint.h>
+#include <bpf-helpers.h>
+
+#ifndef noinline
+# define noinline	__attribute__((noinline))
+#endif
+
+noinline void dt_trace_ptr(uint64_t counter, uint64_t ptr)
+{
+	/*
+	 * Can't use a straight string constant: DTrace cannot yet process
+	 * rodata relocs.
+	 */
+	char fmt[] = "debug: %d: %lx\n";
+	bpf_trace_printk(fmt, 16, counter, ptr);
+}
+#endif
diff --git a/libdtrace/dt_cg.c b/libdtrace/dt_cg.c
index a149c57529f1..5f86c34d7be4 100644
--- a/libdtrace/dt_cg.c
+++ b/libdtrace/dt_cg.c
@@ -25,6 +25,9 @@
 #include <bpf_asm.h>
 
 static void dt_cg_node(dt_node_t *, dt_irlist_t *, dt_regset_t *);
+static void _dt_unused_
+dt_cg_trace(dt_irlist_t *dlp _dt_unused_, dt_regset_t *drp _dt_unused_,
+	    int isreg _dt_unused_, int counter _dt_unused_, uint64_t val _dt_unused_);
 
 /*
  * Generate the generic prologue of the trampoline BPF program.
@@ -744,6 +747,33 @@ dt_cg_fill_gap(dt_pcb_t *pcb, int gap)
 		emit(dlp, BPF_STORE_IMM(BPF_W, BPF_REG_9, off, 0));
 }
 
+/*
+ * Trace an immediate counter and a value to the log at
+ * /sys/kernel/debug/tracing/trace_pipe.  If ISREG, VAL is a register number:
+ * otherwise, it's an immediate value.
+ */
+static void _dt_unused_
+dt_cg_trace(dt_irlist_t *dlp _dt_unused_, dt_regset_t *drp _dt_unused_,
+	    int isreg _dt_unused_, int counter _dt_unused_, uint64_t val _dt_unused_)
+{
+#ifdef DEBUGGING
+	dt_ident_t	*idp = dt_dlib_get_func(yypcb->pcb_hdl, "dt_trace_ptr");
+
+	if (dt_regset_xalloc_args(drp) == -1)
+		longjmp(yypcb->pcb_jmpbuf, EDT_NOREG);
+	dt_regset_xalloc(drp, BPF_REG_0);
+	emit(dlp,  BPF_MOV_IMM(BPF_REG_1, counter));
+	if (isreg)
+		emit(dlp,  BPF_MOV_REG(BPF_REG_2, val));
+	else
+		emit(dlp,  BPF_MOV_IMM(BPF_REG_2, val));
+
+	emite(dlp, BPF_CALL_FUNC(idp->di_id), idp);
+	dt_regset_free(drp, BPF_REG_0);
+	dt_regset_free_args(drp);
+#endif
+}
+
 static void
 dt_cg_memcpy(dt_irlist_t *dlp, dt_regset_t *drp, int dst, int src, size_t size)
 {
-- 
2.35.1.261.g8402f930ba.dirty




More information about the DTrace-devel mailing list