LineProgram.h 5.3 KB

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