-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathmaps_parser.cpp
More file actions
363 lines (315 loc) Β· 11.3 KB
/
maps_parser.cpp
File metadata and controls
363 lines (315 loc) Β· 11.3 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "maps_parser.h"
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <regex>
#include <set>
#include <sstream>
#include <stdexcept>
#include <unistd.h>
#include "logging.h"
namespace pystack {
namespace fs = std::filesystem;
// Regex pattern for parsing /proc/pid/maps lines
// Format: start-end permissions offset dev inode pathname
static const std::regex MAPS_REGEXP(
R"(([0-9a-f]+)-([0-9a-f]+)\s+(.{4})\s+([0-9a-f]+)\s+([0-9a-f]+:[0-9a-f]+)\s+(\d+)\s*(.*)?)");
std::vector<VirtualMap>
parseProcMaps(pid_t pid)
{
std::vector<VirtualMap> maps;
std::string maps_path = "/proc/" + std::to_string(pid) + "/maps";
std::ifstream maps_file(maps_path);
if (!maps_file.is_open()) {
throw std::runtime_error("No such process id: " + std::to_string(pid));
}
std::string line;
while (std::getline(maps_file, line)) {
std::smatch match;
if (!std::regex_match(line, match, MAPS_REGEXP)) {
LOG(DEBUG) << "Line cannot be recognized: " << line;
continue;
}
uintptr_t start = std::stoull(match[1].str(), nullptr, 16);
uintptr_t end = std::stoull(match[2].str(), nullptr, 16);
std::string permissions = match[3].str();
unsigned long offset = std::stoul(match[4].str(), nullptr, 16);
std::string device = match[5].str();
unsigned long inode = std::stoul(match[6].str());
std::string pathname = match[7].str();
size_t start_pos = pathname.find_first_not_of(" \t");
if (start_pos != std::string::npos) {
pathname = pathname.substr(start_pos);
} else {
pathname = "";
}
maps.emplace_back(
start,
end,
end - start, // filesize
permissions,
offset,
device,
inode,
pathname);
}
return maps;
}
std::vector<VirtualMap>
parseCoreFileMaps(
const std::vector<CoreVirtualMap>& mapped_files,
const std::vector<CoreVirtualMap>& memory_maps)
{
std::set<std::pair<uintptr_t, uintptr_t>> memory_map_ranges;
for (const auto& map : memory_maps) {
memory_map_ranges.insert({map.start, map.end});
}
std::vector<CoreVirtualMap> missing_mapped_files;
for (const auto& map : mapped_files) {
if (memory_map_ranges.find({map.start, map.end}) == memory_map_ranges.end()) {
missing_mapped_files.push_back(map);
}
}
std::vector<CoreVirtualMap> all_maps;
all_maps.reserve(memory_maps.size() + missing_mapped_files.size());
all_maps.insert(all_maps.end(), memory_maps.begin(), memory_maps.end());
all_maps.insert(all_maps.end(), missing_mapped_files.begin(), missing_mapped_files.end());
std::sort(all_maps.begin(), all_maps.end(), [](const CoreVirtualMap& a, const CoreVirtualMap& b) {
return a.start < b.start;
});
std::set<std::string> missing_map_paths;
for (const auto& map : missing_mapped_files) {
if (!map.path.empty()) {
try {
missing_map_paths.insert(fs::canonical(map.path).string());
} catch (...) {
missing_map_paths.insert(map.path);
}
}
}
std::unordered_map<std::string, std::string> file_maps;
for (const auto& map : memory_maps) {
if (map.path.empty()) {
continue;
}
try {
std::string resolved_path = fs::canonical(map.path).string();
if (missing_map_paths.count(resolved_path)) {
file_maps[resolved_path] = map.path;
}
} catch (...) {
// Ignore errors resolving paths
}
}
std::vector<VirtualMap> result;
result.reserve(all_maps.size());
for (const auto& elem : all_maps) {
std::string path = elem.path;
if (!path.empty()) {
std::string resolved;
try {
resolved = fs::canonical(path).string();
} catch (...) {
resolved = path;
}
auto it = file_maps.find(resolved);
if (it != file_maps.end()) {
path = it->second;
}
}
result.emplace_back(
elem.start,
elem.end,
elem.filesize,
elem.flags,
elem.offset,
elem.device,
elem.inode,
path);
}
return result;
}
static VirtualMap
getBaseMap(const std::vector<VirtualMap>& binary_maps)
{
for (const auto& map : binary_maps) {
if (!map.Path().empty()) {
return map;
}
}
if (!binary_maps.empty()) {
return binary_maps[0];
}
throw std::runtime_error("No maps available");
}
static std::optional<VirtualMap>
getBss(const std::vector<VirtualMap>& elf_maps, uintptr_t load_point)
{
if (elf_maps.empty()) {
return std::nullopt;
}
VirtualMap binary_map = getBaseMap(elf_maps);
if (binary_map.Path().empty()) {
return std::nullopt;
}
SectionInfo bss_info;
if (!getSectionInfo(binary_map.Path(), ".bss", &bss_info)) {
return std::nullopt;
}
uintptr_t start = load_point + bss_info.corrected_addr;
LOG(INFO) << "Determined exact addr of .bss section: " << std::hex << start << " (" << load_point
<< " + " << bss_info.corrected_addr << ")" << std::dec;
unsigned long offset = 0;
const VirtualMap* first_matching_map = nullptr;
for (const auto& map : elf_maps) {
if (map.containsAddr(start)) {
first_matching_map = ↦
break;
}
}
if (!first_matching_map) {
return std::nullopt;
}
offset = first_matching_map->Offset() + (start - first_matching_map->Start());
return VirtualMap(
start,
start + bss_info.size,
bss_info.size,
"", // flags
offset, // offset
"", // device
0, // inode
""); // path
}
ProcessMemoryMapInfo
parseMapInformation(
const std::string& binary,
const std::vector<VirtualMap>& maps,
const std::unordered_map<std::string, uintptr_t>* load_point_by_module)
{
std::unordered_map<std::string, std::vector<VirtualMap>> maps_by_library;
std::string current_lib;
std::unordered_map<std::string, uintptr_t> computed_load_points;
if (!load_point_by_module) {
for (const auto& map : maps) {
if (!map.Path().empty()) {
std::string name = fs::path(map.Path()).filename().string();
if (computed_load_points.find(name) == computed_load_points.end()) {
computed_load_points[name] = map.Start();
} else {
computed_load_points[name] = std::min(computed_load_points[name], map.Start());
}
}
}
load_point_by_module = &computed_load_points;
}
for (const auto& memory_range : maps) {
std::string path_name;
if (!memory_range.Path().empty()) {
path_name = fs::path(memory_range.Path()).filename().string();
current_lib = path_name;
} else {
path_name = current_lib;
}
maps_by_library[path_name].push_back(memory_range);
}
std::string binary_name = fs::path(binary).filename().string();
auto python_it = maps_by_library.find(binary_name);
if (python_it == maps_by_library.end()) {
// Construct error message with available maps
std::ostringstream available;
for (const auto& map : maps) {
if (!map.Path().empty() && map.Path().find(".so") == std::string::npos) {
available << map.Path() << ", ";
}
}
std::string available_str = available.str();
if (available_str.length() >= 2) {
available_str = available_str.substr(0, available_str.length() - 2);
}
throw std::runtime_error(
"Unable to find maps for the executable " + binary
+ ". Available executable maps: " + available_str);
}
const std::vector<VirtualMap>& binary_maps = python_it->second;
VirtualMap python = getBaseMap(binary_maps);
LOG(INFO) << "python binary first map found: " << python.Path();
std::optional<VirtualMap> libpython;
const std::vector<VirtualMap>* elf_maps = nullptr;
std::string libpython_name;
std::vector<std::string> libpython_binaries;
for (const auto& [lib_name, _] : maps_by_library) {
if (lib_name.find("libpython") != std::string::npos) {
libpython_binaries.push_back(lib_name);
}
}
uintptr_t load_point = 0;
if (libpython_binaries.size() > 1) {
throw std::runtime_error(
"Unexpectedly found multiple libpython in process: "
+ std::to_string(libpython_binaries.size()));
} else if (libpython_binaries.size() == 1) {
libpython_name = libpython_binaries[0];
const auto& libpython_maps = maps_by_library[libpython_name];
elf_maps = &libpython_maps;
auto load_it = load_point_by_module->find(libpython_name);
load_point = (load_it != load_point_by_module->end()) ? load_it->second : UINTPTR_MAX;
libpython = getBaseMap(libpython_maps);
LOG(INFO) << libpython_name << " first map found: " << libpython->Path();
} else {
LOG(INFO) << "Process does not have a libpython.so, reading from binary";
elf_maps = &binary_maps;
auto load_it = load_point_by_module->find(binary_name);
load_point = (load_it != load_point_by_module->end()) ? load_it->second : UINTPTR_MAX;
}
std::optional<VirtualMap> heap;
auto heap_it = maps_by_library.find("[heap]");
if (heap_it != maps_by_library.end() && !heap_it->second.empty()) {
heap = heap_it->second.front();
LOG(INFO) << "Heap map found";
}
std::optional<VirtualMap> bss = getBss(*elf_maps, load_point);
if (!bss) {
for (const auto& map : *elf_maps) {
if (map.Path().empty() && map.Flags().find('r') != std::string::npos) {
bss = map;
break;
}
}
}
if (bss) {
LOG(INFO) << "bss map found";
}
return ProcessMemoryMapInfo{heap, bss, python, libpython};
}
ProcessMemoryMapInfo
parseMapInformationForProcess(pid_t pid, const std::vector<VirtualMap>& maps)
{
std::string exe_link = "/proc/" + std::to_string(pid) + "/exe";
char exe_path[PATH_MAX];
ssize_t len = readlink(exe_link.c_str(), exe_path, sizeof(exe_path) - 1);
if (len == -1) {
throw std::runtime_error("Failed to read /proc/" + std::to_string(pid) + "/exe");
}
exe_path[len] = '\0';
return parseMapInformation(exe_path, maps);
}
std::optional<std::string>
getThreadName(pid_t pid, pid_t tid)
{
std::string comm_path = "/proc/" + std::to_string(pid) + "/task/" + std::to_string(tid) + "/comm";
std::ifstream comm_file(comm_path);
if (!comm_file.is_open()) {
return std::nullopt;
}
std::string name;
std::getline(comm_file, name);
size_t end = name.find_last_not_of(" \t\n\r");
if (end != std::string::npos) {
name = name.substr(0, end + 1);
}
return name;
}
} // namespace pystack