Skip to contentSkip to conversation

uprobe: add support for uprobe systemwide probes

PATCH In review Kris Van Hees 2 participants 2 messages Oct 30 – Nov 6

lore
Share & tools
lore
#0 lore raw

PATCH 17/20 uprobe: add support for uprobe systemwide probes

---
 libdtrace/dt_prov_uprobe.c                    | 170 ++++++++++-
 test/triggers/Build                           |   9 +-
 test/triggers/uprobe-wildcards.c              | 122 ++++++++
 .../providers/uprobe/tst.dlib-path.sh         |  70 +++++
 test/unittest/providers/uprobe/tst.dlopen.sh  | 135 +++++++++
 .../providers/uprobe/tst.elf-offsets.sh       | 278 ++++++++++++++++++
 .../providers/uprobe/tst.lifecycle.sh         | 134 +++++++++
 test/unittest/providers/uprobe/tst.multicpu.r |   4 +
 .../providers/uprobe/tst.multicpu.r.p         |   8 +
 .../unittest/providers/uprobe/tst.multicpu.sh | 138 +++++++++
 test/unittest/providers/uprobe/tst.multicpu.x |   9 +
 test/unittest/providers/uprobe/tst.multipid.r |  20 ++
 .../providers/uprobe/tst.multipid.r.p         |   4 +
 .../unittest/providers/uprobe/tst.multipid.sh | 117 ++++++++
 .../providers/uprobe/tst.multithread.sh       | 160 ++++++++++
 .../providers/uprobe/tst.probespec.sh         |  68 +++++
 .../providers/uprobe/tst.search-path.sh       |  91 ++++++
 test/unittest/providers/uprobe/tst.shlib.sh   |  81 +++++
 test/unittest/providers/uprobe/tst.symbols.sh |  52 ++++
 .../providers/uprobe/tst.systemwide.sh        | 158 ++++++++++
 20 files changed, 1826 insertions(+), 2 deletions(-)
 create mode 100644 test/triggers/uprobe-wildcards.c
 create mode 100755 test/unittest/providers/uprobe/tst.dlib-path.sh
 create mode 100755 test/unittest/providers/uprobe/tst.dlopen.sh
 create mode 100755 test/unittest/providers/uprobe/tst.elf-offsets.sh
 create mode 100755 test/unittest/providers/uprobe/tst.lifecycle.sh
 create mode 100644 test/unittest/providers/uprobe/tst.multicpu.r
 create mode 100755 test/unittest/providers/uprobe/tst.multicpu.r.p
 create mode 100755 test/unittest/providers/uprobe/tst.multicpu.sh
 create mode 100755 test/unittest/providers/uprobe/tst.multicpu.x
 create mode 100644 test/unittest/providers/uprobe/tst.multipid.r
 create mode 100755 test/unittest/providers/uprobe/tst.multipid.r.p
 create mode 100755 test/unittest/providers/uprobe/tst.multipid.sh
 create mode 100755 test/unittest/providers/uprobe/tst.multithread.sh
 create mode 100755 test/unittest/providers/uprobe/tst.probespec.sh
 create mode 100755 test/unittest/providers/uprobe/tst.search-path.sh
 create mode 100755 test/unittest/providers/uprobe/tst.shlib.sh
 create mode 100755 test/unittest/providers/uprobe/tst.symbols.sh
 create mode 100755 test/unittest/providers/uprobe/tst.systemwide.sh
libdtrace/dt_prov_uprobe.c +169 -1
index 1d2b9755..97276fe0 100644
@@ -37,6 +37,7 @@
 #include "dt_dctx.h"
 #include "dt_cg.h"
 #include "dt_list.h"
+#include "dt_module.h"
 #include "dt_provider_tp.h"
 #include "dt_probe.h"
 #include "dt_program.h"
@@ -401,6 +402,161 @@ static int populate_usdt(dtrace_hdl_t *dtp)
 	return 0;
 }
 
