forked from cloudwu/backtrace-mingw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacktrace.c
376 lines (313 loc) · 8.86 KB
/
backtrace.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
369
370
371
372
373
374
375
/*
Copyright (c) 2010 ,
Cloud Wu . All rights reserved.
http://www.codingnow.com
Use, modification and distribution are subject to the "New BSD License"
as listed at <url: http://www.opensource.org/licenses/bsd-license.php >.
filename: backtrace.c
compiler: gcc 3.4.5 (mingw-win32)
build command: gcc -O2 -shared -Wall -o backtrace.dll backtrace.c -lbfd -liberty -limagehlp
how to use: Call LoadLibraryA("backtrace.dll"); at beginning of your program .
*/
#define PACKAGE "your-program-name"
#define PACKAGE_VERSION "0.1"
#include <windows.h>
#include <excpt.h>
#include <imagehlp.h>
#include <bfd.h>
#include <psapi.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdbool.h>
#define BUFFER_MAX (16*1024)
#define BFD_ERR_OK (0)
#define BFD_ERR_OPEN_FAIL (1)
#define BFD_ERR_BAD_FORMAT (2)
#define BFD_ERR_NO_SYMBOLS (3)
#define BFD_ERR_READ_SYMBOL (4)
static const char *const bfd_errors[] = {
"",
"(Failed to open bfd)",
"(Bad format)",
"(No symbols)",
"(Failed to read symbols)",
};
struct bfd_ctx {
bfd * handle;
asymbol ** symbol;
};
struct bfd_set {
char * name;
struct bfd_ctx * bc;
struct bfd_set *next;
};
struct find_info {
asymbol **symbol;
bfd_vma counter;
const char *file;
const char *func;
unsigned line;
};
struct output_buffer {
char * buf;
size_t sz;
size_t ptr;
};
static void
output_init(struct output_buffer *ob, char * buf, size_t sz) {
ob->buf = buf;
ob->sz = sz;
ob->ptr = 0;
ob->buf[0] = '\0';
}
static void
output_print(struct output_buffer *ob, const char * format, ...) {
if (ob->sz == ob->ptr)
return;
ob->buf[ob->ptr] = '\0';
va_list ap;
va_start(ap,format);
vsnprintf(ob->buf + ob->ptr, ob->sz - ob->ptr, format, ap);
va_end(ap);
ob->ptr = strlen(ob->buf + ob->ptr) + ob->ptr;
}
static void
lookup_section(bfd *abfd, asection *sec, void *opaque_data) {
struct find_info *data = (struct find_info *)opaque_data;
if (data->func)
return;
if (!(bfd_get_section_flags(abfd, sec) & SEC_ALLOC))
return;
bfd_vma vma = bfd_get_section_vma(abfd, sec);
if (data->counter < vma || vma + bfd_get_section_size(sec) <= data->counter)
return;
bfd_find_nearest_line(abfd, sec, data->symbol, data->counter - vma, &(data->file), &(data->func), &(data->line));
}
static void
find(struct bfd_ctx * b, DWORD offset, const char **file, const char **func, unsigned *line) {
struct find_info data;
data.func = NULL;
data.symbol = b->symbol;
data.counter = offset;
data.file = NULL;
data.func = NULL;
data.line = 0;
bfd_map_over_sections(b->handle, &lookup_section, &data);
if (file) {
*file = data.file;
}
if (func) {
*func = data.func;
}
if (line) {
*line = data.line;
}
}
static int
init_bfd_ctx(struct bfd_ctx *bc, const char * procname, int *err) {
bc->handle = NULL;
bc->symbol = NULL;
bfd *b = bfd_openr(procname, 0);
if (!b) {
if(err) {
*err = BFD_ERR_OPEN_FAIL;
}
return 1;
}
if(!bfd_check_format(b, bfd_object)) {
bfd_close(b);
if(err) {
*err = BFD_ERR_BAD_FORMAT;
}
return 1;
}
if(!(bfd_get_file_flags(b) & HAS_SYMS)) {
bfd_close(b);
if(err) {
*err = BFD_ERR_NO_SYMBOLS;
}
return 1;
}
void *symbol_table;
unsigned dummy = 0;
if (bfd_read_minisymbols(b, FALSE, &symbol_table, &dummy) == 0) {
if (bfd_read_minisymbols(b, TRUE, &symbol_table, &dummy) < 0) {
free(symbol_table);
bfd_close(b);
if(err) {
*err = BFD_ERR_READ_SYMBOL;
}
return 1;
}
}
bc->handle = b;
bc->symbol = (asymbol **)symbol_table;
if(err) {
*err = BFD_ERR_OK;
}
return 0;
}
static void
close_bfd_ctx(struct bfd_ctx *bc) {
if (bc) {
if (bc->symbol) {
free(bc->symbol);
}
if (bc->handle) {
bfd_close(bc->handle);
}
}
}
static struct bfd_ctx *
get_bc(struct bfd_set *set, const char *procname, int *err) {
while(set->name) {
if (strcmp(set->name, procname) == 0) {
return set->bc;
}
set = set->next;
}
struct bfd_ctx bc;
if (init_bfd_ctx(&bc, procname, err)) {
return NULL;
}
set->next = (struct bfd_set *)calloc(1, sizeof(*set));
set->bc = (struct bfd_ctx *)malloc(sizeof(struct bfd_ctx));
memcpy(set->bc, &bc, sizeof(bc));
set->name = strdup(procname);
return set->bc;
}
static void
release_set(struct bfd_set *set) {
while(set) {
struct bfd_set * temp = set->next;
free(set->name);
close_bfd_ctx(set->bc);
free(set);
set = temp;
}
}
#ifdef _WIN64
#define Eip Rip
#define Esp Rsp
#define Ebp Rbp
#define DISPLACEMENT_TYPE DWORD64
#else
#define DISPLACEMENT_TYPE DWORD
#endif // _WIN64
static void
_backtrace(struct output_buffer *ob, struct bfd_set *set, int depth, LPCONTEXT context) {
char procname[MAX_PATH];
GetModuleFileNameA(NULL, procname, sizeof procname);
struct bfd_ctx *bc = NULL;
int err = BFD_ERR_OK;
STACKFRAME frame;
memset(&frame,0,sizeof(frame));
frame.AddrPC.Offset = context->Eip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrStack.Offset = context->Esp;
frame.AddrStack.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context->Ebp;
frame.AddrFrame.Mode = AddrModeFlat;
HANDLE process = GetCurrentProcess();
HANDLE thread = GetCurrentThread();
char symbol_buffer[sizeof(IMAGEHLP_SYMBOL) + 255];
char module_name_raw[MAX_PATH];
while(StackWalk(IMAGE_FILE_MACHINE_I386,
process,
thread,
&frame,
context,
0,
SymFunctionTableAccess,
SymGetModuleBase, 0)) {
--depth;
if (depth < 0)
break;
IMAGEHLP_SYMBOL *symbol = (IMAGEHLP_SYMBOL *)symbol_buffer;
symbol->SizeOfStruct = (sizeof *symbol) + 255;
symbol->MaxNameLength = 254;
uintptr_t module_base = (uintptr_t)SymGetModuleBase(process, frame.AddrPC.Offset);
const char * module_name = "[unknown module]";
if (module_base &&
GetModuleFileNameA((HINSTANCE)module_base, module_name_raw, MAX_PATH)) {
module_name = module_name_raw;
bc = get_bc(set, module_name, &err);
}
const char * file = NULL;
const char * func = NULL;
unsigned line = 0;
if (bc) {
find(bc,frame.AddrPC.Offset,&file,&func,&line);
}
if (file == NULL) {
DISPLACEMENT_TYPE dummy = 0;
if (SymGetSymFromAddr(process, frame.AddrPC.Offset, &dummy, symbol)) {
file = symbol->Name;
} else {
file = "[unknown file]";
}
}
if (func == NULL) {
output_print(ob,"0x%08x : %s : %s %s \n",
frame.AddrPC.Offset,
module_name,
file,
bfd_errors[err]);
} else {
output_print(ob,"0x%08x : %s : %s (%d) : in function (%s) \n",
frame.AddrPC.Offset,
module_name,
file,
line,
func);
}
}
}
static char * g_output = NULL;
static LPTOP_LEVEL_EXCEPTION_FILTER g_prev = NULL;
static LONG WINAPI
exception_filter(LPEXCEPTION_POINTERS info) {
struct output_buffer ob;
output_init(&ob, g_output, BUFFER_MAX);
if (!SymInitialize(GetCurrentProcess(), 0, TRUE)) {
output_print(&ob,"Failed to init symbol context\n");
} else {
bfd_init();
struct bfd_set *set = (struct bfd_set *)calloc(1,sizeof(*set));
_backtrace(&ob, set, 128, info->ContextRecord);
release_set(set);
SymCleanup(GetCurrentProcess());
}
fputs(g_output, stderr);
return EXCEPTION_CONTINUE_SEARCH;
}
static void
backtrace_register(void) {
if (g_output == NULL) {
g_output = (char *)malloc(BUFFER_MAX);
g_prev = SetUnhandledExceptionFilter(exception_filter);
}
}
static void
backtrace_unregister(void) {
if (g_output) {
free(g_output);
SetUnhandledExceptionFilter(g_prev);
g_prev = NULL;
g_output = NULL;
}
}
BOOL WINAPI
DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpvReserved) {
switch (dwReason) {
case DLL_PROCESS_ATTACH:
backtrace_register();
break;
case DLL_PROCESS_DETACH:
backtrace_unregister();
break;
}
return TRUE;
}