Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "dso.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include <android-base/file.h> |
| 22 | #include <android-base/stringprintf.h> |
Yabin Cui | 991477b | 2020-07-17 16:12:15 -0700 | [diff] [blame] | 23 | #include <android-base/test_utils.h> |
Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 24 | |
| 25 | #include "get_test_data.h" |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 26 | #include "read_apk.h" |
Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 27 | #include "thread_tree.h" |
Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 28 | #include "utils.h" |
Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 29 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 30 | using namespace simpleperf; |
Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 31 | using namespace simpleperf_dso_impl; |
| 32 | |
| 33 | TEST(DebugElfFileFinder, use_build_id_list) { |
| 34 | // Create a temp symdir with build_id_list. |
| 35 | TemporaryDir tmpdir; |
| 36 | TemporaryFile tmpfile(tmpdir.path); |
| 37 | std::string data; |
| 38 | ASSERT_TRUE(android::base::ReadFileToString(GetTestData(ELF_FILE), &data)); |
| 39 | ASSERT_TRUE(android::base::WriteStringToFile(data, tmpfile.path)); |
| 40 | BuildId build_id(ELF_FILE_BUILD_ID); |
| 41 | std::string build_id_list = android::base::StringPrintf( |
Yabin Cui | 2969a9e | 2018-04-19 17:06:24 -0700 | [diff] [blame] | 42 | "%s=%s\n", build_id.ToString().c_str(), android::base::Basename(tmpfile.path).c_str()); |
Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 43 | std::string build_id_list_file = std::string(tmpdir.path) + "/build_id_list"; |
| 44 | ASSERT_TRUE(android::base::WriteStringToFile(build_id_list, build_id_list_file)); |
| 45 | |
| 46 | DebugElfFileFinder finder; |
| 47 | ASSERT_TRUE(finder.SetSymFsDir(tmpdir.path)); |
| 48 | ASSERT_EQ(finder.FindDebugFile("elf", false, build_id), std::string(tmpfile.path)); |
| 49 | unlink(build_id_list_file.c_str()); |
| 50 | } |
| 51 | |
Yabin Cui | 5d269c7 | 2019-05-31 15:30:17 -0700 | [diff] [blame] | 52 | static std::string ConvertPathSeparator(const std::string& path) { |
| 53 | std::string result = path; |
| 54 | if (OS_PATH_SEPARATOR != '/') { |
| 55 | std::replace(result.begin(), result.end(), '/', OS_PATH_SEPARATOR); |
| 56 | } |
| 57 | return result; |
| 58 | } |
| 59 | |
Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 60 | TEST(DebugElfFileFinder, concatenating_symfs_dir) { |
| 61 | DebugElfFileFinder finder; |
| 62 | ASSERT_TRUE(finder.SetSymFsDir(GetTestDataDir())); |
Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 63 | ASSERT_EQ(finder.GetPathInSymFsDir("/system/libc.so"), |
| 64 | GetTestDataDir() + "system" + OS_PATH_SEPARATOR + "libc.so"); |
| 65 | ASSERT_EQ(finder.GetPathInSymFsDir("/data/base.apk!/lib/base.so"), |
| 66 | GetTestDataDir() + "data" + OS_PATH_SEPARATOR + "base.apk!/lib/base.so"); |
| 67 | |
Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 68 | BuildId build_id(ELF_FILE_BUILD_ID); |
| 69 | ASSERT_EQ(finder.FindDebugFile(ELF_FILE, false, build_id), GetTestDataDir() + ELF_FILE); |
| 70 | std::string native_lib_in_apk = APK_FILE + "!/" + NATIVELIB_IN_APK; |
Yabin Cui | 5d269c7 | 2019-05-31 15:30:17 -0700 | [diff] [blame] | 71 | std::string apk_path = ConvertPathSeparator(APK_FILE); |
Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 72 | ASSERT_EQ(finder.FindDebugFile(native_lib_in_apk, false, native_lib_build_id), |
Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 73 | GetTestDataDir() + apk_path + "!/" + NATIVELIB_IN_APK); |
Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | TEST(DebugElfFileFinder, use_vdso) { |
| 77 | DebugElfFileFinder finder; |
| 78 | std::string fake_vdso32 = "fake_vdso32"; |
| 79 | std::string fake_vdso64 = "fake_vdso64"; |
| 80 | finder.SetVdsoFile(fake_vdso32, false); |
| 81 | finder.SetVdsoFile(fake_vdso64, true); |
| 82 | BuildId build_id; |
| 83 | ASSERT_EQ(finder.FindDebugFile("[vdso]", false, build_id), fake_vdso32); |
| 84 | ASSERT_EQ(finder.FindDebugFile("[vdso]", true, build_id), fake_vdso64); |
| 85 | } |
Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 86 | |
Yabin Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 87 | TEST(DebugElfFileFinder, add_symbol_dir) { |
| 88 | DebugElfFileFinder finder; |
| 89 | ASSERT_FALSE(finder.AddSymbolDir(GetTestDataDir() + "dir_not_exist")); |
| 90 | ASSERT_EQ(finder.FindDebugFile("elf", false, CHECK_ELF_FILE_BUILD_ID), "elf"); |
Yabin Cui | 5d269c7 | 2019-05-31 15:30:17 -0700 | [diff] [blame] | 91 | std::string symfs_dir = ConvertPathSeparator(GetTestDataDir() + CORRECT_SYMFS_FOR_BUILD_ID_CHECK); |
Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 92 | ASSERT_TRUE(finder.AddSymbolDir(symfs_dir)); |
Yabin Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 93 | ASSERT_EQ(finder.FindDebugFile("elf", false, CHECK_ELF_FILE_BUILD_ID), |
Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 94 | symfs_dir + OS_PATH_SEPARATOR + "elf_for_build_id_check"); |
Yabin Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Yabin Cui | 5d269c7 | 2019-05-31 15:30:17 -0700 | [diff] [blame] | 97 | TEST(DebugElfFileFinder, build_id_list) { |
| 98 | DebugElfFileFinder finder; |
| 99 | // Find file in symfs dir with correct build_id_list. |
| 100 | std::string symfs_dir = ConvertPathSeparator(GetTestDataDir() + "data/symfs_with_build_id_list"); |
| 101 | ASSERT_TRUE(finder.SetSymFsDir(symfs_dir)); |
| 102 | ASSERT_EQ(finder.FindDebugFile("elf", false, CHECK_ELF_FILE_BUILD_ID), |
| 103 | symfs_dir + OS_PATH_SEPARATOR + "elf_for_build_id_check"); |
| 104 | |
| 105 | // Find file in symfs_dir with wrong build_id_list. |
| 106 | symfs_dir = ConvertPathSeparator(GetTestDataDir() + "data/symfs_with_wrong_build_id_list"); |
| 107 | finder.Reset(); |
| 108 | ASSERT_TRUE(finder.SetSymFsDir(symfs_dir)); |
| 109 | ASSERT_EQ(finder.FindDebugFile("elf", false, CHECK_ELF_FILE_BUILD_ID), "elf"); |
| 110 | } |
| 111 | |
Yabin Cui | d347bb4 | 2019-11-14 15:24:07 -0800 | [diff] [blame] | 112 | TEST(DebugElfFileFinder, no_build_id) { |
| 113 | DebugElfFileFinder finder; |
| 114 | // If not given a build id, we should match an elf in symfs without build id. |
Yabin Cui | d347bb4 | 2019-11-14 15:24:07 -0800 | [diff] [blame] | 115 | std::string symfs_dir = ConvertPathSeparator(GetTestDataDir() + "data/symfs_without_build_id"); |
| 116 | ASSERT_TRUE(finder.SetSymFsDir(symfs_dir)); |
| 117 | BuildId build_id; |
| 118 | ASSERT_EQ(finder.FindDebugFile("elf", false, build_id), symfs_dir + OS_PATH_SEPARATOR + "elf"); |
| 119 | } |
| 120 | |
Yabin Cui | a4496ad | 2019-11-18 16:40:28 -0800 | [diff] [blame] | 121 | TEST(DebugElfFileFinder, find_basename_in_symfs_dir) { |
| 122 | DebugElfFileFinder finder; |
| 123 | // Find normal elf file. |
| 124 | finder.SetSymFsDir(GetTestDataDir()); |
| 125 | BuildId build_id(ELF_FILE_BUILD_ID); |
| 126 | ASSERT_EQ(finder.FindDebugFile("random_dir/elf", false, build_id), GetTestData("elf")); |
| 127 | |
| 128 | // Find embedded native library. |
| 129 | ASSERT_EQ(finder.FindDebugFile("base.apk!/lib/x86_64/elf", false, build_id), GetTestData("elf")); |
| 130 | |
| 131 | // Find elf file without build id. |
| 132 | std::string symfs_dir = ConvertPathSeparator(GetTestDataDir() + "data/symfs_without_build_id"); |
| 133 | finder.SetSymFsDir(symfs_dir); |
| 134 | build_id = BuildId(); |
| 135 | ASSERT_EQ(finder.FindDebugFile("random_dir/elf", false, build_id), |
| 136 | symfs_dir + OS_PATH_SEPARATOR + "elf"); |
| 137 | } |
| 138 | |
Yabin Cui | 991477b | 2020-07-17 16:12:15 -0700 | [diff] [blame] | 139 | TEST(DebugElfFileFinder, build_id_mismatch) { |
| 140 | DebugElfFileFinder finder; |
| 141 | finder.SetSymFsDir(GetTestDataDir()); |
| 142 | CapturedStderr capture; |
| 143 | capture.Start(); |
| 144 | BuildId mismatch_build_id("0c12a384a9f4a3f3659b7171ca615dbec3a81f71"); |
| 145 | std::string debug_file = finder.FindDebugFile(ELF_FILE, false, mismatch_build_id); |
| 146 | capture.Stop(); |
| 147 | std::string stderr_output = capture.str(); |
| 148 | ASSERT_EQ(debug_file, ELF_FILE); |
| 149 | ASSERT_NE(stderr_output.find("build id mismatch"), std::string::npos); |
| 150 | } |
| 151 | |
Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 152 | TEST(dso, dex_file_dso) { |
| 153 | #if defined(__linux__) |
| 154 | for (DsoType dso_type : {DSO_DEX_FILE, DSO_ELF_FILE}) { |
Nicolas Geoffray | 3ae1f42 | 2021-06-29 11:55:06 +0000 | [diff] [blame] | 155 | std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, GetTestData("base.vdex")); |
| 156 | ASSERT_TRUE(dso); |
| 157 | dso->AddDexFileOffset(0x28); |
| 158 | ASSERT_EQ(DSO_DEX_FILE, dso->type()); |
| 159 | const Symbol* symbol = dso->FindSymbol(0x6c77e); |
| 160 | ASSERT_NE(symbol, nullptr); |
| 161 | ASSERT_EQ(symbol->addr, static_cast<uint64_t>(0x6c77e)); |
| 162 | ASSERT_EQ(symbol->len, static_cast<uint64_t>(0x16)); |
| 163 | ASSERT_STREQ(symbol->DemangledName(), |
| 164 | "com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run"); |
| 165 | uint64_t min_vaddr; |
| 166 | uint64_t file_offset_of_min_vaddr; |
| 167 | dso->GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr); |
| 168 | ASSERT_EQ(min_vaddr, 0); |
| 169 | ASSERT_EQ(file_offset_of_min_vaddr, 0); |
Yabin Cui | 15749e0 | 2018-05-30 16:37:06 -0700 | [diff] [blame] | 170 | |
| 171 | // Don't crash on not exist zip entry. |
Nicolas Geoffray | 3ae1f42 | 2021-06-29 11:55:06 +0000 | [diff] [blame] | 172 | dso = Dso::CreateDso(dso_type, GetTestData("base.zip!/not_exist_entry")); |
Yabin Cui | 15749e0 | 2018-05-30 16:37:06 -0700 | [diff] [blame] | 173 | ASSERT_TRUE(dso); |
| 174 | ASSERT_EQ(nullptr, dso->FindSymbol(0)); |
Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 175 | } |
| 176 | #else |
| 177 | GTEST_LOG_(INFO) << "This test only runs on linux because of libdexfile"; |
| 178 | #endif // defined(__linux__) |
| 179 | } |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 180 | |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 181 | TEST(dso, dex_file_offsets) { |
| 182 | std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_DEX_FILE, ""); |
| 183 | ASSERT_TRUE(dso); |
| 184 | for (uint64_t offset : {0x3, 0x1, 0x5, 0x4, 0x2, 0x4, 0x3}) { |
| 185 | dso->AddDexFileOffset(offset); |
| 186 | } |
| 187 | ASSERT_EQ(*dso->DexFileOffsets(), std::vector<uint64_t>({0x1, 0x2, 0x3, 0x4, 0x5})); |
| 188 | } |
| 189 | |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 190 | TEST(dso, embedded_elf) { |
| 191 | const std::string file_path = GetUrlInApk(GetTestData(APK_FILE), NATIVELIB_IN_APK); |
| 192 | std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_ELF_FILE, file_path); |
| 193 | ASSERT_TRUE(dso); |
| 194 | ASSERT_EQ(dso->Path(), file_path); |
| 195 | ASSERT_EQ(dso->GetDebugFilePath(), file_path); |
Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 196 | uint64_t min_vaddr; |
| 197 | uint64_t file_offset_of_min_vaddr; |
| 198 | dso->GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr); |
| 199 | ASSERT_EQ(min_vaddr, 0); |
| 200 | ASSERT_EQ(file_offset_of_min_vaddr, 0); |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 201 | const Symbol* symbol = dso->FindSymbol(0x9a4); |
| 202 | ASSERT_TRUE(symbol != nullptr); |
| 203 | ASSERT_STREQ(symbol->Name(), "Java_com_example_hellojni_HelloJni_callFunc1"); |
| 204 | BuildId build_id; |
| 205 | ASSERT_TRUE(GetBuildIdFromDsoPath(file_path, &build_id)); |
| 206 | ASSERT_EQ(build_id, native_lib_build_id); |
| 207 | } |
Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 208 | |
| 209 | TEST(dso, IpToVaddrInFile) { |
| 210 | std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_ELF_FILE, GetTestData("libc.so")); |
| 211 | ASSERT_TRUE(dso); |
| 212 | ASSERT_EQ(0xa5140, dso->IpToVaddrInFile(0xe9201140, 0xe9201000, 0xa5000)); |
| 213 | } |
Yabin Cui | 991477b | 2020-07-17 16:12:15 -0700 | [diff] [blame] | 214 | |
Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 215 | TEST(dso, kernel_address_randomization) { |
| 216 | // Use ELF_FILE as a fake kernel vmlinux. |
| 217 | const std::string vmlinux_path = GetTestData(ELF_FILE); |
| 218 | Dso::SetVmlinux(vmlinux_path); |
| 219 | std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME); |
| 220 | ASSERT_TRUE(dso); |
| 221 | ASSERT_EQ(dso->GetDebugFilePath(), vmlinux_path); |
| 222 | // When map_start = 0, can't fix kernel address randomization. So vmlinux isn't used. |
| 223 | ASSERT_EQ(dso->IpToVaddrInFile(0x800500, 0, 0), 0x800500); |
| 224 | ASSERT_FALSE(dso->IpToFileOffset(0x800500, 0, 0)); |
| 225 | ASSERT_TRUE(dso->FindSymbol(0x400510) == nullptr); |
| 226 | |
| 227 | dso = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME); |
| 228 | ASSERT_TRUE(dso); |
| 229 | ASSERT_EQ(dso->GetDebugFilePath(), vmlinux_path); |
| 230 | // When map_start != 0, can fix kernel address randomization. So vmlinux is used. |
| 231 | ASSERT_EQ(dso->IpToVaddrInFile(0x800500, 0x800400, 0), 0x400500); |
| 232 | ASSERT_EQ(dso->IpToFileOffset(0x800500, 0x800400, 0).value(), 0x500); |
| 233 | const Symbol* symbol = dso->FindSymbol(0x400510); |
| 234 | ASSERT_TRUE(symbol != nullptr); |
| 235 | ASSERT_STREQ(symbol->Name(), "GlobalFunc"); |
| 236 | } |
| 237 | |
| 238 | TEST(dso, find_vmlinux_in_symdirs) { |
| 239 | // Create a symdir. |
| 240 | TemporaryDir tmpdir; |
| 241 | std::string vmlinux_path = std::string(tmpdir.path) + OS_PATH_SEPARATOR + "elf"; |
| 242 | std::string data; |
| 243 | ASSERT_TRUE(android::base::ReadFileToString(GetTestData(ELF_FILE), &data)); |
| 244 | ASSERT_TRUE(android::base::WriteStringToFile(data, vmlinux_path)); |
| 245 | |
| 246 | // Find vmlinux in symbol dirs. |
| 247 | Dso::SetVmlinux(""); |
| 248 | Dso::AddSymbolDir(tmpdir.path); |
| 249 | Dso::SetBuildIds({std::make_pair(DEFAULT_KERNEL_MMAP_NAME, BuildId(ELF_FILE_BUILD_ID))}); |
| 250 | std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME); |
| 251 | ASSERT_TRUE(dso); |
| 252 | ASSERT_EQ(dso->GetDebugFilePath(), vmlinux_path); |
| 253 | } |
| 254 | |
Yabin Cui | 991477b | 2020-07-17 16:12:15 -0700 | [diff] [blame] | 255 | TEST(dso, kernel_module) { |
| 256 | // Test finding debug files for kernel modules. |
| 257 | Dso::SetSymFsDir(GetTestDataDir()); |
| 258 | std::vector<std::pair<std::string, BuildId>> build_ids; |
| 259 | build_ids.emplace_back(ELF_FILE, BuildId(ELF_FILE_BUILD_ID)); |
| 260 | Dso::SetBuildIds(build_ids); |
Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 261 | std::unique_ptr<Dso> kernel_dso = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME); |
| 262 | ASSERT_TRUE(kernel_dso); |
| 263 | std::unique_ptr<Dso> dso = Dso::CreateKernelModuleDso(ELF_FILE, 0, 0, kernel_dso.get()); |
Yabin Cui | 991477b | 2020-07-17 16:12:15 -0700 | [diff] [blame] | 264 | ASSERT_EQ(dso->GetDebugFilePath(), GetTestData(ELF_FILE)); |
| 265 | } |
Evgeny Eltsin | 91dbae0 | 2020-08-27 15:46:09 +0200 | [diff] [blame] | 266 | |
Yabin Cui | f3da1ed | 2020-11-25 15:37:38 -0800 | [diff] [blame] | 267 | TEST(dso, kernel_module_CalculateMinVaddr) { |
| 268 | // Create fake Dso objects. |
| 269 | auto kernel_dso = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME); |
| 270 | ASSERT_TRUE(kernel_dso); |
| 271 | const uint64_t module_memory_start = 0xffffffa9bc790000ULL; |
| 272 | const uint64_t module_memory_size = 0x8d7000ULL; |
| 273 | auto module_dso = |
| 274 | Dso::CreateKernelModuleDso("fake_module.ko", module_memory_start, |
| 275 | module_memory_start + module_memory_size, kernel_dso.get()); |
| 276 | ASSERT_TRUE(module_dso); |
| 277 | |
| 278 | // Provide symbol info for calculating min vaddr. |
| 279 | std::vector<Symbol> kernel_symbols; |
| 280 | kernel_symbols.emplace_back("fake_module_function [fake_module]", 0xffffffa9bc7a64e8ULL, 0x60c); |
| 281 | kernel_dso->SetSymbols(&kernel_symbols); |
| 282 | std::vector<Symbol> module_symbols; |
| 283 | module_symbols.emplace_back("fake_module_function", 0x144e8, 0x60c); |
| 284 | module_dso->SetSymbols(&module_symbols); |
| 285 | |
| 286 | // Calculate min vaddr. |
| 287 | uint64_t min_vaddr; |
| 288 | uint64_t memory_offset; |
| 289 | module_dso->GetMinExecutableVaddr(&min_vaddr, &memory_offset); |
| 290 | ASSERT_EQ(min_vaddr, 0x144e8); |
| 291 | ASSERT_EQ(memory_offset, 0x164e8); |
| 292 | |
| 293 | // Use min vaddr in IpToVaddrInFile(). |
| 294 | ASSERT_EQ(module_dso->IpToVaddrInFile(0xffffffa9bc7a64e8ULL, module_memory_start, 0), 0x144e8); |
| 295 | } |
| 296 | |
Evgeny Eltsin | 91dbae0 | 2020-08-27 15:46:09 +0200 | [diff] [blame] | 297 | TEST(dso, symbol_map_file) { |
| 298 | auto dso = Dso::CreateDso(DSO_SYMBOL_MAP_FILE, "perf-123.map"); |
| 299 | ASSERT_TRUE(dso); |
| 300 | ASSERT_EQ(DSO_SYMBOL_MAP_FILE, dso->type()); |
| 301 | ASSERT_EQ(0x12345678, dso->IpToVaddrInFile(0x12345678, 0x0, 0x0)); |
| 302 | ASSERT_EQ(0x12345678, dso->IpToVaddrInFile(0x12345678, 0xe9201000, 0xa5000)); |
| 303 | } |
Yabin Cui | 1e16b20 | 2021-08-16 13:37:35 -0700 | [diff] [blame] | 304 | |
Yabin Cui | fef9514 | 2021-08-19 10:51:00 -0700 | [diff] [blame] | 305 | TEST(dso, FunctionName) { |
Yabin Cui | 1e16b20 | 2021-08-16 13:37:35 -0700 | [diff] [blame] | 306 | Symbol symbol = Symbol("void ctep.v(cteo, ctgc, ctbn)", 0x0, 0x1); |
Yabin Cui | fef9514 | 2021-08-19 10:51:00 -0700 | [diff] [blame] | 307 | ASSERT_EQ(symbol.FunctionName(), "ctep.v"); |
| 308 | symbol = Symbol("ctep.v(cteo, ctgc, ctbn)", 0x0, 0x1); |
| 309 | ASSERT_EQ(symbol.FunctionName(), "ctep.v"); |
Yabin Cui | 1e16b20 | 2021-08-16 13:37:35 -0700 | [diff] [blame] | 310 | symbol = Symbol("ctep.v", 0x0, 0x1); |
Yabin Cui | fef9514 | 2021-08-19 10:51:00 -0700 | [diff] [blame] | 311 | ASSERT_EQ(symbol.FunctionName(), "ctep.v"); |
Yabin Cui | 1e16b20 | 2021-08-16 13:37:35 -0700 | [diff] [blame] | 312 | } |
Yabin Cui | 11424c4 | 2022-03-10 16:04:04 -0800 | [diff] [blame] | 313 | |
| 314 | TEST(dso, search_debug_file_only_when_needed) { |
| 315 | Dso::SetBuildIds({std::make_pair("/elf", BuildId("1b12a384a9f4a3f3659b7171ca615dbec3a81f71"))}); |
| 316 | Dso::SetSymFsDir(GetTestDataDir()); |
| 317 | CapturedStderr capture; |
| 318 | capture.Start(); |
| 319 | auto dso = Dso::CreateDso(DSO_ELF_FILE, "/elf"); |
| 320 | ASSERT_EQ(capture.str().find("build id mismatch"), std::string::npos); |
| 321 | ASSERT_EQ(dso->GetDebugFilePath(), "/elf"); |
| 322 | ASSERT_NE(capture.str().find("build id mismatch"), std::string::npos); |
| 323 | capture.Stop(); |
| 324 | } |