tools/testing/selftests/powerpc/ptrace/ptrace-syscall.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/powerpc/ptrace/ptrace-syscall.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/powerpc/ptrace/ptrace-syscall.c
Extension
.c
Size
6239 bytes
Lines
229
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: syscall or user/kernel boundary
Status
core implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/*
 * A ptrace test for testing PTRACE_SYSEMU, PTRACE_SETREGS and
 * PTRACE_GETREG.  This test basically create a child process that executes
 * syscalls and the parent process check if it is being traced appropriated.
 *
 * This test is heavily based on tools/testing/selftests/x86/ptrace_syscall.c
 * test, and it was adapted to run on Powerpc by
 * Breno Leitao <leitao@debian.org>
 */
#define _GNU_SOURCE

#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/syscall.h>
#include <sys/user.h>
#include <unistd.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <err.h>
#include <string.h>
#include <sys/auxv.h>
#include "utils.h"

/* Bitness-agnostic defines for user_regs_struct fields. */
#define user_syscall_nr	gpr[0]
#define user_arg0		gpr[3]
#define user_arg1		gpr[4]
#define user_arg2		gpr[5]
#define user_arg3		gpr[6]
#define user_arg4		gpr[7]
#define user_arg5		gpr[8]
#define user_ip		nip

#define PTRACE_SYSEMU		0x1d

static int nerrs;

static void wait_trap(pid_t chld)
{
	siginfo_t si;

	if (waitid(P_PID, chld, &si, WEXITED|WSTOPPED) != 0)
		err(1, "waitid");
	if (si.si_pid != chld)
		errx(1, "got unexpected pid in event\n");
	if (si.si_code != CLD_TRAPPED)
		errx(1, "got unexpected event type %d\n", si.si_code);
}

static void test_ptrace_syscall_restart(void)
{
	int status;
	struct pt_regs regs;
	pid_t chld;

	printf("[RUN]\tptrace-induced syscall restart\n");

	chld = fork();
	if (chld < 0)
		err(1, "fork");

	/*
	 * Child process is running 4 syscalls after ptrace.
	 *
	 * 1) getpid()
	 * 2) gettid()
	 * 3) tgkill() -> Send SIGSTOP
	 * 4) gettid() -> Where the tests will happen essentially
	 */
	if (chld == 0) {
		if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
			err(1, "PTRACE_TRACEME");

		pid_t pid = getpid(), tid = syscall(SYS_gettid);

		printf("\tChild will make one syscall\n");
		syscall(SYS_tgkill, pid, tid, SIGSTOP);

		syscall(SYS_gettid, 10, 11, 12, 13, 14, 15);
		_exit(0);
	}
	/* Parent process below */

	/* Wait for SIGSTOP sent by tgkill above. */
	if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
		err(1, "waitpid");

Annotation

Implementation Notes