-
Notifications
You must be signed in to change notification settings - Fork 0
/
cptraceback.h
276 lines (212 loc) · 5.94 KB
/
cptraceback.h
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
#ifndef MY_BACKTRACE_H
#define MY_BACKTRACE_H
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <Python.h>
#include <frameobject.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Keep everything on the stack to avoid mallocing and freeing. All strings are
char arrays of this length. */
#define BUF_LEN 1024
/* Forward declarations */
struct c_frame;
/* Public API */
typedef void (*tb_formatter)(FILE *, struct c_frame *);
void dump_traceback(FILE *f, tb_formatter writer);
void print_tb();
void print_tb_to_file(FILE *f);
struct py_frame
{
char filename[BUF_LEN];
char funcname[BUF_LEN];
size_t line;
};
struct c_frame
{
uint64_t pc;
uint64_t offset;
char funcname[BUF_LEN];
struct py_frame *pyframe;
};
/* Private API */
#define S(arr, idx, ch) (arr)[sizeof(arr) + idx] = ch
#define TRUNCATE(arr) \
S(arr, -1, 0); S(arr, -2, '.'); S(arr, -3, '.'); S(arr, -4, '.');
struct py_stack_ctx
{
PyThreadState *tstate;
PyFrameObject *frame;
};
#ifdef _MSC_VER
/* Windows-specific frame walking API */
#include <Windows.h>
#include <Dbghelp.h>
#define MAX_FRAMES 1024
struct c_stack_ctx
{
void *frames[MAX_FRAMES];
USHORT nFramesCaptured;
USHORT currentFrame;
HANDLE process;
};
static inline
void init_stack_ctx(struct c_stack_ctx *ctx)
{
USHORT nFramesCaptured = CaptureStackBackTrace(
0, MAX_FRAMES, ctx->frames, NULL);
ctx->nFramesCaptured = nFramesCaptured;
ctx->currentFrame = 0;
HANDLE process = GetCurrentProcess();
SymInitialize(process, NULL, TRUE);
ctx->process = process;
}
static inline
int get_next_frame(struct c_stack_ctx *ctx, struct c_frame *frame)
{
if (ctx->currentFrame >= ctx->nFramesCaptured)
return -1;
char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = MAX_SYM_NAME;
IMAGEHLP_LINE64 line;
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
DWORD64 address = (DWORD64)ctx->frames[ctx->currentFrame];
DWORD disp = 0;
frame->pyframe = NULL;
if (SymFromAddr(ctx->process, address, NULL, pSymbol)) {
if (SymGetLineFromAddr64(ctx->process, address, &disp, &line)) {
frame->pc = pSymbol->Address;
frame->offset = line.LineNumber;
strncpy(frame->funcname, pSymbol->Name, BUF_LEN);
TRUNCATE(frame->funcname);
} else {
frame->pc = address;
frame->offset = 0;
strcpy(frame->funcname, "no debug info");
}
} else {
/* TODO do a better job of identifying error */
frame->pc = address;
frame->offset = 0;
strcpy(frame->funcname, "invalid address");
}
ctx->currentFrame++;
return 1;
}
#else
/* Posix frame-walking API (based on libunwind) */
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#undef UNW_LOCAL_ONLY
struct c_stack_ctx
{
unw_cursor_t cursor;
unw_context_t context;
};
static inline
void init_stack_ctx(struct c_stack_ctx *ctx)
{
// TODO check for error
// Initialize cursor to current frame for local unwinding.
unw_getcontext(&(ctx->context));
unw_init_local(&(ctx->cursor), &(ctx->context));
}
static inline
int init_py_stack_ctx(struct py_stack_ctx *ctx)
{
PyThreadState *tstate = PyThreadState_GET();
if ((tstate == NULL) || (tstate->frame == NULL))
return -1;
ctx->tstate = tstate;
ctx->frame = tstate->frame;
return 0;
}
static inline
int get_next_frame(struct c_stack_ctx *ctx, struct c_frame *frame)
{
unw_word_t pc, offset;
if (unw_step(&ctx->cursor) <= 0)
return -1;
unw_get_reg(&ctx->cursor, UNW_REG_IP, &pc);
if (pc == 0)
return -1;
if (unw_get_proc_name(&ctx->cursor, frame->funcname,
sizeof(frame->funcname), &offset) != 0)
return -1;
frame->pc = pc;
frame->offset = offset;
frame->pyframe = NULL;
TRUNCATE(frame->funcname);
return 1;
}
#endif /* _MSC_VER */
static inline char*
_decode(PyObject* obj)
{
/* TODO document whether this returns a pointer or a new string */
/* TODO python3 specific */
return PyUnicode_AsUTF8(obj);
}
static inline
int get_next_pyframe(struct py_stack_ctx *ctx, struct py_frame *pyframe)
{
size_t line;
char *filename, *funcname;
PyFrameObject *frame = ctx->frame;
if (frame == NULL)
return -1;
line = PyCode_Addr2Line(frame->f_code, frame->f_lasti);
filename = _decode(frame->f_code->co_filename);
funcname = _decode(frame->f_code->co_name);
ctx->frame = frame->f_back;
pyframe->line = line;
strncpy(pyframe->filename, filename, sizeof(pyframe->filename));
strncpy(pyframe->funcname, funcname, sizeof(pyframe->funcname));
TRUNCATE(pyframe->filename);
TRUNCATE(pyframe->funcname);
return 0;
}
static inline
void file_tb_formatter(FILE *f, struct c_frame *frame)
{
struct py_frame *pyframe = frame->pyframe;
fprintf(f, "0x%llx: (%s+0x%llx)\n",
frame->pc, frame->funcname, frame->offset);
if (pyframe != NULL)
fprintf(f, "\tPython: %s (%s:%zu)\n",
pyframe->funcname, pyframe->filename, pyframe->line);
}
void dump_traceback(FILE *f, tb_formatter writer)
{
struct c_stack_ctx ctx;
struct py_stack_ctx py_ctx;
struct c_frame frame;
struct py_frame pyframe;
init_stack_ctx(&ctx);
init_py_stack_ctx(&py_ctx);
// Unwind frames one by one, going up the frame stack.
while (get_next_frame(&ctx, &frame) > 0) {
if (strcmp(frame.funcname, "_PyEval_EvalFrameDefault") == 0) {
if (get_next_pyframe(&py_ctx, &pyframe) == 0) {
frame.pyframe = &pyframe;
}
}
writer(f, &frame);
}
}
void print_tb()
{
dump_traceback(stderr, file_tb_formatter);
}
void print_tb_to_file(FILE *f)
{
dump_traceback(f, file_tb_formatter);
}
#ifdef __cplusplus
}
#endif
#endif /* MY_BACKTRACE_H */