Merge "Add GetPrintableBuildID function to Elf object."
diff --git a/libunwindstack/Elf.cpp b/libunwindstack/Elf.cpp
index 43b7762..44db356 100644
--- a/libunwindstack/Elf.cpp
+++ b/libunwindstack/Elf.cpp
@@ -24,6 +24,8 @@
 #include <string>
 #include <utility>
 
+#include <android-base/stringprintf.h>
+
 #include <unwindstack/Elf.h>
 #include <unwindstack/ElfInterface.h>
 #include <unwindstack/Log.h>
@@ -433,4 +435,21 @@
   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 f9cddf0..e7c8dda 100644
--- a/libunwindstack/MapInfo.cpp
+++ b/libunwindstack/MapInfo.cpp
@@ -23,8 +23,6 @@
 #include <mutex>
 #include <string>
 
-#include <android-base/stringprintf.h>
-
 #include <unwindstack/Elf.h>
 #include <unwindstack/MapInfo.h>
 #include <unwindstack/Maps.h>
@@ -417,15 +415,7 @@
 
 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 b8f32e9..73ffda9 100644
--- a/libunwindstack/include/unwindstack/Elf.h
+++ b/libunwindstack/include/unwindstack/Elf.h
@@ -68,6 +68,8 @@
 
   std::string GetBuildID();
 
+  std::string GetPrintableBuildID();
+
   int64_t GetLoadBias() { return load_bias_; }
 
   bool IsValidPc(uint64_t pc);
@@ -113,6 +115,8 @@
   static void CacheAdd(MapInfo* info);
   static bool CacheGet(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 @@
   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