Project

General

Profile

Feature #17638 ยป ruby-libbacktrace-using-backtrace.patch

xtkoba (Tee KOBAYASHI), 02/27/2021 10:44 PM

View differences:

vm_dump.c
758 758
}
759 759
#endif
760 760

  
761
#if defined(USE_LIBBACKTRACE)
762
void
763
libbacktrace_cb_error(void *data, const char *msg, int errnum)
764
{
765
    fprintf(stderr, "libbacktrace error: msg: %s, errnum: %d\n", msg, errnum);
766
}
767

  
768
void
769
libbacktrace_cb_syminfo(void *data, uintptr_t pc, const char *symname, uintptr_t symval, uintptr_t symsize)
770
{
771
    if (symname != NULL) {
772
	fprintf(stderr, "[0x%"PRIxPTR"] %s+0x%"PRIxPTR"\n", pc, symname, pc-symval);
773
    } else {
774
	fprintf(stderr, "[0x%"PRIxPTR"]\n", pc);
775
    }
776
}
777

  
778
int
779
libbacktrace_cb_pcinfo(void *data, uintptr_t pc, const char *filename, int lineno, const char *function)
780
{
781
    if (filename != NULL) {
782
	fprintf(stderr, "\t%s:%d\n", filename, lineno);
783
    }
784
    return 0;
785
}
786
#endif
787

  
761 788
void
762 789
rb_print_backtrace(void)
763 790
{
764 791
#if defined(USE_LIBBACKTRACE)
765 792
    struct backtrace_state *state = backtrace_create_state(NULL, 0, NULL, NULL);
766
    backtrace_print(state, 0, stderr);
767
#elif HAVE_BACKTRACE
793
#endif
794
#if HAVE_BACKTRACE
768 795
#define MAX_NATIVE_TRACE 1024
769 796
    static void *trace[MAX_NATIVE_TRACE];
770 797
    int n = (int)backtrace(trace, MAX_NATIVE_TRACE);
771
#if (defined(USE_ELF) || defined(HAVE_MACH_O_LOADER_H)) && defined(HAVE_DLADDR) && !defined(__sparc)
798
    int i;
799
#if defined(USE_LIBBACKTRACE)
800
    for (i = 0; i < n; i++) {
801
#if defined(__arm__)
802
	uintptr_t pc = ((uintptr_t)trace[i] & (~1)) - 1;
803
#else
804
	uintptr_t pc = (uintptr_t)trace[i] - 1;
805
#endif
806
	backtrace_syminfo(state, pc, libbacktrace_cb_syminfo, libbacktrace_cb_error, NULL);
807
	backtrace_pcinfo(state, pc, libbacktrace_cb_pcinfo, libbacktrace_cb_error, NULL);
808
    }
809
#elif (defined(USE_ELF) || defined(HAVE_MACH_O_LOADER_H)) && defined(HAVE_DLADDR) && !defined(__sparc)
772 810
    rb_dump_backtrace_with_lines(n, trace);
773 811
#elif defined(HAVE_EXECINFO_H)
774 812
    char **syms = backtrace_symbols(trace, n);
......
780 818
	free(syms);
781 819
    }
782 820
#endif
821
#elif defined(USE_LIBBACKTRACE)
822
    backtrace_print(state, 0, stderr);
783 823
#elif defined(_WIN32)
784 824
    DWORD tid = GetCurrentThreadId();
785 825
    HANDLE th = (HANDLE)_beginthread(dump_thread, 0, &tid);