blob: 28a77dc1681c48f36dce1b6f16dde5a1e0a853ac [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
Christopher Ferris7b8e4672017-06-01 17:55:25 -070036RegsArm::RegsArm()
37 : RegsImpl<uint32_t>(ARM_REG_LAST, ARM_REG_SP, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -080038
Josh Gao0953ecd2017-08-25 13:55:06 -070039uint32_t RegsArm::MachineType() {
40 return EM_ARM;
41}
42
Christopher Ferris3958f802017-02-01 15:44:40 -080043uint64_t RegsArm::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
44 if (!elf->valid()) {
45 return rel_pc;
46 }
47
Christopher Ferrise69f4702017-10-19 16:08:58 -070048 uint64_t load_bias = elf->GetLoadBias();
Christopher Ferris3958f802017-02-01 15:44:40 -080049 if (rel_pc < load_bias) {
50 return rel_pc;
51 }
52 uint64_t adjusted_rel_pc = rel_pc - load_bias;
53
54 if (adjusted_rel_pc < 5) {
55 return rel_pc;
56 }
57
58 if (adjusted_rel_pc & 1) {
59 // This is a thumb instruction, it could be 2 or 4 bytes.
60 uint32_t value;
Josh Gaoef35aa52017-10-18 11:44:51 -070061 if (rel_pc < 5 || !elf->memory()->ReadFully(adjusted_rel_pc - 5, &value, sizeof(value)) ||
Christopher Ferris3958f802017-02-01 15:44:40 -080062 (value & 0xe000f000) != 0xe000f000) {
63 return rel_pc - 2;
64 }
65 }
66 return rel_pc - 4;
67}
68
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070069void RegsArm::SetFromRaw() {
70 set_pc(regs_[ARM_REG_PC]);
71 set_sp(regs_[ARM_REG_SP]);
72}
73
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070074bool RegsArm::SetPcFromReturnAddress(Memory*) {
75 if (pc() == regs_[ARM_REG_LR]) {
76 return false;
77 }
78
79 set_pc(regs_[ARM_REG_LR]);
80 return true;
81}
82
Josh Gao6f580d82017-09-22 12:57:15 -070083void RegsArm::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
84 fn("r0", regs_[ARM_REG_R0]);
85 fn("r1", regs_[ARM_REG_R1]);
86 fn("r2", regs_[ARM_REG_R2]);
87 fn("r3", regs_[ARM_REG_R3]);
88 fn("r4", regs_[ARM_REG_R4]);
89 fn("r5", regs_[ARM_REG_R5]);
90 fn("r6", regs_[ARM_REG_R6]);
91 fn("r7", regs_[ARM_REG_R7]);
92 fn("r8", regs_[ARM_REG_R8]);
93 fn("r9", regs_[ARM_REG_R9]);
94 fn("r10", regs_[ARM_REG_R10]);
95 fn("r11", regs_[ARM_REG_R11]);
96 fn("ip", regs_[ARM_REG_R12]);
97 fn("sp", regs_[ARM_REG_SP]);
98 fn("lr", regs_[ARM_REG_LR]);
99 fn("pc", regs_[ARM_REG_PC]);
100}
101
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700102RegsArm64::RegsArm64()
103 : RegsImpl<uint64_t>(ARM64_REG_LAST, ARM64_REG_SP, Location(LOCATION_REGISTER, ARM64_REG_LR)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800104
Josh Gao0953ecd2017-08-25 13:55:06 -0700105uint32_t RegsArm64::MachineType() {
106 return EM_AARCH64;
107}
108
Christopher Ferris3958f802017-02-01 15:44:40 -0800109uint64_t RegsArm64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
110 if (!elf->valid()) {
111 return rel_pc;
112 }
113
114 if (rel_pc < 4) {
115 return rel_pc;
116 }
117 return rel_pc - 4;
118}
119
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700120void RegsArm64::SetFromRaw() {
121 set_pc(regs_[ARM64_REG_PC]);
122 set_sp(regs_[ARM64_REG_SP]);
123}
124
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700125bool RegsArm64::SetPcFromReturnAddress(Memory*) {
126 if (pc() == regs_[ARM64_REG_LR]) {
127 return false;
128 }
129
130 set_pc(regs_[ARM64_REG_LR]);
131 return true;
132}
133
Josh Gao6f580d82017-09-22 12:57:15 -0700134void RegsArm64::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
135 fn("x0", regs_[ARM64_REG_R0]);
136 fn("x1", regs_[ARM64_REG_R1]);
137 fn("x2", regs_[ARM64_REG_R2]);
138 fn("x3", regs_[ARM64_REG_R3]);
139 fn("x4", regs_[ARM64_REG_R4]);
140 fn("x5", regs_[ARM64_REG_R5]);
141 fn("x6", regs_[ARM64_REG_R6]);
142 fn("x7", regs_[ARM64_REG_R7]);
143 fn("x8", regs_[ARM64_REG_R8]);
144 fn("x9", regs_[ARM64_REG_R9]);
145 fn("x10", regs_[ARM64_REG_R10]);
146 fn("x11", regs_[ARM64_REG_R11]);
147 fn("x12", regs_[ARM64_REG_R12]);
148 fn("x13", regs_[ARM64_REG_R13]);
149 fn("x14", regs_[ARM64_REG_R14]);
150 fn("x15", regs_[ARM64_REG_R15]);
151 fn("x16", regs_[ARM64_REG_R16]);
152 fn("x17", regs_[ARM64_REG_R17]);
153 fn("x18", regs_[ARM64_REG_R18]);
154 fn("x19", regs_[ARM64_REG_R19]);
155 fn("x20", regs_[ARM64_REG_R20]);
156 fn("x21", regs_[ARM64_REG_R21]);
157 fn("x22", regs_[ARM64_REG_R22]);
158 fn("x23", regs_[ARM64_REG_R23]);
159 fn("x24", regs_[ARM64_REG_R24]);
160 fn("x25", regs_[ARM64_REG_R25]);
161 fn("x26", regs_[ARM64_REG_R26]);
162 fn("x27", regs_[ARM64_REG_R27]);
163 fn("x28", regs_[ARM64_REG_R28]);
164 fn("x29", regs_[ARM64_REG_R29]);
165 fn("sp", regs_[ARM64_REG_SP]);
166 fn("lr", regs_[ARM64_REG_LR]);
167 fn("pc", regs_[ARM64_REG_PC]);
168}
169
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700170RegsX86::RegsX86()
171 : RegsImpl<uint32_t>(X86_REG_LAST, X86_REG_SP, Location(LOCATION_SP_OFFSET, -4)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800172
Josh Gao0953ecd2017-08-25 13:55:06 -0700173uint32_t RegsX86::MachineType() {
174 return EM_386;
175}
176
Christopher Ferris3958f802017-02-01 15:44:40 -0800177uint64_t RegsX86::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
178 if (!elf->valid()) {
179 return rel_pc;
180 }
181
182 if (rel_pc == 0) {
183 return 0;
184 }
185 return rel_pc - 1;
186}
187
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700188void RegsX86::SetFromRaw() {
189 set_pc(regs_[X86_REG_PC]);
190 set_sp(regs_[X86_REG_SP]);
191}
192
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700193bool RegsX86::SetPcFromReturnAddress(Memory* process_memory) {
194 // Attempt to get the return address from the top of the stack.
195 uint32_t new_pc;
Josh Gaoef35aa52017-10-18 11:44:51 -0700196 if (!process_memory->ReadFully(sp_, &new_pc, sizeof(new_pc)) || new_pc == pc()) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700197 return false;
198 }
199
200 set_pc(new_pc);
201 return true;
202}
203
Josh Gao6f580d82017-09-22 12:57:15 -0700204void RegsX86::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
205 fn("eax", regs_[X86_REG_EAX]);
206 fn("ebx", regs_[X86_REG_EBX]);
207 fn("ecx", regs_[X86_REG_ECX]);
208 fn("edx", regs_[X86_REG_EDX]);
209 fn("ebp", regs_[X86_REG_EBP]);
210 fn("edi", regs_[X86_REG_EDI]);
211 fn("esi", regs_[X86_REG_ESI]);
212 fn("esp", regs_[X86_REG_ESP]);
213 fn("eip", regs_[X86_REG_EIP]);
214}
215
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700216RegsX86_64::RegsX86_64()
217 : RegsImpl<uint64_t>(X86_64_REG_LAST, X86_64_REG_SP, Location(LOCATION_SP_OFFSET, -8)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800218
Josh Gao0953ecd2017-08-25 13:55:06 -0700219uint32_t RegsX86_64::MachineType() {
220 return EM_X86_64;
221}
222
Christopher Ferris3958f802017-02-01 15:44:40 -0800223uint64_t RegsX86_64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
224 if (!elf->valid()) {
225 return rel_pc;
226 }
227
228 if (rel_pc == 0) {
229 return 0;
230 }
231
232 return rel_pc - 1;
233}
234
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700235void RegsX86_64::SetFromRaw() {
236 set_pc(regs_[X86_64_REG_PC]);
237 set_sp(regs_[X86_64_REG_SP]);
238}
239
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700240bool RegsX86_64::SetPcFromReturnAddress(Memory* process_memory) {
241 // Attempt to get the return address from the top of the stack.
242 uint64_t new_pc;
Josh Gaoef35aa52017-10-18 11:44:51 -0700243 if (!process_memory->ReadFully(sp_, &new_pc, sizeof(new_pc)) || new_pc == pc()) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700244 return false;
245 }
246
247 set_pc(new_pc);
248 return true;
249}
250
Josh Gao6f580d82017-09-22 12:57:15 -0700251void RegsX86_64::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
252 fn("rax", regs_[X86_64_REG_RAX]);
253 fn("rbx", regs_[X86_64_REG_RBX]);
254 fn("rcx", regs_[X86_64_REG_RCX]);
255 fn("rdx", regs_[X86_64_REG_RDX]);
256 fn("r8", regs_[X86_64_REG_R8]);
257 fn("r9", regs_[X86_64_REG_R9]);
258 fn("r10", regs_[X86_64_REG_R10]);
259 fn("r11", regs_[X86_64_REG_R11]);
260 fn("r12", regs_[X86_64_REG_R12]);
261 fn("r13", regs_[X86_64_REG_R13]);
262 fn("r14", regs_[X86_64_REG_R14]);
263 fn("r15", regs_[X86_64_REG_R15]);
264 fn("rdi", regs_[X86_64_REG_RDI]);
265 fn("rsi", regs_[X86_64_REG_RSI]);
266 fn("rbp", regs_[X86_64_REG_RBP]);
267 fn("rsp", regs_[X86_64_REG_RSP]);
268 fn("rip", regs_[X86_64_REG_RIP]);
269}
270
Christopher Ferris3958f802017-02-01 15:44:40 -0800271static Regs* ReadArm(void* remote_data) {
272 arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data);
273
274 RegsArm* regs = new RegsArm();
275 memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700276 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800277 return regs;
278}
279
280static Regs* ReadArm64(void* remote_data) {
281 arm64_user_regs* user = reinterpret_cast<arm64_user_regs*>(remote_data);
282
283 RegsArm64* regs = new RegsArm64();
284 memcpy(regs->RawData(), &user->regs[0], (ARM64_REG_R31 + 1) * sizeof(uint64_t));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700285 uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
286 reg_data[ARM64_REG_PC] = user->pc;
287 reg_data[ARM64_REG_SP] = user->sp;
288 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800289 return regs;
290}
291
292static Regs* ReadX86(void* remote_data) {
293 x86_user_regs* user = reinterpret_cast<x86_user_regs*>(remote_data);
294
295 RegsX86* regs = new RegsX86();
296 (*regs)[X86_REG_EAX] = user->eax;
297 (*regs)[X86_REG_EBX] = user->ebx;
298 (*regs)[X86_REG_ECX] = user->ecx;
299 (*regs)[X86_REG_EDX] = user->edx;
300 (*regs)[X86_REG_EBP] = user->ebp;
301 (*regs)[X86_REG_EDI] = user->edi;
302 (*regs)[X86_REG_ESI] = user->esi;
303 (*regs)[X86_REG_ESP] = user->esp;
304 (*regs)[X86_REG_EIP] = user->eip;
305
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700306 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800307 return regs;
308}
309
310static Regs* ReadX86_64(void* remote_data) {
311 x86_64_user_regs* user = reinterpret_cast<x86_64_user_regs*>(remote_data);
312
313 RegsX86_64* regs = new RegsX86_64();
314 (*regs)[X86_64_REG_RAX] = user->rax;
315 (*regs)[X86_64_REG_RBX] = user->rbx;
316 (*regs)[X86_64_REG_RCX] = user->rcx;
317 (*regs)[X86_64_REG_RDX] = user->rdx;
318 (*regs)[X86_64_REG_R8] = user->r8;
319 (*regs)[X86_64_REG_R9] = user->r9;
320 (*regs)[X86_64_REG_R10] = user->r10;
321 (*regs)[X86_64_REG_R11] = user->r11;
322 (*regs)[X86_64_REG_R12] = user->r12;
323 (*regs)[X86_64_REG_R13] = user->r13;
324 (*regs)[X86_64_REG_R14] = user->r14;
325 (*regs)[X86_64_REG_R15] = user->r15;
326 (*regs)[X86_64_REG_RDI] = user->rdi;
327 (*regs)[X86_64_REG_RSI] = user->rsi;
328 (*regs)[X86_64_REG_RBP] = user->rbp;
329 (*regs)[X86_64_REG_RSP] = user->rsp;
330 (*regs)[X86_64_REG_RIP] = user->rip;
331
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700332 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800333 return regs;
334}
335
336// This function assumes that reg_data is already aligned to a 64 bit value.
337// If not this could crash with an unaligned access.
Josh Gao0953ecd2017-08-25 13:55:06 -0700338Regs* Regs::RemoteGet(pid_t pid) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800339 // Make the buffer large enough to contain the largest registers type.
340 std::vector<uint64_t> buffer(MAX_USER_REGS_SIZE / sizeof(uint64_t));
341 struct iovec io;
342 io.iov_base = buffer.data();
343 io.iov_len = buffer.size() * sizeof(uint64_t);
344
345 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, reinterpret_cast<void*>(&io)) == -1) {
346 return nullptr;
347 }
348
349 switch (io.iov_len) {
350 case sizeof(x86_user_regs):
Christopher Ferris3958f802017-02-01 15:44:40 -0800351 return ReadX86(buffer.data());
352 case sizeof(x86_64_user_regs):
Christopher Ferris3958f802017-02-01 15:44:40 -0800353 return ReadX86_64(buffer.data());
354 case sizeof(arm_user_regs):
Christopher Ferris3958f802017-02-01 15:44:40 -0800355 return ReadArm(buffer.data());
356 case sizeof(arm64_user_regs):
Christopher Ferris3958f802017-02-01 15:44:40 -0800357 return ReadArm64(buffer.data());
358 }
359 return nullptr;
360}
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700361
362static Regs* CreateFromArmUcontext(void* ucontext) {
363 arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext);
364
365 RegsArm* regs = new RegsArm();
366 memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t));
367 regs->SetFromRaw();
368 return regs;
369}
370
371static Regs* CreateFromArm64Ucontext(void* ucontext) {
372 arm64_ucontext_t* arm64_ucontext = reinterpret_cast<arm64_ucontext_t*>(ucontext);
373
374 RegsArm64* regs = new RegsArm64();
375 memcpy(regs->RawData(), &arm64_ucontext->uc_mcontext.regs[0], ARM64_REG_LAST * sizeof(uint64_t));
376 regs->SetFromRaw();
377 return regs;
378}
379
Christopher Ferrisa0196652017-07-18 16:09:20 -0700380void RegsX86::SetFromUcontext(x86_ucontext_t* ucontext) {
381 // Put the registers in the expected order.
382 regs_[X86_REG_EDI] = ucontext->uc_mcontext.edi;
383 regs_[X86_REG_ESI] = ucontext->uc_mcontext.esi;
384 regs_[X86_REG_EBP] = ucontext->uc_mcontext.ebp;
385 regs_[X86_REG_ESP] = ucontext->uc_mcontext.esp;
386 regs_[X86_REG_EBX] = ucontext->uc_mcontext.ebx;
387 regs_[X86_REG_EDX] = ucontext->uc_mcontext.edx;
388 regs_[X86_REG_ECX] = ucontext->uc_mcontext.ecx;
389 regs_[X86_REG_EAX] = ucontext->uc_mcontext.eax;
390 regs_[X86_REG_EIP] = ucontext->uc_mcontext.eip;
391 SetFromRaw();
392}
393
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700394static Regs* CreateFromX86Ucontext(void* ucontext) {
395 x86_ucontext_t* x86_ucontext = reinterpret_cast<x86_ucontext_t*>(ucontext);
396
397 RegsX86* regs = new RegsX86();
Christopher Ferrisa0196652017-07-18 16:09:20 -0700398 regs->SetFromUcontext(x86_ucontext);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700399 return regs;
400}
401
Christopher Ferrisa0196652017-07-18 16:09:20 -0700402void RegsX86_64::SetFromUcontext(x86_64_ucontext_t* ucontext) {
403 // R8-R15
404 memcpy(&regs_[X86_64_REG_R8], &ucontext->uc_mcontext.r8, 8 * sizeof(uint64_t));
405
406 // Rest of the registers.
407 regs_[X86_64_REG_RDI] = ucontext->uc_mcontext.rdi;
408 regs_[X86_64_REG_RSI] = ucontext->uc_mcontext.rsi;
409 regs_[X86_64_REG_RBP] = ucontext->uc_mcontext.rbp;
410 regs_[X86_64_REG_RBX] = ucontext->uc_mcontext.rbx;
411 regs_[X86_64_REG_RDX] = ucontext->uc_mcontext.rdx;
412 regs_[X86_64_REG_RAX] = ucontext->uc_mcontext.rax;
413 regs_[X86_64_REG_RCX] = ucontext->uc_mcontext.rcx;
414 regs_[X86_64_REG_RSP] = ucontext->uc_mcontext.rsp;
415 regs_[X86_64_REG_RIP] = ucontext->uc_mcontext.rip;
416
417 SetFromRaw();
418}
419
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700420static Regs* CreateFromX86_64Ucontext(void* ucontext) {
421 x86_64_ucontext_t* x86_64_ucontext = reinterpret_cast<x86_64_ucontext_t*>(ucontext);
422
423 RegsX86_64* regs = new RegsX86_64();
Christopher Ferrisa0196652017-07-18 16:09:20 -0700424 regs->SetFromUcontext(x86_64_ucontext);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700425 return regs;
426}
427
428Regs* Regs::CreateFromUcontext(uint32_t machine_type, void* ucontext) {
429 switch (machine_type) {
430 case EM_386:
431 return CreateFromX86Ucontext(ucontext);
432 case EM_X86_64:
433 return CreateFromX86_64Ucontext(ucontext);
434 case EM_ARM:
435 return CreateFromArmUcontext(ucontext);
436 case EM_AARCH64:
437 return CreateFromArm64Ucontext(ucontext);
438 }
439 return nullptr;
440}
441
Josh Gao0953ecd2017-08-25 13:55:06 -0700442uint32_t Regs::CurrentMachineType() {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700443#if defined(__arm__)
444 return EM_ARM;
445#elif defined(__aarch64__)
446 return EM_AARCH64;
447#elif defined(__i386__)
448 return EM_386;
449#elif defined(__x86_64__)
450 return EM_X86_64;
451#else
452 abort();
453#endif
454}
455
456Regs* Regs::CreateFromLocal() {
457 Regs* regs;
458#if defined(__arm__)
459 regs = new RegsArm();
460#elif defined(__aarch64__)
461 regs = new RegsArm64();
462#elif defined(__i386__)
463 regs = new RegsX86();
464#elif defined(__x86_64__)
465 regs = new RegsX86_64();
466#else
467 abort();
468#endif
469 return regs;
470}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700471
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700472bool RegsArm::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700473 uint32_t data;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700474 Memory* elf_memory = elf->memory();
475 // Read from elf memory since it is usually more expensive to read from
476 // process memory.
Josh Gaoef35aa52017-10-18 11:44:51 -0700477 if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700478 return false;
479 }
480
481 uint64_t offset = 0;
482 if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
483 // non-RT sigreturn call.
484 // __restore:
485 //
486 // Form 1 (arm):
487 // 0x77 0x70 mov r7, #0x77
488 // 0xa0 0xe3 svc 0x00000000
489 //
490 // Form 2 (arm):
491 // 0x77 0x00 0x90 0xef svc 0x00900077
492 //
493 // Form 3 (thumb):
494 // 0x77 0x27 movs r7, #77
495 // 0x00 0xdf svc 0
Josh Gaoef35aa52017-10-18 11:44:51 -0700496 if (!process_memory->ReadFully(sp(), &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700497 return false;
498 }
499 if (data == 0x5ac3c35a) {
500 // SP + uc_mcontext offset + r0 offset.
501 offset = sp() + 0x14 + 0xc;
502 } else {
503 // SP + r0 offset
504 offset = sp() + 0xc;
505 }
506 } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
507 // RT sigreturn call.
508 // __restore_rt:
509 //
510 // Form 1 (arm):
511 // 0xad 0x70 mov r7, #0xad
512 // 0xa0 0xe3 svc 0x00000000
513 //
514 // Form 2 (arm):
515 // 0xad 0x00 0x90 0xef svc 0x009000ad
516 //
517 // Form 3 (thumb):
518 // 0xad 0x27 movs r7, #ad
519 // 0x00 0xdf svc 0
Josh Gaoef35aa52017-10-18 11:44:51 -0700520 if (!process_memory->ReadFully(sp(), &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700521 return false;
522 }
523 if (data == sp() + 8) {
524 // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
525 offset = sp() + 8 + 0x80 + 0x14 + 0xc;
526 } else {
527 // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
528 offset = sp() + 0x80 + 0x14 + 0xc;
529 }
530 }
531 if (offset == 0) {
532 return false;
533 }
534
Josh Gaoef35aa52017-10-18 11:44:51 -0700535 if (!process_memory->ReadFully(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700536 return false;
537 }
538 SetFromRaw();
539 return true;
540}
541
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700542bool RegsArm64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700543 uint64_t data;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700544 Memory* elf_memory = elf->memory();
545 // Read from elf memory since it is usually more expensive to read from
546 // process memory.
Josh Gaoef35aa52017-10-18 11:44:51 -0700547 if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700548 return false;
549 }
550
551 // Look for the kernel sigreturn function.
552 // __kernel_rt_sigreturn:
553 // 0xd2801168 mov x8, #0x8b
554 // 0xd4000001 svc #0x0
555 if (data != 0xd4000001d2801168ULL) {
556 return false;
557 }
558
559 // SP + sizeof(siginfo_t) + uc_mcontext offset + X0 offset.
Josh Gaoef35aa52017-10-18 11:44:51 -0700560 if (!process_memory->ReadFully(sp() + 0x80 + 0xb0 + 0x08, regs_.data(),
561 sizeof(uint64_t) * ARM64_REG_LAST)) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700562 return false;
563 }
564
565 SetFromRaw();
566 return true;
567}
568
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700569bool RegsX86::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700570 uint64_t data;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700571 Memory* elf_memory = elf->memory();
572 // Read from elf memory since it is usually more expensive to read from
573 // process memory.
Josh Gaoef35aa52017-10-18 11:44:51 -0700574 if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700575 return false;
576 }
577
578 if (data == 0x80cd00000077b858ULL) {
579 // Without SA_SIGINFO set, the return sequence is:
580 //
581 // __restore:
582 // 0x58 pop %eax
583 // 0xb8 0x77 0x00 0x00 0x00 movl 0x77,%eax
584 // 0xcd 0x80 int 0x80
585 //
586 // SP points at arguments:
587 // int signum
588 // struct sigcontext (same format as mcontext)
589 struct x86_mcontext_t context;
Josh Gaoef35aa52017-10-18 11:44:51 -0700590 if (!process_memory->ReadFully(sp() + 4, &context, sizeof(context))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700591 return false;
592 }
593 regs_[X86_REG_EBP] = context.ebp;
594 regs_[X86_REG_ESP] = context.esp;
595 regs_[X86_REG_EBX] = context.ebx;
596 regs_[X86_REG_EDX] = context.edx;
597 regs_[X86_REG_ECX] = context.ecx;
598 regs_[X86_REG_EAX] = context.eax;
599 regs_[X86_REG_EIP] = context.eip;
600 SetFromRaw();
601 return true;
602 } else if ((data & 0x00ffffffffffffffULL) == 0x0080cd000000adb8ULL) {
603 // With SA_SIGINFO set, the return sequence is:
604 //
605 // __restore_rt:
606 // 0xb8 0xad 0x00 0x00 0x00 movl 0xad,%eax
607 // 0xcd 0x80 int 0x80
608 //
609 // SP points at arguments:
610 // int signum
611 // siginfo*
612 // ucontext*
613
614 // Get the location of the sigcontext data.
615 uint32_t ptr;
Josh Gaoef35aa52017-10-18 11:44:51 -0700616 if (!process_memory->ReadFully(sp() + 8, &ptr, sizeof(ptr))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700617 return false;
618 }
619 // Only read the portion of the data structure we care about.
620 x86_ucontext_t x86_ucontext;
Josh Gaoef35aa52017-10-18 11:44:51 -0700621 if (!process_memory->ReadFully(ptr + 0x14, &x86_ucontext.uc_mcontext, sizeof(x86_mcontext_t))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700622 return false;
623 }
624 SetFromUcontext(&x86_ucontext);
625 return true;
626 }
627 return false;
628}
629
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700630bool RegsX86_64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700631 uint64_t data;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700632 Memory* elf_memory = elf->memory();
633 // Read from elf memory since it is usually more expensive to read from
634 // process memory.
Josh Gaoef35aa52017-10-18 11:44:51 -0700635 if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data)) || data != 0x0f0000000fc0c748) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700636 return false;
637 }
638
639 uint16_t data2;
Josh Gaoef35aa52017-10-18 11:44:51 -0700640 if (!elf_memory->ReadFully(rel_pc + 8, &data2, sizeof(data2)) || data2 != 0x0f05) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700641 return false;
642 }
643
644 // __restore_rt:
645 // 0x48 0xc7 0xc0 0x0f 0x00 0x00 0x00 mov $0xf,%rax
646 // 0x0f 0x05 syscall
647 // 0x0f nopl 0x0($rax)
648
649 // Read the mcontext data from the stack.
650 // sp points to the ucontext data structure, read only the mcontext part.
651 x86_64_ucontext_t x86_64_ucontext;
Josh Gaoef35aa52017-10-18 11:44:51 -0700652 if (!process_memory->ReadFully(sp() + 0x28, &x86_64_ucontext.uc_mcontext,
653 sizeof(x86_64_mcontext_t))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700654 return false;
655 }
656 SetFromUcontext(&x86_64_ucontext);
657 return true;
658}
659
Christopher Ferrisd226a512017-07-14 10:37:19 -0700660} // namespace unwindstack