Skip to content

Commit

Permalink
print C stack trace, too, on call failure
Browse files Browse the repository at this point in the history
  • Loading branch information
timfel committed Nov 12, 2010
1 parent 80ef9f0 commit 0036f28
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
17 changes: 15 additions & 2 deletions cext/src/include/ruby/ruby.h
Expand Up @@ -35,10 +35,23 @@
// Some platform specific includes
#if defined(__WIN32__) || defined(__MINGW32__)
# include "jruby_win32.h"
# define PRINT_STACK_TRACE
#else
# define RUBY_DLLSPEC
# include <sys/select.h>
# include <pthread.h>
# include <sys/select.h>
# include <pthread.h>
# ifndef NDEBUG
# include <execinfo.h>
# define PRINT_STACK_TRACE \
void* callstack[256] \
int i, frames = backtrace(callstack, 256); \
char** strs = backtrace_symbols(callstack, frames); \
for (i = 0; i < frames; ++i) { \
printf("%s\n", strs[i]); \
} \
free(strs);
#
# endif
#endif

#ifdef RUBY_EXTCONF_H
Expand Down
18 changes: 16 additions & 2 deletions cext/src/invoke.cpp
Expand Up @@ -98,11 +98,11 @@ Java_org_jruby_cext_Native_callInit(JNIEnv* env, jobject self, jobject jThreadCo
((void (*)(void)) address)();

} catch (jruby::JavaException& ex) {
PRINT_STACK_TRACE
env->Throw(ex.getCause());

} catch (std::exception& ex) {
PRINT_STACK_TRACE
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());

}

return 0;
Expand Down Expand Up @@ -134,10 +134,12 @@ Java_org_jruby_cext_Native_callMethod(JNIEnv* env, jobject nativeClass, jobject
return valueToObject(env, v);

} catch (jruby::JavaException& ex) {
PRINT_STACK_TRACE
env->Throw(ex.getCause());
return NULL;

} catch (std::exception& ex) {
PRINT_STACK_TRACE
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return NULL;
}
Expand All @@ -159,10 +161,12 @@ Java_org_jruby_cext_Native_callMethod0(JNIEnv* env, jobject self, jlong fn, jlon
return valueToObject(env, ((VALUE (*)(VALUE)) fn)((VALUE) recv));

} catch (jruby::JavaException& ex) {
PRINT_STACK_TRACE
env->Throw(ex.getCause());
return NULL;

} catch (std::exception& ex) {
PRINT_STACK_TRACE
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return NULL;
}
Expand All @@ -183,10 +187,12 @@ Java_org_jruby_cext_Native_callMethod1(JNIEnv* env, jobject self, jlong fn, jlon
return valueToObject(env, ((VALUE (*)(VALUE, VALUE)) fn)((VALUE) recv, (VALUE) arg1));

} catch (jruby::JavaException& ex) {
PRINT_STACK_TRACE
env->Throw(ex.getCause());
return NULL;

} catch (std::exception& ex) {
PRINT_STACK_TRACE
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return NULL;
}
Expand All @@ -209,10 +215,12 @@ Java_org_jruby_cext_Native_callMethod2(JNIEnv* env, jobject self, jlong fn, jlon
return valueToObject(env, ((VALUE (*)(VALUE, VALUE, VALUE)) fn)((VALUE) recv, (VALUE) arg1, (VALUE) arg2));

} catch (jruby::JavaException& ex) {
PRINT_STACK_TRACE
env->Throw(ex.getCause());
return NULL;

} catch (std::exception& ex) {
PRINT_STACK_TRACE
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return NULL;
}
Expand All @@ -237,10 +245,12 @@ Java_org_jruby_cext_Native_callMethod3(JNIEnv* env, jobject self, jlong fn, jlon
return valueToObject(env, ((VALUE (*)(VALUE, VALUE, VALUE, VALUE)) fn)((VALUE) recv, (VALUE) arg1, (VALUE) arg2, (VALUE) arg3));

} catch (jruby::JavaException& ex) {
PRINT_STACK_TRACE
env->Throw(ex.getCause());
return NULL;

} catch (std::exception& ex) {
PRINT_STACK_TRACE
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return NULL;
}
Expand Down Expand Up @@ -300,10 +310,12 @@ Java_org_jruby_cext_Native_callFunction(JNIEnv* env, jobject, jlong function, jl
return ((VALUE (*)(void*)) function)((void*) data);

} catch (jruby::JavaException& ex) {
PRINT_STACK_TRACE
env->Throw(ex.getCause());
return 0x0;

} catch (std::exception& ex) {
PRINT_STACK_TRACE
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return 0x0;
}
Expand All @@ -324,10 +336,12 @@ Java_org_jruby_cext_Native_callProcMethod(JNIEnv* env, jobject, jlong fn, jlong
return valueToObject(env, ((VALUE (*)(VALUE)) fn)((VALUE) args_ary));

} catch (jruby::JavaException& ex) {
PRINT_STACK_TRACE
env->Throw(ex.getCause());
return NULL;

} catch (std::exception& ex) {
PRINT_STACK_TRACE
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return NULL;
}
Expand Down

0 comments on commit 0036f28

Please sign in to comment.