forked from KDE/heaptrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heaptrack_interpret.cpp
390 lines (338 loc) · 12.3 KB
/
heaptrack_interpret.cpp
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* Copyright 2014 Milian Wolff <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* @file heaptrack_interpret.cpp
*
* @brief Interpret raw heaptrack data and add Dwarf based debug information.
*/
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <vector>
#include <tuple>
#include <algorithm>
#include <stdio_ext.h>
#include <cxxabi.h>
#include <boost/algorithm/string/predicate.hpp>
#include "libbacktrace/backtrace.h"
#include "libbacktrace/internal.h"
#include "linereader.h"
using namespace std;
namespace {
string demangle(const char* function)
{
if (!function) {
return {};
} else if (function[0] != '_' || function[1] != 'Z') {
return {function};
}
string ret;
int status = 0;
char* demangled = abi::__cxa_demangle(function, 0, 0, &status);
if (demangled) {
ret = demangled;
free(demangled);
}
return ret;
}
struct AddressInformation
{
string function;
string file;
int line = 0;
};
struct Module
{
Module(uintptr_t addressStart, uintptr_t addressEnd, backtrace_state* backtraceState, size_t moduleIndex)
: addressStart(addressStart)
, addressEnd(addressEnd)
, moduleIndex(moduleIndex)
, backtraceState(backtraceState)
{
}
AddressInformation resolveAddress(uintptr_t address) const
{
AddressInformation info;
if (!backtraceState) {
return info;
}
backtrace_pcinfo(backtraceState, address,
[] (void *data, uintptr_t /*addr*/, const char *file, int line, const char *function) -> int {
auto info = reinterpret_cast<AddressInformation*>(data);
info->function = demangle(function);
info->file = file ? file : "";
info->line = line;
return 0;
}, [] (void */*data*/, const char */*msg*/, int /*errnum*/) {}, &info);
if (info.function.empty()) {
backtrace_syminfo(backtraceState, address,
[] (void *data, uintptr_t /*pc*/, const char *symname, uintptr_t /*symval*/, uintptr_t /*symsize*/) {
if (symname) {
reinterpret_cast<AddressInformation*>(data)->function = demangle(symname);
}
}, [] (void */*data*/, const char *msg, int errnum) {
cerr << "Module backtrace error (code " << errnum << "): " << msg << endl;
}, &info);
}
return info;
}
bool operator<(const Module& module) const
{
return make_tuple(addressStart, addressEnd, moduleIndex)
< make_tuple(module.addressStart, module.addressEnd, module.moduleIndex);
}
bool operator!=(const Module& module) const
{
return make_tuple(addressStart, addressEnd, moduleIndex)
!= make_tuple(module.addressStart, module.addressEnd, module.moduleIndex);
}
uintptr_t addressStart;
uintptr_t addressEnd;
size_t moduleIndex;
backtrace_state* backtraceState;
};
struct Allocation
{
// backtrace entry point
size_t ipIndex;
// number of allocations
size_t allocations;
// amount of bytes leaked
size_t leaked;
};
/**
* Information for a single call to an allocation function
*/
struct AllocationInfo
{
size_t ipIndex;
size_t size;
};
struct ResolvedIP
{
size_t moduleIndex = 0;
size_t fileIndex = 0;
size_t functionIndex = 0;
int line = 0;
};
struct AccumulatedTraceData
{
AccumulatedTraceData()
{
m_modules.reserve(256);
m_backtraceStates.reserve(64);
m_internedData.reserve(4096);
m_encounteredIps.reserve(32768);
}
~AccumulatedTraceData()
{
fprintf(stdout, "# strings: %zu\n# ips: %zu\n",
m_internedData.size(), m_encounteredIps.size());
}
ResolvedIP resolve(const uintptr_t ip)
{
if (m_modulesDirty) {
// sort by addresses, required for binary search below
sort(m_modules.begin(), m_modules.end());
for (size_t i = 0; i < m_modules.size(); ++i) {
const auto& m1 = m_modules[i];
for (size_t j = i + 1; j < m_modules.size(); ++j) {
if (i == j) {
continue;
}
const auto& m2 = m_modules[j];
if ((m1.addressStart <= m2.addressStart && m1.addressEnd > m2.addressStart) ||
(m1.addressStart < m2.addressEnd && m1.addressEnd >= m2.addressEnd))
{
cerr << "OVERLAPPING MODULES: " << hex
<< m1.moduleIndex << " (" << m1.addressStart << " to " << m1.addressEnd << ") and "
<< m1.moduleIndex << " (" << m2.addressStart << " to " << m2.addressEnd << ")\n"
<< dec;
} else if (m2.addressStart >= m1.addressEnd) {
break;
}
}
}
m_modulesDirty = false;
}
ResolvedIP data;
// find module for this instruction pointer
auto module = lower_bound(m_modules.begin(), m_modules.end(), ip,
[] (const Module& module, const uintptr_t ip) -> bool {
return module.addressEnd < ip;
});
if (module != m_modules.end() && module->addressStart <= ip && module->addressEnd >= ip) {
data.moduleIndex = module->moduleIndex;
const auto info = module->resolveAddress(ip);
data.fileIndex = intern(info.file);
data.functionIndex = intern(info.function);
data.line = info.line;
}
return data;
}
size_t intern(const string& str)
{
if (str.empty()) {
return 0;
}
auto it = m_internedData.find(str);
if (it != m_internedData.end()) {
return it->second;
}
const size_t id = m_internedData.size() + 1;
m_internedData.insert(it, make_pair(str, id));
fprintf(stdout, "s %s\n", str.c_str());
return id;
}
void addModule(backtrace_state* backtraceState, const size_t moduleIndex,
const uintptr_t addressStart, const uintptr_t addressEnd)
{
m_modules.emplace_back(addressStart, addressEnd, backtraceState, moduleIndex);
m_modulesDirty = true;
}
void clearModules()
{
// TODO: optimize this, reuse modules that are still valid
m_modules.clear();
m_modulesDirty = true;
}
size_t addIp(const uintptr_t instructionPointer)
{
if (!instructionPointer) {
return 0;
}
auto it = m_encounteredIps.find(instructionPointer);
if (it != m_encounteredIps.end()) {
return it->second;
}
const size_t ipId = m_encounteredIps.size() + 1;
m_encounteredIps.insert(it, make_pair(instructionPointer, ipId));
const auto ip = resolve(instructionPointer);
fprintf(stdout, "i %zx %zx", instructionPointer, ip.moduleIndex);
if (ip.functionIndex || ip.fileIndex) {
fprintf(stdout, " %zx", ip.functionIndex);
if (ip.fileIndex) {
fprintf(stdout, " %zx %x", ip.fileIndex, ip.line);
}
}
fputc('\n', stdout);
return ipId;
}
/**
* Prevent the same file from being initialized multiple times.
* This drastically cuts the memory consumption down
*/
backtrace_state* findBacktraceState(const string& fileName, uintptr_t addressStart)
{
if (boost::algorithm::starts_with(fileName, "linux-vdso.so")) {
// prevent warning, since this will always fail
return nullptr;
}
auto it = m_backtraceStates.find(fileName);
if (it != m_backtraceStates.end()) {
return it->second;
}
struct CallbackData {
const string* fileName;
};
CallbackData data = {&fileName};
auto errorHandler = [] (void *rawData, const char *msg, int errnum) {
auto data = reinterpret_cast<const CallbackData*>(rawData);
cerr << "Failed to create backtrace state for module " << *data->fileName << ": "
<< msg << " / " << strerror(errnum) << " (error code " << errnum << ")" << endl;
};
auto state = backtrace_create_state(fileName.c_str(), /* we are single threaded, so: not thread safe */ false,
errorHandler, &data);
if (state) {
const int descriptor = backtrace_open(fileName.c_str(), errorHandler, &data, nullptr);
if (descriptor >= 1) {
int foundSym = 0;
int foundDwarf = 0;
auto ret = elf_add(state, descriptor, addressStart, errorHandler, &data,
&state->fileline_fn, &foundSym, &foundDwarf, false);
if (ret && foundSym) {
state->syminfo_fn = &elf_syminfo;
}
}
}
m_backtraceStates.insert(it, make_pair(fileName, state));
return state;
}
private:
vector<Module> m_modules;
unordered_map<string, backtrace_state*> m_backtraceStates;
bool m_modulesDirty = false;
unordered_map<string, size_t> m_internedData;
unordered_map<uintptr_t, size_t> m_encounteredIps;
};
}
int main(int /*argc*/, char** /*argv*/)
{
// optimize: we only have a single thread
ios_base::sync_with_stdio(false);
__fsetlocking(stdout, FSETLOCKING_BYCALLER);
__fsetlocking(stdin, FSETLOCKING_BYCALLER);
AccumulatedTraceData data;
LineReader reader;
string exe;
while (reader.getLine(cin)) {
if (reader.mode() == 'x') {
reader >> exe;
} else if (reader.mode() == 'm') {
string fileName;
reader >> fileName;
if (fileName == "-") {
data.clearModules();
} else {
if (fileName == "x") {
fileName = exe;
}
const auto moduleIndex = data.intern(fileName);
uintptr_t addressStart = 0;
if (!(reader >> addressStart)) {
cerr << "failed to parse line: " << reader.line() << endl;
return 1;
}
auto state = data.findBacktraceState(fileName, addressStart);
uintptr_t vAddr = 0;
uintptr_t memSize = 0;
while ((reader >> vAddr) && (reader >> memSize)) {
data.addModule(state, moduleIndex,
addressStart + vAddr,
addressStart + vAddr + memSize);
}
}
} else if (reader.mode() == 't') {
uintptr_t instructionPointer = 0;
size_t parentIndex = 0;
if (!(reader >> instructionPointer) || !(reader >> parentIndex)) {
cerr << "failed to parse line: " << reader.line() << endl;
return 1;
}
// ensure ip is encountered
const auto ipId = data.addIp(instructionPointer);
// trace point, map current output index to parent index
fprintf(stdout, "t %zx %zx\n", ipId, parentIndex);
} else {
fputs(reader.line().c_str(), stdout);
fputc('\n', stdout);
}
}
return 0;
}