blob: aa59e314a59d1b91488129d16e7251384510a7d8 [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 <elf.h>
18#include <stdint.h>
19#include <sys/ptrace.h>
20#include <sys/uio.h>
21
22#include <vector>
23
Christopher Ferrisd226a512017-07-14 10:37:19 -070024#include <unwindstack/Elf.h>
25#include <unwindstack/MapInfo.h>
26#include <unwindstack/Memory.h>
27#include <unwindstack/Regs.h>
28
Christopher Ferris94167032017-06-28 18:56:52 -070029#include "Check.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080030#include "Machine.h"
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070031#include "Ucontext.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080032#include "User.h"
33
Christopher Ferrisd226a512017-07-14 10:37:19 -070034namespace unwindstack {
Christopher Ferris3958f802017-02-01 15:44:40 -080035
36template <typename AddressType>
Christopher Ferris7b8e4672017-06-01 17:55:25 -070037bool RegsImpl<AddressType>::GetReturnAddressFromDefault(Memory* memory, uint64_t* value) {
Christopher Ferris3958f802017-02-01 15:44:40 -080038 switch (return_loc_.type) {
39 case LOCATION_REGISTER:
Christopher Ferris94167032017-06-28 18:56:52 -070040 CHECK(return_loc_.value < total_regs_);
Christopher Ferris3958f802017-02-01 15:44:40 -080041 *value = regs_[return_loc_.value];
42 return true;
43 case LOCATION_SP_OFFSET:
44 AddressType return_value;
45 if (!memory->Read(sp_ + return_loc_.value, &return_value, sizeof(return_value))) {
46 return false;
47 }
48 *value = return_value;
49 return true;
50 case LOCATION_UNKNOWN:
51 default:
52 return false;
53 }
54}
55
Christopher Ferris7b8e4672017-06-01 17:55:25 -070056RegsArm::RegsArm()
57 : RegsImpl<uint32_t>(ARM_REG_LAST, ARM_REG_SP, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -080058
59uint64_t RegsArm::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
60 if (!elf->valid()) {
61 return rel_pc;
62 }
63
64 uint64_t load_bias = elf->interface()->load_bias();
65 if (rel_pc < load_bias) {
66 return rel_pc;
67 }
68 uint64_t adjusted_rel_pc = rel_pc - load_bias;
69
70 if (adjusted_rel_pc < 5) {
71 return rel_pc;
72 }
73
74 if (adjusted_rel_pc & 1) {
75 // This is a thumb instruction, it could be 2 or 4 bytes.
76 uint32_t value;
77 if (rel_pc < 5 || !elf->memory()->Read(adjusted_rel_pc - 5, &value, sizeof(value)) ||
78 (value & 0xe000f000) != 0xe000f000) {
79 return rel_pc - 2;
80 }
81 }
82 return rel_pc - 4;
83}
84
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070085void RegsArm::SetFromRaw() {
86 set_pc(regs_[ARM_REG_PC]);
87 set_sp(regs_[ARM_REG_SP]);
88}
89
Christopher Ferris7b8e4672017-06-01 17:55:25 -070090RegsArm64::RegsArm64()
91 : RegsImpl<uint64_t>(ARM64_REG_LAST, ARM64_REG_SP, Location(LOCATION_REGISTER, ARM64_REG_LR)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -080092
93uint64_t RegsArm64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
94 if (!elf->valid()) {
95 return rel_pc;
96 }
97
98 if (rel_pc < 4) {
99 return rel_pc;
100 }
101 return rel_pc - 4;
102}
103
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700104void RegsArm64::SetFromRaw() {
105 set_pc(regs_[ARM64_REG_PC]);
106 set_sp(regs_[ARM64_REG_SP]);
107}
108
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700109RegsX86::RegsX86()
110 : RegsImpl<uint32_t>(X86_REG_LAST, X86_REG_SP, Location(LOCATION_SP_OFFSET, -4)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800111
112uint64_t RegsX86::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
113 if (!elf->valid()) {
114 return rel_pc;
115 }
116
117 if (rel_pc == 0) {
118 return 0;
119 }
120 return rel_pc - 1;
121}
122
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700123void RegsX86::SetFromRaw() {
124 set_pc(regs_[X86_REG_PC]);
125 set_sp(regs_[X86_REG_SP]);
126}
127
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700128RegsX86_64::RegsX86_64()
129 : RegsImpl<uint64_t>(X86_64_REG_LAST, X86_64_REG_SP, Location(LOCATION_SP_OFFSET, -8)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800130
131uint64_t RegsX86_64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
132 if (!elf->valid()) {
133 return rel_pc;
134 }
135
136 if (rel_pc == 0) {
137 return 0;
138 }
139
140 return rel_pc - 1;
141}
142
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700143void RegsX86_64::SetFromRaw() {
144 set_pc(regs_[X86_64_REG_PC]);
145 set_sp(regs_[X86_64_REG_SP]);
146}
147
Christopher Ferris3958f802017-02-01 15:44:40 -0800148static Regs* ReadArm(void* remote_data) {
149 arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data);
150
151 RegsArm* regs = new RegsArm();
152 memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700153 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800154 return regs;
155}
156
157static Regs* ReadArm64(void* remote_data) {
158 arm64_user_regs* user = reinterpret_cast<arm64_user_regs*>(remote_data);
159
160 RegsArm64* regs = new RegsArm64();
161 memcpy(regs->RawData(), &user->regs[0], (ARM64_REG_R31 + 1) * sizeof(uint64_t));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700162 uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
163 reg_data[ARM64_REG_PC] = user->pc;
164 reg_data[ARM64_REG_SP] = user->sp;
165 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800166 return regs;
167}
168
169static Regs* ReadX86(void* remote_data) {
170 x86_user_regs* user = reinterpret_cast<x86_user_regs*>(remote_data);
171
172 RegsX86* regs = new RegsX86();
173 (*regs)[X86_REG_EAX] = user->eax;
174 (*regs)[X86_REG_EBX] = user->ebx;
175 (*regs)[X86_REG_ECX] = user->ecx;
176 (*regs)[X86_REG_EDX] = user->edx;
177 (*regs)[X86_REG_EBP] = user->ebp;
178 (*regs)[X86_REG_EDI] = user->edi;
179 (*regs)[X86_REG_ESI] = user->esi;
180 (*regs)[X86_REG_ESP] = user->esp;
181 (*regs)[X86_REG_EIP] = user->eip;
182
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700183 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800184 return regs;
185}
186
187static Regs* ReadX86_64(void* remote_data) {
188 x86_64_user_regs* user = reinterpret_cast<x86_64_user_regs*>(remote_data);
189
190 RegsX86_64* regs = new RegsX86_64();
191 (*regs)[X86_64_REG_RAX] = user->rax;
192 (*regs)[X86_64_REG_RBX] = user->rbx;
193 (*regs)[X86_64_REG_RCX] = user->rcx;
194 (*regs)[X86_64_REG_RDX] = user->rdx;
195 (*regs)[X86_64_REG_R8] = user->r8;
196 (*regs)[X86_64_REG_R9] = user->r9;
197 (*regs)[X86_64_REG_R10] = user->r10;
198 (*regs)[X86_64_REG_R11] = user->r11;
199 (*regs)[X86_64_REG_R12] = user->r12;
200 (*regs)[X86_64_REG_R13] = user->r13;
201 (*regs)[X86_64_REG_R14] = user->r14;
202 (*regs)[X86_64_REG_R15] = user->r15;
203 (*regs)[X86_64_REG_RDI] = user->rdi;
204 (*regs)[X86_64_REG_RSI] = user->rsi;
205 (*regs)[X86_64_REG_RBP] = user->rbp;
206 (*regs)[X86_64_REG_RSP] = user->rsp;
207 (*regs)[X86_64_REG_RIP] = user->rip;
208
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700209 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800210 return regs;
211}
212
213// This function assumes that reg_data is already aligned to a 64 bit value.
214// If not this could crash with an unaligned access.
215Regs* Regs::RemoteGet(pid_t pid, uint32_t* machine_type) {
216 // Make the buffer large enough to contain the largest registers type.
217 std::vector<uint64_t> buffer(MAX_USER_REGS_SIZE / sizeof(uint64_t));
218 struct iovec io;
219 io.iov_base = buffer.data();
220 io.iov_len = buffer.size() * sizeof(uint64_t);
221
222 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, reinterpret_cast<void*>(&io)) == -1) {
223 return nullptr;
224 }
225
226 switch (io.iov_len) {
227 case sizeof(x86_user_regs):
228 *machine_type = EM_386;
229 return ReadX86(buffer.data());
230 case sizeof(x86_64_user_regs):
231 *machine_type = EM_X86_64;
232 return ReadX86_64(buffer.data());
233 case sizeof(arm_user_regs):
234 *machine_type = EM_ARM;
235 return ReadArm(buffer.data());
236 case sizeof(arm64_user_regs):
237 *machine_type = EM_AARCH64;
238 return ReadArm64(buffer.data());
239 }
240 return nullptr;
241}
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700242
243static Regs* CreateFromArmUcontext(void* ucontext) {
244 arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext);
245
246 RegsArm* regs = new RegsArm();
247 memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t));
248 regs->SetFromRaw();
249 return regs;
250}
251
252static Regs* CreateFromArm64Ucontext(void* ucontext) {
253 arm64_ucontext_t* arm64_ucontext = reinterpret_cast<arm64_ucontext_t*>(ucontext);
254
255 RegsArm64* regs = new RegsArm64();
256 memcpy(regs->RawData(), &arm64_ucontext->uc_mcontext.regs[0], ARM64_REG_LAST * sizeof(uint64_t));
257 regs->SetFromRaw();
258 return regs;
259}
260
261static Regs* CreateFromX86Ucontext(void* ucontext) {
262 x86_ucontext_t* x86_ucontext = reinterpret_cast<x86_ucontext_t*>(ucontext);
263
264 RegsX86* regs = new RegsX86();
265 // Put the registers in the expected order.
266 (*regs)[X86_REG_GS] = x86_ucontext->uc_mcontext.gs;
267 (*regs)[X86_REG_FS] = x86_ucontext->uc_mcontext.fs;
268 (*regs)[X86_REG_ES] = x86_ucontext->uc_mcontext.es;
269 (*regs)[X86_REG_DS] = x86_ucontext->uc_mcontext.ds;
270 (*regs)[X86_REG_EDI] = x86_ucontext->uc_mcontext.edi;
271 (*regs)[X86_REG_ESI] = x86_ucontext->uc_mcontext.esi;
272 (*regs)[X86_REG_EBP] = x86_ucontext->uc_mcontext.ebp;
273 (*regs)[X86_REG_ESP] = x86_ucontext->uc_mcontext.esp;
274 (*regs)[X86_REG_EBX] = x86_ucontext->uc_mcontext.ebx;
275 (*regs)[X86_REG_EDX] = x86_ucontext->uc_mcontext.edx;
276 (*regs)[X86_REG_ECX] = x86_ucontext->uc_mcontext.ecx;
277 (*regs)[X86_REG_EAX] = x86_ucontext->uc_mcontext.eax;
278 (*regs)[X86_REG_EIP] = x86_ucontext->uc_mcontext.eip;
279 regs->SetFromRaw();
280 return regs;
281}
282
283static Regs* CreateFromX86_64Ucontext(void* ucontext) {
284 x86_64_ucontext_t* x86_64_ucontext = reinterpret_cast<x86_64_ucontext_t*>(ucontext);
285
286 RegsX86_64* regs = new RegsX86_64();
287 // Put the registers in the expected order.
288
289 // R8-R15
290 memcpy(&(*regs)[X86_64_REG_R8], &x86_64_ucontext->uc_mcontext.r8, 8 * sizeof(uint64_t));
291
292 // Rest of the registers.
293 (*regs)[X86_64_REG_RDI] = x86_64_ucontext->uc_mcontext.rdi;
294 (*regs)[X86_64_REG_RSI] = x86_64_ucontext->uc_mcontext.rsi;
295 (*regs)[X86_64_REG_RBP] = x86_64_ucontext->uc_mcontext.rbp;
296 (*regs)[X86_64_REG_RBX] = x86_64_ucontext->uc_mcontext.rbx;
297 (*regs)[X86_64_REG_RDX] = x86_64_ucontext->uc_mcontext.rdx;
298 (*regs)[X86_64_REG_RAX] = x86_64_ucontext->uc_mcontext.rax;
299 (*regs)[X86_64_REG_RCX] = x86_64_ucontext->uc_mcontext.rcx;
300 (*regs)[X86_64_REG_RSP] = x86_64_ucontext->uc_mcontext.rsp;
301 (*regs)[X86_64_REG_RIP] = x86_64_ucontext->uc_mcontext.rip;
302
303 regs->SetFromRaw();
304 return regs;
305}
306
307Regs* Regs::CreateFromUcontext(uint32_t machine_type, void* ucontext) {
308 switch (machine_type) {
309 case EM_386:
310 return CreateFromX86Ucontext(ucontext);
311 case EM_X86_64:
312 return CreateFromX86_64Ucontext(ucontext);
313 case EM_ARM:
314 return CreateFromArmUcontext(ucontext);
315 case EM_AARCH64:
316 return CreateFromArm64Ucontext(ucontext);
317 }
318 return nullptr;
319}
320
321uint32_t Regs::GetMachineType() {
322#if defined(__arm__)
323 return EM_ARM;
324#elif defined(__aarch64__)
325 return EM_AARCH64;
326#elif defined(__i386__)
327 return EM_386;
328#elif defined(__x86_64__)
329 return EM_X86_64;
330#else
331 abort();
332#endif
333}
334
335Regs* Regs::CreateFromLocal() {
336 Regs* regs;
337#if defined(__arm__)
338 regs = new RegsArm();
339#elif defined(__aarch64__)
340 regs = new RegsArm64();
341#elif defined(__i386__)
342 regs = new RegsX86();
343#elif defined(__x86_64__)
344 regs = new RegsX86_64();
345#else
346 abort();
347#endif
348 return regs;
349}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700350
351} // namespace unwindstack