DebugInfo.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/NonnullOwnPtrVector.h>
  28. #include <AK/NonnullRefPtr.h>
  29. #include <AK/Optional.h>
  30. #include <AK/Vector.h>
  31. #include <LibDebug/Dwarf/DwarfInfo.h>
  32. #include <LibELF/Loader.h>
  33. #include <Libraries/LibDebug/Dwarf/DIE.h>
  34. #include <Libraries/LibDebug/Dwarf/LineProgram.h>
  35. #include <sys/arch/i386/regs.h>
  36. class DebugInfo {
  37. public:
  38. explicit DebugInfo(NonnullRefPtr<const ELF::Loader> elf);
  39. struct SourcePosition {
  40. String file_path;
  41. size_t line_number { 0 };
  42. u32 address_of_first_statement { 0 };
  43. bool operator==(const SourcePosition& other) const { return file_path == other.file_path && line_number == other.line_number; }
  44. bool operator!=(const SourcePosition& other) const { return !(*this == other); }
  45. };
  46. struct VariableInfo {
  47. enum class LocationType {
  48. None,
  49. Address,
  50. Regsiter,
  51. };
  52. String name;
  53. String type;
  54. LocationType location_type { LocationType::None };
  55. union {
  56. u32 address;
  57. } location_data { 0 };
  58. NonnullOwnPtrVector<VariableInfo> members;
  59. VariableInfo* parent { nullptr };
  60. };
  61. struct VariablesScope {
  62. bool is_function { false };
  63. Optional<String> name;
  64. u32 address_low { 0 };
  65. u32 address_high { 0 };
  66. Vector<Dwarf::DIE> dies_of_variables;
  67. };
  68. NonnullOwnPtrVector<VariableInfo> get_variables_in_current_scope(const PtraceRegisters&) const;
  69. Optional<SourcePosition> get_source_position(u32 address) const;
  70. Optional<u32> get_instruction_from_source(const String& file, size_t line) const;
  71. template<typename Callback>
  72. void for_each_source_position(Callback callback) const
  73. {
  74. String previous_file = "";
  75. size_t previous_line = 0;
  76. for (const auto& line_info : m_sorted_lines) {
  77. if (line_info.file == previous_file && line_info.line == previous_line)
  78. continue;
  79. previous_file = line_info.file;
  80. previous_line = line_info.line;
  81. callback({ line_info.file, line_info.line, line_info.address });
  82. }
  83. }
  84. private:
  85. void prepare_variable_scopes();
  86. void prepare_lines();
  87. void parse_scopes_impl(const Dwarf::DIE& die);
  88. Optional<VariablesScope> get_scope(u32 instruction_pointer) const;
  89. NonnullOwnPtr<VariableInfo> create_variable_info(const Dwarf::DIE& variable_die, const PtraceRegisters&) const;
  90. NonnullRefPtr<const ELF::Loader> m_elf;
  91. NonnullRefPtr<Dwarf::DwarfInfo> m_dwarf_info;
  92. Vector<VariablesScope> m_scopes;
  93. Vector<LineProgram::LineInfo> m_sorted_lines;
  94. };