DIE.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "DIE.h"
  27. #include "CompilationUnit.h"
  28. #include "DwarfInfo.h"
  29. #include <AK/ByteBuffer.h>
  30. #include <AK/MemoryStream.h>
  31. namespace Debug::Dwarf {
  32. DIE::DIE(const CompilationUnit& unit, u32 offset)
  33. : m_compilation_unit(unit)
  34. , m_offset(offset)
  35. {
  36. InputMemoryStream stream(m_compilation_unit.dwarf_info().debug_info_data());
  37. stream.discard_or_error(m_offset);
  38. stream.read_LEB128_unsigned(m_abbreviation_code);
  39. m_data_offset = stream.offset();
  40. if (m_abbreviation_code == 0) {
  41. // An abbreviation code of 0 ( = null DIE entry) means the end of a chain of siblings
  42. m_tag = EntryTag::None;
  43. } else {
  44. auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
  45. VERIFY(abbreviation_info.has_value());
  46. m_tag = abbreviation_info.value().tag;
  47. m_has_children = abbreviation_info.value().has_children;
  48. // We iterate the attributes data only to calculate this DIE's size
  49. for (auto& attribute_spec : abbreviation_info.value().attribute_specifications) {
  50. get_attribute_value(attribute_spec.form, stream);
  51. }
  52. }
  53. m_size = stream.offset() - m_offset;
  54. }
  55. DIE::AttributeValue DIE::get_attribute_value(AttributeDataForm form,
  56. InputMemoryStream& debug_info_stream) const
  57. {
  58. AttributeValue value;
  59. auto assign_raw_bytes_value = [&](size_t length) {
  60. value.data.as_raw_bytes.length = length;
  61. value.data.as_raw_bytes.bytes = reinterpret_cast<const u8*>(m_compilation_unit.dwarf_info().debug_info_data().data()
  62. + debug_info_stream.offset());
  63. debug_info_stream.discard_or_error(length);
  64. };
  65. switch (form) {
  66. case AttributeDataForm::StringPointer: {
  67. u32 offset;
  68. debug_info_stream >> offset;
  69. VERIFY(!debug_info_stream.has_any_error());
  70. value.type = AttributeValue::Type::String;
  71. auto strings_data = m_compilation_unit.dwarf_info().debug_strings_data();
  72. value.data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
  73. break;
  74. }
  75. case AttributeDataForm::Data1: {
  76. u8 data;
  77. debug_info_stream >> data;
  78. VERIFY(!debug_info_stream.has_any_error());
  79. value.type = AttributeValue::Type::UnsignedNumber;
  80. value.data.as_u32 = data;
  81. break;
  82. }
  83. case AttributeDataForm::Data2: {
  84. u16 data;
  85. debug_info_stream >> data;
  86. VERIFY(!debug_info_stream.has_any_error());
  87. value.type = AttributeValue::Type::UnsignedNumber;
  88. value.data.as_u32 = data;
  89. break;
  90. }
  91. case AttributeDataForm::Addr: {
  92. u32 address;
  93. debug_info_stream >> address;
  94. VERIFY(!debug_info_stream.has_any_error());
  95. value.type = AttributeValue::Type::UnsignedNumber;
  96. value.data.as_u32 = address;
  97. break;
  98. }
  99. case AttributeDataForm::SData: {
  100. ssize_t data;
  101. debug_info_stream.read_LEB128_signed(data);
  102. VERIFY(!debug_info_stream.has_any_error());
  103. value.type = AttributeValue::Type::SignedNumber;
  104. value.data.as_i32 = data;
  105. break;
  106. }
  107. case AttributeDataForm::SecOffset: {
  108. u32 data;
  109. debug_info_stream >> data;
  110. VERIFY(!debug_info_stream.has_any_error());
  111. value.type = AttributeValue::Type::SecOffset;
  112. value.data.as_u32 = data;
  113. break;
  114. }
  115. case AttributeDataForm::Data4: {
  116. u32 data;
  117. debug_info_stream >> data;
  118. VERIFY(!debug_info_stream.has_any_error());
  119. value.type = AttributeValue::Type::UnsignedNumber;
  120. value.data.as_u32 = data;
  121. break;
  122. }
  123. case AttributeDataForm::Data8: {
  124. u64 data;
  125. debug_info_stream >> data;
  126. VERIFY(!debug_info_stream.has_any_error());
  127. value.type = AttributeValue::Type::LongUnsignedNumber;
  128. value.data.as_u64 = data;
  129. break;
  130. }
  131. case AttributeDataForm::Ref4: {
  132. u32 data;
  133. debug_info_stream >> data;
  134. VERIFY(!debug_info_stream.has_any_error());
  135. value.type = AttributeValue::Type::DieReference;
  136. value.data.as_u32 = data + m_compilation_unit.offset();
  137. break;
  138. }
  139. case AttributeDataForm::FlagPresent: {
  140. value.type = AttributeValue::Type::Boolean;
  141. value.data.as_bool = true;
  142. break;
  143. }
  144. case AttributeDataForm::ExprLoc: {
  145. size_t length;
  146. debug_info_stream.read_LEB128_unsigned(length);
  147. VERIFY(!debug_info_stream.has_any_error());
  148. value.type = AttributeValue::Type::DwarfExpression;
  149. assign_raw_bytes_value(length);
  150. break;
  151. }
  152. case AttributeDataForm::String: {
  153. String str;
  154. u32 str_offset = debug_info_stream.offset();
  155. debug_info_stream >> str;
  156. VERIFY(!debug_info_stream.has_any_error());
  157. value.type = AttributeValue::Type::String;
  158. value.data.as_string = reinterpret_cast<const char*>(str_offset + m_compilation_unit.dwarf_info().debug_info_data().data());
  159. break;
  160. }
  161. case AttributeDataForm::Block1: {
  162. value.type = AttributeValue::Type::RawBytes;
  163. u8 length;
  164. debug_info_stream >> length;
  165. VERIFY(!debug_info_stream.has_any_error());
  166. assign_raw_bytes_value(length);
  167. break;
  168. }
  169. case AttributeDataForm::Block2: {
  170. value.type = AttributeValue::Type::RawBytes;
  171. u16 length;
  172. debug_info_stream >> length;
  173. VERIFY(!debug_info_stream.has_any_error());
  174. assign_raw_bytes_value(length);
  175. break;
  176. }
  177. case AttributeDataForm::Block4: {
  178. value.type = AttributeValue::Type::RawBytes;
  179. u32 length;
  180. debug_info_stream >> length;
  181. VERIFY(!debug_info_stream.has_any_error());
  182. assign_raw_bytes_value(length);
  183. break;
  184. }
  185. case AttributeDataForm::Block: {
  186. value.type = AttributeValue::Type::RawBytes;
  187. size_t length;
  188. debug_info_stream.read_LEB128_unsigned(length);
  189. VERIFY(!debug_info_stream.has_any_error());
  190. assign_raw_bytes_value(length);
  191. break;
  192. }
  193. default:
  194. dbgln("Unimplemented AttributeDataForm: {}", (u32)form);
  195. VERIFY_NOT_REACHED();
  196. }
  197. return value;
  198. }
  199. Optional<DIE::AttributeValue> DIE::get_attribute(const Attribute& attribute) const
  200. {
  201. InputMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
  202. stream.discard_or_error(m_data_offset);
  203. auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
  204. VERIFY(abbreviation_info.has_value());
  205. for (const auto& attribute_spec : abbreviation_info.value().attribute_specifications) {
  206. auto value = get_attribute_value(attribute_spec.form, stream);
  207. if (attribute_spec.attribute == attribute) {
  208. return value;
  209. }
  210. }
  211. return {};
  212. }
  213. void DIE::for_each_child(Function<void(const DIE& child)> callback) const
  214. {
  215. if (!m_has_children)
  216. return;
  217. NonnullOwnPtr<DIE> current_child = make<DIE>(m_compilation_unit, m_offset + m_size);
  218. while (true) {
  219. callback(*current_child);
  220. if (current_child->is_null())
  221. break;
  222. if (!current_child->has_children()) {
  223. current_child = make<DIE>(m_compilation_unit, current_child->offset() + current_child->size());
  224. continue;
  225. }
  226. auto sibling = current_child->get_attribute(Attribute::Sibling);
  227. u32 sibling_offset = 0;
  228. if (sibling.has_value()) {
  229. sibling_offset = sibling.value().data.as_u32;
  230. }
  231. if (!sibling.has_value()) {
  232. // NOTE: According to the spec, the compiler doesn't have to supply the sibling information.
  233. // When it doesn't, we have to recursively iterate the current child's children to find where they end
  234. current_child->for_each_child([&](const DIE& sub_child) {
  235. sibling_offset = sub_child.offset() + sub_child.size();
  236. });
  237. }
  238. current_child = make<DIE>(m_compilation_unit, sibling_offset);
  239. }
  240. }
  241. DIE DIE::get_die_at_offset(u32 offset) const
  242. {
  243. VERIFY(offset >= m_compilation_unit.offset() && offset < m_compilation_unit.offset() + m_compilation_unit.size());
  244. return DIE(m_compilation_unit, offset);
  245. }
  246. }