blob: a9479c8ed14aa3e8be93380477119f9c4faf53c8 [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>
23#include <android-base/test_utils.h>
24
25#include "get_test_data.h"
Yabin Cui8422f342018-05-09 17:27:27 -070026#include "read_apk.h"
Yabin Cui1b9b1c12018-10-29 14:23:48 -070027#include "utils.h"
Yabin Cui40b70ff2018-04-09 14:06:08 -070028
29using namespace simpleperf_dso_impl;
30
31TEST(DebugElfFileFinder, use_build_id_list) {
32 // Create a temp symdir with build_id_list.
33 TemporaryDir tmpdir;
34 TemporaryFile tmpfile(tmpdir.path);
35 std::string data;
36 ASSERT_TRUE(android::base::ReadFileToString(GetTestData(ELF_FILE), &data));
37 ASSERT_TRUE(android::base::WriteStringToFile(data, tmpfile.path));
38 BuildId build_id(ELF_FILE_BUILD_ID);
39 std::string build_id_list = android::base::StringPrintf(
Yabin Cui2969a9e2018-04-19 17:06:24 -070040 "%s=%s\n", build_id.ToString().c_str(), android::base::Basename(tmpfile.path).c_str());
Yabin Cui40b70ff2018-04-09 14:06:08 -070041 std::string build_id_list_file = std::string(tmpdir.path) + "/build_id_list";
42 ASSERT_TRUE(android::base::WriteStringToFile(build_id_list, build_id_list_file));
43
44 DebugElfFileFinder finder;
45 ASSERT_TRUE(finder.SetSymFsDir(tmpdir.path));
46 ASSERT_EQ(finder.FindDebugFile("elf", false, build_id), std::string(tmpfile.path));
47 unlink(build_id_list_file.c_str());
48}
49
50TEST(DebugElfFileFinder, concatenating_symfs_dir) {
51 DebugElfFileFinder finder;
52 ASSERT_TRUE(finder.SetSymFsDir(GetTestDataDir()));
Yabin Cui1b9b1c12018-10-29 14:23:48 -070053 ASSERT_EQ(finder.GetPathInSymFsDir("/system/libc.so"),
54 GetTestDataDir() + "system" + OS_PATH_SEPARATOR + "libc.so");
55 ASSERT_EQ(finder.GetPathInSymFsDir("/data/base.apk!/lib/base.so"),
56 GetTestDataDir() + "data" + OS_PATH_SEPARATOR + "base.apk!/lib/base.so");
57
Yabin Cui40b70ff2018-04-09 14:06:08 -070058 BuildId build_id(ELF_FILE_BUILD_ID);
59 ASSERT_EQ(finder.FindDebugFile(ELF_FILE, false, build_id), GetTestDataDir() + ELF_FILE);
60 std::string native_lib_in_apk = APK_FILE + "!/" + NATIVELIB_IN_APK;
Yabin Cui1b9b1c12018-10-29 14:23:48 -070061 std::string apk_path = APK_FILE;
62 std::replace(apk_path.begin(), apk_path.end(), '/', OS_PATH_SEPARATOR);
Yabin Cui40b70ff2018-04-09 14:06:08 -070063 ASSERT_EQ(finder.FindDebugFile(native_lib_in_apk, false, native_lib_build_id),
Yabin Cui1b9b1c12018-10-29 14:23:48 -070064 GetTestDataDir() + apk_path + "!/" + NATIVELIB_IN_APK);
Yabin Cui40b70ff2018-04-09 14:06:08 -070065}
66
67TEST(DebugElfFileFinder, use_vdso) {
68 DebugElfFileFinder finder;
69 std::string fake_vdso32 = "fake_vdso32";
70 std::string fake_vdso64 = "fake_vdso64";
71 finder.SetVdsoFile(fake_vdso32, false);
72 finder.SetVdsoFile(fake_vdso64, true);
73 BuildId build_id;
74 ASSERT_EQ(finder.FindDebugFile("[vdso]", false, build_id), fake_vdso32);
75 ASSERT_EQ(finder.FindDebugFile("[vdso]", true, build_id), fake_vdso64);
76}
Yabin Cuidd401b32018-04-11 11:17:06 -070077
Yabin Cui3939b9d2018-07-20 17:12:13 -070078TEST(DebugElfFileFinder, add_symbol_dir) {
79 DebugElfFileFinder finder;
80 ASSERT_FALSE(finder.AddSymbolDir(GetTestDataDir() + "dir_not_exist"));
81 ASSERT_EQ(finder.FindDebugFile("elf", false, CHECK_ELF_FILE_BUILD_ID), "elf");
Yabin Cui1b9b1c12018-10-29 14:23:48 -070082 std::string symfs_dir = GetTestDataDir() + CORRECT_SYMFS_FOR_BUILD_ID_CHECK;
83 std::replace(symfs_dir.begin(), symfs_dir.end(), '/', OS_PATH_SEPARATOR);
84 ASSERT_TRUE(finder.AddSymbolDir(symfs_dir));
Yabin Cui3939b9d2018-07-20 17:12:13 -070085 ASSERT_EQ(finder.FindDebugFile("elf", false, CHECK_ELF_FILE_BUILD_ID),
Yabin Cui1b9b1c12018-10-29 14:23:48 -070086 symfs_dir + OS_PATH_SEPARATOR + "elf_for_build_id_check");
Yabin Cui3939b9d2018-07-20 17:12:13 -070087}
88
Yabin Cuidd401b32018-04-11 11:17:06 -070089TEST(dso, dex_file_dso) {
90#if defined(__linux__)
91 for (DsoType dso_type : {DSO_DEX_FILE, DSO_ELF_FILE}) {
92 std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, GetTestData("base.vdex"));
93 ASSERT_TRUE(dso);
94 dso->AddDexFileOffset(0x28);
95 ASSERT_EQ(DSO_DEX_FILE, dso->type());
96 const Symbol* symbol = dso->FindSymbol(0x6c77e);
97 ASSERT_NE(symbol, nullptr);
98 ASSERT_EQ(symbol->addr, static_cast<uint64_t>(0x6c77e));
99 ASSERT_EQ(symbol->len, static_cast<uint64_t>(0x16));
100 ASSERT_STREQ(symbol->DemangledName(),
101 "com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run");
102 ASSERT_EQ(0u, dso->MinVirtualAddress());
Yabin Cui15749e02018-05-30 16:37:06 -0700103
104 // Don't crash on not exist zip entry.
105 dso = Dso::CreateDso(dso_type, GetTestData("base.zip!/not_exist_entry"));
106 ASSERT_TRUE(dso);
107 ASSERT_EQ(nullptr, dso->FindSymbol(0));
Yabin Cuidd401b32018-04-11 11:17:06 -0700108 }
109#else
110 GTEST_LOG_(INFO) << "This test only runs on linux because of libdexfile";
111#endif // defined(__linux__)
112}
Yabin Cui8422f342018-05-09 17:27:27 -0700113
Yabin Cuic8571d42018-06-06 11:20:39 -0700114TEST(dso, dex_file_offsets) {
115 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_DEX_FILE, "");
116 ASSERT_TRUE(dso);
117 for (uint64_t offset : {0x3, 0x1, 0x5, 0x4, 0x2, 0x4, 0x3}) {
118 dso->AddDexFileOffset(offset);
119 }
120 ASSERT_EQ(*dso->DexFileOffsets(), std::vector<uint64_t>({0x1, 0x2, 0x3, 0x4, 0x5}));
121}
122
Yabin Cui8422f342018-05-09 17:27:27 -0700123TEST(dso, embedded_elf) {
124 const std::string file_path = GetUrlInApk(GetTestData(APK_FILE), NATIVELIB_IN_APK);
125 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_ELF_FILE, file_path);
126 ASSERT_TRUE(dso);
127 ASSERT_EQ(dso->Path(), file_path);
128 ASSERT_EQ(dso->GetDebugFilePath(), file_path);
129 ASSERT_EQ(dso->MinVirtualAddress(), 0u);
130 const Symbol* symbol = dso->FindSymbol(0x9a4);
131 ASSERT_TRUE(symbol != nullptr);
132 ASSERT_STREQ(symbol->Name(), "Java_com_example_hellojni_HelloJni_callFunc1");
133 BuildId build_id;
134 ASSERT_TRUE(GetBuildIdFromDsoPath(file_path, &build_id));
135 ASSERT_EQ(build_id, native_lib_build_id);
136}