From 6a296b61e8ed81eedad6736cbfa2f1710511c671 Mon Sep 17 00:00:00 2001 From: David Srbecky Date: Thu, 15 Apr 2021 20:52:22 +0100 Subject: Adjust to libdexfile API changes Test: simpleperf_unit_test --gtest_filter=*dex* Change-Id: I837c1e1669bd7f602da6d86051b2fb666044b1ac --- simpleperf/dso.cpp | 5 ++-- simpleperf/read_dex_file.cpp | 50 ++++++++++++++++++++------------------- simpleperf/read_dex_file.h | 12 +++------- simpleperf/read_dex_file_test.cpp | 3 +-- 4 files changed, 32 insertions(+), 38 deletions(-) (limited to 'simpleperf') diff --git a/simpleperf/dso.cpp b/simpleperf/dso.cpp index 76751d20..73e21ef8 100644 --- a/simpleperf/dso.cpp +++ b/simpleperf/dso.cpp @@ -469,9 +469,8 @@ class DexFileDso : public Dso { std::vector symbols; auto tuple = SplitUrlInApk(debug_file_path_); bool status = false; - auto symbol_callback = [&](DexFileSymbol* dex_symbol) { - symbols.emplace_back(std::string_view(dex_symbol->name, dex_symbol->name_size), - dex_symbol->addr, dex_symbol->size); + auto symbol_callback = [&](DexFileSymbol* symbol) { + symbols.emplace_back(symbol->name, symbol->addr, symbol->size); }; if (std::get<0>(tuple)) { std::unique_ptr ahelper = ArchiveHelper::CreateInstance(std::get<1>(tuple)); diff --git a/simpleperf/read_dex_file.cpp b/simpleperf/read_dex_file.cpp index d4d50e07..13ca403f 100644 --- a/simpleperf/read_dex_file.cpp +++ b/simpleperf/read_dex_file.cpp @@ -25,26 +25,32 @@ #include #include +#include #include #include +#include "utils.h" + namespace simpleperf { static bool ReadSymbols( const std::vector& dex_file_offsets, const std::function(uint64_t offset)>& open_file_cb, const std::function& symbol_cb) { - for (uint64_t offset : dex_file_offsets) { - std::unique_ptr dex_file = open_file_cb(offset); + for (uint64_t dex_offset : dex_file_offsets) { + std::unique_ptr dex_file = open_file_cb(dex_offset); if (dex_file == nullptr) { return false; } - auto callback = [&](DexFileSymbol* symbol) { - symbol->addr += offset; - symbol_cb(symbol); + auto callback = [&](const art_api::dex::DexFile::Method& method) { + size_t name_size, code_size; + const char* name = method.GetQualifiedName(/*with_params=*/false, &name_size); + size_t offset = method.GetCodeOffset(&code_size); + DexFileSymbol symbol{std::string_view(name, name_size), dex_offset + offset, code_size}; + symbol_cb(&symbol); }; - dex_file->GetAllMethodInfos(callback); + dex_file->ForEachMethod(callback); } return true; @@ -61,11 +67,11 @@ bool ReadSymbolsFromDexFileInMemory(void* addr, uint64_t size, return nullptr; } uint8_t* file_addr = static_cast(addr) + offset; - std::string error_msg; - std::unique_ptr dex_file = - art_api::dex::DexFile::OpenFromMemory(file_addr, &max_file_size, "", &error_msg); + std::unique_ptr dex_file; + art_api::dex::DexFile::Error error_msg = + art_api::dex::DexFile::Create(file_addr, max_file_size, nullptr, "", &dex_file); if (dex_file == nullptr) { - LOG(WARNING) << "Failed to read dex file symbols: " << error_msg; + LOG(WARNING) << "Failed to read dex file symbols: " << error_msg.ToString(); return nullptr; } return dex_file; @@ -80,20 +86,16 @@ bool ReadSymbolsFromDexFile(const std::string& file_path, if (fd == -1) { return false; } - return ReadSymbols( - dex_file_offsets, - [&](uint64_t offset) -> std::unique_ptr { - std::string error_msg; - std::unique_ptr dex_file = - art_api::dex::DexFile::OpenFromFd(fd, offset, file_path, &error_msg); - if (dex_file == nullptr) { - LOG(WARNING) << "Failed to read dex file symbols from '" << file_path - << "': " << error_msg; - return nullptr; - } - return dex_file; - }, - symbol_callback); + size_t file_size = GetFileSize(file_path); + if (file_size == 0) { + return false; + } + std::unique_ptr map; + map = android::base::MappedFile::FromFd(fd, 0, file_size, PROT_READ); + if (map == nullptr) { + return false; + } + return ReadSymbolsFromDexFileInMemory(map->data(), file_size, dex_file_offsets, symbol_callback); } } // namespace simpleperf diff --git a/simpleperf/read_dex_file.h b/simpleperf/read_dex_file.h index e5b97904..b4298fab 100644 --- a/simpleperf/read_dex_file.h +++ b/simpleperf/read_dex_file.h @@ -28,17 +28,11 @@ namespace simpleperf { -#ifdef NO_LIBDEXFILE_SUPPORT struct DexFileSymbol { - size_t sizeof_struct; - uint32_t addr; - uint32_t size; - const char* name; - size_t name_size; + std::string_view name; + uint64_t addr; + uint64_t size; }; -#else -using DexFileSymbol = ExtDexFileMethodInfo; -#endif bool ReadSymbolsFromDexFileInMemory(void* addr, uint64_t size, const std::vector& dex_file_offsets, diff --git a/simpleperf/read_dex_file_test.cpp b/simpleperf/read_dex_file_test.cpp index 5d71bc35..843a964c 100644 --- a/simpleperf/read_dex_file_test.cpp +++ b/simpleperf/read_dex_file_test.cpp @@ -30,8 +30,7 @@ using namespace simpleperf; TEST(read_dex_file, smoke) { std::vector symbols; auto symbol_callback = [&](DexFileSymbol* symbol) { - symbols.emplace_back(std::string_view(symbol->name, symbol->name_size), symbol->addr, - symbol->size); + symbols.emplace_back(symbol->name, symbol->addr, symbol->size); }; ASSERT_TRUE(ReadSymbolsFromDexFile(GetTestData("base.vdex"), {0x28}, symbol_callback)); ASSERT_EQ(12435u, symbols.size()); -- cgit v1.2.3