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