LineProgram.cpp 7.7 KB

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