Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hlc stack trace to Linux/Mac #634

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/hlc_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ extern void sys_global_exit();
# define _CrtCheckMemory()
#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 @@ -80,6 +84,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
return NULL;
}
Expand All @@ -88,8 +104,12 @@ 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
if( count < 0 ) count = 0;
return count;
}

Expand Down