blob: 4d3c246a413eff6021165fdd682d7647b432bd12 [file] [log] [blame]
Christopher Ferrisd06001d2017-11-30 18:56:01 -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
17#include <stdint.h>
18
19#include <functional>
20
21#include <unwindstack/Elf.h>
Christopher Ferris53914162018-02-08 19:27:47 -080022#include <unwindstack/MachineX86.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080023#include <unwindstack/MapInfo.h>
24#include <unwindstack/Memory.h>
25#include <unwindstack/RegsX86.h>
Christopher Ferris53914162018-02-08 19:27:47 -080026#include <unwindstack/UcontextX86.h>
27#include <unwindstack/UserX86.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080028
29namespace unwindstack {
30
Yabin Cui11e96fe2018-03-14 18:16:22 -070031RegsX86::RegsX86() : RegsImpl<uint32_t>(X86_REG_LAST, Location(LOCATION_SP_OFFSET, -4)) {}
Christopher Ferrisd06001d2017-11-30 18:56:01 -080032
33ArchEnum RegsX86::Arch() {
34 return ARCH_X86;
35}
36
Yabin Cui11e96fe2018-03-14 18:16:22 -070037uint64_t RegsX86::pc() {
38 return regs_[X86_REG_PC];
39}
40
41uint64_t RegsX86::sp() {
42 return regs_[X86_REG_SP];
43}
44
45void RegsX86::set_pc(uint64_t pc) {
46 regs_[X86_REG_PC] = static_cast<uint32_t>(pc);
47}
48
49void RegsX86::set_sp(uint64_t sp) {
50 regs_[X86_REG_SP] = static_cast<uint32_t>(sp);
51}
52
Christopher Ferrisd06001d2017-11-30 18:56:01 -080053bool RegsX86::SetPcFromReturnAddress(Memory* process_memory) {
54 // Attempt to get the return address from the top of the stack.
55 uint32_t new_pc;
Yabin Cui11e96fe2018-03-14 18:16:22 -070056 if (!process_memory->ReadFully(regs_[X86_REG_SP], &new_pc, sizeof(new_pc)) ||
57 new_pc == regs_[X86_REG_PC]) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -080058 return false;
59 }
60
Yabin Cui11e96fe2018-03-14 18:16:22 -070061 regs_[X86_REG_PC] = new_pc;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080062 return true;
63}
64
65void RegsX86::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
66 fn("eax", regs_[X86_REG_EAX]);
67 fn("ebx", regs_[X86_REG_EBX]);
68 fn("ecx", regs_[X86_REG_ECX]);
69 fn("edx", regs_[X86_REG_EDX]);
70 fn("ebp", regs_[X86_REG_EBP]);
71 fn("edi", regs_[X86_REG_EDI]);
72 fn("esi", regs_[X86_REG_ESI]);
73 fn("esp", regs_[X86_REG_ESP]);
74 fn("eip", regs_[X86_REG_EIP]);
75}
76
77Regs* RegsX86::Read(void* user_data) {
78 x86_user_regs* user = reinterpret_cast<x86_user_regs*>(user_data);
79
80 RegsX86* regs = new RegsX86();
81 (*regs)[X86_REG_EAX] = user->eax;
82 (*regs)[X86_REG_EBX] = user->ebx;
83 (*regs)[X86_REG_ECX] = user->ecx;
84 (*regs)[X86_REG_EDX] = user->edx;
85 (*regs)[X86_REG_EBP] = user->ebp;
86 (*regs)[X86_REG_EDI] = user->edi;
87 (*regs)[X86_REG_ESI] = user->esi;
88 (*regs)[X86_REG_ESP] = user->esp;
89 (*regs)[X86_REG_EIP] = user->eip;
90
Christopher Ferrisd06001d2017-11-30 18:56:01 -080091 return regs;
92}
93
94void RegsX86::SetFromUcontext(x86_ucontext_t* ucontext) {
95 // Put the registers in the expected order.
96 regs_[X86_REG_EDI] = ucontext->uc_mcontext.edi;
97 regs_[X86_REG_ESI] = ucontext->uc_mcontext.esi;
98 regs_[X86_REG_EBP] = ucontext->uc_mcontext.ebp;
99 regs_[X86_REG_ESP] = ucontext->uc_mcontext.esp;
100 regs_[X86_REG_EBX] = ucontext->uc_mcontext.ebx;
101 regs_[X86_REG_EDX] = ucontext->uc_mcontext.edx;
102 regs_[X86_REG_ECX] = ucontext->uc_mcontext.ecx;
103 regs_[X86_REG_EAX] = ucontext->uc_mcontext.eax;
104 regs_[X86_REG_EIP] = ucontext->uc_mcontext.eip;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800105}
106
107Regs* RegsX86::CreateFromUcontext(void* ucontext) {
108 x86_ucontext_t* x86_ucontext = reinterpret_cast<x86_ucontext_t*>(ucontext);
109
110 RegsX86* regs = new RegsX86();
111 regs->SetFromUcontext(x86_ucontext);
112 return regs;
113}
114
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800115bool RegsX86::StepIfSignalHandler(uint64_t elf_offset, Elf* elf, Memory* process_memory) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800116 uint64_t data;
117 Memory* elf_memory = elf->memory();
118 // Read from elf memory since it is usually more expensive to read from
119 // process memory.
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800120 if (!elf_memory->ReadFully(elf_offset, &data, sizeof(data))) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800121 return false;
122 }
123
124 if (data == 0x80cd00000077b858ULL) {
125 // Without SA_SIGINFO set, the return sequence is:
126 //
127 // __restore:
128 // 0x58 pop %eax
129 // 0xb8 0x77 0x00 0x00 0x00 movl 0x77,%eax
130 // 0xcd 0x80 int 0x80
131 //
132 // SP points at arguments:
133 // int signum
134 // struct sigcontext (same format as mcontext)
135 struct x86_mcontext_t context;
Yabin Cui11e96fe2018-03-14 18:16:22 -0700136 if (!process_memory->ReadFully(regs_[X86_REG_SP] + 4, &context, sizeof(context))) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800137 return false;
138 }
139 regs_[X86_REG_EBP] = context.ebp;
140 regs_[X86_REG_ESP] = context.esp;
141 regs_[X86_REG_EBX] = context.ebx;
142 regs_[X86_REG_EDX] = context.edx;
143 regs_[X86_REG_ECX] = context.ecx;
144 regs_[X86_REG_EAX] = context.eax;
145 regs_[X86_REG_EIP] = context.eip;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800146 return true;
147 } else if ((data & 0x00ffffffffffffffULL) == 0x0080cd000000adb8ULL) {
148 // With SA_SIGINFO set, the return sequence is:
149 //
150 // __restore_rt:
151 // 0xb8 0xad 0x00 0x00 0x00 movl 0xad,%eax
152 // 0xcd 0x80 int 0x80
153 //
154 // SP points at arguments:
155 // int signum
156 // siginfo*
157 // ucontext*
158
159 // Get the location of the sigcontext data.
160 uint32_t ptr;
Yabin Cui11e96fe2018-03-14 18:16:22 -0700161 if (!process_memory->ReadFully(regs_[X86_REG_SP] + 8, &ptr, sizeof(ptr))) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800162 return false;
163 }
164 // Only read the portion of the data structure we care about.
165 x86_ucontext_t x86_ucontext;
166 if (!process_memory->ReadFully(ptr + 0x14, &x86_ucontext.uc_mcontext, sizeof(x86_mcontext_t))) {
167 return false;
168 }
169 SetFromUcontext(&x86_ucontext);
170 return true;
171 }
172 return false;
173}
174
Josh Gao2f37a152018-04-20 11:51:14 -0700175Regs* RegsX86::Clone() {
176 return new RegsX86(*this);
177}
178
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800179} // namespace unwindstack