diff --git a/deps/Makefile b/deps/Makefile index d536c7e162196..efec7f206328f 100644 --- a/deps/Makefile +++ b/deps/Makefile @@ -1283,7 +1283,7 @@ install-unwind: $(LIBUNWIND_TARGET_OBJ) ## OS X Unwind ## -OSXUNWIND_FLAGS = ARCH="$(ARCH)" CC="$(CC)" FC="$(FC)" AR="$(AR)" OS="$(OS)" USECLANG=$(USECLANG) USEGCC=$(USEGCC) +OSXUNWIND_FLAGS = ARCH="$(ARCH)" CC="$(CC)" FC="$(FC)" AR="$(AR)" OS="$(OS)" USECLANG=$(USECLANG) USEGCC=$(USEGCC) CFLAGS="-ggdb3 -O0" CXXFLAGS="-ggdb3 -O0" SFLAGS="-ggdb3" OSXUNWIND_OBJ_TARGET = $(BUILD)/lib/libosxunwind.$(SHLIB_EXT) OSXUNWIND_OBJ_SOURCE = libosxunwind-$(OSXUNWIND_VER)/libosxunwind.$(SHLIB_EXT) diff --git a/deps/Versions.make b/deps/Versions.make index 4a074d6b8d487..b3ff216ec936f 100644 --- a/deps/Versions.make +++ b/deps/Versions.make @@ -14,7 +14,7 @@ ARPACK_VER = 3.1.3 FFTW_VER = 3.3.3 SUITESPARSE_VER = 4.2.1 UNWIND_VER = 1.1 -OSXUNWIND_VER = 0.0.1-rc2 +OSXUNWIND_VER = 0.0.1-rc3 GMP_VER=5.1.1 MPFR_VER=3.1.2 ZLIB_VER = 1.2.8 diff --git a/src/init.c b/src/init.c index a5e111b41604f..7ef63dacd238c 100644 --- a/src/init.c +++ b/src/init.c @@ -546,6 +546,9 @@ void darwin_stack_overflow_handler(unw_context_t *uc) #define HANDLE_MACH_ERROR(msg, retval) \ if (retval!=KERN_SUCCESS) { mach_error(msg ":", (retval)); jl_exit(1); } +extern kern_return_t profiler_segv_handler(mach_port_t,mach_port_t,mach_port_t,exception_type_t,exception_data_t,mach_msg_type_number_t); +extern volatile mach_port_t mach_profiler_thread; + //exc_server uses dlsym to find symbol DLLEXPORT kern_return_t catch_exception_raise (mach_port_t exception_port, @@ -562,6 +565,10 @@ DLLEXPORT kern_return_t catch_exception_raise kern_return_t ret; //memset(&state,0,sizeof(x86_thread_state64_t)); //memset(&exc_state,0,sizeof(x86_exception_state64_t)); + if (thread == mach_profiler_thread) + { + return profiler_segv_handler(exception_port,thread,task,exception,code,code_count); + } ret = thread_get_state(thread,x86_EXCEPTION_STATE64,(thread_state_t)&exc_state,&exc_count); HANDLE_MACH_ERROR("thread_get_state(1)",ret); uint64_t fault_addr = exc_state.__faultvaddr; diff --git a/src/julia.h b/src/julia.h index 39cc2eeaf9fe9..0f03b44800e54 100644 --- a/src/julia.h +++ b/src/julia.h @@ -1234,6 +1234,10 @@ extern ptrint_t bt_data[MAX_BT_SIZE+1]; extern size_t bt_size; DLLEXPORT size_t rec_backtrace(ptrint_t *data, size_t maxsize); DLLEXPORT size_t rec_backtrace_ctx(ptrint_t *data, size_t maxsize, bt_context_t ctx); +#ifdef _OS_DARWIN_ +size_t rec_backtrace_ctx_dwarf(ptrint_t *data, size_t maxsize, bt_context_t ctx); +#endif + //IO objects extern DLLEXPORT uv_stream_t *jl_uv_stdin; diff --git a/src/profile.c b/src/profile.c index 72fbef05ed7bf..5378481c2ccbc 100644 --- a/src/profile.c +++ b/src/profile.c @@ -92,6 +92,7 @@ DLLEXPORT void profile_stop_timer(void) { #include #include #include +#include #define HANDLE_MACH_ERROR(msg, retval) \ if (retval!=KERN_SUCCESS) { mach_error(msg ":", (retval)); jl_exit(1); } @@ -102,30 +103,59 @@ clock_serv_t clk; static int profile_started = 0; static mach_port_t profile_port = 0; volatile static int running = 0; +volatile static int forceDwarf = -2; +volatile mach_port_t mach_profiler_thread = 0; mach_timespec_t timerprof; - -kern_return_t clock_alarm_reply -( - clock_reply_t alarm_port, - mach_msg_type_name_t alarm_portPoly, - kern_return_t alarm_code, - alarm_type_t alarm_type, - mach_timespec_t alarm_time -) +static unw_context_t profiler_uc; + +kern_return_t profiler_segv_handler + (mach_port_t exception_port, + mach_port_t thread, + mach_port_t task, + exception_type_t exception, + exception_data_t code, + mach_msg_type_number_t code_count) { - HANDLE_MACH_ERROR("clock_alarm_reply",alarm_code); - + assert(thread == mach_profiler_thread); + x86_thread_state64_t state; + + // Not currently unwinding. Raise regular segfault + if (forceDwarf == -2) + return -309; + + if (forceDwarf == 0) + forceDwarf = 1; + else + forceDwarf = -1; + + unsigned int count = MACHINE_THREAD_STATE_COUNT; + + thread_get_state(thread,x86_THREAD_STATE64,(thread_state_t)&state,&count); + + // don't change cs fs gs rflags + uint64_t cs = state.__cs; + uint64_t fs = state.__fs; + uint64_t gs = state.__gs; + uint64_t rflags = state.__rflags; + memcpy(&state,&profiler_uc,sizeof(x86_thread_state64_t)); + + state.__cs = cs; + state.__fs = fs; + state.__gs = gs; + state.__rflags = rflags; + + kern_return_t ret = thread_set_state(thread,x86_THREAD_STATE64,(thread_state_t)&state,count); + HANDLE_MACH_ERROR("thread_set_state",ret); return KERN_SUCCESS; } -extern boolean_t clock_alarm_reply_server(mach_msg_header_t *, mach_msg_header_t *); - void * mach_profile_listener(void *arg) { (void) arg; int max_size = 512; + mach_profiler_thread = mach_thread_self(); mig_reply_error_t *bufRequest = (mig_reply_error_t *) malloc(max_size); while (1) { kern_return_t ret = mach_msg(&bufRequest->Head, MACH_RCV_MSG, @@ -151,14 +181,40 @@ void * mach_profile_listener(void *arg) memset(&uc,0,sizeof(unw_context_t)); memcpy(&uc,&state,sizeof(x86_thread_state64_t)); - // Save the backtrace - bt_size_cur += rec_backtrace_ctx((ptrint_t*)bt_data_prof+bt_size_cur, bt_size_max-bt_size_cur-1, &uc); + /* + * Unfortunately compact unwind info is incorrectly generated for quite a number of + * libraries by quite a large number of compilers. We can fall back to DWARF unwind info + * in some cases, but in quite a number of cases (especially libraries not compiled in debug + * mode, only the compact unwind info may be available). Even more unfortunately, there is no + * way to detect such bogus compact unwind info (other than noticing the resulting segfault). + * What we do here is ugly, but necessary until the compact unwind info situation improves. + * We try to use the compact unwind info and if that results in a segfault, we retry with DWARF info. + * Note that in a small number of cases this may result in bogus stack traces, but at least the topmost + * entry will always be correct, and the number of cases in which this is an issue is rather small. + * Other than that, this implementation is not incorrect as the other thread is paused while we are profiling + * and during stack unwinding we only ever read memory, but never write it. + */ + + forceDwarf = 0; + unw_getcontext(&profiler_uc); + + if (forceDwarf == 0) { + // Save the backtrace + bt_size_cur += rec_backtrace_ctx((ptrint_t*)bt_data_prof+bt_size_cur, bt_size_max-bt_size_cur-1, &uc); + } else if(forceDwarf == 1) { + bt_size_cur += rec_backtrace_ctx_dwarf((ptrint_t*)bt_data_prof+bt_size_cur, bt_size_max-bt_size_cur-1, &uc); + } else if (forceDwarf == -1) { + JL_PRINTF(JL_STDERR, "Warning: Profiler attempt to access an invalid memory location\n"); + } + + forceDwarf = -2; + // Mark the end of this block with 0 bt_data_prof[bt_size_cur] = 0; bt_size_cur++; // We're done! Resume the thread. - thread_resume(main_thread); + ret = thread_resume(main_thread); HANDLE_MACH_ERROR("thread_resume",ret) if (running) { diff --git a/src/task.c b/src/task.c index f243946cb3e09..aca384e57e3cb 100644 --- a/src/task.c +++ b/src/task.c @@ -558,14 +558,35 @@ DLLEXPORT size_t rec_backtrace_ctx(ptrint_t *data, size_t maxsize, unw_context_t size_t n=0; unw_init_local(&cursor, uc); - while (unw_step(&cursor) > 0 && n < maxsize) { + do { + if (n >= maxsize) + break; if (unw_get_reg(&cursor, UNW_REG_IP, &ip) < 0) { break; } data[n++] = ip; - } + } while (unw_step(&cursor) > 0); return n; } +#ifdef _OS_DARWIN_ +size_t rec_backtrace_ctx_dwarf(ptrint_t *data, size_t maxsize, unw_context_t *uc) +{ + unw_cursor_t cursor; + unw_word_t ip; + size_t n=0; + + unw_init_local_dwarf(&cursor, uc); + do { + if (n >= maxsize) + break; + if (unw_get_reg(&cursor, UNW_REG_IP, &ip) < 0) { + break; + } + data[n++] = ip; + } while (unw_step(&cursor) > 0); + return n; +} +#endif #endif static void record_backtrace(void)