+/* Create a concrete probe, retaining existing probes across provide calls. */
+static int provide_uprobe(dtrace_hdl_t *dtp, dt_provider_t *prv,
+			  dt_module_t *dmp, const char *fun, uint64_t off,
+			  int flags)
+{
+	dtrace_probedesc_t pd = {
+		.prv = prv->desc.dtvd_name,
+		.mod = dmp->dm_name,
+		.fun = fun,
+		.prb = flags & PP_IS_RETURN ? "return" : "entry",
+	};
+	dt_uprobe_t	*upp;
+
+	if (dt_probe_lookup(dtp, &pd) != NULL)
+		return 0;
+
+	upp = dt_zalloc(dtp, sizeof(dt_uprobe_t));
+	if (upp == NULL)
+		return 0;
+
+	upp->fn = strdup(dmp->dm_file);
+	if (upp->fn == NULL) {
+		dt_free(dtp, upp);
+		return 0;
+	}
+	upp->off = off;
+	upp->fd = -1;
+	upp->pid = -1;
+	upp->flags = flags;
+
+	if (dt_probe_insert(dtp, prv, pd.prv, pd.mod, pd.fun, pd.prb,
+			    upp) != NULL)
+		return 1;
+
+	dt_free(dtp, upp->fn);
+	dt_free(dtp, upp);
+	return 0;
+}
+
+/* Probe kinds selected by provide(), independent of per-probe PP_IS_* flags. */
+#define PROVIDE_ENTRY	0x1
+#define PROVIDE_RETURN	0x2
+
+/* Match defined functions in both regular and dynamic ELF symbol tables. */
+static int provide_module(dtrace_hdl_t *dtp, dt_provider_t *prv,
+			  dt_module_t *dmp, const dtrace_probedesc_t *pdp,
+			  int flags)
+{
+	dtrace_objinfo_t	info;
+	Elf_Scn		*scn = NULL;
+	int		n = 0;
+
+	if ((dmp->dm_flags & DT_DM_KERNEL) || dmp->dm_file[0] == '\0' ||
+	    dtrace_object_info(dtp, dmp->dm_name, &info) == -1 ||
+	    dmp->dm_elf == NULL)
+		return 0;
+
+	while ((scn = elf_nextscn(dmp->dm_elf, scn)) != NULL) {
+		GElf_Shdr	sh;
+		Elf_Data	*data;
+		size_t		i;
+
+		if (gelf_getshdr(scn, &sh) == NULL ||
+		    (sh.sh_type != SHT_SYMTAB && sh.sh_type != SHT_DYNSYM) ||
+		    sh.sh_entsize == 0 || (data = elf_getdata(scn, NULL)) == NULL)
+			continue;
+
+		for (i = 0; i < sh.sh_size / sh.sh_entsize; i++) {
+			GElf_Sym	sym;
+			GElf_Shdr	fsh;
+			Elf_Scn		*fscn;
+			const char	*fun;
+			uint64_t	off;
+
+			if (gelf_getsym(data, i, &sym) == NULL ||
+			    GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
+			    sym.st_shndx == SHN_UNDEF ||
+			    sym.st_shndx >= SHN_LORESERVE)
+				continue;
+
+			fun = elf_strptr(dmp->dm_elf, sh.sh_link, sym.st_name);
+			if (fun == NULL || *fun == '\0' ||
+			    !dt_gmatch(fun, pdp->fun))
+				continue;
+
+			/* perf uprobes take file offsets, not virtual addresses. */
+			fscn = elf_getscn(dmp->dm_elf, sym.st_shndx);
+			if (fscn == NULL || gelf_getshdr(fscn, &fsh) == NULL ||
+			    !(fsh.sh_flags & SHF_EXECINSTR) ||
+			    fsh.sh_type == SHT_NOBITS || sym.st_value < fsh.sh_addr ||
+			    sym.st_value - fsh.sh_addr >= fsh.sh_size)
+				continue;
+			off = sym.st_value - fsh.sh_addr + fsh.sh_offset;
+
+			if (flags & PROVIDE_ENTRY)
+				n += provide_uprobe(dtp, prv, dmp, fun, off, 0);
+			if (flags & PROVIDE_RETURN)
+				n += provide_uprobe(dtp, prv, dmp, fun, off,
+						    PP_IS_RETURN);
+		}
+	}
+
+	return n;
+}
+
+typedef struct provide_module_arg {
+	dt_provider_t		*prv;
+	const dtrace_probedesc_t	*pdp;
+	int			flags;
+	int			n;
+} provide_module_arg_t;
+
+static int provide_module_cb(dtrace_hdl_t *dtp, dt_module_t *dmp, void *arg)
+{
+	provide_module_arg_t	*data = arg;
+
+	data->n += provide_module(dtp, data->prv, dmp, data->pdp, data->flags);
+	return 0;
+}
+
+static int provide(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp)
+{
+	dt_provider_t	*prv = dt_provider_lookup(dtp, pdp->prv);
+	dt_module_t	*dmp;
+	provide_module_arg_t arg = { .prv = prv, .pdp = pdp };
+
+	if (prv == NULL)
+		return 0;
+
+	/* Disallow uprobe probe specifications with empty module component. */
+	if (pdp->mod && pdp->mod[0] == 0)
+		return 0;
+
+	if (dt_gmatch("entry", pdp->prb))
+		arg.flags |= PROVIDE_ENTRY;
+	if (dt_gmatch("return", pdp->prb))
+		arg.flags |= PROVIDE_RETURN;
+	if (arg.flags == 0)
+		return 0;
+
+	if (pdp->mod[0] != '\0' && !strisglob(pdp->mod)) {
+		dmp = dt_module_lookup_by_name(dtp, pdp->mod);
+		if (dmp == NULL)
+			dmp = dt_module_create_user(dtp, pdp->mod);
+		if (dmp == NULL)
+			return 0;
+
+		return provide_module(dtp, prv, dmp, pdp, arg.flags);
+	}
+
+	dt_module_user_iter(dtp, pdp->mod, provide_module_cb, &arg);
+
+	return arg.n;
+}
+
 static void free_probe_list(dtrace_hdl_t *dtp, list_probe_t *elem)
 {
 	while (elem != NULL) {
@@ -1390,6 +1546,16 @@ static int trampoline(dt_pcb_t *pcb, uint_t exitlbl)
 	 */
 	dt_cg_tramp_copy_regs(pcb);
 
+	if (!dt_list_empty(&uprp->stmts)) {
+		/* Populate probe arguments.  */
+		if (upp->flags & PP_IS_RETURN)
+			dt_cg_tramp_copy_rval_from_regs(pcb);
+		else
+			dt_cg_tramp_copy_args_from_regs(pcb, 1);
+
+		dt_cg_tramp_call_clauses(pcb, uprp, DT_ACTIVITY_ACTIVE);
+	}
+
 	/*
 	 * pid probes.
 	 *
@@ -1542,7 +1708,8 @@ static int uprobe_create(dtrace_hdl_t *dtp, const dt_uprobe_t *upp,
 	attr.uprobe_path = (uint64_t)upp->fn;
 	attr.probe_offset = upp->off;
 
-	return dt_perf_event_open(&attr, upp->pid, -1, -1, 0);
+	return dt_perf_event_open(&attr, upp->pid, upp->pid == -1 ? 0 : -1,
+				  -1, 0);
 }
 
 static int attach(dtrace_hdl_t *dtp, const dt_probe_t *uprp, int bpf_fd)
@@ -1720,6 +1887,7 @@ dt_provimpl_t	dt_uprobe = {
 	.name		= prvname,
 	.prog_type	= BPF_PROG_TYPE_KPROBE,
 	.populate	= &populate,
+	.provide	= &provide,
 	.load_prog	= &dt_bpf_prog_load,
 	.trampoline	= &trampoline,
 	.attach		= &attach,
test/triggers/Build +8 -1
index 0dd546ac..fed3474b 100644
@@ -25,7 +25,8 @@ EXTERNAL_TRIGGERS = $(EXTERNAL_64BIT_TRIGGERS) $(if $(NATIVE_BITNESS_ONLY),,$(EX
 INTERNAL_64BIT_TRIGGERS = libproc-pldd libproc-consistency libproc-sleeper \
     libproc-sleeper-pie libproc-dlmadopen libproc-lookup-by-name \
     libproc-lookup-victim libproc-execing-bkpts libproc-execing-bkpts-victim \
-    btf-cyclic-types btf-lazy-types shared-ctf module-discovery elf-symbols
+    btf-cyclic-types btf-lazy-types shared-ctf module-discovery elf-symbols \
+    uprobe-wildcards
 INTERNAL_32BIT_TRIGGERS := libproc-sleeper-32 libproc-sleeper-pie-32
 INTERNAL_TRIGGERS = $(INTERNAL_64BIT_TRIGGERS) $(if $(NATIVE_BITNESS_ONLY),,$(INTERNAL_32BIT_TRIGGERS))
 
@@ -258,6 +259,12 @@ ustack-tst-spin_CFLAGS := -O0
 ustack-tst-mtspin_CFLAGS := -O0
 ustack-tst-mtspin_LIBS := -lpthread
 
+uprobe-wildcards_CFLAGS := -Ilibproc -Ilibdtrace -Ilibcommon -Wno-pedantic
+uprobe-wildcards_LDFLAGS := -Wl,--export-dynamic
+uprobe-wildcards_DEPS := build-libdtrace.a build-libproc.a libcommon.a libport.a
+uprobe-wildcards_LIBS := $(objdir)/build-libdtrace.a $(objdir)/build-libproc.a \
+    $(objdir)/build-libcommon.a $(objdir)/build-libport.a $(libdtrace_LIBS)
+
 module-discovery_CFLAGS := -Ilibproc -Ilibdtrace -Ilibcommon -Wno-pedantic
 module-discovery_LDFLAGS := -Wl,--wrap=fopen64 \
     -Wl,--wrap=dt_bpf_btf_get_next_id -Wl,--wrap=dt_bpf_btf_get_fd_by_id \
test/triggers/uprobe-wildcards.c new file +122 -0

122 lines not on this page · raw message

test/unittest/providers/uprobe/tst.dlib-path.sh new file +70 -0

70 lines not on this page · raw message

test/unittest/providers/uprobe/tst.dlopen.sh new file +135 -0

135 lines not on this page · raw message

test/unittest/providers/uprobe/tst.elf-offsets.sh new file +278 -0

278 lines not on this page · raw message

test/unittest/providers/uprobe/tst.lifecycle.sh new file +134 -0

134 lines not on this page · raw message

test/unittest/providers/uprobe/tst.multicpu.r new file +4 -0
new file mode 100644
index 00000000..8f30a28f
@@ -0,0 +1,4 @@
+1st CPU is A
+2nd CPU is B
+cpu A
+cpu B
test/unittest/providers/uprobe/tst.multicpu.r.p new file +8 -0
new file mode 100755
index 00000000..89a1dbb4
@@ -0,0 +1,8 @@
+#!/usr/bin/gawk -f
+
+# 1st CPU is %d
+/^1st/ { cpu_a = $NF; $NF = "A"; print; next; }
+# 2nd CPU is %d
+/^2nd/ { cpu_b = $NF; $NF = "B"; print; next; }
+# cpu %d
+/^cpu/ { $NF = ($NF == cpu_a) ? "A" : ($NF == cpu_b) ? "B" : "X"; print; }
test/unittest/providers/uprobe/tst.multicpu.sh new file +138 -0

138 lines not on this page · raw message

test/unittest/providers/uprobe/tst.multicpu.x new file +9 -0
new file mode 100755
index 00000000..1a1a05c8
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+# Skip test if taskset is not available.
+if ! command -v taskset > /dev/null; then
+	echo "taskset not found, skipping test"
+	exit 2
+fi
+
+exit 0
test/unittest/providers/uprobe/tst.multipid.r new file +20 -0
new file mode 100644
index 00000000..e2d34299
@@ -0,0 +1,20 @@
+0 = PID NNN
+1 = PID NNN
+2 = PID NNN
+3 = PID NNN
+4 = PID NNN
+5 = PID NNN
+6 = PID NNN
+7 = PID NNN
+8 = PID NNN
+9 = PID NNN
+11 = PID NNN
+10 = PID NNN
+12 = PID NNN
+13 = PID NNN
+14 = PID NNN
+15 = PID NNN
+16 = PID NNN
+17 = PID NNN
+18 = PID NNN
+19 = PID NNN
test/unittest/providers/uprobe/tst.multipid.r.p new file +4 -0
new file mode 100755
index 00000000..a78716f1
@@ -0,0 +1,4 @@
+#!/usr/bin/gawk -f
+
+# n = PID NNN
+$3 == "PID" { $4 = "NNN"; print; }
test/unittest/providers/uprobe/tst.multipid.sh new file +117 -0

117 lines not on this page · raw message

test/unittest/providers/uprobe/tst.multithread.sh new file +160 -0

160 lines not on this page · raw message

test/unittest/providers/uprobe/tst.probespec.sh new file +68 -0

68 lines not on this page · raw message

test/unittest/providers/uprobe/tst.search-path.sh new file +91 -0

91 lines not on this page · raw message

test/unittest/providers/uprobe/tst.shlib.sh new file +81 -0

81 lines not on this page · raw message

test/unittest/providers/uprobe/tst.symbols.sh new file +52 -0

52 lines not on this page · raw message

test/unittest/providers/uprobe/tst.systemwide.sh new file +158 -0

158 lines not on this page · raw message

Signature · 1 line
2.52.0
Reviewed-by on v1 17/20
#1 lore raw

On 30 Oct 2026, Kris Van Hees verbalised:

Quoted 1 line

Oooh!! Wiring this into the module support is obviously the right way to do it :)

I'm really very surprised it's so little code, even given the amount of prep you needed. I guess the basic uprobes machinery already did what we want these days, after all Eugene's systemwide uprobes work. But all the tests are reassuring (very nice tests).

Quoted 1 line

+/* Match defined functions in both regular and dynamic ELF symbol tables. */

If only we had symtabs that really stuck around in executables without doing -Wl,--export-symbol :( Solaris got this right...

(want me to give this a test too?)

Signature · 1 line
NULL && (void)