LineProgram.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/String.h>
  28. //#define DWARF_DEBUG
  29. namespace Debug::Dwarf {
  30. LineProgram::LineProgram(InputMemoryStream& stream)
  31. : m_stream(stream)
  32. {
  33. m_unit_offset = m_stream.offset();
  34. parse_unit_header();
  35. parse_source_directories();
  36. parse_source_files();
  37. run_program();
  38. }
  39. void LineProgram::parse_unit_header()
  40. {
  41. m_stream >> Bytes { &m_unit_header, sizeof(m_unit_header) };
  42. ASSERT(m_unit_header.version == DWARF_VERSION);
  43. ASSERT(m_unit_header.opcode_base == SPECIAL_OPCODES_BASE);
  44. #ifdef DWARF_DEBUG
  45. dbg() << "unit length: " << m_unit_header.length;
  46. #endif
  47. }
  48. void LineProgram::parse_source_directories()
  49. {
  50. m_source_directories.append(".");
  51. while (m_stream.peek_or_error()) {
  52. String directory;
  53. m_stream >> directory;
  54. #ifdef DWARF_DEBUG
  55. dbg() << "directory: " << directory;
  56. #endif
  57. m_source_directories.append(move(directory));
  58. }
  59. m_stream.handle_error();
  60. m_stream.discard_or_error(1);
  61. ASSERT(!m_stream.handle_error());
  62. }
  63. void LineProgram::parse_source_files()
  64. {
  65. m_source_files.append({ ".", 0 });
  66. while (!m_stream.eof() && m_stream.peek_or_error()) {
  67. String file_name;
  68. m_stream >> file_name;
  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. #ifdef DWARF_DEBUG
  75. dbg() << "file: " << file_name << ", directory index: " << directory_index;
  76. #endif
  77. m_source_files.append({ file_name, directory_index });
  78. }
  79. m_stream.discard_or_error(1);
  80. ASSERT(!m_stream.handle_error());
  81. }
  82. void LineProgram::append_to_line_info()
  83. {
  84. #ifdef DWARF_DEBUG
  85. dbg() << "appending line info: " << (void*)m_address << ", " << m_source_files[m_file_index].name << ":" << m_line;
  86. #endif
  87. if (!m_is_statement)
  88. return;
  89. String directory = m_source_directories[m_source_files[m_file_index].directory_index];
  90. String full_path = String::format("%s/%s", directory.characters(), m_source_files[m_file_index].name.characters());
  91. m_lines.append({ m_address, full_path, m_line });
  92. }
  93. void LineProgram::reset_registers()
  94. {
  95. m_address = 0;
  96. m_line = 1;
  97. m_file_index = 1;
  98. m_is_statement = m_unit_header.default_is_stmt == 1;
  99. }
  100. void LineProgram::handle_extended_opcode()
  101. {
  102. size_t length = 0;
  103. m_stream.read_LEB128_unsigned(length);
  104. u8 sub_opcode = 0;
  105. m_stream >> sub_opcode;
  106. switch (sub_opcode) {
  107. case ExtendedOpcodes::EndSequence: {
  108. append_to_line_info();
  109. reset_registers();
  110. break;
  111. }
  112. case ExtendedOpcodes::SetAddress: {
  113. ASSERT(length == sizeof(size_t) + 1);
  114. m_stream >> m_address;
  115. #ifdef DWARF_DEBUG
  116. dbg() << "SetAddress: " << (void*)m_address;
  117. #endif
  118. break;
  119. }
  120. case ExtendedOpcodes::SetDiscriminator: {
  121. #ifdef DWARF_DEBUG
  122. dbg() << "SetDiscriminator";
  123. #endif
  124. m_stream.discard_or_error(1);
  125. break;
  126. }
  127. default:
  128. #ifdef DWARF_DEBUG
  129. dbg() << "offset: " << (void*)m_stream.offset();
  130. #endif
  131. ASSERT_NOT_REACHED();
  132. }
  133. }
  134. void LineProgram::handle_standard_opcode(u8 opcode)
  135. {
  136. switch (opcode) {
  137. case StandardOpcodes::Copy: {
  138. append_to_line_info();
  139. break;
  140. }
  141. case StandardOpcodes::AdvancePc: {
  142. size_t operand = 0;
  143. m_stream.read_LEB128_unsigned(operand);
  144. size_t delta = operand * m_unit_header.min_instruction_length;
  145. #ifdef DWARF_DEBUG
  146. dbg() << "AdvnacePC by: " << delta << " to: " << (void*)(m_address + delta);
  147. #endif
  148. m_address += delta;
  149. break;
  150. }
  151. case StandardOpcodes::SetFile: {
  152. size_t new_file_index = 0;
  153. m_stream.read_LEB128_unsigned(new_file_index);
  154. #ifdef DWARF_DEBUG
  155. dbg() << "SetFile: new file index: " << new_file_index;
  156. #endif
  157. m_file_index = new_file_index;
  158. break;
  159. }
  160. case StandardOpcodes::SetColumn: {
  161. // not implemented
  162. #ifdef DWARF_DEBUG
  163. dbg() << "SetColumn";
  164. #endif
  165. size_t new_column;
  166. m_stream.read_LEB128_unsigned(new_column);
  167. break;
  168. }
  169. case StandardOpcodes::AdvanceLine: {
  170. ssize_t line_delta;
  171. m_stream.read_LEB128_signed(line_delta);
  172. // dbg() << "line_delta: " << line_delta;
  173. ASSERT(line_delta >= 0 || m_line >= (size_t)(-line_delta));
  174. m_line += line_delta;
  175. #ifdef DWARF_DEBUG
  176. dbg() << "AdvanceLine: " << m_line;
  177. #endif
  178. break;
  179. }
  180. case StandardOpcodes::NegateStatement: {
  181. #ifdef DWARF_DEBUG
  182. dbg() << "NegateStatement";
  183. #endif
  184. m_is_statement = !m_is_statement;
  185. break;
  186. }
  187. case StandardOpcodes::ConstAddPc: {
  188. u8 adjusted_opcode = 255 - SPECIAL_OPCODES_BASE;
  189. ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range) * m_unit_header.min_instruction_length;
  190. address_increment *= m_unit_header.min_instruction_length;
  191. #ifdef DWARF_DEBUG
  192. dbg() << "ConstAddPc: advance pc by: " << address_increment << " to: " << (m_address + address_increment);
  193. #endif
  194. m_address += address_increment;
  195. break;
  196. }
  197. default:
  198. ASSERT_NOT_REACHED();
  199. }
  200. }
  201. void LineProgram::handle_sepcial_opcode(u8 opcode)
  202. {
  203. u8 adjusted_opcode = opcode - SPECIAL_OPCODES_BASE;
  204. ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range) * m_unit_header.min_instruction_length;
  205. ssize_t line_increment = m_unit_header.line_base + (adjusted_opcode % m_unit_header.line_range);
  206. m_address += address_increment;
  207. m_line += line_increment;
  208. #ifdef DWARF_DEBUG
  209. dbg() << "Special adjusted_opcode: " << adjusted_opcode << ", delta_address: " << address_increment << ", delta_line: " << line_increment;
  210. dbg() << "Address is now:" << (void*)m_address << ", and line is: " << m_source_files[m_file_index].name << ":" << m_line;
  211. #endif
  212. append_to_line_info();
  213. }
  214. void LineProgram::run_program()
  215. {
  216. reset_registers();
  217. while ((size_t)m_stream.offset() < m_unit_offset + sizeof(u32) + m_unit_header.length) {
  218. u8 opcode = 0;
  219. m_stream >> opcode;
  220. #ifdef DWARF_DEBUG
  221. dbg() << (void*)(m_stream.offset() - 1) << ": opcode: " << opcode;
  222. #endif
  223. if (opcode == 0) {
  224. handle_extended_opcode();
  225. } else if (opcode >= 1 && opcode <= 12) {
  226. handle_standard_opcode(opcode);
  227. } else {
  228. handle_sepcial_opcode(opcode);
  229. }
  230. }
  231. }
  232. }