blob: 175ce1d5018e8d65024176ab1e76126935f5fcdf [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 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()));
52 BuildId build_id(ELF_FILE_BUILD_ID);
53 ASSERT_EQ(finder.FindDebugFile(ELF_FILE, false, build_id), GetTestDataDir() + ELF_FILE);
54 std::string native_lib_in_apk = APK_FILE + "!/" + NATIVELIB_IN_APK;
55 ASSERT_EQ(finder.FindDebugFile(native_lib_in_apk, false, native_lib_build_id),
56 GetTestDataDir() + native_lib_in_apk);
57}
58
59TEST(DebugElfFileFinder, use_vdso) {
60 DebugElfFileFinder finder;
61 std::string fake_vdso32 = "fake_vdso32";
62 std::string fake_vdso64 = "fake_vdso64";
63 finder.SetVdsoFile(fake_vdso32, false);
64 finder.SetVdsoFile(fake_vdso64, true);
65 BuildId build_id;
66 ASSERT_EQ(finder.FindDebugFile("[vdso]", false, build_id), fake_vdso32);
67 ASSERT_EQ(finder.FindDebugFile("[vdso]", true, build_id), fake_vdso64);
68}
Yabin Cuidd401b32018-04-11 11:17:06 -070069
70TEST(dso, dex_file_dso) {
71#if defined(__linux__)
72 for (DsoType dso_type : {DSO_DEX_FILE, DSO_ELF_FILE}) {
73 std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, GetTestData("base.vdex"));
74 ASSERT_TRUE(dso);
75 dso->AddDexFileOffset(0x28);
76 ASSERT_EQ(DSO_DEX_FILE, dso->type());
77 const Symbol* symbol = dso->FindSymbol(0x6c77e);
78 ASSERT_NE(symbol, nullptr);
79 ASSERT_EQ(symbol->addr, static_cast<uint64_t>(0x6c77e));
80 ASSERT_EQ(symbol->len, static_cast<uint64_t>(0x16));
81 ASSERT_STREQ(symbol->DemangledName(),
82 "com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run");
83 ASSERT_EQ(0u, dso->MinVirtualAddress());
84 }
85#else
86 GTEST_LOG_(INFO) << "This test only runs on linux because of libdexfile";
87#endif // defined(__linux__)
88}
Yabin Cui8422f342018-05-09 17:27:27 -070089
90TEST(dso, embedded_elf) {
91 const std::string file_path = GetUrlInApk(GetTestData(APK_FILE), NATIVELIB_IN_APK);
92 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_ELF_FILE, file_path);
93 ASSERT_TRUE(dso);
94 ASSERT_EQ(dso->Path(), file_path);
95 ASSERT_EQ(dso->GetDebugFilePath(), file_path);
96 ASSERT_EQ(dso->MinVirtualAddress(), 0u);
97 const Symbol* symbol = dso->FindSymbol(0x9a4);
98 ASSERT_TRUE(symbol != nullptr);
99 ASSERT_STREQ(symbol->Name(), "Java_com_example_hellojni_HelloJni_callFunc1");
100 BuildId build_id;
101 ASSERT_TRUE(GetBuildIdFromDsoPath(file_path, &build_id));
102 ASSERT_EQ(build_id, native_lib_build_id);
103}