< prev index next >

src/os/posix/vm/os_posix.cpp

Print this page




 841 
 842   const int me = (int) ::getpid();
 843   const int pid = (int) si->si_pid;
 844 
 845   if (si->si_code == SI_USER || si->si_code == SI_QUEUE) {
 846     if (IS_VALID_PID(pid) && pid != me) {
 847       os->print(", sent from pid: %d (uid: %d)", pid, (int) si->si_uid);
 848     }
 849   } else if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
 850              sig == SIGTRAP || sig == SIGFPE) {
 851     os->print(", si_addr: " PTR_FORMAT, si->si_addr);
 852 #ifdef SIGPOLL
 853   } else if (sig == SIGPOLL) {
 854     os->print(", si_band: " PTR64_FORMAT, (uint64_t)si->si_band);
 855 #endif
 856   } else if (sig == SIGCHLD) {
 857     os->print_cr(", si_pid: %d, si_uid: %d, si_status: %d", (int) si->si_pid, si->si_uid, si->si_status);
 858   }
 859 }
 860 
 861 os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() {
 862   assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread");



 863 }
 864 
 865 /*
 866  * See the caveats for this class in os_posix.hpp
 867  * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
 868  * method and returns false. If none of the signals are raised, returns true.
 869  * The callback is supposed to provide the method that should be protected.
 870  */
 871 bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
 872   sigset_t saved_sig_mask;
 873 
 874   assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread");
 875   assert(!WatcherThread::watcher_thread()->has_crash_protection(),
 876       "crash_protection already set?");

 877 
 878   // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask
 879   // since on at least some systems (OS X) siglongjmp will restore the mask
 880   // for the process, not the thread
 881   pthread_sigmask(0, NULL, &saved_sig_mask);
 882   if (sigsetjmp(_jmpbuf, 0) == 0) {
 883     // make sure we can see in the signal handler that we have crash protection
 884     // installed
 885     WatcherThread::watcher_thread()->set_crash_protection(this);
 886     cb.call();
 887     // and clear the crash protection
 888     WatcherThread::watcher_thread()->set_crash_protection(NULL);


 889     return true;
 890   }
 891   // this happens when we siglongjmp() back
 892   pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL);
 893   WatcherThread::watcher_thread()->set_crash_protection(NULL);


 894   return false;
 895 }
 896 
 897 void os::WatcherThreadCrashProtection::restore() {
 898   assert(WatcherThread::watcher_thread()->has_crash_protection(),
 899       "must have crash protection");
 900 
 901   siglongjmp(_jmpbuf, 1);
 902 }
 903 
 904 void os::WatcherThreadCrashProtection::check_crash_protection(int sig,
 905     Thread* thread) {
 906 
 907   if (thread != NULL &&
 908       thread->is_Watcher_thread() &&
 909       WatcherThread::watcher_thread()->has_crash_protection()) {
 910 
 911     if (sig == SIGSEGV || sig == SIGBUS) {
 912       WatcherThread::watcher_thread()->crash_protection()->restore();
 913     }
 914   }
 915 }


 841 
 842   const int me = (int) ::getpid();
 843   const int pid = (int) si->si_pid;
 844 
 845   if (si->si_code == SI_USER || si->si_code == SI_QUEUE) {
 846     if (IS_VALID_PID(pid) && pid != me) {
 847       os->print(", sent from pid: %d (uid: %d)", pid, (int) si->si_uid);
 848     }
 849   } else if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
 850              sig == SIGTRAP || sig == SIGFPE) {
 851     os->print(", si_addr: " PTR_FORMAT, si->si_addr);
 852 #ifdef SIGPOLL
 853   } else if (sig == SIGPOLL) {
 854     os->print(", si_band: " PTR64_FORMAT, (uint64_t)si->si_band);
 855 #endif
 856   } else if (sig == SIGCHLD) {
 857     os->print_cr(", si_pid: %d, si_uid: %d, si_status: %d", (int) si->si_pid, si->si_uid, si->si_status);
 858   }
 859 }
 860 
 861 Thread* os::ThreadCrashProtection::_protected_thread = NULL;
 862 os::ThreadCrashProtection* os::ThreadCrashProtection::_crash_protection = NULL;
 863 volatile intptr_t os::ThreadCrashProtection::_crash_mux = 0;
 864 
 865 os::ThreadCrashProtection::ThreadCrashProtection() {
 866 }
 867 
 868 /*
 869  * See the caveats for this class in os_posix.hpp
 870  * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
 871  * method and returns false. If none of the signals are raised, returns true.
 872  * The callback is supposed to provide the method that should be protected.
 873  */
 874 bool os::ThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
 875   sigset_t saved_sig_mask;
 876 
 877   Thread::muxAcquire(&_crash_mux, "CrashProtection");
 878 
 879   _protected_thread = ThreadLocalStorage::thread();
 880   assert(_protected_thread != NULL, "Cannot crash protect a NULL thread");
 881 
 882   // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask
 883   // since on at least some systems (OS X) siglongjmp will restore the mask
 884   // for the process, not the thread
 885   pthread_sigmask(0, NULL, &saved_sig_mask);
 886   if (sigsetjmp(_jmpbuf, 0) == 0) {
 887     // make sure we can see in the signal handler that we have crash protection
 888     // installed
 889     _crash_protection = this;
 890     cb.call();
 891     // and clear the crash protection
 892     _crash_protection = NULL;
 893     _protected_thread = NULL;
 894     Thread::muxRelease(&_crash_mux);
 895     return true;
 896   }
 897   // this happens when we siglongjmp() back
 898   pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL);
 899   _crash_protection = NULL;
 900   _protected_thread = NULL;
 901   Thread::muxRelease(&_crash_mux);
 902   return false;
 903 }
 904 
 905 void os::ThreadCrashProtection::restore() {
 906   assert(_crash_protection != NULL, "must have crash protection");


 907   siglongjmp(_jmpbuf, 1);
 908 }
 909 
 910 void os::ThreadCrashProtection::check_crash_protection(int sig,
 911     Thread* thread) {
 912 
 913   if (thread != NULL &&
 914       thread == _protected_thread &&
 915       _crash_protection != NULL) {
 916 
 917     if (sig == SIGSEGV || sig == SIGBUS) {
 918       _crash_protection->restore();
 919     }
 920   }
 921 }
< prev index next >