blob: 8c6172e77f99a0ae2ccec670a49df389d9c141e3 [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
Josh Gao0953ecd2017-08-25 13:55:06 -070059uint32_t RegsArm::MachineType() {
60 return EM_ARM;
61}
62
Christopher Ferris3958f802017-02-01 15:44:40 -080063uint64_t RegsArm::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
64 if (!elf->valid()) {
65 return rel_pc;
66 }
67
68 uint64_t load_bias = elf->interface()->load_bias();
69 if (rel_pc < load_bias) {
70 return rel_pc;
71 }
72 uint64_t adjusted_rel_pc = rel_pc - load_bias;
73
74 if (adjusted_rel_pc < 5) {
75 return rel_pc;
76 }
77
78 if (adjusted_rel_pc & 1) {
79 // This is a thumb instruction, it could be 2 or 4 bytes.
80 uint32_t value;
81 if (rel_pc < 5 || !elf->memory()->Read(adjusted_rel_pc - 5, &value, sizeof(value)) ||
82 (value & 0xe000f000) != 0xe000f000) {
83 return rel_pc - 2;
84 }
85 }
86 return rel_pc - 4;
87}
88
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070089void RegsArm::SetFromRaw() {
90 set_pc(regs_[ARM_REG_PC]);
91 set_sp(regs_[ARM_REG_SP]);
92}
93
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070094bool RegsArm::SetPcFromReturnAddress(Memory*) {
95 if (pc() == regs_[ARM_REG_LR]) {
96 return false;
97 }
98
99 set_pc(regs_[ARM_REG_LR]);
100 return true;
101}
102
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700103RegsArm64::RegsArm64()
104 : RegsImpl<uint64_t>(ARM64_REG_LAST, ARM64_REG_SP, Location(LOCATION_REGISTER, ARM64_REG_LR)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800105
Josh Gao0953ecd2017-08-25 13:55:06 -0700106uint32_t RegsArm64::MachineType() {
107 return EM_AARCH64;
108}
109
Christopher Ferris3958f802017-02-01 15:44:40 -0800110uint64_t RegsArm64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
111 if (!elf->valid()) {
112 return rel_pc;
113 }
114
115 if (rel_pc < 4) {
116 return rel_pc;
117 }
118 return rel_pc - 4;
119}
120
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700121void RegsArm64::SetFromRaw() {
122 set_pc(regs_[ARM64_REG_PC]);
123 set_sp(regs_[ARM64_REG_SP]);
124}
125
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700126bool RegsArm64::SetPcFromReturnAddress(Memory*) {
127 if (pc() == regs_[ARM64_REG_LR]) {
128 return false;
129 }
130
131 set_pc(regs_[ARM64_REG_LR]);
132 return true;
133}
134
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700135RegsX86::RegsX86()
136 : RegsImpl<uint32_t>(X86_REG_LAST, X86_REG_SP, Location(LOCATION_SP_OFFSET, -4)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800137
Josh Gao0953ecd2017-08-25 13:55:06 -0700138uint32_t RegsX86::MachineType() {
139 return EM_386;
140}
141
Christopher Ferris3958f802017-02-01 15:44:40 -0800142uint64_t RegsX86::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
143 if (!elf->valid()) {
144 return rel_pc;
145 }
146
147 if (rel_pc == 0) {
148 return 0;
149 }
150 return rel_pc - 1;
151}
152
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700153void RegsX86::SetFromRaw() {
154 set_pc(regs_[X86_REG_PC]);
155 set_sp(regs_[X86_REG_SP]);
156}
157
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700158bool RegsX86::SetPcFromReturnAddress(Memory* process_memory) {
159 // Attempt to get the return address from the top of the stack.
160 uint32_t new_pc;
161 if (!process_memory->Read(sp_, &new_pc, sizeof(new_pc)) || new_pc == pc()) {
162 return false;
163 }
164
165 set_pc(new_pc);
166 return true;
167}
168
Christopher Ferris7b8e4672017-06-01 17:55:25 -0700169RegsX86_64::RegsX86_64()
170 : RegsImpl<uint64_t>(X86_64_REG_LAST, X86_64_REG_SP, Location(LOCATION_SP_OFFSET, -8)) {}
Christopher Ferris3958f802017-02-01 15:44:40 -0800171
Josh Gao0953ecd2017-08-25 13:55:06 -0700172uint32_t RegsX86_64::MachineType() {
173 return EM_X86_64;
174}
175
Christopher Ferris3958f802017-02-01 15:44:40 -0800176uint64_t RegsX86_64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) {
177 if (!elf->valid()) {
178 return rel_pc;
179 }
180
181 if (rel_pc == 0) {
182 return 0;
183 }
184
185 return rel_pc - 1;
186}
187
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700188void RegsX86_64::SetFromRaw() {
189 set_pc(regs_[X86_64_REG_PC]);
190 set_sp(regs_[X86_64_REG_SP]);
191}
192
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700193bool RegsX86_64::SetPcFromReturnAddress(Memory* process_memory) {
194 // Attempt to get the return address from the top of the stack.
195 uint64_t new_pc;
196 if (!process_memory->Read(sp_, &new_pc, sizeof(new_pc)) || new_pc == pc()) {
197 return false;
198 }
199
200 set_pc(new_pc);
201 return true;
202}
203
Christopher Ferris3958f802017-02-01 15:44:40 -0800204static Regs* ReadArm(void* remote_data) {
205 arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data);
206
207 RegsArm* regs = new RegsArm();
208 memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700209 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800210 return regs;
211}
212
213static Regs* ReadArm64(void* remote_data) {
214 arm64_user_regs* user = reinterpret_cast<arm64_user_regs*>(remote_data);
215
216 RegsArm64* regs = new RegsArm64();
217 memcpy(regs->RawData(), &user->regs[0], (ARM64_REG_R31 + 1) * sizeof(uint64_t));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700218 uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
219 reg_data[ARM64_REG_PC] = user->pc;
220 reg_data[ARM64_REG_SP] = user->sp;
221 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800222 return regs;
223}
224
225static Regs* ReadX86(void* remote_data) {
226 x86_user_regs* user = reinterpret_cast<x86_user_regs*>(remote_data);
227
228 RegsX86* regs = new RegsX86();
229 (*regs)[X86_REG_EAX] = user->eax;
230 (*regs)[X86_REG_EBX] = user->ebx;
231 (*regs)[X86_REG_ECX] = user->ecx;
232 (*regs)[X86_REG_EDX] = user->edx;
233 (*regs)[X86_REG_EBP] = user->ebp;
234 (*regs)[X86_REG_EDI] = user->edi;
235 (*regs)[X86_REG_ESI] = user->esi;
236 (*regs)[X86_REG_ESP] = user->esp;
237 (*regs)[X86_REG_EIP] = user->eip;
238
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700239 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800240 return regs;
241}
242
243static Regs* ReadX86_64(void* remote_data) {
244 x86_64_user_regs* user = reinterpret_cast<x86_64_user_regs*>(remote_data);
245
246 RegsX86_64* regs = new RegsX86_64();
247 (*regs)[X86_64_REG_RAX] = user->rax;
248 (*regs)[X86_64_REG_RBX] = user->rbx;
249 (*regs)[X86_64_REG_RCX] = user->rcx;
250 (*regs)[X86_64_REG_RDX] = user->rdx;
251 (*regs)[X86_64_REG_R8] = user->r8;
252 (*regs)[X86_64_REG_R9] = user->r9;
253 (*regs)[X86_64_REG_R10] = user->r10;
254 (*regs)[X86_64_REG_R11] = user->r11;
255 (*regs)[X86_64_REG_R12] = user->r12;
256 (*regs)[X86_64_REG_R13] = user->r13;
257 (*regs)[X86_64_REG_R14] = user->r14;
258 (*regs)[X86_64_REG_R15] = user->r15;
259 (*regs)[X86_64_REG_RDI] = user->rdi;
260 (*regs)[X86_64_REG_RSI] = user->rsi;
261 (*regs)[X86_64_REG_RBP] = user->rbp;
262 (*regs)[X86_64_REG_RSP] = user->rsp;
263 (*regs)[X86_64_REG_RIP] = user->rip;
264
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700265 regs->SetFromRaw();
Christopher Ferris3958f802017-02-01 15:44:40 -0800266 return regs;
267}
268
269// This function assumes that reg_data is already aligned to a 64 bit value.
270// If not this could crash with an unaligned access.
Josh Gao0953ecd2017-08-25 13:55:06 -0700271Regs* Regs::RemoteGet(pid_t pid) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800272 // Make the buffer large enough to contain the largest registers type.
273 std::vector<uint64_t> buffer(MAX_USER_REGS_SIZE / sizeof(uint64_t));
274 struct iovec io;
275 io.iov_base = buffer.data();
276 io.iov_len = buffer.size() * sizeof(uint64_t);
277
278 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, reinterpret_cast<void*>(&io)) == -1) {
279 return nullptr;
280 }
281
282 switch (io.iov_len) {
283 case sizeof(x86_user_regs):
Christopher Ferris3958f802017-02-01 15:44:40 -0800284 return ReadX86(buffer.data());
285 case sizeof(x86_64_user_regs):
Christopher Ferris3958f802017-02-01 15:44:40 -0800286 return ReadX86_64(buffer.data());
287 case sizeof(arm_user_regs):
Christopher Ferris3958f802017-02-01 15:44:40 -0800288 return ReadArm(buffer.data());
289 case sizeof(arm64_user_regs):
Christopher Ferris3958f802017-02-01 15:44:40 -0800290 return ReadArm64(buffer.data());
291 }
292 return nullptr;
293}
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700294
295static Regs* CreateFromArmUcontext(void* ucontext) {
296 arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext);
297
298 RegsArm* regs = new RegsArm();
299 memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t));
300 regs->SetFromRaw();
301 return regs;
302}
303
304static Regs* CreateFromArm64Ucontext(void* ucontext) {
305 arm64_ucontext_t* arm64_ucontext = reinterpret_cast<arm64_ucontext_t*>(ucontext);
306
307 RegsArm64* regs = new RegsArm64();
308 memcpy(regs->RawData(), &arm64_ucontext->uc_mcontext.regs[0], ARM64_REG_LAST * sizeof(uint64_t));
309 regs->SetFromRaw();
310 return regs;
311}
312
Christopher Ferrisa0196652017-07-18 16:09:20 -0700313void RegsX86::SetFromUcontext(x86_ucontext_t* ucontext) {
314 // Put the registers in the expected order.
315 regs_[X86_REG_EDI] = ucontext->uc_mcontext.edi;
316 regs_[X86_REG_ESI] = ucontext->uc_mcontext.esi;
317 regs_[X86_REG_EBP] = ucontext->uc_mcontext.ebp;
318 regs_[X86_REG_ESP] = ucontext->uc_mcontext.esp;
319 regs_[X86_REG_EBX] = ucontext->uc_mcontext.ebx;
320 regs_[X86_REG_EDX] = ucontext->uc_mcontext.edx;
321 regs_[X86_REG_ECX] = ucontext->uc_mcontext.ecx;
322 regs_[X86_REG_EAX] = ucontext->uc_mcontext.eax;
323 regs_[X86_REG_EIP] = ucontext->uc_mcontext.eip;
324 SetFromRaw();
325}
326
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700327static Regs* CreateFromX86Ucontext(void* ucontext) {
328 x86_ucontext_t* x86_ucontext = reinterpret_cast<x86_ucontext_t*>(ucontext);
329
330 RegsX86* regs = new RegsX86();
Christopher Ferrisa0196652017-07-18 16:09:20 -0700331 regs->SetFromUcontext(x86_ucontext);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700332 return regs;
333}
334
Christopher Ferrisa0196652017-07-18 16:09:20 -0700335void RegsX86_64::SetFromUcontext(x86_64_ucontext_t* ucontext) {
336 // R8-R15
337 memcpy(&regs_[X86_64_REG_R8], &ucontext->uc_mcontext.r8, 8 * sizeof(uint64_t));
338
339 // Rest of the registers.
340 regs_[X86_64_REG_RDI] = ucontext->uc_mcontext.rdi;
341 regs_[X86_64_REG_RSI] = ucontext->uc_mcontext.rsi;
342 regs_[X86_64_REG_RBP] = ucontext->uc_mcontext.rbp;
343 regs_[X86_64_REG_RBX] = ucontext->uc_mcontext.rbx;
344 regs_[X86_64_REG_RDX] = ucontext->uc_mcontext.rdx;
345 regs_[X86_64_REG_RAX] = ucontext->uc_mcontext.rax;
346 regs_[X86_64_REG_RCX] = ucontext->uc_mcontext.rcx;
347 regs_[X86_64_REG_RSP] = ucontext->uc_mcontext.rsp;
348 regs_[X86_64_REG_RIP] = ucontext->uc_mcontext.rip;
349
350 SetFromRaw();
351}
352
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700353static Regs* CreateFromX86_64Ucontext(void* ucontext) {
354 x86_64_ucontext_t* x86_64_ucontext = reinterpret_cast<x86_64_ucontext_t*>(ucontext);
355
356 RegsX86_64* regs = new RegsX86_64();
Christopher Ferrisa0196652017-07-18 16:09:20 -0700357 regs->SetFromUcontext(x86_64_ucontext);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700358 return regs;
359}
360
361Regs* Regs::CreateFromUcontext(uint32_t machine_type, void* ucontext) {
362 switch (machine_type) {
363 case EM_386:
364 return CreateFromX86Ucontext(ucontext);
365 case EM_X86_64:
366 return CreateFromX86_64Ucontext(ucontext);
367 case EM_ARM:
368 return CreateFromArmUcontext(ucontext);
369 case EM_AARCH64:
370 return CreateFromArm64Ucontext(ucontext);
371 }
372 return nullptr;
373}
374
Josh Gao0953ecd2017-08-25 13:55:06 -0700375uint32_t Regs::CurrentMachineType() {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700376#if defined(__arm__)
377 return EM_ARM;
378#elif defined(__aarch64__)
379 return EM_AARCH64;
380#elif defined(__i386__)
381 return EM_386;
382#elif defined(__x86_64__)
383 return EM_X86_64;
384#else
385 abort();
386#endif
387}
388
389Regs* Regs::CreateFromLocal() {
390 Regs* regs;
391#if defined(__arm__)
392 regs = new RegsArm();
393#elif defined(__aarch64__)
394 regs = new RegsArm64();
395#elif defined(__i386__)
396 regs = new RegsX86();
397#elif defined(__x86_64__)
398 regs = new RegsX86_64();
399#else
400 abort();
401#endif
402 return regs;
403}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700404
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700405bool RegsArm::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700406 uint32_t data;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700407 Memory* elf_memory = elf->memory();
408 // Read from elf memory since it is usually more expensive to read from
409 // process memory.
410 if (!elf_memory->Read(rel_pc, &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700411 return false;
412 }
413
414 uint64_t offset = 0;
415 if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
416 // non-RT sigreturn call.
417 // __restore:
418 //
419 // Form 1 (arm):
420 // 0x77 0x70 mov r7, #0x77
421 // 0xa0 0xe3 svc 0x00000000
422 //
423 // Form 2 (arm):
424 // 0x77 0x00 0x90 0xef svc 0x00900077
425 //
426 // Form 3 (thumb):
427 // 0x77 0x27 movs r7, #77
428 // 0x00 0xdf svc 0
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700429 if (!process_memory->Read(sp(), &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700430 return false;
431 }
432 if (data == 0x5ac3c35a) {
433 // SP + uc_mcontext offset + r0 offset.
434 offset = sp() + 0x14 + 0xc;
435 } else {
436 // SP + r0 offset
437 offset = sp() + 0xc;
438 }
439 } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
440 // RT sigreturn call.
441 // __restore_rt:
442 //
443 // Form 1 (arm):
444 // 0xad 0x70 mov r7, #0xad
445 // 0xa0 0xe3 svc 0x00000000
446 //
447 // Form 2 (arm):
448 // 0xad 0x00 0x90 0xef svc 0x009000ad
449 //
450 // Form 3 (thumb):
451 // 0xad 0x27 movs r7, #ad
452 // 0x00 0xdf svc 0
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700453 if (!process_memory->Read(sp(), &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700454 return false;
455 }
456 if (data == sp() + 8) {
457 // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
458 offset = sp() + 8 + 0x80 + 0x14 + 0xc;
459 } else {
460 // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
461 offset = sp() + 0x80 + 0x14 + 0xc;
462 }
463 }
464 if (offset == 0) {
465 return false;
466 }
467
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700468 if (!process_memory->Read(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700469 return false;
470 }
471 SetFromRaw();
472 return true;
473}
474
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700475bool RegsArm64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700476 uint64_t data;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700477 Memory* elf_memory = elf->memory();
478 // Read from elf memory since it is usually more expensive to read from
479 // process memory.
480 if (!elf_memory->Read(rel_pc, &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700481 return false;
482 }
483
484 // Look for the kernel sigreturn function.
485 // __kernel_rt_sigreturn:
486 // 0xd2801168 mov x8, #0x8b
487 // 0xd4000001 svc #0x0
488 if (data != 0xd4000001d2801168ULL) {
489 return false;
490 }
491
492 // SP + sizeof(siginfo_t) + uc_mcontext offset + X0 offset.
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700493 if (!process_memory->Read(sp() + 0x80 + 0xb0 + 0x08, regs_.data(),
494 sizeof(uint64_t) * ARM64_REG_LAST)) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700495 return false;
496 }
497
498 SetFromRaw();
499 return true;
500}
501
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700502bool RegsX86::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700503 uint64_t data;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700504 Memory* elf_memory = elf->memory();
505 // Read from elf memory since it is usually more expensive to read from
506 // process memory.
507 if (!elf_memory->Read(rel_pc, &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700508 return false;
509 }
510
511 if (data == 0x80cd00000077b858ULL) {
512 // Without SA_SIGINFO set, the return sequence is:
513 //
514 // __restore:
515 // 0x58 pop %eax
516 // 0xb8 0x77 0x00 0x00 0x00 movl 0x77,%eax
517 // 0xcd 0x80 int 0x80
518 //
519 // SP points at arguments:
520 // int signum
521 // struct sigcontext (same format as mcontext)
522 struct x86_mcontext_t context;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700523 if (!process_memory->Read(sp() + 4, &context, sizeof(context))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700524 return false;
525 }
526 regs_[X86_REG_EBP] = context.ebp;
527 regs_[X86_REG_ESP] = context.esp;
528 regs_[X86_REG_EBX] = context.ebx;
529 regs_[X86_REG_EDX] = context.edx;
530 regs_[X86_REG_ECX] = context.ecx;
531 regs_[X86_REG_EAX] = context.eax;
532 regs_[X86_REG_EIP] = context.eip;
533 SetFromRaw();
534 return true;
535 } else if ((data & 0x00ffffffffffffffULL) == 0x0080cd000000adb8ULL) {
536 // With SA_SIGINFO set, the return sequence is:
537 //
538 // __restore_rt:
539 // 0xb8 0xad 0x00 0x00 0x00 movl 0xad,%eax
540 // 0xcd 0x80 int 0x80
541 //
542 // SP points at arguments:
543 // int signum
544 // siginfo*
545 // ucontext*
546
547 // Get the location of the sigcontext data.
548 uint32_t ptr;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700549 if (!process_memory->Read(sp() + 8, &ptr, sizeof(ptr))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700550 return false;
551 }
552 // Only read the portion of the data structure we care about.
553 x86_ucontext_t x86_ucontext;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700554 if (!process_memory->Read(ptr + 0x14, &x86_ucontext.uc_mcontext, sizeof(x86_mcontext_t))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700555 return false;
556 }
557 SetFromUcontext(&x86_ucontext);
558 return true;
559 }
560 return false;
561}
562
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700563bool RegsX86_64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700564 uint64_t data;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700565 Memory* elf_memory = elf->memory();
566 // Read from elf memory since it is usually more expensive to read from
567 // process memory.
568 if (!elf_memory->Read(rel_pc, &data, sizeof(data)) || data != 0x0f0000000fc0c748) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700569 return false;
570 }
571
572 uint16_t data2;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700573 if (!elf_memory->Read(rel_pc + 8, &data2, sizeof(data2)) || data2 != 0x0f05) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700574 return false;
575 }
576
577 // __restore_rt:
578 // 0x48 0xc7 0xc0 0x0f 0x00 0x00 0x00 mov $0xf,%rax
579 // 0x0f 0x05 syscall
580 // 0x0f nopl 0x0($rax)
581
582 // Read the mcontext data from the stack.
583 // sp points to the ucontext data structure, read only the mcontext part.
584 x86_64_ucontext_t x86_64_ucontext;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700585 if (!process_memory->Read(sp() + 0x28, &x86_64_ucontext.uc_mcontext, sizeof(x86_64_mcontext_t))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700586 return false;
587 }
588 SetFromUcontext(&x86_64_ucontext);
589 return true;
590}
591
Christopher Ferrisd226a512017-07-14 10:37:19 -0700592} // namespace unwindstack