| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 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 <unistd.h> | 
|  | 22 |  | 
|  | 23 | #include <gtest/gtest.h> | 
|  | 24 |  | 
|  | 25 | #include <string> | 
|  | 26 | #include <vector> | 
|  | 27 |  | 
|  | 28 | #include <unwindstack/Maps.h> | 
|  | 29 | #include <unwindstack/Memory.h> | 
|  | 30 | #include <unwindstack/Regs.h> | 
|  | 31 | #include <unwindstack/Unwinder.h> | 
|  | 32 |  | 
|  | 33 | #include "Machine.h" | 
|  | 34 |  | 
|  | 35 | #include "ElfTestUtils.h" | 
|  | 36 |  | 
|  | 37 | namespace unwindstack { | 
|  | 38 |  | 
|  | 39 | static std::string DumpFrames(Unwinder& unwinder) { | 
|  | 40 | std::string str; | 
|  | 41 | for (size_t i = 0; i < unwinder.NumFrames(); i++) { | 
|  | 42 | str += unwinder.FormatFrame(i) + "\n"; | 
|  | 43 | } | 
|  | 44 | return str; | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | TEST(UnwindOfflineTest, pc_straddle_arm32) { | 
|  | 48 | std::string dir(TestGetFileDirectory() + "offline/straddle_arm32/"); | 
|  | 49 |  | 
|  | 50 | MemoryOffline* memory = new MemoryOffline; | 
|  | 51 | ASSERT_TRUE(memory->Init((dir + "stack.data").c_str(), 0)); | 
|  | 52 |  | 
|  | 53 | FILE* fp = fopen((dir + "regs.txt").c_str(), "r"); | 
|  | 54 | ASSERT_TRUE(fp != nullptr); | 
|  | 55 | RegsArm regs; | 
|  | 56 | uint64_t reg_value; | 
|  | 57 | ASSERT_EQ(1, fscanf(fp, "pc: %" SCNx64 "\n", ®_value)); | 
|  | 58 | regs[ARM_REG_PC] = reg_value; | 
|  | 59 | ASSERT_EQ(1, fscanf(fp, "sp: %" SCNx64 "\n", ®_value)); | 
|  | 60 | regs[ARM_REG_SP] = reg_value; | 
|  | 61 | ASSERT_EQ(1, fscanf(fp, "lr: %" SCNx64 "\n", ®_value)); | 
|  | 62 | regs[ARM_REG_LR] = reg_value; | 
|  | 63 | regs.SetFromRaw(); | 
|  | 64 | fclose(fp); | 
|  | 65 |  | 
|  | 66 | fp = fopen((dir + "maps.txt").c_str(), "r"); | 
|  | 67 | ASSERT_TRUE(fp != nullptr); | 
|  | 68 | // The file is guaranteed to be less than 4096 bytes. | 
|  | 69 | std::vector<char> buffer(4096); | 
|  | 70 | ASSERT_NE(0U, fread(buffer.data(), 1, buffer.size(), fp)); | 
|  | 71 | fclose(fp); | 
|  | 72 |  | 
|  | 73 | BufferMaps maps(buffer.data()); | 
|  | 74 | ASSERT_TRUE(maps.Parse()); | 
|  | 75 |  | 
|  | 76 | ASSERT_EQ(static_cast<uint32_t>(EM_ARM), regs.MachineType()); | 
|  | 77 |  | 
|  | 78 | std::shared_ptr<Memory> process_memory(memory); | 
|  | 79 |  | 
|  | 80 | char* cwd = getcwd(nullptr, 0); | 
|  | 81 | ASSERT_EQ(0, chdir(dir.c_str())); | 
|  | 82 | Unwinder unwinder(128, &maps, ®s, process_memory); | 
|  | 83 | unwinder.Unwind(); | 
|  | 84 | ASSERT_EQ(0, chdir(cwd)); | 
|  | 85 | free(cwd); | 
|  | 86 |  | 
|  | 87 | std::string frame_info(DumpFrames(unwinder)); | 
|  | 88 | ASSERT_EQ(4U, unwinder.NumFrames()) << "Unwind:\n" << frame_info; | 
|  | 89 | EXPECT_EQ( | 
|  | 90 | "  #00 pc 0001a9f8  libc.so (abort+63)\n" | 
|  | 91 | "  #01 pc 00006a1b  libbase.so (_ZN7android4base14DefaultAborterEPKc+6)\n" | 
|  | 92 | "  #02 pc 00007441  libbase.so (_ZN7android4base10LogMessageD2Ev+748)\n" | 
|  | 93 | "  #03 pc 00015149  /does/not/exist/libhidlbase.so\n", | 
|  | 94 | frame_info); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | TEST(UnwindOfflineTest, pc_straddle_arm64) { | 
|  | 98 | std::string dir(TestGetFileDirectory() + "offline/straddle_arm64/"); | 
|  | 99 |  | 
|  | 100 | MemoryOffline* memory = new MemoryOffline; | 
|  | 101 | ASSERT_TRUE(memory->Init((dir + "stack.data").c_str(), 0)); | 
|  | 102 |  | 
|  | 103 | FILE* fp = fopen((dir + "regs.txt").c_str(), "r"); | 
|  | 104 | ASSERT_TRUE(fp != nullptr); | 
|  | 105 | RegsArm64 regs; | 
|  | 106 | uint64_t reg_value; | 
|  | 107 | ASSERT_EQ(1, fscanf(fp, "pc: %" SCNx64 "\n", ®_value)); | 
|  | 108 | regs[ARM64_REG_PC] = reg_value; | 
|  | 109 | ASSERT_EQ(1, fscanf(fp, "sp: %" SCNx64 "\n", ®_value)); | 
|  | 110 | regs[ARM64_REG_SP] = reg_value; | 
|  | 111 | ASSERT_EQ(1, fscanf(fp, "lr: %" SCNx64 "\n", ®_value)); | 
|  | 112 | regs[ARM64_REG_LR] = reg_value; | 
|  | 113 | ASSERT_EQ(1, fscanf(fp, "x29: %" SCNx64 "\n", ®_value)); | 
|  | 114 | regs[ARM64_REG_R29] = reg_value; | 
|  | 115 | regs.SetFromRaw(); | 
|  | 116 | fclose(fp); | 
|  | 117 |  | 
|  | 118 | fp = fopen((dir + "maps.txt").c_str(), "r"); | 
|  | 119 | ASSERT_TRUE(fp != nullptr); | 
|  | 120 | // The file is guaranteed to be less than 4096 bytes. | 
|  | 121 | std::vector<char> buffer(4096); | 
|  | 122 | ASSERT_NE(0U, fread(buffer.data(), 1, buffer.size(), fp)); | 
|  | 123 | fclose(fp); | 
|  | 124 |  | 
|  | 125 | BufferMaps maps(buffer.data()); | 
|  | 126 | ASSERT_TRUE(maps.Parse()); | 
|  | 127 |  | 
|  | 128 | ASSERT_EQ(static_cast<uint32_t>(EM_AARCH64), regs.MachineType()); | 
|  | 129 |  | 
|  | 130 | std::shared_ptr<Memory> process_memory(memory); | 
|  | 131 |  | 
|  | 132 | char* cwd = getcwd(nullptr, 0); | 
|  | 133 | ASSERT_EQ(0, chdir(dir.c_str())); | 
|  | 134 | Unwinder unwinder(128, &maps, ®s, process_memory); | 
|  | 135 | unwinder.Unwind(); | 
|  | 136 | ASSERT_EQ(0, chdir(cwd)); | 
|  | 137 | free(cwd); | 
|  | 138 |  | 
|  | 139 | std::string frame_info(DumpFrames(unwinder)); | 
|  | 140 | ASSERT_EQ(6U, unwinder.NumFrames()) << "Unwind:\n" << frame_info; | 
|  | 141 | EXPECT_EQ( | 
|  | 142 | "  #00 pc 0000000000429fd8  libunwindstack_test (SignalInnerFunction+24)\n" | 
|  | 143 | "  #01 pc 000000000042a078  libunwindstack_test (SignalMiddleFunction+8)\n" | 
|  | 144 | "  #02 pc 000000000042a08c  libunwindstack_test (SignalOuterFunction+8)\n" | 
|  | 145 | "  #03 pc 000000000042d8fc  libunwindstack_test " | 
|  | 146 | "(_ZN11unwindstackL19RemoteThroughSignalEij+20)\n" | 
|  | 147 | "  #04 pc 000000000042d8d8  libunwindstack_test " | 
|  | 148 | "(_ZN11unwindstack37UnwindTest_remote_through_signal_Test8TestBodyEv+32)\n" | 
|  | 149 | "  #05 pc 0000000000455d70  libunwindstack_test (_ZN7testing4Test3RunEv+392)\n", | 
|  | 150 | frame_info); | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | }  // namespace unwindstack |