Try to demangle c++ symbols when logging the stacktrace

PiperOrigin-RevId: 243612828
Change-Id: I09c748da0c119ba2024b2906802858b5b9bcfeb0
This commit is contained in:
Kevin Hamacher 2019-04-15 07:37:03 -07:00 committed by Copybara-Service
parent d90b2c6328
commit af44845246
4 changed files with 18 additions and 7 deletions

View File

@ -168,7 +168,6 @@ cc_library(
deps = [ deps = [
":bpfdisassembler", ":bpfdisassembler",
":comms", ":comms",
":fs",
":namespace", ":namespace",
":regs", ":regs",
":syscall", ":syscall",
@ -271,7 +270,6 @@ cc_library(
":violation_proto_cc", ":violation_proto_cc",
":forkserver", ":forkserver",
":forkserver_proto_cc", ":forkserver_proto_cc",
":fs",
":global_forkserver", ":global_forkserver",
":ipc", ":ipc",
":limits", ":limits",
@ -281,7 +279,6 @@ cc_library(
":notify", ":notify",
":policy", ":policy",
":regs", ":regs",
":reporter",
":result", ":result",
":syscall", ":syscall",
":util", ":util",

View File

@ -92,6 +92,8 @@ void SymbolizationWorksCommon(
ASSERT_THAT(result.final_status(), Eq(Result::SIGNALED)); ASSERT_THAT(result.final_status(), Eq(Result::SIGNALED));
ASSERT_THAT(result.GetStackTrace(), HasSubstr("CrashMe")); ASSERT_THAT(result.GetStackTrace(), HasSubstr("CrashMe"));
// Check that demangling works as well.
ASSERT_THAT(result.GetStackTrace(), HasSubstr("CrashMe()"));
} }
TEST(StackTraceTest, SymbolizationWorksNonSandboxedLibunwind) { TEST(StackTraceTest, SymbolizationWorksNonSandboxedLibunwind) {

View File

@ -14,6 +14,8 @@
#include "sandboxed_api/sandbox2/unwind/unwind.h" #include "sandboxed_api/sandbox2/unwind/unwind.h"
#include <cxxabi.h>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
@ -34,6 +36,16 @@
namespace sandbox2 { namespace sandbox2 {
namespace { namespace {
std::string DemangleSymbol(const std::string& maybe_mangled) {
int status;
std::unique_ptr<char, std::function<void(char*)>> symbol = {
abi::__cxa_demangle(maybe_mangled.c_str(), nullptr, nullptr, &status),
free};
if (symbol && status == 0) {
return symbol.get();
}
return maybe_mangled;
}
std::string GetSymbolAt(const std::map<uint64_t, std::string>& addr_to_symbol, std::string GetSymbolAt(const std::map<uint64_t, std::string>& addr_to_symbol,
uint64_t addr) { uint64_t addr) {
auto entry_for_next_symbol = addr_to_symbol.lower_bound(addr); auto entry_for_next_symbol = addr_to_symbol.lower_bound(addr);
@ -41,13 +53,14 @@ std::string GetSymbolAt(const std::map<uint64_t, std::string>& addr_to_symbol,
entry_for_next_symbol != addr_to_symbol.begin()) { entry_for_next_symbol != addr_to_symbol.begin()) {
// Matches the addr exactly: // Matches the addr exactly:
if (entry_for_next_symbol->first == addr) { if (entry_for_next_symbol->first == addr) {
return entry_for_next_symbol->second; return DemangleSymbol(entry_for_next_symbol->second);
} }
// Might be inside a function, return symbol+offset; // Might be inside a function, return symbol+offset;
const auto entry_for_previous_symbol = --entry_for_next_symbol; const auto entry_for_previous_symbol = --entry_for_next_symbol;
if (!entry_for_previous_symbol->second.empty()) { if (!entry_for_previous_symbol->second.empty()) {
return absl::StrCat(entry_for_previous_symbol->second, "+0x", return absl::StrCat(DemangleSymbol(entry_for_previous_symbol->second),
"+0x",
absl::Hex(addr - entry_for_previous_symbol->first)); absl::Hex(addr - entry_for_previous_symbol->first));
} }
} }
@ -166,7 +179,7 @@ void RunLibUnwindAndSymbolizer(pid_t pid, std::string* stack_trace_out,
addr_to_symbol[entry.end] = ""; addr_to_symbol[entry.end] = "";
} }
if (!entry.path.empty() && entry.is_executable) { if (!entry.path.empty() && entry.path != "[vdso]" && entry.is_executable) {
auto elf_or = ElfFile::ParseFromFile(entry.path, ElfFile::kLoadSymbols); auto elf_or = ElfFile::ParseFromFile(entry.path, ElfFile::kLoadSymbols);
if (!elf_or.ok()) { if (!elf_or.ok()) {
SAPI_RAW_LOG(WARNING, "Could not load symbols for %s: %s", entry.path, SAPI_RAW_LOG(WARNING, "Could not load symbols for %s: %s", entry.path,

View File

@ -76,7 +76,6 @@ cc_test(
name = "status_test", name = "status_test",
srcs = ["status_test.cc"], srcs = ["status_test.cc"],
deps = [ deps = [
":status",
":status_matchers", ":status_matchers",
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest_main",
], ],