src/os/posix/vm/os_posix.cpp

Print this page


   1 /*
   2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
   3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4 *
   5 * This code is free software; you can redistribute it and/or modify it
   6 * under the terms of the GNU General Public License version 2 only, as
   7 * published by the Free Software Foundation.
   8 *
   9 * This code is distributed in the hope that it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12 * version 2 for more details (a copy is included in the LICENSE file that
  13 * accompanied this code).
  14 *
  15 * You should have received a copy of the GNU General Public License version
  16 * 2 along with this work; if not, write to the Free Software Foundation,
  17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18 *
  19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20 * or visit www.oracle.com if you need additional information or have any
  21 * questions.
  22 *
  23 */
  24 
  25 #include "prims/jvm.h"
  26 #include "runtime/frame.inline.hpp"

  27 #include "runtime/os.hpp"
  28 #include "utilities/vmError.hpp"
  29 
  30 #include <unistd.h>
  31 #include <sys/resource.h>
  32 #include <sys/utsname.h>
  33 #include <pthread.h>
  34 #include <signal.h>
  35 
  36 
  37 // Check core dump limit and report possible place where core can be found
  38 void os::check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize) {
  39   int n;
  40   struct rlimit rlim;
  41   bool success;
  42 
  43   n = get_core_path(buffer, bufferSize);
  44 
  45   if (getrlimit(RLIMIT_CORE, &rlim) != 0) {
  46     jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d (may not exist)", current_process_id());


 290       if (len <= (prefix_len + suffix_len)) {
 291         return NULL;
 292       }
 293       lib_name += prefix_len;
 294       name_len = strlen(lib_name) - suffix_len;
 295     }
 296   }
 297   len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;
 298   agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
 299   if (agent_entry_name == NULL) {
 300     return NULL;
 301   }
 302   strcpy(agent_entry_name, sym_name);
 303   if (lib_name != NULL) {
 304     strcat(agent_entry_name, "_");
 305     strncat(agent_entry_name, lib_name, name_len);
 306   }
 307   return agent_entry_name;
 308 }
 309 







































































































































 310 os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() {
 311   assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread");
 312 }
 313 
 314 /*
 315  * See the caveats for this class in os_posix.hpp
 316  * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
 317  * method and returns false. If none of the signals are raised, returns true.
 318  * The callback is supposed to provide the method that should be protected.
 319  */
 320 bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
 321   sigset_t saved_sig_mask;
 322 
 323   assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread");
 324   assert(!WatcherThread::watcher_thread()->has_crash_protection(),
 325       "crash_protection already set?");
 326 
 327   // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask
 328   // since on at least some systems (OS X) siglongjmp will restore the mask
 329   // for the process, not the thread


   1 /*
   2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
   3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4 *
   5 * This code is free software; you can redistribute it and/or modify it
   6 * under the terms of the GNU General Public License version 2 only, as
   7 * published by the Free Software Foundation.
   8 *
   9 * This code is distributed in the hope that it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12 * version 2 for more details (a copy is included in the LICENSE file that
  13 * accompanied this code).
  14 *
  15 * You should have received a copy of the GNU General Public License version
  16 * 2 along with this work; if not, write to the Free Software Foundation,
  17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18 *
  19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20 * or visit www.oracle.com if you need additional information or have any
  21 * questions.
  22 *
  23 */
  24 
  25 #include "prims/jvm.h"
  26 #include "runtime/frame.inline.hpp"
  27 #include "runtime/interfaceSupport.hpp"
  28 #include "runtime/os.hpp"
  29 #include "utilities/vmError.hpp"
  30 
  31 #include <unistd.h>
  32 #include <sys/resource.h>
  33 #include <sys/utsname.h>
  34 #include <pthread.h>
  35 #include <signal.h>
  36 
  37 
  38 // Check core dump limit and report possible place where core can be found
  39 void os::check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize) {
  40   int n;
  41   struct rlimit rlim;
  42   bool success;
  43 
  44   n = get_core_path(buffer, bufferSize);
  45 
  46   if (getrlimit(RLIMIT_CORE, &rlim) != 0) {
  47     jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d (may not exist)", current_process_id());


 291       if (len <= (prefix_len + suffix_len)) {
 292         return NULL;
 293       }
 294       lib_name += prefix_len;
 295       name_len = strlen(lib_name) - suffix_len;
 296     }
 297   }
 298   len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;
 299   agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
 300   if (agent_entry_name == NULL) {
 301     return NULL;
 302   }
 303   strcpy(agent_entry_name, sym_name);
 304   if (lib_name != NULL) {
 305     strcat(agent_entry_name, "_");
 306     strncat(agent_entry_name, lib_name, name_len);
 307   }
 308   return agent_entry_name;
 309 }
 310 
 311 int os::sleep(Thread* thread, jlong millis, bool interruptible) {
 312   assert(thread == Thread::current(),  "thread consistency check");
 313 
 314   ParkEvent * const slp = thread->_SleepEvent ;
 315   slp->reset() ;
 316   OrderAccess::fence() ;
 317 
 318   if (interruptible) {
 319     jlong prevtime = javaTimeNanos();
 320 
 321     for (;;) {
 322       if (os::is_interrupted(thread, true)) {
 323         return OS_INTRPT;
 324       }
 325 
 326       jlong newtime = javaTimeNanos();
 327 
 328       if (newtime - prevtime < 0) {
 329         // time moving backwards, should only happen if no monotonic clock
 330         if (os::supports_monotonic_clock()) {
 331           // not a guarantee() because JVM should not abort on kernel/glibc bugs
 332           warning("time moving backwards detected in os:sleep()");
 333           assert(true, "unexpected time moving backwards detected in os:sleep()");
 334         }
 335       } else {
 336         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
 337       }
 338 
 339       if (millis <= 0) {
 340         return OS_OK;
 341       }
 342 
 343       prevtime = newtime;
 344 
 345       {
 346         assert(thread->is_Java_thread(), "sanity check");
 347         JavaThread *jt = (JavaThread *) thread;
 348         ThreadBlockInVM tbivm(jt);
 349         OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
 350 
 351         jt->set_suspend_equivalent();
 352         // cleared by handle_special_suspend_equivalent_condition() or
 353         // java_suspend_self() via check_and_wait_while_suspended()
 354 
 355         slp->park(millis);
 356 
 357         // were we externally suspended while we were waiting?
 358         jt->check_and_wait_while_suspended();
 359       }
 360     }
 361   } else {
 362     OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
 363     jlong prevtime = javaTimeNanos();
 364 
 365     for (;;) {
 366       // It'd be nice to avoid the back-to-back javaTimeNanos() calls on
 367       // the 1st iteration ...
 368       jlong newtime = javaTimeNanos();
 369 
 370       if (newtime - prevtime < 0) {
 371         // time moving backwards, should only happen if no monotonic clock
 372         if (os::supports_monotonic_clock()) {
 373           // not a guarantee() because JVM should not abort on kernel/glibc bugs
 374           warning("time moving backwards detected on os::sleep()");
 375           assert(true, "unexpected time moving backwards detected on os::sleep()");
 376         }
 377       } else {
 378         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
 379       }
 380 
 381       if (millis <= 0) break ;
 382 
 383       prevtime = newtime;
 384       slp->park(millis);
 385     }
 386     return OS_OK ;
 387   }
 388 }
 389 
 390 ////////////////////////////////////////////////////////////////////////////////
 391 // interrupt support
 392 
 393 void os::interrupt(Thread* thread) {
 394   assert(Thread::current() == thread || Threads_lock->owned_by_self(),
 395     "possibility of dangling Thread pointer");
 396 
 397   OSThread* osthread = thread->osthread();
 398 
 399   if (!osthread->interrupted()) {
 400     osthread->set_interrupted(true);
 401     // More than one thread can get here with the same value of osthread,
 402     // resulting in multiple notifications.  We do, however, want the store
 403     // to interrupted() to be visible to other threads before we execute unpark().
 404     OrderAccess::fence();
 405     ParkEvent * const slp = thread->_SleepEvent ;
 406     if (slp != NULL) slp->unpark() ;
 407   }
 408 
 409   // For JSR166. Unpark even if interrupt status already was set
 410   if (thread->is_Java_thread())
 411     ((JavaThread*)thread)->parker()->unpark();
 412 
 413   ParkEvent * ev = thread->_ParkEvent ;
 414   if (ev != NULL) ev->unpark() ;
 415 
 416 }
 417 
 418 bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
 419   assert(Thread::current() == thread || Threads_lock->owned_by_self(),
 420     "possibility of dangling Thread pointer");
 421 
 422   OSThread* osthread = thread->osthread();
 423 
 424   bool interrupted = osthread->interrupted();
 425 
 426   // NOTE that since there is no "lock" around the interrupt and
 427   // is_interrupted operations, there is the possibility that the
 428   // interrupted flag (in osThread) will be "false" but that the
 429   // low-level events will be in the signaled state. This is
 430   // intentional. The effect of this is that Object.wait() and
 431   // LockSupport.park() will appear to have a spurious wakeup, which
 432   // is allowed and not harmful, and the possibility is so rare that
 433   // it is not worth the added complexity to add yet another lock.
 434   // For the sleep event an explicit reset is performed on entry
 435   // to os::sleep, so there is no early return. It has also been
 436   // recommended not to put the interrupted flag into the "event"
 437   // structure because it hides the issue. 
 438   if (interrupted && clear_interrupted) {
 439     osthread->set_interrupted(false);
 440     // consider thread->_SleepEvent->reset() ... optional optimization
 441   }
 442 
 443   return interrupted;
 444 }
 445 
 446 os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() {
 447   assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread");
 448 }
 449 
 450 /*
 451  * See the caveats for this class in os_posix.hpp
 452  * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
 453  * method and returns false. If none of the signals are raised, returns true.
 454  * The callback is supposed to provide the method that should be protected.
 455  */
 456 bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
 457   sigset_t saved_sig_mask;
 458 
 459   assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread");
 460   assert(!WatcherThread::watcher_thread()->has_crash_protection(),
 461       "crash_protection already set?");
 462 
 463   // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask
 464   // since on at least some systems (OS X) siglongjmp will restore the mask
 465   // for the process, not the thread