Skip to content

Commit 509f208

Browse files
kpamnanyd-netto
authored andcommitted
RAI: Prepend signal number and thread ID on backtraces
Prepend `[signal (X) ]thread (Y) ` to each backtrace line that is displayed. Co-authored-by: Diogo Netto <61364108+d-netto@users.noreply.github.com>
1 parent 8837037 commit 509f208

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

src/julia_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,8 +1162,8 @@ JL_DLLEXPORT jl_value_t *jl_get_backtrace(void);
11621162
void jl_critical_error(int sig, int si_code, bt_context_t *context, jl_task_t *ct);
11631163
JL_DLLEXPORT void jl_raise_debugger(void) JL_NOTSAFEPOINT;
11641164
JL_DLLEXPORT void jl_gdblookup(void* ip) JL_NOTSAFEPOINT;
1165-
void jl_print_native_codeloc(uintptr_t ip) JL_NOTSAFEPOINT;
1166-
void jl_print_bt_entry_codeloc(jl_bt_element_t *bt_data) JL_NOTSAFEPOINT;
1165+
void jl_print_native_codeloc(char *pre_str, uintptr_t ip) JL_NOTSAFEPOINT;
1166+
void jl_print_bt_entry_codeloc(int sig, jl_bt_element_t *bt_data) JL_NOTSAFEPOINT;
11671167
#ifdef _OS_WINDOWS_
11681168
JL_DLLEXPORT void jl_refresh_dbg_module_list(void);
11691169
#endif

src/signal-handling.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ void jl_critical_error(int sig, int si_code, bt_context_t *context, jl_task_t *c
475475
*bt_size = n = rec_backtrace_ctx(bt_data, JL_MAX_BT_SIZE, context, NULL);
476476
}
477477
for (i = 0; i < n; i += jl_bt_entry_size(bt_data + i)) {
478-
jl_print_bt_entry_codeloc(bt_data + i);
478+
jl_print_bt_entry_codeloc(sig, bt_data + i);
479479
}
480480
jl_gc_debug_print_status();
481481
jl_gc_debug_critical_error();

src/signals-unix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ static void *signal_listener(void *arg)
956956
jl_safe_printf("\nsignal (%d): %s\n", sig, strsignal(sig));
957957
size_t i;
958958
for (i = 0; i < bt_size; i += jl_bt_entry_size(bt_data + i)) {
959-
jl_print_bt_entry_codeloc(bt_data + i);
959+
jl_print_bt_entry_codeloc(-1, bt_data + i);
960960
}
961961
}
962962
}

src/signals-win.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ LONG WINAPI jl_exception_handler(struct _EXCEPTION_POINTERS *ExceptionInfo)
327327
jl_safe_printf("UNKNOWN"); break;
328328
}
329329
jl_safe_printf(" at 0x%Ix -- ", (size_t)ExceptionInfo->ExceptionRecord->ExceptionAddress);
330-
jl_print_native_codeloc((uintptr_t)ExceptionInfo->ExceptionRecord->ExceptionAddress);
330+
jl_print_native_codeloc("", (uintptr_t)ExceptionInfo->ExceptionRecord->ExceptionAddress);
331331

332332
jl_critical_error(0, 0, ExceptionInfo->ContextRecord, ct);
333333
static int recursion = 0;

src/stackwalk.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -614,22 +614,25 @@ JL_DLLEXPORT jl_value_t *jl_lookup_code_address(void *ip, int skipC)
614614
return rs;
615615
}
616616

