LineProgram.cpp 7.7 KB

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