blob: 04d039a28ee7f0390f171af653b2ab75ebb3b17c [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 Cui40b70ff2018-04-09 14:06:08 -070023
24#include "get_test_data.h"
Yabin Cui8422f342018-05-09 17:27:27 -070025#include "read_apk.h"
Yabin Cui1b9b1c12018-10-29 14:23:48 -070026#include "utils.h"
Yabin Cui40b70ff2018-04-09 14:06:08 -070027
28using namespace simpleperf_dso_impl;
29
30TEST(DebugElfFileFinder, use_build_id_list) {
31 // Create a temp symdir with build_id_list.
32 TemporaryDir tmpdir;
33 TemporaryFile tmpfile(tmpdir.path);
34 std::string data;
35 ASSERT_TRUE(android::base::ReadFileToString(GetTestData(ELF_FILE), &data));
36 ASSERT_TRUE(android::base::WriteStringToFile(data, tmpfile.path));
37 BuildId build_id(ELF_FILE_BUILD_ID);
38 std::string build_id_list = android::base::StringPrintf(
Yabin Cui2969a9e2018-04-19 17:06:24 -070039 "%s=%s\n", build_id.ToString().c_str(), android::base::Basename(tmpfile.path).c_str());
Yabin Cui40b70ff2018-04-09 14:06:08 -070040 std::string build_id_list_file = std::string(tmpdir.path) + "/build_id_list";
41 ASSERT_TRUE(android::base::WriteStringToFile(build_id_list, build_id_list_file));
42
43 DebugElfFileFinder finder;
44 ASSERT_TRUE(finder.SetSymFsDir(tmpdir.path));
45 ASSERT_EQ(finder.FindDebugFile("elf", false, build_id), std::string(tmpfile.path));
46 unlink(build_id_list_file.c_str());
47}
48
49TEST(DebugElfFileFinder, concatenating_symfs_dir) {
50 DebugElfFileFinder finder;
51 ASSERT_TRUE(finder.SetSymFsDir(GetTestDataDir()));
Yabin Cui1b9b1c12018-10-29 14:23:48 -070052 ASSERT_EQ(finder.GetPathInSymFsDir("/system/libc.so"),
53 GetTestDataDir() + "system" + OS_PATH_SEPARATOR + "libc.so");
54 ASSERT_EQ(finder.GetPathInSymFsDir("/data/base.apk!/lib/base.so"),
55 GetTestDataDir() + "data" + OS_PATH_SEPARATOR + "base.apk!/lib/base.so");
56
Yabin Cui40b70ff2018-04-09 14:06:08 -070057 BuildId build_id(ELF_FILE_BUILD_ID);
58 ASSERT_EQ(finder.FindDebugFile(ELF_FILE, false, build_id), GetTestDataDir() + ELF_FILE);
59 std::string native_lib_in_apk = APK_FILE + "!/" + NATIVELIB_IN_APK;
Yabin Cui1b9b1c12018-10-29 14:23:48 -070060 std::string apk_path = APK_FILE;
61 std::replace(apk_path.begin(), apk_path.end(), '/', OS_PATH_SEPARATOR);
Yabin Cui40b70ff2018-04-09 14:06:08 -070062 ASSERT_EQ(finder.FindDebugFile(native_lib_in_apk, false, native_lib_build_id),
Yabin Cui1b9b1c12018-10-29 14:23:48 -070063 GetTestDataDir() + apk_path + "!/" + NATIVELIB_IN_APK);
Yabin Cui40b70ff2018-04-09 14:06:08 -070064}
65
66TEST(DebugElfFileFinder, use_vdso) {
67 DebugElfFileFinder finder;
68 std::string fake_vdso32 = "fake_vdso32";
69 std::string fake_vdso64 = "fake_vdso64";
70 finder.SetVdsoFile(fake_vdso32, false);
71 finder.SetVdsoFile(fake_vdso64, true);
72 BuildId build_id;
73 ASSERT_EQ(finder.FindDebugFile("[vdso]", false, build_id), fake_vdso32);
74 ASSERT_EQ(finder.FindDebugFile("[vdso]", true, build_id), fake_vdso64);
75}
Yabin Cuidd401b32018-04-11 11:17:06 -070076
Yabin Cui3939b9d2018-07-20 17:12:13 -070077TEST(DebugElfFileFinder, add_symbol_dir) {
78 DebugElfFileFinder finder;
79 ASSERT_FALSE(finder.AddSymbolDir(GetTestDataDir() + "dir_not_exist"));
80 ASSERT_EQ(finder.FindDebugFile("elf", false, CHECK_ELF_FILE_BUILD_ID), "elf");
Yabin Cui1b9b1c12018-10-29 14:23:48 -070081 std::string symfs_dir = GetTestDataDir() + CORRECT_SYMFS_FOR_BUILD_ID_CHECK;
82 std::replace(symfs_dir.begin(), symfs_dir.end(), '/', OS_PATH_SEPARATOR);
83 ASSERT_TRUE(finder.AddSymbolDir(symfs_dir));
Yabin Cui3939b9d2018-07-20 17:12:13 -070084 ASSERT_EQ(finder.FindDebugFile("elf", false, CHECK_ELF_FILE_BUILD_ID),
Yabin Cui1b9b1c12018-10-29 14:23:48 -070085 symfs_dir + OS_PATH_SEPARATOR + "elf_for_build_id_check");
Yabin Cui3939b9d2018-07-20 17:12:13 -070086}
87
Yabin Cuidd401b32018-04-11 11:17:06 -070088TEST(dso, dex_file_dso) {
89#if defined(__linux__)
90 for (DsoType dso_type : {DSO_DEX_FILE, DSO_ELF_FILE}) {
91 std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, GetTestData("base.vdex"));
92 ASSERT_TRUE(dso);
93 dso->AddDexFileOffset(0x28);
94 ASSERT_EQ(DSO_DEX_FILE, dso->type());
95 const Symbol* symbol = dso->FindSymbol(0x6c77e);
96 ASSERT_NE(symbol, nullptr);
97 ASSERT_EQ(symbol->addr, static_cast<uint64_t>(0x6c77e));
98 ASSERT_EQ(symbol->len, static_cast<uint64_t>(0x16));
99 ASSERT_STREQ(symbol->DemangledName(),
100 "com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run");
Yabin Cuidb2c4932019-02-07 15:06:42 -0800101 uint64_t min_vaddr;
102 uint64_t file_offset_of_min_vaddr;
103 dso->GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr);
104 ASSERT_EQ(min_vaddr, 0);
105 ASSERT_EQ(file_offset_of_min_vaddr, 0);
Yabin Cui15749e02018-05-30 16:37:06 -0700106
107 // Don't crash on not exist zip entry.
108 dso = Dso::CreateDso(dso_type, GetTestData("base.zip!/not_exist_entry"));
109 ASSERT_TRUE(dso);
110 ASSERT_EQ(nullptr, dso->FindSymbol(0));
Yabin Cuidd401b32018-04-11 11:17:06 -0700111 }
112#else
113 GTEST_LOG_(INFO) << "This test only runs on linux because of libdexfile";
114#endif // defined(__linux__)
115}
Yabin Cui8422f342018-05-09 17:27:27 -0700116
Yabin Cuic8571d42018-06-06 11:20:39 -0700117TEST(dso, dex_file_offsets) {
118 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_DEX_FILE, "");
119 ASSERT_TRUE(dso);
120 for (uint64_t offset : {0x3, 0x1, 0x5, 0x4, 0x2, 0x4, 0x3}) {
121 dso->AddDexFileOffset(offset);
122 }
123 ASSERT_EQ(*dso->DexFileOffsets(), std::vector<uint64_t>({0x1, 0x2, 0x3, 0x4, 0x5}));
124}
125
Yabin Cui8422f342018-05-09 17:27:27 -0700126TEST(dso, embedded_elf) {
127 const std::string file_path = GetUrlInApk(GetTestData(APK_FILE), NATIVELIB_IN_APK);
128 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_ELF_FILE, file_path);
129 ASSERT_TRUE(dso);
130 ASSERT_EQ(dso->Path(), file_path);
131 ASSERT_EQ(dso->GetDebugFilePath(), file_path);
Yabin Cuidb2c4932019-02-07 15:06:42 -0800132 uint64_t min_vaddr;
133 uint64_t file_offset_of_min_vaddr;
134 dso->GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr);
135 ASSERT_EQ(min_vaddr, 0);
136 ASSERT_EQ(file_offset_of_min_vaddr, 0);
Yabin Cui8422f342018-05-09 17:27:27 -0700137 const Symbol* symbol = dso->FindSymbol(0x9a4);
138 ASSERT_TRUE(symbol != nullptr);
139 ASSERT_STREQ(symbol->Name(), "Java_com_example_hellojni_HelloJni_callFunc1");
140 BuildId build_id;
141 ASSERT_TRUE(GetBuildIdFromDsoPath(file_path, &build_id));
142 ASSERT_EQ(build_id, native_lib_build_id);
143}
Yabin Cuidb2c4932019-02-07 15:06:42 -0800144
145TEST(dso, IpToVaddrInFile) {
146 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_ELF_FILE, GetTestData("libc.so"));
147 ASSERT_TRUE(dso);
148 ASSERT_EQ(0xa5140, dso->IpToVaddrInFile(0xe9201140, 0xe9201000, 0xa5000));
149}