LineProgram.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/Vector.h>
  9. #include <LibDebug/Dwarf/DwarfTypes.h>
  10. namespace Debug::Dwarf {
  11. class DwarfInfo;
  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. static ErrorOr<LineProgramUnitHeader32> read_from_stream(Stream& stream)
  56. {
  57. LineProgramUnitHeader32 header;
  58. TRY(stream.read_until_filled(Bytes { &header.common, sizeof(header.common) }));
  59. if (header.common.version <= 4)
  60. TRY(stream.read_until_filled(Bytes { &header.v4, sizeof(header.v4) }));
  61. else
  62. TRY(stream.read_until_filled(Bytes { &header.v5, sizeof(header.v5) }));
  63. 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])) }));
  64. return header;
  65. }
  66. };
  67. enum class ContentType {
  68. Path = 1,
  69. DirectoryIndex = 2,
  70. Timestamp = 3,
  71. Size = 4,
  72. MD5 = 5,
  73. LoUser = 0x2000,
  74. HiUser = 0x3fff,
  75. };
  76. struct PathEntryFormat {
  77. ContentType type;
  78. AttributeDataForm form;
  79. };
  80. struct PathEntry {
  81. DeprecatedString path;
  82. size_t directory_index { 0 };
  83. };
  84. enum class PathListType {
  85. Directories,
  86. Filenames,
  87. };
  88. class LineProgram {
  89. AK_MAKE_NONCOPYABLE(LineProgram);
  90. AK_MAKE_NONMOVABLE(LineProgram);
  91. public:
  92. explicit LineProgram(DwarfInfo& dwarf_info, SeekableStream& stream);
  93. struct LineInfo {
  94. FlatPtr address { 0 };
  95. DeprecatedFlyString file;
  96. size_t line { 0 };
  97. };
  98. Vector<LineInfo> const& lines() const { return m_lines; }
  99. struct DirectoryAndFile {
  100. DeprecatedFlyString directory;
  101. DeprecatedFlyString filename;
  102. };
  103. DirectoryAndFile get_directory_and_file(size_t file_index) const;
  104. struct FileEntry {
  105. DeprecatedFlyString name;
  106. size_t directory_index { 0 };
  107. };
  108. Vector<FileEntry> const& source_files() const { return m_source_files; }
  109. bool looks_like_embedded_resource() const;
  110. private:
  111. ErrorOr<void> parse_unit_header();
  112. ErrorOr<void> parse_source_directories();
  113. ErrorOr<void> parse_source_files();
  114. ErrorOr<void> run_program();
  115. void append_to_line_info();
  116. void reset_registers();
  117. ErrorOr<void> handle_extended_opcode();
  118. ErrorOr<void> handle_standard_opcode(u8 opcode);
  119. void handle_special_opcode(u8 opcode);
  120. ErrorOr<void> parse_path_entries(Function<void(PathEntry& entry)> callback, PathListType list_type);
  121. enum StandardOpcodes {
  122. Copy = 1,
  123. AdvancePc,
  124. AdvanceLine,
  125. SetFile,
  126. SetColumn,
  127. NegateStatement,
  128. SetBasicBlock,
  129. ConstAddPc,
  130. FixAdvancePc,
  131. SetPrologueEnd,
  132. SetEpilogueBegin,
  133. SetIsa
  134. };
  135. enum ExtendedOpcodes {
  136. EndSequence = 1,
  137. SetAddress,
  138. DefineFile,
  139. SetDiscriminator,
  140. };
  141. static constexpr u16 MIN_DWARF_VERSION = 3;
  142. static constexpr u16 MAX_DWARF_VERSION = 5;
  143. DwarfInfo& m_dwarf_info;
  144. SeekableStream& m_stream;
  145. size_t m_unit_offset { 0 };
  146. LineProgramUnitHeader32 m_unit_header {};
  147. Vector<DeprecatedString> m_source_directories;
  148. Vector<FileEntry> m_source_files;
  149. // The registers of the "line program" virtual machine
  150. FlatPtr m_address { 0 };
  151. size_t m_line { 0 };
  152. size_t m_file_index { 0 };
  153. bool m_is_statement { false };
  154. bool m_basic_block { false };
  155. bool m_prologue_end { false };
  156. Vector<LineInfo> m_lines;
  157. };
  158. }