From 9a7fdb2dae8f0ae0f9c5b0596bb2710f782925aa Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 16 Jul 2014 15:18:54 -0700 Subject: [PATCH] ptrace(3) should be varargs. Bug: 16352070 (cherry picked from commit 98b088dce70a2625d5cfa1872e427af5f06bfd99) Change-Id: I6193ef44df9a5668020916eaca90b47fc4c8ab77 --- libc/bionic/ptrace.cpp | 40 ++++++++++++++++++++------------------- libc/include/sys/ptrace.h | 8 ++++---- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/libc/bionic/ptrace.cpp b/libc/bionic/ptrace.cpp index 5715b0934..5156ec24c 100644 --- a/libc/bionic/ptrace.cpp +++ b/libc/bionic/ptrace.cpp @@ -26,28 +26,30 @@ * SUCH DAMAGE. */ -#include +#include #include -extern "C" long __ptrace(int request, pid_t pid, void* addr, void* data); +extern "C" long __ptrace(int req, pid_t pid, void* addr, void* data); -long ptrace(int request, pid_t pid, void* addr, void* data) { - switch (request) { - case PTRACE_PEEKUSR: - case PTRACE_PEEKTEXT: - case PTRACE_PEEKDATA: - { - long word; - long ret = __ptrace(request, pid, addr, &word); - if (ret == 0) { - return word; - } else { - // __ptrace already set errno for us. - return -1; - } - } +long ptrace(int req, ...) { + bool is_peek = (req == PTRACE_PEEKUSR || req == PTRACE_PEEKTEXT || req == PTRACE_PEEKDATA); + long peek_result; - default: - return __ptrace(request, pid, addr, data); + va_list args; + va_start(args, req); + pid_t pid = va_arg(args, pid_t); + void* addr = va_arg(args, void*); + void* data; + if (is_peek) { + data = &peek_result; + } else { + data = va_arg(args, void*); } + va_end(args); + + long result = __ptrace(req, pid, addr, data); + if (is_peek && result == 0) { + return peek_result; + } + return result; } diff --git a/libc/include/sys/ptrace.h b/libc/include/sys/ptrace.h index 848416b64..8bba9fe43 100644 --- a/libc/include/sys/ptrace.h +++ b/libc/include/sys/ptrace.h @@ -30,15 +30,15 @@ #include #include -/* For all of the defines */ #include __BEGIN_DECLS -#define PTRACE_POKEUSER PTRACE_POKEUSR -#define PTRACE_PEEKUSER PTRACE_PEEKUSR +/* glibc uses different names from the kernel for these two... */ +#define PTRACE_POKEUSER PTRACE_POKEUSR +#define PTRACE_PEEKUSER PTRACE_PEEKUSR -extern long ptrace(int request, pid_t pid, void *addr, void *data); +extern long ptrace(int, ...); __END_DECLS