2020-03-28 11:57:46 +03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-28 11:57:46 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <sys/ptrace.h>
|
2021-02-05 12:16:30 +01:00
|
|
|
#include <syscall.h>
|
2020-03-28 11:57:46 +03:00
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
2021-11-19 16:13:07 +02:00
|
|
|
long ptrace(int request, pid_t tid, void* addr, void* data)
|
2020-03-28 11:57:46 +03:00
|
|
|
{
|
2021-11-25 23:04:52 +01:00
|
|
|
if (request == PT_PEEKBUF) {
|
|
|
|
|
// PT_PEEKBUF cannot easily be correctly used through this function signature:
|
|
|
|
|
// The amount of data to be copied is not available.
|
|
|
|
|
// We could VERIFY() here, but to safeguard against ports that attempt to use
|
|
|
|
|
// the same number, let's claim that the Kernel just doesn't know the command.
|
|
|
|
|
// Use Core::System::ptrace_peekbuf instead.
|
|
|
|
|
return EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 17:34:31 +03:00
|
|
|
// PT_PEEK needs special handling since the syscall wrapper
|
|
|
|
|
// returns the peeked value as an int, which can be negative because of the cast.
|
2020-10-02 09:59:28 -04:00
|
|
|
// When using PT_PEEK, the user can check if an error occurred
|
2020-04-10 17:34:31 +03:00
|
|
|
// by looking at errno rather than the return value.
|
|
|
|
|
|
2021-11-19 16:13:07 +02:00
|
|
|
FlatPtr out_data;
|
2021-04-15 12:34:51 -04:00
|
|
|
auto is_peek_type = request == PT_PEEK || request == PT_PEEKDEBUG;
|
|
|
|
|
if (is_peek_type) {
|
2021-11-25 20:15:02 +01:00
|
|
|
data = &out_data;
|
2020-04-10 17:34:31 +03:00
|
|
|
}
|
|
|
|
|
|
2020-03-28 11:57:46 +03:00
|
|
|
Syscall::SC_ptrace_params params {
|
|
|
|
|
request,
|
2020-08-09 01:08:24 +02:00
|
|
|
tid,
|
2021-11-19 16:13:07 +02:00
|
|
|
addr,
|
|
|
|
|
(FlatPtr)data
|
2020-03-28 11:57:46 +03:00
|
|
|
};
|
2021-11-19 16:13:07 +02:00
|
|
|
long rc = syscall(SC_ptrace, ¶ms);
|
2020-04-10 17:34:31 +03:00
|
|
|
|
2021-04-15 12:34:51 -04:00
|
|
|
if (is_peek_type) {
|
2020-04-10 17:34:31 +03:00
|
|
|
if (rc < 0) {
|
|
|
|
|
errno = -rc;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
errno = 0;
|
2021-11-19 16:13:07 +02:00
|
|
|
return static_cast<long>(out_data);
|
2020-04-10 17:34:31 +03:00
|
|
|
}
|
|
|
|
|
|
2020-03-28 11:57:46 +03:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
}
|