LineProgram.cpp 7.6 KB

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