forked from osandov/drgn
-
Notifications
You must be signed in to change notification settings - Fork 6
/
stack_trace.c
368 lines (337 loc) · 9.74 KB
/
stack_trace.c
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
// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "drgnpy.h"
#include "../stack_trace.h"
#include "../util.h"
PyObject *StackTrace_wrap(struct drgn_stack_trace *trace) {
StackTrace *ret = call_tp_alloc(StackTrace);
if (!ret)
return NULL;
Py_INCREF(container_of(trace->prog, Program, prog));
ret->trace = trace;
return (PyObject *)ret;
}
static void StackTrace_dealloc(StackTrace *self)
{
struct drgn_program *prog = self->trace->prog;
drgn_stack_trace_destroy(self->trace);
Py_XDECREF(container_of(prog, Program, prog));
Py_TYPE(self)->tp_free((PyObject *)self);
}
static Program *StackTrace_get_prog(StackTrace *self, void *arg)
{
Program *prog = container_of(drgn_stack_trace_program(self->trace),
Program, prog);
Py_INCREF(prog);
return prog;
}
static PyObject *StackTrace_str(StackTrace *self)
{
struct drgn_error *err;
_cleanup_free_ char *str = NULL;
err = drgn_format_stack_trace(self->trace, &str);
if (err)
return set_drgn_error(err);
return PyUnicode_FromString(str);
}
static Py_ssize_t StackTrace_length(StackTrace *self)
{
return drgn_stack_trace_num_frames(self->trace);
}
static StackFrame *StackTrace_item(StackTrace *self, Py_ssize_t i)
{
if (i < 0 || i >= drgn_stack_trace_num_frames(self->trace)) {
PyErr_SetString(PyExc_IndexError,
"stack frame index out of range");
return NULL;
}
StackFrame *ret = call_tp_alloc(StackFrame);
if (!ret)
return NULL;
ret->i = i;
ret->trace = self;
Py_INCREF(self);
return ret;
}
static PyMethodDef StackTrace_methods[] = {
{"_repr_pretty_", (PyCFunction)repr_pretty_from_str,
METH_VARARGS | METH_KEYWORDS},
{},
};
static PySequenceMethods StackTrace_as_sequence = {
.sq_length = (lenfunc)StackTrace_length,
.sq_item = (ssizeargfunc)StackTrace_item,
};
static PyGetSetDef StackTrace_getset[] = {
{"prog", (getter)StackTrace_get_prog, NULL, drgn_StackTrace_prog_DOC},
{},
};
PyTypeObject StackTrace_type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "_drgn.StackTrace",
.tp_basicsize = sizeof(StackTrace),
.tp_dealloc = (destructor)StackTrace_dealloc,
.tp_as_sequence = &StackTrace_as_sequence,
.tp_str = (reprfunc)StackTrace_str,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = drgn_StackTrace_DOC,
.tp_methods = StackTrace_methods,
.tp_getset = StackTrace_getset,
};
static void StackFrame_dealloc(StackFrame *self)
{
Py_XDECREF(self->trace);
Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject *StackFrame_str(StackFrame *self)
{
struct drgn_error *err;
_cleanup_free_ char *str = NULL;
err = drgn_format_stack_frame(self->trace->trace, self->i, &str);
if (err)
return set_drgn_error(err);
return PyUnicode_FromString(str);
}
static PyObject *StackFrame_locals(StackFrame *self)
{
struct drgn_error *err;
const char **names;
size_t count;
err = drgn_stack_frame_locals(self->trace->trace, self->i, &names,
&count);
if (err)
return set_drgn_error(err);
_cleanup_pydecref_ PyObject *list = PyList_New(count);
if (!list) {
drgn_stack_frame_locals_destroy(names, count);
return NULL;
}
for (size_t i = 0; i < count; i++) {
PyObject *string = PyUnicode_FromString(names[i]);
if (!string) {
drgn_stack_frame_locals_destroy(names, count);
return NULL;
}
PyList_SET_ITEM(list, i, string);
}
drgn_stack_frame_locals_destroy(names, count);
return_ptr(list);
}
static DrgnObject *StackFrame_subscript(StackFrame *self, PyObject *key)
{
struct drgn_error *err;
Program *prog = container_of(self->trace->trace->prog, Program, prog);
if (!PyUnicode_Check(key)) {
PyErr_SetObject(PyExc_KeyError, key);
return NULL;
}
const char *name = PyUnicode_AsUTF8(key);
if (!name)
return NULL;
_cleanup_pydecref_ DrgnObject *ret = DrgnObject_alloc(prog);
if (!ret)
return NULL;
bool clear = set_drgn_in_python();
err = drgn_stack_frame_find_object(self->trace->trace, self->i, name,
&ret->obj);
if (clear)
clear_drgn_in_python();
if (err) {
if (err->code == DRGN_ERROR_LOOKUP) {
drgn_error_destroy(err);
PyErr_SetObject(PyExc_KeyError, key);
} else {
set_drgn_error(err);
}
return NULL;
}
return_ptr(ret);
}
static int StackFrame_contains(StackFrame *self, PyObject *key)
{
struct drgn_error *err;
if (!PyUnicode_Check(key)) {
PyErr_SetObject(PyExc_KeyError, key);
return -1;
}
const char *name = PyUnicode_AsUTF8(key);
if (!name)
return -1;
DRGN_OBJECT(tmp, self->trace->trace->prog);
err = drgn_stack_frame_find_object(self->trace->trace, self->i, name,
&tmp);
if (!err) {
return 1;
} else if (err->code == DRGN_ERROR_LOOKUP) {
drgn_error_destroy(err);
return 0;
} else {
return -1;
}
}
static PyObject *StackFrame_source(StackFrame *self)
{
int line;
int column;
const char *filename = drgn_stack_frame_source(self->trace->trace,
self->i, &line, &column);
if (!filename) {
PyErr_SetString(PyExc_LookupError,
"source code location not available");
return NULL;
}
return Py_BuildValue("sii", filename, line, column);
}
static PyObject *StackFrame_symbol(StackFrame *self)
{
struct drgn_error *err;
Program *prog = container_of(self->trace->trace->prog, Program, prog);
struct drgn_symbol *sym;
err = drgn_stack_frame_symbol(self->trace->trace, self->i, &sym);
if (err)
return set_drgn_error(err);
PyObject *ret = Symbol_wrap(sym, (PyObject *)prog);
if (!ret) {
drgn_symbol_destroy(sym);
return NULL;
}
return ret;
}
static PyObject *StackFrame_register(StackFrame *self, PyObject *arg)
{
const char *name = PyUnicode_AsUTF8(arg);
if (!name)
return NULL;
const struct drgn_platform *platform =
drgn_program_platform(self->trace->trace->prog);
const struct drgn_register *reg =
drgn_platform_register_by_name(platform, name);
if (!reg) {
return PyErr_Format(PyExc_ValueError,
"unknown register %R", arg);
}
uint64_t value;
if (!drgn_stack_frame_register(self->trace->trace, self->i, reg,
&value)) {
PyErr_SetString(PyExc_LookupError,
"register value is not known");
return NULL;
}
return PyLong_FromUint64(value);
}
static PyObject *StackFrame_registers(StackFrame *self)
{
_cleanup_pydecref_ PyObject *dict = PyDict_New();
if (!dict)
return NULL;
const struct drgn_platform *platform =
drgn_program_platform(self->trace->trace->prog);
size_t num_registers = drgn_platform_num_registers(platform);
for (size_t i = 0; i < num_registers; i++) {
const struct drgn_register *reg =
drgn_platform_register(platform, i);
uint64_t value;
if (!drgn_stack_frame_register(self->trace->trace, self->i, reg,
&value))
continue;
_cleanup_pydecref_ PyObject *value_obj =
PyLong_FromUint64(value);
if (!value_obj)
return NULL;
size_t num_names;
const char * const *names = drgn_register_names(reg,
&num_names);
for (size_t j = 0; j < num_names; j++) {
if (PyDict_SetItemString(dict, names[j], value_obj))
return NULL;
}
}
return_ptr(dict);
}
static PyObject *StackFrame_get_name(StackFrame *self, void *arg)
{
const char *name = drgn_stack_frame_name(self->trace->trace, self->i);
if (name)
return PyUnicode_FromString(name);
else
Py_RETURN_NONE;
}
static PyObject *StackFrame_get_is_inline(StackFrame *self, void *arg)
{
Py_RETURN_BOOL(drgn_stack_frame_is_inline(self->trace->trace, self->i));
}
static PyObject *StackFrame_get_interrupted(StackFrame *self, void *arg)
{
Py_RETURN_BOOL(drgn_stack_frame_interrupted(self->trace->trace,
self->i));
}
static PyObject *StackFrame_get_pc(StackFrame *self, void *arg)
{
uint64_t pc;
if (drgn_stack_frame_pc(self->trace->trace, self->i, &pc)) {
return PyLong_FromUint64(pc);
} else {
PyErr_SetString(PyExc_LookupError,
"program counter is not known");
return NULL;
}
}
static PyObject *StackFrame_get_sp(StackFrame *self, void *arg)
{
uint64_t sp;
if (drgn_stack_frame_sp(self->trace->trace, self->i, &sp)) {
return PyLong_FromUint64(sp);
} else {
PyErr_SetString(PyExc_LookupError,
"stack pointer is not known");
return NULL;
}
}
static PyMethodDef StackFrame_methods[] = {
{"__getitem__", (PyCFunction)StackFrame_subscript,
METH_O | METH_COEXIST, drgn_StackFrame___getitem___DOC},
{"__contains__", (PyCFunction)StackFrame_contains,
METH_O | METH_COEXIST, drgn_StackFrame___contains___DOC},
{"locals", (PyCFunction)StackFrame_locals,
METH_NOARGS, drgn_StackFrame_locals_DOC},
{"source", (PyCFunction)StackFrame_source, METH_NOARGS,
drgn_StackFrame_source_DOC},
{"symbol", (PyCFunction)StackFrame_symbol, METH_NOARGS,
drgn_StackFrame_symbol_DOC},
{"register", (PyCFunction)StackFrame_register,
METH_O, drgn_StackFrame_register_DOC},
{"registers", (PyCFunction)StackFrame_registers,
METH_NOARGS, drgn_StackFrame_registers_DOC},
{"_repr_pretty_", (PyCFunction)repr_pretty_from_str,
METH_VARARGS | METH_KEYWORDS},
{},
};
static PyGetSetDef StackFrame_getset[] = {
{"name", (getter)StackFrame_get_name, NULL, drgn_StackFrame_name_DOC},
{"is_inline", (getter)StackFrame_get_is_inline, NULL,
drgn_StackFrame_is_inline_DOC},
{"interrupted", (getter)StackFrame_get_interrupted, NULL,
drgn_StackFrame_interrupted_DOC},
{"pc", (getter)StackFrame_get_pc, NULL, drgn_StackFrame_pc_DOC},
{"sp", (getter)StackFrame_get_sp, NULL, drgn_StackFrame_sp_DOC},
{},
};
static PyMappingMethods StackFrame_as_mapping = {
.mp_subscript = (binaryfunc)StackFrame_subscript,
};
static PySequenceMethods StackFrame_as_sequence = {
.sq_contains = (objobjproc)StackFrame_contains,
};
PyTypeObject StackFrame_type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "_drgn.StackFrame",
.tp_basicsize = sizeof(StackFrame),
.tp_dealloc = (destructor)StackFrame_dealloc,
.tp_as_sequence = &StackFrame_as_sequence,
.tp_as_mapping = &StackFrame_as_mapping,
.tp_str = (reprfunc)StackFrame_str,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = drgn_StackFrame_DOC,
.tp_methods = StackFrame_methods,
.tp_getset = StackFrame_getset,
};