forked from bloomberg/pystack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelf_common.h
More file actions
136 lines (109 loc) Β· 2.89 KB
/
elf_common.h
File metadata and controls
136 lines (109 loc) Β· 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#pragma once
#include <cstring>
#include <fcntl.h>
#include <functional>
#include <memory>
#include <optional>
#include <stdexcept>
#include <unistd.h>
#include "logging.h"
#include <elf.h>
#include <elfutils/libdwelf.h>
#include <elfutils/libdwfl.h>
#include <gelf.h>
namespace pystack {
std::string
parse_permissions(long flags);
class ElfAnalyzerError : public std::exception
{
public:
explicit ElfAnalyzerError(std::string error)
: d_error(std::move(error)){};
const char* what() const noexcept override
{
return d_error.c_str();
}
private:
std::string d_error;
};
// Aliases
using dwfl_unique_ptr = std::unique_ptr<Dwfl, std::function<void(Dwfl*)>>;
using elf_unique_ptr = std::unique_ptr<Elf, std::function<void(Elf*)>>;
class Analyzer
{
// Methods
public:
virtual const dwfl_unique_ptr& getDwfl() const = 0;
};
class CoreFileAnalyzer : public Analyzer
{
public:
// Constructors
explicit CoreFileAnalyzer(
std::string corefile,
std::optional<std::string> executable = std::nullopt,
const std::optional<std::string>& lib_search_path = std::nullopt);
// Methods
const dwfl_unique_ptr& getDwfl() const override;
std::string locateLibrary(const std::string& lib) const;
// Destructors
virtual ~CoreFileAnalyzer();
// Data members
dwfl_unique_ptr d_dwfl;
char* d_debuginfo_path;
Dwfl_Callbacks d_callbacks;
std::string d_filename;
std::optional<std::string> d_executable;
std::optional<std::string> d_lib_search_path;
int d_fd;
int d_pid;
elf_unique_ptr d_elf;
std::vector<std::string> d_missing_modules{};
private:
void removeModuleIf(std::function<bool(Dwfl_Module*)> predicate) const;
void resolveLibraries();
};
class ProcessAnalyzer : public Analyzer
{
public:
// Constructors
explicit ProcessAnalyzer(pid_t pid);
// Destructors
virtual ~ProcessAnalyzer() = default;
// Methods
const dwfl_unique_ptr& getDwfl() const override;
// Data members
dwfl_unique_ptr d_dwfl;
char* d_debuginfo_path;
Dwfl_Callbacks d_callbacks;
int d_pid;
};
uintptr_t
getLoadPointOfModule(const dwfl_unique_ptr& dwfl, const std::string& mod);
// Utility functions for accessing NOTE sections
struct NoteData
{
Elf* elf{nullptr};
Elf_Data* data{nullptr};
Elf64_Xword descriptor_size{0};
size_t desc_offset{0};
GElf_Nhdr nhdr{};
};
std::vector<NoteData>
getNoteData(Elf* elf, Elf64_Word note_type, Elf_Type note_data_type);
struct SectionInfo
{
std::string name;
std::string flags;
uintptr_t addr;
uintptr_t corrected_addr;
off_t offset;
size_t size;
};
bool
getSectionInfo(const std::string& filename, const std::string& section_name, SectionInfo* result);
std::string
buildIdPtrToString(const uint8_t* id, ssize_t size);
std::string
getBuildId(const std::string& filename);
} // namespace pystack