26 #ifdef HAVE_VALGRIND_MEMCHECK_H
27 # include <valgrind/memcheck.h>
28 # ifndef VALGRIND_MAKE_MEM_DEFINED
29 # define VALGRIND_MAKE_MEM_DEFINED(p, n) VALGRIND_MAKE_READABLE((p), (n))
31 # ifndef VALGRIND_MAKE_MEM_UNDEFINED
32 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE((p), (n))
35 # define VALGRIND_MAKE_MEM_DEFINED(p, n) 0
36 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) 0
39 #if defined(__native_client__) && defined(NACL_NEWLIB)
43 #ifdef NEED_RUBY_ATOMIC_OPS
64 #if defined(__BEOS__) || defined(__HAIKU__)
69 # define NSIG (_SIGMAX + 1)
161 {
"VTALRM", SIGVTALRM},
188 {
"DANGER", SIGDANGER},
191 {
"MIGRATE", SIGMIGRATE},
200 {
"RETRACT", SIGRETRACT},
217 if (strcmp(sigs->
signm, nm) == 0)
228 if (sigs->
signo == no)
278 if (!
NIL_P(sig)) argnum = 2;
284 if (signo < 0 || signo >
NSIG) {
302 if (strncmp(signm,
"SIG", 3) == 0) signm += 3;
388 #define killpg(pg, sig) kill(-(pg), (sig))
399 switch (
TYPE(argv[0])) {
416 if (strncmp(
"SIG", s, 3) == 0)
436 if (argc <= 1)
return INT2FIX(0);
440 for (i=1; i<
argc; i++) {
446 const rb_pid_t
self = (
GET_THREAD() ==
GET_VM()->main_thread) ? getpid() : -1;
449 for (i=1; i<
argc; i++) {
452 if ((sig != 0) && (
self != -1) && (pid ==
self)) {
474 if (t < 0 &&
kill(pid, sig))
482 else if (
kill(pid, sig) < 0) {
501 #define sighandler_t sh_t
503 #define sighandler_t ruby_sighandler_t
507 #ifdef USE_SIGALTSTACK
509 #define SIGINFO_ARG , siginfo_t *info, void *ctx
515 #ifdef USE_SIGALTSTACK
523 if (size < MINSIGSTKSZ)
526 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
529 pagesize = (int)sysconf(_SC_PAGE_SIZE);
542 stack_t newSS, oldSS;
545 rb_bug(
"rb_register_sigaltstack: th->altstack not initialized\n");
547 newSS.ss_sp = th->altstack;
551 sigaltstack(&newSS, &oldSS);
559 struct sigaction sigact, old;
562 rb_trap_accept_nativethreads[signum] = 0;
565 sigemptyset(&sigact.sa_mask);
566 #ifdef USE_SIGALTSTACK
568 sigact.sa_flags = SA_SIGINFO;
570 sigact.sa_handler = handler;
575 if (signum == SIGCHLD && handler == SIG_IGN)
576 sigact.sa_flags |= SA_NOCLDWAIT;
578 #if defined(SA_ONSTACK) && defined(USE_SIGALTSTACK)
579 if (signum == SIGSEGV
584 sigact.sa_flags |= SA_ONSTACK;
587 if (sigaction(signum, &sigact, &old) < 0) {
592 if (old.sa_flags & SA_SIGINFO)
595 return old.sa_handler;
605 #define ruby_signal(sig,handler) ( signal((sig),(handler)))
608 ruby_nativethread_signal(
int signum,
sighandler_t handler)
612 old =
signal(signum, handler);
613 rb_trap_accept_nativethreads[signum] = 1;
624 struct sigaction old;
626 if (sigaction(sig,
NULL, &old) < 0)
return FALSE;
627 func = old.sa_handler;
633 if (func == SIG_IGN)
return 1;
649 #if !defined(BSD_SIGNAL) && !defined(POSIX_SIGNAL)
667 #ifdef HAVE_PTHREAD_SIGMASK
677 #ifdef HAVE_PTHREAD_SIGMASK
703 #if defined(USE_SIGALTSTACK) || defined(_WIN32)
705 #if defined(HAVE_UCONTEXT_H) && defined __linux__ && (defined __i386__ || defined __x86_64__)
706 # define USE_UCONTEXT_REG 1
708 #ifdef USE_UCONTEXT_REG
710 check_stack_overflow(
const uintptr_t addr,
const ucontext_t *ctx)
713 const greg_t sp = ctx->uc_mcontext.gregs[REG_RSP];
715 const greg_t sp = ctx->uc_mcontext.gregs[REG_ESP];
717 enum {pagesize = 4096};
719 const uintptr_t fault_page = addr / pagesize;
723 if (sp_page == fault_page || sp_page == fault_page + 1) {
736 check_stack_overflow(
const void *addr)
738 int ruby_stack_overflowed_p(
const rb_thread_t *,
const void *);
740 if (ruby_stack_overflowed_p(th, addr)) {
746 #define CHECK_STACK_OVERFLOW() check_stack_overflow(0)
748 #define FAULT_ADDRESS info->si_addr
749 # ifdef USE_UCONTEXT_REG
750 # define CHECK_STACK_OVERFLOW() check_stack_overflow((uintptr_t)FAULT_ADDRESS, ctx)
752 # define CHECK_STACK_OVERFLOW() check_stack_overflow(FAULT_ADDRESS)
754 #define MESSAGE_FAULT_ADDRESS " at %p", FAULT_ADDRESS
757 #define CHECK_STACK_OVERFLOW() (void)0
759 #ifndef MESSAGE_FAULT_ADDRESS
760 #define MESSAGE_FAULT_ADDRESS
772 #if defined __APPLE__
794 static int segv_received = 0;
802 char msg[] =
"SEGV received in SEGV handler\n";
804 err = write(2, msg,
sizeof(msg));
811 ruby_disable_gc_stress = 1;
820 volatile unsigned long old_interrupt_mask = cur_th->
interrupt_mask;
966 if (!
NIL_P(command)) {
974 if (strncmp(
RSTRING_PTR(command),
"SYSTEM_DEFAULT", 14) == 0) {
980 if (strncmp(
RSTRING_PTR(command),
"SIG_IGN", 7) == 0) {
985 else if (strncmp(
RSTRING_PTR(command),
"SIG_DFL", 7) == 0) {
990 else if (strncmp(
RSTRING_PTR(command),
"DEFAULT", 7) == 0) {
995 if (strncmp(
RSTRING_PTR(command),
"IGNORE", 6) == 0) {
1000 if (strncmp(
RSTRING_PTR(command),
"EXIT", 4) == 0) {
1022 switch (
TYPE(vsig)) {
1025 if (sig < 0 || sig >=
NSIG) {
1039 if (strncmp(
"SIG", s, 3) == 0)
1042 if (sig == 0 && strcmp(s,
"EXIT") != 0)
1065 if (oldfunc == SIG_IGN) oldcmd =
rb_str_new2(
"IGNORE");
1087 if (signo == SIGSEGV)
1091 if (signo == SIGBUS)
1095 if (signo == SIGILL)
1099 if (signo == SIGFPE)
1105 if (signo == SIGVTALRM)
1175 return trap(sig, func, cmd);
1208 if (old != SIG_DFL) {
1214 #if defined(SIGCLD) || defined(SIGCHLD)
1216 init_sigchld(
int sig)
1222 if (oldfunc != SIG_DFL && oldfunc != SIG_IGN) {
1225 GET_VM()->trap_list[sig].cmd = 0;
1244 #ifndef RUBY_DEBUG_ENV
1245 #define ruby_enable_coredump 0
1320 if (!ruby_enable_coredump) {
1325 # ifdef USE_SIGALTSTACK
1336 init_sigchld(SIGCLD);
1337 #elif defined(SIGCHLD)
1338 init_sigchld(SIGCHLD);
static VALUE sig_list(void)
static void signal_exec(VALUE cmd, int safe, int sig)
#define MESSAGE_FAULT_ADDRESS
static void signal_enque(int sig)
const char * ruby_signal_name(int no)
void ruby_thread_stack_overflow(rb_thread_t *th)
void rb_bug(const char *fmt,...)
static VALUE trap(int sig, sighandler_t func, VALUE command)
static struct @152 signal_buff
rb_atomic_t ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val)
#define GetProcPtr(obj, ptr)
static int signal_ignored(int sig)
static VALUE interrupt_init(int argc, VALUE *argv, VALUE self)
static void rb_enable_interrupt(void)
void rb_threadptr_signal_raise(rb_thread_t *th, int sig)
RETSIGTYPE ruby_sigaction_t(int)
void rb_signal_exec(rb_thread_t *th, int sig)
#define ruby_enable_coredump
VALUE rb_iv_set(VALUE, const char *, VALUE)
VALUE rb_iv_get(VALUE, const char *)
void rb_raise(VALUE exc, const char *fmt,...)
void ruby_default_signal(int sig)
static VALUE esignal_signo(VALUE self)
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
VALUE rb_check_to_integer(VALUE, const char *)
static sighandler_t trap_handler(VALUE *cmd, int sig)
const char * rb_obj_classname(VALUE)
static VALUE sig_signame(VALUE recv, VALUE signo)
sighandler_t posix_signal(int signum, sighandler_t handler)
int ruby_disable_gc_stress
static int reserved_signal_p(int signo)
void rb_thread_wakeup_timer_thread(void)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
static RETSIGTYPE sighandler(int sig)
VALUE rb_thread_current(void)
struct rb_vm_struct::@167 trap_list[RUBY_NSIG]
int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
static void install_sighandler(int signum, sighandler_t handler)
static const struct signals siglist[]
rb_atomic_t cnt[RUBY_NSIG]
void rb_threadptr_check_signal(rb_thread_t *mth)
static sighandler_t default_handler(int sig)
static VALUE sig_trap(int argc, VALUE *argv)
static int signm2signo(const char *nm)
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
SSL_METHOD *(* func)(void)
VALUE rb_sprintf(const char *format,...)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
void rb_thread_execute_interrupts(VALUE th)
static const char * signo2signm(int no)
rb_thread_t * ruby_current_thread
#define VALGRIND_MAKE_MEM_DEFINED(p, n)
static void rb_disable_interrupt(void)
void rb_bug_errno(const char *mesg, int errno_arg)
void rb_alias(VALUE, ID, ID)
VALUE rb_call_super(int, const VALUE *)
VALUE rb_str_new_cstr(const char *)
int rb_sigaltstack_size(void)
void rb_sys_fail(const char *mesg)
int rb_get_next_signal(void)
VALUE rb_f_kill(int argc, VALUE *argv)
rb_atomic_t ruby_atomic_compare_and_swap(rb_atomic_t *ptr, rb_atomic_t cmp, rb_atomic_t newval)
#define CHECK_STACK_OVERFLOW()
#define UNLIMITED_ARGUMENTS
sighandler_t signal(int signum, sighandler_t handler)
unsigned long interrupt_mask
VALUE rb_block_proc(void)
void ruby_sig_finalize(void)
VALUE rb_check_string_type(VALUE)
int rb_signal_buff_size(void)
static VALUE esignal_init(int argc, VALUE *argv, VALUE self)
VALUE rb_eval_cmd(VALUE, VALUE, int)
#define SafeStringValue(v)
void ruby_kill(rb_pid_t pid, int sig)
static int trap_signm(VALUE vsig)
const char * rb_id2name(ID id)
#define StringValuePtr(v)
#define ruby_signal(sig, handler)
VALUE rb_define_module(const char *name)
static rb_thread_t * GET_THREAD(void)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
void rb_threadptr_signal_exit(rb_thread_t *th)