forked from ianlancetaylor/libbacktrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacktrace.c
More file actions
427 lines (359 loc) · 11.8 KB
/
backtrace.c
File metadata and controls
427 lines (359 loc) · 11.8 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
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
/* backtrace.c -- Entry point for stack backtrace library.
Copyright (C) 2012-2019 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Google.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
(1) Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
(2) Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
(3) The name of the author may not be used to
endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. */
#include "config.h"
#include <sys/types.h>
#include "unwind.h"
#include "backtrace.h"
#include "internal.h"
/* The main backtrace_full routine. */
/* Data passed through _Unwind_Backtrace. */
struct backtrace_data
{
/* Number of frames to skip. */
int skip;
/* Library state. */
struct backtrace_state *state;
/* Callback routine. */
backtrace_full_callback callback;
/* Error callback routine. */
backtrace_error_callback error_callback;
/* Data to pass to callback routines. */
void *data;
/* Value to return from backtrace_full. */
int ret;
/* Whether there is any memory available. */
int can_alloc;
};
#ifdef __CYGWIN__
#include <windows.h>
static _Unwind_Reason_Code
unwind (uintptr_t pc, void *vdata)
{
struct backtrace_data *bdata = (struct backtrace_data *) vdata;
if (!bdata->can_alloc)
bdata->ret = bdata->callback (bdata->data, pc, NULL, 0, NULL);
else
bdata->ret = backtrace_pcinfo (bdata->state, pc, bdata->callback,
bdata->error_callback, bdata->data);
if (bdata->ret != 0)
return _URC_END_OF_STACK;
return _URC_NO_REASON;
}
static DWORD64 WINAPI MyGetModuleBase64(
_In_ HANDLE hProcess,
_In_ DWORD64 dwAddr)
{
(void)hProcess;
UNWIND_HISTORY_TABLE HistoryTable = {0};
DWORD64 ImageBase = 0;
RtlLookupFunctionEntry(dwAddr, &ImageBase, &HistoryTable);
return ImageBase;
}
static PVOID CALLBACK MyFunctionTableAccess64(
_In_ HANDLE hProcess,
_In_ DWORD64 AddrBase)
{
(void)hProcess;
UNWIND_HISTORY_TABLE HistoryTable = {0};
DWORD64 ImageBase;
return RtlLookupFunctionEntry(AddrBase, &ImageBase, &HistoryTable);
}
static int readable_pointer(LPCVOID pointer)
{
// Check whether the pointer is valid and executable before dereferencing
// to avoid segfault while recording. See #10638.
MEMORY_BASIC_INFORMATION mInfo;
if (VirtualQuery(pointer, &mInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0)
return 0;
DWORD X = mInfo.AllocationProtect;
if (!((X&PAGE_READONLY) || (X&PAGE_READWRITE) || (X&PAGE_WRITECOPY) || (X&PAGE_EXECUTE_READ)) ||
(X&PAGE_GUARD) || (X&PAGE_NOACCESS))
return 0;
return 1;
}
typedef enum {
AddrMode1616,
AddrMode1632,
AddrModeReal,
AddrModeFlat
} ADDRESS_MODE;
typedef struct _tagADDRESS64 {
DWORD64 Offset;
WORD Segment;
ADDRESS_MODE Mode;
} ADDRESS64,*LPADDRESS64;
typedef struct _KDHELP64 {
DWORD64 Thread;
DWORD ThCallbackStack;
DWORD ThCallbackBStore;
DWORD NextCallback;
DWORD FramePointer;
DWORD64 KiCallUserMode;
DWORD64 KeUserCallbackDispatcher;
DWORD64 SystemRangeStart;
DWORD64 KiUserExceptionDispatcher;
DWORD64 StackBase;
DWORD64 StackLimit;
DWORD64 Reserved[5];
} KDHELP64,*PKDHELP64;
typedef struct _tagSTACKFRAME64 {
ADDRESS64 AddrPC;
ADDRESS64 AddrReturn;
ADDRESS64 AddrFrame;
ADDRESS64 AddrStack;
ADDRESS64 AddrBStore;
PVOID FuncTableEntry;
DWORD64 Params[4];
WINBOOL Far;
WINBOOL Virtual;
DWORD64 Reserved[3];
KDHELP64 KdHelp;
} STACKFRAME64,*LPSTACKFRAME64;
struct stack_info
{
CONTEXT c;
STACKFRAME64 sf; /* For storing the stack information */
};
static void stack_info_init(struct stack_info *si, PUINT_PTR framep, PCONTEXT ctx)
{
memset (si, 0, sizeof *si);
memcpy (&si->c, ctx, sizeof si->c);
memset (&si->sf, 0, sizeof (si->sf));
si->sf.AddrFrame.Offset = (UINT_PTR) framep;
si->sf.AddrReturn.Offset = framep[1];
si->sf.AddrPC.Mode = AddrModeFlat;
si->sf.AddrFrame.Mode = AddrModeFlat;
si->sf.AddrStack.Mode = AddrModeFlat;
}
static inline void
__unwind_single_frame (PCONTEXT ctx)
{
PRUNTIME_FUNCTION f;
ULONG64 imagebase;
UNWIND_HISTORY_TABLE hist = {0};
DWORD64 establisher;
PVOID hdl;
f = RtlLookupFunctionEntry (ctx->Rip, &imagebase, &hist);
if (f) {
RtlVirtualUnwind (0, imagebase, ctx->Rip, f, ctx, &hdl, &establisher,
NULL);
}
else {
ctx->Rip = *(ULONG_PTR *) ctx->Rsp;
ctx->Rsp += 8;
}
}
static int stack_info_walk(struct stack_info *si)
{
if (!si->c.Rip) {
printf("DONE: si->c.Rsp: %p, si->c.Rbp : %p\n", (void*)si->c.Rsp, (void*)si->c.Rbp);
return 0;
}
printf("WALK: si->c.Rip: %p, si->c.Rsp: %p, si->c.Rbp : %p, Rip readable: %d, Rsp readable: %d\n", (void*)si->c.Rip, (void*)si->c.Rsp, (void*)si->c.Rbp,
readable_pointer((LPCVOID)si->c.Rip),
readable_pointer((LPCVOID)si->c.Rsp)
);
if ( readable_pointer((LPCVOID)si->c.Rsp)) {
DWORD64 *sptr = (DWORD64*)si->c.Rsp;
sptr -= 4;
printf("sptr[0]=%p, sptr[1]=%p, sptr[2]=%p, sptr[3]=%p, sptr[4]=%p, sptr[5]=%p\n",
(void*)sptr[0], (void*)sptr[1], (void*)sptr[2], (void*)sptr[3], (void*)sptr[4], (void*)sptr[5]);
}
si->sf.AddrPC.Offset = si->c.Rip;
si->sf.AddrStack.Offset = si->c.Rsp;
si->sf.AddrFrame.Offset = si->c.Rbp;
__unwind_single_frame (&si->c);
return 1;
}
int cygwin_backtrace (void **array, int size);
static void *frames[16];
int __attribute__((noinline))
backtrace_full (struct backtrace_state *state, int skip,
backtrace_full_callback callback,
backtrace_error_callback error_callback, void *data)
{
int size = cygwin_backtrace(frames, 16);
struct backtrace_data bdata;
void *p;
bdata.skip = skip + 1;
bdata.state = state;
bdata.callback = callback;
bdata.error_callback = error_callback;
bdata.data = data;
bdata.ret = 0;
/* If we can't allocate any memory at all, don't try to produce
file/line information. */
p = backtrace_alloc (state, 4096, NULL, NULL);
if (p == NULL)
bdata.can_alloc = 0;
else
{
backtrace_free (state, p, 4096, NULL, NULL);
bdata.can_alloc = 1;
}
int i;
char module_name_raw[MAX_PATH];
HANDLE process = GetCurrentProcess();
for (i = skip + 1; i < size; i++) {
DWORD64 ImageBase = MyGetModuleBase64(process, frames[i]);
if (ImageBase && GetModuleFileNameA((HINSTANCE)ImageBase, module_name_raw, MAX_PATH)) {
state->filename = module_name_raw;
}
unwind((uintptr_t)frames[i], &bdata);
}
// New
#if 0
CONTEXT c;
memset (&c, 0, sizeof c);
c.ContextFlags = CONTEXT_FULL;
RtlCaptureContext (&c);
struct stack_info si;
stack_info_init(&si, (PUINT_PTR) c.Rbp, &c);
int i;
for (i = 0; i < 32 && stack_info_walk(&si); i++)
{
printf("%d %p %p\n", i, (void*)si.sf.AddrFrame.Offset, (void*)si.sf.AddrPC.Offset);
}
#endif
#if 0
CONTEXT context = {};
context.ContextFlags = CONTEXT_FULL;
RtlCaptureContext(&context);
CONTEXT cursor = context;
int i = skip + 1;
char module_name_raw[MAX_PATH];
while (1) {
uintptr_t *ip = (uintptr_t*)cursor.Rip;
uintptr_t *sp = (uintptr_t*)cursor.Rsp;
// if (*ip == 0) {
// if (!readable_pointer((LPCVOID)*sp))
// break;
// cursor.Rip = *(DWORD64*)*sp; // POP RIP (aka RET)
// cursor.Rsp += sizeof(void*);
// if (cursor.Rip == 0) {
// break;
// }
// }
DWORD64 ImageBase = MyGetModuleBase64(GetCurrentProcess(), cursor.Rip);
if (!ImageBase)
break;
PRUNTIME_FUNCTION FunctionEntry = (PRUNTIME_FUNCTION)MyFunctionTableAccess64(
GetCurrentProcess(), cursor.Rip);
if (GetModuleFileNameA((HINSTANCE)ImageBase, module_name_raw, MAX_PATH)) {
state->filename = module_name_raw;
} else {
state->filename = "[unknown module]";
}
// fprintf(stderr, "ip: %p (%d); sp: %p (%d) base: %p, name: %s\n", ip, *ip, sp, *sp, ImageBase, module_name);
if (i-- <= 0)
unwind((uintptr_t)ip, &bdata);
if (!FunctionEntry) { // assume this is a NO_FPO RBP-based function
// cursor.Rsp = cursor.Rbp; // MOV RSP, RBP
// if (!readable_pointer((LPCVOID)cursor.Rsp))
// break;
// cursor.Rbp = *(DWORD64*)cursor.Rsp; // POP RBP
// cursor.Rsp += sizeof(void*);
// cursor.Rip = *(DWORD64*)cursor.Rsp; // POP RIP (aka RET)
// cursor.Rsp += sizeof(void*);
}
else {
PVOID HandlerData;
DWORD64 EstablisherFrame;
(void)RtlVirtualUnwind(
0 /*UNW_FLAG_NHANDLER*/,
ImageBase,
cursor.Rip,
FunctionEntry,
&cursor,
&HandlerData,
&EstablisherFrame,
NULL);
}
if (cursor.Rip == 0)
break;
}
#endif
return bdata.ret;
}
#else
/* Unwind library callback routine. This is passed to
_Unwind_Backtrace. */
static _Unwind_Reason_Code
unwind (struct _Unwind_Context *context, void *vdata)
{
struct backtrace_data *bdata = (struct backtrace_data *) vdata;
uintptr_t pc;
int ip_before_insn = 0;
#ifdef HAVE_GETIPINFO
pc = _Unwind_GetIPInfo (context, &ip_before_insn);
#else
pc = _Unwind_GetIP (context);
#endif
if (bdata->skip > 0)
{
--bdata->skip;
return _URC_NO_REASON;
}
if (!ip_before_insn)
--pc;
if (!bdata->can_alloc)
bdata->ret = bdata->callback (bdata->data, pc, NULL, 0, NULL);
else
bdata->ret = backtrace_pcinfo (bdata->state, pc, bdata->callback,
bdata->error_callback, bdata->data);
if (bdata->ret != 0)
return _URC_END_OF_STACK;
return _URC_NO_REASON;
}
/* Get a stack backtrace. */
int __attribute__((noinline))
backtrace_full (struct backtrace_state *state, int skip,
backtrace_full_callback callback,
backtrace_error_callback error_callback, void *data)
{
struct backtrace_data bdata;
void *p;
bdata.skip = skip + 1;
bdata.state = state;
bdata.callback = callback;
bdata.error_callback = error_callback;
bdata.data = data;
bdata.ret = 0;
/* If we can't allocate any memory at all, don't try to produce
file/line information. */
p = backtrace_alloc (state, 4096, NULL, NULL);
if (p == NULL)
bdata.can_alloc = 0;
else
{
backtrace_free (state, p, 4096, NULL, NULL);
bdata.can_alloc = 1;
}
_Unwind_Backtrace (unwind, &bdata);
return bdata.ret;
}
#endif