diff options
author | Itamar <itamar8910@gmail.com> | 2020-11-11 22:05:49 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-12-14 23:05:53 +0100 |
commit | a74dad14a830e2eed4c49df7576bab28c15f7958 (patch) | |
tree | 14f73369de8b244792ffbcafbf5317bd9f08caff | |
parent | 50219429fdf134ce01c9beeba7031278c3251c0b (diff) | |
download | serenity-a74dad14a830e2eed4c49df7576bab28c15f7958.zip |
LibCoreDump: Add library for parsing coredump files
-rw-r--r-- | Libraries/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Libraries/LibCoreDump/CMakeLists.txt | 6 | ||||
-rw-r--r-- | Libraries/LibCoreDump/CoreDumpReader.cpp | 119 | ||||
-rw-r--r-- | Libraries/LibCoreDump/CoreDumpReader.h | 100 |
4 files changed, 226 insertions, 0 deletions
diff --git a/Libraries/CMakeLists.txt b/Libraries/CMakeLists.txt index 1d205d3cc3..46f6513bd9 100644 --- a/Libraries/CMakeLists.txt +++ b/Libraries/CMakeLists.txt @@ -3,6 +3,7 @@ add_subdirectory(LibC) add_subdirectory(LibChess) add_subdirectory(LibCompress) add_subdirectory(LibCore) +add_subdirectory(LibCoreDump) add_subdirectory(LibCpp) add_subdirectory(LibCrypt) add_subdirectory(LibCrypto) diff --git a/Libraries/LibCoreDump/CMakeLists.txt b/Libraries/LibCoreDump/CMakeLists.txt new file mode 100644 index 0000000000..dd85d22d36 --- /dev/null +++ b/Libraries/LibCoreDump/CMakeLists.txt @@ -0,0 +1,6 @@ +set(SOURCES + CoreDumpReader.cpp +) + +serenity_lib(LibCoreDump coredump) +target_link_libraries(LibCoreDump LibC LibCore) diff --git a/Libraries/LibCoreDump/CoreDumpReader.cpp b/Libraries/LibCoreDump/CoreDumpReader.cpp new file mode 100644 index 0000000000..d4890698fe --- /dev/null +++ b/Libraries/LibCoreDump/CoreDumpReader.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "CoreDumpReader.h" +#include <string.h> + +OwnPtr<CoreDumpReader> CoreDumpReader::create(const String& path) +{ + auto mapped_file = make<MappedFile>(path); + if (!mapped_file->is_valid()) + return nullptr; + return make<CoreDumpReader>(move(mapped_file)); +} + +CoreDumpReader::CoreDumpReader(OwnPtr<MappedFile>&& coredump_file) + : m_coredump_file(move(coredump_file)) + , m_coredump_image((u8*)m_coredump_file->data(), m_coredump_file->size()) +{ + 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; + }); + ASSERT(m_notes_segment_index != -1); +} + +CoreDumpReader::~CoreDumpReader() +{ +} + +CoreDumpReader::NotesEntryIterator::NotesEntryIterator(const u8* notes_data) + : m_current((const ELF::Core::NotesEntry*)notes_data) + , start(notes_data) +{ +} + +ELF::Core::NotesEntryHeader::Type CoreDumpReader::NotesEntryIterator::type() const +{ + ASSERT(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::Null); + return m_current->header.type; +} + +const ELF::Core::NotesEntry* CoreDumpReader::NotesEntryIterator::current() const +{ + return m_current; +} + +void CoreDumpReader::NotesEntryIterator::next() +{ + ASSERT(!at_end()); + if (type() == ELF::Core::NotesEntryHeader::Type::ThreadInfo) { + const ELF::Core::ThreadInfo* current = (const ELF::Core::ThreadInfo*)m_current; + m_current = (const ELF::Core::NotesEntry*)(current + 1); + return; + } + if (type() == ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo) { + const ELF::Core::MemoryRegionInfo* current = (const ELF::Core::MemoryRegionInfo*)m_current; + m_current = (const ELF::Core::NotesEntry*)(current->region_name + strlen(current->region_name) + 1); + return; + } +} + +bool CoreDumpReader::NotesEntryIterator::at_end() const +{ + return type() == ELF::Core::NotesEntryHeader::Type::Null; +} + +Optional<uint32_t> CoreDumpReader::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 uint32_t*)(®ion_data[offset_in_region]); +} + +const ELF::Core::MemoryRegionInfo* CoreDumpReader::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; +} diff --git a/Libraries/LibCoreDump/CoreDumpReader.h b/Libraries/LibCoreDump/CoreDumpReader.h new file mode 100644 index 0000000000..c898911e4c --- /dev/null +++ b/Libraries/LibCoreDump/CoreDumpReader.h @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include <AK/MappedFile.h> +#include <AK/Noncopyable.h> +#include <AK/OwnPtr.h> +#include <LibELF/CoreDump.h> +#include <LibELF/Image.h> + +class CoreDumpReader { + AK_MAKE_NONCOPYABLE(CoreDumpReader); + +public: + static OwnPtr<CoreDumpReader> create(const String&); + ~CoreDumpReader(); + + CoreDumpReader(OwnPtr<MappedFile>&&); + + 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<uint32_t> peek_memory(FlatPtr address) const; + const ELF::Core::MemoryRegionInfo* region_containing(FlatPtr address) const; + +private: + 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 }; + }; + + OwnPtr<MappedFile> m_coredump_file; + ELF::Image m_coredump_image; + ssize_t m_notes_segment_index { -1 }; +}; + +template<typename Func> +void CoreDumpReader::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* region = (const ELF::Core::MemoryRegionInfo*)(it.current()); + IterationDecision decision = func(region); + if (decision == IterationDecision::Break) + return; + } +} + +template<typename Func> +void CoreDumpReader::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* region = (const ELF::Core::ThreadInfo*)(it.current()); + IterationDecision decision = func(region); + if (decision == IterationDecision::Break) + return; + } +} |