From b1c9c20eb270e54de95000d8923813c6a44fe467 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Fri, 25 Jan 2019 14:28:13 -0800 Subject: [PATCH] Add GetPrintableBuildID(). The GetBuildID() function returns the raw build id data, so add a function to get the printable hex version of the data. Bug: 120606663 Test: New unit tests pass. Change-Id: Ia5aefc97457efb08bbd30ea96cbb2d47ae59f954 --- libunwindstack/MapInfo.cpp | 14 ++++++++++++++ libunwindstack/include/unwindstack/MapInfo.h | 4 ++++ libunwindstack/tests/MapInfoGetBuildIDTest.cpp | 3 +++ 3 files changed, 21 insertions(+) diff --git a/libunwindstack/MapInfo.cpp b/libunwindstack/MapInfo.cpp index f3199711b..9e6bcd4e3 100644 --- a/libunwindstack/MapInfo.cpp +++ b/libunwindstack/MapInfo.cpp @@ -22,6 +22,8 @@ #include #include +#include + #include #include #include @@ -311,4 +313,16 @@ std::string MapInfo::GetBuildID() { return *reinterpret_cast(id); } +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) { + printable_build_id += android::base::StringPrintf("%02x", c); + } + return printable_build_id; +} + } // namespace unwindstack diff --git a/libunwindstack/include/unwindstack/MapInfo.h b/libunwindstack/include/unwindstack/MapInfo.h index 5143ff1f5..e938986d8 100644 --- a/libunwindstack/include/unwindstack/MapInfo.h +++ b/libunwindstack/include/unwindstack/MapInfo.h @@ -84,8 +84,12 @@ struct MapInfo { bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset); + // Returns the raw build id read from the elf data. std::string GetBuildID(); + // Returns the printable version of the build id (hex dump of raw data). + std::string GetPrintableBuildID(); + private: MapInfo(const MapInfo&) = delete; void operator=(const MapInfo&) = delete; diff --git a/libunwindstack/tests/MapInfoGetBuildIDTest.cpp b/libunwindstack/tests/MapInfoGetBuildIDTest.cpp index 3b89c5961..ea9befcda 100644 --- a/libunwindstack/tests/MapInfoGetBuildIDTest.cpp +++ b/libunwindstack/tests/MapInfoGetBuildIDTest.cpp @@ -67,6 +67,7 @@ TEST_F(MapInfoGetBuildIDTest, no_elf_and_no_valid_elf_in_memory) { MapInfo info(nullptr, 0x1000, 0x2000, 0, PROT_READ, ""); EXPECT_EQ("", info.GetBuildID()); + EXPECT_EQ("", info.GetPrintableBuildID()); } TEST_F(MapInfoGetBuildIDTest, from_elf) { @@ -74,6 +75,7 @@ TEST_F(MapInfoGetBuildIDTest, from_elf) { elf_interface_->FakeSetBuildID("FAKE_BUILD_ID"); EXPECT_EQ("FAKE_BUILD_ID", map_info_->GetBuildID()); + EXPECT_EQ("46414b455f4255494c445f4944", map_info_->GetPrintableBuildID()); } void MapInfoGetBuildIDTest::MultipleThreadTest(std::string expected_build_id) { @@ -172,6 +174,7 @@ TEST_F(MapInfoGetBuildIDTest, from_memory) { InitElfData(tf_->fd); EXPECT_EQ("ELF_BUILDID", map_info_->GetBuildID()); + EXPECT_EQ("454c465f4255494c444944", map_info_->GetPrintableBuildID()); } TEST_F(MapInfoGetBuildIDTest, multiple_thread_elf_exists_in_memory) {