LineProgram.cpp 11 KB

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