LineProgram.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. size_t content_type = 0;
  37. m_stream.read_LEB128_unsigned(content_type);
  38. size_t data_form = 0;
  39. m_stream.read_LEB128_unsigned(data_form);
  40. format_descriptions.empend((ContentType)content_type, (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.data.as_string;
  51. break;
  52. case ContentType::DirectoryIndex:
  53. entry.directory_index = value.data.as_unsigned;
  54. break;
  55. default:
  56. dbgln_if(DWARF_DEBUG, "Unhandled path list attribute: {}", (int)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. m_stream.discard_or_error(1);
  146. break;
  147. }
  148. default:
  149. dbgln("Encountered unknown sub opcode {} at stream offset {:p}", sub_opcode, m_stream.offset());
  150. VERIFY_NOT_REACHED();
  151. }
  152. }
  153. void LineProgram::handle_standard_opcode(u8 opcode)
  154. {
  155. switch (opcode) {
  156. case StandardOpcodes::Copy: {
  157. append_to_line_info();
  158. break;
  159. }
  160. case StandardOpcodes::AdvancePc: {
  161. size_t operand = 0;
  162. m_stream.read_LEB128_unsigned(operand);
  163. size_t delta = operand * m_unit_header.min_instruction_length();
  164. dbgln_if(DWARF_DEBUG, "AdvancePC by: {} to: {:p}", delta, m_address + delta);
  165. m_address += delta;
  166. break;
  167. }
  168. case StandardOpcodes::SetFile: {
  169. size_t new_file_index = 0;
  170. m_stream.read_LEB128_unsigned(new_file_index);
  171. dbgln_if(DWARF_DEBUG, "SetFile: new file index: {}", new_file_index);
  172. m_file_index = new_file_index;
  173. break;
  174. }
  175. case StandardOpcodes::SetColumn: {
  176. // not implemented
  177. dbgln_if(DWARF_DEBUG, "SetColumn");
  178. size_t new_column;
  179. m_stream.read_LEB128_unsigned(new_column);
  180. break;
  181. }
  182. case StandardOpcodes::AdvanceLine: {
  183. ssize_t line_delta;
  184. m_stream.read_LEB128_signed(line_delta);
  185. VERIFY(line_delta >= 0 || m_line >= (size_t)(-line_delta));
  186. m_line += line_delta;
  187. dbgln_if(DWARF_DEBUG, "AdvanceLine: {}", m_line);
  188. break;
  189. }
  190. case StandardOpcodes::NegateStatement: {
  191. dbgln_if(DWARF_DEBUG, "NegateStatement");
  192. m_is_statement = !m_is_statement;
  193. break;
  194. }
  195. case StandardOpcodes::ConstAddPc: {
  196. u8 adjusted_opcode = 255 - m_unit_header.opcode_base();
  197. ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range()) * m_unit_header.min_instruction_length();
  198. address_increment *= m_unit_header.min_instruction_length();
  199. dbgln_if(DWARF_DEBUG, "ConstAddPc: advance pc by: {} to: {}", address_increment, (m_address + address_increment));
  200. m_address += address_increment;
  201. break;
  202. }
  203. case StandardOpcodes::SetIsa: {
  204. size_t isa;
  205. m_stream.read_LEB128_unsigned(isa);
  206. dbgln_if(DWARF_DEBUG, "SetIsa: {}", isa);
  207. break;
  208. }
  209. case StandardOpcodes::FixAdvancePc: {
  210. u16 delta = 0;
  211. m_stream >> delta;
  212. dbgln_if(DWARF_DEBUG, "FixAdvancePC by: {} to: {:p}", delta, m_address + delta);
  213. m_address += delta;
  214. break;
  215. }
  216. case StandardOpcodes::SetBasicBlock: {
  217. m_basic_block = true;
  218. break;
  219. }
  220. case StandardOpcodes::SetProlougeEnd: {
  221. m_prologue_end = true;
  222. break;
  223. }
  224. default:
  225. dbgln("Unhandled LineProgram opcode {}", opcode);
  226. VERIFY_NOT_REACHED();
  227. }
  228. }
  229. void LineProgram::handle_special_opcode(u8 opcode)
  230. {
  231. u8 adjusted_opcode = opcode - m_unit_header.opcode_base();
  232. ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range()) * m_unit_header.min_instruction_length();
  233. ssize_t line_increment = m_unit_header.line_base() + (adjusted_opcode % m_unit_header.line_range());
  234. m_address += address_increment;
  235. m_line += line_increment;
  236. if constexpr (DWARF_DEBUG) {
  237. dbgln("Special adjusted_opcode: {}, address_increment: {}, line_increment: {}", adjusted_opcode, address_increment, line_increment);
  238. dbgln("Address is now: {:p}, and line is: {}:{}", m_address, m_source_files[m_file_index].name, m_line);
  239. }
  240. append_to_line_info();
  241. m_basic_block = false;
  242. m_prologue_end = false;
  243. }
  244. void LineProgram::run_program()
  245. {
  246. reset_registers();
  247. while ((size_t)m_stream.offset() < m_unit_offset + sizeof(u32) + m_unit_header.length()) {
  248. u8 opcode = 0;
  249. m_stream >> opcode;
  250. dbgln_if(DWARF_DEBUG, "{:p}: opcode: {}", m_stream.offset() - 1, opcode);
  251. if (opcode == 0) {
  252. handle_extended_opcode();
  253. } else if (opcode >= 1 && opcode <= 12) {
  254. handle_standard_opcode(opcode);
  255. } else {
  256. handle_special_opcode(opcode);
  257. }
  258. }
  259. }
  260. LineProgram::DirectoryAndFile LineProgram::get_directory_and_file(size_t file_index) const
  261. {
  262. VERIFY(file_index < m_source_files.size());
  263. auto file_entry = m_source_files[file_index];
  264. VERIFY(file_entry.directory_index < m_source_directories.size());
  265. auto directory_entry = m_source_directories[file_entry.directory_index];
  266. return { directory_entry, file_entry.name };
  267. }
  268. }