blob: 29dbf9d1535d23af8a17dbd854f76bd44115158d [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Christopher Ferris3958f802017-02-01 15:44:40 -080017#include <stdint.h>
18#include <sys/ptrace.h>
19#include <sys/uio.h>
20
21#include <vector>
22
Christopher Ferrisd226a512017-07-14 10:37:19 -070023#include <unwindstack/Elf.h>
24#include <unwindstack/MapInfo.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070025#include <unwindstack/Regs.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080026#include <unwindstack/RegsArm.h>
27#include <unwindstack/RegsArm64.h>
28#include <unwindstack/RegsX86.h>
29#include <unwindstack/RegsX86_64.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070030
Christopher Ferrisd06001d2017-11-30 18:56:01 -080031#include "UserArm.h"
32#include "UserArm64.h"
33#include "UserX86.h"
34#include "UserX86_64.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080035
Christopher Ferrisd226a512017-07-14 10:37:19 -070036namespace unwindstack {
Christopher Ferris3958f802017-02-01 15:44:40 -080037
Christopher Ferrisd06001d2017-11-30 18:56:01 -080038// The largest user structure.
39constexpr size_t MAX_USER_REGS_SIZE = sizeof(arm64_user_regs) + 10;
Christopher Ferris3958f802017-02-01 15:44:40 -080040
41// This function assumes that reg_data is already aligned to a 64 bit value.
42// If not this could crash with an unaligned access.
Josh Gao0953ecd2017-08-25 13:55:06 -070043Regs* Regs::RemoteGet(pid_t pid) {
Christopher Ferris3958f802017-02-01 15:44:40 -080044 // Make the buffer large enough to contain the largest registers type.
45 std::vector<uint64_t> buffer(MAX_USER_REGS_SIZE / sizeof(uint64_t));
46 struct iovec io;
47 io.iov_base = buffer.data();
48 io.iov_len = buffer.size() * sizeof(uint64_t);
49
50 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, reinterpret_cast<void*>(&io)) == -1) {
51 return nullptr;
52 }
53
54 switch (io.iov_len) {
55 case sizeof(x86_user_regs):
Christopher Ferrisd06001d2017-11-30 18:56:01 -080056 return RegsX86::Read(buffer.data());
Christopher Ferris3958f802017-02-01 15:44:40 -080057 case sizeof(x86_64_user_regs):
Christopher Ferrisd06001d2017-11-30 18:56:01 -080058 return RegsX86_64::Read(buffer.data());
Christopher Ferris3958f802017-02-01 15:44:40 -080059 case sizeof(arm_user_regs):
Christopher Ferrisd06001d2017-11-30 18:56:01 -080060 return RegsArm::Read(buffer.data());
Christopher Ferris3958f802017-02-01 15:44:40 -080061 case sizeof(arm64_user_regs):
Christopher Ferrisd06001d2017-11-30 18:56:01 -080062 return RegsArm64::Read(buffer.data());
Christopher Ferris3958f802017-02-01 15:44:40 -080063 }
64 return nullptr;
65}
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070066
Christopher Ferrisd06001d2017-11-30 18:56:01 -080067Regs* Regs::CreateFromUcontext(ArchEnum arch, void* ucontext) {
68 switch (arch) {
69 case ARCH_X86:
70 return RegsX86::CreateFromUcontext(ucontext);
71 case ARCH_X86_64:
72 return RegsX86_64::CreateFromUcontext(ucontext);
73 case ARCH_ARM:
74 return RegsArm::CreateFromUcontext(ucontext);
75 case ARCH_ARM64:
76 return RegsArm64::CreateFromUcontext(ucontext);
77 case ARCH_UNKNOWN:
78 default:
79 return nullptr;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070080 }
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070081}
82
Christopher Ferrisd06001d2017-11-30 18:56:01 -080083ArchEnum Regs::CurrentArch() {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070084#if defined(__arm__)
Christopher Ferrisd06001d2017-11-30 18:56:01 -080085 return ARCH_ARM;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070086#elif defined(__aarch64__)
Christopher Ferrisd06001d2017-11-30 18:56:01 -080087 return ARCH_ARM64;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070088#elif defined(__i386__)
Christopher Ferrisd06001d2017-11-30 18:56:01 -080089 return ARCH_X86;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070090#elif defined(__x86_64__)
Christopher Ferrisd06001d2017-11-30 18:56:01 -080091 return ARCH_X86_64;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070092#else
93 abort();
94#endif
95}
96
97Regs* Regs::CreateFromLocal() {
98 Regs* regs;
99#if defined(__arm__)
100 regs = new RegsArm();
101#elif defined(__aarch64__)
102 regs = new RegsArm64();
103#elif defined(__i386__)
104 regs = new RegsX86();
105#elif defined(__x86_64__)
106 regs = new RegsX86_64();
107#else
108 abort();
109#endif
110 return regs;
111}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700112
113} // namespace unwindstack