| 1 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF |
| 2 |
|
| 3 |
#include "symbols/dwarf/resolver.hpp" |
| 4 |
|
| 5 |
#include <cpptrace/basic.hpp> |
| 6 |
#include "symbols/dwarf/dwarf.hpp" // has dwarf #includes |
| 7 |
#include "symbols/dwarf/dwarf_utils.hpp" |
| 8 |
#include "symbols/dwarf/dwarf_options.hpp" |
| 9 |
#include "symbols/symbols.hpp" |
| 10 |
#include "utils/common.hpp" |
| 11 |
#include "utils/error.hpp" |
| 12 |
#include "utils/utils.hpp" |
| 13 |
#include "utils/lru_cache.hpp" |
| 14 |
#include "platform/path.hpp" |
| 15 |
#include "platform/program_name.hpp" // For CPPTRACE_MAX_PATH |
| 16 |
#include "logging.hpp" |
| 17 |
|
| 18 |
#if IS_APPLE |
| 19 |
#include "binary/mach-o.hpp" |
| 20 |
#endif |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
#include <cstdint> |
| 24 |
#include <cstdio> |
| 25 |
#include <functional> |
| 26 |
#include <limits> |
| 27 |
#include <memory> |
| 28 |
#include <string> |
| 29 |
#include <type_traits> |
| 30 |
#include <unordered_map> |
| 31 |
#include <vector> |
| 32 |
|
| 33 |
// It's been tricky to piece together how to handle all this dwarf stuff. Some resources I've used are |
| 34 |
// https://www.prevanders.net/libdwarf.pdf |
| 35 |
// https://github.com/davea42/libdwarf-addr2line |
| 36 |
// https://github.com/ruby/ruby/blob/master/addr2line.c |
| 37 |
|
| 38 |
CPPTRACE_BEGIN_NAMESPACE |
| 39 |
namespace detail { |
| 40 |
namespace libdwarf { |
| 41 |
// printbugging as we go |
| 42 |
constexpr bool dump_dwarf = false; |
| 43 |
constexpr bool trace_dwarf = false; |
| 44 |
|
| 45 |
class dwarf_resolver; |
| 46 |
|
| 47 |
// used to describe data from an upstream binary to a resolver for the .dwo |
| 48 |
struct skeleton_info { |
| 49 |
die_object cu_die; |
| 50 |
Dwarf_Half dwversion; |
| 51 |
dwarf_resolver& resolver; |
| 52 |
}; |
| 53 |
|
| 54 |
class dwarf_resolver : public symbol_resolver { |
| 55 |
std::string object_path; |
| 56 |
// dwarf_finish needs to be called after all other dwarf stuff is cleaned up, e.g. `srcfiles` and aranges etc |
| 57 |
// raii_wrapping ensures this is the last thing done after the destructor logic and all other data members are |
| 58 |
// cleaned up |
| 59 |
raii_wrapper<Dwarf_Debug, void(*)(Dwarf_Debug)> dbg{nullptr, [](Dwarf_Debug dbg) { dwarf_finish(dbg); }}; |
| 60 |
bool ok = false; |
| 61 |
// .debug_aranges cache |
| 62 |
Dwarf_Arange* aranges = nullptr; |
| 63 |
Dwarf_Signed arange_count = 0; |
| 64 |
// Map from CU -> Line context |
| 65 |
lru_cache<Dwarf_Off, line_table_info> line_tables{get_dwarf_resolver_line_table_cache_size()}; |
| 66 |
// Map from CU -> Sorted subprograms vector |
| 67 |
using subprogram_map = range_map<Dwarf_Addr, die_object>; |
| 68 |
std::unordered_map<Dwarf_Off, subprogram_map> subprograms_cache; |
| 69 |
// Vector of ranges and their corresponding CU offsets |
| 70 |
struct compile_unit { |
| 71 |
die_object die; |
| 72 |
Dwarf_Half dwversion; |
| 73 |
}; |
| 74 |
range_map<Dwarf_Addr, compile_unit> cu_cache; |
| 75 |
bool generated_cu_cache = false; |
| 76 |
// Map from CU -> {srcfiles, count} |
| 77 |
std::unordered_map<Dwarf_Off, srcfiles> srcfiles_cache; |
| 78 |
// Map from CU -> split full cu resolver |
| 79 |
std::unordered_map<Dwarf_Off, std::unique_ptr<dwarf_resolver>> split_full_cu_resolvers; |
| 80 |
// info for resolving a dwo object |
| 81 |
optional<skeleton_info> skeleton; |
| 82 |
|
| 83 |
private: |
| 84 |
// Error handling helper |
| 85 |
// For some reason R (*f)(Args..., void*)-style deduction isn't possible, seems like a bug in all compilers |
| 86 |
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56190 |
| 87 |
template< |
| 88 |
typename... Args, |
| 89 |
typename... Args2, |
| 90 |
typename std::enable_if< |
| 91 |
std::is_same< |
| 92 |
decltype( |
| 93 |
(void)std::declval<int(Args...)>()(std::forward<Args2>(std::declval<Args2>())..., nullptr) |
| 94 |
), |
| 95 |
void |
| 96 |
>::value, |
| 97 |
int |
| 98 |
>::type = 0 |
| 99 |
> |
| 100 |
int wrap(int (*f)(Args...), Args2&&... args) const { |
| 101 |
Dwarf_Error error = nullptr; |
| 102 |
int ret = f(std::forward<Args2>(args)..., &error); |
| 103 |
if(ret == DW_DLV_ERROR) { |
| 104 |
handle_dwarf_error(dbg, error); |
| 105 |
} |
| 106 |
return ret; |
| 107 |
} |
| 108 |
|
| 109 |
public: |
| 110 |
CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING |
| 111 |
explicit dwarf_resolver(cstring_view object_path_, optional<skeleton_info> split_ = nullopt) |
| 112 |
: object_path(object_path_), |
| 113 |
skeleton(std::move(split_)) |
| 114 |
{ |
| 115 |
// use a buffer when invoking dwarf_init_path, which allows it to automatically find debuglink or dSYM |
| 116 |
// sources |
| 117 |
bool use_buffer = true; |
| 118 |
// for universal / fat mach-o files |
| 119 |
unsigned universal_number = 0; |
| 120 |
#if IS_APPLE |
| 121 |
if(directory_exists(object_path + ".dSYM")) { |
| 122 |
// Possibly depends on the build system but a obj.cpp.o.dSYM/Contents/Resources/DWARF/obj.cpp.o can be |
| 123 |
// created alongside .o files. These are text files containing directives, as opposed to something we |
| 124 |
// can actually use |
| 125 |
std::string dsym_resource = object_path + ".dSYM/Contents/Resources/DWARF/" + basename(object_path); |
| 126 |
if(file_is_mach_o(dsym_resource)) { |
| 127 |
object_path = std::move(dsym_resource); |
| 128 |
} |
| 129 |
use_buffer = false; // we resolved dSYM above as appropriate |
| 130 |
} |
| 131 |
auto result = macho_is_fat(object_path); |
| 132 |
if(result.is_error()) { |
| 133 |
result.drop_error(); |
| 134 |
} else if(result.unwrap_value()) { |
| 135 |
auto mach_o_object = open_mach_o_cached(object_path); |
| 136 |
if(!mach_o_object) { |
| 137 |
ok = false; |
| 138 |
return; |
| 139 |
} |
| 140 |
universal_number = mach_o_object.unwrap_value()->get_fat_index(); |
| 141 |
} |
| 142 |
#endif |
| 143 |
|
| 144 |
// Giving libdwarf a buffer for a true output path is needed for its automatic resolution of debuglink and |
| 145 |
// dSYM files. We don't utilize the dSYM logic here, we just care about debuglink. |
| 146 |
std::unique_ptr<char[]> buffer; |
| 147 |
if(use_buffer) { |
| 148 |
buffer = std::unique_ptr<char[]>(new char[CPPTRACE_MAX_PATH]); |
| 149 |
} |
| 150 |
dwarf_set_de_alloc_flag(0); |
| 151 |
Dwarf_Error error = nullptr; |
| 152 |
auto ret = dwarf_init_path_a( |
| 153 |
object_path.c_str(), |
| 154 |
buffer.get(), |
| 155 |
CPPTRACE_MAX_PATH, |
| 156 |
DW_GROUPNUMBER_ANY, |
| 157 |
universal_number, |
| 158 |
nullptr, |
| 159 |
nullptr, |
| 160 |
&dbg.get(), |
| 161 |
&error |
| 162 |
); |
| 163 |
if(ret == DW_DLV_OK) { |
| 164 |
ok = true; |
| 165 |
} else if(ret == DW_DLV_NO_ENTRY) { |
| 166 |
// fail, no debug info |
| 167 |
ok = false; |
| 168 |
} else if(ret == DW_DLV_ERROR) { |
| 169 |
// fail, parsing error |
| 170 |
ok = false; |
| 171 |
Dwarf_Unsigned ev = dwarf_errno(error); |
| 172 |
auto msg = raii_wrap( |
| 173 |
dwarf_errmsg(error), |
| 174 |
[this, error] (char*) { dwarf_dealloc_error(dbg.get(), error); } |
| 175 |
); |
| 176 |
log::error("dwarf error: dwarf_init_path_a failed with error {} {}", ev, msg.get()); |
| 177 |
} else { |
| 178 |
ok = false; |
| 179 |
PANIC("Unknown return code from dwarf_init_path"); |
| 180 |
} |
| 181 |
|
| 182 |
if(skeleton) { |
| 183 |
VERIFY(wrap(dwarf_set_tied_dbg, dbg, skeleton.unwrap().resolver.dbg) == DW_DLV_OK); |
| 184 |
} |
| 185 |
|
| 186 |
if(ok && !get_dwarf_resolver_disable_aranges()) { |
| 187 |
// Check for .debug_aranges for fast lookup |
| 188 |
wrap(dwarf_get_aranges, dbg, &aranges, &arange_count); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING |
| 193 |
~dwarf_resolver() override { |
| 194 |
if(aranges) { |
| 195 |
for(int i = 0; i < arange_count; i++) { |
| 196 |
dwarf_dealloc(dbg, aranges[i], DW_DLA_ARANGE); |
| 197 |
aranges[i] = nullptr; |
| 198 |
} |
| 199 |
dwarf_dealloc(dbg, aranges, DW_DLA_LIST); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
dwarf_resolver(const dwarf_resolver&) = delete; |
| 204 |
dwarf_resolver& operator=(const dwarf_resolver&) = delete; |
| 205 |
dwarf_resolver(dwarf_resolver&&) = delete; |
| 206 |
dwarf_resolver& operator=(dwarf_resolver&&) = delete; |
| 207 |
|
| 208 |
private: |
| 209 |
// walk all CU's in a dbg, callback is called on each die and should return true to |
| 210 |
// continue traversal |
| 211 |
void walk_compilation_units(const std::function<bool(const die_object&)>& fn) { |
| 212 |
// libdwarf keeps track of where it is in the file, dwarf_next_cu_header_d is statefull |
| 213 |
Dwarf_Unsigned next_cu_header; |
| 214 |
Dwarf_Half header_cu_type; |
| 215 |
while(true) { |
| 216 |
int ret = wrap( |
| 217 |
dwarf_next_cu_header_d, |
| 218 |
dbg, |
| 219 |
true, |
| 220 |
nullptr, |
| 221 |
nullptr, |
| 222 |
nullptr, |
| 223 |
nullptr, |
| 224 |
nullptr, |
| 225 |
nullptr, |
| 226 |
nullptr, |
| 227 |
nullptr, |
| 228 |
&next_cu_header, |
| 229 |
&header_cu_type |
| 230 |
); |
| 231 |
if(ret == DW_DLV_NO_ENTRY) { |
| 232 |
if(dump_dwarf) { |
| 233 |
std::fprintf(stderr, "End walk_dbg\n"); |
| 234 |
} |
| 235 |
return; |
| 236 |
} |
| 237 |
if(ret != DW_DLV_OK) { |
| 238 |
PANIC("Unexpected return code from dwarf_next_cu_header_d"); |
| 239 |
return; |
| 240 |
} |
| 241 |
// 0 passed as the die to the first call of dwarf_siblingof_b immediately after dwarf_next_cu_header_d |
| 242 |
// to fetch the cu die |
| 243 |
die_object cu_die(dbg, nullptr); |
| 244 |
cu_die = cu_die.get_sibling(); |
| 245 |
if(!cu_die) { |
| 246 |
break; |
| 247 |
} |
| 248 |
if(!walk_die_list(cu_die, fn)) { |
| 249 |
break; |
| 250 |
} |
| 251 |
} |
| 252 |
if(dump_dwarf) { |
| 253 |
std::fprintf(stderr, "End walk_compilation_units\n"); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
void lazy_generate_cu_cache() { |
| 258 |
if(!generated_cu_cache) { |
| 259 |
walk_compilation_units([this] (const die_object& cu_die) { |
| 260 |
Dwarf_Half offset_size = 0; |
| 261 |
Dwarf_Half dwversion = 0; |
| 262 |
VERIFY(dwarf_get_version_of_die(cu_die.get(), &dwversion, &offset_size) == DW_DLV_OK); |
| 263 |
if(skeleton) { |
| 264 |
// NOTE: If we have a corresponding skeleton, we assume we have one CU matching the skeleton CU |
| 265 |
// Precedence for this assumption is https://dwarfstd.org/doc/DWARF5.pdf#subsection.3.1.3 |
| 266 |
// TODO: Also assuming same dwversion |
| 267 |
const auto& skeleton_cu = skeleton.unwrap().cu_die; |
| 268 |
auto ranges_vec = skeleton_cu.get_rangelist_entries(skeleton_cu, dwversion); |
| 269 |
if(!ranges_vec.empty()) { |
| 270 |
auto cu_die_handle = cu_cache.add_item({cu_die.clone(), dwversion}); |
| 271 |
for(auto range : ranges_vec) { |
| 272 |
cu_cache.insert(cu_die_handle, range.first, range.second); |
| 273 |
} |
| 274 |
} |
| 275 |
return false; |
| 276 |
} else { |
| 277 |
auto ranges_vec = cu_die.get_rangelist_entries(cu_die, dwversion); |
| 278 |
if(!ranges_vec.empty()) { |
| 279 |
auto cu_die_handle = cu_cache.add_item({cu_die.clone(), dwversion}); |
| 280 |
for(auto range : ranges_vec) { |
| 281 |
cu_cache.insert(cu_die_handle, range.first, range.second); |
| 282 |
} |
| 283 |
} |
| 284 |
return true; |
| 285 |
} |
| 286 |
}); |
| 287 |
cu_cache.finalize(); |
| 288 |
generated_cu_cache = true; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
std::string subprogram_symbol( |
| 293 |
const die_object& die, |
| 294 |
Dwarf_Half dwversion |
| 295 |
) { |
| 296 |
ASSERT(die.get_tag() == DW_TAG_subprogram || die.get_tag() == DW_TAG_inlined_subroutine); |
| 297 |
optional<std::string> name; |
| 298 |
if(auto linkage_name = die.get_string_attribute(DW_AT_linkage_name)) { |
| 299 |
name = std::move(linkage_name); |
| 300 |
} else if(auto linkage_name = die.get_string_attribute(DW_AT_MIPS_linkage_name)) { |
| 301 |
name = std::move(linkage_name); |
| 302 |
} else if(auto linkage_name = die.get_string_attribute(DW_AT_name)) { |
| 303 |
name = std::move(linkage_name); |
| 304 |
} |
| 305 |
if(name.has_value()) { |
| 306 |
return std::move(name).unwrap(); |
| 307 |
} else { |
| 308 |
if(die.has_attr(DW_AT_specification)) { |
| 309 |
die_object spec = die.resolve_reference_attribute(DW_AT_specification); |
| 310 |
return subprogram_symbol(spec, dwversion); |
| 311 |
} else if(die.has_attr(DW_AT_abstract_origin)) { |
| 312 |
die_object spec = die.resolve_reference_attribute(DW_AT_abstract_origin); |
| 313 |
return subprogram_symbol(spec, dwversion); |
| 314 |
} |
| 315 |
} |
| 316 |
return ""; |
| 317 |
} |
| 318 |
|
| 319 |
// despite (some) dwarf using 1-indexing, file_i should be the 0-based index |
| 320 |
std::string resolve_filename(const die_object& cu_die, Dwarf_Unsigned file_i) { |
| 321 |
// for split-dwarf line resolution happens in the skeleton |
| 322 |
if(skeleton) { |
| 323 |
return skeleton.unwrap().resolver.resolve_filename(skeleton.unwrap().cu_die, file_i); |
| 324 |
} |
| 325 |
std::string filename; |
| 326 |
if(get_cache_mode() == cache_mode::prioritize_memory) { |
| 327 |
char** dw_srcfiles; |
| 328 |
Dwarf_Signed dw_filecount; |
| 329 |
VERIFY(wrap(dwarf_srcfiles, cu_die.get(), &dw_srcfiles, &dw_filecount) == DW_DLV_OK); |
| 330 |
srcfiles srcfiles(cu_die.dbg, dw_srcfiles, dw_filecount); |
| 331 |
if(Dwarf_Signed(file_i) < dw_filecount) { |
| 332 |
// dwarf is using 1-indexing |
| 333 |
filename = srcfiles.get(file_i); |
| 334 |
} |
| 335 |
} else { |
| 336 |
auto off = cu_die.get_global_offset(); |
| 337 |
auto it = srcfiles_cache.find(off); |
| 338 |
if(it == srcfiles_cache.end()) { |
| 339 |
char** dw_srcfiles; |
| 340 |
Dwarf_Signed dw_filecount; |
| 341 |
VERIFY(wrap(dwarf_srcfiles, cu_die.get(), &dw_srcfiles, &dw_filecount) == DW_DLV_OK); |
| 342 |
it = srcfiles_cache.emplace_hint(it, off, srcfiles{cu_die.dbg, dw_srcfiles, dw_filecount}); |
| 343 |
} |
| 344 |
if(file_i < it->second.count()) { |
| 345 |
// dwarf is using 1-indexing |
| 346 |
filename = it->second.get(file_i); |
| 347 |
} |
| 348 |
} |
| 349 |
return filename; |
| 350 |
} |
| 351 |
|
| 352 |
void get_inlines_info( |
| 353 |
const die_object& cu_die, |
| 354 |
const die_object& die, |
| 355 |
Dwarf_Addr pc, |
| 356 |
Dwarf_Half dwversion, |
| 357 |
std::vector<stacktrace_frame>& inlines |
| 358 |
) { |
| 359 |
ASSERT(die.get_tag() == DW_TAG_subprogram || die.get_tag() == DW_TAG_inlined_subroutine); |
| 360 |
// get_inlines_info is recursive and recurses into dies with pc ranges matching the pc we're looking for, |
| 361 |
// however, because I wouldn't want anything stack overflowing I'm breaking the recursion out into a loop |
| 362 |
// while looping when we find the target die we need to be able to store a die somewhere that doesn't die |
| 363 |
// at the end of the list traversal, we'll use this as a holder for it |
| 364 |
die_object current_obj_holder(dbg, nullptr); |
| 365 |
optional<std::reference_wrapper<const die_object>> current_die = die; |
| 366 |
while(current_die.has_value()) { |
| 367 |
auto child = current_die.unwrap().get().get_child(); |
| 368 |
if(!child) { |
| 369 |
break; |
| 370 |
} |
| 371 |
optional<std::reference_wrapper<const die_object>> target_die; |
| 372 |
walk_die_list( |
| 373 |
child, |
| 374 |
[this, &cu_die, pc, dwversion, &inlines, &target_die, ¤t_obj_holder] (const die_object& die) { |
| 375 |
if(die.get_tag() == DW_TAG_inlined_subroutine && die.pc_in_die(cu_die, dwversion, pc)) { |
| 376 |
const auto name = subprogram_symbol(die, dwversion); |
| 377 |
auto file_i = die.get_unsigned_attribute(DW_AT_call_file); |
| 378 |
// TODO: Refactor.... Probably put logic in resolve_filename. |
| 379 |
if(file_i) { |
| 380 |
// for dwarf 2, 3, 4, and experimental line table version 0xfe06 1-indexing is used |
| 381 |
// for dwarf 5 0-indexing is used |
| 382 |
optional<line_table_info&> line_table_opt; |
| 383 |
if(skeleton) { |
| 384 |
line_table_opt = skeleton.unwrap().resolver.get_line_table( |
| 385 |
skeleton.unwrap().cu_die |
| 386 |
); |
| 387 |
} else { |
| 388 |
line_table_opt = get_line_table(cu_die); |
| 389 |
} |
| 390 |
if(line_table_opt) { |
| 391 |
auto& line_table = line_table_opt.unwrap(); |
| 392 |
if(line_table.version != 5) { |
| 393 |
if(file_i.unwrap() == 0) { |
| 394 |
file_i.reset(); // 0 means no name to be found |
| 395 |
} else { |
| 396 |
// decrement to 0-based index |
| 397 |
file_i.unwrap()--; |
| 398 |
} |
| 399 |
} |
| 400 |
} else { |
| 401 |
// silently continue |
| 402 |
} |
| 403 |
} |
| 404 |
std::string file = file_i ? resolve_filename(cu_die, file_i.unwrap()) : ""; |
| 405 |
const auto line = die.get_unsigned_attribute(DW_AT_call_line); |
| 406 |
const auto col = die.get_unsigned_attribute(DW_AT_call_column); |
| 407 |
inlines.push_back(stacktrace_frame{ |
| 408 |
0, |
| 409 |
0, // TODO: Could put an object address here... |
| 410 |
{static_cast<std::uint32_t>(line.value_or(0))}, |
| 411 |
{static_cast<std::uint32_t>(col.value_or(0))}, |
| 412 |
file, |
| 413 |
name, |
| 414 |
true |
| 415 |
}); |
| 416 |
current_obj_holder = die.clone(); |
| 417 |
target_die = current_obj_holder; |
| 418 |
return false; |
| 419 |
} else if(die.get_tag() == DW_TAG_lexical_block && die.pc_in_die(cu_die, dwversion, pc)) { |
| 420 |
current_obj_holder = die.clone(); |
| 421 |
target_die = current_obj_holder; |
| 422 |
return false; |
| 423 |
} else { |
| 424 |
return true; |
| 425 |
} |
| 426 |
} |
| 427 |
); |
| 428 |
// recursing into the found target as-if by get_inlines_info(cu_die, die, pc, dwversion, inlines); |
| 429 |
current_die = target_die; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
std::string retrieve_symbol_for_subprogram( |
| 434 |
const die_object& cu_die, |
| 435 |
const die_object& die, |
| 436 |
Dwarf_Addr pc, |
| 437 |
Dwarf_Half dwversion, |
| 438 |
std::vector<stacktrace_frame>& inlines |
| 439 |
) { |
| 440 |
ASSERT(die.get_tag() == DW_TAG_subprogram); |
| 441 |
const auto name = subprogram_symbol(die, dwversion); |
| 442 |
if(should_resolve_inlined_calls()) { |
| 443 |
get_inlines_info(cu_die, die, pc, dwversion, inlines); |
| 444 |
} |
| 445 |
return name; |
| 446 |
} |
| 447 |
|
| 448 |
// returns true if this call found the symbol |
| 449 |
CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING |
| 450 |
bool retrieve_symbol_walk( |
| 451 |
const die_object& cu_die, |
| 452 |
const die_object& die, |
| 453 |
Dwarf_Addr pc, |
| 454 |
Dwarf_Half dwversion, |
| 455 |
stacktrace_frame& frame, |
| 456 |
std::vector<stacktrace_frame>& inlines |
| 457 |
) { |
| 458 |
bool found = false; |
| 459 |
walk_die_list( |
| 460 |
die, |
| 461 |
[this, &cu_die, pc, dwversion, &frame, &inlines, &found] (const die_object& die) { |
| 462 |
if(dump_dwarf) { |
| 463 |
std::fprintf( |
| 464 |
stderr, |
| 465 |
"-------------> %08llx %s %s\n", |
| 466 |
to_ull(die.get_global_offset()), |
| 467 |
die.get_tag_name(), |
| 468 |
die.get_name().c_str() |
| 469 |
); |
| 470 |
} |
| 471 |
if(!(die.get_tag() == DW_TAG_namespace || die.pc_in_die(cu_die, dwversion, pc))) { |
| 472 |
if(dump_dwarf) { |
| 473 |
std::fprintf(stderr, "pc not in die\n"); |
| 474 |
} |
| 475 |
} else { |
| 476 |
if(trace_dwarf) { |
| 477 |
std::fprintf( |
| 478 |
stderr, |
| 479 |
"%s %08llx %s\n", |
| 480 |
die.get_tag() == DW_TAG_namespace ? "pc maybe in die (namespace)" : "pc in die", |
| 481 |
to_ull(die.get_global_offset()), |
| 482 |
die.get_tag_name() |
| 483 |
); |
| 484 |
} |
| 485 |
if(die.get_tag() == DW_TAG_subprogram) { |
| 486 |
frame.symbol = retrieve_symbol_for_subprogram(cu_die, die, pc, dwversion, inlines); |
| 487 |
found = true; |
| 488 |
return false; |
| 489 |
} |
| 490 |
auto child = die.get_child(); |
| 491 |
if(child) { |
| 492 |
if(retrieve_symbol_walk(cu_die, child, pc, dwversion, frame, inlines)) { |
| 493 |
found = true; |
| 494 |
return false; |
| 495 |
} |
| 496 |
} else { |
| 497 |
if(dump_dwarf) { |
| 498 |
std::fprintf(stderr, "(no child)\n"); |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
return true; |
| 503 |
} |
| 504 |
); |
| 505 |
if(dump_dwarf) { |
| 506 |
std::fprintf(stderr, "End walk_die_list\n"); |
| 507 |
} |
| 508 |
return found; |
| 509 |
} |
| 510 |
|
| 511 |
CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING |
| 512 |
void preprocess_subprograms( |
| 513 |
const die_object& cu_die, |
| 514 |
const die_object& die, |
| 515 |
Dwarf_Half dwversion, |
| 516 |
subprogram_map& subprogram_cache |
| 517 |
) { |
| 518 |
walk_die_list( |
| 519 |
die, |
| 520 |
[this, &cu_die, dwversion, &subprogram_cache] (const die_object& die) { |
| 521 |
switch(die.get_tag()) { |
| 522 |
case DW_TAG_subprogram: |
| 523 |
{ |
| 524 |
auto ranges_vec = die.get_rangelist_entries(cu_die, dwversion); |
| 525 |
// TODO: Feels super inefficient and some day should maybe use an interval tree. |
| 526 |
if(!ranges_vec.empty()) { |
| 527 |
auto die_handle = subprogram_cache.add_item(die.clone()); |
| 528 |
for(auto range : ranges_vec) { |
| 529 |
subprogram_cache.insert(die_handle, range.first, range.second); |
| 530 |
} |
| 531 |
} |
| 532 |
// Walk children to get things like lambdas |
| 533 |
// TODO: Somehow find a way to get better names here? For gcc it's just "operator()" |
| 534 |
// On clang it's better |
| 535 |
auto child = die.get_child(); |
| 536 |
if(child) { |
| 537 |
preprocess_subprograms(cu_die, child, dwversion, subprogram_cache); |
| 538 |
} |
| 539 |
} |
| 540 |
break; |
| 541 |
case DW_TAG_namespace: |
| 542 |
case DW_TAG_structure_type: |
| 543 |
case DW_TAG_class_type: |
| 544 |
case DW_TAG_module: |
| 545 |
case DW_TAG_imported_module: |
| 546 |
case DW_TAG_compile_unit: |
| 547 |
{ |
| 548 |
auto child = die.get_child(); |
| 549 |
if(child) { |
| 550 |
preprocess_subprograms(cu_die, child, dwversion, subprogram_cache); |
| 551 |
} |
| 552 |
} |
| 553 |
break; |
| 554 |
default: |
| 555 |
break; |
| 556 |
} |
| 557 |
return true; |
| 558 |
} |
| 559 |
); |
| 560 |
if(dump_dwarf) { |
| 561 |
std::fprintf(stderr, "End walk_die_list\n"); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING |
| 566 |
void retrieve_symbol( |
| 567 |
const die_object& cu_die, |
| 568 |
Dwarf_Addr pc, |
| 569 |
Dwarf_Half dwversion, |
| 570 |
stacktrace_frame& frame, |
| 571 |
std::vector<stacktrace_frame>& inlines |
| 572 |
) { |
| 573 |
if(get_cache_mode() == cache_mode::prioritize_memory) { |
| 574 |
retrieve_symbol_walk(cu_die, cu_die, pc, dwversion, frame, inlines); |
| 575 |
} else { |
| 576 |
auto off = cu_die.get_global_offset(); |
| 577 |
auto it = subprograms_cache.find(off); |
| 578 |
if(it == subprograms_cache.end()) { |
| 579 |
// TODO: Refactor. Do the sort in the preprocess function and return the vec directly. |
| 580 |
subprogram_map subprogram_cache; |
| 581 |
preprocess_subprograms(cu_die, cu_die, dwversion, subprogram_cache); |
| 582 |
subprogram_cache.finalize(); |
| 583 |
subprograms_cache.emplace(off, std::move(subprogram_cache)); |
| 584 |
it = subprograms_cache.find(off); |
| 585 |
} |
| 586 |
const auto& subprogram_cache = it->second; |
| 587 |
auto maybe_die = subprogram_cache.lookup(pc); |
| 588 |
// If the vector has been empty this can happen |
| 589 |
if(maybe_die.has_value() && maybe_die.unwrap().pc_in_die(cu_die, dwversion, pc)) { |
| 590 |
frame.symbol = retrieve_symbol_for_subprogram(cu_die, maybe_die.unwrap(), pc, dwversion, inlines); |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
// returns a reference to a CU's line table, may be invalidated if the line_tables map is modified |
| 596 |
CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING |
| 597 |
optional<line_table_info&> get_line_table(const die_object& cu_die) { |
| 598 |
auto off = cu_die.get_global_offset(); |
| 599 |
auto res = line_tables.maybe_get(off); |
| 600 |
if(res) { |
| 601 |
return res; |
| 602 |
} else { |
| 603 |
Dwarf_Unsigned version; |
| 604 |
Dwarf_Small table_count; |
| 605 |
Dwarf_Line_Context line_context; |
| 606 |
int ret = wrap( |
| 607 |
dwarf_srclines_b, |
| 608 |
cu_die.get(), |
| 609 |
&version, |
| 610 |
&table_count, |
| 611 |
&line_context |
| 612 |
); |
| 613 |
static_assert(std::is_unsigned<decltype(table_count)>::value, "Expected unsigned Dwarf_Small"); |
| 614 |
VERIFY(/*table_count >= 0 &&*/ table_count <= 2, "Unknown dwarf line table count"); |
| 615 |
if(ret == DW_DLV_NO_ENTRY) { |
| 616 |
// TODO: Failing silently for now |
| 617 |
return nullopt; |
| 618 |
} |
| 619 |
VERIFY(ret == DW_DLV_OK); |
| 620 |
|
| 621 |
std::vector<line_entry> line_entries; |
| 622 |
|
| 623 |
if(get_cache_mode() == cache_mode::prioritize_speed) { |
| 624 |
// build lookup table |
| 625 |
Dwarf_Line* line_buffer = nullptr; |
| 626 |
Dwarf_Signed line_count = 0; |
| 627 |
Dwarf_Line* linebuf_actuals = nullptr; |
| 628 |
Dwarf_Signed linecount_actuals = 0; |
| 629 |
VERIFY( |
| 630 |
wrap( |
| 631 |
dwarf_srclines_two_level_from_linecontext, |
| 632 |
line_context, |
| 633 |
&line_buffer, |
| 634 |
&line_count, |
| 635 |
&linebuf_actuals, |
| 636 |
&linecount_actuals |
| 637 |
) == DW_DLV_OK |
| 638 |
); |
| 639 |
|
| 640 |
// TODO: Make any attempt to note PC ranges? Handle line end sequence? |
| 641 |
line_entries.reserve(line_count); |
| 642 |
for(int i = 0; i < line_count; i++) { |
| 643 |
Dwarf_Line line = line_buffer[i]; |
| 644 |
Dwarf_Addr low_addr = 0; |
| 645 |
VERIFY(wrap(dwarf_lineaddr, line, &low_addr) == DW_DLV_OK); |
| 646 |
// scan ahead for the last line entry matching this pc |
| 647 |
int j; |
| 648 |
for(j = i + 1; j < line_count; j++) { |
| 649 |
Dwarf_Addr addr = 0; |
| 650 |
VERIFY(wrap(dwarf_lineaddr, line_buffer[j], &addr) == DW_DLV_OK); |
| 651 |
if(addr != low_addr) { |
| 652 |
break; |
| 653 |
} |
| 654 |
} |
| 655 |
line = line_buffer[j - 1]; |
| 656 |
line_entries.push_back({ |
| 657 |
low_addr, |
| 658 |
line |
| 659 |
}); |
| 660 |
i = j - 1; |
| 661 |
} |
| 662 |
// sort lines |
| 663 |
std::sort(line_entries.begin(), line_entries.end(), [] (const line_entry& a, const line_entry& b) { |
| 664 |
return a.low < b.low; |
| 665 |
}); |
| 666 |
} |
| 667 |
|
| 668 |
return line_tables.insert(off, line_table_info{version, line_context, std::move(line_entries)}); |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING |
| 673 |
void retrieve_line_info( |
| 674 |
const die_object& cu_die, |
| 675 |
Dwarf_Addr pc, |
| 676 |
stacktrace_frame& frame |
| 677 |
) { |
| 678 |
// For debug fission the skeleton debug info will have the line table |
| 679 |
if(skeleton) { |
| 680 |
return skeleton.unwrap().resolver.retrieve_line_info(skeleton.unwrap().cu_die, pc, frame); |
| 681 |
} |
| 682 |
auto table_info_opt = get_line_table(cu_die); |
| 683 |
if(!table_info_opt) { |
| 684 |
return; // failing silently for now |
| 685 |
} |
| 686 |
auto& table_info = table_info_opt.unwrap(); |
| 687 |
if(get_cache_mode() == cache_mode::prioritize_speed) { |
| 688 |
// Lookup in the table |
| 689 |
auto& line_entries = table_info.line_entries; |
| 690 |
auto table_it = first_less_than_or_equal( |
| 691 |
line_entries.begin(), |
| 692 |
line_entries.end(), |
| 693 |
pc, |
| 694 |
[] (Dwarf_Addr pc, const line_entry& entry) { |
| 695 |
return pc < entry.low; |
| 696 |
} |
| 697 |
); |
| 698 |
// If the vector has been empty this can happen |
| 699 |
if(table_it != line_entries.end()) { |
| 700 |
Dwarf_Line line = table_it->line; |
| 701 |
// line number |
| 702 |
if(!table_it->line_number) { |
| 703 |
Dwarf_Unsigned line_number = 0; |
| 704 |
VERIFY(wrap(dwarf_lineno, line, &line_number) == DW_DLV_OK); |
| 705 |
table_it->line_number = static_cast<std::uint32_t>(line_number); |
| 706 |
} |
| 707 |
frame.line = table_it->line_number.unwrap(); |
| 708 |
// column number |
| 709 |
if(!table_it->column_number) { |
| 710 |
Dwarf_Unsigned column_number = 0; |
| 711 |
VERIFY(wrap(dwarf_lineoff_b, line, &column_number) == DW_DLV_OK); |
| 712 |
table_it->column_number = static_cast<std::uint32_t>(column_number); |
| 713 |
} |
| 714 |
frame.column = table_it->column_number.unwrap(); |
| 715 |
// filename |
| 716 |
if(!table_it->path) { |
| 717 |
char* filename = nullptr; |
| 718 |
VERIFY(wrap(dwarf_linesrc, line, &filename) == DW_DLV_OK); |
| 719 |
auto wrapper = raii_wrap( |
| 720 |
filename, |
| 721 |
[this] (char* str) { if(str) dwarf_dealloc(dbg, str, DW_DLA_STRING); } |
| 722 |
); |
| 723 |
table_it->path = filename; |
| 724 |
} |
| 725 |
frame.filename = table_it->path.unwrap(); |
| 726 |
} |
| 727 |
} else { |
| 728 |
Dwarf_Line_Context line_context = table_info.line_context; |
| 729 |
// walk for it |
| 730 |
Dwarf_Line* line_buffer = nullptr; |
| 731 |
Dwarf_Signed line_count = 0; |
| 732 |
Dwarf_Line* linebuf_actuals = nullptr; |
| 733 |
Dwarf_Signed linecount_actuals = 0; |
| 734 |
VERIFY( |
| 735 |
wrap( |
| 736 |
dwarf_srclines_two_level_from_linecontext, |
| 737 |
line_context, |
| 738 |
&line_buffer, |
| 739 |
&line_count, |
| 740 |
&linebuf_actuals, |
| 741 |
&linecount_actuals |
| 742 |
) == DW_DLV_OK |
| 743 |
); |
| 744 |
Dwarf_Addr last_lineaddr = 0; |
| 745 |
Dwarf_Line last_line = nullptr; |
| 746 |
for(int i = 0; i < line_count; i++) { |
| 747 |
Dwarf_Line line = line_buffer[i]; |
| 748 |
Dwarf_Addr lineaddr = 0; |
| 749 |
VERIFY(wrap(dwarf_lineaddr, line, &lineaddr) == DW_DLV_OK); |
| 750 |
Dwarf_Line found_line = nullptr; |
| 751 |
if(pc == lineaddr) { |
| 752 |
// Multiple PCs may correspond to a line, find the last one |
| 753 |
found_line = line; |
| 754 |
for(int j = i + 1; j < line_count; j++) { |
| 755 |
Dwarf_Line line = line_buffer[j]; |
| 756 |
Dwarf_Addr lineaddr = 0; |
| 757 |
VERIFY(wrap(dwarf_lineaddr, line, &lineaddr) == DW_DLV_OK); |
| 758 |
if(pc == lineaddr) { |
| 759 |
found_line = line; |
| 760 |
} |
| 761 |
} |
| 762 |
} else if(last_line && pc > last_lineaddr && pc < lineaddr) { |
| 763 |
// Guess that the last line had it |
| 764 |
found_line = last_line; |
| 765 |
} |
| 766 |
if(found_line) { |
| 767 |
Dwarf_Unsigned line_number = 0; |
| 768 |
VERIFY(wrap(dwarf_lineno, found_line, &line_number) == DW_DLV_OK); |
| 769 |
frame.line = static_cast<std::uint32_t>(line_number); |
| 770 |
char* filename = nullptr; |
| 771 |
VERIFY(wrap(dwarf_linesrc, found_line, &filename) == DW_DLV_OK); |
| 772 |
auto wrapper = raii_wrap( |
| 773 |
filename, |
| 774 |
[this] (char* str) { if(str) dwarf_dealloc(dbg, str, DW_DLA_STRING); } |
| 775 |
); |
| 776 |
frame.filename = filename; |
| 777 |
} else { |
| 778 |
Dwarf_Bool is_line_end; |
| 779 |
VERIFY(wrap(dwarf_lineendsequence, line, &is_line_end) == DW_DLV_OK); |
| 780 |
if(is_line_end) { |
| 781 |
last_lineaddr = 0; |
| 782 |
last_line = nullptr; |
| 783 |
} else { |
| 784 |
last_lineaddr = lineaddr; |
| 785 |
last_line = line; |
| 786 |
} |
| 787 |
} |
| 788 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
struct cu_info { |
| 793 |
maybe_owned_die_object cu_die; |
| 794 |
Dwarf_Half dwversion; |
| 795 |
}; |
| 796 |
|
| 797 |
// CU resolution has three paths: |
| 798 |
// - If aranges are present, the pc is looked up in aranges (falls through to next cases if not in aranges) |
| 799 |
// - If cache mode is prioritize memory, the CUs are walked for a match |
| 800 |
// - Otherwise a CU cache is built up and CUs are looked up in the map |
| 801 |
CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING |
| 802 |
optional<cu_info> lookup_cu(Dwarf_Addr pc) { |
| 803 |
// Check for .debug_aranges for fast lookup |
| 804 |
if(aranges && !skeleton) { // don't bother under split dwarf |
| 805 |
// Try to find pc in aranges |
| 806 |
Dwarf_Arange arange; |
| 807 |
if(wrap(dwarf_get_arange, aranges, arange_count, pc, &arange) == DW_DLV_OK) { |
| 808 |
// Address in table, load CU die |
| 809 |
Dwarf_Off cu_die_offset; |
| 810 |
VERIFY(wrap(dwarf_get_cu_die_offset, arange, &cu_die_offset) == DW_DLV_OK); |
| 811 |
Dwarf_Die raw_die; |
| 812 |
// Setting is_info = true for now, assuming in .debug_info rather than .debug_types |
| 813 |
VERIFY(wrap(dwarf_offdie_b, dbg, cu_die_offset, true, &raw_die) == DW_DLV_OK); |
| 814 |
die_object cu_die(dbg, raw_die); |
| 815 |
Dwarf_Half offset_size = 0; |
| 816 |
Dwarf_Half dwversion = 0; |
| 817 |
VERIFY(dwarf_get_version_of_die(cu_die.get(), &dwversion, &offset_size) == DW_DLV_OK); |
| 818 |
if(trace_dwarf) { |
| 819 |
std::fprintf(stderr, "Found CU in aranges\n"); |
| 820 |
cu_die.print(); |
| 821 |
} |
| 822 |
return cu_info{maybe_owned_die_object::owned(std::move(cu_die)), dwversion}; |
| 823 |
} |
| 824 |
} |
| 825 |
// otherwise, or if not in aranges |
| 826 |
// one reason to fallback here is if the compilation has dwarf generated from different compilers and only |
| 827 |
// some of them generate aranges (e.g. static linking with cpptrace after specifying clang++ as the c++ |
| 828 |
// compiler while the C compiler defaults to an older gcc) |
| 829 |
if(get_cache_mode() == cache_mode::prioritize_memory) { |
| 830 |
// walk for the cu and go from there |
| 831 |
optional<cu_info> info; |
| 832 |
walk_compilation_units([this, pc, &info] (const die_object& cu_die) { |
| 833 |
Dwarf_Half offset_size = 0; |
| 834 |
Dwarf_Half dwversion = 0; |
| 835 |
dwarf_get_version_of_die(cu_die.get(), &dwversion, &offset_size); |
| 836 |
//auto p = cu_die.get_pc_range(dwversion); |
| 837 |
//cu_die.print(); |
| 838 |
//fprintf(stderr, " %llx, %llx\n", p.first, p.second); |
| 839 |
if(trace_dwarf) { |
| 840 |
std::fprintf(stderr, "CU: %d %s\n", dwversion, cu_die.get_name().c_str()); |
| 841 |
} |
| 842 |
// NOTE: If we have a corresponding skeleton, we assume we have one CU matching the skeleton CU |
| 843 |
if( |
| 844 |
( |
| 845 |
skeleton |
| 846 |
&& skeleton.unwrap().cu_die.pc_in_die( |
| 847 |
skeleton.unwrap().cu_die, |
| 848 |
skeleton.unwrap().dwversion, |
| 849 |
pc |
| 850 |
) |
| 851 |
) || cu_die.pc_in_die(cu_die, dwversion, pc) |
| 852 |
) { |
| 853 |
if(trace_dwarf) { |
| 854 |
std::fprintf( |
| 855 |
stderr, |
| 856 |
"pc in die %08llx %s (now searching for %08llx)\n", |
| 857 |
to_ull(cu_die.get_global_offset()), |
| 858 |
cu_die.get_tag_name(), |
| 859 |
to_ull(pc) |
| 860 |
); |
| 861 |
} |
| 862 |
info = cu_info{maybe_owned_die_object::owned(cu_die.clone()), dwversion}; |
| 863 |
return false; |
| 864 |
} |
| 865 |
return true; |
| 866 |
}); |
| 867 |
return info; |
| 868 |
} else { |
| 869 |
lazy_generate_cu_cache(); |
| 870 |
// look up the cu |
| 871 |
auto res = cu_cache.lookup(pc); |
| 872 |
// res can be nullopt if the cu_cache vector is empty |
| 873 |
// It can also happen for something like _start, where there is a cached CU for the object but |
| 874 |
// _start is outside of the CU's PC range |
| 875 |
if(res) { |
| 876 |
const auto& die = res.unwrap().die; |
| 877 |
const auto dwversion = res.unwrap().dwversion; |
| 878 |
// TODO: Cache the range list? |
| 879 |
// NOTE: If we have a corresponding skeleton, we assume we have one CU matching the skeleton CU |
| 880 |
if( |
| 881 |
( |
| 882 |
skeleton |
| 883 |
&& skeleton.unwrap().cu_die.pc_in_die( |
| 884 |
skeleton.unwrap().cu_die, |
| 885 |
skeleton.unwrap().dwversion, |
| 886 |
pc |
| 887 |
) |
| 888 |
) || die.pc_in_die(die, dwversion, pc) |
| 889 |
) { |
| 890 |
return cu_info{maybe_owned_die_object::ref(die), dwversion}; |
| 891 |
} |
| 892 |
} |
| 893 |
return nullopt; |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
optional<std::string> get_dwo_name(const die_object& cu_die) { |
| 898 |
if(auto dwo_name = cu_die.get_string_attribute(DW_AT_GNU_dwo_name)) { |
| 899 |
return dwo_name; |
| 900 |
} else if(auto dwo_name = cu_die.get_string_attribute(DW_AT_dwo_name)) { |
| 901 |
return dwo_name; |
| 902 |
} else { |
| 903 |
return nullopt; |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
void perform_dwarf_fission_resolution( |
| 908 |
const die_object& cu_die, |
| 909 |
const optional<std::string>& dwo_name, |
| 910 |
const object_frame& object_frame_info, |
| 911 |
stacktrace_frame& frame, |
| 912 |
std::vector<stacktrace_frame>& inlines |
| 913 |
) { |
| 914 |
// Split dwarf / debug fission / dwo is handled here |
| 915 |
// Location of the split full CU is a combination of DW_AT_dwo_name/DW_AT_GNU_dwo_name and DW_AT_comp_dir |
| 916 |
// https://gcc.gnu.org/wiki/DebugFission |
| 917 |
if(dwo_name) { |
| 918 |
// TODO: DWO ID? |
| 919 |
auto comp_dir = cu_die.get_string_attribute(DW_AT_comp_dir); |
| 920 |
Dwarf_Half offset_size = 0; |
| 921 |
Dwarf_Half dwversion = 0; |
| 922 |
dwarf_get_version_of_die(cu_die.get(), &dwversion, &offset_size); |
| 923 |
std::string path; |
| 924 |
if(is_absolute(dwo_name.unwrap())) { |
| 925 |
path = dwo_name.unwrap(); |
| 926 |
} else if(comp_dir) { |
| 927 |
path = comp_dir.unwrap() + PATH_SEP + dwo_name.unwrap(); |
| 928 |
} else { |
| 929 |
// maybe default to dwo_name but for now not doing anything |
| 930 |
return; |
| 931 |
} |
| 932 |
// todo: slight inefficiency in this copy-back strategy due to other frame members |
| 933 |
frame_with_inlines res; |
| 934 |
if(get_cache_mode() == cache_mode::prioritize_memory) { |
| 935 |
dwarf_resolver resolver( |
| 936 |
path, |
| 937 |
skeleton_info{cu_die.clone(), dwversion, *this} |
| 938 |
); |
| 939 |
res = resolver.resolve_frame(object_frame_info); |
| 940 |
} else { |
| 941 |
auto off = cu_die.get_global_offset(); |
| 942 |
auto it = split_full_cu_resolvers.find(off); |
| 943 |
if(it == split_full_cu_resolvers.end()) { |
| 944 |
it = split_full_cu_resolvers.emplace( |
| 945 |
off, |
| 946 |
detail::make_unique<dwarf_resolver>(path, skeleton_info{cu_die.clone(), dwversion, *this}) |
| 947 |
).first; |
| 948 |
} |
| 949 |
res = it->second->resolve_frame(object_frame_info); |
| 950 |
} |
| 951 |
frame = std::move(res.frame); |
| 952 |
inlines = std::move(res.inlines); |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING |
| 957 |
void resolve_frame_core( |
| 958 |
const object_frame& object_frame_info, |
| 959 |
stacktrace_frame& frame, |
| 960 |
std::vector<stacktrace_frame>& inlines |
| 961 |
) { |
| 962 |
auto pc = object_frame_info.object_address; |
| 963 |
if(dump_dwarf) { |
| 964 |
std::fprintf(stderr, "%s\n", object_path.c_str()); |
| 965 |
std::fprintf(stderr, "%llx\n", to_ull(pc)); |
| 966 |
} |
| 967 |
optional<cu_info> cu = lookup_cu(pc); |
| 968 |
if(cu) { |
| 969 |
const auto& cu_die = cu.unwrap().cu_die.get(); |
| 970 |
// gnu non-standard debug-fission may create non-skeleton CU DIEs and just add dwo attributes |
| 971 |
// clang emits dwo names in the split CUs, so guard against going down the dwarf fission path (which |
| 972 |
// doesn't infinitely recurse because it's not emitted as an absolute path and there's no comp dir but |
| 973 |
// it's good to guard against the infinite recursion anyway) |
| 974 |
auto dwo_name = get_dwo_name(cu_die); |
| 975 |
if(cu_die.get_tag() == DW_TAG_skeleton_unit || (dwo_name && !skeleton)) { |
| 976 |
perform_dwarf_fission_resolution(cu_die, dwo_name, object_frame_info, frame, inlines); |
| 977 |
} else { |
| 978 |
retrieve_line_info(cu_die, pc, frame); |
| 979 |
retrieve_symbol(cu_die, pc, cu.unwrap().dwversion, frame, inlines); |
| 980 |
} |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
public: |
| 985 |
CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING |
| 986 |
frame_with_inlines resolve_frame(const object_frame& frame_info) override { |
| 987 |
if(!ok) { |
| 988 |
return { |
| 989 |
{ |
| 990 |
frame_info.raw_address, |
| 991 |
frame_info.object_address, |
| 992 |
nullable<std::uint32_t>::null(), |
| 993 |
nullable<std::uint32_t>::null(), |
| 994 |
frame_info.object_path, |
| 995 |
"", |
| 996 |
false |
| 997 |
}, |
| 998 |
{} |
| 999 |
}; |
| 1000 |
} |
| 1001 |
stacktrace_frame frame = null_frame(); |
| 1002 |
frame.filename = frame_info.object_path; |
| 1003 |
frame.raw_address = frame_info.raw_address; |
| 1004 |
frame.object_address = frame_info.object_address; |
| 1005 |
if(trace_dwarf) { |
| 1006 |
std::fprintf( |
| 1007 |
stderr, |
| 1008 |
"Starting resolution for %s %08llx\n", |
| 1009 |
object_path.c_str(), |
| 1010 |
to_ull(frame_info.object_address) |
| 1011 |
); |
| 1012 |
} |
| 1013 |
std::vector<stacktrace_frame> inlines; |
| 1014 |
resolve_frame_core( |
| 1015 |
frame_info, |
| 1016 |
frame, |
| 1017 |
inlines |
| 1018 |
); |
| 1019 |
return {std::move(frame), std::move(inlines)}; |
| 1020 |
} |
| 1021 |
}; |
| 1022 |
|
| 1023 |
std::unique_ptr<symbol_resolver> make_dwarf_resolver(cstring_view object_path) { |
| 1024 |
return detail::make_unique<dwarf_resolver>(object_path); |
| 1025 |
} |
| 1026 |
} |
| 1027 |
} |
| 1028 |
CPPTRACE_END_NAMESPACE |
| 1029 |
|
| 1030 |
#endif |