blob: 09c6e047d0d3218603b52e235e6231e5919a3590 [file] [log] [blame]
Christopher Ferrisf6f691b2017-09-25 19:23:07 -07001/*
2 * Copyright (C) 2017 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 <elf.h>
18#include <stdint.h>
19#include <sys/mman.h>
20
21#include <memory>
22#include <set>
23#include <string>
24
25#include <gtest/gtest.h>
26
27#include <unwindstack/Elf.h>
28#include <unwindstack/Maps.h>
29#include <unwindstack/Memory.h>
30#include <unwindstack/Regs.h>
Christopher Ferris02fdb562017-12-08 15:04:49 -080031#include <unwindstack/RegsArm.h>
32#include <unwindstack/RegsArm64.h>
33#include <unwindstack/RegsX86.h>
34#include <unwindstack/RegsX86_64.h>
Douglas Leung61b1a1a2017-11-08 10:53:53 +010035#include <unwindstack/RegsMips.h>
36#include <unwindstack/RegsMips64.h>
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070037#include <unwindstack/Unwinder.h>
38
39#include "ElfFake.h"
40#include "MemoryFake.h"
41#include "RegsFake.h"
42
43namespace unwindstack {
44
45class MapsFake : public Maps {
46 public:
47 MapsFake() = default;
48 virtual ~MapsFake() = default;
49
50 bool Parse() { return true; }
51
52 void FakeClear() { maps_.clear(); }
53
Christopher Ferrisbe788d82017-11-27 14:50:38 -080054 void FakeAddMapInfo(MapInfo* map_info) { maps_.push_back(map_info); }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070055};
56
57class UnwinderTest : public ::testing::Test {
58 protected:
59 static void SetUpTestCase() {
60 maps_.FakeClear();
Christopher Ferrisbe788d82017-11-27 14:50:38 -080061 MapInfo* info = new MapInfo(0x1000, 0x8000, 0, PROT_READ | PROT_WRITE, "/system/fake/libc.so");
Christopher Ferris02fdb562017-12-08 15:04:49 -080062 ElfFake* elf = new ElfFake(new MemoryFake);
Christopher Ferris0b79ae12018-01-25 12:15:56 -080063 info->elf.reset(elf);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070064 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070065 maps_.FakeAddMapInfo(info);
66
Christopher Ferrisbe788d82017-11-27 14:50:38 -080067 info = new MapInfo(0x10000, 0x12000, 0, PROT_READ | PROT_WRITE, "[stack]");
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070068 maps_.FakeAddMapInfo(info);
69
Christopher Ferrisbe788d82017-11-27 14:50:38 -080070 info = new MapInfo(0x13000, 0x15000, 0, PROT_READ | PROT_WRITE | MAPS_FLAGS_DEVICE_MAP,
71 "/dev/fake_device");
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070072 maps_.FakeAddMapInfo(info);
73
Christopher Ferrisbe788d82017-11-27 14:50:38 -080074 info = new MapInfo(0x20000, 0x22000, 0, PROT_READ | PROT_WRITE, "/system/fake/libunwind.so");
Christopher Ferris02fdb562017-12-08 15:04:49 -080075 elf = new ElfFake(new MemoryFake);
Christopher Ferris0b79ae12018-01-25 12:15:56 -080076 info->elf.reset(elf);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070077 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
78 maps_.FakeAddMapInfo(info);
79
Christopher Ferrisbe788d82017-11-27 14:50:38 -080080 info = new MapInfo(0x23000, 0x24000, 0, PROT_READ | PROT_WRITE, "/fake/libanother.so");
Christopher Ferris02fdb562017-12-08 15:04:49 -080081 elf = new ElfFake(new MemoryFake);
Christopher Ferris0b79ae12018-01-25 12:15:56 -080082 info->elf.reset(elf);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070083 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
84 maps_.FakeAddMapInfo(info);
Christopher Ferrisc5a3baa2017-10-17 18:42:03 -070085
Christopher Ferrisbe788d82017-11-27 14:50:38 -080086 info = new MapInfo(0x33000, 0x34000, 0, PROT_READ | PROT_WRITE, "/fake/compressed.so");
Christopher Ferris02fdb562017-12-08 15:04:49 -080087 elf = new ElfFake(new MemoryFake);
Christopher Ferris0b79ae12018-01-25 12:15:56 -080088 info->elf.reset(elf);
Christopher Ferrise69f4702017-10-19 16:08:58 -070089 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
90 maps_.FakeAddMapInfo(info);
91
Christopher Ferrisbe788d82017-11-27 14:50:38 -080092 info = new MapInfo(0x43000, 0x44000, 0x1d000, PROT_READ | PROT_WRITE, "/fake/fake.apk");
Christopher Ferris02fdb562017-12-08 15:04:49 -080093 elf = new ElfFake(new MemoryFake);
Christopher Ferris0b79ae12018-01-25 12:15:56 -080094 info->elf.reset(elf);
Christopher Ferrisc5a3baa2017-10-17 18:42:03 -070095 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
96 maps_.FakeAddMapInfo(info);
Christopher Ferrise69f4702017-10-19 16:08:58 -070097
Christopher Ferrisbe788d82017-11-27 14:50:38 -080098 info = new MapInfo(0x53000, 0x54000, 0, PROT_READ | PROT_WRITE, "/fake/fake.oat");
Christopher Ferrise69f4702017-10-19 16:08:58 -070099 maps_.FakeAddMapInfo(info);
Christopher Ferris02fdb562017-12-08 15:04:49 -0800100
Christopher Ferrise762f1f2018-02-06 14:51:48 -0800101 info = new MapInfo(0xa3000, 0xa4000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/fake.vdex");
102 info->load_bias = 0;
103 maps_.FakeAddMapInfo(info);
104
Christopher Ferris02fdb562017-12-08 15:04:49 -0800105 process_memory_.reset(new MemoryFake);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700106 }
107
108 void SetUp() override {
109 ElfInterfaceFake::FakeClear();
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800110 regs_.FakeSetArch(ARCH_ARM);
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700111 regs_.FakeSetReturnAddressValid(false);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700112 }
113
114 static MapsFake maps_;
115 static RegsFake regs_;
116 static std::shared_ptr<Memory> process_memory_;
117};
118
119MapsFake UnwinderTest::maps_;
120RegsFake UnwinderTest::regs_(5, 0);
121std::shared_ptr<Memory> UnwinderTest::process_memory_(nullptr);
122
123TEST_F(UnwinderTest, multiple_frames) {
124 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
125 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
126 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
127
128 regs_.FakeSetPc(0x1000);
129 regs_.FakeSetSp(0x10000);
130 ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
131 ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
132 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
133
134 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
135 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800136 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700137
138 ASSERT_EQ(3U, unwinder.NumFrames());
139
140 auto* frame = &unwinder.frames()[0];
141 EXPECT_EQ(0U, frame->num);
142 EXPECT_EQ(0U, frame->rel_pc);
143 EXPECT_EQ(0x1000U, frame->pc);
144 EXPECT_EQ(0x10000U, frame->sp);
145 EXPECT_EQ("Frame0", frame->function_name);
146 EXPECT_EQ(0U, frame->function_offset);
147 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
148 EXPECT_EQ(0U, frame->map_offset);
149 EXPECT_EQ(0x1000U, frame->map_start);
150 EXPECT_EQ(0x8000U, frame->map_end);
151 EXPECT_EQ(0U, frame->map_load_bias);
152 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
153
154 frame = &unwinder.frames()[1];
155 EXPECT_EQ(1U, frame->num);
156 EXPECT_EQ(0x100U, frame->rel_pc);
157 EXPECT_EQ(0x1100U, frame->pc);
158 EXPECT_EQ(0x10010U, frame->sp);
159 EXPECT_EQ("Frame1", frame->function_name);
160 EXPECT_EQ(1U, frame->function_offset);
161 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
162 EXPECT_EQ(0U, frame->map_offset);
163 EXPECT_EQ(0x1000U, frame->map_start);
164 EXPECT_EQ(0x8000U, frame->map_end);
165 EXPECT_EQ(0U, frame->map_load_bias);
166 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
167
168 frame = &unwinder.frames()[2];
169 EXPECT_EQ(2U, frame->num);
170 EXPECT_EQ(0x200U, frame->rel_pc);
171 EXPECT_EQ(0x1200U, frame->pc);
172 EXPECT_EQ(0x10020U, frame->sp);
173 EXPECT_EQ("Frame2", frame->function_name);
174 EXPECT_EQ(2U, frame->function_offset);
175 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
176 EXPECT_EQ(0U, frame->map_offset);
177 EXPECT_EQ(0x1000U, frame->map_start);
178 EXPECT_EQ(0x8000U, frame->map_end);
179 EXPECT_EQ(0U, frame->map_load_bias);
180 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
181}
182
Christopher Ferrisc5a3baa2017-10-17 18:42:03 -0700183TEST_F(UnwinderTest, non_zero_map_offset) {
184 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
185
186 regs_.FakeSetPc(0x43000);
187 regs_.FakeSetSp(0x10000);
188 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
189
190 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
191 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800192 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisc5a3baa2017-10-17 18:42:03 -0700193
194 ASSERT_EQ(1U, unwinder.NumFrames());
195
196 auto* frame = &unwinder.frames()[0];
197 EXPECT_EQ(0U, frame->num);
198 EXPECT_EQ(0U, frame->rel_pc);
199 EXPECT_EQ(0x43000U, frame->pc);
200 EXPECT_EQ(0x10000U, frame->sp);
201 EXPECT_EQ("Frame0", frame->function_name);
202 EXPECT_EQ(0U, frame->function_offset);
203 EXPECT_EQ("/fake/fake.apk", frame->map_name);
204 EXPECT_EQ(0x1d000U, frame->map_offset);
205 EXPECT_EQ(0x43000U, frame->map_start);
206 EXPECT_EQ(0x44000U, frame->map_end);
207 EXPECT_EQ(0U, frame->map_load_bias);
208 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
209}
210
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700211// Verify that no attempt to continue after the step indicates it is done.
212TEST_F(UnwinderTest, no_frames_after_finished) {
213 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
214 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
215 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
216 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
217 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
218
219 regs_.FakeSetPc(0x1000);
220 regs_.FakeSetSp(0x10000);
221 ElfInterfaceFake::FakePushStepData(StepData(0x1000, 0x10000, true));
222 ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
223 ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
224
225 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
226 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800227 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700228
229 ASSERT_EQ(1U, unwinder.NumFrames());
230
231 auto* frame = &unwinder.frames()[0];
232 EXPECT_EQ(0U, frame->num);
233 EXPECT_EQ(0U, frame->rel_pc);
234 EXPECT_EQ(0x1000U, frame->pc);
235 EXPECT_EQ(0x10000U, frame->sp);
236 EXPECT_EQ("Frame0", frame->function_name);
237 EXPECT_EQ(0U, frame->function_offset);
238 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
239 EXPECT_EQ(0U, frame->map_offset);
240 EXPECT_EQ(0x1000U, frame->map_start);
241 EXPECT_EQ(0x8000U, frame->map_end);
242 EXPECT_EQ(0U, frame->map_load_bias);
243 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
244}
245
246// Verify the maximum frames to save.
247TEST_F(UnwinderTest, max_frames) {
248 for (size_t i = 0; i < 30; i++) {
249 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame" + std::to_string(i), i));
250 ElfInterfaceFake::FakePushStepData(StepData(0x1102 + i * 0x100, 0x10010 + i * 0x10, false));
251 }
252
253 regs_.FakeSetPc(0x1000);
254 regs_.FakeSetSp(0x10000);
255
256 Unwinder unwinder(20, &maps_, &regs_, process_memory_);
257 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800258 EXPECT_EQ(ERROR_MAX_FRAMES_EXCEEDED, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700259
260 ASSERT_EQ(20U, unwinder.NumFrames());
261
262 for (size_t i = 0; i < 20; i++) {
263 auto* frame = &unwinder.frames()[i];
264 EXPECT_EQ(i, frame->num);
265 EXPECT_EQ(i * 0x100, frame->rel_pc) << "Failed at frame " << i;
266 EXPECT_EQ(0x1000 + i * 0x100, frame->pc) << "Failed at frame " << i;
267 EXPECT_EQ(0x10000 + 0x10 * i, frame->sp) << "Failed at frame " << i;
268 EXPECT_EQ("Frame" + std::to_string(i), frame->function_name) << "Failed at frame " << i;
269 EXPECT_EQ(i, frame->function_offset) << "Failed at frame " << i;
270 EXPECT_EQ("/system/fake/libc.so", frame->map_name) << "Failed at frame " << i;
271 EXPECT_EQ(0U, frame->map_offset) << "Failed at frame " << i;
272 EXPECT_EQ(0x1000U, frame->map_start) << "Failed at frame " << i;
273 EXPECT_EQ(0x8000U, frame->map_end) << "Failed at frame " << i;
274 EXPECT_EQ(0U, frame->map_load_bias) << "Failed at frame " << i;
275 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags) << "Failed at frame " << i;
276 }
277}
278
279// Verify that initial map names frames are removed.
280TEST_F(UnwinderTest, verify_frames_skipped) {
281 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
282 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
283 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
284
285 regs_.FakeSetPc(0x20000);
286 regs_.FakeSetSp(0x10000);
287 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
288 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
289 ElfInterfaceFake::FakePushStepData(StepData(0x20002, 0x10030, false));
290 ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x10040, false));
291 ElfInterfaceFake::FakePushStepData(StepData(0x1002, 0x10050, false));
292 ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x10060, false));
293 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10070, false));
294 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
295
296 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
Christopher Ferris1cb84ce2017-10-24 15:36:00 -0700297 std::vector<std::string> skip_libs{"libunwind.so", "libanother.so"};
298 unwinder.Unwind(&skip_libs);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800299 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700300
301 ASSERT_EQ(3U, unwinder.NumFrames());
302
303 auto* frame = &unwinder.frames()[0];
304 EXPECT_EQ(0U, frame->num);
305 EXPECT_EQ(0U, frame->rel_pc);
306 EXPECT_EQ(0x1000U, frame->pc);
307 EXPECT_EQ(0x10050U, frame->sp);
308 EXPECT_EQ("Frame0", frame->function_name);
309 EXPECT_EQ(0U, frame->function_offset);
310 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
311 EXPECT_EQ(0U, frame->map_offset);
312 EXPECT_EQ(0x1000U, frame->map_start);
313 EXPECT_EQ(0x8000U, frame->map_end);
314 EXPECT_EQ(0U, frame->map_load_bias);
315 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
316
317 frame = &unwinder.frames()[1];
318 EXPECT_EQ(1U, frame->num);
319 EXPECT_EQ(0x1000U, frame->rel_pc);
320 EXPECT_EQ(0x21000U, frame->pc);
321 EXPECT_EQ(0x10060U, frame->sp);
322 EXPECT_EQ("Frame1", frame->function_name);
323 EXPECT_EQ(1U, frame->function_offset);
324 EXPECT_EQ("/system/fake/libunwind.so", frame->map_name);
325 EXPECT_EQ(0U, frame->map_offset);
326 EXPECT_EQ(0x20000U, frame->map_start);
327 EXPECT_EQ(0x22000U, frame->map_end);
328 EXPECT_EQ(0U, frame->map_load_bias);
329 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
330
331 frame = &unwinder.frames()[2];
332 EXPECT_EQ(2U, frame->num);
333 EXPECT_EQ(0U, frame->rel_pc);
334 EXPECT_EQ(0x23000U, frame->pc);
335 EXPECT_EQ(0x10070U, frame->sp);
336 EXPECT_EQ("Frame2", frame->function_name);
337 EXPECT_EQ(2U, frame->function_offset);
338 EXPECT_EQ("/fake/libanother.so", frame->map_name);
339 EXPECT_EQ(0U, frame->map_offset);
340 EXPECT_EQ(0x23000U, frame->map_start);
341 EXPECT_EQ(0x24000U, frame->map_end);
342 EXPECT_EQ(0U, frame->map_load_bias);
343 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
344}
345
346// Verify SP in a non-existant map is okay.
347TEST_F(UnwinderTest, sp_not_in_map) {
348 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
349 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
350
351 regs_.FakeSetPc(0x1000);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700352 regs_.FakeSetSp(0x63000);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700353 ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x50020, false));
354 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
355
356 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
357 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800358 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700359
360 ASSERT_EQ(2U, unwinder.NumFrames());
361
362 auto* frame = &unwinder.frames()[0];
363 EXPECT_EQ(0U, frame->num);
364 EXPECT_EQ(0U, frame->rel_pc);
365 EXPECT_EQ(0x1000U, frame->pc);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700366 EXPECT_EQ(0x63000U, frame->sp);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700367 EXPECT_EQ("Frame0", frame->function_name);
368 EXPECT_EQ(0U, frame->function_offset);
369 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
370 EXPECT_EQ(0U, frame->map_offset);
371 EXPECT_EQ(0x1000U, frame->map_start);
372 EXPECT_EQ(0x8000U, frame->map_end);
373 EXPECT_EQ(0U, frame->map_load_bias);
374 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
375
376 frame = &unwinder.frames()[1];
377 EXPECT_EQ(1U, frame->num);
378 EXPECT_EQ(0x1000U, frame->rel_pc);
379 EXPECT_EQ(0x21000U, frame->pc);
380 EXPECT_EQ(0x50020U, frame->sp);
381 EXPECT_EQ("Frame1", frame->function_name);
382 EXPECT_EQ(1U, frame->function_offset);
383 EXPECT_EQ("/system/fake/libunwind.so", frame->map_name);
384 EXPECT_EQ(0U, frame->map_offset);
385 EXPECT_EQ(0x20000U, frame->map_start);
386 EXPECT_EQ(0x22000U, frame->map_end);
387 EXPECT_EQ(0U, frame->map_load_bias);
388 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
389}
390
391// Verify PC in a device stops the unwind.
392TEST_F(UnwinderTest, pc_in_device_stops_unwind) {
393 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
394 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
395 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
396
397 regs_.FakeSetPc(0x13000);
398 regs_.FakeSetSp(0x10000);
399 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
400 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
401 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
402
403 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
404 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800405 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700406
407 ASSERT_EQ(1U, unwinder.NumFrames());
408}
409
410// Verify SP in a device stops the unwind.
411TEST_F(UnwinderTest, sp_in_device_stops_unwind) {
412 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
413 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
414 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
415
416 regs_.FakeSetPc(0x1000);
417 regs_.FakeSetSp(0x13000);
418 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
419 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
420 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
421
422 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
423 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800424 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700425
426 ASSERT_EQ(1U, unwinder.NumFrames());
427}
428
429// Verify a no map info frame gets a frame.
430TEST_F(UnwinderTest, pc_without_map) {
431 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
432
433 regs_.FakeSetPc(0x41000);
434 regs_.FakeSetSp(0x13000);
435
436 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
437 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800438 EXPECT_EQ(ERROR_INVALID_MAP, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700439
440 ASSERT_EQ(1U, unwinder.NumFrames());
441
442 auto* frame = &unwinder.frames()[0];
443 EXPECT_EQ(0U, frame->num);
444 EXPECT_EQ(0x41000U, frame->rel_pc);
445 EXPECT_EQ(0x41000U, frame->pc);
446 EXPECT_EQ(0x13000U, frame->sp);
447 EXPECT_EQ("", frame->function_name);
448 EXPECT_EQ(0U, frame->function_offset);
449 EXPECT_EQ("", frame->map_name);
450 EXPECT_EQ(0U, frame->map_offset);
451 EXPECT_EQ(0U, frame->map_start);
452 EXPECT_EQ(0U, frame->map_end);
453 EXPECT_EQ(0U, frame->map_load_bias);
454 EXPECT_EQ(0, frame->map_flags);
455}
456
457// Verify that a speculative frame is added.
458TEST_F(UnwinderTest, speculative_frame) {
459 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
460 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
461
462 // Fake as if code called a nullptr function.
463 regs_.FakeSetPc(0);
464 regs_.FakeSetSp(0x10000);
465 regs_.FakeSetReturnAddress(0x1202);
466 regs_.FakeSetReturnAddressValid(true);
467
468 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
469 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
470
471 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
472 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800473 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700474
475 ASSERT_EQ(3U, unwinder.NumFrames());
476
477 auto* frame = &unwinder.frames()[0];
478 EXPECT_EQ(0U, frame->num);
479 EXPECT_EQ(0U, frame->rel_pc);
480 EXPECT_EQ(0U, frame->pc);
481 EXPECT_EQ(0x10000U, frame->sp);
482 EXPECT_EQ("", frame->function_name);
483 EXPECT_EQ(0U, frame->function_offset);
484 EXPECT_EQ("", frame->map_name);
485 EXPECT_EQ(0U, frame->map_offset);
486 EXPECT_EQ(0U, frame->map_start);
487 EXPECT_EQ(0U, frame->map_end);
488 EXPECT_EQ(0U, frame->map_load_bias);
489 EXPECT_EQ(0, frame->map_flags);
490
491 frame = &unwinder.frames()[1];
492 EXPECT_EQ(1U, frame->num);
493 EXPECT_EQ(0x200U, frame->rel_pc);
494 EXPECT_EQ(0x1200U, frame->pc);
495 EXPECT_EQ(0x10000U, frame->sp);
496 EXPECT_EQ("Frame0", frame->function_name);
497 EXPECT_EQ(0U, frame->function_offset);
498 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
499 EXPECT_EQ(0U, frame->map_offset);
500 EXPECT_EQ(0x1000U, frame->map_start);
501 EXPECT_EQ(0x8000U, frame->map_end);
502 EXPECT_EQ(0U, frame->map_load_bias);
503 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
504
505 frame = &unwinder.frames()[2];
506 EXPECT_EQ(2U, frame->num);
507 EXPECT_EQ(0x100U, frame->rel_pc);
508 EXPECT_EQ(0x23100U, frame->pc);
509 EXPECT_EQ(0x10020U, frame->sp);
510 EXPECT_EQ("Frame1", frame->function_name);
511 EXPECT_EQ(1U, frame->function_offset);
512 EXPECT_EQ("/fake/libanother.so", frame->map_name);
513 EXPECT_EQ(0U, frame->map_offset);
514 EXPECT_EQ(0x23000U, frame->map_start);
515 EXPECT_EQ(0x24000U, frame->map_end);
516 EXPECT_EQ(0U, frame->map_load_bias);
517 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
518}
519
520// Verify that a speculative frame is added then removed because no other
521// frames are added.
522TEST_F(UnwinderTest, speculative_frame_removed) {
523 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
524 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
525
526 // Fake as if code called a nullptr function.
527 regs_.FakeSetPc(0);
528 regs_.FakeSetSp(0x10000);
529 regs_.FakeSetReturnAddress(0x1202);
530 regs_.FakeSetReturnAddressValid(true);
531
532 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
533 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800534 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700535
536 ASSERT_EQ(1U, unwinder.NumFrames());
537
538 auto* frame = &unwinder.frames()[0];
539 EXPECT_EQ(0U, frame->num);
540 EXPECT_EQ(0U, frame->rel_pc);
541 EXPECT_EQ(0U, frame->pc);
542 EXPECT_EQ(0x10000U, frame->sp);
543 EXPECT_EQ("", frame->function_name);
544 EXPECT_EQ(0U, frame->function_offset);
545 EXPECT_EQ("", frame->map_name);
546 EXPECT_EQ(0U, frame->map_offset);
547 EXPECT_EQ(0U, frame->map_start);
548 EXPECT_EQ(0U, frame->map_end);
549 EXPECT_EQ(0U, frame->map_load_bias);
550 EXPECT_EQ(0, frame->map_flags);
551}
552
Christopher Ferrise69f4702017-10-19 16:08:58 -0700553// Verify that an unwind stops when a frame is in given suffix.
554TEST_F(UnwinderTest, map_ignore_suffixes) {
555 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
556 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
557 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
558 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
559
560 // Fake as if code called a nullptr function.
561 regs_.FakeSetPc(0x1000);
562 regs_.FakeSetSp(0x10000);
563 ElfInterfaceFake::FakePushStepData(StepData(0x43402, 0x10010, false));
564 ElfInterfaceFake::FakePushStepData(StepData(0x53502, 0x10020, false));
565 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
566
567 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
Christopher Ferris1cb84ce2017-10-24 15:36:00 -0700568 std::vector<std::string> suffixes{"oat"};
Christopher Ferrise69f4702017-10-19 16:08:58 -0700569 unwinder.Unwind(nullptr, &suffixes);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800570 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrise69f4702017-10-19 16:08:58 -0700571
572 ASSERT_EQ(2U, unwinder.NumFrames());
573 // Make sure the elf was not initialized.
574 MapInfo* map_info = maps_.Find(0x53000);
575 ASSERT_TRUE(map_info != nullptr);
576 EXPECT_TRUE(map_info->elf == nullptr);
577
578 auto* frame = &unwinder.frames()[0];
579 EXPECT_EQ(0U, frame->num);
580 EXPECT_EQ(0U, frame->rel_pc);
581 EXPECT_EQ(0x1000U, frame->pc);
582 EXPECT_EQ(0x10000U, frame->sp);
583 EXPECT_EQ("Frame0", frame->function_name);
584 EXPECT_EQ(0U, frame->function_offset);
585 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
586 EXPECT_EQ(0U, frame->map_offset);
587 EXPECT_EQ(0x1000U, frame->map_start);
588 EXPECT_EQ(0x8000U, frame->map_end);
589 EXPECT_EQ(0U, frame->map_load_bias);
590 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
591
592 frame = &unwinder.frames()[1];
593 EXPECT_EQ(1U, frame->num);
594 EXPECT_EQ(0x400U, frame->rel_pc);
595 EXPECT_EQ(0x43400U, frame->pc);
596 EXPECT_EQ(0x10010U, frame->sp);
597 EXPECT_EQ("Frame1", frame->function_name);
598 EXPECT_EQ(1U, frame->function_offset);
599 EXPECT_EQ("/fake/fake.apk", frame->map_name);
600 EXPECT_EQ(0x1d000U, frame->map_offset);
601 EXPECT_EQ(0x43000U, frame->map_start);
602 EXPECT_EQ(0x44000U, frame->map_end);
603 EXPECT_EQ(0U, frame->map_load_bias);
604 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
605}
606
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700607// Verify that an unwind stops when the sp and pc don't change.
608TEST_F(UnwinderTest, sp_pc_do_not_change) {
609 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
610 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
611 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
612 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
613 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
614
615 regs_.FakeSetPc(0x1000);
616 regs_.FakeSetSp(0x10000);
617 ElfInterfaceFake::FakePushStepData(StepData(0x33402, 0x10010, false));
618 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
619 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
620 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
621 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
622 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
623
624 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
625 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800626 EXPECT_EQ(ERROR_REPEATED_FRAME, unwinder.LastErrorCode());
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700627
628 ASSERT_EQ(3U, unwinder.NumFrames());
629
630 auto* frame = &unwinder.frames()[0];
631 EXPECT_EQ(0U, frame->num);
632 EXPECT_EQ(0U, frame->rel_pc);
633 EXPECT_EQ(0x1000U, frame->pc);
634 EXPECT_EQ(0x10000U, frame->sp);
635 EXPECT_EQ("Frame0", frame->function_name);
636 EXPECT_EQ(0U, frame->function_offset);
637 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
638 EXPECT_EQ(0U, frame->map_offset);
639 EXPECT_EQ(0x1000U, frame->map_start);
640 EXPECT_EQ(0x8000U, frame->map_end);
641 EXPECT_EQ(0U, frame->map_load_bias);
642 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
643
644 frame = &unwinder.frames()[1];
645 EXPECT_EQ(1U, frame->num);
646 EXPECT_EQ(0x400U, frame->rel_pc);
647 EXPECT_EQ(0x33400U, frame->pc);
648 EXPECT_EQ(0x10010U, frame->sp);
649 EXPECT_EQ("Frame1", frame->function_name);
650 EXPECT_EQ(1U, frame->function_offset);
651 EXPECT_EQ("/fake/compressed.so", frame->map_name);
652 EXPECT_EQ(0U, frame->map_offset);
653 EXPECT_EQ(0x33000U, frame->map_start);
654 EXPECT_EQ(0x34000U, frame->map_end);
655 EXPECT_EQ(0U, frame->map_load_bias);
656 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
657
658 frame = &unwinder.frames()[2];
659 EXPECT_EQ(2U, frame->num);
660 EXPECT_EQ(0x500U, frame->rel_pc);
661 EXPECT_EQ(0x33500U, frame->pc);
662 EXPECT_EQ(0x10020U, frame->sp);
663 EXPECT_EQ("Frame2", frame->function_name);
664 EXPECT_EQ(2U, frame->function_offset);
665 EXPECT_EQ("/fake/compressed.so", frame->map_name);
666 EXPECT_EQ(0U, frame->map_offset);
667 EXPECT_EQ(0x33000U, frame->map_start);
668 EXPECT_EQ(0x34000U, frame->map_end);
669 EXPECT_EQ(0U, frame->map_load_bias);
670 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
671}
672
Christopher Ferrise762f1f2018-02-06 14:51:48 -0800673TEST_F(UnwinderTest, dex_pc_in_map) {
674 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
675 regs_.FakeSetPc(0x1000);
676 regs_.FakeSetSp(0x10000);
677 regs_.FakeSetDexPc(0xa3400);
678
679 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
680 unwinder.Unwind();
681 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
682
683 ASSERT_EQ(2U, unwinder.NumFrames());
684
685 auto* frame = &unwinder.frames()[0];
686 EXPECT_EQ(0U, frame->num);
687 EXPECT_EQ(0x400U, frame->rel_pc);
688 EXPECT_EQ(0xa3400U, frame->pc);
689 EXPECT_EQ(0x10000U, frame->sp);
690 EXPECT_EQ("", frame->function_name);
691 EXPECT_EQ(0U, frame->function_offset);
692 EXPECT_EQ("/fake/fake.vdex", frame->map_name);
693 EXPECT_EQ(0U, frame->map_offset);
694 EXPECT_EQ(0xa3000U, frame->map_start);
695 EXPECT_EQ(0xa4000U, frame->map_end);
696 EXPECT_EQ(0U, frame->map_load_bias);
697 EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
698
699 frame = &unwinder.frames()[1];
700 EXPECT_EQ(1U, frame->num);
701 EXPECT_EQ(0U, frame->rel_pc);
702 EXPECT_EQ(0x1000U, frame->pc);
703 EXPECT_EQ(0x10000U, frame->sp);
704 EXPECT_EQ("Frame0", frame->function_name);
705 EXPECT_EQ(0U, frame->function_offset);
706 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
707 EXPECT_EQ(0U, frame->map_offset);
708 EXPECT_EQ(0x1000U, frame->map_start);
709 EXPECT_EQ(0x8000U, frame->map_end);
710 EXPECT_EQ(0U, frame->map_load_bias);
711 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
712}
713
714TEST_F(UnwinderTest, dex_pc_not_in_map) {
715 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
716 regs_.FakeSetPc(0x1000);
717 regs_.FakeSetSp(0x10000);
718 regs_.FakeSetDexPc(0x50000);
719
720 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
721 unwinder.Unwind();
722 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
723
724 ASSERT_EQ(2U, unwinder.NumFrames());
725
726 auto* frame = &unwinder.frames()[0];
727 EXPECT_EQ(0U, frame->num);
728 EXPECT_EQ(0x50000U, frame->rel_pc);
729 EXPECT_EQ(0x50000U, frame->pc);
730 EXPECT_EQ(0x10000U, frame->sp);
731 EXPECT_EQ("", frame->function_name);
732 EXPECT_EQ(0U, frame->function_offset);
733 EXPECT_EQ("", frame->map_name);
734 EXPECT_EQ(0U, frame->map_offset);
735 EXPECT_EQ(0U, frame->map_start);
736 EXPECT_EQ(0U, frame->map_end);
737 EXPECT_EQ(0U, frame->map_load_bias);
738 EXPECT_EQ(0, frame->map_flags);
739
740 frame = &unwinder.frames()[1];
741 EXPECT_EQ(1U, frame->num);
742 EXPECT_EQ(0U, frame->rel_pc);
743 EXPECT_EQ(0x1000U, frame->pc);
744 EXPECT_EQ(0x10000U, frame->sp);
745 EXPECT_EQ("Frame0", frame->function_name);
746 EXPECT_EQ(0U, frame->function_offset);
747 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
748 EXPECT_EQ(0U, frame->map_offset);
749 EXPECT_EQ(0x1000U, frame->map_start);
750 EXPECT_EQ(0x8000U, frame->map_end);
751 EXPECT_EQ(0U, frame->map_load_bias);
752 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
753}
754
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700755// Verify format frame code.
756TEST_F(UnwinderTest, format_frame_static) {
757 FrameData frame;
758 frame.num = 1;
759 frame.rel_pc = 0x1000;
760 frame.pc = 0x4000;
761 frame.sp = 0x1000;
762 frame.function_name = "function";
763 frame.function_offset = 100;
764 frame.map_name = "/fake/libfake.so";
765 frame.map_offset = 0x2000;
766 frame.map_start = 0x3000;
767 frame.map_end = 0x6000;
768 frame.map_flags = PROT_READ;
769
770 EXPECT_EQ(" #01 pc 0000000000001000 (offset 0x2000) /fake/libfake.so (function+100)",
771 Unwinder::FormatFrame(frame, false));
772 EXPECT_EQ(" #01 pc 00001000 (offset 0x2000) /fake/libfake.so (function+100)",
773 Unwinder::FormatFrame(frame, true));
774
775 frame.map_offset = 0;
776 EXPECT_EQ(" #01 pc 0000000000001000 /fake/libfake.so (function+100)",
777 Unwinder::FormatFrame(frame, false));
778 EXPECT_EQ(" #01 pc 00001000 /fake/libfake.so (function+100)",
779 Unwinder::FormatFrame(frame, true));
780
781 frame.function_offset = 0;
782 EXPECT_EQ(" #01 pc 0000000000001000 /fake/libfake.so (function)",
783 Unwinder::FormatFrame(frame, false));
784 EXPECT_EQ(" #01 pc 00001000 /fake/libfake.so (function)", Unwinder::FormatFrame(frame, true));
785
786 frame.function_name = "";
787 EXPECT_EQ(" #01 pc 0000000000001000 /fake/libfake.so", Unwinder::FormatFrame(frame, false));
788 EXPECT_EQ(" #01 pc 00001000 /fake/libfake.so", Unwinder::FormatFrame(frame, true));
789
790 frame.map_name = "";
791 EXPECT_EQ(" #01 pc 0000000000001000 <anonymous:3000>", Unwinder::FormatFrame(frame, false));
792 EXPECT_EQ(" #01 pc 00001000 <anonymous:3000>", Unwinder::FormatFrame(frame, true));
793
794 frame.map_start = 0;
795 frame.map_end = 0;
796 EXPECT_EQ(" #01 pc 0000000000001000 <unknown>", Unwinder::FormatFrame(frame, false));
797 EXPECT_EQ(" #01 pc 00001000 <unknown>", Unwinder::FormatFrame(frame, true));
798}
799
Christopher Ferris02fdb562017-12-08 15:04:49 -0800800static std::string ArchToString(ArchEnum arch) {
801 if (arch == ARCH_ARM) {
802 return "Arm";
803 } else if (arch == ARCH_ARM64) {
804 return "Arm64";
805 } else if (arch == ARCH_X86) {
806 return "X86";
807 } else if (arch == ARCH_X86_64) {
808 return "X86_64";
809 } else {
810 return "Unknown";
811 }
812}
813
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700814// Verify format frame code.
815TEST_F(UnwinderTest, format_frame) {
Christopher Ferris02fdb562017-12-08 15:04:49 -0800816 std::vector<Regs*> reg_list;
817 RegsArm* arm = new RegsArm;
818 arm->set_pc(0x2300);
819 arm->set_sp(0x10000);
820 reg_list.push_back(arm);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700821
Christopher Ferris02fdb562017-12-08 15:04:49 -0800822 RegsArm64* arm64 = new RegsArm64;
823 arm64->set_pc(0x2300);
824 arm64->set_sp(0x10000);
825 reg_list.push_back(arm64);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700826
Christopher Ferris02fdb562017-12-08 15:04:49 -0800827 RegsX86* x86 = new RegsX86;
828 x86->set_pc(0x2300);
829 x86->set_sp(0x10000);
830 reg_list.push_back(x86);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700831
Christopher Ferris02fdb562017-12-08 15:04:49 -0800832 RegsX86_64* x86_64 = new RegsX86_64;
833 x86_64->set_pc(0x2300);
834 x86_64->set_sp(0x10000);
835 reg_list.push_back(x86_64);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700836
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100837 RegsMips* mips = new RegsMips;
838 mips->set_pc(0x2300);
839 mips->set_sp(0x10000);
840 reg_list.push_back(mips);
841
842 RegsMips64* mips64 = new RegsMips64;
843 mips64->set_pc(0x2300);
844 mips64->set_sp(0x10000);
845 reg_list.push_back(mips64);
846
Christopher Ferris02fdb562017-12-08 15:04:49 -0800847 for (auto regs : reg_list) {
848 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 10));
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700849
Christopher Ferris02fdb562017-12-08 15:04:49 -0800850 Unwinder unwinder(64, &maps_, regs, process_memory_);
851 unwinder.Unwind();
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700852
Christopher Ferris02fdb562017-12-08 15:04:49 -0800853 ASSERT_EQ(1U, unwinder.NumFrames());
854 std::string expected;
855 switch (regs->Arch()) {
856 case ARCH_ARM:
857 case ARCH_X86:
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100858 case ARCH_MIPS:
Christopher Ferris02fdb562017-12-08 15:04:49 -0800859 expected = " #00 pc 00001300 /system/fake/libc.so (Frame0+10)";
860 break;
861 case ARCH_ARM64:
862 case ARCH_X86_64:
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100863 case ARCH_MIPS64:
Christopher Ferris02fdb562017-12-08 15:04:49 -0800864 expected = " #00 pc 0000000000001300 /system/fake/libc.so (Frame0+10)";
865 break;
866 default:
867 expected = "";
868 }
869 EXPECT_EQ(expected, unwinder.FormatFrame(0))
870 << "Mismatch of frame format for regs arch " << ArchToString(regs->Arch());
871 delete regs;
872 }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700873}
874
875} // namespace unwindstack