DebugInfo.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. DebugInfo::DebugInfo(NonnullRefPtr<const ELF::Loader> elf)
  32. : m_elf(elf)
  33. , m_dwarf_info(Dwarf::DwarfInfo::create(m_elf))
  34. {
  35. prepare_variable_scopes();
  36. prepare_lines();
  37. }
  38. void DebugInfo::prepare_variable_scopes()
  39. {
  40. m_dwarf_info->for_each_compilation_unit([&](const Dwarf::CompilationUnit& unit) {
  41. auto root = unit.root_die();
  42. parse_scopes_impl(root);
  43. });
  44. }
  45. void DebugInfo::parse_scopes_impl(const Dwarf::DIE& die)
  46. {
  47. die.for_each_child([&](const Dwarf::DIE& child) {
  48. if (child.is_null())
  49. return;
  50. if (!(child.tag() == Dwarf::EntryTag::SubProgram || child.tag() == Dwarf::EntryTag::LexicalBlock))
  51. return;
  52. if (child.get_attribute(Dwarf::Attribute::Inline).has_value()) {
  53. dbg() << "DWARF inlined functions are not supported";
  54. return;
  55. }
  56. if (child.get_attribute(Dwarf::Attribute::Ranges).has_value()) {
  57. dbg() << "DWARF ranges are not supported";
  58. return;
  59. }
  60. auto name = child.get_attribute(Dwarf::Attribute::Name);
  61. VariablesScope scope {};
  62. scope.is_function = (child.tag() == Dwarf::EntryTag::SubProgram);
  63. if (name.has_value())
  64. scope.name = name.value().data.as_string;
  65. scope.address_low = child.get_attribute(Dwarf::Attribute::LowPc).value().data.as_u32;
  66. // The attribute name HighPc is confusing. In this context, it seems to actually be a positive offset from LowPc
  67. scope.address_high = scope.address_low + child.get_attribute(Dwarf::Attribute::HighPc).value().data.as_u32;
  68. child.for_each_child([&](const Dwarf::DIE& variable_entry) {
  69. if (variable_entry.tag() != Dwarf::EntryTag::Variable)
  70. return;
  71. scope.dies_of_variables.append(variable_entry);
  72. });
  73. m_scopes.append(scope);
  74. parse_scopes_impl(child);
  75. });
  76. }
  77. void DebugInfo::prepare_lines()
  78. {
  79. auto section = m_elf->image().lookup_section(".debug_line");
  80. ASSERT(!section.is_undefined());
  81. auto buffer = ByteBuffer::wrap(reinterpret_cast<const u8*>(section.raw_data()), section.size());
  82. BufferStream stream(buffer);
  83. Vector<LineProgram::LineInfo> all_lines;
  84. while (!stream.at_end()) {
  85. LineProgram program(stream);
  86. all_lines.append(program.lines());
  87. }
  88. for (auto& line_info : all_lines) {
  89. String file_path = line_info.file;
  90. if (file_path.contains("Toolchain/") || file_path.contains("libgcc"))
  91. continue;
  92. if (file_path.contains("serenity/")) {
  93. auto start_index = file_path.index_of("serenity/").value() + String("serenity/").length();
  94. file_path = file_path.substring(start_index, file_path.length() - start_index);
  95. }
  96. m_sorted_lines.append({ line_info.address, file_path, line_info.line });
  97. }
  98. quick_sort(m_sorted_lines, [](auto& a, auto& b) {
  99. return a.address < b.address;
  100. });
  101. }
  102. Optional<DebugInfo::SourcePosition> DebugInfo::get_source_position(u32 target_address) const
  103. {
  104. if (m_sorted_lines.is_empty())
  105. return {};
  106. if (target_address < m_sorted_lines[0].address)
  107. return {};
  108. // TODO: We can do a binray search here
  109. for (size_t i = 0; i < m_sorted_lines.size() - 1; ++i) {
  110. if (m_sorted_lines[i + 1].address > target_address) {
  111. return Optional<SourcePosition>({ m_sorted_lines[i].file, m_sorted_lines[i].line, m_sorted_lines[i].address });
  112. }
  113. }
  114. return {};
  115. }
  116. Optional<u32> DebugInfo::get_instruction_from_source(const String& file, size_t line) const
  117. {
  118. for (const auto& line_entry : m_sorted_lines) {
  119. if (line_entry.file == file && line_entry.line == line)
  120. return Optional<u32>(line_entry.address);
  121. }
  122. return {};
  123. }
  124. NonnullOwnPtrVector<DebugInfo::VariableInfo> DebugInfo::get_variables_in_current_scope(const PtraceRegisters& regs) const
  125. {
  126. auto scope = get_scope(regs.eip);
  127. if (!scope.has_value())
  128. return {};
  129. NonnullOwnPtrVector<DebugInfo::VariableInfo> variables;
  130. for (const auto& die_entry : scope.value().dies_of_variables) {
  131. variables.append(create_variable_info(die_entry, regs));
  132. }
  133. return variables;
  134. }
  135. Optional<DebugInfo::VariablesScope> DebugInfo::get_scope(u32 instruction_pointer) const
  136. {
  137. Optional<VariablesScope> best_matching_scope;
  138. // TODO: We can store the scopes in a better data strucutre
  139. for (const auto& scope : m_scopes) {
  140. if (instruction_pointer < scope.address_low || instruction_pointer >= scope.address_high)
  141. continue;
  142. if (!best_matching_scope.has_value()) {
  143. best_matching_scope = scope;
  144. } else if (scope.address_low > best_matching_scope.value().address_low || scope.address_high < best_matching_scope.value().address_high) {
  145. best_matching_scope = scope;
  146. }
  147. }
  148. return best_matching_scope;
  149. }
  150. NonnullOwnPtr<DebugInfo::VariableInfo> DebugInfo::create_variable_info(const Dwarf::DIE& variable_die, const PtraceRegisters& regs) const
  151. {
  152. ASSERT(variable_die.tag() == Dwarf::EntryTag::Variable || variable_die.tag() == Dwarf::EntryTag::Member);
  153. NonnullOwnPtr<VariableInfo> variable_info = make<VariableInfo>();
  154. variable_info->name = variable_die.get_attribute(Dwarf::Attribute::Name).value().data.as_string;
  155. auto type_die_offset = variable_die.get_attribute(Dwarf::Attribute::Type);
  156. ASSERT(type_die_offset.has_value());
  157. ASSERT(type_die_offset.value().type == Dwarf::DIE::AttributeValue::Type::DieReference);
  158. auto type_die = variable_die.get_die_at_offset(type_die_offset.value().data.as_u32);
  159. auto type_name = type_die.get_attribute(Dwarf::Attribute::Name);
  160. if (type_name.has_value()) {
  161. variable_info->type = type_name.value().data.as_string;
  162. } else {
  163. dbg() << "Unnamed DWRAF type at offset: " << type_die.offset();
  164. variable_info->name = "[Unnamed Type]";
  165. }
  166. auto location_info = variable_die.get_attribute(Dwarf::Attribute::Location);
  167. if (!location_info.has_value()) {
  168. location_info = variable_die.get_attribute(Dwarf::Attribute::MemberLocation);
  169. }
  170. if (location_info.has_value()) {
  171. if (location_info.value().type == Dwarf::DIE::AttributeValue::Type::UnsignedNumber) {
  172. variable_info->location_type = VariableInfo::LocationType::Address;
  173. variable_info->location_data.address = location_info.value().data.as_u32;
  174. }
  175. if (location_info.value().type == Dwarf::DIE::AttributeValue::Type::DwarfExpression) {
  176. auto expression_bytes = ByteBuffer::wrap(location_info.value().data.as_dwarf_expression.bytes, location_info.value().data.as_dwarf_expression.length);
  177. auto value = Dwarf::Expression::evaluate(expression_bytes, regs);
  178. if (value.type != Dwarf::Expression::Type::None) {
  179. ASSERT(value.type == Dwarf::Expression::Type::UnsignedIntetger);
  180. variable_info->location_type = VariableInfo::LocationType::Address;
  181. variable_info->location_data.address = value.data.as_u32;
  182. }
  183. }
  184. }
  185. type_die.for_each_child([&](const Dwarf::DIE& member) {
  186. if (member.is_null())
  187. return;
  188. auto member_variable = create_variable_info(member, regs);
  189. ASSERT(member_variable->location_type == DebugInfo::VariableInfo::LocationType::Address);
  190. ASSERT(variable_info->location_type == DebugInfo::VariableInfo::LocationType::Address);
  191. member_variable->location_data.address += variable_info->location_data.address;
  192. member_variable->parent = variable_info.ptr();
  193. variable_info->members.append(move(member_variable));
  194. });
  195. return variable_info;
  196. }