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