commit 6eb7c01ce5308bdcd7149f02da026d1ecda08b7a parent 145f518ec3b5e5433ad9b220d3ee3bdc3f2723c6 Author: Michael Savage <mikejsavage@gmail.com> Date: Fri May 5 00:21:11 +0300 Re-add platform_backtrace.h. Windows untested Diffstat:
intrinsics.h | | | 2 | ++ |
platform_backtrace.h | | | 11 | +++++++++++ |
unix_backtrace.h | | | 11 | +++++++++++ |
win32_backtrace.h | | | 35 | +++++++++++++++++++++++++++++++++++ |
diff --git a/intrinsics.h b/intrinsics.h @@ -7,6 +7,7 @@ #include <errno.h> #include "platform.h" +#include "platform_backtrace.h" typedef int8_t s8; typedef int16_t s16; @@ -105,6 +106,7 @@ inline void assert_impl( const bool predicate, const char * message ) { puts( message ); int err = errno; printf( "errno(%d): %s\n", err, strerror( err ) ); + print_backtrace(); abort(); } } diff --git a/platform_backtrace.h b/platform_backtrace.h @@ -0,0 +1,11 @@ +#pragma once + +#include "platform.h" + +#if PLATFORM_WINDOWS +#include "win32_backtrace.h" +#elif PLATFORM_UNIX +#include "unix_backtrace.h" +#else +#error new platform +#endif diff --git a/unix_backtrace.h b/unix_backtrace.h @@ -0,0 +1,11 @@ +#pragma once + +#include <stdio.h> +#include <unistd.h> +#include <execinfo.h> + +inline void print_backtrace() { + void * stack[ 128 ]; + const int stack_size = backtrace( stack, 128 ); + backtrace_symbols_fd( stack, stack_size, STDERR_FILENO ); +} diff --git a/win32_backtrace.h b/win32_backtrace.h @@ -0,0 +1,35 @@ +#pragma once + +#include <windows.h> +#include <dbghelp.h> + +inline void print_backtrace() { + int max_symbol_len = 1024; + + HANDLE process = GetCurrentProcess(); + SymInitialize( process, NULL, TRUE ); + + void * stack[ 128 ]; + WORD num_frames = CaptureStackBackTrace( 0, 128, stack, NULL ); + + for( WORD i = 0; i < num_frames; i++ ) { + char symbol_mem[ sizeof( SYMBOL_INFO ) + max_symbol_len * sizeof( TCHAR ) ]; + SYMBOL_INFO * symbol = ( SYMBOL_INFO * ) symbol_mem; + symbol->MaxNameLen = max_symbol_len; + symbol->SizeOfStruct = sizeof( SYMBOL_INFO ); + + DWORD64 addr64 = DWORD64( stack[ i ] ); + SymFromAddr( process, addr64, NULL, symbol ); + + IMAGEHLP_LINE64 line; + line.SizeOfStruct = sizeof( line ); + BOOL has_file_info = SymGetLineFromAddr64( process, addr64, &col, &line ); + + if( has_file_info ) { + printf( "%s (%s:%d - 0x%l64x)\n", symbol->Name, line->FileName, line->LineNumber, symbol->Address ); + } + else { + printf( "%s (0x%l64x)\n", symbol->Name, symbol->Address ); + } + } +}