forked from peadar/pstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpstack.cc
More file actions
275 lines (253 loc) · 8.11 KB
/
pstack.cc
File metadata and controls
275 lines (253 loc) · 8.11 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
#include "libpstack/dwarf.h"
#include "libpstack/proc.h"
#include "libpstack/ps_callback.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 <sysexits.h>
#include <unistd.h>
#include <csignal>
#include <iostream>
#include <set>
#define XSTR(a) #a
#define STR(a) XSTR(a)
extern std::ostream & operator << (std::ostream &os, const JSON<ThreadStack, Process *> &jt);
namespace {
bool doJson = false;
volatile bool interrupted = false;
int usage(const char *);
std::ostream &
pstack(Process &proc, std::ostream &os, const PstackOptions &options)
{
// get its back trace.
std::list<ThreadStack> threadStacks;
std::set<pid_t> tracedLwps;
{
StopProcess here(&proc);
proc.listThreads([&proc, &threadStacks, &tracedLwps] (const td_thrhandle_t *thr) {
Elf::CoreRegisters regs;
td_err_e the;
#ifdef __linux__
the = td_thr_getgregs(thr, (elf_greg_t *) ®s);
#else
the = td_thr_getgregs(thr, ®s);
#endif
if (the == TD_OK) {
threadStacks.push_back(ThreadStack());
td_thr_get_info(thr, &threadStacks.back().info);
threadStacks.back().unwind(proc, regs);
tracedLwps.insert(threadStacks.back().info.ti_lid);
}
});
for (auto &lwp : proc.lwps) {
if (tracedLwps.find(lwp.first) == tracedLwps.end()) {
threadStacks.push_back(ThreadStack());
threadStacks.back().info.ti_lid = lwp.first;
Elf::CoreRegisters regs;
proc.getRegs(lwp.first, ®s);
threadStacks.back().unwind(proc, regs);
}
}
}
/*
* resume at this point - maybe a bit optimistic if a shared library gets
* unloaded while we print stuff out, but worth the risk, normally.
*/
if (doJson) {
os << json(threadStacks, &proc);
} else {
os << "process: " << *proc.io << "\n";
for (auto &s : threadStacks) {
proc.dumpStackText(os, s, options);
os << std::endl;
}
}
return os;
}
#ifdef WITH_PYTHON
template<int V> void doPy(Process &proc, std::ostream &o, const PstackOptions &options, bool showModules, const PyInterpInfo &info) {
PythonPrinter<V> printer(proc, o, options, info);
if (!printer.interpFound())
throw Exception() << "no python interpreter found";
printer.printInterpreters(showModules);
}
void pystack(Process &proc, std::ostream &o, const PstackOptions &options, bool showModules) {
PyInterpInfo info = getPyInterpInfo(proc);
if (info.versionHex < V2HEX(3, 0)) { // Python 2.x
#ifdef WITH_PYTHON2
doPy<2>(proc, o, options, showModules, info);
#else
throw (Exception() << "no support for discovered python 2 interpreter");
#endif
} else { // Python 3.x
#ifdef WITH_PYTHON3
doPy<3>(proc, o, options, showModules, info);
#else
throw (Exception() << "no support for discovered python 3 interpreter");
#endif
}
}
#endif
int
emain(int argc, char **argv)
{
int i, c;
std::string execFile;
Elf::Object::sptr exec;
Dwarf::ImageCache imageCache;
double sleepTime = 0.0;
PstackOptions options;
#if defined(WITH_PYTHON)
bool python = false;
bool pythonModules = false;
#endif
bool coreOnExit = false;
while ((c = getopt(argc, argv, "F:b:d:CD:hjmsVvag:ptz:")) != -1) {
switch (c) {
case 'F': g_openPrefix = optarg;
break;
case 'g':
Elf::globalDebugDirectories.add(optarg);
break;
case 'D': {
auto dumpobj = std::make_shared<Elf::Object>(imageCache, loadFile(optarg));
auto di = std::make_shared<Dwarf::Info>(dumpobj, imageCache);
std::cout << json(*di);
goto done;
}
case 'z':
case 'd': {
/* Undocumented option to dump image contents */
std::cout << json(Elf::Object(imageCache, loadFile(optarg)));
goto done;
}
case 'h':
usage(argv[0]);
goto done;
case 'a':
options.flags.set(PstackOptions::doargs);
break;
case 'j':
doJson = true;
break;
case 's':
options.flags.set(PstackOptions::nosrc);
break;
case 'v':
verbose++;
break;
case 'b':
sleepTime = strtod(optarg, nullptr);
break;
case 'm':
#if defined(WITH_PYTHON)
pythonModules = true;
#else
std::clog << "no python support compiled in" << std::endl;
#endif
break;
case 'p':
#if defined(WITH_PYTHON)
python = true;
#else
std::clog << "no python support compiled in" << std::endl;
#endif
break;
case 't':
options.flags.set(PstackOptions::nothreaddb);
break;
case 'V':
std::clog << STR(VERSION) << "\n";
return 0;
case 'C':
coreOnExit = true;
break;
default:
return usage(argv[0]);
}
}
if (optind == argc)
return usage(argv[0]);
for (i = optind; i < argc; i++) {
try {
auto doStack = [=, &options] (Process &proc) {
proc.load(options);
while (!interrupted) {
#if defined(WITH_PYTHON)
if (python) {
pystack(proc, std::cout, options, pythonModules);
}
else
#endif
{
pstack(proc, std::cout, options);
}
if (sleepTime != 0.0) {
usleep(sleepTime * 1000000);
} else {
break;
}
}
};
auto process = Process::load(exec, argv[i], options, imageCache);
if (process == nullptr) {
exec = imageCache.getImageForName(argv[i]);
} else {
doStack(*process);
}
} catch (const std::exception &e) {
std::cerr << "failed to process " << argv[i] << ": " << e.what() << "\n";
}
}
done:
if (coreOnExit)
abort();
return 0;
}
int
usage(const char *name)
{
std::clog <<
"usage: " << name << "\n"
"\t[-<D|d> <elf object>] dump details of ELF object (D => show DWARF info\n"
"or\n"
"\t[-h] show this message\n"
"or\n"
"\t[-v] include verbose information to stderr\n"
"\t[-V] dump git tag of source\n"
"\t[-s] don't include source-level details\n"
"\t[-g] add global debug directory\n"
"\t[-a] show arguments to functions where possible\n"
"\t[-n] don't try to find external debug images\n"
"\t[-t] don't try to use the thread_db library\n"
"\t[-b<n>] batch mode: repeat every 'n' seconds\n"
#ifdef WITH_PYTHON
"\t[-p] print python backtrace if available\n"
#endif
"\t[<pid>|<core>|<executable>]* list cores and pids to examine. An executable\n"
"\t will override use of in-core or in-process information\n"
"\t to predict location of the executable\n"
;
return (EX_USAGE);
}
}
int
main(int argc, char **argv)
{
try {
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);
emain(argc, argv);
}
catch (std::exception &ex) {
std::clog << "error: " << ex.what() << std::endl;
return EX_SOFTWARE;
}
}