From d452e57da25d144a41634c8f91068e6028365ef8 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Fri, 19 Nov 2021 01:07:44 +0000 Subject: Add GetPrintableBuildID function to Elf object. Bug: 197981919 Test: Unit tests pass. Change-Id: I6373920ad09d005ffa93d08c573f865bae3c070e --- libunwindstack/Elf.cpp | 19 +++++++++++++++++++ libunwindstack/MapInfo.cpp | 12 +----------- libunwindstack/include/unwindstack/Elf.h | 4 ++++ libunwindstack/tests/ElfTest.cpp | 10 ++++++++++ 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/libunwindstack/Elf.cpp b/libunwindstack/Elf.cpp index 7702f1f..53e9b24 100644 --- a/libunwindstack/Elf.cpp +++ b/libunwindstack/Elf.cpp @@ -22,6 +22,8 @@ #include #include +#include + #include #include #include @@ -436,4 +438,21 @@ std::string Elf::GetBuildID(Memory* memory) { return ""; } +std::string Elf::GetPrintableBuildID(std::string& build_id) { + if (build_id.empty()) { + return ""; + } + std::string printable_build_id; + for (const char& c : build_id) { + // Use %hhx to avoid sign extension on abis that have signed chars. + printable_build_id += android::base::StringPrintf("%02hhx", c); + } + return printable_build_id; +} + +std::string Elf::GetPrintableBuildID() { + std::string build_id = GetBuildID(); + return Elf::GetPrintableBuildID(build_id); +} + } // namespace unwindstack diff --git a/libunwindstack/MapInfo.cpp b/libunwindstack/MapInfo.cpp index eb1987f..3d32066 100644 --- a/libunwindstack/MapInfo.cpp +++ b/libunwindstack/MapInfo.cpp @@ -23,8 +23,6 @@ #include #include -#include - #include #include #include @@ -415,15 +413,7 @@ MapInfo::ElfFields& MapInfo::GetElfFields() { std::string MapInfo::GetPrintableBuildID() { std::string raw_build_id = GetBuildID(); - if (raw_build_id.empty()) { - return ""; - } - std::string printable_build_id; - for (const char& c : raw_build_id) { - // Use %hhx to avoid sign extension on abis that have signed chars. - printable_build_id += android::base::StringPrintf("%02hhx", c); - } - return printable_build_id; + return Elf::GetPrintableBuildID(raw_build_id); } } // namespace unwindstack diff --git a/libunwindstack/include/unwindstack/Elf.h b/libunwindstack/include/unwindstack/Elf.h index 01d9af9..328280d 100644 --- a/libunwindstack/include/unwindstack/Elf.h +++ b/libunwindstack/include/unwindstack/Elf.h @@ -68,6 +68,8 @@ class Elf { std::string GetBuildID(); + std::string GetPrintableBuildID(); + int64_t GetLoadBias() { return load_bias_; } bool IsValidPc(uint64_t pc); @@ -109,6 +111,8 @@ class Elf { static bool CacheGet(MapInfo* info); static bool CacheAfterCreateMemory(MapInfo* info); + static std::string GetPrintableBuildID(std::string& build_id); + protected: bool valid_ = false; int64_t load_bias_ = 0; diff --git a/libunwindstack/tests/ElfTest.cpp b/libunwindstack/tests/ElfTest.cpp index 5af02ff..77a94be 100644 --- a/libunwindstack/tests/ElfTest.cpp +++ b/libunwindstack/tests/ElfTest.cpp @@ -551,4 +551,14 @@ TEST_F(ElfTest, error_code_valid) { EXPECT_EQ(0x1000U, elf.GetLastErrorAddress()); } +TEST_F(ElfTest, get_printable_build_id_empty) { + std::string empty; + ASSERT_EQ("", Elf::GetPrintableBuildID(empty)); +} + +TEST_F(ElfTest, get_printable_build_id_check) { + std::string empty = {'\xff', '\x45', '\x40', '\x0f'}; + ASSERT_EQ("ff45400f", Elf::GetPrintableBuildID(empty)); +} + } // namespace unwindstack -- cgit v1.2.3