Serenity Operating System
1/*
2 * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <errno.h>
8#include <sys/ptrace.h>
9#include <syscall.h>
10
11extern "C" {
12
13long ptrace(int request, pid_t tid, void* addr, void* data)
14{
15 if (request == PT_PEEKBUF) {
16 // PT_PEEKBUF cannot easily be correctly used through this function signature:
17 // The amount of data to be copied is not available.
18 // We could VERIFY() here, but to safeguard against ports that attempt to use
19 // the same number, let's claim that the Kernel just doesn't know the command.
20 // Use Core::System::ptrace_peekbuf instead.
21 return EINVAL;
22 }
23
24 // PT_PEEK needs special handling since the syscall wrapper
25 // returns the peeked value as an int, which can be negative because of the cast.
26 // When using PT_PEEK, the user can check if an error occurred
27 // by looking at errno rather than the return value.
28
29 FlatPtr out_data;
30 auto is_peek_type = request == PT_PEEK || request == PT_PEEKDEBUG;
31 if (is_peek_type) {
32 data = &out_data;
33 }
34
35 Syscall::SC_ptrace_params params {
36 request,
37 tid,
38 addr,
39 (FlatPtr)data
40 };
41 long rc = syscall(SC_ptrace, ¶ms);
42
43 if (is_peek_type) {
44 if (rc < 0) {
45 errno = -rc;
46 return -1;
47 }
48 errno = 0;
49 return static_cast<long>(out_data);
50 }
51
52 __RETURN_WITH_ERRNO(rc, rc, -1);
53}
54}