LineProgram.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "LineProgram.h"
  7. #include <AK/Debug.h>
  8. #include <AK/Function.h>
  9. #include <AK/LEB128.h>
  10. #include <AK/StringBuilder.h>
  11. #include <LibDebug/Dwarf/DwarfInfo.h>
  12. namespace Debug::Dwarf {
  13. LineProgram::LineProgram(DwarfInfo& dwarf_info, SeekableStream& stream)
  14. : m_dwarf_info(dwarf_info)
  15. , m_stream(stream)
  16. {
  17. m_unit_offset = m_stream.tell().release_value_but_fixme_should_propagate_errors();
  18. parse_unit_header().release_value_but_fixme_should_propagate_errors();
  19. parse_source_directories().release_value_but_fixme_should_propagate_errors();
  20. parse_source_files().release_value_but_fixme_should_propagate_errors();
  21. run_program().release_value_but_fixme_should_propagate_errors();
  22. }
  23. ErrorOr<void> LineProgram::parse_unit_header()
  24. {
  25. m_unit_header = TRY(m_stream.read_value<LineProgramUnitHeader32>());
  26. VERIFY(m_unit_header.version() >= MIN_DWARF_VERSION && m_unit_header.version() <= MAX_DWARF_VERSION);
  27. VERIFY(m_unit_header.opcode_base() <= sizeof(m_unit_header.std_opcode_lengths) / sizeof(m_unit_header.std_opcode_lengths[0]) + 1);
  28. dbgln_if(DWARF_DEBUG, "unit length: {}", m_unit_header.length());
  29. return {};
  30. }
  31. ErrorOr<void> LineProgram::parse_path_entries(Function<void(PathEntry& entry)> callback, PathListType list_type)
  32. {
  33. if (m_unit_header.version() >= 5) {
  34. auto path_entry_format_count = TRY(m_stream.read_value<u8>());
  35. Vector<PathEntryFormat> format_descriptions;
  36. for (u8 i = 0; i < path_entry_format_count; i++) {
  37. UnderlyingType<ContentType> content_type = TRY(m_stream.read_value<LEB128<UnderlyingType<ContentType>>>());
  38. UnderlyingType<AttributeDataForm> data_form = TRY(m_stream.read_value<LEB128<UnderlyingType<AttributeDataForm>>>());
  39. format_descriptions.empend(static_cast<ContentType>(content_type), static_cast<AttributeDataForm>(data_form));
  40. }
  41. size_t paths_count = TRY(m_stream.read_value<LEB128<size_t>>());
  42. for (size_t i = 0; i < paths_count; i++) {
  43. PathEntry entry;
  44. for (auto& format_description : format_descriptions) {
  45. auto value = TRY(m_dwarf_info.get_attribute_value(format_description.form, 0, m_stream));
  46. switch (format_description.type) {
  47. case ContentType::Path:
  48. entry.path = TRY(value.as_string());
  49. break;
  50. case ContentType::DirectoryIndex:
  51. entry.directory_index = value.as_unsigned();
  52. break;
  53. default:
  54. dbgln_if(DWARF_DEBUG, "Unhandled path list attribute: {}", to_underlying(format_description.type));
  55. }
  56. }
  57. callback(entry);
  58. }
  59. } else {
  60. while (true) {
  61. StringBuilder builder;
  62. while (auto c = TRY(m_stream.read_value<char>()))
  63. TRY(builder.try_append(c));
  64. auto path = builder.to_deprecated_string();
  65. if (path.length() == 0)
  66. break;
  67. dbgln_if(DWARF_DEBUG, "path: {}", path);
  68. PathEntry entry;
  69. entry.path = path;
  70. if (list_type == PathListType::Filenames) {
  71. size_t directory_index = TRY(m_stream.read_value<LEB128<size_t>>());
  72. TRY(m_stream.read_value<LEB128<size_t>>()); // skip modification time
  73. TRY(m_stream.read_value<LEB128<size_t>>()); // skip file size
  74. entry.directory_index = directory_index;
  75. dbgln_if(DWARF_DEBUG, "file: {}, directory index: {}", path, directory_index);
  76. }
  77. callback(entry);
  78. }
  79. }
  80. return {};
  81. }
  82. ErrorOr<void> LineProgram::parse_source_directories()
  83. {
  84. if (m_unit_header.version() < 5) {
  85. m_source_directories.append(".");
  86. }
  87. TRY(parse_path_entries([this](PathEntry& entry) {
  88. m_source_directories.append(entry.path);
  89. },
  90. PathListType::Directories));
  91. return {};
  92. }
  93. ErrorOr<void> LineProgram::parse_source_files()
  94. {
  95. if (m_unit_header.version() < 5) {
  96. m_source_files.append({ ".", 0 });
  97. }
  98. TRY(parse_path_entries([this](PathEntry& entry) {
  99. m_source_files.append({ entry.path, entry.directory_index });
  100. },
  101. PathListType::Filenames));
  102. return {};
  103. }
  104. void LineProgram::append_to_line_info()
  105. {
  106. dbgln_if(DWARF_DEBUG, "appending line info: {:p}, {}:{}", m_address, m_source_files[m_file_index].name, m_line);
  107. if (!m_is_statement)
  108. return;
  109. if (m_file_index >= m_source_files.size())
  110. return;
  111. auto const& directory = m_source_directories[m_source_files[m_file_index].directory_index];
  112. StringBuilder full_path(directory.length() + m_source_files[m_file_index].name.length() + 1);
  113. full_path.append(directory);
  114. full_path.append('/');
  115. full_path.append(m_source_files[m_file_index].name);
  116. m_lines.append({ m_address, DeprecatedFlyString { full_path.string_view() }, m_line });
  117. }
  118. void LineProgram::reset_registers()
  119. {
  120. m_address = 0;
  121. m_line = 1;
  122. m_file_index = 1;
  123. m_is_statement = m_unit_header.default_is_stmt() == 1;
  124. }
  125. ErrorOr<void> LineProgram::handle_extended_opcode()
  126. {
  127. size_t length = TRY(m_stream.read_value<LEB128<size_t>>());
  128. auto sub_opcode = TRY(m_stream.read_value<u8>());
  129. switch (sub_opcode) {
  130. case ExtendedOpcodes::EndSequence: {
  131. append_to_line_info();
  132. reset_registers();
  133. break;
  134. }
  135. case ExtendedOpcodes::SetAddress: {
  136. VERIFY(length == sizeof(size_t) + 1);
  137. m_address = TRY(m_stream.read_value<FlatPtr>());
  138. dbgln_if(DWARF_DEBUG, "SetAddress: {:p}", m_address);
  139. break;
  140. }
  141. case ExtendedOpcodes::SetDiscriminator: {
  142. dbgln_if(DWARF_DEBUG, "SetDiscriminator");
  143. [[maybe_unused]] size_t discriminator = TRY(m_stream.read_value<LEB128<size_t>>());
  144. break;
  145. }
  146. default:
  147. dbgln("Encountered unknown sub opcode {} at stream offset {:p}", sub_opcode, TRY(m_stream.tell()));
  148. VERIFY_NOT_REACHED();
  149. }
  150. return {};
  151. }
  152. ErrorOr<void> LineProgram::handle_standard_opcode(u8 opcode)
  153. {
  154. switch (opcode) {
  155. case StandardOpcodes::Copy: {
  156. append_to_line_info();
  157. break;
  158. }
  159. case StandardOpcodes::AdvancePc: {
  160. size_t operand = TRY(m_stream.read_value<LEB128<size_t>>());
  161. size_t delta = operand * m_unit_header.min_instruction_length();
  162. dbgln_if(DWARF_DEBUG, "AdvancePC by: {} to: {:p}", delta, m_address + delta);
  163. m_address += delta;
  164. break;
  165. }
  166. case StandardOpcodes::SetFile: {
  167. size_t new_file_index = TRY(m_stream.read_value<LEB128<size_t>>());
  168. dbgln_if(DWARF_DEBUG, "SetFile: new file index: {}", new_file_index);
  169. m_file_index = new_file_index;
  170. break;
  171. }
  172. case StandardOpcodes::SetColumn: {
  173. // not implemented
  174. dbgln_if(DWARF_DEBUG, "SetColumn");
  175. [[maybe_unused]] size_t new_column = TRY(m_stream.read_value<LEB128<size_t>>());
  176. break;
  177. }
  178. case StandardOpcodes::AdvanceLine: {
  179. ssize_t line_delta = TRY(m_stream.read_value<LEB128<ssize_t>>());
  180. VERIFY(line_delta >= 0 || m_line >= (size_t)(-line_delta));
  181. m_line += line_delta;
  182. dbgln_if(DWARF_DEBUG, "AdvanceLine: {}", m_line);
  183. break;
  184. }
  185. case StandardOpcodes::NegateStatement: {
  186. dbgln_if(DWARF_DEBUG, "NegateStatement");
  187. m_is_statement = !m_is_statement;
  188. break;
  189. }
  190. case StandardOpcodes::ConstAddPc: {
  191. u8 adjusted_opcode = 255 - m_unit_header.opcode_base();
  192. ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range()) * m_unit_header.min_instruction_length();
  193. address_increment *= m_unit_header.min_instruction_length();
  194. dbgln_if(DWARF_DEBUG, "ConstAddPc: advance pc by: {} to: {}", address_increment, (m_address + address_increment));
  195. m_address += address_increment;
  196. break;
  197. }
  198. case StandardOpcodes::SetIsa: {
  199. size_t isa = TRY(m_stream.read_value<LEB128<size_t>>());
  200. dbgln_if(DWARF_DEBUG, "SetIsa: {}", isa);
  201. break;
  202. }
  203. case StandardOpcodes::FixAdvancePc: {
  204. auto delta = TRY(m_stream.read_value<u16>());
  205. dbgln_if(DWARF_DEBUG, "FixAdvancePC by: {} to: {:p}", delta, m_address + delta);
  206. m_address += delta;
  207. break;
  208. }
  209. case StandardOpcodes::SetBasicBlock: {
  210. m_basic_block = true;
  211. break;
  212. }
  213. case StandardOpcodes::SetPrologueEnd: {
  214. m_prologue_end = true;
  215. break;
  216. }
  217. default:
  218. dbgln("Unhandled LineProgram opcode {}", opcode);
  219. VERIFY_NOT_REACHED();
  220. }
  221. return {};
  222. }
  223. void LineProgram::handle_special_opcode(u8 opcode)
  224. {
  225. u8 adjusted_opcode = opcode - m_unit_header.opcode_base();
  226. ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range()) * m_unit_header.min_instruction_length();
  227. ssize_t line_increment = m_unit_header.line_base() + (adjusted_opcode % m_unit_header.line_range());
  228. m_address += address_increment;
  229. m_line += line_increment;
  230. if constexpr (DWARF_DEBUG) {
  231. dbgln("Special adjusted_opcode: {}, address_increment: {}, line_increment: {}", adjusted_opcode, address_increment, line_increment);
  232. dbgln("Address is now: {:p}, and line is: {}:{}", m_address, m_source_files[m_file_index].name, m_line);
  233. }
  234. append_to_line_info();
  235. m_basic_block = false;
  236. m_prologue_end = false;
  237. }
  238. ErrorOr<void> LineProgram::run_program()
  239. {
  240. reset_registers();
  241. while (TRY(m_stream.tell()) < m_unit_offset + sizeof(u32) + m_unit_header.length()) {
  242. auto opcode = TRY(m_stream.read_value<u8>());
  243. dbgln_if(DWARF_DEBUG, "{:p}: opcode: {}", TRY(m_stream.tell()) - 1, opcode);
  244. if (opcode == 0) {
  245. TRY(handle_extended_opcode());
  246. } else if (opcode >= 1 && opcode <= 12) {
  247. TRY(handle_standard_opcode(opcode));
  248. } else {
  249. handle_special_opcode(opcode);
  250. }
  251. }
  252. return {};
  253. }
  254. LineProgram::DirectoryAndFile LineProgram::get_directory_and_file(size_t file_index) const
  255. {
  256. VERIFY(file_index < m_source_files.size());
  257. auto file_entry = m_source_files[file_index];
  258. VERIFY(file_entry.directory_index < m_source_directories.size());
  259. auto directory_entry = m_source_directories[file_entry.directory_index];
  260. return { directory_entry, file_entry.name };
  261. }
  262. bool LineProgram::looks_like_embedded_resource() const
  263. {
  264. if (source_files().size() == 1)
  265. return source_files()[0].name.view().contains("serenity_icon_"sv);
  266. if (source_files().size() == 2 && source_files()[0].name.view() == "."sv)
  267. return source_files()[1].name.view().contains("serenity_icon_"sv);
  268. return false;
  269. }
  270. }