Skip to content

Commit 0c80e1e

Browse files
kpamnanyd-netto
authored and
RAI CI (GitHub Action Automation)
committed
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 8b15dff commit 0c80e1e

File tree

6 files changed

+35
-22
lines changed

6 files changed

+35
-22
lines changed

src/julia_internal.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1509,8 +1509,8 @@ JL_DLLEXPORT jl_value_t *jl_get_backtrace(void);
15091509
void jl_critical_error(int sig, int si_code, bt_context_t *context, jl_task_t *ct);
15101510
JL_DLLEXPORT void jl_raise_debugger(void) JL_NOTSAFEPOINT;
15111511
JL_DLLEXPORT void jl_gdblookup(void* ip) JL_NOTSAFEPOINT;
1512-
void jl_print_native_codeloc(uintptr_t ip) JL_NOTSAFEPOINT;
1513-
void jl_print_bt_entry_codeloc(jl_bt_element_t *bt_data) JL_NOTSAFEPOINT;
1512+
void jl_print_native_codeloc(char *pre_str, uintptr_t ip) JL_NOTSAFEPOINT;
1513+
void jl_print_bt_entry_codeloc(int sig, jl_bt_element_t *bt_data) JL_NOTSAFEPOINT;
15141514
#ifdef _OS_WINDOWS_
15151515
JL_DLLEXPORT void jl_refresh_dbg_module_list(void);
15161516
#endif

src/safepoint.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void jl_gc_wait_for_the_world(jl_ptls_t* gc_all_tls_states, int gc_n_threads)
172172
size_t bt_size = jl_try_record_thread_backtrace(ptls2, ptls->bt_data, JL_MAX_BT_SIZE);
173173
// Print the backtrace of the straggler
174174
for (size_t i = 0; i < bt_size; i += jl_bt_entry_size(ptls->bt_data + i)) {
175-
jl_print_bt_entry_codeloc(ptls->bt_data + i);
175+
jl_print_bt_entry_codeloc(-1, ptls->bt_data + i);
176176
}
177177
}
178178
}

src/signal-handling.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ void jl_critical_error(int sig, int si_code, bt_context_t *context, jl_task_t *c
615615
*bt_size = n = rec_backtrace_ctx(bt_data, JL_MAX_BT_SIZE, context, NULL);
616616
}
617617
for (i = 0; i < n; i += jl_bt_entry_size(bt_data + i)) {
618-
jl_print_bt_entry_codeloc(bt_data + i);
618+
jl_print_bt_entry_codeloc(sig, bt_data + i);
619619
}
620620
jl_gc_debug_print_status();
621621
jl_gc_debug_critical_error();

src/signals-unix.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ static void *signal_listener(void *arg)
11071107
jl_safe_printf("\nsignal (%d): %s\n", sig, strsignal(sig));
11081108
size_t i;
11091109
for (i = 0; i < signal_bt_size; i += jl_bt_entry_size(signal_bt_data + i)) {
1110-
jl_print_bt_entry_codeloc(signal_bt_data + i);
1110+
jl_print_bt_entry_codeloc(-1, signal_bt_data + i);
11111111
}
11121112
}
11131113
}

src/signals-win.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ LONG WINAPI jl_exception_handler(struct _EXCEPTION_POINTERS *ExceptionInfo)
333333
jl_safe_printf("UNKNOWN"); break;
334334
}
335335
jl_safe_printf(" at 0x%zx -- ", (size_t)ExceptionInfo->ExceptionRecord->ExceptionAddress);
336-
jl_print_native_codeloc((uintptr_t)ExceptionInfo->ExceptionRecord->ExceptionAddress);
336+
jl_print_native_codeloc("", (uintptr_t)ExceptionInfo->ExceptionRecord->ExceptionAddress);
337337

338338
jl_critical_error(0, 0, ExceptionInfo->ContextRecord, ct);
339339
static int recursion = 0;

src/stackwalk.c

+29-16
Original file line numberDiff line numberDiff line change
@@ -637,22 +637,25 @@ JL_DLLEXPORT jl_value_t *jl_lookup_code_address(void *ip, int skipC)
637637
return rs;
638638
}
639639

