Skip to content

Commit

Permalink
Add Profiler equivalent of 'On Error Resume Next'
Browse files Browse the repository at this point in the history
See the comment in src/profile.c for an explanation for what this is doing
  • Loading branch information
Keno committed Aug 27, 2013
1 parent 72d2154 commit 8b74988
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 20 deletions.
2 changes: 1 addition & 1 deletion deps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion deps/Versions.make
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
88 changes: 72 additions & 16 deletions src/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ DLLEXPORT void profile_stop_timer(void) {
#include <mach/clock.h>
#include <mach/clock_types.h>
#include <mach/clock_reply.h>
#include <assert.h>

#define HANDLE_MACH_ERROR(msg, retval) \
if (retval!=KERN_SUCCESS) { mach_error(msg ":", (retval)); jl_exit(1); }
Expand All @@ -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;

This comment has been minimized.

Copy link
@blakejohnson

blakejohnson Aug 27, 2013

Contributor

Can we define -309 somewhere? This otherwise looks like a magic number.

This comment has been minimized.

Copy link
@Keno

Keno Aug 27, 2013

Author Member

I think it's MIG_TRAILER_ERROR, though I don't quite remember why that did the trick.


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,
Expand All @@ -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) {
Expand Down
25 changes: 23 additions & 2 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8b74988

Please sign in to comment.