2017-02-02 00:44:40 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2017-07-14 19:37:19 +02:00
|
|
|
#include <unwindstack/Elf.h>
|
|
|
|
#include <unwindstack/MapInfo.h>
|
|
|
|
#include <unwindstack/Regs.h>
|
2017-12-01 03:56:01 +01:00
|
|
|
#include <unwindstack/RegsArm.h>
|
|
|
|
#include <unwindstack/RegsArm64.h>
|
2017-11-08 10:53:53 +01:00
|
|
|
#include <unwindstack/RegsMips.h>
|
|
|
|
#include <unwindstack/RegsMips64.h>
|
2018-02-09 04:27:47 +01:00
|
|
|
#include <unwindstack/RegsX86.h>
|
|
|
|
#include <unwindstack/RegsX86_64.h>
|
|
|
|
#include <unwindstack/UserArm.h>
|
|
|
|
#include <unwindstack/UserArm64.h>
|
|
|
|
#include <unwindstack/UserMips.h>
|
|
|
|
#include <unwindstack/UserMips64.h>
|
|
|
|
#include <unwindstack/UserX86.h>
|
|
|
|
#include <unwindstack/UserX86_64.h>
|
2017-02-02 00:44:40 +01:00
|
|
|
|
2017-07-14 19:37:19 +02:00
|
|
|
namespace unwindstack {
|
2017-02-02 00:44:40 +01:00
|
|
|
|
2017-12-01 03:56:01 +01:00
|
|
|
// The largest user structure.
|
2017-11-08 10:53:53 +01:00
|
|
|
constexpr size_t MAX_USER_REGS_SIZE = sizeof(mips64_user_regs) + 10;
|
2017-02-02 00:44:40 +01:00
|
|
|
|
|
|
|
// This function assumes that reg_data is already aligned to a 64 bit value.
|
|
|
|
// If not this could crash with an unaligned access.
|
2017-08-25 22:55:06 +02:00
|
|
|
Regs* Regs::RemoteGet(pid_t pid) {
|
2017-02-02 00:44:40 +01:00
|
|
|
// Make the buffer large enough to contain the largest registers type.
|
|
|
|
std::vector<uint64_t> buffer(MAX_USER_REGS_SIZE / sizeof(uint64_t));
|
|
|
|
struct iovec io;
|
|
|
|
io.iov_base = buffer.data();
|
|
|
|
io.iov_len = buffer.size() * sizeof(uint64_t);
|
|
|
|
|
|
|
|
if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, reinterpret_cast<void*>(&io)) == -1) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (io.iov_len) {
|
|
|
|
case sizeof(x86_user_regs):
|
2017-12-01 03:56:01 +01:00
|
|
|
return RegsX86::Read(buffer.data());
|
2017-02-02 00:44:40 +01:00
|
|
|
case sizeof(x86_64_user_regs):
|
2017-12-01 03:56:01 +01:00
|
|
|
return RegsX86_64::Read(buffer.data());
|
2017-02-02 00:44:40 +01:00
|
|
|
case sizeof(arm_user_regs):
|
2017-12-01 03:56:01 +01:00
|
|
|
return RegsArm::Read(buffer.data());
|
2017-02-02 00:44:40 +01:00
|
|
|
case sizeof(arm64_user_regs):
|
2017-12-01 03:56:01 +01:00
|
|
|
return RegsArm64::Read(buffer.data());
|
2017-11-08 10:53:53 +01:00
|
|
|
case sizeof(mips_user_regs):
|
|
|
|
return RegsMips::Read(buffer.data());
|
|
|
|
case sizeof(mips64_user_regs):
|
|
|
|
return RegsMips64::Read(buffer.data());
|
2017-02-02 00:44:40 +01:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-07-08 01:35:48 +02:00
|
|
|
|
2017-12-01 03:56:01 +01:00
|
|
|
Regs* Regs::CreateFromUcontext(ArchEnum arch, void* ucontext) {
|
|
|
|
switch (arch) {
|
|
|
|
case ARCH_X86:
|
|
|
|
return RegsX86::CreateFromUcontext(ucontext);
|
|
|
|
case ARCH_X86_64:
|
|
|
|
return RegsX86_64::CreateFromUcontext(ucontext);
|
|
|
|
case ARCH_ARM:
|
|
|
|
return RegsArm::CreateFromUcontext(ucontext);
|
|
|
|
case ARCH_ARM64:
|
|
|
|
return RegsArm64::CreateFromUcontext(ucontext);
|
2017-11-08 10:53:53 +01:00
|
|
|
case ARCH_MIPS:
|
|
|
|
return RegsMips::CreateFromUcontext(ucontext);
|
|
|
|
case ARCH_MIPS64:
|
|
|
|
return RegsMips64::CreateFromUcontext(ucontext);
|
2017-12-01 03:56:01 +01:00
|
|
|
case ARCH_UNKNOWN:
|
|
|
|
default:
|
|
|
|
return nullptr;
|
2017-07-08 01:35:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 03:56:01 +01:00
|
|
|
ArchEnum Regs::CurrentArch() {
|
2017-07-08 01:35:48 +02:00
|
|
|
#if defined(__arm__)
|
2017-12-01 03:56:01 +01:00
|
|
|
return ARCH_ARM;
|
2017-07-08 01:35:48 +02:00
|
|
|
#elif defined(__aarch64__)
|
2017-12-01 03:56:01 +01:00
|
|
|
return ARCH_ARM64;
|
2017-07-08 01:35:48 +02:00
|
|
|
#elif defined(__i386__)
|
2017-12-01 03:56:01 +01:00
|
|
|
return ARCH_X86;
|
2017-07-08 01:35:48 +02:00
|
|
|
#elif defined(__x86_64__)
|
2017-12-01 03:56:01 +01:00
|
|
|
return ARCH_X86_64;
|
2017-07-08 01:35:48 +02:00
|
|
|
#else
|
|
|
|
abort();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Regs* Regs::CreateFromLocal() {
|
|
|
|
Regs* regs;
|
|
|
|
#if defined(__arm__)
|
|
|
|
regs = new RegsArm();
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
regs = new RegsArm64();
|
|
|
|
#elif defined(__i386__)
|
|
|
|
regs = new RegsX86();
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
regs = new RegsX86_64();
|
|
|
|
#else
|
|
|
|
abort();
|
|
|
|
#endif
|
|
|
|
return regs;
|
|
|
|
}
|
2017-07-14 19:37:19 +02:00
|
|
|
|
2020-03-20 01:27:58 +01:00
|
|
|
uint64_t GetPcAdjustment(uint64_t rel_pc, Elf* elf, ArchEnum arch) {
|
|
|
|
switch (arch) {
|
|
|
|
case ARCH_ARM: {
|
|
|
|
if (!elf->valid()) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t load_bias = elf->GetLoadBias();
|
|
|
|
if (rel_pc < load_bias) {
|
|
|
|
if (rel_pc < 2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
uint64_t adjusted_rel_pc = rel_pc - load_bias;
|
|
|
|
if (adjusted_rel_pc < 5) {
|
|
|
|
if (adjusted_rel_pc < 2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (adjusted_rel_pc & 1) {
|
|
|
|
// This is a thumb instruction, it could be 2 or 4 bytes.
|
|
|
|
uint32_t value;
|
|
|
|
if (!elf->memory()->ReadFully(adjusted_rel_pc - 5, &value, sizeof(value)) ||
|
|
|
|
(value & 0xe000f000) != 0xe000f000) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
case ARCH_ARM64: {
|
|
|
|
if (rel_pc < 4) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
case ARCH_MIPS:
|
|
|
|
case ARCH_MIPS64: {
|
|
|
|
if (rel_pc < 8) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// For now, just assume no compact branches
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
case ARCH_X86:
|
|
|
|
case ARCH_X86_64: {
|
|
|
|
if (rel_pc == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case ARCH_UNKNOWN:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 19:37:19 +02:00
|
|
|
} // namespace unwindstack
|