640-
static void jl_safe_print_codeloc(const char* func_name, const char* file_name,
640+
static void jl_safe_print_codeloc(const char *pre_str,
641+
const char* func_name, const char* file_name,
641642
int line, int inlined) JL_NOTSAFEPOINT
642643
{
643644
const char *inlined_str = inlined ? " [inlined]" : "";
644645
if (line != -1) {
645-
jl_safe_printf("%s at %s:%d%s\n", func_name, file_name, line, inlined_str);
646+
jl_safe_printf("%s%s at %s:%d%s\n",
647+
pre_str, func_name, file_name, line, inlined_str);
646648
}
647649
else {
648-
jl_safe_printf("%s at %s (unknown line)%s\n", func_name, file_name, inlined_str);
650+
jl_safe_printf("%s%s at %s (unknown line)%s\n",
651+
pre_str, func_name, file_name, inlined_str);
649652
}
650653
}
651654

652655
// Print function, file and line containing native instruction pointer `ip` by
653656
// looking up debug info. Prints multiple such frames when `ip` points to
654657
// inlined code.
655-
void jl_print_native_codeloc(uintptr_t ip) JL_NOTSAFEPOINT
658+
void jl_print_native_codeloc(char *pre_str, uintptr_t ip) JL_NOTSAFEPOINT
656659
{
657660
// This function is not allowed to reference any TLS variables since
658661
// it can be called from an unmanaged thread on OSX.
@@ -664,10 +667,11 @@ void jl_print_native_codeloc(uintptr_t ip) JL_NOTSAFEPOINT
664667
for (i = 0; i < n; i++) {
665668
jl_frame_t frame = frames[i];
666669
if (!frame.func_name) {
667-
jl_safe_printf("unknown function (ip: %p) at %s\n", (void*)ip, frame.file_name ? frame.file_name : "(unknown file)");
670+
jl_safe_printf("%sunknown function (ip: %p) at %s\n", pre_str, (void*)ip, frame.file_name ? frame.file_name : "(unknown file)");
668671
}
669672
else {
670-
jl_safe_print_codeloc(frame.func_name, frame.file_name, frame.line, frame.inlined);
673+
jl_safe_print_codeloc(pre_str, frame.func_name,
674+
frame.file_name, frame.line, frame.inlined);
671675
free(frame.func_name);
672676
}
673677
free(frame.file_name);
@@ -725,7 +729,7 @@ const char *jl_debuginfo_name(jl_value_t *func)
725729

726730
// func == module : top-level
727731
// func == NULL : macro expansion
728-
static void jl_print_debugloc(jl_debuginfo_t *debuginfo, jl_value_t *func, size_t ip, int inlined) JL_NOTSAFEPOINT
732+
static void jl_print_debugloc(const char *pre_str, jl_debuginfo_t *debuginfo, jl_value_t *func, size_t ip, int inlined) JL_NOTSAFEPOINT
729733
{
730734
if (!jl_is_symbol(debuginfo->def)) // this is a path or
731735
func = debuginfo->def; // this is inlined code
@@ -734,26 +738,33 @@ static void jl_print_debugloc(jl_debuginfo_t *debuginfo, jl_value_t *func, size_
734738
if (edges_idx) {
735739
jl_debuginfo_t *edge = (jl_debuginfo_t*)jl_svecref(debuginfo->edges, edges_idx - 1);
736740
assert(jl_typetagis(edge, jl_debuginfo_type));
737-
jl_print_debugloc(edge, NULL, stmt.pc, 1);
741+
jl_print_debugloc(pre_str, edge, NULL, stmt.pc, 1);
738742
}
739743
intptr_t ip2 = stmt.line;
740744
if (ip2 >= 0 && ip > 0 && (jl_value_t*)debuginfo->linetable != jl_nothing) {
741-
jl_print_debugloc(debuginfo->linetable, func, ip2, 0);
745+
jl_print_debugloc(pre_str, debuginfo->linetable, func, ip2, 0);
742746
}
743747
else {
744748
if (ip2 < 0) // set broken debug info to ignored
745749
ip2 = 0;
746750
const char *func_name = jl_debuginfo_name(func);
747751
const char *file = jl_debuginfo_file(debuginfo);
748-
jl_safe_print_codeloc(func_name, file, ip2, inlined);
752+
jl_safe_print_codeloc(pre_str, func_name, file, ip2, inlined);
749753
}
750754
}
751755

752756
// Print code location for backtrace buffer entry at *bt_entry
753-
void jl_print_bt_entry_codeloc(jl_bt_element_t *bt_entry) JL_NOTSAFEPOINT
757+
void jl_print_bt_entry_codeloc(int sig, jl_bt_element_t *bt_entry) JL_NOTSAFEPOINT
754758
{
759+
char sig_str[32], pre_str[64];
760+
sig_str[0] = '\0';
761+
if (sig != -1) {
762+
snprintf(sig_str, 32, "signal (%d) ", sig);
763+
}
764+
snprintf(pre_str, 64, "%sthread (%d) ", sig_str, jl_threadid() + 1);
765+
755766
if (jl_bt_is_native(bt_entry)) {
756-
jl_print_native_codeloc(bt_entry[0].uintptr);
767+
jl_print_native_codeloc(pre_str, bt_entry[0].uintptr);
757768
}
758769
else if (jl_bt_entry_tag(bt_entry) == JL_BT_INTERP_FRAME_TAG) {
759770
size_t ip = jl_bt_entry_header(bt_entry); // zero-indexed
@@ -772,7 +783,7 @@ void jl_print_bt_entry_codeloc(jl_bt_element_t *bt_entry) JL_NOTSAFEPOINT
772783
if (jl_is_code_info(code)) {
773784
jl_code_info_t *src = (jl_code_info_t*)code;
774785
// See also the debug info handling in codegen.cpp.
775-
jl_print_debugloc(src->debuginfo, def, ip + 1, 0);
786+
jl_print_debugloc(pre_str, src->debuginfo, def, ip + 1, 0);
776787
}
777788
else {
778789
// If we're using this function something bad has already happened;
@@ -1361,7 +1372,9 @@ JL_DLLEXPORT jl_record_backtrace_result_t jl_record_backtrace(jl_task_t *t, jl_b
13611372

13621373
JL_DLLEXPORT void jl_gdblookup(void* ip)
13631374
{
1364-
jl_print_native_codeloc((uintptr_t)ip);
1375+
char pre_str[64];
1376+
snprintf(pre_str, 64, "thread (%d) ", jl_threadid() + 1);
1377+
jl_print_native_codeloc(pre_str, (uintptr_t)ip);
13651378
}
13661379

13671380
// Print backtrace for current exception in catch block
@@ -1376,7 +1389,7 @@ JL_DLLEXPORT void jlbacktrace(void) JL_NOTSAFEPOINT
13761389
size_t i, bt_size = jl_excstack_bt_size(s, s->top);
13771390
jl_bt_element_t *bt_data = jl_excstack_bt_data(s, s->top);
13781391
for (i = 0; i < bt_size; i += jl_bt_entry_size(bt_data + i)) {
1379-
jl_print_bt_entry_codeloc(bt_data + i);
1392+
jl_print_bt_entry_codeloc(-1, bt_data + i);
13801393
}
13811394
}
13821395

@@ -1391,7 +1404,7 @@ JL_DLLEXPORT void jlbacktracet(jl_task_t *t) JL_NOTSAFEPOINT
13911404
size_t bt_size = r.bt_size;
13921405
size_t i;
13931406
for (i = 0; i < bt_size; i += jl_bt_entry_size(bt_data + i)) {
1394-
jl_print_bt_entry_codeloc(bt_data + i);
1407+
jl_print_bt_entry_codeloc(-1, bt_data + i);
13951408
}
13961409
if (bt_size == 0)
13971410
jl_safe_printf(" no backtrace recorded\n");

0 commit comments

Comments
 (0)