DebugInfo.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "DebugInfo.h"
  7. #include <AK/Debug.h>
  8. #include <AK/LexicalPath.h>
  9. #include <AK/MemoryStream.h>
  10. #include <AK/QuickSort.h>
  11. #include <LibDebug/Dwarf/CompilationUnit.h>
  12. #include <LibDebug/Dwarf/DwarfInfo.h>
  13. #include <LibDebug/Dwarf/Expression.h>
  14. namespace Debug {
  15. DebugInfo::DebugInfo(NonnullOwnPtr<const ELF::Image> elf, String source_root, FlatPtr base_address)
  16. : m_elf(move(elf))
  17. , m_source_root(source_root)
  18. , m_base_address(base_address)
  19. , m_dwarf_info(*m_elf)
  20. {
  21. prepare_variable_scopes();
  22. prepare_lines();
  23. }
  24. void DebugInfo::prepare_variable_scopes()
  25. {
  26. m_dwarf_info.for_each_compilation_unit([&](const Dwarf::CompilationUnit& unit) {
  27. auto root = unit.root_die();
  28. parse_scopes_impl(root);
  29. });
  30. }
  31. void DebugInfo::parse_scopes_impl(const Dwarf::DIE& die)
  32. {
  33. die.for_each_child([&](const Dwarf::DIE& child) {
  34. if (child.is_null())
  35. return;
  36. if (!(child.tag() == Dwarf::EntryTag::SubProgram || child.tag() == Dwarf::EntryTag::LexicalBlock))
  37. return;
  38. if (child.get_attribute(Dwarf::Attribute::Inline).has_value()) {
  39. #if SPAM_DEBUG
  40. dbgln("DWARF inlined functions are not supported");
  41. #endif
  42. return;
  43. }
  44. if (child.get_attribute(Dwarf::Attribute::Ranges).has_value()) {
  45. #if SPAM_DEBUG
  46. dbgln("DWARF ranges are not supported");
  47. #endif
  48. return;
  49. }
  50. auto name = child.get_attribute(Dwarf::Attribute::Name);
  51. VariablesScope scope {};
  52. scope.is_function = (child.tag() == Dwarf::EntryTag::SubProgram);
  53. if (name.has_value())
  54. scope.name = name.value().data.as_string;
  55. if (!child.get_attribute(Dwarf::Attribute::LowPc).has_value()) {
  56. #if SPAM_DEBUG
  57. dbgln("DWARF: Couldn't find attribute LowPc for scope");
  58. #endif
  59. return;
  60. }
  61. scope.address_low = child.get_attribute(Dwarf::Attribute::LowPc).value().data.as_u32;
  62. // The attribute name HighPc is confusing. In this context, it seems to actually be a positive offset from LowPc
  63. scope.address_high = scope.address_low + child.get_attribute(Dwarf::Attribute::HighPc).value().data.as_u32;
  64. child.for_each_child([&](const Dwarf::DIE& variable_entry) {
  65. if (!(variable_entry.tag() == Dwarf::EntryTag::Variable
  66. || variable_entry.tag() == Dwarf::EntryTag::FormalParameter))
  67. return;
  68. scope.dies_of_variables.append(variable_entry);
  69. });
  70. m_scopes.append(scope);
  71. parse_scopes_impl(child);
  72. });
  73. }
  74. void DebugInfo::prepare_lines()
  75. {
  76. auto section = elf().lookup_section(".debug_line");
  77. if (section.is_undefined())
  78. return;
  79. InputMemoryStream stream { section.bytes() };
  80. Vector<Dwarf::LineProgram::LineInfo> all_lines;
  81. while (!stream.eof()) {
  82. Dwarf::LineProgram program(m_dwarf_info, stream);
  83. all_lines.append(program.lines());
  84. }
  85. String serenity_slash("serenity/");
  86. for (auto& line_info : all_lines) {
  87. String file_path = line_info.file;
  88. if (file_path.contains("Toolchain/") || file_path.contains("libgcc"))
  89. continue;
  90. if (file_path.contains(serenity_slash)) {
  91. auto start_index = file_path.index_of(serenity_slash).value() + serenity_slash.length();
  92. file_path = file_path.substring(start_index, file_path.length() - start_index);
  93. }
  94. if (file_path.starts_with("./") && !m_source_root.is_null()) {
  95. file_path = LexicalPath::canonicalized_path(String::formatted("{}/{}", m_source_root, file_path));
  96. }
  97. m_sorted_lines.append({ line_info.address, file_path, line_info.line });
  98. }
  99. quick_sort(m_sorted_lines, [](auto& a, auto& b) {
  100. return a.address < b.address;
  101. });
  102. }
  103. Optional<DebugInfo::SourcePosition> DebugInfo::get_source_position(u32 target_address) const
  104. {
  105. if (m_sorted_lines.is_empty())
  106. return {};
  107. if (target_address < m_sorted_lines[0].address)
  108. return {};
  109. // TODO: We can do a binray search here
  110. for (size_t i = 0; i < m_sorted_lines.size() - 1; ++i) {
  111. if (m_sorted_lines[i + 1].address > target_address) {
  112. return SourcePosition::from_line_info(m_sorted_lines[i]);
  113. }
  114. }
  115. return {};
  116. }
  117. Optional<DebugInfo::SourcePositionAndAddress> DebugInfo::get_address_from_source_position(const String& file, size_t line) const
  118. {
  119. String file_path = file;
  120. if (!file_path.starts_with("/"))
  121. file_path = String::formatted("/{}", file_path);
  122. constexpr char SERENITY_LIBS_PREFIX[] = "/usr/src/serenity";
  123. if (file.starts_with(SERENITY_LIBS_PREFIX)) {
  124. file_path = file.substring(sizeof(SERENITY_LIBS_PREFIX), file.length() - sizeof(SERENITY_LIBS_PREFIX));
  125. file_path = String::formatted("../{}", file_path);
  126. }
  127. Optional<SourcePositionAndAddress> result;
  128. for (const auto& line_entry : m_sorted_lines) {
  129. if (!line_entry.file.ends_with(file_path))
  130. continue;
  131. if (line_entry.line > line)
  132. continue;
  133. // We look for the source position that is closest to the desired position, and is not after it.
  134. // For example, get_address_of_source_position("main.cpp", 73) could return the address for an instruction whose location is ("main.cpp", 72)
  135. // as there might not be an instruction mapped for "main.cpp", 73.
  136. if (!result.has_value() || (line_entry.line > result.value().line)) {
  137. result = SourcePositionAndAddress { line_entry.file, line_entry.line, line_entry.address };
  138. }
  139. }
  140. return result;
  141. }
  142. NonnullOwnPtrVector<DebugInfo::VariableInfo> DebugInfo::get_variables_in_current_scope(const PtraceRegisters& regs) const
  143. {
  144. NonnullOwnPtrVector<DebugInfo::VariableInfo> variables;
  145. // TODO: We can store the scopes in a better data structure
  146. for (const auto& scope : m_scopes) {
  147. if (regs.eip - m_base_address < scope.address_low || regs.eip - m_base_address >= scope.address_high)
  148. continue;
  149. for (const auto& die_entry : scope.dies_of_variables) {
  150. auto variable_info = create_variable_info(die_entry, regs);
  151. if (!variable_info)
  152. continue;
  153. variables.append(variable_info.release_nonnull());
  154. }
  155. }
  156. return variables;
  157. }
  158. static Optional<Dwarf::DIE> parse_variable_type_die(const Dwarf::DIE& variable_die, DebugInfo::VariableInfo& variable_info)
  159. {
  160. auto type_die_offset = variable_die.get_attribute(Dwarf::Attribute::Type);
  161. if (!type_die_offset.has_value())
  162. return {};
  163. VERIFY(type_die_offset.value().type == Dwarf::AttributeValue::Type::DieReference);
  164. auto type_die = variable_die.get_die_at_offset(type_die_offset.value().data.as_u32);
  165. auto type_name = type_die.get_attribute(Dwarf::Attribute::Name);
  166. if (type_name.has_value()) {
  167. variable_info.type_name = type_name.value().data.as_string;
  168. } else {
  169. dbgln("Unnamed DWARF type at offset: {}", type_die.offset());
  170. variable_info.type_name = "[Unnamed Type]";
  171. }
  172. return type_die;
  173. }
  174. static void parse_variable_location(const Dwarf::DIE& variable_die, DebugInfo::VariableInfo& variable_info, const PtraceRegisters& regs)
  175. {
  176. auto location_info = variable_die.get_attribute(Dwarf::Attribute::Location);
  177. if (!location_info.has_value()) {
  178. location_info = variable_die.get_attribute(Dwarf::Attribute::MemberLocation);
  179. }
  180. if (!location_info.has_value())
  181. return;
  182. switch (location_info.value().type) {
  183. case Dwarf::AttributeValue::Type::UnsignedNumber:
  184. variable_info.location_type = DebugInfo::VariableInfo::LocationType::Address;
  185. variable_info.location_data.address = location_info.value().data.as_u32;
  186. break;
  187. case Dwarf::AttributeValue::Type::DwarfExpression: {
  188. auto expression_bytes = ReadonlyBytes { location_info.value().data.as_raw_bytes.bytes, location_info.value().data.as_raw_bytes.length };
  189. auto value = Dwarf::Expression::evaluate(expression_bytes, regs);
  190. if (value.type != Dwarf::Expression::Type::None) {
  191. VERIFY(value.type == Dwarf::Expression::Type::UnsignedIntetger);
  192. variable_info.location_type = DebugInfo::VariableInfo::LocationType::Address;
  193. variable_info.location_data.address = value.data.as_u32;
  194. }
  195. break;
  196. }
  197. default:
  198. dbgln("Warning: unhandled Dwarf location type: {}", (int)location_info.value().type);
  199. }
  200. }
  201. OwnPtr<DebugInfo::VariableInfo> DebugInfo::create_variable_info(const Dwarf::DIE& variable_die, const PtraceRegisters& regs, u32 address_offset) const
  202. {
  203. VERIFY(is_variable_tag_supported(variable_die.tag()));
  204. if (variable_die.tag() == Dwarf::EntryTag::FormalParameter
  205. && !variable_die.get_attribute(Dwarf::Attribute::Name).has_value()) {
  206. // We don't want to display info for unused parameters
  207. return {};
  208. }
  209. NonnullOwnPtr<VariableInfo> variable_info = make<VariableInfo>();
  210. auto name_attribute = variable_die.get_attribute(Dwarf::Attribute::Name);
  211. if (name_attribute.has_value())
  212. variable_info->name = name_attribute.value().data.as_string;
  213. auto type_die = parse_variable_type_die(variable_die, *variable_info);
  214. if (variable_die.tag() == Dwarf::EntryTag::Enumerator) {
  215. auto constant = variable_die.get_attribute(Dwarf::Attribute::ConstValue);
  216. VERIFY(constant.has_value());
  217. switch (constant.value().type) {
  218. case Dwarf::AttributeValue::Type::UnsignedNumber:
  219. variable_info->constant_data.as_u32 = constant.value().data.as_u32;
  220. break;
  221. case Dwarf::AttributeValue::Type::SignedNumber:
  222. variable_info->constant_data.as_i32 = constant.value().data.as_i32;
  223. break;
  224. case Dwarf::AttributeValue::Type::String:
  225. variable_info->constant_data.as_string = constant.value().data.as_string;
  226. break;
  227. default:
  228. VERIFY_NOT_REACHED();
  229. }
  230. } else {
  231. parse_variable_location(variable_die, *variable_info, regs);
  232. variable_info->location_data.address += address_offset;
  233. }
  234. if (type_die.has_value())
  235. add_type_info_to_variable(type_die.value(), regs, variable_info);
  236. return variable_info;
  237. }
  238. void DebugInfo::add_type_info_to_variable(const Dwarf::DIE& type_die, const PtraceRegisters& regs, DebugInfo::VariableInfo* parent_variable) const
  239. {
  240. OwnPtr<VariableInfo> type_info;
  241. auto is_array_type = type_die.tag() == Dwarf::EntryTag::ArrayType;
  242. if (type_die.tag() == Dwarf::EntryTag::EnumerationType
  243. || type_die.tag() == Dwarf::EntryTag::StructureType
  244. || is_array_type) {
  245. type_info = create_variable_info(type_die, regs);
  246. }
  247. type_die.for_each_child([&](const Dwarf::DIE& member) {
  248. if (member.is_null())
  249. return;
  250. if (is_array_type && member.tag() == Dwarf::EntryTag::SubRangeType) {
  251. auto upper_bound = member.get_attribute(Dwarf::Attribute::UpperBound);
  252. VERIFY(upper_bound.has_value());
  253. auto size = upper_bound.value().data.as_u32 + 1;
  254. type_info->dimension_sizes.append(size);
  255. return;
  256. }
  257. if (!is_variable_tag_supported(member.tag()))
  258. return;
  259. auto member_variable = create_variable_info(member, regs, parent_variable->location_data.address);
  260. VERIFY(member_variable);
  261. if (type_die.tag() == Dwarf::EntryTag::EnumerationType) {
  262. member_variable->parent = type_info.ptr();
  263. type_info->members.append(member_variable.release_nonnull());
  264. } else {
  265. if (parent_variable->location_type != DebugInfo::VariableInfo::LocationType::Address)
  266. return;
  267. member_variable->parent = parent_variable;
  268. parent_variable->members.append(member_variable.release_nonnull());
  269. }
  270. });
  271. if (type_info) {
  272. if (is_array_type) {
  273. StringBuilder array_type_name;
  274. array_type_name.append(type_info->type_name);
  275. for (auto array_size : type_info->dimension_sizes) {
  276. array_type_name.append("[");
  277. array_type_name.append(String::formatted("{:d}", array_size));
  278. array_type_name.append("]");
  279. }
  280. parent_variable->type_name = array_type_name.to_string();
  281. }
  282. parent_variable->type = move(type_info);
  283. parent_variable->type->type_tag = type_die.tag();
  284. }
  285. }
  286. bool DebugInfo::is_variable_tag_supported(const Dwarf::EntryTag& tag)
  287. {
  288. return tag == Dwarf::EntryTag::Variable
  289. || tag == Dwarf::EntryTag::Member
  290. || tag == Dwarf::EntryTag::FormalParameter
  291. || tag == Dwarf::EntryTag::EnumerationType
  292. || tag == Dwarf::EntryTag::Enumerator
  293. || tag == Dwarf::EntryTag::StructureType
  294. || tag == Dwarf::EntryTag::ArrayType;
  295. }
  296. String DebugInfo::name_of_containing_function(u32 address) const
  297. {
  298. auto function = get_containing_function(address);
  299. if (!function.has_value())
  300. return {};
  301. return function.value().name;
  302. }
  303. Optional<DebugInfo::VariablesScope> DebugInfo::get_containing_function(u32 address) const
  304. {
  305. for (const auto& scope : m_scopes) {
  306. if (!scope.is_function || address < scope.address_low || address >= scope.address_high)
  307. continue;
  308. return scope;
  309. }
  310. return {};
  311. }
  312. Vector<DebugInfo::SourcePosition> DebugInfo::source_lines_in_scope(const VariablesScope& scope) const
  313. {
  314. Vector<DebugInfo::SourcePosition> source_lines;
  315. for (const auto& line : m_sorted_lines) {
  316. if (line.address < scope.address_low)
  317. continue;
  318. if (line.address >= scope.address_high)
  319. break;
  320. source_lines.append(SourcePosition::from_line_info(line));
  321. }
  322. return source_lines;
  323. }
  324. DebugInfo::SourcePosition DebugInfo::SourcePosition::from_line_info(const Dwarf::LineProgram::LineInfo& line)
  325. {
  326. return { line.file, line.line, { line.address } };
  327. }
  328. }