DebugInfo.cpp 13 KB

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