blob: 11bcde19fff02feab757d5c37b66e3c258b6c25e [file] [log] [blame]
Yabin Cui40b70ff2018-04-09 14:06:08 -07001/*
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 Cui991477b2020-07-17 16:12:15 -070023#include <android-base/test_utils.h>
Yabin Cui40b70ff2018-04-09 14:06:08 -070024
25#include "get_test_data.h"
Yabin Cui8422f342018-05-09 17:27:27 -070026#include "read_apk.h"
Yabin Cui7078c672020-11-10 16:24:12 -080027#include "thread_tree.h"
Yabin Cui1b9b1c12018-10-29 14:23:48 -070028#include "utils.h"
Yabin Cui40b70ff2018-04-09 14:06:08 -070029
Yabin Cuifaa7b922021-01-11 17:35:57 -080030using namespace simpleperf;
Yabin Cui40b70ff2018-04-09 14:06:08 -070031using namespace simpleperf_dso_impl;
32
33TEST(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 Cui2969a9e2018-04-19 17:06:24 -070042 "%s=%s\n", build_id.ToString().c_str(), android::base::Basename(tmpfile.path).c_str());
Yabin Cui40b70ff2018-04-09 14:06:08 -070043 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 Cui5d269c72019-05-31 15:30:17 -070052static 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 Cui40b70ff2018-04-09 14:06:08 -070060TEST(DebugElfFileFinder, concatenating_symfs_dir) {
61 DebugElfFileFinder finder;
62 ASSERT_TRUE(finder.SetSymFsDir(GetTestDataDir()));
Yabin Cui1b9b1c12018-10-29 14:23:48 -070063 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 Cui40b70ff2018-04-09 14:06:08 -070068 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 Cui5d269c72019-05-31 15:30:17 -070071 std::string apk_path = ConvertPathSeparator(APK_FILE);
Yabin Cui40b70ff2018-04-09 14:06:08 -070072 ASSERT_EQ(finder.FindDebugFile(native_lib_in_apk, false, native_lib_build_id),
Yabin Cui1b9b1c12018-10-29 14:23:48 -070073 GetTestDataDir() + apk_path + "!/" + NATIVELIB_IN_APK);
Yabin Cui40b70ff2018-04-09 14:06:08 -070074}
75
76TEST(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 Cuidd401b32018-04-11 11:17:06 -070086
Yabin Cui3939b9d2018-07-20 17:12:13 -070087TEST(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 Cui5d269c72019-05-31 15:30:17 -070091 std::string symfs_dir = ConvertPathSeparator(GetTestDataDir() + CORRECT_SYMFS_FOR_BUILD_ID_CHECK);
Yabin Cui1b9b1c12018-10-29 14:23:48 -070092 ASSERT_TRUE(finder.AddSymbolDir(symfs_dir));
Yabin Cui3939b9d2018-07-20 17:12:13 -070093 ASSERT_EQ(finder.FindDebugFile("elf", false, CHECK_ELF_FILE_BUILD_ID),
Yabin Cui1b9b1c12018-10-29 14:23:48 -070094 symfs_dir + OS_PATH_SEPARATOR + "elf_for_build_id_check");
Yabin Cui3939b9d2018-07-20 17:12:13 -070095}
96
Yabin Cui5d269c72019-05-31 15:30:17 -070097TEST(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 Cuid347bb42019-11-14 15:24:07 -0800112TEST(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 Cuid347bb42019-11-14 15:24:07 -0800115 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 Cuia4496ad2019-11-18 16:40:28 -0800121TEST(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 Cui991477b2020-07-17 16:12:15 -0700139TEST(DebugElfFileFinder, build_id_mismatch) {
140 DebugElfFileFinder finder;
141 finder.SetSymFsDir(GetTestDataDir());
142 CapturedStderr capture;
Yabin Cui991477b2020-07-17 16:12:15 -0700143 BuildId mismatch_build_id("0c12a384a9f4a3f3659b7171ca615dbec3a81f71");
144 std::string debug_file = finder.FindDebugFile(ELF_FILE, false, mismatch_build_id);
145 capture.Stop();
146 std::string stderr_output = capture.str();
147 ASSERT_EQ(debug_file, ELF_FILE);
148 ASSERT_NE(stderr_output.find("build id mismatch"), std::string::npos);
149}
150
Yabin Cuidd401b32018-04-11 11:17:06 -0700151TEST(dso, dex_file_dso) {
152#if defined(__linux__)
153 for (DsoType dso_type : {DSO_DEX_FILE, DSO_ELF_FILE}) {
Nicolas Geoffray3ae1f422021-06-29 11:55:06 +0000154 std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, GetTestData("base.vdex"));
155 ASSERT_TRUE(dso);
156 dso->AddDexFileOffset(0x28);
157 ASSERT_EQ(DSO_DEX_FILE, dso->type());
158 const Symbol* symbol = dso->FindSymbol(0x6c77e);
159 ASSERT_NE(symbol, nullptr);
160 ASSERT_EQ(symbol->addr, static_cast<uint64_t>(0x6c77e));
161 ASSERT_EQ(symbol->len, static_cast<uint64_t>(0x16));
162 ASSERT_STREQ(symbol->DemangledName(),
163 "com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run");
164 uint64_t min_vaddr;
165 uint64_t file_offset_of_min_vaddr;
166 dso->GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr);
167 ASSERT_EQ(min_vaddr, 0);
168 ASSERT_EQ(file_offset_of_min_vaddr, 0);
Yabin Cui15749e02018-05-30 16:37:06 -0700169
170 // Don't crash on not exist zip entry.
Nicolas Geoffray3ae1f422021-06-29 11:55:06 +0000171 dso = Dso::CreateDso(dso_type, GetTestData("base.zip!/not_exist_entry"));
Yabin Cui15749e02018-05-30 16:37:06 -0700172 ASSERT_TRUE(dso);
173 ASSERT_EQ(nullptr, dso->FindSymbol(0));
Yabin Cuidd401b32018-04-11 11:17:06 -0700174 }
175#else
176 GTEST_LOG_(INFO) << "This test only runs on linux because of libdexfile";
177#endif // defined(__linux__)
178}
Yabin Cui8422f342018-05-09 17:27:27 -0700179
Yabin Cuic8571d42018-06-06 11:20:39 -0700180TEST(dso, dex_file_offsets) {
181 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_DEX_FILE, "");
182 ASSERT_TRUE(dso);
183 for (uint64_t offset : {0x3, 0x1, 0x5, 0x4, 0x2, 0x4, 0x3}) {
184 dso->AddDexFileOffset(offset);
185 }
186 ASSERT_EQ(*dso->DexFileOffsets(), std::vector<uint64_t>({0x1, 0x2, 0x3, 0x4, 0x5}));
187}
188
Yabin Cui8422f342018-05-09 17:27:27 -0700189TEST(dso, embedded_elf) {
190 const std::string file_path = GetUrlInApk(GetTestData(APK_FILE), NATIVELIB_IN_APK);
191 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_ELF_FILE, file_path);
192 ASSERT_TRUE(dso);
193 ASSERT_EQ(dso->Path(), file_path);
194 ASSERT_EQ(dso->GetDebugFilePath(), file_path);
Yabin Cuidb2c4932019-02-07 15:06:42 -0800195 uint64_t min_vaddr;
196 uint64_t file_offset_of_min_vaddr;
197 dso->GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr);
198 ASSERT_EQ(min_vaddr, 0);
199 ASSERT_EQ(file_offset_of_min_vaddr, 0);
Yabin Cui8422f342018-05-09 17:27:27 -0700200 const Symbol* symbol = dso->FindSymbol(0x9a4);
201 ASSERT_TRUE(symbol != nullptr);
202 ASSERT_STREQ(symbol->Name(), "Java_com_example_hellojni_HelloJni_callFunc1");
203 BuildId build_id;
204 ASSERT_TRUE(GetBuildIdFromDsoPath(file_path, &build_id));
205 ASSERT_EQ(build_id, native_lib_build_id);
206}
Yabin Cuidb2c4932019-02-07 15:06:42 -0800207
208TEST(dso, IpToVaddrInFile) {
209 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_ELF_FILE, GetTestData("libc.so"));
210 ASSERT_TRUE(dso);
211 ASSERT_EQ(0xa5140, dso->IpToVaddrInFile(0xe9201140, 0xe9201000, 0xa5000));
212}
Yabin Cui991477b2020-07-17 16:12:15 -0700213
Yabin Cui7078c672020-11-10 16:24:12 -0800214TEST(dso, kernel_address_randomization) {
215 // Use ELF_FILE as a fake kernel vmlinux.
216 const std::string vmlinux_path = GetTestData(ELF_FILE);
217 Dso::SetVmlinux(vmlinux_path);
218 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
219 ASSERT_TRUE(dso);
220 ASSERT_EQ(dso->GetDebugFilePath(), vmlinux_path);
221 // When map_start = 0, can't fix kernel address randomization. So vmlinux isn't used.
222 ASSERT_EQ(dso->IpToVaddrInFile(0x800500, 0, 0), 0x800500);
223 ASSERT_FALSE(dso->IpToFileOffset(0x800500, 0, 0));
224 ASSERT_TRUE(dso->FindSymbol(0x400510) == nullptr);
225
226 dso = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
227 ASSERT_TRUE(dso);
228 ASSERT_EQ(dso->GetDebugFilePath(), vmlinux_path);
229 // When map_start != 0, can fix kernel address randomization. So vmlinux is used.
230 ASSERT_EQ(dso->IpToVaddrInFile(0x800500, 0x800400, 0), 0x400500);
231 ASSERT_EQ(dso->IpToFileOffset(0x800500, 0x800400, 0).value(), 0x500);
232 const Symbol* symbol = dso->FindSymbol(0x400510);
233 ASSERT_TRUE(symbol != nullptr);
234 ASSERT_STREQ(symbol->Name(), "GlobalFunc");
235}
236
237TEST(dso, find_vmlinux_in_symdirs) {
238 // Create a symdir.
239 TemporaryDir tmpdir;
240 std::string vmlinux_path = std::string(tmpdir.path) + OS_PATH_SEPARATOR + "elf";
241 std::string data;
242 ASSERT_TRUE(android::base::ReadFileToString(GetTestData(ELF_FILE), &data));
243 ASSERT_TRUE(android::base::WriteStringToFile(data, vmlinux_path));
244
245 // Find vmlinux in symbol dirs.
246 Dso::SetVmlinux("");
247 Dso::AddSymbolDir(tmpdir.path);
248 Dso::SetBuildIds({std::make_pair(DEFAULT_KERNEL_MMAP_NAME, BuildId(ELF_FILE_BUILD_ID))});
249 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
250 ASSERT_TRUE(dso);
251 ASSERT_EQ(dso->GetDebugFilePath(), vmlinux_path);
Yabin Cuic7aed042022-09-09 15:54:45 -0700252 ASSERT_EQ(0x400927, dso->IpToVaddrInFile(0x800527, 0x800000, 0));
253
254 // Find vmlinux by CreateDsoWithBuildId.
255 Dso::SetBuildIds({});
256 BuildId build_id(ELF_FILE_BUILD_ID);
257 dso = Dso::CreateDsoWithBuildId(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME, build_id);
258 ASSERT_TRUE(dso);
259 ASSERT_EQ(dso->GetDebugFilePath(), vmlinux_path);
260 ASSERT_EQ(0x400927, dso->IpToVaddrInFile(0x800527, 0x800000, 0));
Yabin Cui7078c672020-11-10 16:24:12 -0800261}
262
Yabin Cui991477b2020-07-17 16:12:15 -0700263TEST(dso, kernel_module) {
264 // Test finding debug files for kernel modules.
265 Dso::SetSymFsDir(GetTestDataDir());
266 std::vector<std::pair<std::string, BuildId>> build_ids;
267 build_ids.emplace_back(ELF_FILE, BuildId(ELF_FILE_BUILD_ID));
268 Dso::SetBuildIds(build_ids);
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800269 std::unique_ptr<Dso> kernel_dso = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
270 ASSERT_TRUE(kernel_dso);
271 std::unique_ptr<Dso> dso = Dso::CreateKernelModuleDso(ELF_FILE, 0, 0, kernel_dso.get());
Yabin Cui991477b2020-07-17 16:12:15 -0700272 ASSERT_EQ(dso->GetDebugFilePath(), GetTestData(ELF_FILE));
273}
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200274
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800275TEST(dso, kernel_module_CalculateMinVaddr) {
276 // Create fake Dso objects.
277 auto kernel_dso = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
278 ASSERT_TRUE(kernel_dso);
279 const uint64_t module_memory_start = 0xffffffa9bc790000ULL;
280 const uint64_t module_memory_size = 0x8d7000ULL;
281 auto module_dso =
282 Dso::CreateKernelModuleDso("fake_module.ko", module_memory_start,
283 module_memory_start + module_memory_size, kernel_dso.get());
284 ASSERT_TRUE(module_dso);
285
286 // Provide symbol info for calculating min vaddr.
287 std::vector<Symbol> kernel_symbols;
288 kernel_symbols.emplace_back("fake_module_function [fake_module]", 0xffffffa9bc7a64e8ULL, 0x60c);
289 kernel_dso->SetSymbols(&kernel_symbols);
290 std::vector<Symbol> module_symbols;
291 module_symbols.emplace_back("fake_module_function", 0x144e8, 0x60c);
292 module_dso->SetSymbols(&module_symbols);
293
294 // Calculate min vaddr.
295 uint64_t min_vaddr;
296 uint64_t memory_offset;
297 module_dso->GetMinExecutableVaddr(&min_vaddr, &memory_offset);
298 ASSERT_EQ(min_vaddr, 0x144e8);
299 ASSERT_EQ(memory_offset, 0x164e8);
300
301 // Use min vaddr in IpToVaddrInFile().
302 ASSERT_EQ(module_dso->IpToVaddrInFile(0xffffffa9bc7a64e8ULL, module_memory_start, 0), 0x144e8);
303}
304
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200305TEST(dso, symbol_map_file) {
306 auto dso = Dso::CreateDso(DSO_SYMBOL_MAP_FILE, "perf-123.map");
307 ASSERT_TRUE(dso);
308 ASSERT_EQ(DSO_SYMBOL_MAP_FILE, dso->type());
309 ASSERT_EQ(0x12345678, dso->IpToVaddrInFile(0x12345678, 0x0, 0x0));
310 ASSERT_EQ(0x12345678, dso->IpToVaddrInFile(0x12345678, 0xe9201000, 0xa5000));
311}
Yabin Cui1e16b202021-08-16 13:37:35 -0700312
Yabin Cuifef95142021-08-19 10:51:00 -0700313TEST(dso, FunctionName) {
Yabin Cui1e16b202021-08-16 13:37:35 -0700314 Symbol symbol = Symbol("void ctep.v(cteo, ctgc, ctbn)", 0x0, 0x1);
Yabin Cuifef95142021-08-19 10:51:00 -0700315 ASSERT_EQ(symbol.FunctionName(), "ctep.v");
316 symbol = Symbol("ctep.v(cteo, ctgc, ctbn)", 0x0, 0x1);
317 ASSERT_EQ(symbol.FunctionName(), "ctep.v");
Yabin Cui1e16b202021-08-16 13:37:35 -0700318 symbol = Symbol("ctep.v", 0x0, 0x1);
Yabin Cuifef95142021-08-19 10:51:00 -0700319 ASSERT_EQ(symbol.FunctionName(), "ctep.v");
Yabin Cui1e16b202021-08-16 13:37:35 -0700320}
Yabin Cui11424c42022-03-10 16:04:04 -0800321
322TEST(dso, search_debug_file_only_when_needed) {
323 Dso::SetBuildIds({std::make_pair("/elf", BuildId("1b12a384a9f4a3f3659b7171ca615dbec3a81f71"))});
324 Dso::SetSymFsDir(GetTestDataDir());
325 CapturedStderr capture;
Yabin Cui11424c42022-03-10 16:04:04 -0800326 auto dso = Dso::CreateDso(DSO_ELF_FILE, "/elf");
327 ASSERT_EQ(capture.str().find("build id mismatch"), std::string::npos);
328 ASSERT_EQ(dso->GetDebugFilePath(), "/elf");
329 ASSERT_NE(capture.str().find("build id mismatch"), std::string::npos);
330 capture.Stop();
331}
Yabin Cui2315ff62022-11-14 11:52:18 -0800332
333TEST(dso, read_symbol_warning) {
334 {
335 // Don't warn when the file may not be an ELF file.
336 auto dso = Dso::CreateDso(DSO_ELF_FILE, GetTestData("not_exist_file"));
337 CapturedStderr capture;
338 dso->LoadSymbols();
339 ASSERT_EQ(capture.str().find("failed to read symbols"), std::string::npos);
340 }
341 {
342 // Don't warn when the file may not be an ELF file.
343 auto dso = Dso::CreateDso(DSO_ELF_FILE, GetTestData("base.vdex"));
344 CapturedStderr capture;
345 dso->LoadSymbols();
346 ASSERT_EQ(capture.str().find("failed to read symbols"), std::string::npos);
347 }
348 {
349 // Warn when the file is an ELF file (having a build id).
350 std::string file_path = GetTestData("not_exist_file");
351 Dso::SetBuildIds(
352 {std::make_pair(file_path, BuildId("1b12a384a9f4a3f3659b7171ca615dbec3a81f71"))});
353 auto dso = Dso::CreateDso(DSO_ELF_FILE, file_path);
354 CapturedStderr capture;
355 dso->LoadSymbols();
356 ASSERT_NE(capture.str().find("failed to read symbols"), std::string::npos);
357 }
358 {
359 // Don't warn when we already have symbols.
360 std::string file_path = GetTestData("not_exist_file");
361 Dso::SetBuildIds(
362 {std::make_pair(file_path, BuildId("1b12a384a9f4a3f3659b7171ca615dbec3a81f71"))});
363 auto dso = Dso::CreateDso(DSO_ELF_FILE, file_path);
364 std::vector<Symbol> symbols;
365 symbols.emplace_back("fake_symbol", 0x1234, 0x60);
366 dso->SetSymbols(&symbols);
367 CapturedStderr capture;
368 dso->LoadSymbols();
369 ASSERT_EQ(capture.str().find("failed to read symbols"), std::string::npos);
370 }
371}
Yabin Cui4d8137f2023-02-14 17:00:25 -0800372
373TEST(dso, demangle) {
374 ASSERT_EQ(Dso::Demangle("main"), "main");
375 ASSERT_EQ(Dso::Demangle("_ZN4main4main17h2a68d4d833d7495aE"), "main::main::h2a68d4d833d7495a");
376#if defined(__linux__) || defined(__darwin__)
377 // Demangling rust symbol is only supported on linux and darwin.
378 ASSERT_EQ(Dso::Demangle("_RNvC6_123foo3bar"), "123foo::bar");
379#endif
380}