git: 1610f04c9dac - stable/13 - tools/test/ptrace: update scescx to do remote getpid(2) on each SCX event

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Fri, 20 Jan 2023 03:23:48 UTC
The branch stable/13 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=1610f04c9daceb5b4f2849e48d0021ecc975bb60

commit 1610f04c9daceb5b4f2849e48d0021ecc975bb60
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2022-12-02 01:26:13 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-01-20 03:21:16 +0000

    tools/test/ptrace: update scescx to do remote getpid(2) on each SCX event
    
    Tested by:      pho
    
    (cherry picked from commit 6403a140243d4f5377f589f93aeb44fe75ea8d59)
---
 tools/test/ptrace/scescx.c | 34 ++++++++++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/tools/test/ptrace/scescx.c b/tools/test/ptrace/scescx.c
index 582d1734427e..8f19f9a55889 100644
--- a/tools/test/ptrace/scescx.c
+++ b/tools/test/ptrace/scescx.c
@@ -28,6 +28,7 @@ __FBSDID("$FreeBSD$");
 
 #include <sys/types.h>
 #include <sys/ptrace.h>
+#include <sys/syscall.h>
 #include <sys/sysctl.h>
 #include <sys/wait.h>
 #include <assert.h>
@@ -213,9 +214,13 @@ wait_info(int pid, int status, struct ptrace_lwpinfo *lwpinfo)
 	printf("\n");
 }
 
+static int trace_syscalls = 1;
+static int remote_getpid = 0;
+
 static int
 trace_sc(int pid)
 {
+	struct ptrace_sc_remote pscr;
 	struct ptrace_lwpinfo lwpinfo;
 	int status;
 
@@ -269,6 +274,24 @@ trace_sc(int pid)
 	wait_info(pid, status, &lwpinfo);
 	assert(lwpinfo.pl_flags & PL_FLAG_SCX);
 
+	if (remote_getpid) {
+		memset(&pscr, 0, sizeof(pscr));
+		pscr.pscr_syscall = SYS_getpid;
+		pscr.pscr_nargs = 0;
+		if (ptrace(PT_SC_REMOTE, pid, (caddr_t)&pscr,
+		    sizeof(pscr)) < 0) {
+			perror("PT_SC_REMOTE");
+			ptrace(PT_KILL, pid, NULL, 0);
+			return (-1);
+		} else {
+			printf(TRACE "remote getpid %ld errno %d\n",
+			    pscr.pscr_ret.sr_retval[0], pscr.pscr_ret.sr_error);
+			if (waitpid(pid, &status, 0) == -1) {
+			  perror("waitpid");
+			  return (-1);
+			}
+		}
+	}
 	if (lwpinfo.pl_flags & PL_FLAG_EXEC)
 		get_pathname(pid);
 
@@ -322,8 +345,6 @@ trace_cont(int pid)
 	return (0);
 }
 
-static int trace_syscalls = 1;
-
 static int
 trace(pid_t pid)
 {
@@ -340,12 +361,16 @@ main(int argc, char *argv[])
 	pid_t pid, pid1;
 
 	trace_syscalls = 1;
+	remote_getpid = 0;
 	use_vfork = 0;
-	while ((c = getopt(argc, argv, "csv")) != -1) {
+	while ((c = getopt(argc, argv, "crsv")) != -1) {
 		switch (c) {
 		case 'c':
 			trace_syscalls = 0;
 			break;
+		case 'r':
+			remote_getpid = 1;
+			break;
 		case 's':
 			trace_syscalls = 1;
 			break;
@@ -354,7 +379,8 @@ main(int argc, char *argv[])
 			break;
 		default:
 		case '?':
-			fprintf(stderr, "Usage: %s [-c] [-s] [-v]\n", argv[0]);
+			fprintf(stderr, "Usage: %s [-c] [-r] [-s] [-v]\n",
+			    argv[0]);
 			return (2);
 		}
 	}