From da750a79c9fe503d52feb7af2c417b19f763649c Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Mon, 30 Nov 2015 13:36:08 -0800 Subject: [PATCH] Change the way some maps are printed. Before, an anonymous map wound up printing the pc as relative. Unfortunately, this meant that it was impossible to tell the actual pc. The new code prints the map name as and still prints the pc as relative. In addition, add the start of the map for map names that begin with a '[' character. Bug: 25844836 Change-Id: Ie0b6149dde258fe13f0e5a3e5739d85374512f4b --- libbacktrace/Backtrace.cpp | 22 ++++++++++++++++------ libbacktrace/backtrace_test.cpp | 21 +++++++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/libbacktrace/Backtrace.cpp b/libbacktrace/Backtrace.cpp index 9ead452cf..555e8cf20 100644 --- a/libbacktrace/Backtrace.cpp +++ b/libbacktrace/Backtrace.cpp @@ -93,16 +93,26 @@ std::string Backtrace::FormatFrameData(size_t frame_num) { } std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) { - const char* map_name; - if (BacktraceMap::IsValid(frame->map) && !frame->map.name.empty()) { - map_name = frame->map.name.c_str(); + uintptr_t relative_pc; + std::string map_name; + if (BacktraceMap::IsValid(frame->map)) { + relative_pc = BacktraceMap::GetRelativePc(frame->map, frame->pc); + if (!frame->map.name.empty()) { + map_name = frame->map.name.c_str(); + if (map_name[0] == '[' && map_name[map_name.size() - 1] == ']') { + map_name.resize(map_name.size() - 1); + map_name += StringPrintf(":%" PRIPTR "]", frame->map.start); + } + } else { + map_name = StringPrintf("", frame->map.start); + } } else { map_name = ""; + relative_pc = frame->pc; } - uintptr_t relative_pc = BacktraceMap::GetRelativePc(frame->map, frame->pc); - - std::string line(StringPrintf("#%02zu pc %" PRIPTR " %s", frame->num, relative_pc, map_name)); + std::string line(StringPrintf("#%02zu pc %" PRIPTR " ", frame->num, relative_pc)); + line += map_name; // Special handling for non-zero offset maps, we need to print that // information. if (frame->map.offset != 0) { diff --git a/libbacktrace/backtrace_test.cpp b/libbacktrace/backtrace_test.cpp index 9ebd63913..ce04817db 100644 --- a/libbacktrace/backtrace_test.cpp +++ b/libbacktrace/backtrace_test.cpp @@ -775,16 +775,29 @@ TEST(libbacktrace, format_test) { backtrace->FormatFrameData(&frame)); // Check map name empty, but exists. - frame.map.start = 1; - frame.map.end = 1; + frame.pc = 0xb0020; + frame.map.start = 0xb0000; + frame.map.end = 0xbffff; frame.map.load_base = 0; #if defined(__LP64__) - EXPECT_EQ("#01 pc 0000000000000001 ", + EXPECT_EQ("#01 pc 0000000000000020 ", #else - EXPECT_EQ("#01 pc 00000001 ", + EXPECT_EQ("#01 pc 00000020 ", #endif backtrace->FormatFrameData(&frame)); + // Check map name begins with a [. + frame.pc = 0xc0020; + frame.map.start = 0xc0000; + frame.map.end = 0xcffff; + frame.map.load_base = 0; + frame.map.name = "[anon:thread signal stack]"; +#if defined(__LP64__) + EXPECT_EQ("#01 pc 0000000000000020 [anon:thread signal stack:00000000000c0000]", +#else + EXPECT_EQ("#01 pc 00000020 [anon:thread signal stack:000c0000]", +#endif + backtrace->FormatFrameData(&frame)); // Check relative pc is set and map name is set. frame.pc = 0x12345679;