[Cosmetic] backtrace.c: tab --> space x 4

git-svn-id: https://svn.eduke32.com/eduke32@4504 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
hendricks266 2014-06-13 09:03:25 +00:00
parent e647b6a42f
commit 3d37dc86ae

View file

@ -1,11 +1,11 @@
/* /*
Copyright (c) 2010 , Copyright (c) 2010 ,
Cloud Wu . All rights reserved. Cloud Wu . All rights reserved.
http://www.codingnow.com http://www.codingnow.com
Use, modification and distribution are subject to the "New BSD License" Use, modification and distribution are subject to the "New BSD License"
as listed at <url: http://www.opensource.org/licenses/bsd-license.php >. as listed at <url: http://www.opensource.org/licenses/bsd-license.php >.
filename: backtrace.c filename: backtrace.c
@ -75,183 +75,183 @@
#define BUFFER_MAX (16*1024) #define BUFFER_MAX (16*1024)
struct bfd_ctx { struct bfd_ctx {
bfd * handle; bfd * handle;
asymbol ** symbol; asymbol ** symbol;
}; };
struct bfd_set { struct bfd_set {
char * name; char * name;
struct bfd_ctx * bc; struct bfd_ctx * bc;
struct bfd_set *next; struct bfd_set *next;
}; };
struct find_info { struct find_info {
asymbol **symbol; asymbol **symbol;
bfd_vma counter; bfd_vma counter;
const char *file; const char *file;
const char *func; const char *func;
unsigned line; unsigned line;
}; };
struct output_buffer { struct output_buffer {
char * buf; char * buf;
size_t sz; size_t sz;
size_t ptr; size_t ptr;
}; };
static void static void
output_init(struct output_buffer *ob, char * buf, size_t sz) output_init(struct output_buffer *ob, char * buf, size_t sz)
{ {
ob->buf = buf; ob->buf = buf;
ob->sz = sz; ob->sz = sz;
ob->ptr = 0; ob->ptr = 0;
ob->buf[0] = '\0'; ob->buf[0] = '\0';
} }
static void static void
output_print(struct output_buffer *ob, const char * format, ...) output_print(struct output_buffer *ob, const char * format, ...)
{ {
va_list ap; va_list ap;
if (ob->sz == ob->ptr) if (ob->sz == ob->ptr)
return; return;
ob->buf[ob->ptr] = '\0'; ob->buf[ob->ptr] = '\0';
va_start(ap,format); va_start(ap,format);
vsnprintf(ob->buf + ob->ptr , ob->sz - ob->ptr , format, ap); vsnprintf(ob->buf + ob->ptr , ob->sz - ob->ptr , format, ap);
va_end(ap); va_end(ap);
ob->ptr = strlen(ob->buf + ob->ptr) + ob->ptr; ob->ptr = strlen(ob->buf + ob->ptr) + ob->ptr;
} }
static void static void
lookup_section(bfd *abfd, asection *sec, void *opaque_data) lookup_section(bfd *abfd, asection *sec, void *opaque_data)
{ {
struct find_info *data = opaque_data; struct find_info *data = opaque_data;
bfd_vma vma; bfd_vma vma;
if (data->func) if (data->func)
return; return;
if (!(bfd_get_section_flags(abfd, sec) & SEC_ALLOC)) if (!(bfd_get_section_flags(abfd, sec) & SEC_ALLOC))
return; return;
vma = bfd_get_section_vma(abfd, sec); vma = bfd_get_section_vma(abfd, sec);
if (data->counter < vma || vma + bfd_get_section_size(sec) <= data->counter) if (data->counter < vma || vma + bfd_get_section_size(sec) <= data->counter)
return; return;
bfd_find_nearest_line(abfd, sec, data->symbol, data->counter - vma, &(data->file), &(data->func), &(data->line)); bfd_find_nearest_line(abfd, sec, data->symbol, data->counter - vma, &(data->file), &(data->func), &(data->line));
} }
static void static void
find(struct bfd_ctx * b, DWORD offset, const char **file, const char **func, unsigned *line) find(struct bfd_ctx * b, DWORD offset, const char **file, const char **func, unsigned *line)
{ {
struct find_info data; struct find_info data;
data.func = NULL; data.func = NULL;
data.symbol = b->symbol; data.symbol = b->symbol;
data.counter = offset; data.counter = offset;
data.file = NULL; data.file = NULL;
data.func = NULL; data.func = NULL;
data.line = 0; data.line = 0;
bfd_map_over_sections(b->handle, &lookup_section, &data); bfd_map_over_sections(b->handle, &lookup_section, &data);
if (file) { if (file) {
*file = data.file; *file = data.file;
} }
if (func) { if (func) {
*func = data.func; *func = data.func;
} }
if (line) { if (line) {
*line = data.line; *line = data.line;
} }
} }
static int static int
init_bfd_ctx(struct bfd_ctx *bc, const char * procname, struct output_buffer *ob) init_bfd_ctx(struct bfd_ctx *bc, const char * procname, struct output_buffer *ob)
{ {
int r1, r2, r3; int r1, r2, r3;
bfd *b; bfd *b;
void *symbol_table; void *symbol_table;
unsigned dummy = 0; unsigned dummy = 0;
bc->handle = NULL; bc->handle = NULL;
bc->symbol = NULL; bc->symbol = NULL;
b = bfd_openr(procname, 0); b = bfd_openr(procname, 0);
if (!b) { if (!b) {
output_print(ob,"Failed to open bfd from (%s)\n" , procname); output_print(ob,"Failed to open bfd from (%s)\n" , procname);
return 1; return 1;
} }
r1 = bfd_check_format(b, bfd_object); r1 = bfd_check_format(b, bfd_object);
r2 = bfd_check_format_matches(b, bfd_object, NULL); r2 = bfd_check_format_matches(b, bfd_object, NULL);
r3 = bfd_get_file_flags(b) & HAS_SYMS; r3 = bfd_get_file_flags(b) & HAS_SYMS;
if (!(r1 && r2 && r3)) { if (!(r1 && r2 && r3)) {
bfd_close(b); bfd_close(b);
if (!(r1 && r2)) if (!(r1 && r2))
output_print(ob,"Failed to init bfd from (%s): %d %d %d\n", procname, r1, r2, r3); output_print(ob,"Failed to init bfd from (%s): %d %d %d\n", procname, r1, r2, r3);
return 1; return 1;
} }
if (bfd_read_minisymbols(b, FALSE, &symbol_table, &dummy) == 0) { if (bfd_read_minisymbols(b, FALSE, &symbol_table, &dummy) == 0) {
if (bfd_read_minisymbols(b, TRUE, &symbol_table, &dummy) < 0) { if (bfd_read_minisymbols(b, TRUE, &symbol_table, &dummy) < 0) {
free(symbol_table); free(symbol_table);
bfd_close(b); bfd_close(b);
output_print(ob,"Failed to read symbols from (%s)\n", procname); output_print(ob,"Failed to read symbols from (%s)\n", procname);
return 1; return 1;
} }
} }
bc->handle = b; bc->handle = b;
bc->symbol = symbol_table; bc->symbol = symbol_table;
return 0; return 0;
} }
static void static void
close_bfd_ctx(struct bfd_ctx *bc) close_bfd_ctx(struct bfd_ctx *bc)
{ {
if (bc) { if (bc) {
if (bc->symbol) { if (bc->symbol) {
free(bc->symbol); free(bc->symbol);
} }
if (bc->handle) { if (bc->handle) {
bfd_close(bc->handle); bfd_close(bc->handle);
} }
} }
} }
static struct bfd_ctx * static struct bfd_ctx *
get_bc(struct output_buffer *ob , struct bfd_set *set , const char *procname) get_bc(struct output_buffer *ob , struct bfd_set *set , const char *procname)
{ {
struct bfd_ctx bc; struct bfd_ctx bc;
while(set->name) { while(set->name) {
if (strcmp(set->name , procname) == 0) { if (strcmp(set->name , procname) == 0) {
return set->bc; return set->bc;
} }
set = set->next; set = set->next;
} }
if (init_bfd_ctx(&bc, procname , ob)) { if (init_bfd_ctx(&bc, procname , ob)) {
return NULL; return NULL;
} }
set->next = calloc(1, sizeof(*set)); set->next = calloc(1, sizeof(*set));
set->bc = malloc(sizeof(struct bfd_ctx)); set->bc = malloc(sizeof(struct bfd_ctx));
memcpy(set->bc, &bc, sizeof(bc)); memcpy(set->bc, &bc, sizeof(bc));
set->name = strdup(procname); set->name = strdup(procname);
return set->bc; return set->bc;
} }
static void static void
release_set(struct bfd_set *set) release_set(struct bfd_set *set)
{ {
while(set) { while(set) {
struct bfd_set * temp = set->next; struct bfd_set * temp = set->next;
if (set->name) if (set->name)
free(set->name); free(set->name);
close_bfd_ctx(set->bc); close_bfd_ctx(set->bc);
free(set); free(set);
set = temp; set = temp;
} }
} }
static char procname[MAX_PATH]; static char procname[MAX_PATH];
@ -267,84 +267,84 @@ static char procname[MAX_PATH];
static void static void
_backtrace(struct output_buffer *ob, struct bfd_set *set, int depth , LPCONTEXT context) _backtrace(struct output_buffer *ob, struct bfd_set *set, int depth , LPCONTEXT context)
{ {
MAYBE64(STACKFRAME) frame; MAYBE64(STACKFRAME) frame;
HANDLE process, thread; HANDLE process, thread;
char symbol_buffer[sizeof(MAYBE64(IMAGEHLP_SYMBOL)) + 255]; char symbol_buffer[sizeof(MAYBE64(IMAGEHLP_SYMBOL)) + 255];
char module_name_raw[MAX_PATH]; char module_name_raw[MAX_PATH];
struct bfd_ctx *bc = NULL; struct bfd_ctx *bc = NULL;
GetModuleFileNameA(NULL, procname, sizeof procname); GetModuleFileNameA(NULL, procname, sizeof procname);
memset(&frame,0,sizeof(frame)); memset(&frame,0,sizeof(frame));
#ifdef EBACKTRACE64 #ifdef EBACKTRACE64
frame.AddrPC.Offset = context->Rip; frame.AddrPC.Offset = context->Rip;
frame.AddrStack.Offset = context->Rsp; frame.AddrStack.Offset = context->Rsp;
frame.AddrFrame.Offset = context->Rbp; frame.AddrFrame.Offset = context->Rbp;
#else #else
frame.AddrPC.Offset = context->Eip; frame.AddrPC.Offset = context->Eip;
frame.AddrStack.Offset = context->Esp; frame.AddrStack.Offset = context->Esp;
frame.AddrFrame.Offset = context->Ebp; frame.AddrFrame.Offset = context->Ebp;
#endif #endif
frame.AddrPC.Mode = AddrModeFlat; frame.AddrPC.Mode = AddrModeFlat;
frame.AddrStack.Mode = AddrModeFlat; frame.AddrStack.Mode = AddrModeFlat;
frame.AddrFrame.Mode = AddrModeFlat; frame.AddrFrame.Mode = AddrModeFlat;
process = GetCurrentProcess(); process = GetCurrentProcess();
thread = GetCurrentThread(); thread = GetCurrentThread();
while(MAYBE64(StackWalk)(MachineType, while(MAYBE64(StackWalk)(MachineType,
process, process,
thread, thread,
&frame, &frame,
context, context,
NULL, NULL,
MAYBE64(SymFunctionTableAccess), MAYBE64(SymFunctionTableAccess),
MAYBE64(SymGetModuleBase), NULL)) { MAYBE64(SymGetModuleBase), NULL)) {
MAYBE64(IMAGEHLP_SYMBOL) *symbol; MAYBE64(IMAGEHLP_SYMBOL) *symbol;
MAYBE64(DWORD) module_base; MAYBE64(DWORD) module_base;
const char * module_name = "[unknown module]"; const char * module_name = "[unknown module]";
const char * file = NULL; const char * file = NULL;
const char * func = NULL; const char * func = NULL;
unsigned line = 0; unsigned line = 0;
--depth; --depth;
if (depth < 0) if (depth < 0)
break; break;
symbol = (MAYBE64(IMAGEHLP_SYMBOL) *)symbol_buffer; symbol = (MAYBE64(IMAGEHLP_SYMBOL) *)symbol_buffer;
symbol->SizeOfStruct = (sizeof *symbol) + 255; symbol->SizeOfStruct = (sizeof *symbol) + 255;
symbol->MaxNameLength = 254; symbol->MaxNameLength = 254;
module_base = MAYBE64(SymGetModuleBase)(process, frame.AddrPC.Offset); module_base = MAYBE64(SymGetModuleBase)(process, frame.AddrPC.Offset);
if (module_base && if (module_base &&
GetModuleFileNameA((HINSTANCE)(intptr_t)module_base, module_name_raw, MAX_PATH)) { GetModuleFileNameA((HINSTANCE)(intptr_t)module_base, module_name_raw, MAX_PATH)) {
module_name = module_name_raw; module_name = module_name_raw;
bc = get_bc(ob, set, module_name); bc = get_bc(ob, set, module_name);
} }
if (bc) { if (bc) {
find(bc,frame.AddrPC.Offset,&file,&func,&line); find(bc,frame.AddrPC.Offset,&file,&func,&line);
} }
if (file == NULL) { if (file == NULL) {
MAYBE64(DWORD) dummy = 0; MAYBE64(DWORD) dummy = 0;
if (MAYBE64(SymGetSymFromAddr)(process, frame.AddrPC.Offset, &dummy, symbol)) { if (MAYBE64(SymGetSymFromAddr)(process, frame.AddrPC.Offset, &dummy, symbol)) {
file = symbol->Name; file = symbol->Name;
} }
else { else {
file = "[unknown file]"; file = "[unknown file]";
} }
} }
output_print(ob,"0x%p : %s : %s", frame.AddrPC.Offset, module_name, file); output_print(ob,"0x%p : %s : %s", frame.AddrPC.Offset, module_name, file);
if (func != NULL) if (func != NULL)
output_print(ob, " (%d) : in function (%s)", line, func); output_print(ob, " (%d) : in function (%s)", line, func);
output_print(ob, "\n"); output_print(ob, "\n");
} }
} }
static LPTSTR FormatErrorMessage(DWORD dwMessageId) static LPTSTR FormatErrorMessage(DWORD dwMessageId)
@ -373,7 +373,7 @@ static PVOID g_prev = NULL;
static LONG WINAPI static LONG WINAPI
exception_filter(LPEXCEPTION_POINTERS info) exception_filter(LPEXCEPTION_POINTERS info)
{ {
struct output_buffer ob; struct output_buffer ob;
int logfd, written; int logfd, written;
PEXCEPTION_RECORD exception; PEXCEPTION_RECORD exception;
BOOL initialized = FALSE; BOOL initialized = FALSE;
@ -455,43 +455,43 @@ exception_filter(LPEXCEPTION_POINTERS info)
//fputs(g_output, stderr); //fputs(g_output, stderr);
exit(0xBAC); exit(0xBAC);
return EXCEPTION_CONTINUE_SEARCH; return EXCEPTION_CONTINUE_SEARCH;
} }
static void static void
backtrace_register(void) backtrace_register(void)
{ {
if (g_output == NULL) { if (g_output == NULL) {
g_output = malloc(BUFFER_MAX); g_output = malloc(BUFFER_MAX);
g_prev = AddVectoredExceptionHandler(1, exception_filter); g_prev = AddVectoredExceptionHandler(1, exception_filter);
} }
} }
static void static void
backtrace_unregister(void) backtrace_unregister(void)
{ {
if (g_output) { if (g_output) {
free(g_output); free(g_output);
RemoveVectoredExceptionHandler(g_prev); RemoveVectoredExceptionHandler(g_prev);
g_prev = NULL; g_prev = NULL;
g_output = NULL; g_output = NULL;
} }
} }
BOOL WINAPI BOOL WINAPI
DllMain(HINSTANCE hinstDLL ATTRIBUTE((unused)), DWORD dwReason, LPVOID lpvReserved ATTRIBUTE((unused))) DllMain(HINSTANCE hinstDLL ATTRIBUTE((unused)), DWORD dwReason, LPVOID lpvReserved ATTRIBUTE((unused)))
{ {
switch (dwReason) { switch (dwReason) {
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
backtrace_register(); backtrace_register();
break; break;
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH:
backtrace_unregister(); backtrace_unregister();
break; break;
} }
return TRUE; return TRUE;
} }