Debian Codegraph @

src/symbols/symbols_with_libdwarf.cpp

raw
1 #ifdef CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF
2
3 #include "symbols/symbols.hpp"
4
5 #include <cpptrace/basic.hpp>
6
7 #include "dwarf/resolver.hpp"
8 #include "utils/common.hpp"
9 #include "utils/utils.hpp"
10 #include "binary/elf.hpp"
11 #include "binary/mach-o.hpp"
12 #include "jit/jit_objects.hpp"
13
14 #include <cstdint>
15 #include <cstdio>
16 #include <memory>
17 #include <mutex>
18 #include <unordered_map>
19 #include <vector>
20
21 CPPTRACE_BEGIN_NAMESPACE
22 namespace detail {
23 namespace libdwarf {
24 std::unique_ptr<symbol_resolver> get_resolver_for_object(const std::string& object_path) {
25 #if IS_APPLE
26 // Check if dSYM exist, if not fallback to debug map
27 if(!directory_exists(object_path + ".dSYM")) {
28 return make_debug_map_resolver(object_path);
29 }
30 #endif
31 return make_dwarf_resolver(object_path);
32 }
33
34 // not thread-safe, replies on caller to lock
35 maybe_owned<symbol_resolver> get_resolver(const std::string& object_name) {
36 // cache resolvers since objects are likely to be traced more than once
37 static std::unordered_map<std::string, std::unique_ptr<symbol_resolver>> resolver_map;
38 auto it = resolver_map.find(object_name);
39 if(it != resolver_map.end()) {
40 return it->second.get();
41 } else {
42 std::unique_ptr<symbol_resolver> resolver_object = get_resolver_for_object(object_name);
43 if(get_cache_mode() == cache_mode::prioritize_speed) {
44 // .emplace needed, for some reason .insert tries to copy <= gcc 7.2
45 return resolver_map.emplace(object_name, std::move(resolver_object)).first->second.get();
46 } else {
47 // gcc 4 has trouble with automatic moves of locals here https://godbolt.org/z/9oWdWjbf8
48 return maybe_owned<symbol_resolver>{std::move(resolver_object)};
49 }
50 }
51 }
52
53 // flatten trace with inlines
54 std::vector<stacktrace_frame> flatten_inlines(std::vector<frame_with_inlines>& trace) {
55 std::vector<stacktrace_frame> final_trace;
56 for(auto& entry : trace) {
57 // most recent call first
58 if(!entry.inlines.empty()) {
59 // insert in reverse order
60 final_trace.insert(
61 final_trace.end(),
62 std::make_move_iterator(entry.inlines.rbegin()),
63 std::make_move_iterator(entry.inlines.rend())
64 );
65 }
66 final_trace.push_back(std::move(entry.frame));
67 if(!entry.inlines.empty()) {
68 // rotate line info due to quirk of how dwarf stores this stuff
69 // inclusive range
70 auto begin = final_trace.end() - (1 + entry.inlines.size());
71 auto end = final_trace.end() - 1;
72 auto carry_line = end->line;
73 auto carry_column = end->column;
74 std::string carry_filename = std::move(end->filename);
75 for(auto it = end; it != begin; it--) {
76 it->line = (it - 1)->line;
77 it->column = (it - 1)->column;
78 it->filename = std::move((it - 1)->filename);
79 }
80 begin->line = carry_line;
81 begin->column = carry_column;
82 begin->filename = std::move(carry_filename);
83 }
84 }
85 return final_trace;
86 }
87
88 #if IS_LINUX || IS_APPLE
89 CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING
90 void try_resolve_jit_frame(const cpptrace::object_frame& dlframe, frame_with_inlines& frame) {
91 auto object_res = lookup_jit_object(dlframe.raw_address);
92 // TODO: At some point, dwarf resolution
93 if(object_res) {
94 frame.frame.symbol = object_res.unwrap().object
95 .lookup_symbol(dlframe.raw_address - object_res.unwrap().base).value_or("");
96 }
97 }
98 #endif
99
100 CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING
101 void try_resolve_frame(
102 symbol_resolver* resolver,
103 const cpptrace::object_frame& dlframe,
104 frame_with_inlines& frame
105 ) {
106 try {
107 frame = resolver->resolve_frame(dlframe);
108 } catch(...) {
109 detail::log_and_maybe_propagate_exception(std::current_exception());
110 frame.frame.raw_address = dlframe.raw_address;
111 frame.frame.object_address = dlframe.object_address;
112 frame.frame.filename = dlframe.object_path;
113 }
114 }
115
116 CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING
117 std::vector<stacktrace_frame> resolve_frames(const std::vector<object_frame>& frames) {
118 std::vector<frame_with_inlines> trace(frames.size(), {null_frame(), {}});
119 // Locking around all libdwarf interaction per https://github.com/davea42/libdwarf-code/discussions/184
120 // And also locking for interactions with get_resolver
121 static std::mutex mutex;
122 const std::lock_guard<std::mutex> lock(mutex);
123 for(const auto& group : collate_frames(frames, trace)) {
124 try {
125 const auto& object_name = group.first;
126 if(object_name.empty()) {
127 #if IS_LINUX || IS_APPLE
128 for(const auto& entry : group.second) {
129 try_resolve_jit_frame(entry.first.get(), entry.second.get());
130 }
131 #endif
132 continue;
133 }
134 // TODO PERF: Potentially a duplicate open and parse with module base stuff (and debug map resolver)
135 #if IS_LINUX
136 auto object = open_elf_cached(object_name);
137 #elif IS_APPLE
138 auto object = open_mach_o_cached(object_name);
139 #endif
140 auto resolver = get_resolver(object_name);
141 for(const auto& entry : group.second) {
142 const auto& dlframe = entry.first.get();
143 auto& frame = entry.second.get();
144 try_resolve_frame(resolver.get(), dlframe, frame);
145 #if IS_LINUX || IS_APPLE
146 // fallback to symbol tables
147 if(frame.frame.symbol.empty() && object.has_value()) {
148 frame.frame.symbol = object
149 .unwrap_value()
150 ->lookup_symbol(dlframe.object_address).value_or("");
151 }
152 #endif
153 }
154 } catch(...) { // NOSONAR
155 detail::log_and_maybe_propagate_exception(std::current_exception());
156 }
157 }
158 // fill in basic info for any frames where there were resolution issues
159 for(std::size_t i = 0; i < frames.size(); i++) {
160 const auto& dlframe = frames[i];
161 auto& frame = trace[i];
162 if(frame.frame == null_frame()) {
163 frame = {
164 {
165 dlframe.raw_address,
166 dlframe.object_address,
167 nullable<std::uint32_t>::null(),
168 nullable<std::uint32_t>::null(),
169 dlframe.object_path,
170 "",
171 false
172 },
173 {}
174 };
175 }
176 }
177 // flatten and finish
178 return flatten_inlines(trace);
179 }
180 }
181 }
182 CPPTRACE_END_NAMESPACE
183
184 #endif