LineProgram.cpp 11 KB

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