mirror of
https://github.com/mirror/tinycc.git
synced 2024-12-26 03:50:07 +08:00
ef42295fe8
This makes it possible to get backtraces with executables (including DLLs/SOs) like we had it already with -g -run. Option -b includes -bt, and -bt includes -g. - new file lib/bt-exe.c: used to link rt_printline and the exception handler from tccrun.c into executables/DLLs. - new file lib/bt-log.c: provides a function that may be called from user code to print out a backtrace with a message (currently for i386/x86_64 only): int (*tcc_backtrace)(const char *fmt, ...); As an extra hack, if 'fmt' is prefixed like "^file.c^..." then the backtrace will skip calls from within 'file.c'. - new file lib/bt-dll.c: used on win32 to link the backtrace and bcheck functions with the main module at runtime - bcheck.c: now uses the tcc_backtrace function from above - tccgen.c: minor cleanups - tccelf.c: stab sections get SHF_ALLOC for easy access. Also in relocate_section(): 64bit relocations for stabs in DLLs cannot work. To find DLL addresses, the DLL base is added manually in tccrun.c via rc.prog_base instead. - tccpe.c: there are some changes to allow merging sections, used to merge .finit_array into .data in the first place. - tccpp.c: tcc -run now #defines __TCC_RUN__ also: refactor a line in tal_realloc that was incompatible with bcheck - tcctest.c: fixed a problem with r12 which tcc cannot preserve as well as gcc does. - tests2/112_backtrace.c: test the feature and the bcheck test18 that previously was in boundtest.c
37 lines
891 B
C
37 lines
891 B
C
/* ------------------------------------------------------------- */
|
|
/* function to get a stack backtrace on demand with a message */
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
int (*__rt_error)(void*, void*, const char *, va_list);
|
|
|
|
#ifdef _WIN32
|
|
# define DLL_EXPORT __declspec(dllexport)
|
|
#else
|
|
# define DLL_EXPORT
|
|
#endif
|
|
|
|
DLL_EXPORT int tcc_backtrace(const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
int ret;
|
|
|
|
if (__rt_error) {
|
|
void *fp = __builtin_frame_address(1);
|
|
void *ip = __builtin_return_address(0);
|
|
va_start(ap, fmt);
|
|
ret = __rt_error(fp, ip, fmt, ap);
|
|
va_end(ap);
|
|
} else {
|
|
const char *p;
|
|
if (fmt[0] == '^' && (p = strchr(fmt + 1, fmt[0])))
|
|
fmt = p + 1;
|
|
va_start(ap, fmt);
|
|
ret = vfprintf(stderr, fmt, ap);
|
|
va_end(ap);
|
|
fprintf(stderr, "\n"), fflush(stderr);
|
|
}
|
|
return ret;
|
|
}
|