LineProgram.cpp 9.7 KB

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