[PATCH v3 5/7] tests: add a test for -Y option

Masatake YAMATO yamato at redhat.com
Thu Sep 9 13:43:35 UTC 2021


* tests/strace-Y-0123456789.c: New file.
* tests/gen_tests.in (strace-Y-0123456789): New test.
* tests/Makefile.am (CHECK_PROGRAMS): Add strace-Y-0123456789.

Signed-off-by: Masatake YAMATO <yamato at redhat.com>
---
 tests/.gitignore            |  1 +
 tests/Makefile.am           |  1 +
 tests/gen_tests.in          |  1 +
 tests/strace-Y-0123456789.c | 88 +++++++++++++++++++++++++++++++++++++
 4 files changed, 91 insertions(+)
 create mode 100644 tests/strace-Y-0123456789.c

diff --git a/tests/.gitignore b/tests/.gitignore
index 9501a8d86..f89da5716 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -884,6 +884,7 @@ strace--strings-in-hex-non-ascii
 strace-n
 strace-x
 strace-xx
+strace-Y-0123456789
 swap
 sxetmask
 symlink
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5eddf2278..b3dbd3bbb 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -324,6 +324,7 @@ check_PROGRAMS = $(PURE_EXECUTABLES) \
 	stack-fcall-mangled \
 	status-none-threads \
 	status-unfinished-threads \
+	strace-Y-0123456789 \
 	syslog-success \
 	tgkill--pidns-translation \
 	threads-execve \
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index cafb4a708..ed8a0f4ac 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -857,6 +857,7 @@ strace--timestamps-unix-us +strace-ttt.test 6 --timestamps=unix,us
 strace-n	-e trace=listen -n -qq -a 12
 strace-x	-e trace=chdir -x -a 12
 strace-xx	-e trace=chdir -xx -a 18
+strace-Y-0123456789	-e trace=getppid -e signal='!SIGCHLD' -q -f -Y -a 18
 swap	-a23 -e trace=swapon,swapoff
 sxetmask	-a11 -e trace=sgetmask,ssetmask
 symlink	-a34
diff --git a/tests/strace-Y-0123456789.c b/tests/strace-Y-0123456789.c
new file mode 100644
index 000000000..45b86aada
--- /dev/null
+++ b/tests/strace-Y-0123456789.c
@@ -0,0 +1,88 @@
+/*
+ * Test strace's -Y option
+ * Copyright (c) 2021 The strace developers.
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+/*
+ * The executable built from this source file should
+ * have a long name (> 16) to test how strace reports
+ * the initial value of /proc/$pid/comm.
+ * Even if linux returns a longer name, strace should
+ * not crash.
+ */
+
+#include "tests.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/prctl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#define NEW_NAME "0123456789abcdefghijklmnopqrstuvwxyz"
+
+static int
+do_default_action(void)
+{
+	pid_t pid  = getpid();
+	pid_t ppid = getppid();
+
+	char comm[sizeof(NEW_NAME)];
+	prctl(PR_GET_NAME, comm);
+
+	printf("%-5d<%s> getppid() = %d\n", pid, comm, ppid);
+
+	fflush(stdout);
+
+	pid_t child = fork();
+	if (child < 0)
+		return 1;
+	else if (child == 0) {
+		pid = getpid();
+		ppid = getppid();
+		prctl(PR_GET_NAME, comm);
+		printf("%-5d<%s> getppid() = %d\n", pid, comm, ppid);
+		strcpy(comm, NEW_NAME);
+		prctl(PR_SET_NAME, comm);
+		prctl(PR_GET_NAME, comm);
+		ppid = getppid();
+		printf("%-5d<%s> getppid() = %d\n", pid, comm, ppid);
+		fflush(stdout);
+
+		char *self_argv[] = { (char *)"unused", (char *)"execve", NULL };
+		if (execve("/proc/self/exe", self_argv, NULL) != 0)
+			perror_msg_and_fail("execve");
+
+	} else {
+		int status;
+		wait(&status);
+		printf("%-5d<exe> +++ exited with 0 +++\n", child);
+
+		ppid = getppid();
+		printf("%-5d<%s> getppid() = %d\n", pid, comm, ppid);
+		printf("%-5d<%s> +++ exited with 0 +++\n", pid, comm);
+		return WEXITSTATUS(status);
+	}
+	return 0;
+}
+
+static int
+do_execve_action(int argc, char **argv)
+{
+	return 0;
+}
+
+int
+main(int argc, char **argv)
+{
+	if (argc < 2)
+		return do_default_action();
+	else if (strcmp(argv[1], "execve") == 0)
+		return do_execve_action(argc, argv);
+	error_msg_and_fail("unexpected argument: %s", argv[1]);
+}
-- 
2.31.1



More information about the Strace-devel mailing list