[DTrace-devel] [External] : [PATCH 2/2] test: add pid probe test for noninvasively grabbed processes

Eslam Almorshdy via B4 Relay devnull+ealmorshdy.d-matrix.ai at kernel.org
Sun Oct 18 07:46:10 UTC 2026


From: Eslam Almorshdy <ealmorshdy at d-matrix.ai>

A process that is already being traced is always grabbed
noninvasively, whether or not it counts as a system daemon.  Run a
program as the ptrace() child of another process and check that pid$pid
probes on a.out:target, libwork.so:work and :target fire.  Without the
previous patch all three fail.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Eslam Almorshdy <ealmorshdy at d-matrix.ai>
---
 test/unittest/pid/tst.noninvasive.r  |   1 +
 test/unittest/pid/tst.noninvasive.sh | 119 +++++++++++++++++++++++++++++++++++
 test/unittest/pid/tst.noninvasive.x  |  15 +++++
 3 files changed, 135 insertions(+)

diff --git a/test/unittest/pid/tst.noninvasive.r b/test/unittest/pid/tst.noninvasive.r
new file mode 100644
index 00000000..2e9ba477
--- /dev/null
+++ b/test/unittest/pid/tst.noninvasive.r
@@ -0,0 +1 @@
+success
diff --git a/test/unittest/pid/tst.noninvasive.sh b/test/unittest/pid/tst.noninvasive.sh
new file mode 100755
index 00000000..9ef9968a
--- /dev/null
+++ b/test/unittest/pid/tst.noninvasive.sh
@@ -0,0 +1,119 @@
+#!/bin/bash
+#
+# Oracle Linux DTrace.
+# Copyright (c) 2026, 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.
+#
+# Check that pid$PID probes named by module and function fire in a process
+# that dtrace can only grab noninvasively: here, the ptrace() child of
+# another process.
+#
+# A noninvasive grab has no rtld_db, hence no link map, which is where
+# libproc otherwise gets each object's name.  The objects must be found by
+# the files they are mapped from: the executable as a.out, a library by its
+# base name, and every object for an empty module.
+#
+
+dtrace=$1
+
+DIRNAME=$tmpdir/pid-noninvasive.$$.$RANDOM
+mkdir -p $DIRNAME
+cd $DIRNAME
+
+cat > work.c <<EOT
+__attribute__((noinline)) int work(int i)
+{
+	return i * 3 + 1;
+}
+EOT
+
+cat > main.c <<EOT
+#include <unistd.h>
+
+extern int work(int);
+volatile int sink;
+
+__attribute__((noinline)) int target(int i)
+{
+	return i * 2 + 1;
+}
+
+int main(void)
+{
+	int i;
+
+	for (i = 0; i < 600; i++) {
+		sink = target(i) + work(i);
+		usleep(100000);
+	}
+	return 0;
+}
+EOT
+
+# tracer PROG: run PROG as a ptrace()d child, print its pid, and resume it
+# at every stop until it exits
+cat > tracer.c <<EOT
+#include <signal.h>
+#include <stdio.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+	pid_t pid;
+	int status, sig;
+
+	if ((pid = fork()) == 0) {
+		ptrace(PTRACE_TRACEME, 0, NULL, NULL);
+		execv(argv[1], &argv[1]);
+		_exit(127);
+	}
+	printf("%d\n", pid);
+	fflush(stdout);
+	while (waitpid(pid, &status, 0) == pid && WIFSTOPPED(status)) {
+		sig = WSTOPSIG(status) == SIGTRAP ? 0 : WSTOPSIG(status);
+		ptrace(PTRACE_CONT, pid, NULL, (void *)(long)sig);
+	}
+	return 0;
+}
+EOT
+
+$CC -O0 -fPIC -shared -o libwork.so work.c &&
+$CC -O0 -o main main.c -L. -lwork -Wl,-rpath,$DIRNAME &&
+$CC -o tracer tracer.c || exit 1
+
+./tracer ./main > pid &
+tracer=$!
+for i in $(seq 50); do
+	pid=$(cat pid)
+	[ -n "$pid" ] && grep -q libwork.so /proc/$pid/maps && break
+	sleep 0.1
+done 2> /dev/null
+
+# fired PROBE: how often PROBE fired in a second, or dtrace's error
+fired() {
+	$dtrace $dt_flags -qn "$1 { n++; }" \
+	    -n 'tick-1s { printf("%d\n", n); exit(0); }' 2>&1 |
+	    awk 'NF { last = $0 } END {
+		sub(/^dtrace: (invalid probe specifier .*: )?/, "", last)
+		print last
+	    }'
+}
+
+status=0
+for spec in a.out:target libwork.so:work :target; do
+	n=$(fired "pid$pid:$spec:entry")
+	case "$n" in
+	''|*[!0-9]*|0)
+		echo "ERROR: pid\$pid:$spec:entry: got '$n', want firings"
+		status=1;;
+	esac
+done
+
+kill $pid
+wait $tracer 2> /dev/null
+
+[ $status -eq 0 ] && echo success
+exit $status
diff --git a/test/unittest/pid/tst.noninvasive.x b/test/unittest/pid/tst.noninvasive.x
new file mode 100755
index 00000000..5c70ea6b
--- /dev/null
+++ b/test/unittest/pid/tst.noninvasive.x
@@ -0,0 +1,15 @@
+#!/bin/bash
+#
+# Oracle Linux DTrace.
+# Copyright (c) 2026, 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.
+#
+# This test links its programs with $CC's default linker.
+
+if ! echo 'int main(void) { return 0; }' |
+    ${CC:-gcc} -x c -o /dev/null - > /dev/null 2>&1; then
+	echo "${CC:-gcc} cannot link"
+	exit 2
+fi
+exit 0

-- 
2.55.0





More information about the DTrace-devel mailing list