-
Notifications
You must be signed in to change notification settings - Fork 35
/
pstack.cc
442 lines (397 loc) · 13.9 KB
/
pstack.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include "libpstack/dwarf.h"
#include "libpstack/flags.h"
#include "libpstack/global.h"
#include "libpstack/proc.h"
#include "libpstack/fs.h"
#if defined(WITH_PYTHON2) || defined(WITH_PYTHON3)
#define WITH_PYTHON
#include "libpstack/python.h"
#endif
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/wait.h>
#include <sysexits.h>
#include <unistd.h>
#include <csignal>
#include <cassert>
#include <algorithm>
#include <fstream>
#include <iostream>
#define XSTR(a) #a
#define STR(a) XSTR(a)
namespace {
using namespace pstack;
bool doJson = false;
bool freeres = 0; // free things on exit (for debugging/valgrind/heapcheck)
volatile bool interrupted = false;
void
pstack(Procman::Process &proc)
{
const auto &threadStacks = proc.getStacks();
auto &os = *proc.options.output;
if (doJson) {
os << json(threadStacks, &proc);
} else {
os << "process: " << *proc.io << "\n";
for (auto &s : threadStacks) {
proc.dumpStackText(os, s);
os << std::endl;
}
}
}
// This is mostly for testing. We start the process, and run pstack when we see
// a signal that is likely to terminate the process, then kill it. This allows
// us to reliably run pstack on a process that will abort or segfault, and
// doesn't require a readable core file.
int
startChild(Elf::Object::sptr exe, const std::string &cmd,
const PstackOptions &options, Dwarf::ImageCache &ic) {
std::vector<std::string> args;
for (size_t off = 0;;) {
auto pos = cmd.find(' ', off);
if (pos == std::string::npos) {
args.push_back(cmd.substr(off));
break;
} else {
args.push_back(cmd.substr(off, pos));
off = pos + 1;
}
}
int rc, status;
pid_t pid = fork();
switch (pid) {
case 0: {
rc = ptrace(PTRACE_TRACEME, 0, 0, 0);
assert(rc == 0);
std::vector<const char *> sysargs;
std::transform(args.begin(), args.end(), std::back_inserter(sysargs),
[] (const std::string &arg) { return arg.c_str(); });
sysargs.push_back(nullptr);
execvp(sysargs[0], (char **)&sysargs[0]);
if (verbose > 2)
*debug << getpid() << " execvp failed: " << strerror(errno) << "\n";
// child
break;
}
case -1:
// error
return -1;
default:
std::shared_ptr<Procman::Process> p;
char statusBuf[PATH_MAX];
snprintf(statusBuf, sizeof statusBuf, "/proc/%d/status", pid);
struct closer { void operator()(FILE *f){ fclose(f); }};
std::unique_ptr<FILE, closer> statusFile { fopen(statusBuf, "r") };
assert(statusFile.get());
for (;;) {
if (verbose > 2)
*debug << getpid() << " waiting...\n";
rc = wait(&status);
if (rc != pid) {
if (verbose > 2)
*debug << getpid() << "... wait failed: " << strerror(errno) << "\n";
break;
}
if (verbose > 2)
*debug << getpid() << "... done - rc=" << rc
<< ", status=" << Procman::WaitStatus(status) << "\n";
if (WIFSTOPPED(status)) {
// Read the content of the process's SigIgn and SigCgt info from procfs.
fflush(statusFile.get());
fseek(statusFile.get(), 0, SEEK_SET);
char line[PATH_MAX];
uint64_t sigblk = -1, sigign = -1, sigcgt = -1;
while (fgets(line, sizeof line, statusFile.get()) != NULL) {
if (strncmp(line, "SigBlk:\t", 8) == 0)
sigblk = strtoull(line + 8, 0, 16);
else if (strncmp(line, "SigCgt:\t", 8) == 0)
sigcgt = strtoull(line + 8, 0, 16);
else if (strncmp(line, "SigIgn:\t", 8) == 0)
sigign = strtoull(line + 8, 0, 16);
}
unsigned long handledSigs = sigblk | sigcgt | sigign;
handledSigs |= 1 << (SIGTRAP - 1);
int stopsig = WSTOPSIG(status);
int contsig = stopsig == SIGSTOP || stopsig == SIGTRAP ? 0 : stopsig;
if (((1 << (stopsig -1)) & handledSigs) == 0) {
p = std::make_shared<Procman::LiveProcess>(exe, pid, options, ic, true);
p->load();
pstack(*p);
rc = ptrace(PTRACE_KILL, pid, 0, contsig);
} else {
rc = ptrace(PTRACE_CONT, pid, 0, contsig);
}
if (rc == -1)
*debug << getpid() << " ptrace failed to kill/continue - " << strerror(errno) << "\n";
assert(rc == 0);
if (verbose > 2)
*debug << getpid() << "..done\n";
}
else {
return 0;
}
}
break;
}
return 0;
}
#ifdef WITH_PYTHON
template<int V> void
doPy(Procman::Process &proc, bool showModules, const PyInterpInfo &info) {
Procman::StopProcess here(&proc);
PythonPrinter<V> printer(proc, *proc.options.output, info);
if (!printer.interpFound())
throw Exception() << "no python interpreter found";
printer.printInterpreters(showModules);
}
/**
* @brief Given a process, tries to print the Python strack trace of it.
* If the process wasn't a Python process, returns false.
* True on successful printing of Python stack trace
*
* @param proc The process
* @param o The stream to which to print the otutput
* @param options Options
* @param showModules Whether to show modules
* @return boolean of whether the process was a Python process or not
*/
bool pystack(Procman::Process &proc, bool showModules) {
PyInterpInfo info = getPyInterpInfo(proc);
if (info.libpython == nullptr) // not a python process or python interpreter not found
return false;
if (info.versionHex < V2HEX(3, 0)) { // Python 2.x
#ifdef WITH_PYTHON2
doPy<2>(proc, showModules, info);
#else
throw (Exception() << "no support for discovered python 2 interpreter");
#endif
} else { // Python 3.x
#ifdef WITH_PYTHON3
doPy<3>(proc, showModules, info);
#else
throw (Exception() << "no support for discovered python 3 interpreter");
#endif
}
return true;
}
#endif
int
usage(std::ostream &os, const char *name, const Flags &options)
{
os <<
"usage: " << name << " <[ exe ] <PID | core> >+\n"
"\n"
"print a stack trace of PID or core. If specified, assume image was created from\n"
" execing `exe`, otherwise, the executable is inferred from the process or core\n"
"\n"
"available options:\n" << options << "\n";
return EX_USAGE;
}
int
emain(int argc, char **argv, Dwarf::ImageCache &imageCache)
{
double sleepTime = 0.0;
PstackOptions options;
std::ofstream out;
bool failures = false;
#if defined(WITH_PYTHON)
bool doPython = false;
bool pythonModules = false;
#endif
std::string execName;
bool printAllStacks = false;
int exitCode = -1; // used for options that exit immediately to signal exit.
std::string subprocessCmd;
Flags flags;
flags
.add("replace-path",
'F',
"from:to",
"replace `from` with `to` in paths when finding shared libraries",
[&](const char *arg) {
auto sep = strchr(arg, ':');
if (sep == 0)
usage(std::cerr, argv[0], flags);
pathReplacements.push_back(std::make_pair(
std::string(arg, sep - arg), std::string(sep + 1))); })
.add("debug-dir",
'g',
"directory",
"extra location to find debug files for binaries and shared libraries",
[&](const char *arg) { Elf::globalDebugDirectories.push_back(arg); })
.add("constant",
'b',
"delay",
"repeat pstack, with `delay` seconds between each iteration (can be non-integer)",
Flags::set(sleepTime))
.add("elf-dump",
'd',
"ELF file",
"dump details of an ELF image in JSON and exit",
[&](const char *arg) {
*options.output << json(Elf::Object(imageCache, loadFile(arg)));
exitCode = 0; })
.add("dwarf-dump",
'D',
"ELF file",
"dump details of DWARF information in an ELF image in JSON and exit",
[&](const char *arg) {
auto dumpobj = std::make_shared<Elf::Object>(imageCache, loadFile(arg));
auto di = std::make_shared<Dwarf::Info>(dumpobj, imageCache);
*options.output << json(*di);
exitCode = 0; })
.add("depth",
'r',
"depth",
"max depth when printing python structures",
Flags::set(options.maxdepth))
.add("max-frames",
'M',
"max frames",
"maximum number of stack frames to print for a thread",
Flags::set(options.maxframes))
.add("help",
'h',
"generate this help message",
[&]() { exitCode = usage(std::cout, argv[0], flags); })
.add("args",
'a',
"attempt to show the value of arguments to functions",
Flags::setf(options.doargs))
.add("json",
'j',
"use JSON output rather than plaintext",
Flags::setf(doJson))
.add("no-src",
's',
"don't include source info",
Flags::setf(options.nosrc))
.add("verbose",
'v',
"more debugging data. Can be repeated",
[&]() { ++verbose; })
.add("no-threaddb",
't',
"don't use the thread_db functions to enumerate pthreads (just uses LWPs)",
Flags::setf(options.nothreaddb))
.add("all",
'A',
"show both python and DWARF (C/C++/go/rust) stack traces",
Flags::setf(printAllStacks))
.add("no-ext-debug",
'n',
"don't load external debugging information when processing",
Flags::setf(Elf::noExtDebug))
.add("version",
'V',
"dump version and exit",
[&]() {
std::clog << STR(VERSION) << "\n";
exitCode = 0; })
#ifdef WITH_PYTHON
.add("python-modules",
'm',
"print contents of all python modules when tracing",
Flags::setf(pythonModules))
.add("python",
'p',
"print python stack traces",
Flags::setf(doPython))
.add("locals",
'l',
"print local variables (just python for now)",
Flags::setf(options.dolocals))
#endif
.add("executable",
'e',
"executable",
"executable to use by default", [&](const char *opt) { execName = opt; })
.add("command",
'x',
"command line",
"execute command line as subprocess, trace when it receives a signal",
Flags::set<std::string>(subprocessCmd))
.add("no-die-names", 'Y', "do not use DIE names for functions",
Flags::setf(options.nodienames))
.add("freeres", Flags::LONGONLY, "free all memory at exit (useful for valgrind/heapcheck)",
Flags::setf(freeres))
.add("output",
'o',
"output file",
"write output to <output file> instead of stdout", [&options, &out] (const char *opt) {
out = std::ofstream(opt, std::ofstream::out|std::ofstream::trunc);
options.output = &out;
})
.parse(argc, argv);
if (exitCode != -1)
return exitCode;
// any instance of a non-core ELF image will override default behaviour of
// discovering the executable
Elf::Object::sptr exec;
if (execName != "")
exec = imageCache.getImageForName(execName);
if (subprocessCmd != "") {
// create a new process and trace it.
startChild(exec, subprocessCmd, options, imageCache);
return 0;
}
if (optind == argc)
return usage(std::cerr, argv[0], flags);
auto doStack = [=] (Procman::Process &proc) {
while (!interrupted) {
#if defined(WITH_PYTHON)
if (doPython || printAllStacks) {
bool isPythonProcess = pystack(proc, pythonModules);
// error if -p but not python process
if (doPython && !isPythonProcess)
throw Exception() << "Couldn't find a Python interpreter";
}
if (!doPython)
#endif
{
pstack(proc);
}
if (sleepTime != 0.0)
usleep(sleepTime * 1000000);
else
break;
}
};
for (int i = optind; i < argc; i++) {
try {
auto process = Procman::Process::load(exec, argv[i], options, imageCache); // this calls the load() instance member.
if (process == nullptr)
exec = imageCache.getImageForName(argv[i]);
else
doStack(*process);
} catch (const std::exception &e) {
std::cerr << "trace of " << argv[i] << " failed: " << e.what() << "\n";
failures = true;
}
}
return failures ? EX_SOFTWARE : 0;
}
}
int
main(int argc, char **argv)
{
try {
Dwarf::ImageCache imageCache;
struct sigaction sa;
memset(&sa, 0, sizeof sa);
sa.sa_handler = [](int) { interrupted = true; };
// Only interrupt cleanly once. Then just terminate, in case we're stuck in a loop
sa.sa_flags = SA_RESETHAND;
sigaction(SIGINT, &sa, nullptr);
int rc = emain(argc, argv, imageCache);
// Normally, exit without free'ing imagecache - don't waste effort
// moving pointers around in a terminating process
if (!freeres)
exit(rc);
return rc;
}
catch (std::exception &ex) {
std::clog << "error: " << ex.what() << std::endl;
return EX_SOFTWARE;
}
}