| 1 |
#ifndef DWARF_UTILS_HPP |
| 2 |
#define DWARF_UTILS_HPP |
| 3 |
|
| 4 |
#include <cpptrace/basic.hpp> |
| 5 |
#include "symbols/dwarf/dwarf.hpp" // has dwarf #includes |
| 6 |
#include "utils/error.hpp" |
| 7 |
#include "utils/microfmt.hpp" |
| 8 |
#include "utils/utils.hpp" |
| 9 |
|
| 10 |
CPPTRACE_BEGIN_NAMESPACE |
| 11 |
namespace detail { |
| 12 |
namespace libdwarf { |
| 13 |
class srcfiles { |
| 14 |
Dwarf_Debug dbg = nullptr; |
| 15 |
char** dw_srcfiles = nullptr; |
| 16 |
Dwarf_Unsigned dw_filecount = 0; |
| 17 |
|
| 18 |
public: |
| 19 |
srcfiles(Dwarf_Debug dbg, char** dw_srcfiles, Dwarf_Signed filecount) |
| 20 |
: dbg(dbg), dw_srcfiles(dw_srcfiles), dw_filecount(static_cast<Dwarf_Unsigned>(filecount)) |
| 21 |
{ |
| 22 |
if(filecount < 0) { |
| 23 |
throw internal_error("Unexpected dw_filecount {}", filecount); |
| 24 |
} |
| 25 |
} |
| 26 |
~srcfiles() { |
| 27 |
release(); |
| 28 |
} |
| 29 |
void release() { |
| 30 |
if(dw_srcfiles) { |
| 31 |
for(unsigned i = 0; i < dw_filecount; i++) { |
| 32 |
dwarf_dealloc(dbg, dw_srcfiles[i], DW_DLA_STRING); |
| 33 |
dw_srcfiles[i] = nullptr; |
| 34 |
} |
| 35 |
dwarf_dealloc(dbg, dw_srcfiles, DW_DLA_LIST); |
| 36 |
dw_srcfiles = nullptr; |
| 37 |
} |
| 38 |
} |
| 39 |
srcfiles(const srcfiles&) = delete; |
| 40 |
srcfiles(srcfiles&& other) { |
| 41 |
*this = std::move(other); |
| 42 |
} |
| 43 |
srcfiles& operator=(const srcfiles&) = delete; |
| 44 |
srcfiles& operator=(srcfiles&& other) { |
| 45 |
release(); |
| 46 |
dbg = exchange(other.dbg, nullptr); |
| 47 |
dw_srcfiles = exchange(other.dw_srcfiles, nullptr); |
| 48 |
dw_filecount = exchange(other.dw_filecount, 0); |
| 49 |
return *this; |
| 50 |
} |
| 51 |
// note: dwarf uses 1-indexing |
| 52 |
const char* get(Dwarf_Unsigned file_i) const { |
| 53 |
if(file_i >= dw_filecount) { |
| 54 |
throw internal_error( |
| 55 |
"Error while accessing the srcfiles list, requested index {} is out of bounds (count = {})", |
| 56 |
file_i, |
| 57 |
dw_filecount |
| 58 |
); |
| 59 |
} |
| 60 |
return dw_srcfiles[file_i]; |
| 61 |
} |
| 62 |
Dwarf_Unsigned count() const { |
| 63 |
return dw_filecount; |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
// container of items which are keyed by ranges |
| 68 |
template<typename K, typename V> |
| 69 |
class range_map { |
| 70 |
public: |
| 71 |
struct handle { |
| 72 |
std::uint32_t index; |
| 73 |
}; |
| 74 |
private: |
| 75 |
struct PACKED range_entry { |
| 76 |
handle item; |
| 77 |
K low; |
| 78 |
K high; |
| 79 |
}; |
| 80 |
std::vector<V> items; |
| 81 |
std::vector<range_entry> range_entries; |
| 82 |
public: |
| 83 |
handle add_item(V&& item) { |
| 84 |
items.push_back(std::move(item)); |
| 85 |
VERIFY(items.size() < std::numeric_limits<std::uint32_t>::max()); |
| 86 |
return handle{static_cast<std::uint32_t>(items.size() - 1)}; |
| 87 |
} |
| 88 |
void insert(handle handle, K low, K high) { |
| 89 |
range_entries.push_back({handle, low, high}); |
| 90 |
} |
| 91 |
void finalize() { |
| 92 |
std::sort(range_entries.begin(), range_entries.end(), [] (const range_entry& a, const range_entry& b) { |
| 93 |
return a.low < b.low; |
| 94 |
}); |
| 95 |
} |
| 96 |
std::size_t ranges_count() const { |
| 97 |
return range_entries.size(); |
| 98 |
} |
| 99 |
|
| 100 |
optional<const V&> lookup(K key) const { |
| 101 |
auto vec_it = first_less_than_or_equal( |
| 102 |
range_entries.begin(), |
| 103 |
range_entries.end(), |
| 104 |
key, |
| 105 |
[] (K key, const range_entry& entry) { |
| 106 |
return key < entry.low; |
| 107 |
} |
| 108 |
); |
| 109 |
if(vec_it == range_entries.end()) { |
| 110 |
return nullopt; |
| 111 |
} |
| 112 |
return items.at(vec_it->item.index); |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
struct line_entry { |
| 117 |
Dwarf_Addr low; |
| 118 |
// Dwarf_Addr high; |
| 119 |
// int i; |
| 120 |
Dwarf_Line line; |
| 121 |
optional<std::string> path; |
| 122 |
optional<std::uint32_t> line_number; |
| 123 |
optional<std::uint32_t> column_number; |
| 124 |
line_entry(Dwarf_Addr low, Dwarf_Line line) : low(low), line(line) {} |
| 125 |
}; |
| 126 |
|
| 127 |
struct line_table_info { |
| 128 |
Dwarf_Unsigned version = 0; |
| 129 |
Dwarf_Line_Context line_context = nullptr; |
| 130 |
// sorted by low_addr |
| 131 |
// TODO: Make this optional at some point, it may not be generated if cache mode switches during program exec... |
| 132 |
std::vector<line_entry> line_entries; |
| 133 |
|
| 134 |
line_table_info( |
| 135 |
Dwarf_Unsigned version, |
| 136 |
Dwarf_Line_Context line_context, |
| 137 |
std::vector<line_entry>&& line_entries |
| 138 |
) : version(version), line_context(line_context), line_entries(std::move(line_entries)) {} |
| 139 |
~line_table_info() { |
| 140 |
release(); |
| 141 |
} |
| 142 |
void release() { |
| 143 |
dwarf_srclines_dealloc_b(line_context); |
| 144 |
line_context = nullptr; |
| 145 |
} |
| 146 |
line_table_info(const line_table_info&) = delete; |
| 147 |
line_table_info(line_table_info&& other) { |
| 148 |
*this = std::move(other); |
| 149 |
} |
| 150 |
line_table_info& operator=(const line_table_info&) = delete; |
| 151 |
line_table_info& operator=(line_table_info&& other) { |
| 152 |
release(); |
| 153 |
version = other.version; |
| 154 |
line_context = exchange(other.line_context, nullptr); |
| 155 |
line_entries = std::move(other.line_entries); |
| 156 |
return *this; |
| 157 |
} |
| 158 |
}; |
| 159 |
} |
| 160 |
} |
| 161 |
CPPTRACE_END_NAMESPACE |
| 162 |
|
| 163 |
#endif |