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;
387 #define killpg(pg, sig) kill(-(pg), (sig))
398 switch (
TYPE(argv[0])) {
415 if (strncmp(
"SIG", s, 3) == 0)
435 if (argc <= 1)
return INT2FIX(0);
439 for (i=1; i<
argc; i++) {
445 const rb_pid_t
self = (
GET_THREAD() ==
GET_VM()->main_thread) ? getpid() : -1;
448 for (i=1; i<
argc; i++) {
451 if ((sig != 0) && (
self != -1) && (pid ==
self)) {
475 else if (
kill(pid, sig) < 0) {
494 #define sighandler_t sh_t
496 #define sighandler_t ruby_sighandler_t
500 #ifdef USE_SIGALTSTACK
502 #define SIGINFO_ARG , siginfo_t *info, void *ctx
508 #ifdef USE_SIGALTSTACK
516 if (size < MINSIGSTKSZ)
519 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
522 pagesize = (int)sysconf(_SC_PAGE_SIZE);
535 stack_t newSS, oldSS;
538 rb_bug(
"rb_register_sigaltstack: th->altstack not initialized\n");
540 newSS.ss_sp = th->altstack;
544 sigaltstack(&newSS, &oldSS);
552 struct sigaction sigact, old;
555 rb_trap_accept_nativethreads[signum] = 0;
558 sigemptyset(&sigact.sa_mask);
559 #ifdef USE_SIGALTSTACK
561 sigact.sa_flags = SA_SIGINFO;
563 sigact.sa_handler = handler;
568 if (signum == SIGCHLD && handler == SIG_IGN)
569 sigact.sa_flags |= SA_NOCLDWAIT;
571 #if defined(SA_ONSTACK) && defined(USE_SIGALTSTACK)
572 if (signum == SIGSEGV
577 sigact.sa_flags |= SA_ONSTACK;
580 if (sigaction(signum, &sigact, &old) < 0) {
585 if (old.sa_flags & SA_SIGINFO)
588 return old.sa_handler;
598 #define ruby_signal(sig,handler) ( signal((sig),(handler)))
601 ruby_nativethread_signal(
int signum,
sighandler_t handler)
605 old =
signal(signum, handler);
606 rb_trap_accept_nativethreads[signum] = 1;
616 struct sigaction old;
618 if (sigaction(sig,
NULL, &old) < 0)
return FALSE;
619 return old.sa_handler == SIG_IGN;
623 return old == SIG_IGN;
639 #if !defined(BSD_SIGNAL) && !defined(POSIX_SIGNAL)
657 #ifdef HAVE_PTHREAD_SIGMASK
667 #ifdef HAVE_PTHREAD_SIGMASK
693 #if defined(USE_SIGALTSTACK) || defined(_WIN32)
695 #if defined(HAVE_UCONTEXT_H) && defined __linux__ && (defined __i386__ || defined __x86_64__)
696 # define USE_UCONTEXT_REG 1
698 #ifdef USE_UCONTEXT_REG
700 check_stack_overflow(
const uintptr_t addr,
const ucontext_t *ctx)
703 const greg_t sp = ctx->uc_mcontext.gregs[REG_RSP];
705 const greg_t sp = ctx->uc_mcontext.gregs[REG_ESP];
707 enum {pagesize = 4096};
709 const uintptr_t fault_page = addr / pagesize;
713 if (sp_page == fault_page || sp_page == fault_page + 1) {
726 check_stack_overflow(
const void *addr)
728 int ruby_stack_overflowed_p(
const rb_thread_t *,
const void *);
730 if (ruby_stack_overflowed_p(th, addr)) {
736 #define CHECK_STACK_OVERFLOW() check_stack_overflow(0)
738 #define FAULT_ADDRESS info->si_addr
739 # ifdef USE_UCONTEXT_REG
740 # define CHECK_STACK_OVERFLOW() check_stack_overflow((uintptr_t)FAULT_ADDRESS, ctx)
742 # define CHECK_STACK_OVERFLOW() check_stack_overflow(FAULT_ADDRESS)
744 #define MESSAGE_FAULT_ADDRESS " at %p", FAULT_ADDRESS
747 #define CHECK_STACK_OVERFLOW() (void)0
749 #ifndef MESSAGE_FAULT_ADDRESS
750 #define MESSAGE_FAULT_ADDRESS
762 #if defined __APPLE__
784 static int segv_received = 0;
792 char msg[] =
"SEGV received in SEGV handler\n";
794 err = write(2, msg,
sizeof(msg));
801 ruby_disable_gc_stress = 1;
810 volatile unsigned long old_interrupt_mask = cur_th->
interrupt_mask;
956 if (!
NIL_P(command)) {
964 if (strncmp(
RSTRING_PTR(command),
"SYSTEM_DEFAULT", 14) == 0) {
970 if (strncmp(
RSTRING_PTR(command),
"SIG_IGN", 7) == 0) {
975 else if (strncmp(
RSTRING_PTR(command),
"SIG_DFL", 7) == 0) {
980 else if (strncmp(
RSTRING_PTR(command),
"DEFAULT", 7) == 0) {
985 if (strncmp(
RSTRING_PTR(command),
"IGNORE", 6) == 0) {
990 if (strncmp(
RSTRING_PTR(command),
"EXIT", 4) == 0) {
1012 switch (
TYPE(vsig)) {
1015 if (sig < 0 || sig >=
NSIG) {
1029 if (strncmp(
"SIG", s, 3) == 0)
1032 if (sig == 0 && strcmp(s,
"EXIT") != 0)
1055 if (oldfunc == SIG_IGN) oldcmd =
rb_str_new2(
"IGNORE");
1077 if (signo == SIGSEGV)
1081 if (signo == SIGBUS)
1085 if (signo == SIGILL)
1089 if (signo == SIGFPE)
1095 if (signo == SIGVTALRM)
1165 return trap(sig, func, cmd);
1198 if (old != SIG_DFL) {
1204 #if defined(SIGCLD) || defined(SIGCHLD)
1206 init_sigchld(
int sig)
1212 if (oldfunc != SIG_DFL && oldfunc != SIG_IGN) {
1215 GET_VM()->trap_list[sig].cmd = 0;
1234 #ifndef RUBY_DEBUG_ENV
1235 #define ruby_enable_coredump 0
1310 if (!ruby_enable_coredump) {
1315 # ifdef USE_SIGALTSTACK
1326 init_sigchld(SIGCLD);
1327 #elif defined(SIGCHLD)
1328 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)