summaryrefslogtreecommitdiffstats
path: root/libunwindstack/Memory.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libunwindstack/Memory.cpp')
-rw-r--r--libunwindstack/Memory.cpp139
1 files changed, 80 insertions, 59 deletions
diff --git a/libunwindstack/Memory.cpp b/libunwindstack/Memory.cpp
index b1b39a0b3..1f3c6c393 100644
--- a/libunwindstack/Memory.cpp
+++ b/libunwindstack/Memory.cpp
@@ -32,7 +32,9 @@
#include "Check.h"
-static size_t ProcessVmRead(pid_t pid, void* dst, uint64_t remote_src, size_t len) {
+namespace unwindstack {
+
+static size_t ProcessVmRead(pid_t pid, uint64_t remote_src, void* dst, size_t len) {
struct iovec dst_iov = {
.iov_base = dst,
.iov_len = len,
@@ -82,7 +84,59 @@ static size_t ProcessVmRead(pid_t pid, void* dst, uint64_t remote_src, size_t le
return rc == -1 ? 0 : rc;
}
-namespace unwindstack {
+static bool PtraceReadLong(pid_t pid, uint64_t addr, long* value) {
+ // ptrace() returns -1 and sets errno when the operation fails.
+ // To disambiguate -1 from a valid result, we clear errno beforehand.
+ errno = 0;
+ *value = ptrace(PTRACE_PEEKTEXT, pid, reinterpret_cast<void*>(addr), nullptr);
+ if (*value == -1 && errno) {
+ return false;
+ }
+ return true;
+}
+
+static size_t PtraceRead(pid_t pid, uint64_t addr, void* dst, size_t bytes) {
+ // Make sure that there is no overflow.
+ uint64_t max_size;
+ if (__builtin_add_overflow(addr, bytes, &max_size)) {
+ return 0;
+ }
+
+ size_t bytes_read = 0;
+ long data;
+ size_t align_bytes = addr & (sizeof(long) - 1);
+ if (align_bytes != 0) {
+ if (!PtraceReadLong(pid, addr & ~(sizeof(long) - 1), &data)) {
+ return 0;
+ }
+ size_t copy_bytes = std::min(sizeof(long) - align_bytes, bytes);
+ memcpy(dst, reinterpret_cast<uint8_t*>(&data) + align_bytes, copy_bytes);
+ addr += copy_bytes;
+ dst = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(dst) + copy_bytes);
+ bytes -= copy_bytes;
+ bytes_read += copy_bytes;
+ }
+
+ for (size_t i = 0; i < bytes / sizeof(long); i++) {
+ if (!PtraceReadLong(pid, addr, &data)) {
+ return bytes_read;
+ }
+ memcpy(dst, &data, sizeof(long));
+ dst = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(dst) + sizeof(long));
+ addr += sizeof(long);
+ bytes_read += sizeof(long);
+ }
+
+ size_t left_over = bytes & (sizeof(long) - 1);
+ if (left_over) {
+ if (!PtraceReadLong(pid, addr, &data)) {
+ return bytes_read;
+ }
+ memcpy(dst, &data, left_over);
+ bytes_read += left_over;
+ }
+ return bytes_read;
+}
bool Memory::ReadFully(uint64_t addr, void* dst, size_t size) {
size_t rc = Read(addr, dst, size);
@@ -198,72 +252,39 @@ size_t MemoryFileAtOffset::Read(uint64_t addr, void* dst, size_t size) {
return actual_len;
}
-static bool PtraceReadLong(pid_t pid, uint64_t addr, long* value) {
- // ptrace() returns -1 and sets errno when the operation fails.
- // To disambiguate -1 from a valid result, we clear errno beforehand.
- errno = 0;
- *value = ptrace(PTRACE_PEEKTEXT, pid, reinterpret_cast<void*>(addr), nullptr);
- if (*value == -1 && errno) {
- return false;
- }
- return true;
-}
-
-static size_t ReadWithPtrace(pid_t pid, uint64_t addr, void* dst, size_t bytes) {
- // Make sure that there is no overflow.
- uint64_t max_size;
- if (__builtin_add_overflow(addr, bytes, &max_size)) {
- return 0;
- }
-
- size_t bytes_read = 0;
- long data;
- size_t align_bytes = addr & (sizeof(long) - 1);
- if (align_bytes != 0) {
- if (!PtraceReadLong(pid, addr & ~(sizeof(long) - 1), &data)) {
- return 0;
- }
- size_t copy_bytes = std::min(sizeof(long) - align_bytes, bytes);
- memcpy(dst, reinterpret_cast<uint8_t*>(&data) + align_bytes, copy_bytes);
- addr += copy_bytes;
- dst = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(dst) + copy_bytes);
- bytes -= copy_bytes;
- bytes_read += copy_bytes;
- }
-
- for (size_t i = 0; i < bytes / sizeof(long); i++) {
- if (!PtraceReadLong(pid, addr, &data)) {
- return bytes_read;
- }
- memcpy(dst, &data, sizeof(long));
- dst = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(dst) + sizeof(long));
- addr += sizeof(long);
- bytes_read += sizeof(long);
- }
-
- size_t left_over = bytes & (sizeof(long) - 1);
- if (left_over) {
- if (!PtraceReadLong(pid, addr, &data)) {
- return bytes_read;
- }
- memcpy(dst, &data, left_over);
- bytes_read += left_over;
- }
- return bytes_read;
-}
-
size_t MemoryRemote::Read(uint64_t addr, void* dst, size_t size) {
#if !defined(__LP64__)
- // Cannot read an address greater than 32 bits.
+ // Cannot read an address greater than 32 bits in a 32 bit context.
if (addr > UINT32_MAX) {
return 0;
}
#endif
- return ReadWithPtrace(pid_, addr, dst, size);
+
+ size_t (*read_func)(pid_t, uint64_t, void*, size_t) =
+ reinterpret_cast<size_t (*)(pid_t, uint64_t, void*, size_t)>(read_redirect_func_.load());
+ if (read_func != nullptr) {
+ return read_func(pid_, addr, dst, size);
+ } else {
+ // Prefer process_vm_read, try it first. If it doesn't work, use the
+ // ptrace function. If at least one of them returns at least some data,
+ // set that as the permanent function to use.
+ // This assumes that if process_vm_read works once, it will continue
+ // to work.
+ size_t bytes = ProcessVmRead(pid_, addr, dst, size);
+ if (bytes > 0) {
+ read_redirect_func_ = reinterpret_cast<uintptr_t>(ProcessVmRead);
+ return bytes;
+ }
+ bytes = PtraceRead(pid_, addr, dst, size);
+ if (bytes > 0) {
+ read_redirect_func_ = reinterpret_cast<uintptr_t>(PtraceRead);
+ }
+ return bytes;
+ }
}
size_t MemoryLocal::Read(uint64_t addr, void* dst, size_t size) {
- return ProcessVmRead(getpid(), dst, addr, size);
+ return ProcessVmRead(getpid(), addr, dst, size);
}
MemoryRange::MemoryRange(const std::shared_ptr<Memory>& memory, uint64_t begin, uint64_t length,