LineProgram.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. public:
  89. explicit LineProgram(DwarfInfo& dwarf_info, InputMemoryStream& stream);
  90. struct LineInfo {
  91. u32 address { 0 };
  92. FlyString file;
  93. size_t line { 0 };
  94. };
  95. const Vector<LineInfo>& lines() const { return m_lines; }
  96. private:
  97. void parse_unit_header();
  98. void parse_source_directories();
  99. void parse_source_files();
  100. void run_program();
  101. void append_to_line_info();
  102. void reset_registers();
  103. void handle_extended_opcode();
  104. void handle_standard_opcode(u8 opcode);
  105. void handle_special_opcode(u8 opcode);
  106. void parse_path_entries(Function<void(PathEntry& entry)> callback, PathListType list_type);
  107. enum StandardOpcodes {
  108. Copy = 1,
  109. AdvancePc,
  110. AdvanceLine,
  111. SetFile,
  112. SetColumn,
  113. NegateStatement,
  114. SetBasicBlock,
  115. ConstAddPc,
  116. FixAdvancePc,
  117. SetProlougeEnd,
  118. SetEpilogueBegin,
  119. SetIsa
  120. };
  121. enum ExtendedOpcodes {
  122. EndSequence = 1,
  123. SetAddress,
  124. DefineFile,
  125. SetDiscriminator,
  126. };
  127. struct FileEntry {
  128. FlyString name;
  129. size_t directory_index { 0 };
  130. };
  131. static constexpr u16 MIN_DWARF_VERSION = 3;
  132. static constexpr u16 MAX_DWARF_VERSION = 5;
  133. DwarfInfo& m_dwarf_info;
  134. InputMemoryStream& m_stream;
  135. size_t m_unit_offset { 0 };
  136. LineProgramUnitHeader32 m_unit_header {};
  137. Vector<String> m_source_directories;
  138. Vector<FileEntry> m_source_files;
  139. // The registers of the "line program" virtual machine
  140. u32 m_address { 0 };
  141. size_t m_line { 0 };
  142. size_t m_file_index { 0 };
  143. bool m_is_statement { false };
  144. bool m_basic_block { false };
  145. Vector<LineInfo> m_lines;
  146. };
  147. }