2020-01-18 05:58:39 +08:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
/* function to get a stack backtrace on demand with a message */
|
|
|
|
|
2020-04-15 10:13:05 +08:00
|
|
|
#include <stdarg.h>
|
2020-01-18 05:58:39 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2024-02-11 20:13:13 +08:00
|
|
|
#undef __attribute__
|
2020-01-18 05:58:39 +08:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
# define DLL_EXPORT __declspec(dllexport)
|
|
|
|
#else
|
|
|
|
# define DLL_EXPORT
|
|
|
|
#endif
|
|
|
|
|
2022-12-17 16:37:37 +08:00
|
|
|
/* Needed when using ...libtcc1-usegcc=yes in lib/Makefile */
|
|
|
|
#if (defined(__GNUC__) && (__GNUC__ >= 6)) || defined(__clang__)
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wframe-address"
|
|
|
|
#endif
|
|
|
|
|
2024-02-11 20:13:13 +08:00
|
|
|
typedef struct rt_frame
|
|
|
|
{
|
|
|
|
void *ip, *fp, *sp;
|
|
|
|
} rt_frame;
|
|
|
|
|
|
|
|
__attribute__((weak))
|
|
|
|
int _rt_error(rt_frame *f, const char *msg, const char *fmt, va_list ap);
|
|
|
|
|
2020-01-18 05:58:39 +08:00
|
|
|
DLL_EXPORT int tcc_backtrace(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
2024-02-11 20:13:13 +08:00
|
|
|
if (_rt_error) {
|
|
|
|
rt_frame f;
|
|
|
|
f.fp = __builtin_frame_address(1);
|
|
|
|
f.ip = __builtin_return_address(0);
|
2020-01-18 05:58:39 +08:00
|
|
|
va_start(ap, fmt);
|
2024-02-11 20:13:13 +08:00
|
|
|
ret = _rt_error(&f, "", fmt, ap);
|
2020-01-18 05:58:39 +08:00
|
|
|
va_end(ap);
|
|
|
|
} else {
|
2023-08-31 03:20:13 +08:00
|
|
|
const char *p, *nl = "\n";
|
2020-01-18 05:58:39 +08:00
|
|
|
if (fmt[0] == '^' && (p = strchr(fmt + 1, fmt[0])))
|
|
|
|
fmt = p + 1;
|
2023-08-31 03:20:13 +08:00
|
|
|
if (fmt[0] == '\001')
|
|
|
|
++fmt, nl = "";
|
2020-01-18 05:58:39 +08:00
|
|
|
va_start(ap, fmt);
|
|
|
|
ret = vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
2024-01-17 14:07:48 +08:00
|
|
|
fprintf(stderr, "%s", nl), fflush(stderr);
|
2020-01-18 05:58:39 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2022-12-17 16:37:37 +08:00
|
|
|
|
|
|
|
#if (defined(__GNUC__) && (__GNUC__ >= 6)) || defined(__clang__)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|