LineProgram.cpp 7.8 KB

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