• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <inttypes.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/mman.h>
22 #include <unistd.h>
23 
24 #include <gtest/gtest.h>
25 
26 #include <string>
27 #include <unordered_map>
28 #include <vector>
29 
30 #include <android-base/file.h>
31 
32 #include <unwindstack/JitDebug.h>
33 #include <unwindstack/MachineArm.h>
34 #include <unwindstack/MachineArm64.h>
35 #include <unwindstack/MachineX86.h>
36 #include <unwindstack/MachineX86_64.h>
37 #include <unwindstack/Maps.h>
38 #include <unwindstack/RegsArm.h>
39 #include <unwindstack/RegsArm64.h>
40 #include <unwindstack/RegsX86.h>
41 #include <unwindstack/RegsX86_64.h>
42 #include <unwindstack/Unwinder.h>
43 
44 #include "ElfTestUtils.h"
45 #include "MemoryOffline.h"
46 #include "TestUtils.h"
47 
48 namespace unwindstack {
49 
AddMemory(std::string file_name,MemoryOfflineParts * parts)50 static void AddMemory(std::string file_name, MemoryOfflineParts* parts) {
51   MemoryOffline* memory = new MemoryOffline;
52   ASSERT_TRUE(memory->Init(file_name.c_str(), 0));
53   parts->Add(memory);
54 }
55 
56 class UnwindOfflineTest : public ::testing::Test {
57  protected:
TearDown()58   void TearDown() override {
59     if (cwd_ != nullptr) {
60       ASSERT_EQ(0, chdir(cwd_));
61     }
62     free(cwd_);
63   }
64 
Init(const char * file_dir,ArchEnum arch,bool add_stack=true)65   void Init(const char* file_dir, ArchEnum arch, bool add_stack = true) {
66     dir_ = TestGetFileDirectory() + "offline/" + file_dir;
67 
68     std::string data;
69     ASSERT_TRUE(android::base::ReadFileToString((dir_ + "maps.txt"), &data));
70 
71     maps_.reset(new BufferMaps(data.c_str()));
72     ASSERT_TRUE(maps_->Parse());
73 
74     if (add_stack) {
75       std::string stack_name(dir_ + "stack.data");
76       struct stat st;
77       if (stat(stack_name.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
78         std::unique_ptr<MemoryOffline> stack_memory(new MemoryOffline);
79         ASSERT_TRUE(stack_memory->Init((dir_ + "stack.data").c_str(), 0));
80         process_memory_.reset(stack_memory.release());
81       } else {
82         std::unique_ptr<MemoryOfflineParts> stack_memory(new MemoryOfflineParts);
83         for (size_t i = 0;; i++) {
84           stack_name = dir_ + "stack" + std::to_string(i) + ".data";
85           if (stat(stack_name.c_str(), &st) == -1 || !S_ISREG(st.st_mode)) {
86             ASSERT_TRUE(i != 0) << "No stack data files found.";
87             break;
88           }
89           AddMemory(stack_name, stack_memory.get());
90         }
91         process_memory_.reset(stack_memory.release());
92       }
93     }
94 
95     switch (arch) {
96       case ARCH_ARM: {
97         RegsArm* regs = new RegsArm;
98         regs_.reset(regs);
99         ReadRegs<uint32_t>(regs, arm_regs_);
100         break;
101       }
102       case ARCH_ARM64: {
103         RegsArm64* regs = new RegsArm64;
104         regs_.reset(regs);
105         ReadRegs<uint64_t>(regs, arm64_regs_);
106         break;
107       }
108       case ARCH_X86: {
109         RegsX86* regs = new RegsX86;
110         regs_.reset(regs);
111         ReadRegs<uint32_t>(regs, x86_regs_);
112         break;
113       }
114       case ARCH_X86_64: {
115         RegsX86_64* regs = new RegsX86_64;
116         regs_.reset(regs);
117         ReadRegs<uint64_t>(regs, x86_64_regs_);
118         break;
119       }
120       default:
121         ASSERT_TRUE(false) << "Unknown arch " << std::to_string(arch);
122     }
123     cwd_ = getcwd(nullptr, 0);
124     // Make dir_ an absolute directory.
125     if (dir_.empty() || dir_[0] != '/') {
126       dir_ = std::string(cwd_) + '/' + dir_;
127     }
128     ASSERT_EQ(0, chdir(dir_.c_str()));
129   }
130 
131   template <typename AddressType>
ReadRegs(RegsImpl<AddressType> * regs,const std::unordered_map<std::string,uint32_t> & name_to_reg)132   void ReadRegs(RegsImpl<AddressType>* regs,
133                 const std::unordered_map<std::string, uint32_t>& name_to_reg) {
134     FILE* fp = fopen((dir_ + "regs.txt").c_str(), "r");
135     ASSERT_TRUE(fp != nullptr);
136     while (!feof(fp)) {
137       uint64_t value;
138       char reg_name[100];
139       ASSERT_EQ(2, fscanf(fp, "%s %" SCNx64 "\n", reg_name, &value));
140       std::string name(reg_name);
141       if (!name.empty()) {
142         // Remove the : from the end.
143         name.resize(name.size() - 1);
144       }
145       auto entry = name_to_reg.find(name);
146       ASSERT_TRUE(entry != name_to_reg.end()) << "Unknown register named " << name;
147       (*regs)[entry->second] = value;
148     }
149     fclose(fp);
150   }
151 
152   static std::unordered_map<std::string, uint32_t> arm_regs_;
153   static std::unordered_map<std::string, uint32_t> arm64_regs_;
154   static std::unordered_map<std::string, uint32_t> x86_regs_;
155   static std::unordered_map<std::string, uint32_t> x86_64_regs_;
156 
157   char* cwd_ = nullptr;
158   std::string dir_;
159   std::unique_ptr<Regs> regs_;
160   std::unique_ptr<Maps> maps_;
161   std::shared_ptr<Memory> process_memory_;
162 };
163 
164 std::unordered_map<std::string, uint32_t> UnwindOfflineTest::arm_regs_ = {
165     {"r0", ARM_REG_R0},  {"r1", ARM_REG_R1}, {"r2", ARM_REG_R2},   {"r3", ARM_REG_R3},
166     {"r4", ARM_REG_R4},  {"r5", ARM_REG_R5}, {"r6", ARM_REG_R6},   {"r7", ARM_REG_R7},
167     {"r8", ARM_REG_R8},  {"r9", ARM_REG_R9}, {"r10", ARM_REG_R10}, {"r11", ARM_REG_R11},
168     {"ip", ARM_REG_R12}, {"sp", ARM_REG_SP}, {"lr", ARM_REG_LR},   {"pc", ARM_REG_PC},
169 };
170 
171 std::unordered_map<std::string, uint32_t> UnwindOfflineTest::arm64_regs_ = {
172     {"x0", ARM64_REG_R0},      {"x1", ARM64_REG_R1},   {"x2", ARM64_REG_R2},
173     {"x3", ARM64_REG_R3},      {"x4", ARM64_REG_R4},   {"x5", ARM64_REG_R5},
174     {"x6", ARM64_REG_R6},      {"x7", ARM64_REG_R7},   {"x8", ARM64_REG_R8},
175     {"x9", ARM64_REG_R9},      {"x10", ARM64_REG_R10}, {"x11", ARM64_REG_R11},
176     {"x12", ARM64_REG_R12},    {"x13", ARM64_REG_R13}, {"x14", ARM64_REG_R14},
177     {"x15", ARM64_REG_R15},    {"x16", ARM64_REG_R16}, {"x17", ARM64_REG_R17},
178     {"x18", ARM64_REG_R18},    {"x19", ARM64_REG_R19}, {"x20", ARM64_REG_R20},
179     {"x21", ARM64_REG_R21},    {"x22", ARM64_REG_R22}, {"x23", ARM64_REG_R23},
180     {"x24", ARM64_REG_R24},    {"x25", ARM64_REG_R25}, {"x26", ARM64_REG_R26},
181     {"x27", ARM64_REG_R27},    {"x28", ARM64_REG_R28}, {"x29", ARM64_REG_R29},
182     {"sp", ARM64_REG_SP},      {"lr", ARM64_REG_LR},   {"pc", ARM64_REG_PC},
183     {"pst", ARM64_REG_PSTATE},
184 };
185 
186 std::unordered_map<std::string, uint32_t> UnwindOfflineTest::x86_regs_ = {
187     {"eax", X86_REG_EAX}, {"ebx", X86_REG_EBX}, {"ecx", X86_REG_ECX},
188     {"edx", X86_REG_EDX}, {"ebp", X86_REG_EBP}, {"edi", X86_REG_EDI},
189     {"esi", X86_REG_ESI}, {"esp", X86_REG_ESP}, {"eip", X86_REG_EIP},
190 };
191 
192 std::unordered_map<std::string, uint32_t> UnwindOfflineTest::x86_64_regs_ = {
193     {"rax", X86_64_REG_RAX}, {"rbx", X86_64_REG_RBX}, {"rcx", X86_64_REG_RCX},
194     {"rdx", X86_64_REG_RDX}, {"r8", X86_64_REG_R8},   {"r9", X86_64_REG_R9},
195     {"r10", X86_64_REG_R10}, {"r11", X86_64_REG_R11}, {"r12", X86_64_REG_R12},
196     {"r13", X86_64_REG_R13}, {"r14", X86_64_REG_R14}, {"r15", X86_64_REG_R15},
197     {"rdi", X86_64_REG_RDI}, {"rsi", X86_64_REG_RSI}, {"rbp", X86_64_REG_RBP},
198     {"rsp", X86_64_REG_RSP}, {"rip", X86_64_REG_RIP},
199 };
200 
DumpFrames(Unwinder & unwinder)201 static std::string DumpFrames(Unwinder& unwinder) {
202   std::string str;
203   for (size_t i = 0; i < unwinder.NumFrames(); i++) {
204     str += unwinder.FormatFrame(i) + "\n";
205   }
206   return str;
207 }
208 
TEST_F(UnwindOfflineTest,pc_straddle_arm)209 TEST_F(UnwindOfflineTest, pc_straddle_arm) {
210   ASSERT_NO_FATAL_FAILURE(Init("straddle_arm/", ARCH_ARM));
211 
212   std::unique_ptr<Regs> regs_copy(regs_->Clone());
213   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
214   unwinder.Unwind();
215 
216   std::string frame_info(DumpFrames(unwinder));
217   ASSERT_EQ(4U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
218   EXPECT_EQ(
219       "  #00 pc 0001a9f8  libc.so (abort+64)\n"
220       "  #01 pc 00006a1b  libbase.so (android::base::DefaultAborter(char const*)+6)\n"
221       "  #02 pc 00007441  libbase.so (android::base::LogMessage::~LogMessage()+748)\n"
222       "  #03 pc 00015147  /does/not/exist/libhidlbase.so\n",
223       frame_info);
224   EXPECT_EQ(0xf31ea9f8U, unwinder.frames()[0].pc);
225   EXPECT_EQ(0xe9c866f8U, unwinder.frames()[0].sp);
226   EXPECT_EQ(0xf2da0a1bU, unwinder.frames()[1].pc);
227   EXPECT_EQ(0xe9c86728U, unwinder.frames()[1].sp);
228   EXPECT_EQ(0xf2da1441U, unwinder.frames()[2].pc);
229   EXPECT_EQ(0xe9c86730U, unwinder.frames()[2].sp);
230   EXPECT_EQ(0xf3367147U, unwinder.frames()[3].pc);
231   EXPECT_EQ(0xe9c86778U, unwinder.frames()[3].sp);
232 
233   // Display build ids now.
234   unwinder.SetRegs(regs_copy.get());
235   unwinder.SetDisplayBuildID(true);
236   unwinder.Unwind();
237 
238   frame_info = DumpFrames(unwinder);
239   ASSERT_EQ(4U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
240   EXPECT_EQ(
241       "  #00 pc 0001a9f8  libc.so (abort+64) (BuildId: 2dd0d4ba881322a0edabeed94808048c)\n"
242       "  #01 pc 00006a1b  libbase.so (android::base::DefaultAborter(char const*)+6) (BuildId: "
243       "ed43842c239cac1a618e600ea91c4cbd)\n"
244       "  #02 pc 00007441  libbase.so (android::base::LogMessage::~LogMessage()+748) (BuildId: "
245       "ed43842c239cac1a618e600ea91c4cbd)\n"
246       "  #03 pc 00015147  /does/not/exist/libhidlbase.so\n",
247       frame_info);
248 }
249 
TEST_F(UnwindOfflineTest,pc_in_gnu_debugdata_arm)250 TEST_F(UnwindOfflineTest, pc_in_gnu_debugdata_arm) {
251   ASSERT_NO_FATAL_FAILURE(Init("gnu_debugdata_arm/", ARCH_ARM));
252 
253   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
254   unwinder.Unwind();
255 
256   std::string frame_info(DumpFrames(unwinder));
257   ASSERT_EQ(2U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
258   EXPECT_EQ(
259       "  #00 pc 0006dc49  libandroid_runtime.so "
260       "(android::AndroidRuntime::javaThreadShell(void*)+80)\n"
261       "  #01 pc 0006dce5  libandroid_runtime.so "
262       "(android::AndroidRuntime::javaCreateThreadEtc(int (*)(void*), void*, char const*, int, "
263       "unsigned int, void**))\n",
264       frame_info);
265   EXPECT_EQ(0xf1f6dc49U, unwinder.frames()[0].pc);
266   EXPECT_EQ(0xd8fe6930U, unwinder.frames()[0].sp);
267   EXPECT_EQ(0xf1f6dce5U, unwinder.frames()[1].pc);
268   EXPECT_EQ(0xd8fe6958U, unwinder.frames()[1].sp);
269 }
270 
TEST_F(UnwindOfflineTest,pc_straddle_arm64)271 TEST_F(UnwindOfflineTest, pc_straddle_arm64) {
272   ASSERT_NO_FATAL_FAILURE(Init("straddle_arm64/", ARCH_ARM64));
273 
274   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
275   unwinder.Unwind();
276 
277   std::string frame_info(DumpFrames(unwinder));
278   ASSERT_EQ(6U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
279   EXPECT_EQ(
280       "  #00 pc 0000000000429fd8  libunwindstack_test (SignalInnerFunction+24)\n"
281       "  #01 pc 000000000042a078  libunwindstack_test (SignalMiddleFunction+8)\n"
282       "  #02 pc 000000000042a08c  libunwindstack_test (SignalOuterFunction+8)\n"
283       "  #03 pc 000000000042d8fc  libunwindstack_test "
284       "(unwindstack::RemoteThroughSignal(int, unsigned int)+20)\n"
285       "  #04 pc 000000000042d8d8  libunwindstack_test "
286       "(unwindstack::UnwindTest_remote_through_signal_Test::TestBody()+32)\n"
287       "  #05 pc 0000000000455d70  libunwindstack_test (testing::Test::Run()+392)\n",
288       frame_info);
289   EXPECT_EQ(0x64d09d4fd8U, unwinder.frames()[0].pc);
290   EXPECT_EQ(0x7fe0d84040U, unwinder.frames()[0].sp);
291   EXPECT_EQ(0x64d09d5078U, unwinder.frames()[1].pc);
292   EXPECT_EQ(0x7fe0d84070U, unwinder.frames()[1].sp);
293   EXPECT_EQ(0x64d09d508cU, unwinder.frames()[2].pc);
294   EXPECT_EQ(0x7fe0d84080U, unwinder.frames()[2].sp);
295   EXPECT_EQ(0x64d09d88fcU, unwinder.frames()[3].pc);
296   EXPECT_EQ(0x7fe0d84090U, unwinder.frames()[3].sp);
297   EXPECT_EQ(0x64d09d88d8U, unwinder.frames()[4].pc);
298   EXPECT_EQ(0x7fe0d840f0U, unwinder.frames()[4].sp);
299   EXPECT_EQ(0x64d0a00d70U, unwinder.frames()[5].pc);
300   EXPECT_EQ(0x7fe0d84110U, unwinder.frames()[5].sp);
301 }
302 
TEST_F(UnwindOfflineTest,jit_debug_x86)303 TEST_F(UnwindOfflineTest, jit_debug_x86) {
304   ASSERT_NO_FATAL_FAILURE(Init("jit_debug_x86/", ARCH_X86));
305 
306   MemoryOfflineParts* memory = new MemoryOfflineParts;
307   AddMemory(dir_ + "descriptor.data", memory);
308   AddMemory(dir_ + "stack.data", memory);
309   for (size_t i = 0; i < 7; i++) {
310     AddMemory(dir_ + "entry" + std::to_string(i) + ".data", memory);
311     AddMemory(dir_ + "jit" + std::to_string(i) + ".data", memory);
312   }
313   process_memory_.reset(memory);
314 
315   JitDebug jit_debug(process_memory_);
316   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
317   unwinder.SetJitDebug(&jit_debug, regs_->Arch());
318   unwinder.Unwind();
319 
320   std::string frame_info(DumpFrames(unwinder));
321   ASSERT_EQ(69U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
322   EXPECT_EQ(
323       "  #00 pc 00068fb8  libarttestd.so (art::CauseSegfault()+72)\n"
324       "  #01 pc 00067f00  libarttestd.so (Java_Main_unwindInProcess+10032)\n"
325       "  #02 pc 000021a8  137-cfi.odex (boolean Main.unwindInProcess(boolean, int, "
326       "boolean)+136)\n"
327       "  #03 pc 0000fe80  anonymous:ee74c000 (boolean Main.bar(boolean)+64)\n"
328       "  #04 pc 006ad4d2  libartd.so (art_quick_invoke_stub+338)\n"
329       "  #05 pc 00146ab5  libartd.so "
330       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
331       "const*)+885)\n"
332       "  #06 pc 0039cf0d  libartd.so "
333       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
334       "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
335       "  #07 pc 00392552  libartd.so "
336       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
337       "art::ShadowFrame&, art::JValue, bool)+354)\n"
338       "  #08 pc 0039399a  libartd.so "
339       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
340       "const&, art::ShadowFrame*)+234)\n"
341       "  #09 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
342       "  #10 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\n"
343       "  #11 pc 0000fe03  anonymous:ee74c000 (int Main.compare(Main, Main)+51)\n"
344       "  #12 pc 006ad4d2  libartd.so (art_quick_invoke_stub+338)\n"
345       "  #13 pc 00146ab5  libartd.so "
346       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
347       "const*)+885)\n"
348       "  #14 pc 0039cf0d  libartd.so "
349       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
350       "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
351       "  #15 pc 00392552  libartd.so "
352       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
353       "art::ShadowFrame&, art::JValue, bool)+354)\n"
354       "  #16 pc 0039399a  libartd.so "
355       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
356       "const&, art::ShadowFrame*)+234)\n"
357       "  #17 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
358       "  #18 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\n"
359       "  #19 pc 0000fd3b  anonymous:ee74c000 (int Main.compare(java.lang.Object, "
360       "java.lang.Object)+107)\n"
361       "  #20 pc 006ad4d2  libartd.so (art_quick_invoke_stub+338)\n"
362       "  #21 pc 00146ab5  libartd.so "
363       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
364       "const*)+885)\n"
365       "  #22 pc 0039cf0d  libartd.so "
366       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
367       "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
368       "  #23 pc 00392552  libartd.so "
369       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
370       "art::ShadowFrame&, art::JValue, bool)+354)\n"
371       "  #24 pc 0039399a  libartd.so "
372       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
373       "const&, art::ShadowFrame*)+234)\n"
374       "  #25 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
375       "  #26 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\n"
376       "  #27 pc 0000fbdb  anonymous:ee74c000 (int "
377       "java.util.Arrays.binarySearch0(java.lang.Object[], int, int, java.lang.Object, "
378       "java.util.Comparator)+331)\n"
379       "  #28 pc 006ad6a2  libartd.so (art_quick_invoke_static_stub+418)\n"
380       "  #29 pc 00146acb  libartd.so "
381       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
382       "const*)+907)\n"
383       "  #30 pc 0039cf0d  libartd.so "
384       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
385       "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
386       "  #31 pc 00392552  libartd.so "
387       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
388       "art::ShadowFrame&, art::JValue, bool)+354)\n"
389       "  #32 pc 0039399a  libartd.so "
390       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
391       "const&, art::ShadowFrame*)+234)\n"
392       "  #33 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
393       "  #34 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\n"
394       "  #35 pc 0000f624  anonymous:ee74c000 (boolean Main.foo()+164)\n"
395       "  #36 pc 006ad4d2  libartd.so (art_quick_invoke_stub+338)\n"
396       "  #37 pc 00146ab5  libartd.so "
397       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
398       "const*)+885)\n"
399       "  #38 pc 0039cf0d  libartd.so "
400       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
401       "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
402       "  #39 pc 00392552  libartd.so "
403       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
404       "art::ShadowFrame&, art::JValue, bool)+354)\n"
405       "  #40 pc 0039399a  libartd.so "
406       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
407       "const&, art::ShadowFrame*)+234)\n"
408       "  #41 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
409       "  #42 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\n"
410       "  #43 pc 0000eedb  anonymous:ee74c000 (void Main.runPrimary()+59)\n"
411       "  #44 pc 006ad4d2  libartd.so (art_quick_invoke_stub+338)\n"
412       "  #45 pc 00146ab5  libartd.so "
413       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
414       "const*)+885)\n"
415       "  #46 pc 0039cf0d  libartd.so "
416       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
417       "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
418       "  #47 pc 00392552  libartd.so "
419       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
420       "art::ShadowFrame&, art::JValue, bool)+354)\n"
421       "  #48 pc 0039399a  libartd.so "
422       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
423       "const&, art::ShadowFrame*)+234)\n"
424       "  #49 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
425       "  #50 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\n"
426       "  #51 pc 0000ac21  anonymous:ee74c000 (void Main.main(java.lang.String[])+97)\n"
427       "  #52 pc 006ad6a2  libartd.so (art_quick_invoke_static_stub+418)\n"
428       "  #53 pc 00146acb  libartd.so "
429       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
430       "const*)+907)\n"
431       "  #54 pc 0039cf0d  libartd.so "
432       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
433       "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
434       "  #55 pc 00392552  libartd.so "
435       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
436       "art::ShadowFrame&, art::JValue, bool)+354)\n"
437       "  #56 pc 0039399a  libartd.so "
438       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
439       "const&, art::ShadowFrame*)+234)\n"
440       "  #57 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
441       "  #58 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\n"
442       "  #59 pc 006ad6a2  libartd.so (art_quick_invoke_static_stub+418)\n"
443       "  #60 pc 00146acb  libartd.so "
444       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
445       "const*)+907)\n"
446       "  #61 pc 005aac95  libartd.so "
447       "(art::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable const&, art::ArtMethod*, "
448       "art::ArgArray*, art::JValue*, char const*)+85)\n"
449       "  #62 pc 005aab5a  libartd.so "
450       "(art::InvokeWithVarArgs(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, "
451       "_jmethodID*, char*)+362)\n"
452       "  #63 pc 0048a3dd  libartd.so "
453       "(art::JNI::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, char*)+125)\n"
454       "  #64 pc 0018448c  libartd.so "
455       "(art::CheckJNI::CallMethodV(char const*, _JNIEnv*, _jobject*, _jclass*, _jmethodID*, char*, "
456       "art::Primitive::Type, art::InvokeType)+1964)\n"
457       "  #65 pc 0017cf06  libartd.so "
458       "(art::CheckJNI::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, char*)+70)\n"
459       "  #66 pc 00001d8c  dalvikvm32 "
460       "(_JNIEnv::CallStaticVoidMethod(_jclass*, _jmethodID*, ...)+60)\n"
461       "  #67 pc 00001a80  dalvikvm32 (main+1312)\n"
462       "  #68 pc 00018275  libc.so\n",
463       frame_info);
464   EXPECT_EQ(0xeb89bfb8U, unwinder.frames()[0].pc);
465   EXPECT_EQ(0xffeb5280U, unwinder.frames()[0].sp);
466   EXPECT_EQ(0xeb89af00U, unwinder.frames()[1].pc);
467   EXPECT_EQ(0xffeb52a0U, unwinder.frames()[1].sp);
468   EXPECT_EQ(0xec6061a8U, unwinder.frames()[2].pc);
469   EXPECT_EQ(0xffeb5ce0U, unwinder.frames()[2].sp);
470   EXPECT_EQ(0xee75be80U, unwinder.frames()[3].pc);
471   EXPECT_EQ(0xffeb5d30U, unwinder.frames()[3].sp);
472   EXPECT_EQ(0xf728e4d2U, unwinder.frames()[4].pc);
473   EXPECT_EQ(0xffeb5d60U, unwinder.frames()[4].sp);
474   EXPECT_EQ(0xf6d27ab5U, unwinder.frames()[5].pc);
475   EXPECT_EQ(0xffeb5d80U, unwinder.frames()[5].sp);
476   EXPECT_EQ(0xf6f7df0dU, unwinder.frames()[6].pc);
477   EXPECT_EQ(0xffeb5e20U, unwinder.frames()[6].sp);
478   EXPECT_EQ(0xf6f73552U, unwinder.frames()[7].pc);
479   EXPECT_EQ(0xffeb5ec0U, unwinder.frames()[7].sp);
480   EXPECT_EQ(0xf6f7499aU, unwinder.frames()[8].pc);
481   EXPECT_EQ(0xffeb5f40U, unwinder.frames()[8].sp);
482   EXPECT_EQ(0xf7265362U, unwinder.frames()[9].pc);
483   EXPECT_EQ(0xffeb5fb0U, unwinder.frames()[9].sp);
484   EXPECT_EQ(0xf72945bdU, unwinder.frames()[10].pc);
485   EXPECT_EQ(0xffeb6110U, unwinder.frames()[10].sp);
486   EXPECT_EQ(0xee75be03U, unwinder.frames()[11].pc);
487   EXPECT_EQ(0xffeb6160U, unwinder.frames()[11].sp);
488   EXPECT_EQ(0xf728e4d2U, unwinder.frames()[12].pc);
489   EXPECT_EQ(0xffeb6180U, unwinder.frames()[12].sp);
490   EXPECT_EQ(0xf6d27ab5U, unwinder.frames()[13].pc);
491   EXPECT_EQ(0xffeb61b0U, unwinder.frames()[13].sp);
492   EXPECT_EQ(0xf6f7df0dU, unwinder.frames()[14].pc);
493   EXPECT_EQ(0xffeb6250U, unwinder.frames()[14].sp);
494   EXPECT_EQ(0xf6f73552U, unwinder.frames()[15].pc);
495   EXPECT_EQ(0xffeb62f0U, unwinder.frames()[15].sp);
496   EXPECT_EQ(0xf6f7499aU, unwinder.frames()[16].pc);
497   EXPECT_EQ(0xffeb6370U, unwinder.frames()[16].sp);
498   EXPECT_EQ(0xf7265362U, unwinder.frames()[17].pc);
499   EXPECT_EQ(0xffeb63e0U, unwinder.frames()[17].sp);
500   EXPECT_EQ(0xf72945bdU, unwinder.frames()[18].pc);
501   EXPECT_EQ(0xffeb6530U, unwinder.frames()[18].sp);
502   EXPECT_EQ(0xee75bd3bU, unwinder.frames()[19].pc);
503   EXPECT_EQ(0xffeb6580U, unwinder.frames()[19].sp);
504   EXPECT_EQ(0xf728e4d2U, unwinder.frames()[20].pc);
505   EXPECT_EQ(0xffeb65b0U, unwinder.frames()[20].sp);
506   EXPECT_EQ(0xf6d27ab5U, unwinder.frames()[21].pc);
507   EXPECT_EQ(0xffeb65e0U, unwinder.frames()[21].sp);
508   EXPECT_EQ(0xf6f7df0dU, unwinder.frames()[22].pc);
509   EXPECT_EQ(0xffeb6680U, unwinder.frames()[22].sp);
510   EXPECT_EQ(0xf6f73552U, unwinder.frames()[23].pc);
511   EXPECT_EQ(0xffeb6720U, unwinder.frames()[23].sp);
512   EXPECT_EQ(0xf6f7499aU, unwinder.frames()[24].pc);
513   EXPECT_EQ(0xffeb67a0U, unwinder.frames()[24].sp);
514   EXPECT_EQ(0xf7265362U, unwinder.frames()[25].pc);
515   EXPECT_EQ(0xffeb6810U, unwinder.frames()[25].sp);
516   EXPECT_EQ(0xf72945bdU, unwinder.frames()[26].pc);
517   EXPECT_EQ(0xffeb6960U, unwinder.frames()[26].sp);
518   EXPECT_EQ(0xee75bbdbU, unwinder.frames()[27].pc);
519   EXPECT_EQ(0xffeb69b0U, unwinder.frames()[27].sp);
520   EXPECT_EQ(0xf728e6a2U, unwinder.frames()[28].pc);
521   EXPECT_EQ(0xffeb69f0U, unwinder.frames()[28].sp);
522   EXPECT_EQ(0xf6d27acbU, unwinder.frames()[29].pc);
523   EXPECT_EQ(0xffeb6a20U, unwinder.frames()[29].sp);
524   EXPECT_EQ(0xf6f7df0dU, unwinder.frames()[30].pc);
525   EXPECT_EQ(0xffeb6ac0U, unwinder.frames()[30].sp);
526   EXPECT_EQ(0xf6f73552U, unwinder.frames()[31].pc);
527   EXPECT_EQ(0xffeb6b60U, unwinder.frames()[31].sp);
528   EXPECT_EQ(0xf6f7499aU, unwinder.frames()[32].pc);
529   EXPECT_EQ(0xffeb6be0U, unwinder.frames()[32].sp);
530   EXPECT_EQ(0xf7265362U, unwinder.frames()[33].pc);
531   EXPECT_EQ(0xffeb6c50U, unwinder.frames()[33].sp);
532   EXPECT_EQ(0xf72945bdU, unwinder.frames()[34].pc);
533   EXPECT_EQ(0xffeb6dd0U, unwinder.frames()[34].sp);
534   EXPECT_EQ(0xee75b624U, unwinder.frames()[35].pc);
535   EXPECT_EQ(0xffeb6e20U, unwinder.frames()[35].sp);
536   EXPECT_EQ(0xf728e4d2U, unwinder.frames()[36].pc);
537   EXPECT_EQ(0xffeb6e50U, unwinder.frames()[36].sp);
538   EXPECT_EQ(0xf6d27ab5U, unwinder.frames()[37].pc);
539   EXPECT_EQ(0xffeb6e70U, unwinder.frames()[37].sp);
540   EXPECT_EQ(0xf6f7df0dU, unwinder.frames()[38].pc);
541   EXPECT_EQ(0xffeb6f10U, unwinder.frames()[38].sp);
542   EXPECT_EQ(0xf6f73552U, unwinder.frames()[39].pc);
543   EXPECT_EQ(0xffeb6fb0U, unwinder.frames()[39].sp);
544   EXPECT_EQ(0xf6f7499aU, unwinder.frames()[40].pc);
545   EXPECT_EQ(0xffeb7030U, unwinder.frames()[40].sp);
546   EXPECT_EQ(0xf7265362U, unwinder.frames()[41].pc);
547   EXPECT_EQ(0xffeb70a0U, unwinder.frames()[41].sp);
548   EXPECT_EQ(0xf72945bdU, unwinder.frames()[42].pc);
549   EXPECT_EQ(0xffeb71f0U, unwinder.frames()[42].sp);
550   EXPECT_EQ(0xee75aedbU, unwinder.frames()[43].pc);
551   EXPECT_EQ(0xffeb7240U, unwinder.frames()[43].sp);
552   EXPECT_EQ(0xf728e4d2U, unwinder.frames()[44].pc);
553   EXPECT_EQ(0xffeb72a0U, unwinder.frames()[44].sp);
554   EXPECT_EQ(0xf6d27ab5U, unwinder.frames()[45].pc);
555   EXPECT_EQ(0xffeb72c0U, unwinder.frames()[45].sp);
556   EXPECT_EQ(0xf6f7df0dU, unwinder.frames()[46].pc);
557   EXPECT_EQ(0xffeb7360U, unwinder.frames()[46].sp);
558   EXPECT_EQ(0xf6f73552U, unwinder.frames()[47].pc);
559   EXPECT_EQ(0xffeb7400U, unwinder.frames()[47].sp);
560   EXPECT_EQ(0xf6f7499aU, unwinder.frames()[48].pc);
561   EXPECT_EQ(0xffeb7480U, unwinder.frames()[48].sp);
562   EXPECT_EQ(0xf7265362U, unwinder.frames()[49].pc);
563   EXPECT_EQ(0xffeb74f0U, unwinder.frames()[49].sp);
564   EXPECT_EQ(0xf72945bdU, unwinder.frames()[50].pc);
565   EXPECT_EQ(0xffeb7680U, unwinder.frames()[50].sp);
566   EXPECT_EQ(0xee756c21U, unwinder.frames()[51].pc);
567   EXPECT_EQ(0xffeb76d0U, unwinder.frames()[51].sp);
568   EXPECT_EQ(0xf728e6a2U, unwinder.frames()[52].pc);
569   EXPECT_EQ(0xffeb76f0U, unwinder.frames()[52].sp);
570   EXPECT_EQ(0xf6d27acbU, unwinder.frames()[53].pc);
571   EXPECT_EQ(0xffeb7710U, unwinder.frames()[53].sp);
572   EXPECT_EQ(0xf6f7df0dU, unwinder.frames()[54].pc);
573   EXPECT_EQ(0xffeb77b0U, unwinder.frames()[54].sp);
574   EXPECT_EQ(0xf6f73552U, unwinder.frames()[55].pc);
575   EXPECT_EQ(0xffeb7850U, unwinder.frames()[55].sp);
576   EXPECT_EQ(0xf6f7499aU, unwinder.frames()[56].pc);
577   EXPECT_EQ(0xffeb78d0U, unwinder.frames()[56].sp);
578   EXPECT_EQ(0xf7265362U, unwinder.frames()[57].pc);
579   EXPECT_EQ(0xffeb7940U, unwinder.frames()[57].sp);
580   EXPECT_EQ(0xf72945bdU, unwinder.frames()[58].pc);
581   EXPECT_EQ(0xffeb7a80U, unwinder.frames()[58].sp);
582   EXPECT_EQ(0xf728e6a2U, unwinder.frames()[59].pc);
583   EXPECT_EQ(0xffeb7ad0U, unwinder.frames()[59].sp);
584   EXPECT_EQ(0xf6d27acbU, unwinder.frames()[60].pc);
585   EXPECT_EQ(0xffeb7af0U, unwinder.frames()[60].sp);
586   EXPECT_EQ(0xf718bc95U, unwinder.frames()[61].pc);
587   EXPECT_EQ(0xffeb7b90U, unwinder.frames()[61].sp);
588   EXPECT_EQ(0xf718bb5aU, unwinder.frames()[62].pc);
589   EXPECT_EQ(0xffeb7c50U, unwinder.frames()[62].sp);
590   EXPECT_EQ(0xf706b3ddU, unwinder.frames()[63].pc);
591   EXPECT_EQ(0xffeb7d10U, unwinder.frames()[63].sp);
592   EXPECT_EQ(0xf6d6548cU, unwinder.frames()[64].pc);
593   EXPECT_EQ(0xffeb7d70U, unwinder.frames()[64].sp);
594   EXPECT_EQ(0xf6d5df06U, unwinder.frames()[65].pc);
595   EXPECT_EQ(0xffeb7df0U, unwinder.frames()[65].sp);
596   EXPECT_EQ(0x56574d8cU, unwinder.frames()[66].pc);
597   EXPECT_EQ(0xffeb7e40U, unwinder.frames()[66].sp);
598   EXPECT_EQ(0x56574a80U, unwinder.frames()[67].pc);
599   EXPECT_EQ(0xffeb7e70U, unwinder.frames()[67].sp);
600   EXPECT_EQ(0xf7363275U, unwinder.frames()[68].pc);
601   EXPECT_EQ(0xffeb7ef0U, unwinder.frames()[68].sp);
602 }
603 
TEST_F(UnwindOfflineTest,jit_debug_arm)604 TEST_F(UnwindOfflineTest, jit_debug_arm) {
605   ASSERT_NO_FATAL_FAILURE(Init("jit_debug_arm/", ARCH_ARM));
606 
607   MemoryOfflineParts* memory = new MemoryOfflineParts;
608   AddMemory(dir_ + "descriptor.data", memory);
609   AddMemory(dir_ + "descriptor1.data", memory);
610   AddMemory(dir_ + "stack.data", memory);
611   for (size_t i = 0; i < 7; i++) {
612     AddMemory(dir_ + "entry" + std::to_string(i) + ".data", memory);
613     AddMemory(dir_ + "jit" + std::to_string(i) + ".data", memory);
614   }
615   process_memory_.reset(memory);
616 
617   JitDebug jit_debug(process_memory_);
618   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
619   unwinder.SetJitDebug(&jit_debug, regs_->Arch());
620   unwinder.Unwind();
621 
622   std::string frame_info(DumpFrames(unwinder));
623   ASSERT_EQ(76U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
624   EXPECT_EQ(
625       "  #00 pc 00018a5e  libarttestd.so (Java_Main_unwindInProcess+866)\n"
626       "  #01 pc 0000212d  137-cfi.odex (boolean Main.unwindInProcess(boolean, int, "
627       "boolean)+92)\n"
628       "  #02 pc 00011cb1  anonymous:e2796000 (boolean Main.bar(boolean)+72)\n"
629       "  #03 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
630       "  #04 pc 00467129  libartd.so (art_quick_invoke_stub+228)\n"
631       "  #05 pc 000bf7a9  libartd.so "
632       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
633       "const*)+864)\n"
634       "  #06 pc 00247833  libartd.so "
635       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
636       "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
637       "  #07 pc 0022e935  libartd.so "
638       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
639       "art::ShadowFrame&, art::JValue, bool)+244)\n"
640       "  #08 pc 0022f71d  libartd.so "
641       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
642       "const&, art::ShadowFrame*)+128)\n"
643       "  #09 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
644       "  #10 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
645       "  #11 pc 00011c31  anonymous:e2796000 (int Main.compare(Main, Main)+64)\n"
646       "  #12 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
647       "  #13 pc 00467129  libartd.so (art_quick_invoke_stub+228)\n"
648       "  #14 pc 000bf7a9  libartd.so "
649       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
650       "const*)+864)\n"
651       "  #15 pc 00247833  libartd.so "
652       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
653       "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
654       "  #16 pc 0022e935  libartd.so "
655       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
656       "art::ShadowFrame&, art::JValue, bool)+244)\n"
657       "  #17 pc 0022f71d  libartd.so "
658       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
659       "const&, art::ShadowFrame*)+128)\n"
660       "  #18 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
661       "  #19 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
662       "  #20 pc 00011b77  anonymous:e2796000 (int Main.compare(java.lang.Object, "
663       "java.lang.Object)+118)\n"
664       "  #21 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
665       "  #22 pc 00467129  libartd.so (art_quick_invoke_stub+228)\n"
666       "  #23 pc 000bf7a9  libartd.so "
667       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
668       "const*)+864)\n"
669       "  #24 pc 00247833  libartd.so "
670       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
671       "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
672       "  #25 pc 0022e935  libartd.so "
673       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
674       "art::ShadowFrame&, art::JValue, bool)+244)\n"
675       "  #26 pc 0022f71d  libartd.so "
676       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
677       "const&, art::ShadowFrame*)+128)\n"
678       "  #27 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
679       "  #28 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
680       "  #29 pc 00011a29  anonymous:e2796000 (int "
681       "java.util.Arrays.binarySearch0(java.lang.Object[], int, int, java.lang.Object, "
682       "java.util.Comparator)+304)\n"
683       "  #30 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
684       "  #31 pc 0046722f  libartd.so (art_quick_invoke_static_stub+226)\n"
685       "  #32 pc 000bf7bb  libartd.so "
686       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
687       "const*)+882)\n"
688       "  #33 pc 00247833  libartd.so "
689       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
690       "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
691       "  #34 pc 0022e935  libartd.so "
692       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
693       "art::ShadowFrame&, art::JValue, bool)+244)\n"
694       "  #35 pc 0022f71d  libartd.so "
695       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
696       "const&, art::ShadowFrame*)+128)\n"
697       "  #36 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
698       "  #37 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
699       "  #38 pc 0001139b  anonymous:e2796000 (boolean Main.foo()+178)\n"
700       "  #39 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
701       "  #40 pc 00467129  libartd.so (art_quick_invoke_stub+228)\n"
702       "  #41 pc 000bf7a9  libartd.so "
703       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
704       "const*)+864)\n"
705       "  #42 pc 00247833  libartd.so "
706       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
707       "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
708       "  #43 pc 0022e935  libartd.so "
709       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
710       "art::ShadowFrame&, art::JValue, bool)+244)\n"
711       "  #44 pc 0022f71d  libartd.so "
712       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
713       "const&, art::ShadowFrame*)+128)\n"
714       "  #45 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
715       "  #46 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
716       "  #47 pc 00010aa7  anonymous:e2796000 (void Main.runPrimary()+70)\n"
717       "  #48 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
718       "  #49 pc 00467129  libartd.so (art_quick_invoke_stub+228)\n"
719       "  #50 pc 000bf7a9  libartd.so "
720       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
721       "const*)+864)\n"
722       "  #51 pc 00247833  libartd.so "
723       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
724       "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
725       "  #52 pc 0022e935  libartd.so "
726       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
727       "art::ShadowFrame&, art::JValue, bool)+244)\n"
728       "  #53 pc 0022f71d  libartd.so "
729       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
730       "const&, art::ShadowFrame*)+128)\n"
731       "  #54 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
732       "  #55 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
733       "  #56 pc 0000ba99  anonymous:e2796000 (void Main.main(java.lang.String[])+144)\n"
734       "  #57 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
735       "  #58 pc 0046722f  libartd.so (art_quick_invoke_static_stub+226)\n"
736       "  #59 pc 000bf7bb  libartd.so "
737       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
738       "const*)+882)\n"
739       "  #60 pc 00247833  libartd.so "
740       "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
741       "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
742       "  #61 pc 0022e935  libartd.so "
743       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
744       "art::ShadowFrame&, art::JValue, bool)+244)\n"
745       "  #62 pc 0022f71d  libartd.so "
746       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
747       "const&, art::ShadowFrame*)+128)\n"
748       "  #63 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
749       "  #64 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
750       "  #65 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
751       "  #66 pc 0046722f  libartd.so (art_quick_invoke_static_stub+226)\n"
752       "  #67 pc 000bf7bb  libartd.so "
753       "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
754       "const*)+882)\n"
755       "  #68 pc 003b292d  libartd.so "
756       "(art::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable const&, art::ArtMethod*, "
757       "art::ArgArray*, art::JValue*, char const*)+52)\n"
758       "  #69 pc 003b26c3  libartd.so "
759       "(art::InvokeWithVarArgs(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, "
760       "_jmethodID*, std::__va_list)+210)\n"
761       "  #70 pc 00308411  libartd.so "
762       "(art::JNI::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, std::__va_list)+76)\n"
763       "  #71 pc 000e6a9f  libartd.so "
764       "(art::CheckJNI::CallMethodV(char const*, _JNIEnv*, _jobject*, _jclass*, _jmethodID*, "
765       "std::__va_list, art::Primitive::Type, art::InvokeType)+1486)\n"
766       "  #72 pc 000e19b9  libartd.so "
767       "(art::CheckJNI::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, std::__va_list)+40)\n"
768       "  #73 pc 0000159f  dalvikvm32 "
769       "(_JNIEnv::CallStaticVoidMethod(_jclass*, _jmethodID*, ...)+30)\n"
770       "  #74 pc 00001349  dalvikvm32 (main+896)\n"
771       "  #75 pc 000850c9  libc.so\n",
772       frame_info);
773   EXPECT_EQ(0xdfe66a5eU, unwinder.frames()[0].pc);
774   EXPECT_EQ(0xff85d180U, unwinder.frames()[0].sp);
775   EXPECT_EQ(0xe044712dU, unwinder.frames()[1].pc);
776   EXPECT_EQ(0xff85d200U, unwinder.frames()[1].sp);
777   EXPECT_EQ(0xe27a7cb1U, unwinder.frames()[2].pc);
778   EXPECT_EQ(0xff85d290U, unwinder.frames()[2].sp);
779   EXPECT_EQ(0xed75c175U, unwinder.frames()[3].pc);
780   EXPECT_EQ(0xff85d2b0U, unwinder.frames()[3].sp);
781   EXPECT_EQ(0xed761129U, unwinder.frames()[4].pc);
782   EXPECT_EQ(0xff85d2e8U, unwinder.frames()[4].sp);
783   EXPECT_EQ(0xed3b97a9U, unwinder.frames()[5].pc);
784   EXPECT_EQ(0xff85d370U, unwinder.frames()[5].sp);
785   EXPECT_EQ(0xed541833U, unwinder.frames()[6].pc);
786   EXPECT_EQ(0xff85d3d8U, unwinder.frames()[6].sp);
787   EXPECT_EQ(0xed528935U, unwinder.frames()[7].pc);
788   EXPECT_EQ(0xff85d428U, unwinder.frames()[7].sp);
789   EXPECT_EQ(0xed52971dU, unwinder.frames()[8].pc);
790   EXPECT_EQ(0xff85d470U, unwinder.frames()[8].sp);
791   EXPECT_EQ(0xed73c865U, unwinder.frames()[9].pc);
792   EXPECT_EQ(0xff85d4b0U, unwinder.frames()[9].sp);
793   EXPECT_EQ(0xed7606ffU, unwinder.frames()[10].pc);
794   EXPECT_EQ(0xff85d5d0U, unwinder.frames()[10].sp);
795   EXPECT_EQ(0xe27a7c31U, unwinder.frames()[11].pc);
796   EXPECT_EQ(0xff85d640U, unwinder.frames()[11].sp);
797   EXPECT_EQ(0xed75c175U, unwinder.frames()[12].pc);
798   EXPECT_EQ(0xff85d660U, unwinder.frames()[12].sp);
799   EXPECT_EQ(0xed761129U, unwinder.frames()[13].pc);
800   EXPECT_EQ(0xff85d698U, unwinder.frames()[13].sp);
801   EXPECT_EQ(0xed3b97a9U, unwinder.frames()[14].pc);
802   EXPECT_EQ(0xff85d720U, unwinder.frames()[14].sp);
803   EXPECT_EQ(0xed541833U, unwinder.frames()[15].pc);
804   EXPECT_EQ(0xff85d788U, unwinder.frames()[15].sp);
805   EXPECT_EQ(0xed528935U, unwinder.frames()[16].pc);
806   EXPECT_EQ(0xff85d7d8U, unwinder.frames()[16].sp);
807   EXPECT_EQ(0xed52971dU, unwinder.frames()[17].pc);
808   EXPECT_EQ(0xff85d820U, unwinder.frames()[17].sp);
809   EXPECT_EQ(0xed73c865U, unwinder.frames()[18].pc);
810   EXPECT_EQ(0xff85d860U, unwinder.frames()[18].sp);
811   EXPECT_EQ(0xed7606ffU, unwinder.frames()[19].pc);
812   EXPECT_EQ(0xff85d970U, unwinder.frames()[19].sp);
813   EXPECT_EQ(0xe27a7b77U, unwinder.frames()[20].pc);
814   EXPECT_EQ(0xff85d9e0U, unwinder.frames()[20].sp);
815   EXPECT_EQ(0xed75c175U, unwinder.frames()[21].pc);
816   EXPECT_EQ(0xff85da10U, unwinder.frames()[21].sp);
817   EXPECT_EQ(0xed761129U, unwinder.frames()[22].pc);
818   EXPECT_EQ(0xff85da48U, unwinder.frames()[22].sp);
819   EXPECT_EQ(0xed3b97a9U, unwinder.frames()[23].pc);
820   EXPECT_EQ(0xff85dad0U, unwinder.frames()[23].sp);
821   EXPECT_EQ(0xed541833U, unwinder.frames()[24].pc);
822   EXPECT_EQ(0xff85db38U, unwinder.frames()[24].sp);
823   EXPECT_EQ(0xed528935U, unwinder.frames()[25].pc);
824   EXPECT_EQ(0xff85db88U, unwinder.frames()[25].sp);
825   EXPECT_EQ(0xed52971dU, unwinder.frames()[26].pc);
826   EXPECT_EQ(0xff85dbd0U, unwinder.frames()[26].sp);
827   EXPECT_EQ(0xed73c865U, unwinder.frames()[27].pc);
828   EXPECT_EQ(0xff85dc10U, unwinder.frames()[27].sp);
829   EXPECT_EQ(0xed7606ffU, unwinder.frames()[28].pc);
830   EXPECT_EQ(0xff85dd20U, unwinder.frames()[28].sp);
831   EXPECT_EQ(0xe27a7a29U, unwinder.frames()[29].pc);
832   EXPECT_EQ(0xff85dd90U, unwinder.frames()[29].sp);
833   EXPECT_EQ(0xed75c175U, unwinder.frames()[30].pc);
834   EXPECT_EQ(0xff85ddc0U, unwinder.frames()[30].sp);
835   EXPECT_EQ(0xed76122fU, unwinder.frames()[31].pc);
836   EXPECT_EQ(0xff85de08U, unwinder.frames()[31].sp);
837   EXPECT_EQ(0xed3b97bbU, unwinder.frames()[32].pc);
838   EXPECT_EQ(0xff85de90U, unwinder.frames()[32].sp);
839   EXPECT_EQ(0xed541833U, unwinder.frames()[33].pc);
840   EXPECT_EQ(0xff85def8U, unwinder.frames()[33].sp);
841   EXPECT_EQ(0xed528935U, unwinder.frames()[34].pc);
842   EXPECT_EQ(0xff85df48U, unwinder.frames()[34].sp);
843   EXPECT_EQ(0xed52971dU, unwinder.frames()[35].pc);
844   EXPECT_EQ(0xff85df90U, unwinder.frames()[35].sp);
845   EXPECT_EQ(0xed73c865U, unwinder.frames()[36].pc);
846   EXPECT_EQ(0xff85dfd0U, unwinder.frames()[36].sp);
847   EXPECT_EQ(0xed7606ffU, unwinder.frames()[37].pc);
848   EXPECT_EQ(0xff85e110U, unwinder.frames()[37].sp);
849   EXPECT_EQ(0xe27a739bU, unwinder.frames()[38].pc);
850   EXPECT_EQ(0xff85e180U, unwinder.frames()[38].sp);
851   EXPECT_EQ(0xed75c175U, unwinder.frames()[39].pc);
852   EXPECT_EQ(0xff85e1b0U, unwinder.frames()[39].sp);
853   EXPECT_EQ(0xed761129U, unwinder.frames()[40].pc);
854   EXPECT_EQ(0xff85e1e0U, unwinder.frames()[40].sp);
855   EXPECT_EQ(0xed3b97a9U, unwinder.frames()[41].pc);
856   EXPECT_EQ(0xff85e268U, unwinder.frames()[41].sp);
857   EXPECT_EQ(0xed541833U, unwinder.frames()[42].pc);
858   EXPECT_EQ(0xff85e2d0U, unwinder.frames()[42].sp);
859   EXPECT_EQ(0xed528935U, unwinder.frames()[43].pc);
860   EXPECT_EQ(0xff85e320U, unwinder.frames()[43].sp);
861   EXPECT_EQ(0xed52971dU, unwinder.frames()[44].pc);
862   EXPECT_EQ(0xff85e368U, unwinder.frames()[44].sp);
863   EXPECT_EQ(0xed73c865U, unwinder.frames()[45].pc);
864   EXPECT_EQ(0xff85e3a8U, unwinder.frames()[45].sp);
865   EXPECT_EQ(0xed7606ffU, unwinder.frames()[46].pc);
866   EXPECT_EQ(0xff85e4c0U, unwinder.frames()[46].sp);
867   EXPECT_EQ(0xe27a6aa7U, unwinder.frames()[47].pc);
868   EXPECT_EQ(0xff85e530U, unwinder.frames()[47].sp);
869   EXPECT_EQ(0xed75c175U, unwinder.frames()[48].pc);
870   EXPECT_EQ(0xff85e5a0U, unwinder.frames()[48].sp);
871   EXPECT_EQ(0xed761129U, unwinder.frames()[49].pc);
872   EXPECT_EQ(0xff85e5d8U, unwinder.frames()[49].sp);
873   EXPECT_EQ(0xed3b97a9U, unwinder.frames()[50].pc);
874   EXPECT_EQ(0xff85e660U, unwinder.frames()[50].sp);
875   EXPECT_EQ(0xed541833U, unwinder.frames()[51].pc);
876   EXPECT_EQ(0xff85e6c8U, unwinder.frames()[51].sp);
877   EXPECT_EQ(0xed528935U, unwinder.frames()[52].pc);
878   EXPECT_EQ(0xff85e718U, unwinder.frames()[52].sp);
879   EXPECT_EQ(0xed52971dU, unwinder.frames()[53].pc);
880   EXPECT_EQ(0xff85e760U, unwinder.frames()[53].sp);
881   EXPECT_EQ(0xed73c865U, unwinder.frames()[54].pc);
882   EXPECT_EQ(0xff85e7a0U, unwinder.frames()[54].sp);
883   EXPECT_EQ(0xed7606ffU, unwinder.frames()[55].pc);
884   EXPECT_EQ(0xff85e8f0U, unwinder.frames()[55].sp);
885   EXPECT_EQ(0xe27a1a99U, unwinder.frames()[56].pc);
886   EXPECT_EQ(0xff85e960U, unwinder.frames()[56].sp);
887   EXPECT_EQ(0xed75c175U, unwinder.frames()[57].pc);
888   EXPECT_EQ(0xff85e990U, unwinder.frames()[57].sp);
889   EXPECT_EQ(0xed76122fU, unwinder.frames()[58].pc);
890   EXPECT_EQ(0xff85e9c8U, unwinder.frames()[58].sp);
891   EXPECT_EQ(0xed3b97bbU, unwinder.frames()[59].pc);
892   EXPECT_EQ(0xff85ea50U, unwinder.frames()[59].sp);
893   EXPECT_EQ(0xed541833U, unwinder.frames()[60].pc);
894   EXPECT_EQ(0xff85eab8U, unwinder.frames()[60].sp);
895   EXPECT_EQ(0xed528935U, unwinder.frames()[61].pc);
896   EXPECT_EQ(0xff85eb08U, unwinder.frames()[61].sp);
897   EXPECT_EQ(0xed52971dU, unwinder.frames()[62].pc);
898   EXPECT_EQ(0xff85eb50U, unwinder.frames()[62].sp);
899   EXPECT_EQ(0xed73c865U, unwinder.frames()[63].pc);
900   EXPECT_EQ(0xff85eb90U, unwinder.frames()[63].sp);
901   EXPECT_EQ(0xed7606ffU, unwinder.frames()[64].pc);
902   EXPECT_EQ(0xff85ec90U, unwinder.frames()[64].sp);
903   EXPECT_EQ(0xed75c175U, unwinder.frames()[65].pc);
904   EXPECT_EQ(0xff85ed00U, unwinder.frames()[65].sp);
905   EXPECT_EQ(0xed76122fU, unwinder.frames()[66].pc);
906   EXPECT_EQ(0xff85ed38U, unwinder.frames()[66].sp);
907   EXPECT_EQ(0xed3b97bbU, unwinder.frames()[67].pc);
908   EXPECT_EQ(0xff85edc0U, unwinder.frames()[67].sp);
909   EXPECT_EQ(0xed6ac92dU, unwinder.frames()[68].pc);
910   EXPECT_EQ(0xff85ee28U, unwinder.frames()[68].sp);
911   EXPECT_EQ(0xed6ac6c3U, unwinder.frames()[69].pc);
912   EXPECT_EQ(0xff85eeb8U, unwinder.frames()[69].sp);
913   EXPECT_EQ(0xed602411U, unwinder.frames()[70].pc);
914   EXPECT_EQ(0xff85ef48U, unwinder.frames()[70].sp);
915   EXPECT_EQ(0xed3e0a9fU, unwinder.frames()[71].pc);
916   EXPECT_EQ(0xff85ef90U, unwinder.frames()[71].sp);
917   EXPECT_EQ(0xed3db9b9U, unwinder.frames()[72].pc);
918   EXPECT_EQ(0xff85f008U, unwinder.frames()[72].sp);
919   EXPECT_EQ(0xab0d459fU, unwinder.frames()[73].pc);
920   EXPECT_EQ(0xff85f038U, unwinder.frames()[73].sp);
921   EXPECT_EQ(0xab0d4349U, unwinder.frames()[74].pc);
922   EXPECT_EQ(0xff85f050U, unwinder.frames()[74].sp);
923   EXPECT_EQ(0xedb0d0c9U, unwinder.frames()[75].pc);
924   EXPECT_EQ(0xff85f0c0U, unwinder.frames()[75].sp);
925 }
926 
927 struct LeakType {
LeakTypeunwindstack::LeakType928   LeakType(Maps* maps, Regs* regs, std::shared_ptr<Memory>& process_memory)
929       : maps(maps), regs(regs), process_memory(process_memory) {}
930 
931   Maps* maps;
932   Regs* regs;
933   std::shared_ptr<Memory>& process_memory;
934 };
935 
OfflineUnwind(void * data)936 static void OfflineUnwind(void* data) {
937   LeakType* leak_data = reinterpret_cast<LeakType*>(data);
938 
939   std::unique_ptr<Regs> regs_copy(leak_data->regs->Clone());
940   JitDebug jit_debug(leak_data->process_memory);
941   Unwinder unwinder(128, leak_data->maps, regs_copy.get(), leak_data->process_memory);
942   unwinder.SetJitDebug(&jit_debug, regs_copy->Arch());
943   unwinder.Unwind();
944   ASSERT_EQ(76U, unwinder.NumFrames());
945 }
946 
TEST_F(UnwindOfflineTest,unwind_offline_check_for_leaks)947 TEST_F(UnwindOfflineTest, unwind_offline_check_for_leaks) {
948   ASSERT_NO_FATAL_FAILURE(Init("jit_debug_arm/", ARCH_ARM));
949 
950   MemoryOfflineParts* memory = new MemoryOfflineParts;
951   AddMemory(dir_ + "descriptor.data", memory);
952   AddMemory(dir_ + "descriptor1.data", memory);
953   AddMemory(dir_ + "stack.data", memory);
954   for (size_t i = 0; i < 7; i++) {
955     AddMemory(dir_ + "entry" + std::to_string(i) + ".data", memory);
956     AddMemory(dir_ + "jit" + std::to_string(i) + ".data", memory);
957   }
958   process_memory_.reset(memory);
959 
960   LeakType data(maps_.get(), regs_.get(), process_memory_);
961   TestCheckForLeaks(OfflineUnwind, &data);
962 }
963 
964 // The eh_frame_hdr data is present but set to zero fdes. This should
965 // fallback to iterating over the cies/fdes and ignore the eh_frame_hdr.
966 // No .gnu_debugdata section in the elf file, so no symbols.
TEST_F(UnwindOfflineTest,bad_eh_frame_hdr_arm64)967 TEST_F(UnwindOfflineTest, bad_eh_frame_hdr_arm64) {
968   ASSERT_NO_FATAL_FAILURE(Init("bad_eh_frame_hdr_arm64/", ARCH_ARM64));
969 
970   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
971   unwinder.Unwind();
972 
973   std::string frame_info(DumpFrames(unwinder));
974   ASSERT_EQ(5U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
975   EXPECT_EQ(
976       "  #00 pc 0000000000000550  waiter64\n"
977       "  #01 pc 0000000000000568  waiter64\n"
978       "  #02 pc 000000000000057c  waiter64\n"
979       "  #03 pc 0000000000000590  waiter64\n"
980       "  #04 pc 00000000000a8e98  libc.so (__libc_init+88)\n",
981       frame_info);
982   EXPECT_EQ(0x60a9fdf550U, unwinder.frames()[0].pc);
983   EXPECT_EQ(0x7fdd141990U, unwinder.frames()[0].sp);
984   EXPECT_EQ(0x60a9fdf568U, unwinder.frames()[1].pc);
985   EXPECT_EQ(0x7fdd1419a0U, unwinder.frames()[1].sp);
986   EXPECT_EQ(0x60a9fdf57cU, unwinder.frames()[2].pc);
987   EXPECT_EQ(0x7fdd1419b0U, unwinder.frames()[2].sp);
988   EXPECT_EQ(0x60a9fdf590U, unwinder.frames()[3].pc);
989   EXPECT_EQ(0x7fdd1419c0U, unwinder.frames()[3].sp);
990   EXPECT_EQ(0x7542d68e98U, unwinder.frames()[4].pc);
991   EXPECT_EQ(0x7fdd1419d0U, unwinder.frames()[4].sp);
992 }
993 
994 // The elf has bad eh_frame unwind information for the pcs. If eh_frame
995 // is used first, the unwind will not match the expected output.
TEST_F(UnwindOfflineTest,debug_frame_first_x86)996 TEST_F(UnwindOfflineTest, debug_frame_first_x86) {
997   ASSERT_NO_FATAL_FAILURE(Init("debug_frame_first_x86/", ARCH_X86));
998 
999   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1000   unwinder.Unwind();
1001 
1002   std::string frame_info(DumpFrames(unwinder));
1003   ASSERT_EQ(5U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1004   EXPECT_EQ(
1005       "  #00 pc 00000685  waiter (call_level3+53)\n"
1006       "  #01 pc 000006b7  waiter (call_level2+23)\n"
1007       "  #02 pc 000006d7  waiter (call_level1+23)\n"
1008       "  #03 pc 000006f7  waiter (main+23)\n"
1009       "  #04 pc 00018275  libc.so\n",
1010       frame_info);
1011   EXPECT_EQ(0x56598685U, unwinder.frames()[0].pc);
1012   EXPECT_EQ(0xffcf9e38U, unwinder.frames()[0].sp);
1013   EXPECT_EQ(0x565986b7U, unwinder.frames()[1].pc);
1014   EXPECT_EQ(0xffcf9e50U, unwinder.frames()[1].sp);
1015   EXPECT_EQ(0x565986d7U, unwinder.frames()[2].pc);
1016   EXPECT_EQ(0xffcf9e60U, unwinder.frames()[2].sp);
1017   EXPECT_EQ(0x565986f7U, unwinder.frames()[3].pc);
1018   EXPECT_EQ(0xffcf9e70U, unwinder.frames()[3].sp);
1019   EXPECT_EQ(0xf744a275U, unwinder.frames()[4].pc);
1020   EXPECT_EQ(0xffcf9e80U, unwinder.frames()[4].sp);
1021 }
1022 
1023 // Make sure that a pc that is at the beginning of an fde unwinds correctly.
TEST_F(UnwindOfflineTest,eh_frame_hdr_begin_x86_64)1024 TEST_F(UnwindOfflineTest, eh_frame_hdr_begin_x86_64) {
1025   ASSERT_NO_FATAL_FAILURE(Init("eh_frame_hdr_begin_x86_64/", ARCH_X86_64));
1026 
1027   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1028   unwinder.Unwind();
1029 
1030   std::string frame_info(DumpFrames(unwinder));
1031   ASSERT_EQ(5U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1032   EXPECT_EQ(
1033       "  #00 pc 0000000000000a80  unwind_test64 (calling3)\n"
1034       "  #01 pc 0000000000000dd9  unwind_test64 (calling2+633)\n"
1035       "  #02 pc 000000000000121e  unwind_test64 (calling1+638)\n"
1036       "  #03 pc 00000000000013ed  unwind_test64 (main+13)\n"
1037       "  #04 pc 00000000000202b0  libc.so\n",
1038       frame_info);
1039   EXPECT_EQ(0x561550b17a80U, unwinder.frames()[0].pc);
1040   EXPECT_EQ(0x7ffcc8596ce8U, unwinder.frames()[0].sp);
1041   EXPECT_EQ(0x561550b17dd9U, unwinder.frames()[1].pc);
1042   EXPECT_EQ(0x7ffcc8596cf0U, unwinder.frames()[1].sp);
1043   EXPECT_EQ(0x561550b1821eU, unwinder.frames()[2].pc);
1044   EXPECT_EQ(0x7ffcc8596f40U, unwinder.frames()[2].sp);
1045   EXPECT_EQ(0x561550b183edU, unwinder.frames()[3].pc);
1046   EXPECT_EQ(0x7ffcc8597190U, unwinder.frames()[3].sp);
1047   EXPECT_EQ(0x7f4de62162b0U, unwinder.frames()[4].pc);
1048   EXPECT_EQ(0x7ffcc85971a0U, unwinder.frames()[4].sp);
1049 }
1050 
TEST_F(UnwindOfflineTest,art_quick_osr_stub_arm)1051 TEST_F(UnwindOfflineTest, art_quick_osr_stub_arm) {
1052   ASSERT_NO_FATAL_FAILURE(Init("art_quick_osr_stub_arm/", ARCH_ARM));
1053 
1054   MemoryOfflineParts* memory = new MemoryOfflineParts;
1055   AddMemory(dir_ + "descriptor.data", memory);
1056   AddMemory(dir_ + "stack.data", memory);
1057   for (size_t i = 0; i < 2; i++) {
1058     AddMemory(dir_ + "entry" + std::to_string(i) + ".data", memory);
1059     AddMemory(dir_ + "jit" + std::to_string(i) + ".data", memory);
1060   }
1061   process_memory_.reset(memory);
1062 
1063   JitDebug jit_debug(process_memory_);
1064   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1065   unwinder.SetJitDebug(&jit_debug, regs_->Arch());
1066   unwinder.Unwind();
1067 
1068   std::string frame_info(DumpFrames(unwinder));
1069   ASSERT_EQ(25U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1070   EXPECT_EQ(
1071       "  #00 pc 0000c788  <anonymous:d0250000> "
1072       "(com.example.simpleperf.simpleperfexamplewithnative.MixActivity.access$000)\n"
1073       "  #01 pc 0000cdd5  <anonymous:d0250000> "
1074       "(com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run+60)\n"
1075       "  #02 pc 004135bb  libart.so (art_quick_osr_stub+42)\n"
1076       "  #03 pc 002657a5  libart.so "
1077       "(art::jit::Jit::MaybeDoOnStackReplacement(art::Thread*, art::ArtMethod*, unsigned int, int, "
1078       "art::JValue*)+876)\n"
1079       "  #04 pc 004021a7  libart.so (MterpMaybeDoOnStackReplacement+86)\n"
1080       "  #05 pc 00412474  libart.so (ExecuteMterpImpl+66164)\n"
1081       "  #06 pc cd8365b0  <unknown>\n"  // symbol in dex file
1082       "  #07 pc 001d7f1b  libart.so "
1083       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
1084       "art::ShadowFrame&, art::JValue, bool)+374)\n"
1085       "  #08 pc 001dc593  libart.so "
1086       "(art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, "
1087       "art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+154)\n"
1088       "  #09 pc 001f4d01  libart.so "
1089       "(bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, "
1090       "art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+732)\n"
1091       "  #10 pc 003fe427  libart.so (MterpInvokeInterface+1354)\n"
1092       "  #11 pc 00405b94  libart.so (ExecuteMterpImpl+14740)\n"
1093       "  #12 pc 7004873e  <unknown>\n"  // symbol in dex file
1094       "  #13 pc 001d7f1b  libart.so "
1095       "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
1096       "art::ShadowFrame&, art::JValue, bool)+374)\n"
1097       "  #14 pc 001dc4d5  libart.so "
1098       "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
1099       "const&, art::ShadowFrame*)+92)\n"
1100       "  #15 pc 003f25ab  libart.so (artQuickToInterpreterBridge+970)\n"
1101       "  #16 pc 00417aff  libart.so (art_quick_to_interpreter_bridge+30)\n"
1102       "  #17 pc 00413575  libart.so (art_quick_invoke_stub_internal+68)\n"
1103       "  #18 pc 00418531  libart.so (art_quick_invoke_stub+236)\n"
1104       "  #19 pc 000b468d  libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned "
1105       "int, art::JValue*, char const*)+136)\n"
1106       "  #20 pc 00362f49  libart.so "
1107       "(art::(anonymous namespace)::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable "
1108       "const&, art::ArtMethod*, art::(anonymous namespace)::ArgArray*, art::JValue*, char "
1109       "const*)+52)\n"
1110       "  #21 pc 00363cd9  libart.so "
1111       "(art::InvokeVirtualOrInterfaceWithJValues(art::ScopedObjectAccessAlreadyRunnable const&, "
1112       "_jobject*, _jmethodID*, jvalue*)+332)\n"
1113       "  #22 pc 003851dd  libart.so (art::Thread::CreateCallback(void*)+868)\n"
1114       "  #23 pc 00062925  libc.so (__pthread_start(void*)+22)\n"
1115       "  #24 pc 0001de39  libc.so (__start_thread+24)\n",
1116       frame_info);
1117   EXPECT_EQ(0xd025c788U, unwinder.frames()[0].pc);
1118   EXPECT_EQ(0xcd4ff140U, unwinder.frames()[0].sp);
1119   EXPECT_EQ(0xd025cdd5U, unwinder.frames()[1].pc);
1120   EXPECT_EQ(0xcd4ff140U, unwinder.frames()[1].sp);
1121   EXPECT_EQ(0xe4a755bbU, unwinder.frames()[2].pc);
1122   EXPECT_EQ(0xcd4ff160U, unwinder.frames()[2].sp);
1123   EXPECT_EQ(0xe48c77a5U, unwinder.frames()[3].pc);
1124   EXPECT_EQ(0xcd4ff190U, unwinder.frames()[3].sp);
1125   EXPECT_EQ(0xe4a641a7U, unwinder.frames()[4].pc);
1126   EXPECT_EQ(0xcd4ff298U, unwinder.frames()[4].sp);
1127   EXPECT_EQ(0xe4a74474U, unwinder.frames()[5].pc);
1128   EXPECT_EQ(0xcd4ff2b8U, unwinder.frames()[5].sp);
1129   EXPECT_EQ(0xcd8365b0U, unwinder.frames()[6].pc);
1130   EXPECT_EQ(0xcd4ff2e0U, unwinder.frames()[6].sp);
1131   EXPECT_EQ(0xe4839f1bU, unwinder.frames()[7].pc);
1132   EXPECT_EQ(0xcd4ff2e0U, unwinder.frames()[7].sp);
1133   EXPECT_EQ(0xe483e593U, unwinder.frames()[8].pc);
1134   EXPECT_EQ(0xcd4ff330U, unwinder.frames()[8].sp);
1135   EXPECT_EQ(0xe4856d01U, unwinder.frames()[9].pc);
1136   EXPECT_EQ(0xcd4ff380U, unwinder.frames()[9].sp);
1137   EXPECT_EQ(0xe4a60427U, unwinder.frames()[10].pc);
1138   EXPECT_EQ(0xcd4ff430U, unwinder.frames()[10].sp);
1139   EXPECT_EQ(0xe4a67b94U, unwinder.frames()[11].pc);
1140   EXPECT_EQ(0xcd4ff498U, unwinder.frames()[11].sp);
1141   EXPECT_EQ(0x7004873eU, unwinder.frames()[12].pc);
1142   EXPECT_EQ(0xcd4ff4c0U, unwinder.frames()[12].sp);
1143   EXPECT_EQ(0xe4839f1bU, unwinder.frames()[13].pc);
1144   EXPECT_EQ(0xcd4ff4c0U, unwinder.frames()[13].sp);
1145   EXPECT_EQ(0xe483e4d5U, unwinder.frames()[14].pc);
1146   EXPECT_EQ(0xcd4ff510U, unwinder.frames()[14].sp);
1147   EXPECT_EQ(0xe4a545abU, unwinder.frames()[15].pc);
1148   EXPECT_EQ(0xcd4ff538U, unwinder.frames()[15].sp);
1149   EXPECT_EQ(0xe4a79affU, unwinder.frames()[16].pc);
1150   EXPECT_EQ(0xcd4ff640U, unwinder.frames()[16].sp);
1151   EXPECT_EQ(0xe4a75575U, unwinder.frames()[17].pc);
1152   EXPECT_EQ(0xcd4ff6b0U, unwinder.frames()[17].sp);
1153   EXPECT_EQ(0xe4a7a531U, unwinder.frames()[18].pc);
1154   EXPECT_EQ(0xcd4ff6e8U, unwinder.frames()[18].sp);
1155   EXPECT_EQ(0xe471668dU, unwinder.frames()[19].pc);
1156   EXPECT_EQ(0xcd4ff770U, unwinder.frames()[19].sp);
1157   EXPECT_EQ(0xe49c4f49U, unwinder.frames()[20].pc);
1158   EXPECT_EQ(0xcd4ff7c8U, unwinder.frames()[20].sp);
1159   EXPECT_EQ(0xe49c5cd9U, unwinder.frames()[21].pc);
1160   EXPECT_EQ(0xcd4ff850U, unwinder.frames()[21].sp);
1161   EXPECT_EQ(0xe49e71ddU, unwinder.frames()[22].pc);
1162   EXPECT_EQ(0xcd4ff8e8U, unwinder.frames()[22].sp);
1163   EXPECT_EQ(0xe7df3925U, unwinder.frames()[23].pc);
1164   EXPECT_EQ(0xcd4ff958U, unwinder.frames()[23].sp);
1165   EXPECT_EQ(0xe7daee39U, unwinder.frames()[24].pc);
1166   EXPECT_EQ(0xcd4ff960U, unwinder.frames()[24].sp);
1167 }
1168 
TEST_F(UnwindOfflineTest,jit_map_arm)1169 TEST_F(UnwindOfflineTest, jit_map_arm) {
1170   ASSERT_NO_FATAL_FAILURE(Init("jit_map_arm/", ARCH_ARM));
1171 
1172   maps_->Add(0xd025c788, 0xd025c9f0, 0, PROT_READ | PROT_EXEC | MAPS_FLAGS_JIT_SYMFILE_MAP,
1173              "jit_map0.so", 0);
1174   maps_->Add(0xd025cd98, 0xd025cff4, 0, PROT_READ | PROT_EXEC | MAPS_FLAGS_JIT_SYMFILE_MAP,
1175              "jit_map1.so", 0);
1176   maps_->Sort();
1177 
1178   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1179   unwinder.Unwind();
1180 
1181   std::string frame_info(DumpFrames(unwinder));
1182   ASSERT_EQ(6U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1183   EXPECT_EQ(
1184       "  #00 pc 00000000  jit_map0.so "
1185       "(com.example.simpleperf.simpleperfexamplewithnative.MixActivity.access$000)\n"
1186       "  #01 pc 0000003d  jit_map1.so "
1187       "(com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run+60)\n"
1188       "  #02 pc 004135bb  libart.so (art_quick_osr_stub+42)\n"
1189 
1190       "  #03 pc 003851dd  libart.so (art::Thread::CreateCallback(void*)+868)\n"
1191       "  #04 pc 00062925  libc.so (__pthread_start(void*)+22)\n"
1192       "  #05 pc 0001de39  libc.so (__start_thread+24)\n",
1193       frame_info);
1194 
1195   EXPECT_EQ(0xd025c788U, unwinder.frames()[0].pc);
1196   EXPECT_EQ(0xcd4ff140U, unwinder.frames()[0].sp);
1197   EXPECT_EQ(0xd025cdd5U, unwinder.frames()[1].pc);
1198   EXPECT_EQ(0xcd4ff140U, unwinder.frames()[1].sp);
1199   EXPECT_EQ(0xe4a755bbU, unwinder.frames()[2].pc);
1200   EXPECT_EQ(0xcd4ff160U, unwinder.frames()[2].sp);
1201   EXPECT_EQ(0xe49e71ddU, unwinder.frames()[3].pc);
1202   EXPECT_EQ(0xcd4ff8e8U, unwinder.frames()[3].sp);
1203   EXPECT_EQ(0xe7df3925U, unwinder.frames()[4].pc);
1204   EXPECT_EQ(0xcd4ff958U, unwinder.frames()[4].sp);
1205   EXPECT_EQ(0xe7daee39U, unwinder.frames()[5].pc);
1206   EXPECT_EQ(0xcd4ff960U, unwinder.frames()[5].sp);
1207 }
1208 
TEST_F(UnwindOfflineTest,offset_arm)1209 TEST_F(UnwindOfflineTest, offset_arm) {
1210   ASSERT_NO_FATAL_FAILURE(Init("offset_arm/", ARCH_ARM));
1211 
1212   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1213   unwinder.Unwind();
1214 
1215   std::string frame_info(DumpFrames(unwinder));
1216   ASSERT_EQ(19U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1217   EXPECT_EQ(
1218       "  #00 pc 0032bfa0  libunwindstack_test (SignalInnerFunction+40)\n"
1219       "  #01 pc 0032bfeb  libunwindstack_test (SignalMiddleFunction+2)\n"
1220       "  #02 pc 0032bff3  libunwindstack_test (SignalOuterFunction+2)\n"
1221       "  #03 pc 0032fed3  libunwindstack_test "
1222       "(unwindstack::SignalCallerHandler(int, siginfo*, void*)+26)\n"
1223       "  #04 pc 0002652c  libc.so (__restore)\n"
1224       "  #05 pc 00000000  <unknown>\n"
1225       "  #06 pc 0032c2d9  libunwindstack_test (InnerFunction+736)\n"
1226       "  #07 pc 0032cc4f  libunwindstack_test (MiddleFunction+42)\n"
1227       "  #08 pc 0032cc81  libunwindstack_test (OuterFunction+42)\n"
1228       "  #09 pc 0032e547  libunwindstack_test "
1229       "(unwindstack::RemoteThroughSignal(int, unsigned int)+270)\n"
1230       "  #10 pc 0032ed99  libunwindstack_test "
1231       "(unwindstack::UnwindTest_remote_through_signal_with_invalid_func_Test::TestBody()+16)\n"
1232       "  #11 pc 00354453  libunwindstack_test (testing::Test::Run()+154)\n"
1233       "  #12 pc 00354de7  libunwindstack_test (testing::TestInfo::Run()+194)\n"
1234       "  #13 pc 00355105  libunwindstack_test (testing::TestCase::Run()+180)\n"
1235       "  #14 pc 0035a215  libunwindstack_test "
1236       "(testing::internal::UnitTestImpl::RunAllTests()+664)\n"
1237       "  #15 pc 00359f4f  libunwindstack_test (testing::UnitTest::Run()+110)\n"
1238       "  #16 pc 0034d3db  libunwindstack_test (main+38)\n"
1239       "  #17 pc 00092c0d  libc.so (__libc_init+48)\n"
1240       "  #18 pc 0004202f  libunwindstack_test (_start_main+38)\n",
1241       frame_info);
1242 
1243   EXPECT_EQ(0x2e55fa0U, unwinder.frames()[0].pc);
1244   EXPECT_EQ(0xf43d2cccU, unwinder.frames()[0].sp);
1245   EXPECT_EQ(0x2e55febU, unwinder.frames()[1].pc);
1246   EXPECT_EQ(0xf43d2ce0U, unwinder.frames()[1].sp);
1247   EXPECT_EQ(0x2e55ff3U, unwinder.frames()[2].pc);
1248   EXPECT_EQ(0xf43d2ce8U, unwinder.frames()[2].sp);
1249   EXPECT_EQ(0x2e59ed3U, unwinder.frames()[3].pc);
1250   EXPECT_EQ(0xf43d2cf0U, unwinder.frames()[3].sp);
1251   EXPECT_EQ(0xf413652cU, unwinder.frames()[4].pc);
1252   EXPECT_EQ(0xf43d2d10U, unwinder.frames()[4].sp);
1253   EXPECT_EQ(0U, unwinder.frames()[5].pc);
1254   EXPECT_EQ(0xffcc0ee0U, unwinder.frames()[5].sp);
1255   EXPECT_EQ(0x2e562d9U, unwinder.frames()[6].pc);
1256   EXPECT_EQ(0xffcc0ee0U, unwinder.frames()[6].sp);
1257   EXPECT_EQ(0x2e56c4fU, unwinder.frames()[7].pc);
1258   EXPECT_EQ(0xffcc1060U, unwinder.frames()[7].sp);
1259   EXPECT_EQ(0x2e56c81U, unwinder.frames()[8].pc);
1260   EXPECT_EQ(0xffcc1078U, unwinder.frames()[8].sp);
1261   EXPECT_EQ(0x2e58547U, unwinder.frames()[9].pc);
1262   EXPECT_EQ(0xffcc1090U, unwinder.frames()[9].sp);
1263   EXPECT_EQ(0x2e58d99U, unwinder.frames()[10].pc);
1264   EXPECT_EQ(0xffcc1438U, unwinder.frames()[10].sp);
1265   EXPECT_EQ(0x2e7e453U, unwinder.frames()[11].pc);
1266   EXPECT_EQ(0xffcc1448U, unwinder.frames()[11].sp);
1267   EXPECT_EQ(0x2e7ede7U, unwinder.frames()[12].pc);
1268   EXPECT_EQ(0xffcc1458U, unwinder.frames()[12].sp);
1269   EXPECT_EQ(0x2e7f105U, unwinder.frames()[13].pc);
1270   EXPECT_EQ(0xffcc1490U, unwinder.frames()[13].sp);
1271   EXPECT_EQ(0x2e84215U, unwinder.frames()[14].pc);
1272   EXPECT_EQ(0xffcc14c0U, unwinder.frames()[14].sp);
1273   EXPECT_EQ(0x2e83f4fU, unwinder.frames()[15].pc);
1274   EXPECT_EQ(0xffcc1510U, unwinder.frames()[15].sp);
1275   EXPECT_EQ(0x2e773dbU, unwinder.frames()[16].pc);
1276   EXPECT_EQ(0xffcc1528U, unwinder.frames()[16].sp);
1277   EXPECT_EQ(0xf41a2c0dU, unwinder.frames()[17].pc);
1278   EXPECT_EQ(0xffcc1540U, unwinder.frames()[17].sp);
1279   EXPECT_EQ(0x2b6c02fU, unwinder.frames()[18].pc);
1280   EXPECT_EQ(0xffcc1558U, unwinder.frames()[18].sp);
1281 }
1282 
1283 // Test using a non-zero load bias library that has the fde entries
1284 // encoded as 0xb, which is not set as pc relative.
TEST_F(UnwindOfflineTest,debug_frame_load_bias_arm)1285 TEST_F(UnwindOfflineTest, debug_frame_load_bias_arm) {
1286   ASSERT_NO_FATAL_FAILURE(Init("debug_frame_load_bias_arm/", ARCH_ARM));
1287 
1288   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1289   unwinder.Unwind();
1290 
1291   std::string frame_info(DumpFrames(unwinder));
1292   ASSERT_EQ(8U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1293   EXPECT_EQ(
1294       "  #00 pc 0005138c  libc.so (__ioctl+8)\n"
1295       "  #01 pc 0002140f  libc.so (ioctl+30)\n"
1296       "  #02 pc 00039535  libbinder.so (android::IPCThreadState::talkWithDriver(bool)+204)\n"
1297       "  #03 pc 00039633  libbinder.so (android::IPCThreadState::getAndExecuteCommand()+10)\n"
1298       "  #04 pc 00039b57  libbinder.so (android::IPCThreadState::joinThreadPool(bool)+38)\n"
1299       "  #05 pc 00000c21  mediaserver (main+104)\n"
1300       "  #06 pc 00084b89  libc.so (__libc_init+48)\n"
1301       "  #07 pc 00000b77  mediaserver (_start_main+38)\n",
1302       frame_info);
1303 
1304   EXPECT_EQ(0xf0be238cU, unwinder.frames()[0].pc);
1305   EXPECT_EQ(0xffd4a638U, unwinder.frames()[0].sp);
1306   EXPECT_EQ(0xf0bb240fU, unwinder.frames()[1].pc);
1307   EXPECT_EQ(0xffd4a638U, unwinder.frames()[1].sp);
1308   EXPECT_EQ(0xf1a75535U, unwinder.frames()[2].pc);
1309   EXPECT_EQ(0xffd4a650U, unwinder.frames()[2].sp);
1310   EXPECT_EQ(0xf1a75633U, unwinder.frames()[3].pc);
1311   EXPECT_EQ(0xffd4a6b0U, unwinder.frames()[3].sp);
1312   EXPECT_EQ(0xf1a75b57U, unwinder.frames()[4].pc);
1313   EXPECT_EQ(0xffd4a6d0U, unwinder.frames()[4].sp);
1314   EXPECT_EQ(0x8d1cc21U, unwinder.frames()[5].pc);
1315   EXPECT_EQ(0xffd4a6e8U, unwinder.frames()[5].sp);
1316   EXPECT_EQ(0xf0c15b89U, unwinder.frames()[6].pc);
1317   EXPECT_EQ(0xffd4a700U, unwinder.frames()[6].sp);
1318   EXPECT_EQ(0x8d1cb77U, unwinder.frames()[7].pc);
1319   EXPECT_EQ(0xffd4a718U, unwinder.frames()[7].sp);
1320 }
1321 
TEST_F(UnwindOfflineTest,shared_lib_in_apk_arm64)1322 TEST_F(UnwindOfflineTest, shared_lib_in_apk_arm64) {
1323   ASSERT_NO_FATAL_FAILURE(Init("shared_lib_in_apk_arm64/", ARCH_ARM64));
1324 
1325   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1326   unwinder.Unwind();
1327 
1328   std::string frame_info(DumpFrames(unwinder));
1329   ASSERT_EQ(7U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1330   EXPECT_EQ(
1331       "  #00 pc 000000000014ccbc  linker64 (__dl_syscall+28)\n"
1332       "  #01 pc 000000000005426c  linker64 "
1333       "(__dl__ZL24debuggerd_signal_handleriP7siginfoPv+1128)\n"
1334       "  #02 pc 00000000000008c0  vdso.so (__kernel_rt_sigreturn)\n"
1335       "  #03 pc 00000000000846f4  libc.so (abort+172)\n"
1336       "  #04 pc 0000000000084ad4  libc.so (__assert2+36)\n"
1337       "  #05 pc 000000000003d5b4  ANGLEPrebuilt.apk!libfeature_support_angle.so (offset 0x4000) "
1338       "(ANGLEGetUtilityAPI+56)\n"
1339       "  #06 pc 000000000007fe68  libc.so (__libc_init)\n",
1340       frame_info);
1341 
1342   EXPECT_EQ(0x7e82c4fcbcULL, unwinder.frames()[0].pc);
1343   EXPECT_EQ(0x7df8ca3bf0ULL, unwinder.frames()[0].sp);
1344   EXPECT_EQ(0x7e82b5726cULL, unwinder.frames()[1].pc);
1345   EXPECT_EQ(0x7df8ca3bf0ULL, unwinder.frames()[1].sp);
1346   EXPECT_EQ(0x7e82b018c0ULL, unwinder.frames()[2].pc);
1347   EXPECT_EQ(0x7df8ca3da0ULL, unwinder.frames()[2].sp);
1348   EXPECT_EQ(0x7e7eecc6f4ULL, unwinder.frames()[3].pc);
1349   EXPECT_EQ(0x7dabf3db60ULL, unwinder.frames()[3].sp);
1350   EXPECT_EQ(0x7e7eeccad4ULL, unwinder.frames()[4].pc);
1351   EXPECT_EQ(0x7dabf3dc40ULL, unwinder.frames()[4].sp);
1352   EXPECT_EQ(0x7dabc405b4ULL, unwinder.frames()[5].pc);
1353   EXPECT_EQ(0x7dabf3dc50ULL, unwinder.frames()[5].sp);
1354   EXPECT_EQ(0x7e7eec7e68ULL, unwinder.frames()[6].pc);
1355   EXPECT_EQ(0x7dabf3dc70ULL, unwinder.frames()[6].sp);
1356   // Ignore top frame since the test code was modified to end in __libc_init.
1357 }
1358 
TEST_F(UnwindOfflineTest,shared_lib_in_apk_memory_only_arm64)1359 TEST_F(UnwindOfflineTest, shared_lib_in_apk_memory_only_arm64) {
1360   ASSERT_NO_FATAL_FAILURE(Init("shared_lib_in_apk_memory_only_arm64/", ARCH_ARM64));
1361   // Add the memory that represents the shared library.
1362   MemoryOfflineParts* memory = reinterpret_cast<MemoryOfflineParts*>(process_memory_.get());
1363   AddMemory(dir_ + "lib_mem.data", memory);
1364 
1365   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1366   unwinder.Unwind();
1367 
1368   std::string frame_info(DumpFrames(unwinder));
1369   ASSERT_EQ(7U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1370   EXPECT_EQ(
1371       "  #00 pc 000000000014ccbc  linker64 (__dl_syscall+28)\n"
1372       "  #01 pc 000000000005426c  linker64 "
1373       "(__dl__ZL24debuggerd_signal_handleriP7siginfoPv+1128)\n"
1374       "  #02 pc 00000000000008c0  vdso.so (__kernel_rt_sigreturn)\n"
1375       "  #03 pc 00000000000846f4  libc.so (abort+172)\n"
1376       "  #04 pc 0000000000084ad4  libc.so (__assert2+36)\n"
1377       "  #05 pc 000000000003d5b4  ANGLEPrebuilt.apk (offset 0x21d5000)\n"
1378       "  #06 pc 000000000007fe68  libc.so (__libc_init)\n",
1379       frame_info);
1380 
1381   EXPECT_EQ(0x7e82c4fcbcULL, unwinder.frames()[0].pc);
1382   EXPECT_EQ(0x7df8ca3bf0ULL, unwinder.frames()[0].sp);
1383   EXPECT_EQ(0x7e82b5726cULL, unwinder.frames()[1].pc);
1384   EXPECT_EQ(0x7df8ca3bf0ULL, unwinder.frames()[1].sp);
1385   EXPECT_EQ(0x7e82b018c0ULL, unwinder.frames()[2].pc);
1386   EXPECT_EQ(0x7df8ca3da0ULL, unwinder.frames()[2].sp);
1387   EXPECT_EQ(0x7e7eecc6f4ULL, unwinder.frames()[3].pc);
1388   EXPECT_EQ(0x7dabf3db60ULL, unwinder.frames()[3].sp);
1389   EXPECT_EQ(0x7e7eeccad4ULL, unwinder.frames()[4].pc);
1390   EXPECT_EQ(0x7dabf3dc40ULL, unwinder.frames()[4].sp);
1391   EXPECT_EQ(0x7dabc405b4ULL, unwinder.frames()[5].pc);
1392   EXPECT_EQ(0x7dabf3dc50ULL, unwinder.frames()[5].sp);
1393   EXPECT_EQ(0x7e7eec7e68ULL, unwinder.frames()[6].pc);
1394   EXPECT_EQ(0x7dabf3dc70ULL, unwinder.frames()[6].sp);
1395   // Ignore top frame since the test code was modified to end in __libc_init.
1396 }
1397 
TEST_F(UnwindOfflineTest,shared_lib_in_apk_single_map_arm64)1398 TEST_F(UnwindOfflineTest, shared_lib_in_apk_single_map_arm64) {
1399   ASSERT_NO_FATAL_FAILURE(Init("shared_lib_in_apk_single_map_arm64/", ARCH_ARM64));
1400 
1401   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1402   unwinder.Unwind();
1403 
1404   std::string frame_info(DumpFrames(unwinder));
1405   ASSERT_EQ(13U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1406   EXPECT_EQ(
1407       "  #00 pc 00000000000814bc  libc.so (syscall+28)\n"
1408       "  #01 pc 00000000008cdf5c  test.apk (offset 0x5000)\n"
1409       "  #02 pc 00000000008cde9c  test.apk (offset 0x5000)\n"
1410       "  #03 pc 00000000008cdd70  test.apk (offset 0x5000)\n"
1411       "  #04 pc 00000000008ce408  test.apk (offset 0x5000)\n"
1412       "  #05 pc 00000000008ce8d8  test.apk (offset 0x5000)\n"
1413       "  #06 pc 00000000008ce814  test.apk (offset 0x5000)\n"
1414       "  #07 pc 00000000008bcf60  test.apk (offset 0x5000)\n"
1415       "  #08 pc 0000000000133024  test.apk (offset 0x5000)\n"
1416       "  #09 pc 0000000000134ad0  test.apk (offset 0x5000)\n"
1417       "  #10 pc 0000000000134b64  test.apk (offset 0x5000)\n"
1418       "  #11 pc 00000000000e406c  libc.so (__pthread_start(void*)+36)\n"
1419       "  #12 pc 0000000000085e18  libc.so (__start_thread+64)\n",
1420       frame_info);
1421 
1422   EXPECT_EQ(0x7cbe0b14bcULL, unwinder.frames()[0].pc);
1423   EXPECT_EQ(0x7be4f077d0ULL, unwinder.frames()[0].sp);
1424   EXPECT_EQ(0x7be6715f5cULL, unwinder.frames()[1].pc);
1425   EXPECT_EQ(0x7be4f077d0ULL, unwinder.frames()[1].sp);
1426   EXPECT_EQ(0x7be6715e9cULL, unwinder.frames()[2].pc);
1427   EXPECT_EQ(0x7be4f07800ULL, unwinder.frames()[2].sp);
1428   EXPECT_EQ(0x7be6715d70ULL, unwinder.frames()[3].pc);
1429   EXPECT_EQ(0x7be4f07840ULL, unwinder.frames()[3].sp);
1430   EXPECT_EQ(0x7be6716408ULL, unwinder.frames()[4].pc);
1431   EXPECT_EQ(0x7be4f07860ULL, unwinder.frames()[4].sp);
1432   EXPECT_EQ(0x7be67168d8ULL, unwinder.frames()[5].pc);
1433   EXPECT_EQ(0x7be4f07880ULL, unwinder.frames()[5].sp);
1434   EXPECT_EQ(0x7be6716814ULL, unwinder.frames()[6].pc);
1435   EXPECT_EQ(0x7be4f078f0ULL, unwinder.frames()[6].sp);
1436   EXPECT_EQ(0x7be6704f60ULL, unwinder.frames()[7].pc);
1437   EXPECT_EQ(0x7be4f07910ULL, unwinder.frames()[7].sp);
1438   EXPECT_EQ(0x7be5f7b024ULL, unwinder.frames()[8].pc);
1439   EXPECT_EQ(0x7be4f07950ULL, unwinder.frames()[8].sp);
1440   EXPECT_EQ(0x7be5f7cad0ULL, unwinder.frames()[9].pc);
1441   EXPECT_EQ(0x7be4f07aa0ULL, unwinder.frames()[9].sp);
1442   EXPECT_EQ(0x7be5f7cb64ULL, unwinder.frames()[10].pc);
1443   EXPECT_EQ(0x7be4f07ce0ULL, unwinder.frames()[10].sp);
1444   EXPECT_EQ(0x7cbe11406cULL, unwinder.frames()[11].pc);
1445   EXPECT_EQ(0x7be4f07d00ULL, unwinder.frames()[11].sp);
1446   EXPECT_EQ(0x7cbe0b5e18ULL, unwinder.frames()[12].pc);
1447   EXPECT_EQ(0x7be4f07d20ULL, unwinder.frames()[12].sp);
1448 }
1449 
TEST_F(UnwindOfflineTest,invalid_elf_offset_arm)1450 TEST_F(UnwindOfflineTest, invalid_elf_offset_arm) {
1451   ASSERT_NO_FATAL_FAILURE(Init("invalid_elf_offset_arm/", ARCH_ARM, false));
1452 
1453   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1454   unwinder.Unwind();
1455 
1456   std::string frame_info(DumpFrames(unwinder));
1457   ASSERT_EQ(1U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1458   EXPECT_EQ("  #00 pc 00aa7508  invalid.apk (offset 0x12e4000)\n", frame_info);
1459   EXPECT_EQ(0xc898f508, unwinder.frames()[0].pc);
1460   EXPECT_EQ(0xc2044218, unwinder.frames()[0].sp);
1461 }
1462 
TEST_F(UnwindOfflineTest,load_bias_ro_rx_x86_64)1463 TEST_F(UnwindOfflineTest, load_bias_ro_rx_x86_64) {
1464   ASSERT_NO_FATAL_FAILURE(Init("load_bias_ro_rx_x86_64/", ARCH_X86_64));
1465 
1466   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1467   unwinder.Unwind();
1468 
1469   std::string frame_info(DumpFrames(unwinder));
1470   ASSERT_EQ(17U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1471   EXPECT_EQ(
1472       "  #00 pc 00000000000e9dd4  libc.so (__write+20)\n"
1473       "  #01 pc 000000000007ab9c  libc.so (_IO_file_write+44)\n"
1474       "  #02 pc 0000000000079f3e  libc.so\n"
1475       "  #03 pc 000000000007bce8  libc.so (_IO_do_write+24)\n"
1476       "  #04 pc 000000000007b26e  libc.so (_IO_file_xsputn+270)\n"
1477       "  #05 pc 000000000004f7f9  libc.so (_IO_vfprintf+1945)\n"
1478       "  #06 pc 0000000000057cb5  libc.so (_IO_printf+165)\n"
1479       "  #07 pc 0000000000ed1796  perfetto_unittests "
1480       "(testing::internal::PrettyUnitTestResultPrinter::OnTestIterationStart(testing::UnitTest "
1481       "const&, int)+374)\n"
1482       "  #08 pc 0000000000ed30fd  perfetto_unittests "
1483       "(testing::internal::TestEventRepeater::OnTestIterationStart(testing::UnitTest const&, "
1484       "int)+125)\n"
1485       "  #09 pc 0000000000ed5e25  perfetto_unittests "
1486       "(testing::internal::UnitTestImpl::RunAllTests()+581)\n"
1487       "  #10 pc 0000000000ef63f3  perfetto_unittests "
1488       "(bool "
1489       "testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, "
1490       "bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char "
1491       "const*)+131)\n"
1492       "  #11 pc 0000000000ee2a21  perfetto_unittests "
1493       "(bool "
1494       "testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, "
1495       "bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char "
1496       "const*)+113)\n"
1497       "  #12 pc 0000000000ed5bb9  perfetto_unittests (testing::UnitTest::Run()+185)\n"
1498       "  #13 pc 0000000000e900f0  perfetto_unittests (RUN_ALL_TESTS()+16)\n"
1499       "  #14 pc 0000000000e900d8  perfetto_unittests (main+56)\n"
1500       "  #15 pc 000000000002352a  libc.so (__libc_start_main+234)\n"
1501       "  #16 pc 0000000000919029  perfetto_unittests (_start+41)\n",
1502       frame_info);
1503 
1504   EXPECT_EQ(0x7f9326a57dd4ULL, unwinder.frames()[0].pc);
1505   EXPECT_EQ(0x7ffd224153c8ULL, unwinder.frames()[0].sp);
1506   EXPECT_EQ(0x7f93269e8b9cULL, unwinder.frames()[1].pc);
1507   EXPECT_EQ(0x7ffd224153d0ULL, unwinder.frames()[1].sp);
1508   EXPECT_EQ(0x7f93269e7f3eULL, unwinder.frames()[2].pc);
1509   EXPECT_EQ(0x7ffd22415400ULL, unwinder.frames()[2].sp);
1510   EXPECT_EQ(0x7f93269e9ce8ULL, unwinder.frames()[3].pc);
1511   EXPECT_EQ(0x7ffd22415440ULL, unwinder.frames()[3].sp);
1512   EXPECT_EQ(0x7f93269e926eULL, unwinder.frames()[4].pc);
1513   EXPECT_EQ(0x7ffd22415450ULL, unwinder.frames()[4].sp);
1514   EXPECT_EQ(0x7f93269bd7f9ULL, unwinder.frames()[5].pc);
1515   EXPECT_EQ(0x7ffd22415490ULL, unwinder.frames()[5].sp);
1516   EXPECT_EQ(0x7f93269c5cb5ULL, unwinder.frames()[6].pc);
1517   EXPECT_EQ(0x7ffd22415a10ULL, unwinder.frames()[6].sp);
1518   EXPECT_EQ(0xed1796ULL, unwinder.frames()[7].pc);
1519   EXPECT_EQ(0x7ffd22415af0ULL, unwinder.frames()[7].sp);
1520   EXPECT_EQ(0xed30fdULL, unwinder.frames()[8].pc);
1521   EXPECT_EQ(0x7ffd22415b70ULL, unwinder.frames()[8].sp);
1522   EXPECT_EQ(0xed5e25ULL, unwinder.frames()[9].pc);
1523   EXPECT_EQ(0x7ffd22415bb0ULL, unwinder.frames()[9].sp);
1524   EXPECT_EQ(0xef63f3ULL, unwinder.frames()[10].pc);
1525   EXPECT_EQ(0x7ffd22415c60ULL, unwinder.frames()[10].sp);
1526   EXPECT_EQ(0xee2a21ULL, unwinder.frames()[11].pc);
1527   EXPECT_EQ(0x7ffd22415cc0ULL, unwinder.frames()[11].sp);
1528   EXPECT_EQ(0xed5bb9ULL, unwinder.frames()[12].pc);
1529   EXPECT_EQ(0x7ffd22415d40ULL, unwinder.frames()[12].sp);
1530   EXPECT_EQ(0xe900f0ULL, unwinder.frames()[13].pc);
1531   EXPECT_EQ(0x7ffd22415d90ULL, unwinder.frames()[13].sp);
1532   EXPECT_EQ(0xe900d8ULL, unwinder.frames()[14].pc);
1533   EXPECT_EQ(0x7ffd22415da0ULL, unwinder.frames()[14].sp);
1534   EXPECT_EQ(0x7f932699152aULL, unwinder.frames()[15].pc);
1535   EXPECT_EQ(0x7ffd22415dd0ULL, unwinder.frames()[15].sp);
1536   EXPECT_EQ(0x919029ULL, unwinder.frames()[16].pc);
1537   EXPECT_EQ(0x7ffd22415e90ULL, unwinder.frames()[16].sp);
1538 }
1539 
TEST_F(UnwindOfflineTest,load_bias_different_section_bias_arm64)1540 TEST_F(UnwindOfflineTest, load_bias_different_section_bias_arm64) {
1541   ASSERT_NO_FATAL_FAILURE(Init("load_bias_different_section_bias_arm64/", ARCH_ARM64));
1542 
1543   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1544   unwinder.Unwind();
1545 
1546   std::string frame_info(DumpFrames(unwinder));
1547   ASSERT_EQ(12U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1548   EXPECT_EQ(
1549       "  #00 pc 00000000000d59bc  linker64 (__dl_syscall+28)\n"
1550       "  #01 pc 00000000000554e8  linker64 (__dl__ZL24debuggerd_signal_handleriP7siginfoPv+1148)\n"
1551       "  #02 pc 00000000000008c0  vdso (__kernel_rt_sigreturn)\n"
1552       "  #03 pc 000000000007f3e8  libc.so (abort+168)\n"
1553       "  #04 pc 00000000000459fc  test (std::__ndk1::__throw_bad_cast()+4)\n"
1554       "  #05 pc 0000000000056d80  test (testing::Test::Run()+88)\n"
1555       "  #06 pc 000000000005724c  test (testing::TestInfo::Run()+112)\n"
1556       "  #07 pc 0000000000057558  test (testing::TestSuite::Run()+116)\n"
1557       "  #08 pc 000000000005bffc  test (testing::internal::UnitTestImpl::RunAllTests()+464)\n"
1558       "  #09 pc 000000000005bd9c  test (testing::UnitTest::Run()+116)\n"
1559       "  #10 pc 00000000000464e4  test (main+144)\n"
1560       "  #11 pc 000000000007aa34  libc.so (__libc_init+108)\n",
1561       frame_info);
1562 
1563   EXPECT_EQ(0x7112cb99bcULL, unwinder.frames()[0].pc);
1564   EXPECT_EQ(0x7112bdbbf0ULL, unwinder.frames()[0].sp);
1565   EXPECT_EQ(0x7112c394e8ULL, unwinder.frames()[1].pc);
1566   EXPECT_EQ(0x7112bdbbf0ULL, unwinder.frames()[1].sp);
1567   EXPECT_EQ(0x7112be28c0ULL, unwinder.frames()[2].pc);
1568   EXPECT_EQ(0x7112bdbda0ULL, unwinder.frames()[2].sp);
1569   EXPECT_EQ(0x71115ab3e8ULL, unwinder.frames()[3].pc);
1570   EXPECT_EQ(0x7fdd4a3f00ULL, unwinder.frames()[3].sp);
1571   EXPECT_EQ(0x5f739dc9fcULL, unwinder.frames()[4].pc);
1572   EXPECT_EQ(0x7fdd4a3fe0ULL, unwinder.frames()[4].sp);
1573   EXPECT_EQ(0x5f739edd80ULL, unwinder.frames()[5].pc);
1574   EXPECT_EQ(0x7fdd4a3ff0ULL, unwinder.frames()[5].sp);
1575   EXPECT_EQ(0x5f739ee24cULL, unwinder.frames()[6].pc);
1576   EXPECT_EQ(0x7fdd4a4010ULL, unwinder.frames()[6].sp);
1577   EXPECT_EQ(0x5f739ee558ULL, unwinder.frames()[7].pc);
1578   EXPECT_EQ(0x7fdd4a4040ULL, unwinder.frames()[7].sp);
1579   EXPECT_EQ(0x5f739f2ffcULL, unwinder.frames()[8].pc);
1580   EXPECT_EQ(0x7fdd4a4070ULL, unwinder.frames()[8].sp);
1581   EXPECT_EQ(0x5f739f2d9cULL, unwinder.frames()[9].pc);
1582   EXPECT_EQ(0x7fdd4a4100ULL, unwinder.frames()[9].sp);
1583   EXPECT_EQ(0x5f739dd4e4ULL, unwinder.frames()[10].pc);
1584   EXPECT_EQ(0x7fdd4a4130ULL, unwinder.frames()[10].sp);
1585   EXPECT_EQ(0x71115a6a34ULL, unwinder.frames()[11].pc);
1586   EXPECT_EQ(0x7fdd4a4170ULL, unwinder.frames()[11].sp);
1587 }
1588 
TEST_F(UnwindOfflineTest,eh_frame_bias_x86)1589 TEST_F(UnwindOfflineTest, eh_frame_bias_x86) {
1590   ASSERT_NO_FATAL_FAILURE(Init("eh_frame_bias_x86/", ARCH_X86));
1591 
1592   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1593   unwinder.Unwind();
1594 
1595   std::string frame_info(DumpFrames(unwinder));
1596   ASSERT_EQ(11U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1597   EXPECT_EQ(
1598       "  #00 pc ffffe430  vdso.so (__kernel_vsyscall+16)\n"
1599       "  #01 pc 00082a4b  libc.so (__epoll_pwait+43)\n"
1600       "  #02 pc 000303a3  libc.so (epoll_pwait+115)\n"
1601       "  #03 pc 000303ed  libc.so (epoll_wait+45)\n"
1602       "  #04 pc 00010ea2  tombstoned (epoll_dispatch+226)\n"
1603       "  #05 pc 0000c5e7  tombstoned (event_base_loop+1095)\n"
1604       "  #06 pc 0000c193  tombstoned (event_base_dispatch+35)\n"
1605       "  #07 pc 00005c77  tombstoned (main+884)\n"
1606       "  #08 pc 00015f66  libc.so (__libc_init+102)\n"
1607       "  #09 pc 0000360e  tombstoned (_start+98)\n"
1608       "  #10 pc 00000001  <unknown>\n",
1609       frame_info);
1610 
1611   EXPECT_EQ(0xffffe430ULL, unwinder.frames()[0].pc);
1612   EXPECT_EQ(0xfffe1a30ULL, unwinder.frames()[0].sp);
1613   EXPECT_EQ(0xeb585a4bULL, unwinder.frames()[1].pc);
1614   EXPECT_EQ(0xfffe1a40ULL, unwinder.frames()[1].sp);
1615   EXPECT_EQ(0xeb5333a3ULL, unwinder.frames()[2].pc);
1616   EXPECT_EQ(0xfffe1a60ULL, unwinder.frames()[2].sp);
1617   EXPECT_EQ(0xeb5333edULL, unwinder.frames()[3].pc);
1618   EXPECT_EQ(0xfffe1ab0ULL, unwinder.frames()[3].sp);
1619   EXPECT_EQ(0xeb841ea2ULL, unwinder.frames()[4].pc);
1620   EXPECT_EQ(0xfffe1ae0ULL, unwinder.frames()[4].sp);
1621   EXPECT_EQ(0xeb83d5e7ULL, unwinder.frames()[5].pc);
1622   EXPECT_EQ(0xfffe1b30ULL, unwinder.frames()[5].sp);
1623   EXPECT_EQ(0xeb83d193ULL, unwinder.frames()[6].pc);
1624   EXPECT_EQ(0xfffe1bd0ULL, unwinder.frames()[6].sp);
1625   EXPECT_EQ(0xeb836c77ULL, unwinder.frames()[7].pc);
1626   EXPECT_EQ(0xfffe1c00ULL, unwinder.frames()[7].sp);
1627   EXPECT_EQ(0xeb518f66ULL, unwinder.frames()[8].pc);
1628   EXPECT_EQ(0xfffe1d00ULL, unwinder.frames()[8].sp);
1629   EXPECT_EQ(0xeb83460eULL, unwinder.frames()[9].pc);
1630   EXPECT_EQ(0xfffe1d40ULL, unwinder.frames()[9].sp);
1631   EXPECT_EQ(0x00000001ULL, unwinder.frames()[10].pc);
1632   EXPECT_EQ(0xfffe1d74ULL, unwinder.frames()[10].sp);
1633 }
1634 
TEST_F(UnwindOfflineTest,signal_load_bias_arm)1635 TEST_F(UnwindOfflineTest, signal_load_bias_arm) {
1636   ASSERT_NO_FATAL_FAILURE(Init("signal_load_bias_arm/", ARCH_ARM));
1637 
1638   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1639   unwinder.Unwind();
1640 
1641   std::string frame_info(DumpFrames(unwinder));
1642   ASSERT_EQ(17U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1643   EXPECT_EQ(
1644       "  #00 pc 0029ef9e  libunwindstack_unit_test (SignalInnerFunction+10)\n"
1645       "  #01 pc 0029efa7  libunwindstack_unit_test (SignalMiddleFunction+2)\n"
1646       "  #02 pc 0029efaf  libunwindstack_unit_test (SignalOuterFunction+2)\n"
1647       "  #03 pc 002a280b  libunwindstack_unit_test (unwindstack::SignalCallerHandler(int, "
1648       "siginfo*, void*)+10)\n"
1649       "  #04 pc 00058bd4  libc.so (__restore)\n"
1650       "  #05 pc 0029f01e  libunwindstack_unit_test (InnerFunction+106)\n"
1651       "  #06 pc 0029f633  libunwindstack_unit_test (MiddleFunction+16)\n"
1652       "  #07 pc 0029f64b  libunwindstack_unit_test (OuterFunction+16)\n"
1653       "  #08 pc 002a1711  libunwindstack_unit_test (unwindstack::RemoteThroughSignal(int, unsigned "
1654       "int)+260)\n"
1655       "  #09 pc 002a1603  libunwindstack_unit_test "
1656       "(unwindstack::UnwindTest_remote_through_signal_Test::TestBody()+10)\n"
1657       "  #10 pc 002c8fe3  libunwindstack_unit_test (testing::Test::Run()+130)\n"
1658       "  #11 pc 002c9b25  libunwindstack_unit_test (testing::TestInfo::Run()+184)\n"
1659       "  #12 pc 002c9e27  libunwindstack_unit_test (testing::TestSuite::Run()+202)\n"
1660       "  #13 pc 002d193d  libunwindstack_unit_test "
1661       "(testing::internal::UnitTestImpl::RunAllTests()+660)\n"
1662       "  #14 pc 002d160b  libunwindstack_unit_test (testing::UnitTest::Run()+134)\n"
1663       "  #15 pc 002de035  libunwindstack_unit_test (IsolateMain+680)\n"
1664       "  #16 pc 00058155  libc.so (__libc_init+68)\n",
1665       frame_info);
1666 
1667   EXPECT_EQ(0xb6955f9eULL, unwinder.frames()[0].pc);
1668   EXPECT_EQ(0xf2790ce8ULL, unwinder.frames()[0].sp);
1669   EXPECT_EQ(0xb6955fa7ULL, unwinder.frames()[1].pc);
1670   EXPECT_EQ(0xf2790ce8ULL, unwinder.frames()[1].sp);
1671   EXPECT_EQ(0xb6955fafULL, unwinder.frames()[2].pc);
1672   EXPECT_EQ(0xf2790cf0ULL, unwinder.frames()[2].sp);
1673   EXPECT_EQ(0xb695980bULL, unwinder.frames()[3].pc);
1674   EXPECT_EQ(0xf2790cf8ULL, unwinder.frames()[3].sp);
1675   EXPECT_EQ(0xf23febd4ULL, unwinder.frames()[4].pc);
1676   EXPECT_EQ(0xf2790d10ULL, unwinder.frames()[4].sp);
1677   EXPECT_EQ(0xb695601eULL, unwinder.frames()[5].pc);
1678   EXPECT_EQ(0xffe67798ULL, unwinder.frames()[5].sp);
1679   EXPECT_EQ(0xb6956633ULL, unwinder.frames()[6].pc);
1680   EXPECT_EQ(0xffe67890ULL, unwinder.frames()[6].sp);
1681   EXPECT_EQ(0xb695664bULL, unwinder.frames()[7].pc);
1682   EXPECT_EQ(0xffe678a0ULL, unwinder.frames()[7].sp);
1683   EXPECT_EQ(0xb6958711ULL, unwinder.frames()[8].pc);
1684   EXPECT_EQ(0xffe678b0ULL, unwinder.frames()[8].sp);
1685   EXPECT_EQ(0xb6958603ULL, unwinder.frames()[9].pc);
1686   EXPECT_EQ(0xffe67ac8ULL, unwinder.frames()[9].sp);
1687   EXPECT_EQ(0xb697ffe3ULL, unwinder.frames()[10].pc);
1688   EXPECT_EQ(0xffe67ad8ULL, unwinder.frames()[10].sp);
1689   EXPECT_EQ(0xb6980b25ULL, unwinder.frames()[11].pc);
1690   EXPECT_EQ(0xffe67ae8ULL, unwinder.frames()[11].sp);
1691   EXPECT_EQ(0xb6980e27ULL, unwinder.frames()[12].pc);
1692   EXPECT_EQ(0xffe67b18ULL, unwinder.frames()[12].sp);
1693   EXPECT_EQ(0xb698893dULL, unwinder.frames()[13].pc);
1694   EXPECT_EQ(0xffe67b48ULL, unwinder.frames()[13].sp);
1695   EXPECT_EQ(0xb698860bULL, unwinder.frames()[14].pc);
1696   EXPECT_EQ(0xffe67bb0ULL, unwinder.frames()[14].sp);
1697   EXPECT_EQ(0xb6995035ULL, unwinder.frames()[15].pc);
1698   EXPECT_EQ(0xffe67bd0ULL, unwinder.frames()[15].sp);
1699   EXPECT_EQ(0xf23fe155ULL, unwinder.frames()[16].pc);
1700   EXPECT_EQ(0xffe67d10ULL, unwinder.frames()[16].sp);
1701 }
1702 
TEST_F(UnwindOfflineTest,empty_arm64)1703 TEST_F(UnwindOfflineTest, empty_arm64) {
1704   ASSERT_NO_FATAL_FAILURE(Init("empty_arm64/", ARCH_ARM64));
1705 
1706   Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
1707   unwinder.Unwind();
1708 
1709   std::string frame_info(DumpFrames(unwinder));
1710   ASSERT_EQ(7U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
1711   EXPECT_EQ(
1712       "  #00 pc 00000000000963a4  libc.so (__ioctl+4)\n"
1713       "  #01 pc 000000000005344c  libc.so (ioctl+140)\n"
1714       "  #02 pc 0000000000050ce4  libbinder.so "
1715       "(android::IPCThreadState::talkWithDriver(bool)+308)\n"
1716       "  #03 pc 0000000000050e98  libbinder.so "
1717       "(android::IPCThreadState::getAndExecuteCommand()+24)\n"
1718       "  #04 pc 00000000000516ac  libbinder.so (android::IPCThreadState::joinThreadPool(bool)+60)\n"
1719       "  #05 pc 00000000000443b0  netd (main+1056)\n"
1720       "  #06 pc 0000000000045594  libc.so (__libc_init+108)\n",
1721       frame_info);
1722 
1723   EXPECT_EQ(0x72a02203a4U, unwinder.frames()[0].pc);
1724   EXPECT_EQ(0x7ffb6c0b50U, unwinder.frames()[0].sp);
1725   EXPECT_EQ(0x72a01dd44cU, unwinder.frames()[1].pc);
1726   EXPECT_EQ(0x7ffb6c0b50U, unwinder.frames()[1].sp);
1727   EXPECT_EQ(0x729f759ce4U, unwinder.frames()[2].pc);
1728   EXPECT_EQ(0x7ffb6c0c50U, unwinder.frames()[2].sp);
1729   EXPECT_EQ(0x729f759e98U, unwinder.frames()[3].pc);
1730   EXPECT_EQ(0x7ffb6c0ce0U, unwinder.frames()[3].sp);
1731   EXPECT_EQ(0x729f75a6acU, unwinder.frames()[4].pc);
1732   EXPECT_EQ(0x7ffb6c0d10U, unwinder.frames()[4].sp);
1733   EXPECT_EQ(0x5d478af3b0U, unwinder.frames()[5].pc);
1734   EXPECT_EQ(0x7ffb6c0d40U, unwinder.frames()[5].sp);
1735   EXPECT_EQ(0x72a01cf594U, unwinder.frames()[6].pc);
1736   EXPECT_EQ(0x7ffb6c0f30U, unwinder.frames()[6].sp);
1737 }
1738 
1739 }  // namespace unwindstack
1740