Skip to content

Commit

Permalink
Move crash handler to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 committed Jun 29, 2023
1 parent 6d31cb9 commit 7aae759
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 44 deletions.
4 changes: 4 additions & 0 deletions awscrt/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ def get_cpu_count_for_group(group_idx: int) -> int:
Returns number of processors in a given group.
"""
return _awscrt.get_cpu_count_for_group(group_idx)


def install_crash_handler():
_awscrt.install_crash_handler()
41 changes: 41 additions & 0 deletions source/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,44 @@ PyObject *aws_py_thread_join_all_managed(PyObject *self, PyObject *args) {

Py_RETURN_TRUE;
}

/*******************************************************************************
* Crash handler
******************************************************************************/
#if defined(_WIN32)
# include <windows.h>
static LONG WINAPI s_print_stack_trace(struct _EXCEPTION_POINTERS *exception_pointers) {
aws_backtrace_print(stderr, exception_pointers);
return EXCEPTION_EXECUTE_HANDLER;
}
#elif defined(AWS_HAVE_EXECINFO)
# include <signal.h>
static void s_print_stack_trace(int sig, siginfo_t *sig_info, void *user_data) {
(void)sig;
(void)sig_info;
(void)user_data;
aws_backtrace_print(stderr, sig_info);
exit(-1);
}
#endif

PyObject *aws_py_install_crash_handler(PyObject *self, PyObject *args) {
(void) self;
(void) args;
#if defined(_WIN32)
SetUnhandledExceptionFilter(s_print_stack_trace);
#elif defined(AWS_HAVE_EXECINFO)
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sigemptyset(&sa.sa_mask);

sa.sa_flags = SA_NODEFER;
sa.sa_sigaction = s_print_stack_trace;

sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGABRT, &sa, NULL);
sigaction(SIGILL, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
#endif
return Py_None;
}
1 change: 1 addition & 0 deletions source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ PyObject *aws_py_get_cpu_group_count(PyObject *self, PyObject *args);
PyObject *aws_py_get_cpu_count_for_group(PyObject *self, PyObject *args);

PyObject *aws_py_thread_join_all_managed(PyObject *self, PyObject *args);
PyObject *aws_py_install_crash_handler(PyObject *self, PyObject *args);

#endif /* AWS_CRT_PYTHON_COMMON_H */
45 changes: 1 addition & 44 deletions source/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -613,43 +613,6 @@ PyObject *aws_py_native_memory_dump(PyObject *self, PyObject *args) {
Py_RETURN_NONE;
}

/*******************************************************************************
* Crash handler
******************************************************************************/
#if defined(_WIN32)
# include <windows.h>
static LONG WINAPI s_print_stack_trace(struct _EXCEPTION_POINTERS *exception_pointers) {
aws_backtrace_print(stderr, exception_pointers);
return EXCEPTION_EXECUTE_HANDLER;
}
#elif defined(AWS_HAVE_EXECINFO)
# include <signal.h>
static void s_print_stack_trace(int sig, siginfo_t *sig_info, void *user_data) {
(void)sig;
(void)sig_info;
(void)user_data;
aws_backtrace_print(stderr, sig_info);
exit(-1);
}
#endif

static void s_install_crash_handler(void) {
#if defined(_WIN32)
SetUnhandledExceptionFilter(s_print_stack_trace);
#elif defined(AWS_HAVE_EXECINFO)
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sigemptyset(&sa.sa_mask);

sa.sa_flags = SA_NODEFER;
sa.sa_sigaction = s_print_stack_trace;

sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGABRT, &sa, NULL);
sigaction(SIGILL, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
#endif
}

/*******************************************************************************
* Definitions
Expand All @@ -668,6 +631,7 @@ static PyMethodDef s_module_methods[] = {
AWS_PY_METHOD_DEF(native_memory_usage, METH_NOARGS),
AWS_PY_METHOD_DEF(native_memory_dump, METH_NOARGS),
AWS_PY_METHOD_DEF(thread_join_all_managed, METH_VARARGS),
AWS_PY_METHOD_DEF(install_crash_handler, METH_NOARGS),

/* IO */
AWS_PY_METHOD_DEF(is_alpn_available, METH_NOARGS),
Expand Down Expand Up @@ -804,7 +768,6 @@ static PyMethodDef s_module_methods[] = {

static const char s_module_name[] = "_awscrt";
PyDoc_STRVAR(s_module_doc, "C extension for binding AWS implementations of MQTT, HTTP, and friends");
AWS_STATIC_STRING_FROM_LITERAL(s_crash_handler_env_var, "AWS_CRT_ENABLE_CRASH_HANDLER");

/*******************************************************************************
* Module Init
Expand Down Expand Up @@ -833,12 +796,6 @@ PyMODINIT_FUNC PyInit__awscrt(void) {
/* Don't report this memory when dumping possible leaks. */
struct aws_allocator *nontracing_allocator = aws_default_allocator();

struct aws_string *crash_handler_env = NULL;
aws_get_environment_value(nontracing_allocator, s_crash_handler_env_var, &crash_handler_env);
if (aws_string_eq_c_str(crash_handler_env, "1")) {
s_install_crash_handler();
}

aws_http_library_init(nontracing_allocator);
aws_auth_library_init(nontracing_allocator);
aws_mqtt_library_init(nontracing_allocator);
Expand Down

0 comments on commit 7aae759

Please sign in to comment.