-
Notifications
You must be signed in to change notification settings - Fork 38
/
canal.cc
416 lines (376 loc) · 14 KB
/
canal.cc
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* Canal searches through core files looking for references to symbols. The
* symbols can be provided by a glob style pattern, and defaults to a pattern
* that matches symbols associated with vtables. So, by default, canal finds
* likely instances of classes with virtual methods in the process's address
* space, and can be useful to help identify memory leaks.
*/
#include <unistd.h>
#include <assert.h>
#include <iostream>
#include <exception>
#include <algorithm>
#include <sys/types.h>
#include <map>
#include <err.h>
#include "libpstack/context.h"
#include "libpstack/proc.h"
#include "libpstack/elf.h"
#include "libpstack/dwarf.h"
#include "libpstack/stringify.h"
#include "libpstack/ioflag.h"
#include "libpstack/flags.h"
#if defined( WITH_PYTHON3 )
#define WITH_PYTHON
#endif
#ifdef WITH_PYTHON
#include "libpstack/python.h"
#include <Python.h>
#endif
using namespace std;
using namespace pstack;
using AddressRanges = std::vector<std::pair<Elf::Off, Elf::Off>>;
#ifdef WITH_PYTHON
std::unique_ptr<PythonPrinter<3>> py = nullptr;
#endif
// does "name" match the glob pattern "pattern"?
static int
globmatchR(const char *pattern, const char *name)
{
for (;; name++) {
switch (*pattern) {
case '*':
// if the rest of the name matches the bit of pattern after '*',
for (;;) {
if (globmatchR(pattern + 1, name))
return 1;
if (*name == 0) // exhuasted name without finding a match
return 0;
++name;
}
default:
if (*name != *pattern)
return 0;
}
if (*pattern++ == 0)
return 1;
}
}
static int
globmatch(const string &pattern, const string &name)
{
return globmatchR(pattern.c_str(), name.c_str());
}
struct ListedSymbol {
Elf::Sym sym;
Elf::Off objbase;
string name;
size_t count;
string objname;
ListedSymbol(const Elf::Sym &sym_, Elf::Off objbase_, string name_, string object)
: sym(sym_)
, objbase(objbase_)
, name(name_)
, count(0)
, objname(object)
{}
Elf::Off memaddr() const { return sym.st_value + objbase; }
};
class SymbolStore {
std::map<Elf::Off, ListedSymbol> store_;
public:
void add(ListedSymbol symbol) {
store_.emplace(symbol.memaddr() + symbol.sym.st_size, symbol);
}
template <typename Match>
std::tuple<bool, ListedSymbol*> find(Elf::Off address, const Match match) {
auto pos = store_.lower_bound(address);
auto sym = &pos->second;
if (pos != store_.end() && match(address, sym)) {
return std::make_tuple(true, sym);
}
return std::make_tuple(false, nullptr);
}
std::vector<ListedSymbol> flatten() const {
std::vector<ListedSymbol> retv;
retv.reserve(store_.size());
for(const auto & item : store_) {
retv.emplace_back( item.second );
}
return retv;
}
};
class OffsetFreeSymbolMatcher {
public:
bool operator()(Elf::Off address, const ListedSymbol * sym) const {
return sym->memaddr() <= address && sym->memaddr() + sym->sym.st_size > address;
}
};
class OffsetBoundSymbolMatcher {
const Elf::Off offset_;
public:
OffsetBoundSymbolMatcher(Elf::Addr offset): offset_(offset) {}
bool operator()(Elf::Off address, const ListedSymbol * sym) const {
return sym->memaddr() + offset_ == address;
}
};
struct Usage {
const Flags &flags;
Usage(Flags &flags) : flags(flags) {}
};
bool operator < (const ListedSymbol &sym, Elf::Off addr) {
return sym.memaddr() + sym.sym.st_size < addr;
}
static const char *virtpattern = "_ZTV*"; /* wildcard for all vtbls */
ostream &
operator <<(ostream &os, const Usage &u)
{
return os <<
R"---(
Nominally, Canal finds references to symbols matching a specific set
of patterns within a core file or process, and produces a histogram
showing the frequency of occurrances of references to each mached symbol.
By default, it will find references to vtables (by matching the pattern
'_ZTV*', which starts the mangled name of a vtable), but you can use
your own pattern to find references to similar type-describing objects.
In the default operating mode, it gives a pretty accurate estimate of
the number each type of polymorphic C++ object allocated in the process.
You may also use canal to find references to specific addresses, or
references that lie within a specific range of addresses.
This whole thing should be a python extension module to allow much finer
control over its operation.
usage:
canal [options] [executable] <core|pid>
options:
)---" << u.flags;
}
static void findString(const Procman::Process &process,
const Procman::AddressRange &segment,
const std::string &findstr) {
std::vector<char> corestr;
corestr.resize(std::max(size_t(4096UL), findstr.size() * 4));
for (size_t in, memPos = segment.start, corestrPos = 0; memPos < segment.end; memPos += in) {
size_t freeCorestr = corestr.size() - corestrPos;
size_t remainingSegment = segment.fileEnd - memPos;
size_t readsize = std::min(remainingSegment, freeCorestr);
in = process.io->read(memPos, readsize, corestr.data() + corestrPos);
assert(in == readsize);
corestrPos += in;
for (auto found = corestr.begin();; ++found) {
found = std::search(corestr.begin(), corestr.begin() + corestrPos, findstr.begin(), findstr.end());
if (found == corestr.end())
break;
IOFlagSave _(cout);
std::cout << "0x" << hex << memPos + (found - corestr.begin()) << "\n";
}
if (corestrPos >= findstr.size()) {
memmove(corestr.data(),
corestr.data() + corestrPos - findstr.size() + 1,
findstr.size() - 1);
}
memPos += in;
}
}
template <typename Matcher, typename Word> inline void search(
const Reader::csptr &view,
const Matcher & m,
Elf::Addr loc,
const AddressRanges &searchaddrs,
SymbolStore &store,
bool showaddrs) {
try {
IOFlagSave _(cout);
ReaderArray<Word, 131072> r(*view, 0);
auto start = r.begin();
if (searchaddrs.size()) {
for (auto cur = start; cur != r.end(); ++cur) {
Word p = *cur;
for (const auto &range : searchaddrs )
if (p >= range.first && p < range.second)
cout << "0x" << hex << loc + (cur - start) * sizeof( Word) << dec << "\n";
}
} else {
for (auto cur = start; cur != r.end(); ++cur) {
Word p = *cur;
if ( auto [ found, sym ] = store.find(p, m); found) {
if (showaddrs)
cout
<< sym->name << " 0x" << std::hex << loc + (cur - start) * sizeof(Word)
<< std::dec << " ... size=" << sym->sym.st_size
<< ", diff=" << p - sym->memaddr() << endl;
#ifdef WITH_PYTHON
if (py) {
std::cout << "pyo " << Elf::Addr(loc) << " ";
py->print(Elf::Addr(loc) - sizeof (PyObject) +
sizeof (struct _typeobject *));
std::cout << "\n";
}
#endif
sym->count++;
}
}
}
} catch (const std::exception &ex) {
std::clog << "warning: error reading data at " << std::hex << loc << std::dec << ": " << ex.what() << "\n";
}
}
template <typename Matcher> void search(int wordsize,
Procman::Process &process,
const Matcher & m, const Procman::AddressRange &segment,
const AddressRanges &searchaddrs, SymbolStore &store, bool showaddrs) {
auto view = process.io->view( "segment view", segment.start, segment.fileEnd - segment.start );
if (wordsize == 32) {
return search<Matcher, uint32_t>(view, m, segment.start, searchaddrs, store, showaddrs);
} else if (wordsize == 64) {
return search<Matcher, uint64_t>(view, m, segment.start, searchaddrs, store, showaddrs);
} else {
errx(1, "invalid word size %d, must be 32 or 64", wordsize);
}
}
int
mainExcept(int argc, char *argv[])
{
Context context;
std::vector<std::string> patterns;
Elf::Object::sptr exec;
int wordsize = sizeof (Elf::Off) * 8;
Elf::Object::sptr core;
bool showaddrs = false;
bool showsyms = false;
AddressRanges searchaddrs;
std::string findstr;
int symOffset = -1;
#ifdef WITH_PYTHON
bool doPython = false;
#endif
Flags flags;
flags
#ifdef WITH_PYTHON
.add("python", 'P', "try to find python objects", Flags::setf(doPython))
#endif
.add("show-syms", 'V', "show symbols matching search pattern", Flags::setf(showsyms))
.add("show-addrs", 's', "show adddress of references found in core", Flags::setf(showaddrs))
.add("verbose", 'v', "increase verbosity (may be repeated)", [&]() { ++context.verbose; })
.add("help", 'h', "show this message", [&]() { std::cout << Usage(flags); exit(0); })
.add("offset",
'o',
"offset from symbol location",
"limit search to matches that are exactly <offset> from the symbol",
Flags::set(symOffset))
.add("pattern", 'p', "glob",
"add <glob> to the list of patterns to be matched for symbols",
[&](const char *data) { patterns.push_back(data); })
.add("replace-path", 'r', "from:to",
"replace references to path <from> with <tp> when finding libraries",
[&](const char *data) {
std::string both = data;
size_t colon = both.find(':');
if (colon == std::string::npos)
throw "must specify <to>=<from> for '-r'";
context.pathReplacements.push_back(std::make_pair(both.substr(0, colon), both.substr(colon + 1)));
})
.add("start-location", 'f', "addresss",
"instead of searching for symbols, find references to a specified address. Decimal, or prefix with 0x for hex",
[&](const char *p) {
Elf::Off start = strtoul(p, 0, 0);
searchaddrs.push_back(make_pair(start, start + 1));
})
.add("end-location", 'e', "end-address",
"change previous 'f' option to include all addresses in range ['f' addr, 'e' addr)",
[&](const char *p) { searchaddrs.back().second = strtoul(p, 0, 0); })
.add("wordsize", 'w', "wordsize(16 or 32)", "consider address ranges as wordsize-bit values", Flags::set( wordsize ) )
.add("string", 'S', "text", "search the core for the text string <text>, and print it's address", Flags::set(findstr))
.parse(argc, argv);
if (argc - optind >= 2) {
exec = context.getImageForName(argv[optind]);
optind++;
}
if (argc - optind < 1) {
clog << Usage(flags);
return 0;
}
auto process = Procman::Process::load(context, exec, argv[optind]);
#ifdef WITH_PYTHON
PyInterpInfo info;
if (doPython) {
info = getPyInterpInfo(*process);
py = make_unique<PythonPrinter<3>>(*process, std::cout, info);
}
#endif
if (searchaddrs.size()) {
std::clog << "finding references to " << dec << searchaddrs.size() << " addresses\n";
for (auto &addr : searchaddrs)
std::clog << "\t" << addr.first <<" - " << addr.second << "\n";
}
clog << "opened process " << process << endl;
SymbolStore store;
if (patterns.empty())
patterns.push_back(virtpattern);
for (auto &loaded : process->objects) {
size_t count = 0;
auto findSymbols = [&]( auto table ) {
for (const auto &sym : *table) {
for (auto &pattern : patterns) {
auto name = table->name(sym);
if (globmatch(pattern, name)) {
store.add(ListedSymbol(sym, loaded.first, name, loaded.second.name()));
if (context.verbose > 1 || showsyms)
std::cout << name << "\n";
count++;
}
}
}
};
auto obj = loaded.second.object(process->context);
findSymbols( obj->dynamicSymbols() );
findSymbols( obj->debugSymbols() );
if (context.verbose)
*context.debug << "found " << count << " symbols in " << *obj->io << endl;
}
if (showsyms)
exit(0);
// Now run through the corefile, searching for virtual objects.
auto as = process->addressSpace();
for (auto &segment : as ) {
if (context.verbose) {
IOFlagSave _(*context.debug);
*context.debug << "scan " << hex << segment.start << " to " << segment.start + segment.fileEnd;
}
if (segment.vmflags.find( pstack::Procman::AddressRange::VmFlag::memory_mapped_io ) != segment.vmflags.end() ) {
if (context.verbose) {
*context.debug << "skipping IO mapping\n";
}
continue;
}
if (findstr != "") {
findString( *process, segment, findstr );
} else {
if (symOffset > 0)
search<OffsetBoundSymbolMatcher>(wordsize, *process,
OffsetBoundSymbolMatcher(symOffset),
segment, searchaddrs, store, showaddrs);
else
search<OffsetFreeSymbolMatcher>(wordsize, *process,
OffsetFreeSymbolMatcher(),
segment, searchaddrs, store, showaddrs);
}
}
auto histogram = store.flatten();
sort(histogram.begin(), histogram.end(),
[](const ListedSymbol &l, const ListedSymbol &r) { return l.count > r.count; });
for (auto &i : histogram)
if (i.count)
cout << dec << i.count << " " << i.name << " ( from " << i.objname << ")" << endl;
return 0;
}
int
main(int argc, char *argv[])
{
try {
return mainExcept(argc, argv);
}
catch (const exception &ex) {
cerr << "exception: " << ex.what() << endl;
return -1;
}
}