LineProgram.cpp 11 KB

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