DebugInfo.h 4.4 KB

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