Skip to content

Commit

Permalink
RAI: Prepend signal number and thread ID on backtraces
Browse files Browse the repository at this point in the history
Prepend `[signal (X) ]thread (Y) ` to each backtrace line that is
displayed.

Co-authored-by: Diogo Netto <61364108+d-netto@users.noreply.github.com>
  • Loading branch information
2 people authored and RAI CI (GitHub Action Automation) committed Jan 25, 2024
1 parent b078b9b commit 6100f9b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1163,8 +1163,8 @@ JL_DLLEXPORT jl_value_t *jl_get_backtrace(void);
void jl_critical_error(int sig, int si_code, bt_context_t *context, jl_task_t *ct);
JL_DLLEXPORT void jl_raise_debugger(void) JL_NOTSAFEPOINT;
JL_DLLEXPORT void jl_gdblookup(void* ip) JL_NOTSAFEPOINT;
void jl_print_native_codeloc(uintptr_t ip) JL_NOTSAFEPOINT;
void jl_print_bt_entry_codeloc(jl_bt_element_t *bt_data) JL_NOTSAFEPOINT;
void jl_print_native_codeloc(char *pre_str, uintptr_t ip) JL_NOTSAFEPOINT;
void jl_print_bt_entry_codeloc(int sig, jl_bt_element_t *bt_data) JL_NOTSAFEPOINT;
#ifdef _OS_WINDOWS_
JL_DLLEXPORT void jl_refresh_dbg_module_list(void);
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/signal-handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ void jl_critical_error(int sig, int si_code, bt_context_t *context, jl_task_t *c
*bt_size = n = rec_backtrace_ctx(bt_data, JL_MAX_BT_SIZE, context, NULL);
}
for (i = 0; i < n; i += jl_bt_entry_size(bt_data + i)) {
jl_print_bt_entry_codeloc(bt_data + i);
jl_print_bt_entry_codeloc(sig, bt_data + i);
}
jl_gc_debug_print_status();
jl_gc_debug_critical_error();
Expand Down
2 changes: 1 addition & 1 deletion src/signals-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ static void *signal_listener(void *arg)
jl_safe_printf("\nsignal (%d): %s\n", sig, strsignal(sig));
size_t i;
for (i = 0; i < bt_size; i += jl_bt_entry_size(bt_data + i)) {
jl_print_bt_entry_codeloc(bt_data + i);
jl_print_bt_entry_codeloc(-1, bt_data + i);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/signals-win.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ LONG WINAPI jl_exception_handler(struct _EXCEPTION_POINTERS *ExceptionInfo)
jl_safe_printf("UNKNOWN"); break;
}
jl_safe_printf(" at 0x%Ix -- ", (size_t)ExceptionInfo->ExceptionRecord->ExceptionAddress);
jl_print_native_codeloc((uintptr_t)ExceptionInfo->ExceptionRecord->ExceptionAddress);
jl_print_native_codeloc("", (uintptr_t)ExceptionInfo->ExceptionRecord->ExceptionAddress);

jl_critical_error(0, 0, ExceptionInfo->ContextRecord, ct);
static int recursion = 0;
Expand Down
37 changes: 25 additions & 12 deletions src/stackwalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,22 +614,25 @@ JL_DLLEXPORT jl_value_t *jl_lookup_code_address(void *ip, int skipC)
return rs;
}

