LibCoreDump: Add library for parsing coredump files
This commit is contained in:
parent
50219429fd
commit
a74dad14a8
Notes:
sideshowbarker
2024-07-19 00:50:47 +09:00
Author: https://github.com/itamar8910 Commit: https://github.com/SerenityOS/serenity/commit/a74dad14a83 Pull-request: https://github.com/SerenityOS/serenity/pull/3738 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/awesomekling
4 changed files with 226 additions and 0 deletions
|
@ -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)
|
||||
|
|
6
Libraries/LibCoreDump/CMakeLists.txt
Normal file
6
Libraries/LibCoreDump/CMakeLists.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
set(SOURCES
|
||||
CoreDumpReader.cpp
|
||||
)
|
||||
|
||||
serenity_lib(LibCoreDump coredump)
|
||||
target_link_libraries(LibCoreDump LibC LibCore)
|
119
Libraries/LibCoreDump/CoreDumpReader.cpp
Normal file
119
Libraries/LibCoreDump/CoreDumpReader.cpp
Normal file
|
@ -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;
|
||||
}
|
100
Libraries/LibCoreDump/CoreDumpReader.h
Normal file
100
Libraries/LibCoreDump/CoreDumpReader.h
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue