Debian Codegraph @

src/symbols/dwarf/debug_map_resolver.cpp

raw
1 #ifdef CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF
2
3 #include "symbols/dwarf/resolver.hpp"
4
5 #include <cpptrace/basic.hpp>
6 #include "symbols/symbols.hpp"
7 #include "utils/common.hpp"
8 #include "utils/error.hpp"
9 #include "binary/object.hpp"
10 #include "binary/mach-o.hpp"
11 #include "utils/utils.hpp"
12
13 #include <algorithm>
14 #include <cstdint>
15 #include <cstdio>
16 #include <memory>
17 #include <string>
18 #include <unordered_map>
19 #include <vector>
20
21 CPPTRACE_BEGIN_NAMESPACE
22 namespace detail {
23 namespace libdwarf {
24 #if IS_APPLE
25 struct target_object {
26 std::string object_path;
27 bool path_ok = true;
28 optional<std::unordered_map<std::string, uint64_t>> symbols;
29 std::unique_ptr<symbol_resolver> resolver;
30
31 target_object(std::string object_path) : object_path(std::move(object_path)) {}
32
33 std::unique_ptr<symbol_resolver>& get_resolver() {
34 if(!resolver) {
35 // this seems silly but it's an attempt to not repeatedly try to initialize new dwarf_resolvers if
36 // exceptions are thrown, e.g. if the path doesn't exist
37 resolver = detail::make_unique<null_resolver>();
38 resolver = make_dwarf_resolver(object_path);
39 }
40 return resolver;
41 }
42
43 std::unordered_map<std::string, uint64_t>& get_symbols() {
44 if(!symbols) {
45 // this is an attempt to not repeatedly try to reprocess mach-o files if exceptions are thrown, e.g. if
46 // the path doesn't exist
47 std::unordered_map<std::string, uint64_t> symbols;
48 this->symbols = symbols;
49 auto mach_o_object = open_mach_o_cached(object_path);
50 if(!mach_o_object) {
51 return this->symbols.unwrap();
52 }
53 const auto& symbol_table = mach_o_object.unwrap_value()->symbol_table();
54 if(!symbol_table) {
55 return this->symbols.unwrap();
56 }
57 for(const auto& symbol : symbol_table.unwrap_value()) {
58 symbols[symbol.name] = symbol.address;
59 }
60 this->symbols = std::move(symbols);
61 }
62 return symbols.unwrap();
63 }
64
65 CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING
66 frame_with_inlines resolve_frame(
67 const object_frame& frame_info,
68 const std::string& symbol_name,
69 std::size_t offset
70 ) {
71 const auto& symbol_table = get_symbols();
72 auto it = symbol_table.find(symbol_name);
73 if(it != symbol_table.end()) {
74 auto frame = frame_info;
75 // substitute a translated address object for the target file in
76 frame.object_address = it->second + offset;
77 auto res = get_resolver()->resolve_frame(frame);
78 // replace the translated address with the object address in the binary
79 res.frame.object_address = frame_info.object_address;
80 return res;
81 } else {
82 return {
83 {
84 frame_info.raw_address,
85 frame_info.object_address,
86 nullable<std::uint32_t>::null(),
87 nullable<std::uint32_t>::null(),
88 frame_info.object_path,
89 symbol_name,
90 false
91 },
92 {}
93 };
94 }
95 }
96 };
97
98 struct debug_map_symbol_info {
99 uint64_t source_address;
100 uint64_t size;
101 std::string name;
102 nullable<uint64_t> target_address; // T(-1) is used as a sentinel
103 std::size_t object_index; // index into target_objects
104 };
105
106 class debug_map_resolver : public symbol_resolver {
107 std::vector<target_object> target_objects;
108 std::vector<debug_map_symbol_info> symbols;
109 public:
110 debug_map_resolver(const std::string& source_object_path) {
111 // load mach-o
112 // TODO: Cache somehow?
113 auto mach_o_object = open_mach_o_cached(source_object_path);
114 if(!mach_o_object) {
115 return;
116 }
117 mach_o& source_mach = *mach_o_object.unwrap_value();
118 auto source_debug_map = source_mach.get_debug_map();
119 if(!source_debug_map) {
120 return;
121 }
122 // get symbol entries from debug map, as well as the various object files used to make this binary
123 for(auto& entry : source_debug_map.unwrap_value()) {
124 // object it came from
125 target_objects.push_back({entry.first});
126 // push the symbols
127 auto& map_entry_symbols = entry.second;
128 symbols.reserve(symbols.size() + map_entry_symbols.size());
129 for(auto& symbol : map_entry_symbols) {
130 symbols.push_back({
131 symbol.source_address,
132 symbol.size,
133 std::move(symbol.name),
134 nullable<uint64_t>::null(),
135 target_objects.size() - 1
136 });
137 }
138 }
139 // sort for binary lookup later
140 // TODO: Redundant?
141 std::sort(
142 symbols.begin(),
143 symbols.end(),
144 [] (
145 const debug_map_symbol_info& a,
146 const debug_map_symbol_info& b
147 ) {
148 return a.source_address < b.source_address;
149 }
150 );
151 }
152 CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING
153 frame_with_inlines resolve_frame(const object_frame& frame_info) override {
154 // resolve object frame:
155 // find the symbol in this executable corresponding to the object address
156 // resolve the symbol in the object it came from, based on the symbol name
157 auto closest_symbol_it = first_less_than_or_equal(
158 symbols.begin(),
159 symbols.end(),
160 frame_info.object_address,
161 [] (
162 uint64_t pc,
163 const debug_map_symbol_info& symbol
164 ) {
165 return pc < symbol.source_address;
166 }
167 );
168 if(closest_symbol_it != symbols.end()) {
169 if(frame_info.object_address <= closest_symbol_it->source_address + closest_symbol_it->size) {
170 return target_objects[closest_symbol_it->object_index].resolve_frame(
171 {
172 frame_info.raw_address,
173 // the resolver doesn't care about the object address here, only the offset from the start
174 // of the symbol and it'll lookup the symbol's base-address
175 frame_info.object_address,
176 frame_info.object_path
177 },
178 closest_symbol_it->name,
179 frame_info.object_address - closest_symbol_it->source_address
180 );
181 }
182 }
183 // There was either no closest symbol or the closest symbol didn't end up containing the address we're
184 // looking for, so just return a blank frame
185 return {
186 {
187 frame_info.raw_address,
188 frame_info.object_address,
189 nullable<std::uint32_t>::null(),
190 nullable<std::uint32_t>::null(),
191 frame_info.object_path,
192 "",
193 false
194 },
195 {}
196 };
197 };
198 };
199
200 std::unique_ptr<symbol_resolver> make_debug_map_resolver(const std::string& object_path) {
201 return detail::make_unique<debug_map_resolver>(object_path);
202 }
203 #endif
204 }
205 }
206 CPPTRACE_END_NAMESPACE
207
208 #endif