LineProgram.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "LineProgram.h"
  27. #include <AK/Debug.h>
  28. #include <AK/String.h>
  29. #include <AK/StringBuilder.h>
  30. namespace Debug::Dwarf {
  31. LineProgram::LineProgram(InputMemoryStream& stream)
  32. : m_stream(stream)
  33. {
  34. m_unit_offset = m_stream.offset();
  35. parse_unit_header();
  36. parse_source_directories();
  37. parse_source_files();
  38. run_program();
  39. }
  40. void LineProgram::parse_unit_header()
  41. {
  42. m_stream >> Bytes { &m_unit_header, sizeof(m_unit_header) };
  43. VERIFY(m_unit_header.version == DWARF_VERSION);
  44. VERIFY(m_unit_header.opcode_base == SPECIAL_OPCODES_BASE);
  45. dbgln_if(DWARF_DEBUG, "unit length: {}", m_unit_header.length);
  46. }
  47. void LineProgram::parse_source_directories()
  48. {
  49. m_source_directories.append(".");
  50. while (m_stream.peek_or_error()) {
  51. String directory;
  52. m_stream >> directory;
  53. dbgln_if(DWARF_DEBUG, "directory: {}", directory);
  54. m_source_directories.append(move(directory));
  55. }
  56. m_stream.handle_recoverable_error();
  57. m_stream.discard_or_error(1);
  58. VERIFY(!m_stream.has_any_error());
  59. }
  60. void LineProgram::parse_source_files()
  61. {
  62. m_source_files.append({ ".", 0 });
  63. while (!m_stream.eof() && m_stream.peek_or_error()) {
  64. String file_name;
  65. m_stream >> file_name;
  66. size_t directory_index = 0;
  67. m_stream.read_LEB128_unsigned(directory_index);
  68. size_t _unused = 0;
  69. m_stream.read_LEB128_unsigned(_unused); // skip modification time
  70. m_stream.read_LEB128_unsigned(_unused); // skip file size
  71. dbgln_if(DWARF_DEBUG, "file: {}, directory index: {}", file_name, directory_index);
  72. m_source_files.append({ file_name, directory_index });
  73. }
  74. m_stream.discard_or_error(1);
  75. VERIFY(!m_stream.has_any_error());
  76. }
  77. void LineProgram::append_to_line_info()
  78. {
  79. dbgln_if(DWARF_DEBUG, "appending line info: {:p}, {}:{}", m_address, m_source_files[m_file_index].name, m_line);
  80. if (!m_is_statement)
  81. return;
  82. if (m_file_index >= m_source_files.size())
  83. return;
  84. String directory = m_source_directories[m_source_files[m_file_index].directory_index];
  85. StringBuilder full_path(directory.length() + m_source_files[m_file_index].name.length() + 1);
  86. full_path.append(directory);
  87. full_path.append('/');
  88. full_path.append(m_source_files[m_file_index].name);
  89. m_lines.append({ m_address, full_path.to_string(), m_line });
  90. }
  91. void LineProgram::reset_registers()
  92. {
  93. m_address = 0;
  94. m_line = 1;
  95. m_file_index = 1;
  96. m_is_statement = m_unit_header.default_is_stmt == 1;
  97. }
  98. void LineProgram::handle_extended_opcode()
  99. {
  100. size_t length = 0;
  101. m_stream.read_LEB128_unsigned(length);
  102. u8 sub_opcode = 0;
  103. m_stream >> sub_opcode;
  104. switch (sub_opcode) {
  105. case ExtendedOpcodes::EndSequence: {
  106. append_to_line_info();
  107. reset_registers();
  108. break;
  109. }
  110. case ExtendedOpcodes::SetAddress: {
  111. VERIFY(length == sizeof(size_t) + 1);
  112. m_stream >> m_address;
  113. dbgln_if(DWARF_DEBUG, "SetAddress: {:p}", m_address);
  114. break;
  115. }
  116. case ExtendedOpcodes::SetDiscriminator: {
  117. dbgln_if(DWARF_DEBUG, "SetDiscriminator");
  118. m_stream.discard_or_error(1);
  119. break;
  120. }
  121. default:
  122. dbgln_if(DWARF_DEBUG, "offset: {:p}", m_stream.offset());
  123. VERIFY_NOT_REACHED();
  124. }
  125. }
  126. void LineProgram::handle_standard_opcode(u8 opcode)
  127. {
  128. switch (opcode) {
  129. case StandardOpcodes::Copy: {
  130. append_to_line_info();
  131. break;
  132. }
  133. case StandardOpcodes::AdvancePc: {
  134. size_t operand = 0;
  135. m_stream.read_LEB128_unsigned(operand);
  136. size_t delta = operand * m_unit_header.min_instruction_length;
  137. dbgln_if(DWARF_DEBUG, "AdvancePC by: {} to: {:p}", delta, m_address + delta);
  138. m_address += delta;
  139. break;
  140. }
  141. case StandardOpcodes::SetFile: {
  142. size_t new_file_index = 0;
  143. m_stream.read_LEB128_unsigned(new_file_index);
  144. dbgln_if(DWARF_DEBUG, "SetFile: new file index: {}", new_file_index);
  145. m_file_index = new_file_index;
  146. break;
  147. }
  148. case StandardOpcodes::SetColumn: {
  149. // not implemented
  150. dbgln_if(DWARF_DEBUG, "SetColumn");
  151. size_t new_column;
  152. m_stream.read_LEB128_unsigned(new_column);
  153. break;
  154. }
  155. case StandardOpcodes::AdvanceLine: {
  156. ssize_t line_delta;
  157. m_stream.read_LEB128_signed(line_delta);
  158. VERIFY(line_delta >= 0 || m_line >= (size_t)(-line_delta));
  159. m_line += line_delta;
  160. dbgln_if(DWARF_DEBUG, "AdvanceLine: {}", m_line);
  161. break;
  162. }
  163. case StandardOpcodes::NegateStatement: {
  164. dbgln_if(DWARF_DEBUG, "NegateStatement");
  165. m_is_statement = !m_is_statement;
  166. break;
  167. }
  168. case StandardOpcodes::ConstAddPc: {
  169. u8 adjusted_opcode = 255 - SPECIAL_OPCODES_BASE;
  170. ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range) * m_unit_header.min_instruction_length;
  171. address_increment *= m_unit_header.min_instruction_length;
  172. dbgln_if(DWARF_DEBUG, "ConstAddPc: advance pc by: {} to: {}", address_increment, (m_address + address_increment));
  173. m_address += address_increment;
  174. break;
  175. }
  176. case StandardOpcodes::SetIsa: {
  177. size_t isa;
  178. m_stream.read_LEB128_unsigned(isa);
  179. dbgln_if(DWARF_DEBUG, "SetIsa: {}", isa);
  180. break;
  181. }
  182. case StandardOpcodes::FixAdvancePc: {
  183. u16 delta = 0;
  184. m_stream >> delta;
  185. dbgln_if(DWARF_DEBUG, "FixAdvancePC by: {} to: {:p}", delta, m_address + delta);
  186. m_address += delta;
  187. break;
  188. }
  189. default:
  190. dbgln("Unhandled LineProgram opcode {}", opcode);
  191. VERIFY_NOT_REACHED();
  192. }
  193. }
  194. void LineProgram::handle_special_opcode(u8 opcode)
  195. {
  196. u8 adjusted_opcode = opcode - SPECIAL_OPCODES_BASE;
  197. ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range) * m_unit_header.min_instruction_length;
  198. ssize_t line_increment = m_unit_header.line_base + (adjusted_opcode % m_unit_header.line_range);
  199. m_address += address_increment;
  200. m_line += line_increment;
  201. if constexpr (DWARF_DEBUG) {
  202. dbgln("Special adjusted_opcode: {}, address_increment: {}, line_increment: {}", adjusted_opcode, address_increment, line_increment);
  203. dbgln("Address is now: {:p}, and line is: {}:{}", m_address, m_source_files[m_file_index].name, m_line);
  204. }
  205. append_to_line_info();
  206. }
  207. void LineProgram::run_program()
  208. {
  209. reset_registers();
  210. while ((size_t)m_stream.offset() < m_unit_offset + sizeof(u32) + m_unit_header.length) {
  211. u8 opcode = 0;
  212. m_stream >> opcode;
  213. dbgln_if(DWARF_DEBUG, "{:p}: opcode: {}", m_stream.offset() - 1, opcode);
  214. if (opcode == 0) {
  215. handle_extended_opcode();
  216. } else if (opcode >= 1 && opcode <= 12) {
  217. handle_standard_opcode(opcode);
  218. } else {
  219. handle_special_opcode(opcode);
  220. }
  221. }
  222. }
  223. }