forked from peadar/pstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.tcc
428 lines (378 loc) · 14.4 KB
/
python.tcc
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
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <stdlib.h>
#include <stddef.h>
#include "libpstack/dwarf.h"
#include "libpstack/python.h"
// This reimplements PyCode_Addr2Line
template<int PyV> int
getLine(const Reader &proc, const PyCodeObject *code, const PyFrameObject *frame)
{
auto lnotab = readPyObj<PyV, PyVarObject>(proc, Elf::Addr(code->co_lnotab));
unsigned char linedata[lnotab.ob_size];
proc.readObj(Elf::Addr(code->co_lnotab) + offsetof(PyBytesObject, ob_sval),
&linedata[0], lnotab.ob_size);
int line = code->co_firstlineno;
int addr = 0;
unsigned char *p = linedata;
unsigned char *e = linedata + lnotab.ob_size;
while (p < e) {
addr += *p++;
if (addr > frame->f_lasti) {
break;
}
line += *p++;
}
return line;
}
template <int PyV> class HeapPrinter : public PythonTypePrinter<PyV> {
Elf::Addr print(const PythonPrinter<PyV> *pc, const PyObject *, const PyTypeObject *pto, Elf::Addr remote) const override {
pc->os << pc->proc.io->readString(Elf::Addr(pto->tp_name));
if (pto->tp_dictoffset > 0) {
pc->os << "\n";
pc->depth++;
PyObject *dictAddr;
pc->proc.io->readObj(remote + pto->tp_dictoffset, &dictAddr);
pc->print(Elf::Addr(dictAddr));
pc->depth--;
pc->os << "\n";
}
return 0;
}
const char *type() const override { return nullptr; }
bool dupdetect() const override { return true; }
};
template <int PyV> class StringPrinter : public PythonTypePrinter<PyV> {
Elf::Addr print(const PythonPrinter<PyV> *pc, const PyObject *, const PyTypeObject *, Elf::Addr addr) const override {
auto str = readString<PyV>(*pc->proc.io, addr);
pc->os << "\"" << str << "\"";
return 0;
}
const char *type() const override { return PythonTypePrinter<PyV>::pyBytesType; }
bool dupdetect() const override { return false; }
};
template <int PyV> class FloatPrinter : public PythonTypePrinter<PyV> {
Elf::Addr print(const PythonPrinter<PyV> *pc, const PyObject *pyo, const PyTypeObject *, Elf::Addr) const override {
auto *pfo = (const PyFloatObject *)pyo;
pc->os << std::fixed << std::setprecision(6) << pfo->ob_fval;
return 0;
}
const char *type() const override { return "PyFloat_Type"; }
bool dupdetect() const override { return false; }
};
struct PyModuleObject {
PyObject_HEAD
PyObject *md_dict;
};
template<int PyV> class ModulePrinter : public PythonTypePrinter<PyV> {
Elf::Addr print(const PythonPrinter<PyV> *pc, const PyObject *pyo, const PyTypeObject *, Elf::Addr) const override {
auto *pmo = (PyModuleObject *)pyo;
pc->print((Elf::Addr)pmo->md_dict);
return 0;
}
const char *type() const override { return "PyModule_Type"; }
};
template<int PyV> class ListPrinter : public PythonTypePrinter<PyV> {
Elf::Addr print(const PythonPrinter<PyV> *pc, const PyObject *po, const PyTypeObject *, Elf::Addr) const override {
auto plo = reinterpret_cast<const PyListObject *>(po);
pc->os << "[ \n";
auto size = std::min(((PyVarObject *)plo)->ob_size, Py_ssize_t(100));
PyObject *objects[size];
pc->proc.io->readObj(Elf::Addr(plo->ob_item), &objects[0], size);
pc->depth++;
for (auto addr : objects) {
pc->os << pc->prefix();
pc->print(Elf::Addr(addr));
pc->os << ",\n";
}
pc->depth--;
pc->os << pc->prefix() << "]\n";
return 0;
}
const char *type() const override { return "PyList_Type"; }
bool dupdetect() const override { return true; }
};
template <int PyV> class TypePrinter : public PythonTypePrinter<PyV> {
Elf::Addr print(const PythonPrinter<PyV> *pc, const PyObject *pyo, const PyTypeObject *, Elf::Addr) const override {
auto pto = (const _typeobject *)pyo;
pc->os << "type :\"" << pc->proc.io->readString(Elf::Addr(pto->tp_name)) << "\"";
return 0;
}
const char *type() const override { return "PyType_Type"; }
bool dupdetect() const override { return true; }
};
template <int PyV> class LongPrinter : public PythonTypePrinter<PyV> {
Elf::Addr print(const PythonPrinter<PyV> *pc, const PyObject *pyo, const PyTypeObject *, Elf::Addr) const override {
auto plo = (PyLongObject *)pyo;
intmax_t value = 0;
for (int i = 0; i < ((PyVarObject *)plo)->ob_size; ++i) {
value += intmax_t(plo->ob_digit[i]) << (PyLong_SHIFT * i) ;
}
pc->os << value;
return 0;
}
const char *type() const override {
return "PyLong_Type";
}
bool dupdetect() const override { return false; }
};
template <int PyV> class TuplePrinter : public PythonTypePrinter<PyV> {
Elf::Addr print(const PythonPrinter<PyV> *pc, const PyObject *pyo, const PyTypeObject *, Elf::Addr remoteAddr) const override {
auto tuple = reinterpret_cast<const PyTupleObject *>(pyo);
pc->os << "( \n";
auto size = std::min(((PyVarObject *)tuple)->ob_size, Py_ssize_t(100));
PyObject *objects[size];
pc->proc.io->readObj(remoteAddr + offsetof(PyTupleObject, ob_item), &objects[0], size);
pc->depth++;
for (auto addr : objects) {
pc->os << pc->prefix();
pc->print(Elf::Addr(addr));
pc->os << ",\n";
}
pc->depth--;
pc->os << pc->prefix() << ")\n";
return 0;
}
const char *type() const override {return "PyTuple_Type";}
bool dupdetect() const override {return true;}
};
template <int PyV>
int
printTupleVars(const PythonPrinter<PyV> *pc, Elf::Addr namesAddr, Elf::Addr valuesAddr, const char *type, Py_ssize_t maxvals = 1000000)
{
const auto &names = readPyObj<PyV, PyTupleObject>(*pc->proc.io, namesAddr);
maxvals = std::min(((PyVarObject *)&names)->ob_size, maxvals);
if (maxvals == 0)
return 0;
std::vector<PyObject *> varnames(maxvals);
std::vector<PyObject *> varvals(maxvals);
pc->proc.io->readObj(namesAddr + offsetof(PyTupleObject, ob_item), &varnames[0], maxvals);
pc->proc.io->readObj(valuesAddr, &varvals[0], maxvals);
pc->os << pc->prefix() << type <<":" << std::endl;
pc->depth++;
for (auto i = 0; i < maxvals; ++i) {
pc->os << pc->prefix();
pc->print(Elf::Addr(varnames[i]));
pc->os << "=";
pc->print(Elf::Addr(varvals[i]));
pc->os << "\n";
}
pc->depth--;
return maxvals;
}
template <int PyV> class FramePrinter : public PythonTypePrinter<PyV> {
Elf::Addr print(const PythonPrinter<PyV> *pc, const PyObject *pyo, const PyTypeObject *, Elf::Addr remoteAddr) const override {
auto pfo = (const PyFrameObject *)pyo;
if (pfo->f_code != 0) {
const auto &code = readPyObj<PyV, PyCodeObject>(*pc->proc.io, Elf::Addr(pfo->f_code));
auto lineNo = getLine<PyV>(*pc->proc.io, &code, pfo);
auto func = readString<PyV>(*pc->proc.io, Elf::Addr(code.co_name));
auto file = readString<PyV>(*pc->proc.io, Elf::Addr(code.co_filename));
pc->os << pc->prefix() << func << " in " << file << ":" << lineNo << "\n";
if (pc->options.flags[PstackOptions::doargs]) {
Elf::Addr flocals = remoteAddr + offsetof(PyFrameObject, f_localsplus);
pc->depth++;
printTupleVars<PyV>(pc, Elf::Addr(code.co_varnames), flocals, "fastlocals", code.co_nlocals);
flocals += code.co_nlocals * sizeof (PyObject *);
auto cellcount = printTupleVars(pc, Elf::Addr(code.co_cellvars), flocals, "cells");
flocals += cellcount * sizeof (PyObject *);
printTupleVars(pc, Elf::Addr(code.co_freevars), flocals, "freevars");
--pc->depth;
}
}
if (pc->options.flags[PstackOptions::doargs] && pfo->f_locals != 0) {
pc->depth++;
pc->os << pc->prefix() << "locals: " << std::endl;
pc->print(Elf::Addr(pfo->f_locals));
pc->depth--;
}
return Elf::Addr(pfo->f_back);
}
const char *type() const override { return "PyFrame_Type"; }
bool dupdetect() const override { return true; }
};
template <int PyV>
const char *
PythonPrinter<PyV>::prefix() const
{
static const char spaces[] =
" "
" "
" "
" "
" "
" ";
return spaces + sizeof spaces - 1 - depth * 4;
}
template<int PyV>
PythonTypePrinter<PyV>::PythonTypePrinter()
{
all.insert(this);
}
template<int PyV>
PythonTypePrinter<PyV>::~PythonTypePrinter()
{
all.erase(this);
}
template<int PyV>
void
PythonPrinter<PyV>::printInterpreters(bool withModules)
{
Elf::Addr ptr;
for (proc.io->readObj(interp_head, &ptr); ptr; )
ptr = printInterp(ptr, withModules);
}
template <int PyV> bool PythonPrinter<PyV>::interpFound() const {
return interp_head != 0;
}
template <int PyV>
PythonPrinter<PyV>::PythonPrinter(Process &proc_, std::ostream &os_, const PstackOptions &options_, const PyInterpInfo &info_)
: proc(proc_)
, os(os_)
, depth(0)
, interp_head(info_.interpreterHead)
, libpython(info_.libpython)
, libpythonAddr(info_.libpythonAddr)
, options(options_)
, info(info_)
{
if (!interpFound())
return;
static HeapPrinter<PyV> heapPrinter;
static StringPrinter<PyV> stringPrinter;
static FloatPrinter<PyV> floatPrinter;
static ModulePrinter<PyV> modulePrinter;
static ListPrinter<PyV> listPrinter;
static TypePrinter<PyV> typePrinter;
static LongPrinter<PyV> longPrinter;
static FramePrinter<PyV> framePrinter;
static TuplePrinter<PyV> tuplePrinter;
for (auto ps : PythonTypePrinter<PyV>::all) {
if (ps->type() == nullptr)
continue; // heapPrinter is used specially.
auto sym = libpython->findDynamicSymbol(ps->type());
if (!sym)
throw Exception() << "failed to find python symbol " << ps->type();
printers[(const _typeobject *)(libpythonAddr + sym.symbol.st_value)] = ps;
}
}
template <int PyV>
void
PythonPrinter<PyV>::print(Elf::Addr remoteAddr) const {
if (depth > 10000) {
os << "too deep" << std::endl;
return;
}
depth++;
try {
while (remoteAddr) {
auto baseObj = readPyObj<PyV, PyVarObject>(*proc.io, remoteAddr);
if (pyRefcnt<const PyVarObject, PyV>(&baseObj) == 0) {
os << "(dead object)";
}
auto objtype = pyObjtype<PyV>(&baseObj);
auto it = printers.find(objtype);
const PythonTypePrinter<PyV> *printer = it == printers.end() ? nullptr : it->second;
auto &pto = types[pyObjtype<PyV>(&baseObj)];
if (pto == nullptr) {
pto.reset((_typeobject *)malloc(sizeof(PyTypeObject)));
readPyObj<PyV, PyTypeObject>(*proc.io,
(Elf::Addr)pyObjtype<PyV>(&baseObj),
pto.get());
}
if (printer == 0) {
std::string tn;
tn = proc.io->readString(Elf::Addr(pto->tp_name));
if (tn == "NoneType") {
os << "None";
break;
} else if (printer == 0 && (pto->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
static HeapPrinter<PyV> heapPrinter;
printer = &heapPrinter;
} else {
os << remoteAddr << " unprintable-type-" << tn << "@"<< pyObjtype<PyV>(&baseObj) << std::endl;
break;
}
}
if (printer->dupdetect() && visited.find(remoteAddr ) != visited.end()) {
os << "(already seen)";
break;
}
if (printer->dupdetect())
visited.insert(remoteAddr);
size_t size = pto->tp_basicsize;
size_t itemsize = pto->tp_itemsize;
ssize_t fullSize;
if (itemsize != 0) {
// object is a variable length object:
if (baseObj.ob_size > 65536 || baseObj.ob_size < 0) {
os << "(skip massive object " << baseObj.ob_size << ")";
break;
}
fullSize = size + itemsize * baseObj.ob_size;
} else {
fullSize = size;
}
char buf[fullSize];
proc.io->readObj(remoteAddr, buf, fullSize);
remoteAddr = printer->print(this, (const PyObject *)buf, pto.get(), remoteAddr);
}
}
catch (const std::exception &ex) {
os << "(print failed:" << ex.what() << ")";
}
catch (...) {
os << "(print failed)";
}
--depth;
}
/*
* process one python thread in an interpreter, at remote addr "ptr".
* returns the address of the next thread on the list.
*/
template <int PyV>
Elf::Addr
PythonPrinter<PyV>::printThread(Elf::Addr ptr)
{
auto thread = readPyObj<PyV, PyThreadState>(*proc.io, ptr);
size_t toff;
if (thread.thread_id && pthreadTidOffset(proc, &toff)) {
Elf::Addr tidptr = thread.thread_id + toff;
pid_t tid;
proc.io->readObj(tidptr, &tid);
os << "pthread: 0x" << std::hex << thread.thread_id << std::dec << ", lwp " << tid;
} else {
os << "anonymous thread";
}
os << "\n";
print(Elf::Addr(thread.frame));
return Elf::Addr(thread.next);
}
/*
* Process one python interpreter in the process at remote address ptr
* Returns the address of the next interpreter on on the process's list.
*/
template <int PyV>
Elf::Addr
PythonPrinter<PyV>::printInterp(Elf::Addr ptr, bool showModules)
{
// these are the first two fields in PyInterpreterState - next and tstate_head.
struct State {
Elf::Addr next;
Elf::Addr head;
Elf::Addr modules;
};
State state;
proc.io->readObj(ptr, &state);
os << "---- interpreter @" << std::hex << ptr << std::dec << " -----" << std::endl ;
for (Elf::Addr tsp = state.head; tsp; ) {
tsp = printThread(tsp);
os << std::endl;
}
if (showModules) {
os << "---- modules:" << std::endl;
print(state.modules);
}
return state.next;
}