Commit c2e5f30c authored by Kevin Marshall's avatar Kevin Marshall Committed by Commit Bot

[Fuchsia] Output new-style Fuchsia stack traces.

Migrate base::debug::StackTrace to emit stack traces in the new
format documented at
https://fuchsia.googlesource.com/zircon/+/master/docs/symbolizer_markup.md

Bug: 777252
Change-Id: If42d62af4d358efc3ba61af2b6118026eec9a15f
Reviewed-on: https://chromium-review.googlesource.com/c/1474678
Commit-Queue: Kevin Marshall <kmarshall@chromium.org>
Reviewed-by: default avatarWez <wez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636114}
parent b2c2f313
......@@ -4,9 +4,9 @@
#include "base/debug/stack_trace.h"
#include <elf.h>
#include <link.h>
#include <stddef.h>
#include <string.h>
#include <threads.h>
#include <unwind.h>
#include <zircon/process.h>
......@@ -21,7 +21,10 @@
#include <iostream>
#include <type_traits>
#include "base/atomic_sequence_num.h"
#include "base/debug/elf_reader.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/stl_util.h"
namespace base {
......@@ -30,7 +33,7 @@ namespace debug {
namespace {
const char kProcessNamePrefix[] = "app:";
const size_t kProcessNamePrefixLen = base::size(kProcessNamePrefix) - 1;
const size_t kProcessNamePrefixLength = base::size(kProcessNamePrefix) - 1;
struct BacktraceData {
void** trace_array;
......@@ -49,20 +52,53 @@ _Unwind_Reason_Code UnwindStore(struct _Unwind_Context* context,
return _URC_NO_REASON;
}
// Build a "rwx" C string-based representation of the permission bits.
// The output buffer is reused across calls, and should not be retained across
// consecutive invocations of this function.
const char* PermissionFlagsToString(int flags, char permission_buf[4]) {
char* permission = permission_buf;
if (flags & PF_R)
(*permission++) = 'r';
if (flags & PF_W)
(*permission++) = 'w';
if (flags & PF_X)
(*permission++) = 'x';
*permission = '\0';
return permission_buf;
}
// Stores and queries debugging symbol map info for the current process.
class SymbolMap {
public:
struct Entry {
void* addr;
char name[ZX_MAX_NAME_LEN + kProcessNamePrefixLen];
struct Segment {
const void* addr = nullptr;
size_t relative_addr = 0;
int permission_flags = 0;
size_t size = 0;
};
struct Module {
// Maximum number of PT_LOAD segments to process per ELF binary. Most
// binaries have only 2-3 such segments.
static constexpr size_t kMaxSegmentCount = 8;
const void* addr = nullptr;
std::array<Segment, kMaxSegmentCount> segments;
size_t segment_count = 0;
char name[ZX_MAX_NAME_LEN + kProcessNamePrefixLength + 1] = {0};
char build_id[kMaxBuildIdStringLength + 1] = {0};
};
SymbolMap();
~SymbolMap() = default;
// Gets the symbol map entry for |address|. Returns null if no entry could be
// found for the address, or if the symbol map could not be queried.
Entry* GetForAddress(void* address);
// Gets all entries for the symbol map.
span<Module> GetModules() { return {modules_.data(), count_}; }
private:
// Component builds of Chrome pull about 250 shared libraries (on Linux), so
......@@ -72,7 +108,7 @@ class SymbolMap {
void Populate();
// Sorted in descending order by address, for lookup purposes.
std::array<Entry, kMaxMapEntries> entries_;
std::array<Module, kMaxMapEntries> modules_;
size_t count_ = 0;
bool valid_ = false;
......@@ -84,21 +120,6 @@ SymbolMap::SymbolMap() {
Populate();
}
SymbolMap::Entry* SymbolMap::GetForAddress(void* address) {
if (!valid_) {
return nullptr;
}
// Working backwards in the address space, return the first map entry whose
// address comes before |address| (thereby enclosing it.)
for (size_t i = 0; i < count_; ++i) {
if (address >= entries_[i].addr) {
return &entries_[i];
}
}
return nullptr;
}
void SymbolMap::Populate() {
zx_handle_t process = zx_process_self();
......@@ -107,11 +128,11 @@ void SymbolMap::Populate() {
// TODO(wez): Object names can only have up to ZX_MAX_NAME_LEN characters, so
// if we keep hitting problems with truncation, find a way to plumb argv[0]
// through to here instead, e.g. using CommandLine::GetProgramName().
char app_name[std::extent<decltype(SymbolMap::Entry::name)>()];
strcpy(app_name, kProcessNamePrefix);
char app_name[std::extent<decltype(SymbolMap::Module::name)>()];
strncpy(app_name, kProcessNamePrefix, sizeof(kProcessNamePrefix));
zx_status_t status = zx_object_get_property(
process, ZX_PROP_NAME, app_name + kProcessNamePrefixLen,
sizeof(app_name) - kProcessNamePrefixLen);
process, ZX_PROP_NAME, app_name + kProcessNamePrefixLength,
sizeof(app_name) - kProcessNamePrefixLength);
if (status != ZX_OK) {
DPLOG(WARNING)
<< "Couldn't get name, falling back to 'app' for program name: "
......@@ -136,24 +157,68 @@ void SymbolMap::Populate() {
return;
}
// Copy the contents of the link map linked list to |entries_|.
// Populate ELF binary metadata into |modules_|.
while (lmap != nullptr) {
if (count_ >= entries_.size()) {
if (count_ >= kMaxMapEntries)
break;
SymbolMap::Module& next_entry = modules_[count_];
++count_;
next_entry.addr = reinterpret_cast<void*>(lmap->l_addr);
// Create Segment sub-entries for all PT_LOAD headers.
// Each Segment corresponds to a "mmap" line in the output.
next_entry.segment_count = 0;
for (const Elf64_Phdr& phdr : GetElfProgramHeaders(next_entry.addr)) {
if (phdr.p_type != PT_LOAD)
continue;
if (next_entry.segment_count > Module::kMaxSegmentCount) {
LOG(WARNING) << "Exceeded the maximum number of segments.";
break;
}
Segment segment;
segment.addr =
reinterpret_cast<const char*>(next_entry.addr) + phdr.p_vaddr;
segment.relative_addr = phdr.p_vaddr;
segment.size = phdr.p_memsz;
segment.permission_flags = phdr.p_flags;
next_entry.segments[next_entry.segment_count] = std::move(segment);
++next_entry.segment_count;
}
// Get the human-readable library name from the ELF header, falling back on
// using names from the link map for binaries that aren't shared libraries.
Optional<StringPiece> elf_library_name =
ReadElfLibraryName(next_entry.addr);
if (elf_library_name) {
strlcpy(next_entry.name, elf_library_name->data(),
elf_library_name->size() + 1);
} else {
StringPiece link_map_name(lmap->l_name[0] ? lmap->l_name : app_name);
// The "module" stack trace annotation doesn't allow for strings which
// resemble paths, so extract the filename portion from |link_map_name|.
size_t directory_prefix_idx = link_map_name.find_last_of("/");
if (directory_prefix_idx != StringPiece::npos) {
link_map_name = link_map_name.substr(
directory_prefix_idx + 1,
link_map_name.size() - directory_prefix_idx - 1);
}
strlcpy(next_entry.name, link_map_name.data(), link_map_name.size() + 1);
}
if (!ReadElfBuildId(next_entry.addr, false, next_entry.build_id)) {
LOG(WARNING) << "Couldn't read build ID.";
continue;
}
SymbolMap::Entry* next_entry = &entries_[count_];
count_++;
next_entry->addr = reinterpret_cast<void*>(lmap->l_addr);
char* name_to_use = lmap->l_name[0] ? lmap->l_name : app_name;
strlcpy(next_entry->name, name_to_use, sizeof(next_entry->name));
lmap = lmap->l_next;
}
std::sort(
entries_.begin(), entries_.begin() + count_,
[](const Entry& a, const Entry& b) -> bool { return a.addr > b.addr; });
valid_ = true;
}
......@@ -180,39 +245,35 @@ void StackTrace::PrintWithPrefix(const char* prefix_string) const {
OutputToStreamWithPrefix(&std::cerr, prefix_string);
}
// Sample stack trace output is designed to be similar to Fuchsia's crashlogger:
// bt#00: pc 0x1527a058aa00 (app:/system/base_unittests,0x18bda00)
// bt#01: pc 0x1527a0254b5c (app:/system/base_unittests,0x1587b5c)
// bt#02: pc 0x15279f446ece (app:/system/base_unittests,0x779ece)
// ...
// bt#21: pc 0x1527a05b51b4 (app:/system/base_unittests,0x18e81b4)
// bt#22: pc 0x54fdbf3593de (libc.so,0x1c3de)
// bt#23: end
// Emits stack trace data using the symbolizer markup format specified at:
// https://fuchsia.googlesource.com/zircon/+/master/docs/symbolizer_markup.md
void StackTrace::OutputToStreamWithPrefix(std::ostream* os,
const char* prefix_string) const {
SymbolMap map;
size_t i = 0;
for (; (i < count_) && os->good(); ++i) {
SymbolMap::Entry* entry = map.GetForAddress(trace_[i]);
if (prefix_string)
*os << prefix_string;
if (entry) {
size_t offset = reinterpret_cast<uintptr_t>(trace_[i]) -
reinterpret_cast<uintptr_t>(entry->addr);
*os << "bt#" << std::setw(2) << std::setfill('0') << i << std::setw(0)
<< ": pc " << trace_[i] << " (" << entry->name << ",0x" << std::hex
<< offset << std::dec << std::setw(0) << ")\n";
} else {
// Fallback if the DSO map isn't available.
// Logged PC values are absolute memory addresses, and the shared object
// name is not emitted.
*os << "bt#" << std::setw(2) << std::setfill('0') << i << std::setw(0)
<< ": pc " << trace_[i] << "\n";
int module_id = 0;
*os << "{{{reset}}}\n";
for (const SymbolMap::Module& entry : map.GetModules()) {
*os << "{{{module:" << module_id << ":" << entry.name
<< ":elf:" << entry.build_id << "}}}\n";
for (size_t i = 0; i < entry.segment_count; ++i) {
const SymbolMap::Segment& segment = entry.segments[i];
char permission_string[4] = {};
*os << "{{{mmap:" << segment.addr << ":0x" << std::hex << segment.size
<< std::dec << ":load:" << module_id << ":"
<< PermissionFlagsToString(segment.permission_flags,
permission_string)
<< ":"
<< "0x" << std::hex << segment.relative_addr << std::dec << "}}}\n";
}
++module_id;
}
(*os) << "bt#" << std::setw(2) << i << ": end\n";
for (size_t i = 0; i < count_; ++i)
*os << "{{{bt:" << i << ":" << trace_[i] << "}}}\n";
}
} // namespace debug
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment