DIE.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. {
  15. rehydrate_from(offset, parent_offset);
  16. }
  17. void DIE::rehydrate_from(u32 offset, Optional<u32> parent_offset)
  18. {
  19. m_offset = offset;
  20. InputMemoryStream stream(m_compilation_unit.dwarf_info().debug_info_data());
  21. stream.discard_or_error(m_offset);
  22. stream.read_LEB128_unsigned(m_abbreviation_code);
  23. m_data_offset = stream.offset();
  24. if (m_abbreviation_code == 0) {
  25. // An abbreviation code of 0 ( = null DIE entry) means the end of a chain of siblings
  26. m_tag = EntryTag::None;
  27. } else {
  28. auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
  29. VERIFY(abbreviation_info);
  30. m_tag = abbreviation_info->tag;
  31. m_has_children = abbreviation_info->has_children;
  32. // We iterate the attributes data only to calculate this DIE's size
  33. for (auto& attribute_spec : abbreviation_info->attribute_specifications) {
  34. m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit);
  35. }
  36. }
  37. m_size = stream.offset() - m_offset;
  38. m_parent_offset = parent_offset;
  39. }
  40. Optional<AttributeValue> DIE::get_attribute(Attribute const& attribute) const
  41. {
  42. InputMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
  43. stream.discard_or_error(m_data_offset);
  44. auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
  45. VERIFY(abbreviation_info);
  46. for (auto const& attribute_spec : abbreviation_info->attribute_specifications) {
  47. auto value = m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit);
  48. if (attribute_spec.attribute == attribute) {
  49. return value;
  50. }
  51. }
  52. return {};
  53. }
  54. void DIE::for_each_child(Function<void(DIE const& child)> callback) const
  55. {
  56. if (!m_has_children)
  57. return;
  58. auto current_child = DIE(m_compilation_unit, m_offset + m_size, m_offset);
  59. while (true) {
  60. callback(current_child);
  61. if (current_child.is_null())
  62. break;
  63. if (!current_child.has_children()) {
  64. current_child.rehydrate_from(current_child.offset() + current_child.size(), m_offset);
  65. continue;
  66. }
  67. auto sibling = current_child.get_attribute(Attribute::Sibling);
  68. u32 sibling_offset = 0;
  69. if (sibling.has_value()) {
  70. sibling_offset = sibling.value().as_unsigned();
  71. } else {
  72. // NOTE: According to the spec, the compiler doesn't have to supply the sibling information.
  73. // When it doesn't, we have to recursively iterate the current child's children to find where they end
  74. current_child.for_each_child([&](DIE const& sub_child) {
  75. sibling_offset = sub_child.offset() + sub_child.size();
  76. });
  77. }
  78. current_child.rehydrate_from(sibling_offset, m_offset);
  79. }
  80. }
  81. }