LineProgram.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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, size_t unit_offset)
  14. : m_dwarf_info(dwarf_info)
  15. , m_stream(stream)
  16. , m_unit_offset(unit_offset)
  17. {
  18. }
  19. ErrorOr<NonnullOwnPtr<LineProgram>> LineProgram::create(DwarfInfo& dwarf_info, SeekableStream& stream)
  20. {
  21. auto offset = TRY(stream.tell());
  22. auto program = TRY(adopt_nonnull_own_or_enomem(new (nothrow) LineProgram(dwarf_info, stream, offset)));
  23. TRY(program->parse_unit_header());
  24. TRY(program->parse_source_directories());
  25. TRY(program->parse_source_files());
  26. TRY(program->run_program());
  27. return program;
  28. }
  29. ErrorOr<void> LineProgram::parse_unit_header()
  30. {
  31. m_unit_header = TRY(m_stream.read_value<LineProgramUnitHeader32>());
  32. VERIFY(m_unit_header.version() >= MIN_DWARF_VERSION && m_unit_header.version() <= MAX_DWARF_VERSION);
  33. VERIFY(m_unit_header.opcode_base() <= sizeof(m_unit_header.std_opcode_lengths) / sizeof(m_unit_header.std_opcode_lengths[0]) + 1);
  34. dbgln_if(DWARF_DEBUG, "unit length: {}", m_unit_header.length());
  35. return {};
  36. }
  37. ErrorOr<void> LineProgram::parse_path_entries(Function<void(PathEntry& entry)> callback, PathListType list_type)
  38. {
  39. if (m_unit_header.version() >= 5) {
  40. auto path_entry_format_count = TRY(m_stream.read_value<u8>());
  41. Vector<PathEntryFormat> format_descriptions;
  42. for (u8 i = 0; i < path_entry_format_count; i++) {
  43. UnderlyingType<ContentType> content_type = TRY(m_stream.read_value<LEB128<UnderlyingType<ContentType>>>());
  44. UnderlyingType<AttributeDataForm> data_form = TRY(m_stream.read_value<LEB128<UnderlyingType<AttributeDataForm>>>());
  45. format_descriptions.empend(static_cast<ContentType>(content_type), static_cast<AttributeDataForm>(data_form));
  46. }
  47. size_t paths_count = TRY(m_stream.read_value<LEB128<size_t>>());
  48. for (size_t i = 0; i < paths_count; i++) {
  49. PathEntry entry;
  50. for (auto& format_description : format_descriptions) {
  51. auto value = TRY(m_dwarf_info.get_attribute_value(format_description.form, 0, m_stream));
  52. switch (format_description.type) {
  53. case ContentType::Path:
  54. entry.path = TRY(value.as_string());
  55. break;
  56. case ContentType::DirectoryIndex:
  57. entry.directory_index = value.as_unsigned();
  58. break;
  59. default:
  60. dbgln_if(DWARF_DEBUG, "Unhandled path list attribute: {}", to_underlying(format_description.type));
  61. }
  62. }
  63. callback(entry);
  64. }
  65. } else {
  66. while (true) {
  67. StringBuilder builder;
  68. while (auto c = TRY(m_stream.read_value<char>()))
  69. TRY(builder.try_append(c));
  70. auto path = builder.to_byte_string();
  71. if (path.length() == 0)
  72. break;
  73. dbgln_if(DWARF_DEBUG, "path: {}", path);
  74. PathEntry entry;
  75. entry.path = path;
  76. if (list_type == PathListType::Filenames) {
  77. size_t directory_index = TRY(m_stream.read_value<LEB128<size_t>>());
  78. TRY(m_stream.read_value<LEB128<size_t>>()); // skip modification time
  79. TRY(m_stream.read_value<LEB128<size_t>>()); // 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 = TRY(m_stream.read_value<LEB128<size_t>>());
  134. auto sub_opcode = TRY(m_stream.read_value<u8>());
  135. switch (sub_opcode) {
  136. case ExtendedOpcodes::EndSequence: {
  137. append_to_line_info();
  138. reset_registers();
  139. break;
  140. }
  141. case ExtendedOpcodes::SetAddress: {
  142. VERIFY(length == sizeof(size_t) + 1);
  143. m_address = TRY(m_stream.read_value<FlatPtr>());
  144. dbgln_if(DWARF_DEBUG, "SetAddress: {:p}", m_address);
  145. break;
  146. }
  147. case ExtendedOpcodes::SetDiscriminator: {
  148. dbgln_if(DWARF_DEBUG, "SetDiscriminator");
  149. [[maybe_unused]] size_t discriminator = TRY(m_stream.read_value<LEB128<size_t>>());
  150. break;
  151. }
  152. default:
  153. dbgln("Encountered unknown sub opcode {} at stream offset {:p}", sub_opcode, TRY(m_stream.tell()));
  154. VERIFY_NOT_REACHED();
  155. }
  156. return {};
  157. }
  158. ErrorOr<void> LineProgram::handle_standard_opcode(u8 opcode)
  159. {
  160. switch (opcode) {
  161. case StandardOpcodes::Copy: {
  162. append_to_line_info();
  163. break;
  164. }
  165. case StandardOpcodes::AdvancePc: {
  166. size_t operand = TRY(m_stream.read_value<LEB128<size_t>>());
  167. size_t delta = operand * m_unit_header.min_instruction_length();
  168. dbgln_if(DWARF_DEBUG, "AdvancePC by: {} to: {:p}", delta, m_address + delta);
  169. m_address += delta;
  170. break;
  171. }
  172. case StandardOpcodes::SetFile: {
  173. size_t new_file_index = TRY(m_stream.read_value<LEB128<size_t>>());
  174. dbgln_if(DWARF_DEBUG, "SetFile: new file index: {}", new_file_index);
  175. m_file_index = new_file_index;
  176. break;
  177. }
  178. case StandardOpcodes::SetColumn: {
  179. // not implemented
  180. dbgln_if(DWARF_DEBUG, "SetColumn");
  181. [[maybe_unused]] size_t new_column = TRY(m_stream.read_value<LEB128<size_t>>());
  182. break;
  183. }
  184. case StandardOpcodes::AdvanceLine: {
  185. ssize_t line_delta = TRY(m_stream.read_value<LEB128<ssize_t>>());
  186. VERIFY(line_delta >= 0 || m_line >= (size_t)(-line_delta));
  187. m_line += line_delta;
  188. dbgln_if(DWARF_DEBUG, "AdvanceLine: {}", m_line);
  189. break;
  190. }
  191. case StandardOpcodes::NegateStatement: {
  192. dbgln_if(DWARF_DEBUG, "NegateStatement");
  193. m_is_statement = !m_is_statement;
  194. break;
  195. }
  196. case StandardOpcodes::ConstAddPc: {
  197. u8 adjusted_opcode = 255 - m_unit_header.opcode_base();
  198. ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range()) * m_unit_header.min_instruction_length();
  199. address_increment *= m_unit_header.min_instruction_length();
  200. dbgln_if(DWARF_DEBUG, "ConstAddPc: advance pc by: {} to: {}", address_increment, (m_address + address_increment));
  201. m_address += address_increment;
  202. break;
  203. }
  204. case StandardOpcodes::SetIsa: {
  205. size_t isa = TRY(m_stream.read_value<LEB128<size_t>>());
  206. dbgln_if(DWARF_DEBUG, "SetIsa: {}", isa);
  207. break;
  208. }
  209. case StandardOpcodes::FixAdvancePc: {
  210. auto delta = TRY(m_stream.read_value<u16>());
  211. dbgln_if(DWARF_DEBUG, "FixAdvancePC by: {} to: {:p}", delta, m_address + delta);
  212. m_address += delta;
  213. break;
  214. }
  215. case StandardOpcodes::SetBasicBlock: {
  216. m_basic_block = true;
  217. break;
  218. }
  219. case StandardOpcodes::SetPrologueEnd: {
  220. m_prologue_end = true;
  221. break;
  222. }
  223. case StandardOpcodes::SetEpilogueBegin: {
  224. m_epilogue_begin = true;
  225. break;
  226. }
  227. default:
  228. dbgln("Unhandled LineProgram opcode {}", opcode);
  229. VERIFY_NOT_REACHED();
  230. }
  231. return {};
  232. }
  233. void LineProgram::handle_special_opcode(u8 opcode)
  234. {
  235. u8 adjusted_opcode = opcode - m_unit_header.opcode_base();
  236. ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range()) * m_unit_header.min_instruction_length();
  237. ssize_t line_increment = m_unit_header.line_base() + (adjusted_opcode % m_unit_header.line_range());
  238. m_address += address_increment;
  239. m_line += line_increment;
  240. if constexpr (DWARF_DEBUG) {
  241. dbgln("Special adjusted_opcode: {}, address_increment: {}, line_increment: {}", adjusted_opcode, address_increment, line_increment);
  242. dbgln("Address is now: {:p}, and line is: {}:{}", m_address, m_source_files[m_file_index].name, m_line);
  243. }
  244. append_to_line_info();
  245. m_basic_block = false;
  246. m_prologue_end = false;
  247. }
  248. ErrorOr<void> LineProgram::run_program()
  249. {
  250. reset_registers();
  251. while (TRY(m_stream.tell()) < m_unit_offset + sizeof(u32) + m_unit_header.length()) {
  252. auto opcode = TRY(m_stream.read_value<u8>());
  253. dbgln_if(DWARF_DEBUG, "{:p}: opcode: {}", TRY(m_stream.tell()) - 1, opcode);
  254. if (opcode == 0) {
  255. TRY(handle_extended_opcode());
  256. } else if (opcode >= 1 && opcode <= 12) {
  257. TRY(handle_standard_opcode(opcode));
  258. } else {
  259. handle_special_opcode(opcode);
  260. }
  261. }
  262. return {};
  263. }
  264. LineProgram::DirectoryAndFile LineProgram::get_directory_and_file(size_t file_index) const
  265. {
  266. VERIFY(file_index < m_source_files.size());
  267. auto file_entry = m_source_files[file_index];
  268. VERIFY(file_entry.directory_index < m_source_directories.size());
  269. auto directory_entry = m_source_directories[file_entry.directory_index];
  270. return { directory_entry, file_entry.name };
  271. }
  272. bool LineProgram::looks_like_embedded_resource() const
  273. {
  274. if (source_files().size() == 1)
  275. return source_files()[0].name.view().contains("serenity_icon_"sv);
  276. if (source_files().size() == 2 && source_files()[0].name.view() == "."sv)
  277. return source_files()[1].name.view().contains("serenity_icon_"sv);
  278. return false;
  279. }
  280. }