2020-05-04 12:14:39 +03:00
|
|
|
/*
|
2021-06-19 11:39:51 +03:00
|
|
|
* Copyright (c) 2020-2021, Itamar S. <itamar8910@gmail.com>
|
2020-05-04 12:14:39 +03:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-04 12:14:39 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "DIE.h"
|
|
|
|
|
#include "CompilationUnit.h"
|
|
|
|
|
#include "DwarfInfo.h"
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
2020-09-01 11:43:32 +02:00
|
|
|
#include <AK/MemoryStream.h>
|
2020-05-04 12:14:39 +03:00
|
|
|
|
2020-08-25 04:33:07 +01:00
|
|
|
namespace Debug::Dwarf {
|
2020-05-04 12:14:39 +03:00
|
|
|
|
2021-06-19 15:33:03 +03:00
|
|
|
DIE::DIE(CompilationUnit const& unit, u32 offset, Optional<u32> parent_offset)
|
2020-05-04 12:14:39 +03:00
|
|
|
: m_compilation_unit(unit)
|
|
|
|
|
{
|
2021-09-17 02:12:29 -07:00
|
|
|
rehydrate_from(offset, parent_offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DIE::rehydrate_from(u32 offset, Optional<u32> parent_offset)
|
|
|
|
|
{
|
|
|
|
|
m_offset = offset;
|
|
|
|
|
|
2020-08-15 18:38:24 +02:00
|
|
|
InputMemoryStream stream(m_compilation_unit.dwarf_info().debug_info_data());
|
2020-08-05 10:55:36 +02:00
|
|
|
stream.discard_or_error(m_offset);
|
2020-05-04 12:14:39 +03:00
|
|
|
stream.read_LEB128_unsigned(m_abbreviation_code);
|
|
|
|
|
m_data_offset = stream.offset();
|
|
|
|
|
|
|
|
|
|
if (m_abbreviation_code == 0) {
|
2020-10-02 09:59:28 -04:00
|
|
|
// An abbreviation code of 0 ( = null DIE entry) means the end of a chain of siblings
|
2020-05-04 12:14:39 +03:00
|
|
|
m_tag = EntryTag::None;
|
|
|
|
|
} else {
|
|
|
|
|
auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(abbreviation_info.has_value());
|
2020-05-04 12:14:39 +03:00
|
|
|
|
|
|
|
|
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
|
2020-11-08 22:52:22 +01:00
|
|
|
for (auto& attribute_spec : abbreviation_info.value().attribute_specifications) {
|
2021-04-29 00:51:54 +02:00
|
|
|
m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit);
|
2020-05-04 12:14:39 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_size = stream.offset() - m_offset;
|
2021-06-19 11:39:51 +03:00
|
|
|
m_parent_offset = parent_offset;
|
2020-05-04 12:14:39 +03:00
|
|
|
}
|
|
|
|
|
|
2021-06-19 15:33:03 +03:00
|
|
|
Optional<AttributeValue> DIE::get_attribute(Attribute const& attribute) const
|
2020-05-04 12:14:39 +03:00
|
|
|
{
|
2020-08-15 18:38:24 +02:00
|
|
|
InputMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
|
2020-08-05 10:55:36 +02:00
|
|
|
stream.discard_or_error(m_data_offset);
|
2020-05-04 12:14:39 +03:00
|
|
|
|
|
|
|
|
auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(abbreviation_info.has_value());
|
2020-05-04 12:14:39 +03:00
|
|
|
|
|
|
|
|
for (const auto& attribute_spec : abbreviation_info.value().attribute_specifications) {
|
2021-04-29 00:51:54 +02:00
|
|
|
auto value = m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit);
|
2020-05-04 12:14:39 +03:00
|
|
|
if (attribute_spec.attribute == attribute) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 15:33:03 +03:00
|
|
|
void DIE::for_each_child(Function<void(DIE const& child)> callback) const
|
2020-05-04 12:14:39 +03:00
|
|
|
{
|
|
|
|
|
if (!m_has_children)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-09-17 02:12:29 -07:00
|
|
|
auto current_child = DIE(m_compilation_unit, m_offset + m_size, m_offset);
|
2020-05-04 12:14:39 +03:00
|
|
|
while (true) {
|
2021-09-17 02:12:29 -07:00
|
|
|
callback(current_child);
|
|
|
|
|
if (current_child.is_null())
|
2020-05-04 12:14:39 +03:00
|
|
|
break;
|
2021-09-17 02:12:29 -07:00
|
|
|
if (!current_child.has_children()) {
|
|
|
|
|
current_child.rehydrate_from(current_child.offset() + current_child.size(), m_offset);
|
2020-05-04 12:14:39 +03:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 02:12:29 -07:00
|
|
|
auto sibling = current_child.get_attribute(Attribute::Sibling);
|
2020-05-04 12:14:39 +03:00
|
|
|
u32 sibling_offset = 0;
|
|
|
|
|
if (sibling.has_value()) {
|
2021-07-27 11:03:24 +02:00
|
|
|
sibling_offset = sibling.value().data.as_unsigned;
|
2021-09-17 02:12:29 -07:00
|
|
|
} else {
|
2020-10-02 09:59:28 -04:00
|
|
|
// NOTE: According to the spec, the compiler doesn't have to supply the sibling information.
|
2020-05-04 12:14:39 +03:00
|
|
|
// When it doesn't, we have to recursively iterate the current child's children to find where they end
|
2021-09-17 02:12:29 -07:00
|
|
|
current_child.for_each_child([&](DIE const& sub_child) {
|
2020-05-04 12:14:39 +03:00
|
|
|
sibling_offset = sub_child.offset() + sub_child.size();
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-09-17 02:12:29 -07:00
|
|
|
current_child.rehydrate_from(sibling_offset, m_offset);
|
2020-05-04 12:14:39 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|