LineProgram.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "DwarfInfo.h"
  8. #include <AK/FlyString.h>
  9. #include <AK/MemoryStream.h>
  10. #include <AK/Vector.h>
  11. namespace Debug::Dwarf {
  12. struct [[gnu::packed]] LineProgramUnitHeader32Common {
  13. u32 length;
  14. u16 version;
  15. };
  16. struct [[gnu::packed]] LineProgramUnitHeader32V4Ext {
  17. u32 header_length;
  18. u8 min_instruction_length;
  19. u8 max_instruction_length;
  20. u8 default_is_stmt;
  21. i8 line_base;
  22. u8 line_range;
  23. u8 opcode_base;
  24. };
  25. struct [[gnu::packed]] LineProgramUnitHeader32V5Ext {
  26. u8 address_size;
  27. u8 segment_selector_size;
  28. u32 header_length;
  29. u8 min_instruction_length;
  30. u8 max_instruction_length;
  31. u8 default_is_stmt;
  32. i8 line_base;
  33. u8 line_range;
  34. u8 opcode_base;
  35. };
  36. struct [[gnu::packed]] LineProgramUnitHeader32 {
  37. LineProgramUnitHeader32Common common;
  38. union {
  39. LineProgramUnitHeader32V4Ext v4;
  40. LineProgramUnitHeader32V5Ext v5;
  41. };
  42. u8 std_opcode_lengths[13];
  43. size_t header_size() const
  44. {
  45. return sizeof(common) + ((common.version <= 4) ? sizeof(v4) : sizeof(v5)) + (opcode_base() - 1) * sizeof(std_opcode_lengths[0]);
  46. }
  47. u32 length() const { return common.length; }
  48. u16 version() const { return common.version; }
  49. u32 header_length() const { return (common.version <= 4) ? v4.header_length : v5.header_length; }
  50. u8 min_instruction_length() const { return (common.version <= 4) ? v4.min_instruction_length : v5.min_instruction_length; }
  51. u8 default_is_stmt() const { return (common.version <= 4) ? v4.default_is_stmt : v5.default_is_stmt; }
  52. i8 line_base() const { return (common.version <= 4) ? v4.line_base : v5.line_base; }
  53. u8 line_range() const { return (common.version <= 4) ? v4.line_range : v5.line_range; }
  54. u8 opcode_base() const { return (common.version <= 4) ? v4.opcode_base : v5.opcode_base; }
  55. };
  56. enum class ContentType {
  57. Path = 1,
  58. DirectoryIndex = 2,
  59. Timestamp = 3,
  60. Size = 4,
  61. MD5 = 5,
  62. LoUser = 0x2000,
  63. HiUser = 0x3fff,
  64. };
  65. struct PathEntryFormat {
  66. ContentType type;
  67. AttributeDataForm form;
  68. };
  69. struct PathEntry {
  70. String path;
  71. size_t directory_index { 0 };
  72. };
  73. enum class PathListType {
  74. Directories,
  75. Filenames,
  76. };
  77. inline InputStream& operator>>(InputStream& stream, LineProgramUnitHeader32& header)
  78. {
  79. stream.read_or_error(Bytes { &header.common, sizeof(header.common) });
  80. if (header.common.version <= 4)
  81. stream.read_or_error(Bytes { &header.v4, sizeof(header.v4) });
  82. else
  83. stream.read_or_error(Bytes { &header.v5, sizeof(header.v5) });
  84. stream.read_or_error(Bytes { &header.std_opcode_lengths, min(sizeof(header.std_opcode_lengths), (header.opcode_base() - 1) * sizeof(header.std_opcode_lengths[0])) });
  85. return stream;
  86. }
  87. class LineProgram {
  88. AK_MAKE_NONCOPYABLE(LineProgram);
  89. AK_MAKE_NONMOVABLE(LineProgram);
  90. public:
  91. explicit LineProgram(DwarfInfo& dwarf_info, InputMemoryStream& stream);
  92. struct LineInfo {
  93. FlatPtr address { 0 };
  94. FlyString file;
  95. size_t line { 0 };
  96. };
  97. Vector<LineInfo> const& lines() const { return m_lines; }
  98. struct DirectoryAndFile {
  99. FlyString directory;
  100. FlyString filename;
  101. };
  102. DirectoryAndFile get_directory_and_file(size_t file_index) const;
  103. struct FileEntry {
  104. FlyString name;
  105. size_t directory_index { 0 };
  106. };
  107. Vector<FileEntry> const& source_files() const { return m_source_files; }
  108. private:
  109. void parse_unit_header();
  110. void parse_source_directories();
  111. void parse_source_files();
  112. void run_program();
  113. void append_to_line_info();
  114. void reset_registers();
  115. void handle_extended_opcode();
  116. void handle_standard_opcode(u8 opcode);
  117. void handle_special_opcode(u8 opcode);
  118. void parse_path_entries(Function<void(PathEntry& entry)> callback, PathListType list_type);
  119. enum StandardOpcodes {
  120. Copy = 1,
  121. AdvancePc,
  122. AdvanceLine,
  123. SetFile,
  124. SetColumn,
  125. NegateStatement,
  126. SetBasicBlock,
  127. ConstAddPc,
  128. FixAdvancePc,
  129. SetProlougeEnd,
  130. SetEpilogueBegin,
  131. SetIsa
  132. };
  133. enum ExtendedOpcodes {
  134. EndSequence = 1,
  135. SetAddress,
  136. DefineFile,
  137. SetDiscriminator,
  138. };
  139. static constexpr u16 MIN_DWARF_VERSION = 3;
  140. static constexpr u16 MAX_DWARF_VERSION = 5;
  141. DwarfInfo& m_dwarf_info;
  142. InputMemoryStream& m_stream;
  143. size_t m_unit_offset { 0 };
  144. LineProgramUnitHeader32 m_unit_header {};
  145. Vector<String> m_source_directories;
  146. Vector<FileEntry> m_source_files;
  147. // The registers of the "line program" virtual machine
  148. FlatPtr m_address { 0 };
  149. size_t m_line { 0 };
  150. size_t m_file_index { 0 };
  151. bool m_is_statement { false };
  152. bool m_basic_block { false };
  153. bool m_prologue_end { false };
  154. Vector<LineInfo> m_lines;
  155. };
  156. }