[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
1 changed files with 194 additions and 194 deletions

View File

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