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

Libraries: Move to Userland/Libraries/

This commit is contained in:
Andreas Kling 2021-01-12 12:17:30 +01:00
parent dc28c07fa5
commit 13d7c09125
1857 changed files with 266 additions and 274 deletions

View file

@ -0,0 +1,88 @@
/*
* 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 "AbbreviationsMap.h"
#include "DwarfInfo.h"
#include <AK/MemoryStream.h>
namespace Debug::Dwarf {
AbbreviationsMap::AbbreviationsMap(const DwarfInfo& dwarf_info, u32 offset)
: m_dwarf_info(dwarf_info)
, m_offset(offset)
{
populate_map();
}
void AbbreviationsMap::populate_map()
{
InputMemoryStream abbreviation_stream(m_dwarf_info.abbreviation_data());
abbreviation_stream.discard_or_error(m_offset);
while (!abbreviation_stream.eof()) {
size_t abbreviation_code = 0;
abbreviation_stream.read_LEB128_unsigned(abbreviation_code);
// An abbreviation code of 0 marks the end of the
// abbreviations for a given compilation unit
if (abbreviation_code == 0)
break;
size_t tag {};
abbreviation_stream.read_LEB128_unsigned(tag);
u8 has_children = 0;
abbreviation_stream >> has_children;
AbbreviationEntry abbrevation_entry {};
abbrevation_entry.tag = static_cast<EntryTag>(tag);
abbrevation_entry.has_children = (has_children == 1);
AttributeSpecification current_attribute_specification {};
do {
size_t attribute_value = 0;
size_t form_value = 0;
abbreviation_stream.read_LEB128_unsigned(attribute_value);
abbreviation_stream.read_LEB128_unsigned(form_value);
current_attribute_specification.attribute = static_cast<Attribute>(attribute_value);
current_attribute_specification.form = static_cast<AttributeDataForm>(form_value);
if (current_attribute_specification.attribute != Attribute::None) {
abbrevation_entry.attribute_specifications.append(current_attribute_specification);
}
} while (current_attribute_specification.attribute != Attribute::None || current_attribute_specification.form != AttributeDataForm::None);
m_entries.set((u32)abbreviation_code, move(abbrevation_entry));
}
}
Optional<AbbreviationsMap::AbbreviationEntry> AbbreviationsMap::get(u32 code) const
{
return m_entries.get(code);
}
}

View file

@ -0,0 +1,60 @@
/*
* 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 "DwarfTypes.h"
#include <AK/HashMap.h>
#include <AK/Optional.h>
#include <AK/Types.h>
namespace Debug::Dwarf {
class DwarfInfo;
class AbbreviationsMap {
public:
AbbreviationsMap(const DwarfInfo& dwarf_info, u32 offset);
struct AbbreviationEntry {
EntryTag tag;
bool has_children;
Vector<AttributeSpecification> attribute_specifications;
};
Optional<AbbreviationEntry> get(u32 code) const;
private:
void populate_map();
const DwarfInfo& m_dwarf_info;
u32 m_offset { 0 };
HashMap<u32, AbbreviationEntry> m_entries;
};
}

View file

@ -0,0 +1,44 @@
/*
* 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 "CompilationUnit.h"
#include "DIE.h"
namespace Debug::Dwarf {
CompilationUnit::CompilationUnit(const DwarfInfo& dwarf_info, u32 offset, const CompilationUnitHeader& header)
: m_dwarf_info(dwarf_info)
, m_offset(offset)
, m_header(header)
, m_abbreviations(dwarf_info, header.abbrev_offset)
{
}
DIE CompilationUnit::root_die() const
{
return DIE(*this, m_offset + sizeof(CompilationUnitHeader));
}
}

View file

@ -0,0 +1,56 @@
/*
* 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 "AbbreviationsMap.h"
#include <AK/Types.h>
namespace Debug::Dwarf {
class DwarfInfo;
class DIE;
class CompilationUnit {
public:
CompilationUnit(const DwarfInfo& dwarf_info, u32 offset, const CompilationUnitHeader&);
u32 offset() const { return m_offset; }
u32 size() const { return m_header.length + sizeof(u32); }
DIE root_die() const;
const DwarfInfo& dwarf_info() const { return m_dwarf_info; }
const AbbreviationsMap& abbreviations_map() const { return m_abbreviations; }
private:
const DwarfInfo& m_dwarf_info;
u32 m_offset { 0 };
CompilationUnitHeader m_header;
AbbreviationsMap m_abbreviations;
};
}

View file

@ -0,0 +1,244 @@
/*
* 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 "DIE.h"
#include "CompilationUnit.h"
#include "DwarfInfo.h"
#include <AK/ByteBuffer.h>
#include <AK/MemoryStream.h>
namespace Debug::Dwarf {
DIE::DIE(const CompilationUnit& unit, u32 offset)
: m_compilation_unit(unit)
, m_offset(offset)
{
InputMemoryStream stream(m_compilation_unit.dwarf_info().debug_info_data());
stream.discard_or_error(m_offset);
stream.read_LEB128_unsigned(m_abbreviation_code);
m_data_offset = stream.offset();
if (m_abbreviation_code == 0) {
// An abbreviation code of 0 ( = null DIE entry) means the end of a chain of siblings
m_tag = EntryTag::None;
} else {
auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
ASSERT(abbreviation_info.has_value());
m_tag = abbreviation_info.value().tag;
m_has_children = abbreviation_info.value().has_children;
// We iterate the attributes data only to calculate this DIE's size
for (auto& attribute_spec : abbreviation_info.value().attribute_specifications) {
get_attribute_value(attribute_spec.form, stream);
}
}
m_size = stream.offset() - m_offset;
}
DIE::AttributeValue DIE::get_attribute_value(AttributeDataForm form,
InputMemoryStream& debug_info_stream) const
{
AttributeValue value;
auto assign_raw_bytes_value = [&](size_t length) {
value.data.as_raw_bytes.length = length;
value.data.as_raw_bytes.bytes = reinterpret_cast<const u8*>(m_compilation_unit.dwarf_info().debug_info_data().data()
+ debug_info_stream.offset());
debug_info_stream.discard_or_error(length);
};
switch (form) {
case AttributeDataForm::StringPointer: {
u32 offset;
debug_info_stream >> offset;
value.type = AttributeValue::Type::String;
auto strings_data = m_compilation_unit.dwarf_info().debug_strings_data();
value.data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
break;
}
case AttributeDataForm::Data1: {
u8 data;
debug_info_stream >> data;
value.type = AttributeValue::Type::UnsignedNumber;
value.data.as_u32 = data;
break;
}
case AttributeDataForm::Data2: {
u16 data;
debug_info_stream >> data;
value.type = AttributeValue::Type::UnsignedNumber;
value.data.as_u32 = data;
break;
}
case AttributeDataForm::Addr: {
u32 address;
debug_info_stream >> address;
value.type = AttributeValue::Type::UnsignedNumber;
value.data.as_u32 = address;
break;
}
case AttributeDataForm::SData: {
ssize_t data;
debug_info_stream.read_LEB128_signed(data);
value.type = AttributeValue::Type::SignedNumber;
value.data.as_i32 = data;
break;
}
case AttributeDataForm::SecOffset: {
u32 data;
debug_info_stream >> data;
value.type = AttributeValue::Type::SecOffset;
value.data.as_u32 = data;
break;
}
case AttributeDataForm::Data4: {
u32 data;
debug_info_stream >> data;
value.type = AttributeValue::Type::UnsignedNumber;
value.data.as_u32 = data;
break;
}
case AttributeDataForm::Ref4: {
u32 data;
debug_info_stream >> data;
value.type = AttributeValue::Type::DieReference;
value.data.as_u32 = data + m_compilation_unit.offset();
break;
}
case AttributeDataForm::FlagPresent: {
value.type = AttributeValue::Type::Boolean;
value.data.as_bool = true;
break;
}
case AttributeDataForm::ExprLoc: {
size_t length;
debug_info_stream.read_LEB128_unsigned(length);
value.type = AttributeValue::Type::DwarfExpression;
assign_raw_bytes_value(length);
break;
}
case AttributeDataForm::String: {
String str;
u32 str_offset = debug_info_stream.offset();
debug_info_stream >> str;
value.type = AttributeValue::Type::String;
value.data.as_string = reinterpret_cast<const char*>(str_offset + m_compilation_unit.dwarf_info().debug_info_data().data());
break;
}
case AttributeDataForm::Block1: {
value.type = AttributeValue::Type::RawBytes;
u8 length;
debug_info_stream >> length;
assign_raw_bytes_value(length);
break;
}
case AttributeDataForm::Block2: {
value.type = AttributeValue::Type::RawBytes;
u16 length;
debug_info_stream >> length;
assign_raw_bytes_value(length);
break;
}
case AttributeDataForm::Block4: {
value.type = AttributeValue::Type::RawBytes;
u32 length;
debug_info_stream >> length;
assign_raw_bytes_value(length);
break;
}
case AttributeDataForm::Block: {
value.type = AttributeValue::Type::RawBytes;
size_t length;
debug_info_stream.read_LEB128_unsigned(length);
assign_raw_bytes_value(length);
break;
}
default:
dbgln("Unimplemented AttributeDataForm: {}", (u32)form);
ASSERT_NOT_REACHED();
}
return value;
}
Optional<DIE::AttributeValue> DIE::get_attribute(const Attribute& attribute) const
{
InputMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
stream.discard_or_error(m_data_offset);
auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
ASSERT(abbreviation_info.has_value());
for (const auto& attribute_spec : abbreviation_info.value().attribute_specifications) {
auto value = get_attribute_value(attribute_spec.form, stream);
if (attribute_spec.attribute == attribute) {
return value;
}
}
return {};
}
void DIE::for_each_child(Function<void(const DIE& child)> callback) const
{
if (!m_has_children)
return;
NonnullOwnPtr<DIE> current_child = make<DIE>(m_compilation_unit, m_offset + m_size);
while (true) {
callback(*current_child);
if (current_child->is_null())
break;
if (!current_child->has_children()) {
current_child = make<DIE>(m_compilation_unit, current_child->offset() + current_child->size());
continue;
}
auto sibling = current_child->get_attribute(Attribute::Sibling);
u32 sibling_offset = 0;
if (sibling.has_value()) {
sibling_offset = sibling.value().data.as_u32;
}
if (!sibling.has_value()) {
// NOTE: According to the spec, the compiler doesn't have to supply the sibling information.
// When it doesn't, we have to recursively iterate the current child's children to find where they end
current_child->for_each_child([&](const DIE& sub_child) {
sibling_offset = sub_child.offset() + sub_child.size();
});
}
current_child = make<DIE>(m_compilation_unit, sibling_offset);
}
}
DIE DIE::get_die_at_offset(u32 offset) const
{
ASSERT(offset >= m_compilation_unit.offset() && offset < m_compilation_unit.offset() + m_compilation_unit.size());
return DIE(m_compilation_unit, offset);
}
}

View file

@ -0,0 +1,95 @@
/*
* 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 "CompilationUnit.h"
#include "DwarfTypes.h"
#include <AK/Function.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/Optional.h>
#include <AK/Types.h>
namespace Debug::Dwarf {
class CompilationUnit;
// DIE = Debugging Information Entry
class DIE {
public:
DIE(const CompilationUnit&, u32 offset);
struct AttributeValue {
enum class Type : u8 {
UnsignedNumber,
SignedNumber,
String,
DieReference, // Reference to another DIE in the same compilation unit
Boolean,
DwarfExpression,
SecOffset,
RawBytes,
} type;
union {
u32 as_u32;
i32 as_i32;
const char* as_string; // points to bytes in the memory mapped elf image
bool as_bool;
struct {
u32 length;
const u8* bytes; // points to bytes in the memory mapped elf image
} as_raw_bytes;
} data {};
};
u32 offset() const { return m_offset; }
u32 size() const { return m_size; }
bool has_children() const { return m_has_children; }
EntryTag tag() const { return m_tag; }
Optional<AttributeValue> get_attribute(const Attribute&) const;
void for_each_child(Function<void(const DIE& child)> callback) const;
bool is_null() const { return m_tag == EntryTag::None; }
DIE get_die_at_offset(u32 offset) const;
private:
AttributeValue get_attribute_value(AttributeDataForm form,
InputMemoryStream& debug_info_stream) const;
const CompilationUnit& m_compilation_unit;
u32 m_offset { 0 };
u32 m_data_offset { 0 };
size_t m_abbreviation_code { 0 };
EntryTag m_tag { EntryTag::None };
bool m_has_children { false };
u32 m_size { 0 };
};
}

View file

@ -0,0 +1,71 @@
/*
* 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 "DwarfInfo.h"
#include <AK/MemoryStream.h>
namespace Debug::Dwarf {
DwarfInfo::DwarfInfo(const ELF::Image& elf)
: m_elf(elf)
{
m_debug_info_data = section_data(".debug_info");
m_abbreviation_data = section_data(".debug_abbrev");
m_debug_strings_data = section_data(".debug_str");
populate_compilation_units();
}
ReadonlyBytes DwarfInfo::section_data(const String& section_name) const
{
auto section = m_elf.lookup_section(section_name);
if (section.is_undefined())
return {};
return section.bytes();
}
void DwarfInfo::populate_compilation_units()
{
if (!m_debug_info_data.data())
return;
InputMemoryStream stream { m_debug_info_data };
while (!stream.eof()) {
auto unit_offset = stream.offset();
CompilationUnitHeader compilation_unit_header {};
stream >> Bytes { &compilation_unit_header, sizeof(compilation_unit_header) };
ASSERT(compilation_unit_header.address_size == sizeof(u32));
ASSERT(compilation_unit_header.version <= 4);
u32 length_after_header = compilation_unit_header.length - (sizeof(CompilationUnitHeader) - offsetof(CompilationUnitHeader, version));
m_compilation_units.empend(*this, unit_offset, compilation_unit_header);
stream.discard_or_error(length_after_header);
}
}
}

View file

@ -0,0 +1,71 @@
/*
* 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 "CompilationUnit.h"
#include "DwarfTypes.h"
#include <AK/ByteBuffer.h>
#include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h>
#include <AK/String.h>
#include <LibELF/Image.h>
namespace Debug::Dwarf {
class DwarfInfo {
public:
explicit DwarfInfo(const ELF::Image&);
ReadonlyBytes debug_info_data() const { return m_debug_info_data; }
ReadonlyBytes abbreviation_data() const { return m_abbreviation_data; }
ReadonlyBytes debug_strings_data() const { return m_debug_strings_data; }
template<typename Callback>
void for_each_compilation_unit(Callback) const;
private:
void populate_compilation_units();
ReadonlyBytes section_data(const String& section_name) const;
const ELF::Image& m_elf;
ReadonlyBytes m_debug_info_data;
ReadonlyBytes m_abbreviation_data;
ReadonlyBytes m_debug_strings_data;
Vector<Dwarf::CompilationUnit> m_compilation_units;
};
template<typename Callback>
void DwarfInfo::for_each_compilation_unit(Callback callback) const
{
for (const auto& unit : m_compilation_units) {
callback(unit);
}
}
}

View file

@ -0,0 +1,294 @@
/*
* 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/Types.h>
namespace Debug::Dwarf {
struct [[gnu::packed]] CompilationUnitHeader {
u32 length;
u16 version;
u32 abbrev_offset;
u8 address_size;
};
enum class EntryTag : u16 {
None = 0,
ArrayType = 0x1,
ClassType = 0x2,
EntryPoint = 0x3,
EnumerationType = 0x4,
FormalParameter = 0x5,
ImportedDeclaration = 0x8,
Label = 0xa,
LexicalBlock = 0xb,
Member = 0xd,
PointerType = 0xf,
ReferenceType = 0x10,
CompileUnit = 0x11,
StringType = 0x12,
StructureType = 0x13,
SubroutineType = 0x15,
TypeDef = 0x16,
UnionType = 0x17,
UnspecifiedParameters = 0x18,
Variant = 0x19,
CommonBlock = 0x1a,
CommonInclusion = 0x1b,
Inheritance = 0x1c,
InlinedSubroutine = 0x1d,
Module = 0x1e,
PtrToMemberType = 0x1f,
SetType = 0x20,
SubRangeType = 0x21,
WithStmt = 0x22,
AccessDeclaration = 0x23,
BaseType = 0x24,
CatchBlock = 0x25,
ConstType = 0x26,
Constant = 0x27,
Enumerator = 0x28,
FileType = 0x29,
Friend = 0x2a,
NameList = 0x2b,
NameListItem = 0x2c,
PackedType = 0x2d,
SubProgram = 0x2e,
TemplateTypeParam = 0x2f,
TemplateValueParam = 0x30,
ThrownType = 0x31,
TryBlock = 0x32,
VariantPart = 0x33,
Variable = 0x34,
VolatileType = 0x35,
DwarfProcedure = 0x36,
RestrictType = 0x37,
InterfaceType = 0x38,
Namespace = 0x39,
ImportedModule = 0x3a,
UnspecifiedType = 0x3b,
PartialUnit = 0x3c,
ImportedUnit = 0x3d,
MutableType = 0x3e,
Condition = 0x3f,
SharedTyped = 0x40,
TypeUnit = 0x41,
RValueReferenceType = 0x42,
TemplateAlias = 0x43,
CoArrayType = 0x44,
GenericSubRange = 0x45,
DynamicType = 0x46,
AtomicType = 0x47,
CallSite = 0x48,
CallSiteParameter = 0x49,
SkeletonUnit = 0x4a,
ImmutableType = 0x4b,
LoUser = 0x4080,
HiUser = 0xffff,
};
enum class Attribute : u16 {
None = 0,
Sibling = 0x1,
Location = 0x2,
Name = 0x3,
Ordering = 0x9,
ByteSize = 0xb,
BitOffset = 0xc,
BitSize = 0xd,
StmtList = 0x10,
LowPc = 0x11,
HighPc = 0x12,
Language = 0x13,
Discr = 0x15,
DiscrValue = 0x16,
Visibility = 0x17,
Import = 0x18,
StringLength = 0x19,
CommonReference = 0x1a,
CompDir = 0x1b,
ConstValue = 0x1c,
ContainingType = 0x1d,
DefaultValue = 0x1e,
Inline = 0x20,
IsOptional = 0x21,
LowerBound = 0x22,
Producer = 0x25,
Prototyped = 0x27,
ReturnAddr = 0x2a,
StartScope = 0x2c,
BitStride = 0x2e,
UpperBound = 0x2f,
AbstractOrigin = 0x31,
Accessibility = 0x32,
AddressClass = 0x33,
Artificial = 0x34,
BaseTypes = 0x35,
CallingConvention = 0x36,
Count = 0x37,
MemberLocation = 0x38,
DeclColumn = 0x39,
DeclFile = 0x3a,
DeclLine = 0x3b,
Declaration = 0x3c,
DiscrList = 0x3d,
Encoding = 0x3e,
External = 0x3f,
FrameBase = 0x40,
Friend = 0x41,
IdentifierCase = 0x43,
MacroInfo = 0x43,
NameListItem = 0x44,
Priority = 0x45,
Segment = 0x46,
Specification = 0x47,
StaticLink = 0x48,
Type = 0x49,
UseLocation = 0x4a,
VariableParameter = 0x4b,
Virtuality = 0x4c,
VtableElemLocation = 0x4d,
Allocated = 0x4e,
Associated = 0x4f,
DataLocation = 0x50,
ByteStride = 0x51,
EntryPC = 0x52,
UseUTF8 = 0x53,
Extension = 0x54,
Ranges = 0x55,
Trampoline = 0x56,
CallColumn = 0x57,
CallFile = 0x58,
CallLine = 0x59,
Description = 0x5a,
BinaryScale = 0x5b,
DecimalScale = 0x5c,
Small = 0x5d,
DecimalSign = 0x5e,
DigitCount = 0x5f,
PictureString = 0x60,
Mutable = 0x61,
ThreadsScaled = 0x62,
Explicit = 0x63,
ObjectPointer = 0x64,
Endianity = 0x65,
Elemental = 0x66,
Pure = 0x67,
Recursive = 0x68,
Signature = 0x69,
MainSubprogram = 0x6a,
DataBitOffset = 0x6b,
ConstExpr = 0x6c,
EnumClass = 0x6d,
LinkageName = 0x6e,
StringLengthBitSize = 0x6f,
StringLengthByteSize = 0x70,
Rank = 0x71,
StrOffsetsBase = 0x72,
AddrBase = 0x73,
RngListsBase = 0x74,
DWOName = 0x76,
Reference = 0x77,
RValueReference = 0x78,
Macros = 0x79,
CallAllCalls = 0x7a,
CallAllSourceCalls = 0x7b,
CallAllTailCalls = 0x7c,
CallReturnPC = 0x7d,
CallValue = 0x7e,
CallOrigin = 0x7f,
CallParameter = 0x80,
CallPC = 0x81,
CallTailCall = 0x82,
CallTarget = 0x83,
CallTargetClobbered = 0x84,
CallDataLocation = 0x85,
CallDataValue = 0x86,
NoReturn = 0x87,
Alignment = 0x88,
ExportSymbols = 0x89,
Deleted = 0x8a,
Defaulted = 0x8b,
LocListsBase = 0x8c,
LoUser = 0x2000,
HiUser = 0x3fff,
};
enum class AttributeDataForm : u8 {
None = 0,
Addr = 0x1,
Block2 = 0x3,
Block4 = 0x4,
Data2 = 0x5,
Data4 = 0x6,
Data8 = 0x7,
String = 0x8,
Block = 0x9,
Block1 = 0xa,
Data1 = 0xb,
Flag = 0xc,
SData = 0xd,
StringPointer = 0xe,
UData = 0xf,
RefAddr = 0x10,
Ref1 = 0x11,
Ref2 = 0x12,
Ref4 = 0x13,
Ref8 = 0x14,
RefUData = 0x15,
Indirect = 0x16,
SecOffset = 0x17,
ExprLoc = 0x18,
FlagPresent = 0x19,
StrX = 0x1a,
AddrX = 0x1b,
RefSup4 = 0x1c,
StrPSup = 0x1d,
Data16 = 0x1e,
LineStrP = 0x1f,
RefSig8 = 0x20,
ImplicitConst = 0x21,
LocListX = 0x22,
RngListX = 0x23,
RefSup8 = 0x24,
StrX1 = 0x25,
StrX2 = 0x26,
StrX3 = 0x27,
StrX4 = 0x28,
AddrX1 = 0x29,
AddrX2 = 0x2a,
AddrX3 = 0x2b,
AddrX4 = 0x2c
};
struct [[gnu::packed]] AttributeSpecification {
Attribute attribute;
AttributeDataForm form;
};
}

View file

@ -0,0 +1,64 @@
/*
* 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 "Expression.h"
#include <AK/MemoryStream.h>
#include <sys/arch/i386/regs.h>
namespace Debug::Dwarf::Expression {
Value evaluate(ReadonlyBytes bytes, const PtraceRegisters& regs)
{
InputMemoryStream stream(bytes);
while (!stream.eof()) {
u8 opcode = 0;
stream >> opcode;
switch (static_cast<Operations>(opcode)) {
case Operations::RegEbp: {
ssize_t offset = 0;
stream.read_LEB128_signed(offset);
return Value { Type::UnsignedIntetger, regs.ebp + offset };
}
case Operations::FbReg: {
ssize_t offset = 0;
stream.read_LEB128_signed(offset);
return Value { Type::UnsignedIntetger, regs.ebp + 2 * sizeof(size_t) + offset };
}
default:
dbgln("DWARF expr addr: {}", (const void*)bytes.data());
dbgln("unsupported opcode: {}", (u8)opcode);
ASSERT_NOT_REACHED();
}
}
ASSERT_NOT_REACHED();
}
}

View file

@ -0,0 +1,56 @@
/*
* 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/ByteBuffer.h"
#include "AK/Types.h"
struct PtraceRegisters;
namespace Debug::Dwarf::Expression {
enum class Type {
None,
UnsignedIntetger,
Register,
};
struct Value {
Type type;
union {
u32 as_u32;
} data { 0 };
};
enum class Operations : u8 {
RegEbp = 0x75,
FbReg = 0x91,
};
Value evaluate(ReadonlyBytes, const PtraceRegisters&);
}

View file

@ -0,0 +1,269 @@
/*
* 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 "LineProgram.h"
#include <AK/String.h>
#include <AK/StringBuilder.h>
//#define DWARF_DEBUG
namespace Debug::Dwarf {
LineProgram::LineProgram(InputMemoryStream& stream)
: m_stream(stream)
{
m_unit_offset = m_stream.offset();
parse_unit_header();
parse_source_directories();
parse_source_files();
run_program();
}
void LineProgram::parse_unit_header()
{
m_stream >> Bytes { &m_unit_header, sizeof(m_unit_header) };
ASSERT(m_unit_header.version == DWARF_VERSION);
ASSERT(m_unit_header.opcode_base == SPECIAL_OPCODES_BASE);
#ifdef DWARF_DEBUG
dbgln("unit length: {}", m_unit_header.length);
#endif
}
void LineProgram::parse_source_directories()
{
m_source_directories.append(".");
while (m_stream.peek_or_error()) {
String directory;
m_stream >> directory;
#ifdef DWARF_DEBUG
dbgln("directory: {}", directory);
#endif
m_source_directories.append(move(directory));
}
m_stream.handle_recoverable_error();
m_stream.discard_or_error(1);
ASSERT(!m_stream.has_any_error());
}
void LineProgram::parse_source_files()
{
m_source_files.append({ ".", 0 });
while (!m_stream.eof() && m_stream.peek_or_error()) {
String file_name;
m_stream >> file_name;
size_t directory_index = 0;
m_stream.read_LEB128_unsigned(directory_index);
size_t _unused = 0;
m_stream.read_LEB128_unsigned(_unused); // skip modification time
m_stream.read_LEB128_unsigned(_unused); // skip file size
#ifdef DWARF_DEBUG
dbgln("file: {}, directory index: {}", file_name, directory_index);
#endif
m_source_files.append({ file_name, directory_index });
}
m_stream.discard_or_error(1);
ASSERT(!m_stream.has_any_error());
}
void LineProgram::append_to_line_info()
{
#ifdef DWARF_DEBUG
dbgln("appending line info: {:p}, {}:{}", m_address, m_source_files[m_file_index].name, m_line);
#endif
if (!m_is_statement)
return;
String directory = m_source_directories[m_source_files[m_file_index].directory_index];
StringBuilder full_path(directory.length() + m_source_files[m_file_index].name.length() + 1);
full_path.append(directory);
full_path.append('/');
full_path.append(m_source_files[m_file_index].name);
m_lines.append({ m_address, full_path.to_string(), m_line });
}
void LineProgram::reset_registers()
{
m_address = 0;
m_line = 1;
m_file_index = 1;
m_is_statement = m_unit_header.default_is_stmt == 1;
}
void LineProgram::handle_extended_opcode()
{
size_t length = 0;
m_stream.read_LEB128_unsigned(length);
u8 sub_opcode = 0;
m_stream >> sub_opcode;
switch (sub_opcode) {
case ExtendedOpcodes::EndSequence: {
append_to_line_info();
reset_registers();
break;
}
case ExtendedOpcodes::SetAddress: {
ASSERT(length == sizeof(size_t) + 1);
m_stream >> m_address;
#ifdef DWARF_DEBUG
dbgln("SetAddress: {:p}", m_address);
#endif
break;
}
case ExtendedOpcodes::SetDiscriminator: {
#ifdef DWARF_DEBUG
dbgln("SetDiscriminator");
#endif
m_stream.discard_or_error(1);
break;
}
default:
#ifdef DWARF_DEBUG
dbgln("offset: {:p}", m_stream.offset());
#endif
ASSERT_NOT_REACHED();
}
}
void LineProgram::handle_standard_opcode(u8 opcode)
{
switch (opcode) {
case StandardOpcodes::Copy: {
append_to_line_info();
break;
}
case StandardOpcodes::AdvancePc: {
size_t operand = 0;
m_stream.read_LEB128_unsigned(operand);
size_t delta = operand * m_unit_header.min_instruction_length;
#ifdef DWARF_DEBUG
dbgln("AdvancePC by: {} to: {:p}", delta, m_address + delta);
#endif
m_address += delta;
break;
}
case StandardOpcodes::SetFile: {
size_t new_file_index = 0;
m_stream.read_LEB128_unsigned(new_file_index);
#ifdef DWARF_DEBUG
dbgln("SetFile: new file index: {}", new_file_index);
#endif
m_file_index = new_file_index;
break;
}
case StandardOpcodes::SetColumn: {
// not implemented
#ifdef DWARF_DEBUG
dbgln("SetColumn");
#endif
size_t new_column;
m_stream.read_LEB128_unsigned(new_column);
break;
}
case StandardOpcodes::AdvanceLine: {
ssize_t line_delta;
m_stream.read_LEB128_signed(line_delta);
ASSERT(line_delta >= 0 || m_line >= (size_t)(-line_delta));
m_line += line_delta;
#ifdef DWARF_DEBUG
dbgln("AdvanceLine: {}", m_line);
#endif
break;
}
case StandardOpcodes::NegateStatement: {
#ifdef DWARF_DEBUG
dbgln("NegateStatement");
#endif
m_is_statement = !m_is_statement;
break;
}
case StandardOpcodes::ConstAddPc: {
u8 adjusted_opcode = 255 - SPECIAL_OPCODES_BASE;
ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range) * m_unit_header.min_instruction_length;
address_increment *= m_unit_header.min_instruction_length;
#ifdef DWARF_DEBUG
dbgln("ConstAddPc: advance pc by: {} to: {}", address_increment, (m_address + address_increment));
#endif
m_address += address_increment;
break;
}
case StandardOpcodes::SetIsa: {
size_t isa;
m_stream.read_LEB128_unsigned(isa);
dbgln("SetIsa: {}", isa);
break;
}
default:
dbgln("Unhandled LineProgram opcode {}", opcode);
ASSERT_NOT_REACHED();
}
}
void LineProgram::handle_sepcial_opcode(u8 opcode)
{
u8 adjusted_opcode = opcode - SPECIAL_OPCODES_BASE;
ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range) * m_unit_header.min_instruction_length;
ssize_t line_increment = m_unit_header.line_base + (adjusted_opcode % m_unit_header.line_range);
m_address += address_increment;
m_line += line_increment;
#ifdef DWARF_DEBUG
dbgln("Special adjusted_opcode: {}, address_increment: {}, line_increment: {}", adjusted_opcode, address_increment, line_increment);
dbg() << "Address is now:" << (void*)m_address << ", and line is: " << m_source_files[m_file_index].name << ":" << m_line;
#endif
append_to_line_info();
}
void LineProgram::run_program()
{
reset_registers();
while ((size_t)m_stream.offset() < m_unit_offset + sizeof(u32) + m_unit_header.length) {
u8 opcode = 0;
m_stream >> opcode;
#ifdef DWARF_DEBUG
dbg() << (void*)(m_stream.offset() - 1) << ": opcode: " << opcode;
#endif
if (opcode == 0) {
handle_extended_opcode();
} else if (opcode >= 1 && opcode <= 12) {
handle_standard_opcode(opcode);
} else {
handle_sepcial_opcode(opcode);
}
}
}
}

View file

@ -0,0 +1,118 @@
/*
* 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/FlyString.h>
#include <AK/MemoryStream.h>
#include <AK/Vector.h>
namespace Debug::Dwarf {
class LineProgram {
public:
explicit LineProgram(InputMemoryStream& stream);
struct LineInfo {
u32 address { 0 };
FlyString file;
size_t line { 0 };
};
const Vector<LineInfo>& lines() const { return m_lines; }
private:
void parse_unit_header();
void parse_source_directories();
void parse_source_files();
void run_program();
void append_to_line_info();
void reset_registers();
void handle_extended_opcode();
void handle_standard_opcode(u8 opcode);
void handle_sepcial_opcode(u8 opcode);
struct [[gnu::packed]] UnitHeader32 {
u32 length;
u16 version;
u32 header_length;
u8 min_instruction_length;
u8 default_is_stmt;
i8 line_base;
u8 line_range;
u8 opcode_base;
u8 std_opcode_lengths[12];
};
enum StandardOpcodes {
Copy = 1,
AdvancePc,
AdvanceLine,
SetFile,
SetColumn,
NegateStatement,
SetBasicBlock,
ConstAddPc,
FixAdvancePc,
SetProlougeEnd,
SetEpilogueBegin,
SetIsa
};
enum ExtendedOpcodes {
EndSequence = 1,
SetAddress,
DefineFile,
SetDiscriminator,
};
struct FileEntry {
FlyString name;
size_t directory_index { 0 };
};
static constexpr u16 DWARF_VERSION = 3;
static constexpr u8 SPECIAL_OPCODES_BASE = 13;
InputMemoryStream& m_stream;
size_t m_unit_offset { 0 };
UnitHeader32 m_unit_header {};
Vector<String> m_source_directories;
Vector<FileEntry> m_source_files;
// The registers of the "line program" virtual machine
u32 m_address { 0 };
size_t m_line { 0 };
size_t m_file_index { 0 };
bool m_is_statement { false };
Vector<LineInfo> m_lines;
};
}