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