Re: [PATCH v2 10/11] tools/nolibc: add ptrace support

Thomas Weißschuh
2025-09-20T09:27:27Z

On 2025-09-19 17:34:19+0200, Benjamin Berg wrote:
> From: Benjamin Berg <benjamin.berg@intel.com>
>
> Add ptrace support, as it will be useful in UML.
>
> Signed-off-by: Benjamin Berg <benjamin.berg@intel.com>
> ---
> tools/include/nolibc/Makefile | 1 +
> tools/include/nolibc/nolibc.h | 1 +
> tools/include/nolibc/sys/ptrace.h | 52 ++++++++++++++++++++
> tools/testing/selftests/nolibc/nolibc-test.c | 2 +
> 4 files changed, 56 insertions(+)
> create mode 100644 tools/include/nolibc/sys/ptrace.h

(...)

> diff --git a/tools/include/nolibc/sys/ptrace.h b/tools/include/nolibc/sys/ptrace.h
> new file mode 100644
> index 000000000000..3119abdeeecb
> --- /dev/null
> +++ b/tools/include/nolibc/sys/ptrace.h
> @@ -0,0 +1,52 @@
> +/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
> +/*
> + * ptrace for NOLIBC
> + * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
> + * Copyright (C) 2025 Intel Corporation
> + */
> +
> +/* make sure to include all global symbols */
> +#include "../nolibc.h"
> +
> +#ifndef _NOLIBC_SYS_PTRACE_H
> +#define _NOLIBC_SYS_PTRACE_H
> +
> +#include "../sys.h"
> +#include "uio.h"
> +
> +
> +#include <linux/ptrace.h>
> +
> +/*
> + * long ptrace(int op, pid_t pid, void *addr, void *data);
> + *
> + * However, addr may also be an integer in some cases.

This comment is a bit confusing. It reads like it can be an integer as
in 'int' datatype. But the kernel expects an integer compatible with
'void *', so 'unsigned long'. I think we can drop the comment.

> + */
> +static __attribute__((unused))
> +long sys_vptrace(int op, pid_t pid, va_list args)
> +{
> + return my_syscall4(__NR_ptrace, op, pid,
> + va_arg(args, void *), va_arg(args, void *));
> +}
> +
> +static __attribute__((unused))
> +ssize_t sys_ptrace(int op, pid_t pid, ...)

ptrace(2) does not document addr and data to be optional.
While it does acknowledge the fact that it is variadic on glibc,
users are still recommended to always supply all arguments.
I'd prefer to keep it simple and avoid the va_list.

> +{
> + va_list args;
> +
> + va_start(args, pid);
> + return sys_vptrace(op, pid, args);
> + va_end(args);
> +}
> +
> +static __attribute__((unused))
> +ssize_t ptrace(int op, pid_t pid, ...)
> +{
> + va_list args;
> +
> + va_start(args, pid);
> + return __sysret(sys_vptrace(op, pid, args));
> + va_end(args);
> +}
> +
> +#endif /* _NOLIBC_SYS_PTRACE_H */

(...)