fixed the stack backtrace function

This commit is contained in:
hyung-hwan 2018-04-26 06:35:43 +00:00
parent 21ff19ed40
commit 71da060d29

View File

@ -27,6 +27,7 @@
#include "hcl-prv.h" #include "hcl-prv.h"
#if defined(HCL_ENABLE_LIBUNWIND) #if defined(HCL_ENABLE_LIBUNWIND)
# define UNW_LOCAL_ONLY
# include <libunwind.h> # include <libunwind.h>
#endif #endif
@ -484,28 +485,27 @@ static void backtrace_stack_frames (hcl_t* hcl)
{ {
unw_cursor_t cursor; unw_cursor_t cursor;
unw_context_t context; unw_context_t context;
int n;
unw_getcontext(&context); unw_getcontext(&context);
unw_init_local(&cursor, &context); unw_init_local(&cursor, &context);
int n=0; for (n = 0; unw_step(&cursor) > 0; n++)
while (unw_step(&cursor))
{ {
unw_word_t ip, sp, off; unw_word_t ip, sp, off;
char symbol[256];
unw_get_reg (&cursor, UNW_REG_IP, &ip); unw_get_reg (&cursor, UNW_REG_IP, &ip);
unw_get_reg (&cursor, UNW_REG_SP, &sp); unw_get_reg (&cursor, UNW_REG_SP, &sp);
char symbol[256]; if (unw_get_proc_name(&cursor, symbol, HCL_COUNTOF(symbol), &off))
if (!unw_get_proc_name(&cursor, symbol, HCL_COUNTOF(symbol), &off))
{ {
hcl_copy_bcstr (symbol, "<unknown>"); hcl_copy_bcstr (symbol, HCL_COUNTOF(symbol), "<unknown>");
} }
hcl_logbfmt (hcl, HCL_LOG_UNTYPED | HCL_LOG_DEBUG, hcl_logbfmt (hcl, HCL_LOG_UNTYPED | HCL_LOG_DEBUG,
"#%-2d 0x%016p p=0x%016p %s + 0x%zu\n", "#%02d ip=0x%*p sp=0x%*p %s+0x%zu\n",
++n, (void*)ip, (void*)sp, symbol, (hcl_oow_t)off); n, HCL_SIZEOF(void*) * 2, (void*)ip, HCL_SIZEOF(void*) * 2, (void*)sp, symbol, (hcl_oow_t)off);
} }
} }
#elif defined(HAVE_BACKTRACE) #elif defined(HAVE_BACKTRACE)