DIE.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/LEB128.h>
  11. #include <AK/MemoryStream.h>
  12. namespace Debug::Dwarf {
  13. DIE::DIE(CompilationUnit const& unit, u32 offset, Optional<u32> parent_offset)
  14. : m_compilation_unit(unit)
  15. {
  16. rehydrate_from(offset, parent_offset).release_value_but_fixme_should_propagate_errors();
  17. }
  18. ErrorOr<void> DIE::rehydrate_from(u32 offset, Optional<u32> parent_offset)
  19. {
  20. m_offset = offset;
  21. FixedMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
  22. // Note: We can't just slice away from the input data here, since get_attribute_value will try to recover the original offset using seek().
  23. TRY(stream.seek(m_offset));
  24. m_abbreviation_code = TRY(stream.read_value<LEB128<size_t>>());
  25. m_data_offset = TRY(stream.tell());
  26. if (m_abbreviation_code == 0) {
  27. // An abbreviation code of 0 ( = null DIE entry) means the end of a chain of siblings
  28. m_tag = EntryTag::None;
  29. } else {
  30. auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
  31. VERIFY(abbreviation_info);
  32. m_tag = abbreviation_info->tag;
  33. m_has_children = abbreviation_info->has_children;
  34. // We iterate the attributes data only to calculate this DIE's size
  35. for (auto& attribute_spec : abbreviation_info->attribute_specifications) {
  36. TRY(m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit));
  37. }
  38. }
  39. m_size = TRY(stream.tell()) - m_offset;
  40. m_parent_offset = parent_offset;
  41. return {};
  42. }
  43. ErrorOr<Optional<AttributeValue>> DIE::get_attribute(Attribute const& attribute) const
  44. {
  45. FixedMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
  46. // Note: We can't just slice away from the input data here, since get_attribute_value will try to recover the original offset using seek().
  47. TRY(stream.seek(m_data_offset));
  48. auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
  49. VERIFY(abbreviation_info);
  50. for (auto const& attribute_spec : abbreviation_info->attribute_specifications) {
  51. auto value = TRY(m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit));
  52. if (attribute_spec.attribute == attribute) {
  53. return value;
  54. }
  55. }
  56. return Optional<AttributeValue> {};
  57. }
  58. ErrorOr<void> DIE::for_each_child(Function<ErrorOr<void>(DIE const& child)> callback) const
  59. {
  60. if (!m_has_children)
  61. return {};
  62. auto current_child = DIE(m_compilation_unit, m_offset + m_size, m_offset);
  63. while (true) {
  64. TRY(callback(current_child));
  65. if (current_child.is_null())
  66. break;
  67. if (!current_child.has_children()) {
  68. TRY(current_child.rehydrate_from(current_child.offset() + current_child.size(), m_offset));
  69. continue;
  70. }
  71. auto sibling = TRY(current_child.get_attribute(Attribute::Sibling));
  72. u32 sibling_offset = 0;
  73. if (sibling.has_value()) {
  74. sibling_offset = sibling.value().as_unsigned();
  75. } else {
  76. // NOTE: According to the spec, the compiler doesn't have to supply the sibling information.
  77. // When it doesn't, we have to recursively iterate the current child's children to find where they end
  78. TRY(current_child.for_each_child([&](DIE const& sub_child) -> ErrorOr<void> {
  79. sibling_offset = sub_child.offset() + sub_child.size();
  80. return {};
  81. }));
  82. }
  83. TRY(current_child.rehydrate_from(sibling_offset, m_offset));
  84. }
  85. return {};
  86. }
  87. }