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> |
| 23 | #include <android-base/test_utils.h> |
| 24 | |
| 25 | #include "get_test_data.h" |
| 26 | |
| 27 | using namespace simpleperf_dso_impl; |
| 28 | |
| 29 | TEST(DebugElfFileFinder, use_build_id_list) { |
| 30 | // Create a temp symdir with build_id_list. |
| 31 | TemporaryDir tmpdir; |
| 32 | TemporaryFile tmpfile(tmpdir.path); |
| 33 | std::string data; |
| 34 | ASSERT_TRUE(android::base::ReadFileToString(GetTestData(ELF_FILE), &data)); |
| 35 | ASSERT_TRUE(android::base::WriteStringToFile(data, tmpfile.path)); |
| 36 | BuildId build_id(ELF_FILE_BUILD_ID); |
| 37 | std::string build_id_list = android::base::StringPrintf( |
Yabin Cui | 2969a9e | 2018-04-19 17:06:24 -0700 | [diff] [blame^] | 38 | "%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] | 39 | std::string build_id_list_file = std::string(tmpdir.path) + "/build_id_list"; |
| 40 | ASSERT_TRUE(android::base::WriteStringToFile(build_id_list, build_id_list_file)); |
| 41 | |
| 42 | DebugElfFileFinder finder; |
| 43 | ASSERT_TRUE(finder.SetSymFsDir(tmpdir.path)); |
| 44 | ASSERT_EQ(finder.FindDebugFile("elf", false, build_id), std::string(tmpfile.path)); |
| 45 | unlink(build_id_list_file.c_str()); |
| 46 | } |
| 47 | |
| 48 | TEST(DebugElfFileFinder, concatenating_symfs_dir) { |
| 49 | DebugElfFileFinder finder; |
| 50 | ASSERT_TRUE(finder.SetSymFsDir(GetTestDataDir())); |
| 51 | BuildId build_id(ELF_FILE_BUILD_ID); |
| 52 | ASSERT_EQ(finder.FindDebugFile(ELF_FILE, false, build_id), GetTestDataDir() + ELF_FILE); |
| 53 | std::string native_lib_in_apk = APK_FILE + "!/" + NATIVELIB_IN_APK; |
| 54 | ASSERT_EQ(finder.FindDebugFile(native_lib_in_apk, false, native_lib_build_id), |
| 55 | GetTestDataDir() + native_lib_in_apk); |
| 56 | } |
| 57 | |
| 58 | TEST(DebugElfFileFinder, use_vdso) { |
| 59 | DebugElfFileFinder finder; |
| 60 | std::string fake_vdso32 = "fake_vdso32"; |
| 61 | std::string fake_vdso64 = "fake_vdso64"; |
| 62 | finder.SetVdsoFile(fake_vdso32, false); |
| 63 | finder.SetVdsoFile(fake_vdso64, true); |
| 64 | BuildId build_id; |
| 65 | ASSERT_EQ(finder.FindDebugFile("[vdso]", false, build_id), fake_vdso32); |
| 66 | ASSERT_EQ(finder.FindDebugFile("[vdso]", true, build_id), fake_vdso64); |
| 67 | } |
Yabin Cui | dd401b3 | 2018-04-11 11:17:06 -0700 | [diff] [blame] | 68 | |
| 69 | TEST(dso, dex_file_dso) { |
| 70 | #if defined(__linux__) |
| 71 | for (DsoType dso_type : {DSO_DEX_FILE, DSO_ELF_FILE}) { |
| 72 | std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, GetTestData("base.vdex")); |
| 73 | ASSERT_TRUE(dso); |
| 74 | dso->AddDexFileOffset(0x28); |
| 75 | ASSERT_EQ(DSO_DEX_FILE, dso->type()); |
| 76 | const Symbol* symbol = dso->FindSymbol(0x6c77e); |
| 77 | ASSERT_NE(symbol, nullptr); |
| 78 | ASSERT_EQ(symbol->addr, static_cast<uint64_t>(0x6c77e)); |
| 79 | ASSERT_EQ(symbol->len, static_cast<uint64_t>(0x16)); |
| 80 | ASSERT_STREQ(symbol->DemangledName(), |
| 81 | "com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run"); |
| 82 | ASSERT_EQ(0u, dso->MinVirtualAddress()); |
| 83 | } |
| 84 | #else |
| 85 | GTEST_LOG_(INFO) << "This test only runs on linux because of libdexfile"; |
| 86 | #endif // defined(__linux__) |
| 87 | } |