| 56 | } |
| 57 | |
| 58 | CoreFileAnalyzer::CoreFileAnalyzer( |
| 59 | std::string corefile, |
| 60 | std::optional<std::string> executable, |
| 61 | const std::optional<std::string>& lib_search_path) |
| 62 | : d_dwfl(nullptr) |
| 63 | , d_debuginfo_path(nullptr) |
| 64 | , d_callbacks() |
| 65 | , d_filename(std::move(corefile)) |
| 66 | , d_executable(std::move(executable)) |
| 67 | , d_lib_search_path(std::move(lib_search_path)) |
| 68 | , d_fd(0) |
| 69 | , d_elf(nullptr) |
| 70 | { |
| 71 | if (elf_version(EV_CURRENT) == EV_NONE) { |
| 72 | throw ElfAnalyzerError("libelf library ELF version too old"); |
| 73 | } |
| 74 | |
| 75 | d_fd = open(d_filename.c_str(), O_RDONLY); |
| 76 | if (d_fd == -1) { |
| 77 | throw ElfAnalyzerError( |
| 78 | "Failed to open ELF file '" + d_filename + "' (" + std::strerror(errno) + ")"); |
| 79 | } |
| 80 | |
| 81 | d_elf = elf_unique_ptr(elf_begin(d_fd, ELF_C_READ_MMAP, nullptr), elf_end); |
| 82 | if (!d_elf) { |
| 83 | close(d_fd); |
| 84 | throw ElfAnalyzerError("Cannot read elf file"); |
| 85 | } |
| 86 | |
| 87 | std::memset(&d_callbacks, 0, sizeof(d_callbacks)); |
| 88 | d_callbacks.find_elf = pystack_find_elf; |
| 89 | d_callbacks.find_debuginfo = dwfl_standard_find_debuginfo; |
| 90 | d_callbacks.debuginfo_path = &d_debuginfo_path; |
| 91 | |
| 92 | d_dwfl = dwfl_unique_ptr(dwfl_begin(&d_callbacks), dwfl_end); |
| 93 | |
| 94 | if (!d_dwfl) { |
| 95 | throw ElfAnalyzerError("Failed to initialize core analyzer"); |
| 96 | } |
| 97 | |
| 98 | const char* the_executable = d_executable.has_value() ? d_executable.value().c_str() : nullptr; |
| 99 | |
| 100 | if (dwfl_core_file_report(d_dwfl.get(), d_elf.get(), the_executable) < 0 |
| 101 | || dwfl_report_end(d_dwfl.get(), nullptr, nullptr) != 0) |
| 102 | { |
| 103 | throw ElfAnalyzerError( |
| 104 | "Failed to analyze DWARF information for the core file. '" + d_filename |
| 105 | + "' doesn't look like a valid core file."); |
| 106 | } |
| 107 | |
| 108 | resolveLibraries(); |
| 109 | |
| 110 | int result = dwfl_core_file_attach(d_dwfl.get(), d_elf.get()); |
| 111 | if (result < 0) { |
| 112 | throw ElfAnalyzerError( |
| 113 | "Could not attach the core map analyzer. '" + d_filename |
| 114 | + "' doesn't look like a valid core file."); |
| 115 | } |
nothing calls this directly
no test coverage detected