1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:57:45 +00:00

Everywhere: Core dump => Coredump

We all know what a coredump is, and it feels more natural to refer to
it as a coredump (most code already does), so let's be consistent.
This commit is contained in:
Andreas Kling 2021-08-22 14:51:04 +02:00
parent a930877f31
commit bcd2025311
21 changed files with 73 additions and 72 deletions

View file

@ -0,0 +1,140 @@
/*
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <AK/Platform.h>
#include <AK/StringBuilder.h>
#include <AK/Types.h>
#include <LibCore/File.h>
#include <LibCoredump/Backtrace.h>
#include <LibCoredump/Reader.h>
#include <LibELF/Core.h>
#include <LibELF/Image.h>
namespace Coredump {
ELFObjectInfo const* Backtrace::object_info_for_region(ELF::Core::MemoryRegionInfo const& region)
{
auto path = region.object_name();
if (!path.starts_with('/') && path.ends_with(".so"sv))
path = LexicalPath::join("/usr/lib", path).string();
auto maybe_ptr = m_debug_info_cache.get(path);
if (maybe_ptr.has_value())
return *maybe_ptr;
if (!Core::File::exists(path))
return nullptr;
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;
auto image = make<ELF::Image>(file_or_error.value()->bytes());
auto& image_reference = *image;
auto info = make<ELFObjectInfo>(file_or_error.release_value(), make<Debug::DebugInfo>(image_reference), move(image));
auto* info_ptr = info.ptr();
m_debug_info_cache.set(path, move(info));
return info_ptr;
}
Backtrace::Backtrace(const Reader& coredump, const ELF::Core::ThreadInfo& thread_info)
: m_thread_info(move(thread_info))
{
FlatPtr* bp;
FlatPtr* ip;
#if ARCH(I386)
bp = (FlatPtr*)m_thread_info.regs.ebp;
ip = (FlatPtr*)m_thread_info.regs.eip;
#else
bp = (FlatPtr*)m_thread_info.regs.rbp;
ip = (FlatPtr*)m_thread_info.regs.rip;
#endif
bool first_frame = true;
while (bp && ip) {
// We use eip - 1 because the return address from a function frame
// is the instruction that comes after the 'call' instruction.
// However, because the first frame represents the faulting
// instruction rather than the return address we don't subtract
// 1 there.
VERIFY((FlatPtr)ip > 0);
add_entry(coredump, (FlatPtr)ip - (first_frame ? 0 : 1));
first_frame = false;
auto next_ip = coredump.peek_memory((FlatPtr)(bp + 1));
auto next_bp = coredump.peek_memory((FlatPtr)(bp));
if (!next_ip.has_value() || !next_bp.has_value())
break;
ip = (FlatPtr*)next_ip.value();
bp = (FlatPtr*)next_bp.value();
}
}
Backtrace::~Backtrace()
{
}
void Backtrace::add_entry(const Reader& coredump, FlatPtr ip)
{
auto* ip_region = coredump.region_containing((FlatPtr)ip);
if (!ip_region) {
m_entries.append({ ip, {}, {}, {} });
return;
}
auto object_name = ip_region->object_name();
if (object_name == "Loader.so")
return;
// We need to find the first region for the object, just in case
// the PT_LOAD header for the .text segment isn't the first one
// in the object file.
auto region = coredump.first_region_for_object(object_name);
auto* object_info = object_info_for_region(*region);
if (!object_info)
return;
auto function_name = object_info->debug_info->elf().symbolicate(ip - region->region_start);
auto source_position = object_info->debug_info->get_source_position_with_inlines(ip - region->region_start);
m_entries.append({ ip, object_name, function_name, source_position });
}
String Backtrace::Entry::to_string(bool color) const
{
StringBuilder builder;
builder.appendff("{:p}: ", eip);
if (object_name.is_empty()) {
builder.append("???");
return builder.build();
}
builder.appendff("[{}] {}", object_name, function_name.is_empty() ? "???" : function_name);
builder.append(" (");
Vector<Debug::DebugInfo::SourcePosition> source_positions;
for (auto& position : source_position_with_inlines.inline_chain) {
if (!source_positions.contains_slow(position))
source_positions.append(position);
}
if (source_position_with_inlines.source_position.has_value() && !source_positions.contains_slow(source_position_with_inlines.source_position.value())) {
source_positions.insert(0, source_position_with_inlines.source_position.value());
}
for (size_t i = 0; i < source_positions.size(); ++i) {
auto& position = source_positions[i];
auto fmt = color ? "\033[34;1m{}\033[0m:{}" : "{}:{}";
builder.appendff(fmt, LexicalPath::basename(position.file_path), position.line_number);
if (i != source_positions.size() - 1) {
builder.append(" => ");
}
}
builder.append(")");
return builder.build();
}
}

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
#include <LibCoredump/Reader.h>
#include <LibDebug/DebugInfo.h>
#include <LibELF/Core.h>
namespace Coredump {
struct ELFObjectInfo {
ELFObjectInfo(NonnullRefPtr<MappedFile> file, NonnullOwnPtr<Debug::DebugInfo>&& debug_info, NonnullOwnPtr<ELF::Image> image)
: file(move(file))
, debug_info(move(debug_info))
, image(move(image))
{
}
NonnullRefPtr<MappedFile> file;
NonnullOwnPtr<Debug::DebugInfo> debug_info;
NonnullOwnPtr<ELF::Image> image;
};
class Backtrace {
public:
struct Entry {
FlatPtr eip;
String object_name;
String function_name;
Debug::DebugInfo::SourcePositionWithInlines source_position_with_inlines;
String to_string(bool color = false) const;
};
Backtrace(const Reader&, const ELF::Core::ThreadInfo&);
~Backtrace();
const ELF::Core::ThreadInfo thread_info() const { return m_thread_info; }
const Vector<Entry> entries() const { return m_entries; }
private:
void add_entry(const Reader&, FlatPtr ip);
ELFObjectInfo const* object_info_for_region(ELF::Core::MemoryRegionInfo const&);
ELF::Core::ThreadInfo m_thread_info;
Vector<Entry> m_entries;
HashMap<String, NonnullOwnPtr<ELFObjectInfo>> m_debug_info_cache;
};
}

View file

@ -0,0 +1,7 @@
set(SOURCES
Backtrace.cpp
Reader.cpp
)
serenity_lib(LibCoredump Coredump)
target_link_libraries(LibCoredump LibC LibCompress LibCore LibDebug)

View file

@ -0,0 +1,14 @@
/*
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
namespace Coredump {
class Backtrace;
class Reader;
}

View file

@ -0,0 +1,275 @@
/*
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <LibCompress/Gzip.h>
#include <LibCoredump/Reader.h>
#include <signal_numbers.h>
#include <string.h>
namespace Coredump {
OwnPtr<Reader> Reader::create(const String& path)
{
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return {};
return adopt_own(*new Reader(file_or_error.value()->bytes()));
}
Reader::Reader(ReadonlyBytes coredump_bytes)
: m_coredump_buffer(decompress_coredump(coredump_bytes))
, m_coredump_image(m_coredump_buffer.bytes())
{
size_t index = 0;
m_coredump_image.for_each_program_header([this, &index](auto pheader) {
if (pheader.type() == PT_NOTE) {
m_notes_segment_index = index;
return IterationDecision::Break;
}
++index;
return IterationDecision::Continue;
});
VERIFY(m_notes_segment_index != -1);
}
ByteBuffer Reader::decompress_coredump(const ReadonlyBytes& raw_coredump)
{
if (!Compress::GzipDecompressor::is_likely_compressed(raw_coredump))
return ByteBuffer::copy(raw_coredump); // handle old format coredumps (uncompressed)
auto decompressed_coredump = Compress::GzipDecompressor::decompress_all(raw_coredump);
if (!decompressed_coredump.has_value())
return ByteBuffer::copy(raw_coredump); // if we didn't manage to decompress it, try and parse it as decompressed coredump
return decompressed_coredump.value();
}
Reader::~Reader()
{
}
Reader::NotesEntryIterator::NotesEntryIterator(const u8* notes_data)
: m_current((const ELF::Core::NotesEntry*)notes_data)
, start(notes_data)
{
}
ELF::Core::NotesEntryHeader::Type Reader::NotesEntryIterator::type() const
{
VERIFY(m_current->header.type == ELF::Core::NotesEntryHeader::Type::ProcessInfo
|| m_current->header.type == ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo
|| m_current->header.type == ELF::Core::NotesEntryHeader::Type::ThreadInfo
|| m_current->header.type == ELF::Core::NotesEntryHeader::Type::Metadata
|| m_current->header.type == ELF::Core::NotesEntryHeader::Type::Null);
return m_current->header.type;
}
const ELF::Core::NotesEntry* Reader::NotesEntryIterator::current() const
{
return m_current;
}
void Reader::NotesEntryIterator::next()
{
VERIFY(!at_end());
switch (type()) {
case ELF::Core::NotesEntryHeader::Type::ProcessInfo: {
const auto* current = reinterpret_cast<const ELF::Core::ProcessInfo*>(m_current);
m_current = reinterpret_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
break;
}
case ELF::Core::NotesEntryHeader::Type::ThreadInfo: {
const auto* current = reinterpret_cast<const ELF::Core::ThreadInfo*>(m_current);
m_current = reinterpret_cast<const ELF::Core::NotesEntry*>(current + 1);
break;
}
case ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo: {
const auto* current = reinterpret_cast<const ELF::Core::MemoryRegionInfo*>(m_current);
m_current = reinterpret_cast<const ELF::Core::NotesEntry*>(current->region_name + strlen(current->region_name) + 1);
break;
}
case ELF::Core::NotesEntryHeader::Type::Metadata: {
const auto* current = reinterpret_cast<const ELF::Core::Metadata*>(m_current);
m_current = reinterpret_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
break;
}
default:
VERIFY_NOT_REACHED();
}
}
bool Reader::NotesEntryIterator::at_end() const
{
return type() == ELF::Core::NotesEntryHeader::Type::Null;
}
Optional<FlatPtr> Reader::peek_memory(FlatPtr address) const
{
const auto* region = region_containing(address);
if (!region)
return {};
FlatPtr offset_in_region = address - region->region_start;
const char* region_data = image().program_header(region->program_header_index).raw_data();
return *(const FlatPtr*)(&region_data[offset_in_region]);
}
const JsonObject Reader::process_info() const
{
const ELF::Core::ProcessInfo* process_info_notes_entry = nullptr;
for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
if (it.type() != ELF::Core::NotesEntryHeader::Type::ProcessInfo)
continue;
process_info_notes_entry = reinterpret_cast<const ELF::Core::ProcessInfo*>(it.current());
break;
}
if (!process_info_notes_entry)
return {};
auto process_info_json_value = JsonValue::from_string(process_info_notes_entry->json_data);
if (!process_info_json_value.has_value())
return {};
if (!process_info_json_value.value().is_object())
return {};
return process_info_json_value.value().as_object();
// FIXME: Maybe just cache this on the Reader instance after first access.
}
ELF::Core::MemoryRegionInfo const* Reader::first_region_for_object(StringView object_name) const
{
ELF::Core::MemoryRegionInfo const* ret = nullptr;
for_each_memory_region_info([&ret, &object_name](auto& region_info) {
if (region_info.object_name() == object_name) {
ret = &region_info;
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
return ret;
}
const ELF::Core::MemoryRegionInfo* Reader::region_containing(FlatPtr address) const
{
const ELF::Core::MemoryRegionInfo* ret = nullptr;
for_each_memory_region_info([&ret, address](const ELF::Core::MemoryRegionInfo& region_info) {
if (region_info.region_start <= address && region_info.region_end >= address) {
ret = &region_info;
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
return ret;
}
int Reader::process_pid() const
{
auto process_info = this->process_info();
auto pid = process_info.get("pid");
return pid.to_number<int>();
}
u8 Reader::process_termination_signal() const
{
auto process_info = this->process_info();
auto termination_signal = process_info.get("termination_signal");
auto signal_number = termination_signal.to_number<int>();
if (signal_number <= SIGINVAL || signal_number >= NSIG)
return SIGINVAL;
return (u8)signal_number;
}
String Reader::process_executable_path() const
{
auto process_info = this->process_info();
auto executable_path = process_info.get("executable_path");
return executable_path.as_string_or({});
}
Vector<String> Reader::process_arguments() const
{
auto process_info = this->process_info();
auto arguments = process_info.get("arguments");
if (!arguments.is_array())
return {};
Vector<String> vector;
arguments.as_array().for_each([&](auto& value) {
if (value.is_string())
vector.append(value.as_string());
});
return vector;
}
Vector<String> Reader::process_environment() const
{
auto process_info = this->process_info();
auto environment = process_info.get("environment");
if (!environment.is_array())
return {};
Vector<String> vector;
environment.as_array().for_each([&](auto& value) {
if (value.is_string())
vector.append(value.as_string());
});
return vector;
}
HashMap<String, String> Reader::metadata() const
{
const ELF::Core::Metadata* metadata_notes_entry = nullptr;
for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
if (it.type() != ELF::Core::NotesEntryHeader::Type::Metadata)
continue;
metadata_notes_entry = reinterpret_cast<const ELF::Core::Metadata*>(it.current());
break;
}
if (!metadata_notes_entry)
return {};
auto metadata_json_value = JsonValue::from_string(metadata_notes_entry->json_data);
if (!metadata_json_value.has_value())
return {};
if (!metadata_json_value.value().is_object())
return {};
HashMap<String, String> metadata;
metadata_json_value.value().as_object().for_each_member([&](auto& key, auto& value) {
metadata.set(key, value.as_string_or({}));
});
return metadata;
}
struct LibraryData {
String name;
OwnPtr<MappedFile> file;
ELF::Image lib_elf;
};
const Reader::LibraryData* Reader::library_containing(FlatPtr address) const
{
static HashMap<String, OwnPtr<LibraryData>> cached_libs;
auto* region = region_containing(address);
if (!region)
return {};
auto name = region->object_name();
String path;
if (name.contains(".so"))
path = String::formatted("/usr/lib/{}", name);
else {
path = name;
}
if (!cached_libs.contains(path)) {
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return {};
auto image = ELF::Image(file_or_error.value()->bytes());
cached_libs.set(path, make<LibraryData>(name, (FlatPtr)region->region_start, file_or_error.release_value(), move(image)));
}
auto lib_data = cached_libs.get(path).value();
return lib_data;
}
}

View file

@ -0,0 +1,109 @@
/*
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <AK/MappedFile.h>
#include <AK/Noncopyable.h>
#include <AK/OwnPtr.h>
#include <LibELF/Core.h>
#include <LibELF/Image.h>
namespace Coredump {
class Reader {
AK_MAKE_NONCOPYABLE(Reader);
AK_MAKE_NONMOVABLE(Reader);
public:
static OwnPtr<Reader> create(const String&);
~Reader();
template<typename Func>
void for_each_memory_region_info(Func func) const;
template<typename Func>
void for_each_thread_info(Func func) const;
const ELF::Image& image() const { return m_coredump_image; }
Optional<FlatPtr> peek_memory(FlatPtr address) const;
ELF::Core::MemoryRegionInfo const* first_region_for_object(StringView object_name) const;
const ELF::Core::MemoryRegionInfo* region_containing(FlatPtr address) const;
struct LibraryData {
String name;
FlatPtr base_address { 0 };
NonnullRefPtr<MappedFile> file;
ELF::Image lib_elf;
};
const LibraryData* library_containing(FlatPtr address) const;
int process_pid() const;
u8 process_termination_signal() const;
String process_executable_path() const;
Vector<String> process_arguments() const;
Vector<String> process_environment() const;
HashMap<String, String> metadata() const;
private:
Reader(ReadonlyBytes);
static ByteBuffer decompress_coredump(const ReadonlyBytes&);
class NotesEntryIterator {
public:
NotesEntryIterator(const u8* notes_data);
ELF::Core::NotesEntryHeader::Type type() const;
const ELF::Core::NotesEntry* current() const;
void next();
bool at_end() const;
private:
const ELF::Core::NotesEntry* m_current { nullptr };
const u8* start { nullptr };
};
// Private as we don't need anyone poking around in this JsonObject
// manually - we know very well what should be included and expose that
// as getters with the appropriate (non-JsonValue) types.
const JsonObject process_info() const;
ByteBuffer m_coredump_buffer;
ELF::Image m_coredump_image;
ssize_t m_notes_segment_index { -1 };
};
template<typename Func>
void Reader::for_each_memory_region_info(Func func) const
{
for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
if (it.type() != ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo)
continue;
auto& memory_region_info = reinterpret_cast<const ELF::Core::MemoryRegionInfo&>(*it.current());
IterationDecision decision = func(memory_region_info);
if (decision == IterationDecision::Break)
return;
}
}
template<typename Func>
void Reader::for_each_thread_info(Func func) const
{
for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
if (it.type() != ELF::Core::NotesEntryHeader::Type::ThreadInfo)
continue;
auto& thread_info = reinterpret_cast<const ELF::Core::ThreadInfo&>(*it.current());
IterationDecision decision = func(thread_info);
if (decision == IterationDecision::Break)
return;
}
}
}