static void jl_safe_print_codeloc(const char* func_name, const char* file_name,
static void jl_safe_print_codeloc(const char *pre_str,
const char* func_name, const char* file_name,
int line, int inlined) JL_NOTSAFEPOINT
{
const char *inlined_str = inlined ? " [inlined]" : "";
if (line != -1) {
jl_safe_printf("%s at %s:%d%s\n", func_name, file_name, line, inlined_str);
jl_safe_printf("%s%s at %s:%d%s\n",
pre_str, func_name, file_name, line, inlined_str);
}
else {
jl_safe_printf("%s at %s (unknown line)%s\n", func_name, file_name, inlined_str);
jl_safe_printf("%s%s at %s (unknown line)%s\n",
pre_str, func_name, file_name, inlined_str);
}
}

// Print function, file and line containing native instruction pointer `ip` by
// looking up debug info. Prints multiple such frames when `ip` points to
// inlined code.
void jl_print_native_codeloc(uintptr_t ip) JL_NOTSAFEPOINT
void jl_print_native_codeloc(char *pre_str, uintptr_t ip) JL_NOTSAFEPOINT
{
// This function is not allowed to reference any TLS variables since
// it can be called from an unmanaged thread on OSX.
Expand All @@ -641,10 +644,11 @@ void jl_print_native_codeloc(uintptr_t ip) JL_NOTSAFEPOINT
for (i = 0; i < n; i++) {
jl_frame_t frame = frames[i];
if (!frame.func_name) {
jl_safe_printf("unknown function (ip: %p)\n", (void*)ip);
jl_safe_printf("%sunknown function (ip: %p)\n", pre_str, (void*)ip);
}
else {
jl_safe_print_codeloc(frame.func_name, frame.file_name, frame.line, frame.inlined);
jl_safe_print_codeloc(pre_str, frame.func_name,
frame.file_name, frame.line, frame.inlined);
free(frame.func_name);
free(frame.file_name);
}
Expand All @@ -653,10 +657,17 @@ void jl_print_native_codeloc(uintptr_t ip) JL_NOTSAFEPOINT
}

// Print code location for backtrace buffer entry at *bt_entry
void jl_print_bt_entry_codeloc(jl_bt_element_t *bt_entry) JL_NOTSAFEPOINT
void jl_print_bt_entry_codeloc(int sig, jl_bt_element_t *bt_entry) JL_NOTSAFEPOINT
{
char sig_str[32], pre_str[64];
sig_str[0] = '\0';
if (sig != -1) {
snprintf(sig_str, 32, "signal (%d) ", sig);
}
snprintf(pre_str, 64, "%sthread (%d) ", sig_str, jl_threadid() + 1);

if (jl_bt_is_native(bt_entry)) {
jl_print_native_codeloc(bt_entry[0].uintptr);
jl_print_native_codeloc(pre_str, bt_entry[0].uintptr);
}
else if (jl_bt_entry_tag(bt_entry) == JL_BT_INTERP_FRAME_TAG) {
size_t ip = jl_bt_entry_header(bt_entry);
Expand All @@ -682,7 +693,7 @@ void jl_print_bt_entry_codeloc(jl_bt_element_t *bt_entry) JL_NOTSAFEPOINT
method = (jl_value_t*)((jl_method_t*)method)->name;
if (jl_is_symbol(method))
func_name = jl_symbol_name((jl_sym_t*)method);
jl_safe_print_codeloc(func_name, jl_symbol_name(locinfo->file),
jl_safe_print_codeloc(pre_str, func_name, jl_symbol_name(locinfo->file),
locinfo->line, locinfo->inlined_at);
debuginfoloc = locinfo->inlined_at;
}
Expand Down Expand Up @@ -1108,7 +1119,9 @@ static void jl_rec_backtrace(jl_task_t *t) JL_NOTSAFEPOINT

JL_DLLEXPORT void jl_gdblookup(void* ip)
{
jl_print_native_codeloc((uintptr_t)ip);
char pre_str[64];
snprintf(pre_str, 64, "thread (%d) ", jl_threadid() + 1);
jl_print_native_codeloc(pre_str, (uintptr_t)ip);
}

// Print backtrace for current exception in catch block
Expand All @@ -1123,7 +1136,7 @@ JL_DLLEXPORT void jlbacktrace(void) JL_NOTSAFEPOINT
size_t i, bt_size = jl_excstack_bt_size(s, s->top);
jl_bt_element_t *bt_data = jl_excstack_bt_data(s, s->top);
for (i = 0; i < bt_size; i += jl_bt_entry_size(bt_data + i)) {
jl_print_bt_entry_codeloc(bt_data + i);
jl_print_bt_entry_codeloc(-1, bt_data + i);
}
}

Expand All @@ -1136,7 +1149,7 @@ JL_DLLEXPORT void jlbacktracet(jl_task_t *t) JL_NOTSAFEPOINT
size_t i, bt_size = ptls->bt_size;
jl_bt_element_t *bt_data = ptls->bt_data;
for (i = 0; i < bt_size; i += jl_bt_entry_size(bt_data + i)) {
jl_print_bt_entry_codeloc(bt_data + i);
jl_print_bt_entry_codeloc(-1, bt_data + i);
}
}

Expand Down

0 comments on commit 6100f9b

Please sign in to comment.