DebugInfo.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2020-2021, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullRefPtr.h>
  8. #include <AK/Optional.h>
  9. #include <AK/OwnPtr.h>
  10. #include <AK/Vector.h>
  11. #include <LibDebug/Dwarf/DIE.h>
  12. #include <LibDebug/Dwarf/DwarfInfo.h>
  13. #include <LibDebug/Dwarf/LineProgram.h>
  14. #include <LibELF/Image.h>
  15. #include <sys/arch/regs.h>
  16. namespace Debug {
  17. class DebugInfo {
  18. AK_MAKE_NONCOPYABLE(DebugInfo);
  19. AK_MAKE_NONMOVABLE(DebugInfo);
  20. public:
  21. explicit DebugInfo(ELF::Image const&, ByteString source_root = {}, FlatPtr base_address = 0);
  22. ELF::Image const& elf() const { return m_elf; }
  23. struct SourcePosition {
  24. DeprecatedFlyString file_path;
  25. size_t line_number { 0 };
  26. Optional<FlatPtr> address_of_first_statement;
  27. SourcePosition()
  28. : SourcePosition(ByteString::empty(), 0)
  29. {
  30. }
  31. SourcePosition(ByteString file_path, size_t line_number)
  32. : file_path(file_path)
  33. , line_number(line_number)
  34. {
  35. }
  36. SourcePosition(ByteString file_path, size_t line_number, FlatPtr address_of_first_statement)
  37. : file_path(file_path)
  38. , line_number(line_number)
  39. , address_of_first_statement(address_of_first_statement)
  40. {
  41. }
  42. bool operator==(SourcePosition const& other) const { return file_path == other.file_path && line_number == other.line_number; }
  43. static SourcePosition from_line_info(Dwarf::LineProgram::LineInfo const&);
  44. };
  45. struct VariableInfo {
  46. enum class LocationType {
  47. None,
  48. Address,
  49. Register,
  50. };
  51. ByteString name;
  52. ByteString type_name;
  53. LocationType location_type { LocationType::None };
  54. union {
  55. FlatPtr address;
  56. } location_data { 0 };
  57. union {
  58. u32 as_u32;
  59. u32 as_i32;
  60. char const* as_string;
  61. } constant_data { 0 };
  62. Dwarf::EntryTag type_tag;
  63. OwnPtr<VariableInfo> type;
  64. Vector<NonnullOwnPtr<VariableInfo>> members;
  65. VariableInfo* parent { nullptr };
  66. Vector<u32> dimension_sizes;
  67. bool is_enum_type() const { return type && type->type_tag == Dwarf::EntryTag::EnumerationType; }
  68. };
  69. struct VariablesScope {
  70. bool is_function { false };
  71. ByteString name;
  72. FlatPtr address_low { 0 };
  73. FlatPtr address_high { 0 }; // Non-inclusive - the lowest address after address_low that's not in this scope
  74. Vector<Dwarf::DIE> dies_of_variables;
  75. };
  76. ErrorOr<Vector<NonnullOwnPtr<VariableInfo>>> get_variables_in_current_scope(PtraceRegisters const&) const;
  77. Optional<SourcePosition> get_source_position(FlatPtr address) const;
  78. struct SourcePositionWithInlines {
  79. Optional<SourcePosition> source_position;
  80. Vector<SourcePosition> inline_chain;
  81. };
  82. ErrorOr<SourcePositionWithInlines> get_source_position_with_inlines(FlatPtr address) const;
  83. struct SourcePositionAndAddress {
  84. ByteString file;
  85. size_t line;
  86. FlatPtr address;
  87. };
  88. Optional<SourcePositionAndAddress> get_address_from_source_position(ByteString const& file, size_t line) const;
  89. ByteString name_of_containing_function(FlatPtr address) const;
  90. Vector<SourcePosition> source_lines_in_scope(VariablesScope const&) const;
  91. Optional<VariablesScope> get_containing_function(FlatPtr address) const;
  92. private:
  93. ErrorOr<void> prepare_variable_scopes();
  94. ErrorOr<void> prepare_lines();
  95. ErrorOr<void> parse_scopes_impl(Dwarf::DIE const& die);
  96. ErrorOr<OwnPtr<VariableInfo>> create_variable_info(Dwarf::DIE const& variable_die, PtraceRegisters const&, u32 address_offset = 0) const;
  97. static bool is_variable_tag_supported(Dwarf::EntryTag const& tag);
  98. ErrorOr<void> add_type_info_to_variable(Dwarf::DIE const& type_die, PtraceRegisters const& regs, DebugInfo::VariableInfo* parent_variable) const;
  99. ErrorOr<Optional<Dwarf::LineProgram::DirectoryAndFile>> get_source_path_of_inline(Dwarf::DIE const&) const;
  100. ErrorOr<Optional<uint32_t>> get_line_of_inline(Dwarf::DIE const&) const;
  101. ELF::Image const& m_elf;
  102. ByteString m_source_root;
  103. FlatPtr m_base_address { 0 };
  104. Dwarf::DwarfInfo m_dwarf_info;
  105. Vector<VariablesScope> m_scopes;
  106. Vector<Dwarf::LineProgram::LineInfo> m_sorted_lines;
  107. };
  108. }