Skip to content

Commit

Permalink
Add hlc stack trace to Linux/Mac (HaxeFoundation#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxiaomao authored and crazyjul committed Jan 10, 2024
1 parent 4db2d81 commit 7c4783b
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/hlc_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,17 @@ extern void sys_global_exit();
# define _CrtCheckMemory()
#endif


//Used to get callstack on Apple devices (android ?)
#ifdef HL_MOBILE
HL_API int util_callstack_adresses(int size, void** adresses);
HL_API const char *util_address_to_name(void* adress);
#endif

#if defined(HL_LINUX) || defined(HL_MAC)
# include <execinfo.h>
#endif

static uchar *hlc_resolve_symbol( void *addr, uchar *out, int *outSize ) {
#ifdef HL_WIN_DESKTOP
static HANDLE stack_process_handle = NULL;
Expand All @@ -86,6 +91,18 @@ static uchar *hlc_resolve_symbol( void *addr, uchar *out, int *outSize ) {
*outSize = usprintf(out,*outSize,USTR("%s(%s:%d)"),data.sym.Name,wcsrchr(line.FileName,'\\')+1,(int)line.LineNumber);
return out;
}
#elif defined(HL_LINUX) || defined(HL_MAC)
void *array[1];
char **strings;
array[0] = addr;
strings = backtrace_symbols(array, 1);
if (strings != NULL) {
*outSize = (int)strlen(strings[0]) << 1;
out = (uchar*)hl_gc_alloc_noptr(*outSize);
hl_from_utf8(out,*outSize,strings[0]);
free(strings);
return out;
}
#endif
#ifdef HL_MOBILE
uchar *str = hl_to_utf16(util_address_to_name(addr));
Expand All @@ -99,12 +116,19 @@ static int hlc_capture_stack( void **stack, int size ) {
int count = 0;
# ifdef HL_WIN_DESKTOP
count = CaptureStackBackTrace(2, size, stack, NULL) - 8; // 8 startup
if( count < 0 ) count = 0;
# elif defined(HL_LINUX)
count = backtrace(stack, size) - 8;
# elif defined(HL_MAC)
count = backtrace(stack, size) - 6;
# endif
<<<<<<< HEAD
#ifdef HL_MOBILE
count = util_callstack_adresses(size, stack) - 8; // 8 startup
if( count < 0 ) count = 0;
#endif
=======
if( count < 0 ) count = 0;
>>>>>>> a21544c0 (Add hlc stack trace to Linux/Mac (#634))
return count;
}

Expand Down

0 comments on commit 7c4783b

Please sign in to comment.