DebugInfo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 "DebugInfo.h"
  27. #include <AK/QuickSort.h>
  28. #include <LibDebug/Dwarf/CompilationUnit.h>
  29. #include <LibDebug/Dwarf/DwarfInfo.h>
  30. #include <LibDebug/Dwarf/Expression.h>
  31. //#define DEBUG_SPAM
  32. DebugInfo::DebugInfo(NonnullRefPtr<const ELF::Loader> elf)
  33. : m_elf(elf)
  34. , m_dwarf_info(Dwarf::DwarfInfo::create(m_elf))
  35. {
  36. prepare_variable_scopes();
  37. prepare_lines();
  38. }
  39. void DebugInfo::prepare_variable_scopes()
  40. {
  41. m_dwarf_info->for_each_compilation_unit([&](const Dwarf::CompilationUnit& unit) {
  42. auto root = unit.root_die();
  43. parse_scopes_impl(root);
  44. });
  45. }
  46. void DebugInfo::parse_scopes_impl(const Dwarf::DIE& die)
  47. {
  48. die.for_each_child([&](const Dwarf::DIE& child) {
  49. if (child.is_null())
  50. return;
  51. if (!(child.tag() == Dwarf::EntryTag::SubProgram || child.tag() == Dwarf::EntryTag::LexicalBlock))
  52. return;
  53. if (child.get_attribute(Dwarf::Attribute::Inline).has_value()) {
  54. #ifdef DEBUG_SPAM
  55. dbg() << "DWARF inlined functions are not supported";
  56. #endif
  57. return;
  58. }
  59. if (child.get_attribute(Dwarf::Attribute::Ranges).has_value()) {
  60. #ifdef DEBUG_SPAM
  61. dbg() << "DWARF ranges are not supported";
  62. #endif
  63. return;
  64. }
  65. auto name = child.get_attribute(Dwarf::Attribute::Name);
  66. VariablesScope scope {};
  67. scope.is_function = (child.tag() == Dwarf::EntryTag::SubProgram);
  68. if (name.has_value())
  69. scope.name = name.value().data.as_string;
  70. if (!child.get_attribute(Dwarf::Attribute::LowPc).has_value()) {
  71. #ifdef DEBUG_SPAM
  72. dbg() << "DWARF: Couldn't find attribtue LowPc for scope";
  73. #endif
  74. return;
  75. }
  76. scope.address_low = child.get_attribute(Dwarf::Attribute::LowPc).value().data.as_u32;
  77. // The attribute name HighPc is confusing. In this context, it seems to actually be a positive offset from LowPc
  78. scope.address_high = scope.address_low + child.get_attribute(Dwarf::Attribute::HighPc).value().data.as_u32;
  79. child.for_each_child([&](const Dwarf::DIE& variable_entry) {
  80. if (!(variable_entry.tag() == Dwarf::EntryTag::Variable
  81. || variable_entry.tag() == Dwarf::EntryTag::FormalParameter))
  82. return;
  83. scope.dies_of_variables.append(variable_entry);
  84. });
  85. m_scopes.append(scope);
  86. parse_scopes_impl(child);
  87. });
  88. }
  89. void DebugInfo::prepare_lines()
  90. {
  91. auto section = m_elf->image().lookup_section(".debug_line");
  92. if (section.is_undefined())
  93. return;
  94. auto buffer = section.wrapping_byte_buffer();
  95. BufferStream stream(buffer);
  96. Vector<LineProgram::LineInfo> all_lines;
  97. while (!stream.at_end()) {
  98. LineProgram program(stream);
  99. all_lines.append(program.lines());
  100. }
  101. for (auto& line_info : all_lines) {
  102. String file_path = line_info.file;
  103. if (file_path.contains("Toolchain/") || file_path.contains("libgcc"))
  104. continue;
  105. if (file_path.contains("serenity/")) {
  106. auto start_index = file_path.index_of("serenity/").value() + String("serenity/").length();
  107. file_path = file_path.substring(start_index, file_path.length() - start_index);
  108. }
  109. m_sorted_lines.append({ line_info.address, file_path, line_info.line });
  110. }
  111. quick_sort(m_sorted_lines, [](auto& a, auto& b) {
  112. return a.address < b.address;
  113. });
  114. }
  115. Optional<DebugInfo::SourcePosition> DebugInfo::get_source_position(u32 target_address) const
  116. {
  117. if (m_sorted_lines.is_empty())
  118. return {};
  119. if (target_address < m_sorted_lines[0].address)
  120. return {};
  121. // TODO: We can do a binray search here
  122. for (size_t i = 0; i < m_sorted_lines.size() - 1; ++i) {
  123. if (m_sorted_lines[i + 1].address > target_address) {
  124. return Optional<SourcePosition>({ m_sorted_lines[i].file, m_sorted_lines[i].line, m_sorted_lines[i].address });
  125. }
  126. }
  127. return {};
  128. }
  129. Optional<u32> DebugInfo::get_instruction_from_source(const String& file, size_t line) const
  130. {
  131. for (const auto& line_entry : m_sorted_lines) {
  132. if (line_entry.file == file && line_entry.line == line)
  133. return Optional<u32>(line_entry.address);
  134. }
  135. return {};
  136. }
  137. NonnullOwnPtrVector<DebugInfo::VariableInfo> DebugInfo::get_variables_in_current_scope(const PtraceRegisters& regs) const
  138. {
  139. NonnullOwnPtrVector<DebugInfo::VariableInfo> variables;
  140. // TODO: We can store the scopes in a better data strucutre
  141. for (const auto& scope : m_scopes) {
  142. if (regs.eip < scope.address_low || regs.eip >= scope.address_high)
  143. continue;
  144. for (const auto& die_entry : scope.dies_of_variables) {
  145. auto variable_info = create_variable_info(die_entry, regs);
  146. if (!variable_info)
  147. continue;
  148. variables.append(variable_info.release_nonnull());
  149. }
  150. }
  151. return variables;
  152. }
  153. static Optional<Dwarf::DIE> parse_variable_type_die(const Dwarf::DIE& variable_die, DebugInfo::VariableInfo& variable_info)
  154. {
  155. auto type_die_offset = variable_die.get_attribute(Dwarf::Attribute::Type);
  156. if (!type_die_offset.has_value())
  157. return {};
  158. ASSERT(type_die_offset.value().type == Dwarf::DIE::AttributeValue::Type::DieReference);
  159. auto type_die = variable_die.get_die_at_offset(type_die_offset.value().data.as_u32);
  160. auto type_name = type_die.get_attribute(Dwarf::Attribute::Name);
  161. if (type_name.has_value()) {
  162. variable_info.type_name = type_name.value().data.as_string;
  163. } else {
  164. dbg() << "Unnamed DWARF type at offset: " << type_die.offset();
  165. variable_info.name = "[Unnamed Type]";
  166. }
  167. return type_die;
  168. }
  169. static void parse_variable_location(const Dwarf::DIE& variable_die, DebugInfo::VariableInfo& variable_info, const PtraceRegisters& regs)
  170. {
  171. auto location_info = variable_die.get_attribute(Dwarf::Attribute::Location);
  172. if (!location_info.has_value())
  173. location_info = variable_die.get_attribute(Dwarf::Attribute::MemberLocation);
  174. if (location_info.has_value()) {
  175. if (location_info.value().type == Dwarf::DIE::AttributeValue::Type::UnsignedNumber) {
  176. variable_info.location_type = DebugInfo::VariableInfo::LocationType::Address;
  177. variable_info.location_data.address = location_info.value().data.as_u32;
  178. }
  179. if (location_info.value().type == Dwarf::DIE::AttributeValue::Type::DwarfExpression) {
  180. auto expression_bytes = ByteBuffer::wrap(location_info.value().data.as_dwarf_expression.bytes, location_info.value().data.as_dwarf_expression.length);
  181. auto value = Dwarf::Expression::evaluate(expression_bytes, regs);
  182. if (value.type != Dwarf::Expression::Type::None) {
  183. ASSERT(value.type == Dwarf::Expression::Type::UnsignedIntetger);
  184. variable_info.location_type = DebugInfo::VariableInfo::LocationType::Address;
  185. variable_info.location_data.address = value.data.as_u32;
  186. }
  187. }
  188. }
  189. }
  190. OwnPtr<DebugInfo::VariableInfo> DebugInfo::create_variable_info(const Dwarf::DIE& variable_die, const PtraceRegisters& regs) const
  191. {
  192. ASSERT(variable_die.tag() == Dwarf::EntryTag::Variable
  193. || variable_die.tag() == Dwarf::EntryTag::Member
  194. || variable_die.tag() == Dwarf::EntryTag::FormalParameter
  195. || variable_die.tag() == Dwarf::EntryTag::EnumerationType
  196. || variable_die.tag() == Dwarf::EntryTag::Enumerator
  197. || variable_die.tag() == Dwarf::EntryTag::StructureType);
  198. if (variable_die.tag() == Dwarf::EntryTag::FormalParameter
  199. && !variable_die.get_attribute(Dwarf::Attribute::Name).has_value()) {
  200. // We don't want to display info for unused paramters
  201. return {};
  202. }
  203. NonnullOwnPtr<VariableInfo> variable_info = make<VariableInfo>();
  204. variable_info->name = variable_die.get_attribute(Dwarf::Attribute::Name).value().data.as_string;
  205. auto type_die = parse_variable_type_die(variable_die, *variable_info);
  206. if (variable_die.tag() == Dwarf::EntryTag::Enumerator) {
  207. auto constant = variable_die.get_attribute(Dwarf::Attribute::ConstValue);
  208. ASSERT(constant.has_value());
  209. switch (constant.value().type) {
  210. case Dwarf::DIE::AttributeValue::Type::UnsignedNumber:
  211. variable_info->constant_data.as_u32 = constant.value().data.as_u32;
  212. break;
  213. case Dwarf::DIE::AttributeValue::Type::SignedNumber:
  214. variable_info->constant_data.as_i32 = constant.value().data.as_i32;
  215. break;
  216. case Dwarf::DIE::AttributeValue::Type::String:
  217. variable_info->constant_data.as_string = constant.value().data.as_string;
  218. break;
  219. default:
  220. ASSERT_NOT_REACHED();
  221. }
  222. } else {
  223. parse_variable_location(variable_die, *variable_info, regs);
  224. }
  225. if (type_die.has_value()) {
  226. OwnPtr<VariableInfo> type_info;
  227. if (type_die.value().tag() == Dwarf::EntryTag::EnumerationType || type_die.value().tag() == Dwarf::EntryTag::StructureType) {
  228. type_info = create_variable_info(type_die.value(), regs);
  229. }
  230. type_die.value().for_each_child([&](const Dwarf::DIE& member) {
  231. if (member.is_null())
  232. return;
  233. auto member_variable = create_variable_info(member, regs);
  234. ASSERT(member_variable);
  235. if (type_die.value().tag() == Dwarf::EntryTag::EnumerationType) {
  236. member_variable->parent = type_info.ptr();
  237. type_info->members.append(member_variable.release_nonnull());
  238. } else {
  239. ASSERT(variable_info->location_type == DebugInfo::VariableInfo::LocationType::Address);
  240. if (member_variable->location_type == DebugInfo::VariableInfo::LocationType::Address)
  241. member_variable->location_data.address += variable_info->location_data.address;
  242. member_variable->parent = variable_info.ptr();
  243. variable_info->members.append(member_variable.release_nonnull());
  244. }
  245. });
  246. if (type_info) {
  247. variable_info->type = move(type_info);
  248. variable_info->type->type_tag = type_die.value().tag();
  249. }
  250. }
  251. return variable_info;
  252. }
  253. String DebugInfo::name_of_containing_function(u32 address) const
  254. {
  255. for (const auto& scope : m_scopes) {
  256. if (!scope.is_function || address < scope.address_low || address >= scope.address_high)
  257. continue;
  258. return scope.name;
  259. }
  260. return {};
  261. }