summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Ferris <cferris@google.com>2021-11-19 01:07:44 +0000
committerChristopher Ferris <cferris@google.com>2021-11-19 01:55:42 +0000
commitd452e57da25d144a41634c8f91068e6028365ef8 (patch)
tree85eba0f7e68443519abf52b2bad18c95061b95d2
parent7e9a717d9e7271cf5a1620e4cd0325d8ec77a14f (diff)
downloadunwinding-d452e57da25d144a41634c8f91068e6028365ef8.tar.gz
Add GetPrintableBuildID function to Elf object.
Bug: 197981919 Test: Unit tests pass. Change-Id: I6373920ad09d005ffa93d08c573f865bae3c070e
-rw-r--r--libunwindstack/Elf.cpp19
-rw-r--r--libunwindstack/MapInfo.cpp12
-rw-r--r--libunwindstack/include/unwindstack/Elf.h4
-rw-r--r--libunwindstack/tests/ElfTest.cpp10
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 <string>
#include <utility>
+#include <android-base/stringprintf.h>
+
#include <unwindstack/Elf.h>
#include <unwindstack/ElfInterface.h>
#include <unwindstack/Log.h>
@@ -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 <mutex>
#include <string>
-#include <android-base/stringprintf.h>
-
#include <unwindstack/Elf.h>
#include <unwindstack/MapInfo.h>
#include <unwindstack/Maps.h>
@@ -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