blob: dea7b87ce78743f21d169d639ac02147a7fee25c [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
Christopher Ferrisa0196652017-07-18 16:09:20 -0700261void RegsX86::SetFromUcontext(x86_ucontext_t* ucontext) {
262 // Put the registers in the expected order.
263 regs_[X86_REG_EDI] = ucontext->uc_mcontext.edi;
264 regs_[X86_REG_ESI] = ucontext->uc_mcontext.esi;
265 regs_[X86_REG_EBP] = ucontext->uc_mcontext.ebp;
266 regs_[X86_REG_ESP] = ucontext->uc_mcontext.esp;
267 regs_[X86_REG_EBX] = ucontext->uc_mcontext.ebx;
268 regs_[X86_REG_EDX] = ucontext->uc_mcontext.edx;
269 regs_[X86_REG_ECX] = ucontext->uc_mcontext.ecx;
270 regs_[X86_REG_EAX] = ucontext->uc_mcontext.eax;
271 regs_[X86_REG_EIP] = ucontext->uc_mcontext.eip;
272 SetFromRaw();
273}
274
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700275static Regs* CreateFromX86Ucontext(void* ucontext) {
276 x86_ucontext_t* x86_ucontext = reinterpret_cast<x86_ucontext_t*>(ucontext);
277
278 RegsX86* regs = new RegsX86();
Christopher Ferrisa0196652017-07-18 16:09:20 -0700279 regs->SetFromUcontext(x86_ucontext);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700280 return regs;
281}
282
Christopher Ferrisa0196652017-07-18 16:09:20 -0700283void RegsX86_64::SetFromUcontext(x86_64_ucontext_t* ucontext) {
284 // R8-R15
285 memcpy(&regs_[X86_64_REG_R8], &ucontext->uc_mcontext.r8, 8 * sizeof(uint64_t));
286
287 // Rest of the registers.
288 regs_[X86_64_REG_RDI] = ucontext->uc_mcontext.rdi;
289 regs_[X86_64_REG_RSI] = ucontext->uc_mcontext.rsi;
290 regs_[X86_64_REG_RBP] = ucontext->uc_mcontext.rbp;
291 regs_[X86_64_REG_RBX] = ucontext->uc_mcontext.rbx;
292 regs_[X86_64_REG_RDX] = ucontext->uc_mcontext.rdx;
293 regs_[X86_64_REG_RAX] = ucontext->uc_mcontext.rax;
294 regs_[X86_64_REG_RCX] = ucontext->uc_mcontext.rcx;
295 regs_[X86_64_REG_RSP] = ucontext->uc_mcontext.rsp;
296 regs_[X86_64_REG_RIP] = ucontext->uc_mcontext.rip;
297
298 SetFromRaw();
299}
300
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700301static Regs* CreateFromX86_64Ucontext(void* ucontext) {
302 x86_64_ucontext_t* x86_64_ucontext = reinterpret_cast<x86_64_ucontext_t*>(ucontext);
303
304 RegsX86_64* regs = new RegsX86_64();
Christopher Ferrisa0196652017-07-18 16:09:20 -0700305 regs->SetFromUcontext(x86_64_ucontext);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700306 return regs;
307}
308
309Regs* Regs::CreateFromUcontext(uint32_t machine_type, void* ucontext) {
310 switch (machine_type) {
311 case EM_386:
312 return CreateFromX86Ucontext(ucontext);
313 case EM_X86_64:
314 return CreateFromX86_64Ucontext(ucontext);
315 case EM_ARM:
316 return CreateFromArmUcontext(ucontext);
317 case EM_AARCH64:
318 return CreateFromArm64Ucontext(ucontext);
319 }
320 return nullptr;
321}
322
323uint32_t Regs::GetMachineType() {
324#if defined(__arm__)
325 return EM_ARM;
326#elif defined(__aarch64__)
327 return EM_AARCH64;
328#elif defined(__i386__)
329 return EM_386;
330#elif defined(__x86_64__)
331 return EM_X86_64;
332#else
333 abort();
334#endif
335}
336
337Regs* Regs::CreateFromLocal() {
338 Regs* regs;
339#if defined(__arm__)
340 regs = new RegsArm();
341#elif defined(__aarch64__)
342 regs = new RegsArm64();
343#elif defined(__i386__)
344 regs = new RegsX86();
345#elif defined(__x86_64__)
346 regs = new RegsX86_64();
347#else
348 abort();
349#endif
350 return regs;
351}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700352
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700353bool RegsArm::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700354 uint32_t data;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700355 Memory* elf_memory = elf->memory();
356 // Read from elf memory since it is usually more expensive to read from
357 // process memory.
358 if (!elf_memory->Read(rel_pc, &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700359 return false;
360 }
361
362 uint64_t offset = 0;
363 if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
364 // non-RT sigreturn call.
365 // __restore:
366 //
367 // Form 1 (arm):
368 // 0x77 0x70 mov r7, #0x77
369 // 0xa0 0xe3 svc 0x00000000
370 //
371 // Form 2 (arm):
372 // 0x77 0x00 0x90 0xef svc 0x00900077
373 //
374 // Form 3 (thumb):
375 // 0x77 0x27 movs r7, #77
376 // 0x00 0xdf svc 0
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700377 if (!process_memory->Read(sp(), &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700378 return false;
379 }
380 if (data == 0x5ac3c35a) {
381 // SP + uc_mcontext offset + r0 offset.
382 offset = sp() + 0x14 + 0xc;
383 } else {
384 // SP + r0 offset
385 offset = sp() + 0xc;
386 }
387 } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
388 // RT sigreturn call.
389 // __restore_rt:
390 //
391 // Form 1 (arm):
392 // 0xad 0x70 mov r7, #0xad
393 // 0xa0 0xe3 svc 0x00000000
394 //
395 // Form 2 (arm):
396 // 0xad 0x00 0x90 0xef svc 0x009000ad
397 //
398 // Form 3 (thumb):
399 // 0xad 0x27 movs r7, #ad
400 // 0x00 0xdf svc 0
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700401 if (!process_memory->Read(sp(), &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700402 return false;
403 }
404 if (data == sp() + 8) {
405 // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
406 offset = sp() + 8 + 0x80 + 0x14 + 0xc;
407 } else {
408 // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
409 offset = sp() + 0x80 + 0x14 + 0xc;
410 }
411 }
412 if (offset == 0) {
413 return false;
414 }
415
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700416 if (!process_memory->Read(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700417 return false;
418 }
419 SetFromRaw();
420 return true;
421}
422
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700423bool RegsArm64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700424 uint64_t data;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700425 Memory* elf_memory = elf->memory();
426 // Read from elf memory since it is usually more expensive to read from
427 // process memory.
428 if (!elf_memory->Read(rel_pc, &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700429 return false;
430 }
431
432 // Look for the kernel sigreturn function.
433 // __kernel_rt_sigreturn:
434 // 0xd2801168 mov x8, #0x8b
435 // 0xd4000001 svc #0x0
436 if (data != 0xd4000001d2801168ULL) {
437 return false;
438 }
439
440 // SP + sizeof(siginfo_t) + uc_mcontext offset + X0 offset.
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700441 if (!process_memory->Read(sp() + 0x80 + 0xb0 + 0x08, regs_.data(),
442 sizeof(uint64_t) * ARM64_REG_LAST)) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700443 return false;
444 }
445
446 SetFromRaw();
447 return true;
448}
449
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700450bool RegsX86::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700451 uint64_t data;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700452 Memory* elf_memory = elf->memory();
453 // Read from elf memory since it is usually more expensive to read from
454 // process memory.
455 if (!elf_memory->Read(rel_pc, &data, sizeof(data))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700456 return false;
457 }
458
459 if (data == 0x80cd00000077b858ULL) {
460 // Without SA_SIGINFO set, the return sequence is:
461 //
462 // __restore:
463 // 0x58 pop %eax
464 // 0xb8 0x77 0x00 0x00 0x00 movl 0x77,%eax
465 // 0xcd 0x80 int 0x80
466 //
467 // SP points at arguments:
468 // int signum
469 // struct sigcontext (same format as mcontext)
470 struct x86_mcontext_t context;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700471 if (!process_memory->Read(sp() + 4, &context, sizeof(context))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700472 return false;
473 }
474 regs_[X86_REG_EBP] = context.ebp;
475 regs_[X86_REG_ESP] = context.esp;
476 regs_[X86_REG_EBX] = context.ebx;
477 regs_[X86_REG_EDX] = context.edx;
478 regs_[X86_REG_ECX] = context.ecx;
479 regs_[X86_REG_EAX] = context.eax;
480 regs_[X86_REG_EIP] = context.eip;
481 SetFromRaw();
482 return true;
483 } else if ((data & 0x00ffffffffffffffULL) == 0x0080cd000000adb8ULL) {
484 // With SA_SIGINFO set, the return sequence is:
485 //
486 // __restore_rt:
487 // 0xb8 0xad 0x00 0x00 0x00 movl 0xad,%eax
488 // 0xcd 0x80 int 0x80
489 //
490 // SP points at arguments:
491 // int signum
492 // siginfo*
493 // ucontext*
494
495 // Get the location of the sigcontext data.
496 uint32_t ptr;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700497 if (!process_memory->Read(sp() + 8, &ptr, sizeof(ptr))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700498 return false;
499 }
500 // Only read the portion of the data structure we care about.
501 x86_ucontext_t x86_ucontext;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700502 if (!process_memory->Read(ptr + 0x14, &x86_ucontext.uc_mcontext, sizeof(x86_mcontext_t))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700503 return false;
504 }
505 SetFromUcontext(&x86_ucontext);
506 return true;
507 }
508 return false;
509}
510
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700511bool RegsX86_64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700512 uint64_t data;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700513 Memory* elf_memory = elf->memory();
514 // Read from elf memory since it is usually more expensive to read from
515 // process memory.
516 if (!elf_memory->Read(rel_pc, &data, sizeof(data)) || data != 0x0f0000000fc0c748) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700517 return false;
518 }
519
520 uint16_t data2;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700521 if (!elf_memory->Read(rel_pc + 8, &data2, sizeof(data2)) || data2 != 0x0f05) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700522 return false;
523 }
524
525 // __restore_rt:
526 // 0x48 0xc7 0xc0 0x0f 0x00 0x00 0x00 mov $0xf,%rax
527 // 0x0f 0x05 syscall
528 // 0x0f nopl 0x0($rax)
529
530 // Read the mcontext data from the stack.
531 // sp points to the ucontext data structure, read only the mcontext part.
532 x86_64_ucontext_t x86_64_ucontext;
Christopher Ferriseb4a6db2017-07-19 12:37:45 -0700533 if (!process_memory->Read(sp() + 0x28, &x86_64_ucontext.uc_mcontext, sizeof(x86_64_mcontext_t))) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700534 return false;
535 }
536 SetFromUcontext(&x86_64_ucontext);
537 return true;
538}
539
Christopher Ferrisd226a512017-07-14 10:37:19 -0700540} // namespace unwindstack