DIE.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2020-2021, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "DIE.h"
  7. #include "CompilationUnit.h"
  8. #include "DwarfInfo.h"
  9. #include <AK/ByteBuffer.h>
  10. #include <AK/MemoryStream.h>
  11. namespace Debug::Dwarf {
  12. DIE::DIE(CompilationUnit const& unit, u32 offset, Optional<u32> parent_offset)
  13. : m_compilation_unit(unit)
  14. , m_offset(offset)
  15. {
  16. InputMemoryStream stream(m_compilation_unit.dwarf_info().debug_info_data());
  17. stream.discard_or_error(m_offset);
  18. stream.read_LEB128_unsigned(m_abbreviation_code);
  19. m_data_offset = stream.offset();
  20. if (m_abbreviation_code == 0) {
  21. // An abbreviation code of 0 ( = null DIE entry) means the end of a chain of siblings
  22. m_tag = EntryTag::None;
  23. } else {
  24. auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
  25. VERIFY(abbreviation_info.has_value());
  26. m_tag = abbreviation_info.value().tag;
  27. m_has_children = abbreviation_info.value().has_children;
  28. // We iterate the attributes data only to calculate this DIE's size
  29. for (auto& attribute_spec : abbreviation_info.value().attribute_specifications) {
  30. m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit);
  31. }
  32. }
  33. m_size = stream.offset() - m_offset;
  34. m_parent_offset = parent_offset;
  35. }
  36. Optional<AttributeValue> DIE::get_attribute(Attribute const& attribute) const
  37. {
  38. InputMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
  39. stream.discard_or_error(m_data_offset);
  40. auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
  41. VERIFY(abbreviation_info.has_value());
  42. for (const auto& attribute_spec : abbreviation_info.value().attribute_specifications) {
  43. auto value = m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit);
  44. if (attribute_spec.attribute == attribute) {
  45. return value;
  46. }
  47. }
  48. return {};
  49. }
  50. void DIE::for_each_child(Function<void(DIE const& child)> callback) const
  51. {
  52. if (!m_has_children)
  53. return;
  54. NonnullOwnPtr<DIE> current_child = make<DIE>(m_compilation_unit, m_offset + m_size, m_offset);
  55. while (true) {
  56. callback(*current_child);
  57. if (current_child->is_null())
  58. break;
  59. if (!current_child->has_children()) {
  60. current_child = make<DIE>(m_compilation_unit, current_child->offset() + current_child->size(), m_offset);
  61. continue;
  62. }
  63. auto sibling = current_child->get_attribute(Attribute::Sibling);
  64. u32 sibling_offset = 0;
  65. if (sibling.has_value()) {
  66. sibling_offset = sibling.value().data.as_unsigned;
  67. }
  68. if (!sibling.has_value()) {
  69. // NOTE: According to the spec, the compiler doesn't have to supply the sibling information.
  70. // When it doesn't, we have to recursively iterate the current child's children to find where they end
  71. current_child->for_each_child([&](DIE const& sub_child) {
  72. sibling_offset = sub_child.offset() + sub_child.size();
  73. });
  74. }
  75. current_child = make<DIE>(m_compilation_unit, sibling_offset, m_offset);
  76. }
  77. }
  78. }