617-
static void jl_safe_print_codeloc(const char* func_name, const char* file_name,
617+
static void jl_safe_print_codeloc(const char *pre_str,
618+
const char* func_name, const char* file_name,
618619
int line, int inlined) JL_NOTSAFEPOINT
619620
{
620621
const char *inlined_str = inlined ? " [inlined]" : "";
621622
if (line != -1) {
622-
jl_safe_printf("%s at %s:%d%s\n", func_name, file_name, line, inlined_str);
623+
jl_safe_printf("%s%s at %s:%d%s\n",
624+
pre_str, func_name, file_name, line, inlined_str);
623625
}
624626
else {
625-
jl_safe_printf("%s at %s (unknown line)%s\n", func_name, file_name, inlined_str);
627+
jl_safe_printf("%s%s at %s (unknown line)%s\n",
628+
pre_str, func_name, file_name, inlined_str);
626629
}
627630
}
628631

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

655659
// Print code location for backtrace buffer entry at *bt_entry
656-
void jl_print_bt_entry_codeloc(jl_bt_element_t *bt_entry) JL_NOTSAFEPOINT
660+
void jl_print_bt_entry_codeloc(int sig, jl_bt_element_t *bt_entry) JL_NOTSAFEPOINT
657661
{
662+
char sig_str[32], pre_str[64];
663+
sig_str[0] = '\0';
664+
if (sig != -1) {
665+
snprintf(sig_str, 32, "signal (%d) ", sig);
666+
}
667+
snprintf(pre_str, 64, "%sthread (%d) ", sig_str, jl_threadid() + 1);
668+
658669
if (jl_bt_is_native(bt_entry)) {
659-
jl_print_native_codeloc(bt_entry[0].uintptr);
670+
jl_print_native_codeloc(pre_str, bt_entry[0].uintptr);
660671
}
661672
else if (jl_bt_entry_tag(bt_entry) == JL_BT_INTERP_FRAME_TAG) {
662673
size_t ip = jl_bt_entry_header(bt_entry);
@@ -682,7 +693,7 @@ void jl_print_bt_entry_codeloc(jl_bt_element_t *bt_entry) JL_NOTSAFEPOINT
682693
method = (jl_value_t*)((jl_method_t*)method)->name;
683694
if (jl_is_symbol(method))
684695
func_name = jl_symbol_name((jl_sym_t*)method);
685-
jl_safe_print_codeloc(func_name, jl_symbol_name(locinfo->file),
696+
jl_safe_print_codeloc(pre_str, func_name, jl_symbol_name(locinfo->file),
686697
locinfo->line, locinfo->inlined_at);
687698
debuginfoloc = locinfo->inlined_at;
688699
}
@@ -1108,7 +1119,9 @@ static void jl_rec_backtrace(jl_task_t *t) JL_NOTSAFEPOINT
11081119

11091120
JL_DLLEXPORT void jl_gdblookup(void* ip)
11101121
{
1111-
jl_print_native_codeloc((uintptr_t)ip);
1122+
char pre_str[64];
1123+
snprintf(pre_str, 64, "thread (%d) ", jl_threadid() + 1);
1124+
jl_print_native_codeloc(pre_str, (uintptr_t)ip);
11121125
}
11131126

11141127
// Print backtrace for current exception in catch block
@@ -1123,7 +1136,7 @@ JL_DLLEXPORT void jlbacktrace(void) JL_NOTSAFEPOINT
11231136
size_t i, bt_size = jl_excstack_bt_size(s, s->top);
11241137
jl_bt_element_t *bt_data = jl_excstack_bt_data(s, s->top);
11251138
for (i = 0; i < bt_size; i += jl_bt_entry_size(bt_data + i)) {
1126-
jl_print_bt_entry_codeloc(bt_data + i);
1139+
jl_print_bt_entry_codeloc(-1, bt_data + i);
11271140
}
11281141
}
11291142

@@ -1136,7 +1149,7 @@ JL_DLLEXPORT void jlbacktracet(jl_task_t *t) JL_NOTSAFEPOINT
11361149
size_t i, bt_size = ptls->bt_size;
11371150
jl_bt_element_t *bt_data = ptls->bt_data;
11381151
for (i = 0; i < bt_size; i += jl_bt_entry_size(bt_data + i)) {
1139-
jl_print_bt_entry_codeloc(bt_data + i);
1152+
jl_print_bt_entry_codeloc(-1, bt_data + i);
11401153
}
11411154
}
11421155

0 commit comments

Comments
 (0)