1 /*
   2  * Copyright (c) 1997, 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 "precompiled.hpp"
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/symbolTable.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "code/codeCache.hpp"
  30 #include "compiler/compileBroker.hpp"
  31 #include "compiler/compilerOracle.hpp"
  32 #include "interpreter/bytecodeHistogram.hpp"
  33 #include "memory/genCollectedHeap.hpp"
  34 #include "memory/oopFactory.hpp"
  35 #include "memory/universe.hpp"
  36 #include "oops/constantPool.hpp"
  37 #include "oops/generateOopMap.hpp"
  38 #include "oops/instanceKlass.hpp"
  39 #include "oops/instanceOop.hpp"
  40 #include "oops/method.hpp"
  41 #include "oops/objArrayOop.hpp"
  42 #include "oops/oop.inline.hpp"
  43 #include "oops/symbol.hpp"
  44 #include "prims/jvmtiExport.hpp"
  45 #include "runtime/arguments.hpp"
  46 #include "runtime/biasedLocking.hpp"
  47 #include "runtime/compilationPolicy.hpp"
  48 #include "runtime/fprofiler.hpp"
  49 #include "runtime/init.hpp"
  50 #include "runtime/interfaceSupport.hpp"
  51 #include "runtime/java.hpp"
  52 #include "runtime/memprofiler.hpp"
  53 #include "runtime/sharedRuntime.hpp"
  54 #include "runtime/statSampler.hpp"
  55 #include "runtime/sweeper.hpp"
  56 #include "runtime/task.hpp"
  57 #include "runtime/thread.inline.hpp"
  58 #include "runtime/timer.hpp"
  59 #include "runtime/vm_operations.hpp"
  60 #include "services/memReporter.hpp"
  61 #include "services/memTracker.hpp"
  62 #include "trace/tracing.hpp"
  63 #include "utilities/dtrace.hpp"
  64 #include "utilities/globalDefinitions.hpp"
  65 #include "utilities/histogram.hpp"
  66 #include "utilities/macros.hpp"
  67 #include "utilities/vmError.hpp"
  68 #ifdef TARGET_ARCH_x86
  69 # include "vm_version_x86.hpp"
  70 #endif
  71 #ifdef TARGET_ARCH_sparc
  72 # include "vm_version_sparc.hpp"
  73 #endif
  74 #ifdef TARGET_ARCH_zero
  75 # include "vm_version_zero.hpp"
  76 #endif
  77 #ifdef TARGET_ARCH_arm
  78 # include "vm_version_arm.hpp"
  79 #endif
  80 #ifdef TARGET_ARCH_ppc
  81 # include "vm_version_ppc.hpp"
  82 #endif
  83 #if INCLUDE_ALL_GCS
  84 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
  85 #include "gc_implementation/parallelScavenge/psScavenge.hpp"
  86 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
  87 #endif // INCLUDE_ALL_GCS
  88 #ifdef COMPILER1
  89 #include "c1/c1_Compiler.hpp"
  90 #include "c1/c1_Runtime1.hpp"
  91 #endif
  92 #ifdef COMPILER2
  93 #include "code/compiledIC.hpp"
  94 #include "compiler/methodLiveness.hpp"
  95 #include "opto/compile.hpp"
  96 #include "opto/indexSet.hpp"
  97 #include "opto/runtime.hpp"
  98 #endif
  99 
 100 #ifndef USDT2
 101 HS_DTRACE_PROBE_DECL(hotspot, vm__shutdown);
 102 #endif /* !USDT2 */
 103 
 104 #ifndef PRODUCT
 105 
 106 // Statistics printing (method invocation histogram)
 107 
 108 GrowableArray<Method*>* collected_invoked_methods;
 109 
 110 void collect_invoked_methods(Method* m) {
 111   if (m->invocation_count() + m->compiled_invocation_count() >= 1 ) {
 112     collected_invoked_methods->push(m);
 113   }
 114 }
 115 
 116 
 117 GrowableArray<Method*>* collected_profiled_methods;
 118 
 119 void collect_profiled_methods(Method* m) {
 120   Thread* thread = Thread::current();
 121   // This HandleMark prevents a huge amount of handles from being added
 122   // to the metadata_handles() array on the thread.
 123   HandleMark hm(thread);
 124   methodHandle mh(thread, m);
 125   if ((m->method_data() != NULL) &&
 126       (PrintMethodData || CompilerOracle::should_print(mh))) {
 127     collected_profiled_methods->push(m);
 128   }
 129 }
 130 
 131 
 132 int compare_methods(Method** a, Method** b) {
 133   // %%% there can be 32-bit overflow here
 134   return ((*b)->invocation_count() + (*b)->compiled_invocation_count())
 135        - ((*a)->invocation_count() + (*a)->compiled_invocation_count());
 136 }
 137 
 138 
 139 void print_method_invocation_histogram() {
 140   ResourceMark rm;
 141   HandleMark hm;
 142   collected_invoked_methods = new GrowableArray<Method*>(1024);
 143   SystemDictionary::methods_do(collect_invoked_methods);
 144   collected_invoked_methods->sort(&compare_methods);
 145   //
 146   tty->cr();
 147   tty->print_cr("Histogram Over MethodOop Invocation Counters (cutoff = %d):", MethodHistogramCutoff);
 148   tty->cr();
 149   tty->print_cr("____Count_(I+C)____Method________________________Module_________________");
 150   unsigned total = 0, int_total = 0, comp_total = 0, static_total = 0, final_total = 0,
 151       synch_total = 0, nativ_total = 0, acces_total = 0;
 152   for (int index = 0; index < collected_invoked_methods->length(); index++) {
 153     Method* m = collected_invoked_methods->at(index);
 154     int c = m->invocation_count() + m->compiled_invocation_count();
 155     if (c >= MethodHistogramCutoff) m->print_invocation_count();
 156     int_total  += m->invocation_count();
 157     comp_total += m->compiled_invocation_count();
 158     if (m->is_final())        final_total  += c;
 159     if (m->is_static())       static_total += c;
 160     if (m->is_synchronized()) synch_total  += c;
 161     if (m->is_native())       nativ_total  += c;
 162     if (m->is_accessor())     acces_total  += c;
 163   }
 164   tty->cr();
 165   total = int_total + comp_total;
 166   tty->print_cr("Invocations summary:");
 167   tty->print_cr("\t%9d (%4.1f%%) interpreted",  int_total,    100.0 * int_total    / total);
 168   tty->print_cr("\t%9d (%4.1f%%) compiled",     comp_total,   100.0 * comp_total   / total);
 169   tty->print_cr("\t%9d (100%%)  total",         total);
 170   tty->print_cr("\t%9d (%4.1f%%) synchronized", synch_total,  100.0 * synch_total  / total);
 171   tty->print_cr("\t%9d (%4.1f%%) final",        final_total,  100.0 * final_total  / total);
 172   tty->print_cr("\t%9d (%4.1f%%) static",       static_total, 100.0 * static_total / total);
 173   tty->print_cr("\t%9d (%4.1f%%) native",       nativ_total,  100.0 * nativ_total  / total);
 174   tty->print_cr("\t%9d (%4.1f%%) accessor",     acces_total,  100.0 * acces_total  / total);
 175   tty->cr();
 176   SharedRuntime::print_call_statistics(comp_total);
 177 }
 178 
 179 void print_method_profiling_data() {
 180   ResourceMark rm;
 181   HandleMark hm;
 182   collected_profiled_methods = new GrowableArray<Method*>(1024);
 183   SystemDictionary::methods_do(collect_profiled_methods);
 184   collected_profiled_methods->sort(&compare_methods);
 185 
 186   int count = collected_profiled_methods->length();
 187   if (count > 0) {
 188     for (int index = 0; index < count; index++) {
 189       Method* m = collected_profiled_methods->at(index);
 190       ttyLocker ttyl;
 191       tty->print_cr("------------------------------------------------------------------------");
 192       //m->print_name(tty);
 193       m->print_invocation_count();
 194       tty->cr();
 195       m->print_codes();
 196     }
 197     tty->print_cr("------------------------------------------------------------------------");
 198   }
 199 }
 200 
 201 void print_bytecode_count() {
 202   if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
 203     tty->print_cr("[BytecodeCounter::counter_value = %d]", BytecodeCounter::counter_value());
 204   }
 205 }
 206 
 207 AllocStats alloc_stats;
 208 
 209 
 210 
 211 // General statistics printing (profiling ...)
 212 void print_statistics() {
 213 #ifdef ASSERT
 214 
 215   if (CountRuntimeCalls) {
 216     extern Histogram *RuntimeHistogram;
 217     RuntimeHistogram->print();
 218   }
 219 
 220   if (CountJNICalls) {
 221     extern Histogram *JNIHistogram;
 222     JNIHistogram->print();
 223   }
 224 
 225   if (CountJVMCalls) {
 226     extern Histogram *JVMHistogram;
 227     JVMHistogram->print();
 228   }
 229 
 230 #endif
 231 
 232   if (MemProfiling) {
 233     MemProfiler::disengage();
 234   }
 235 
 236   if (CITime) {
 237     CompileBroker::print_times();
 238   }
 239 
 240 #ifdef COMPILER1
 241   if ((PrintC1Statistics || LogVMOutput || LogCompilation) && UseCompiler) {
 242     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintC1Statistics);
 243     Runtime1::print_statistics();
 244     Deoptimization::print_statistics();
 245     SharedRuntime::print_statistics();
 246     nmethod::print_statistics();
 247   }
 248 #endif /* COMPILER1 */
 249 
 250 #ifdef COMPILER2
 251   if ((PrintOptoStatistics || LogVMOutput || LogCompilation) && UseCompiler) {
 252     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintOptoStatistics);
 253     Compile::print_statistics();
 254 #ifndef COMPILER1
 255     Deoptimization::print_statistics();
 256     nmethod::print_statistics();
 257     SharedRuntime::print_statistics();
 258 #endif //COMPILER1
 259     os::print_statistics();
 260   }
 261 
 262   if (PrintLockStatistics || PrintPreciseBiasedLockingStatistics) {
 263     OptoRuntime::print_named_counters();
 264   }
 265 
 266   if (TimeLivenessAnalysis) {
 267     MethodLiveness::print_times();
 268   }
 269 #ifdef ASSERT
 270   if (CollectIndexSetStatistics) {
 271     IndexSet::print_statistics();
 272   }
 273 #endif // ASSERT
 274 #endif // COMPILER2
 275   if (CountCompiledCalls) {
 276     print_method_invocation_histogram();
 277   }
 278   if (ProfileInterpreter COMPILER1_PRESENT(|| C1UpdateMethodData)) {
 279     print_method_profiling_data();
 280   }
 281   if (TimeCompiler) {
 282     COMPILER2_PRESENT(Compile::print_timers();)
 283   }
 284   if (TimeCompilationPolicy) {
 285     CompilationPolicy::policy()->print_time();
 286   }
 287   if (TimeOopMap) {
 288     GenerateOopMap::print_time();
 289   }
 290   if (ProfilerCheckIntervals) {
 291     PeriodicTask::print_intervals();
 292   }
 293   if (PrintSymbolTableSizeHistogram) {
 294     SymbolTable::print_histogram();
 295   }
 296   if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
 297     BytecodeCounter::print();
 298   }
 299   if (PrintBytecodePairHistogram) {
 300     BytecodePairHistogram::print();
 301   }
 302 
 303   if (PrintCodeCache) {
 304     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 305     CodeCache::print();
 306   }
 307 
 308   if (PrintMethodFlushingStatistics) {
 309     NMethodSweeper::print();
 310   }
 311 
 312   if (PrintCodeCache2) {
 313     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 314     CodeCache::print_internals();
 315   }
 316 
 317   if (PrintClassStatistics) {
 318     SystemDictionary::print_class_statistics();
 319   }
 320   if (PrintMethodStatistics) {
 321     SystemDictionary::print_method_statistics();
 322   }
 323 
 324   if (PrintVtableStats) {
 325     klassVtable::print_statistics();
 326     klassItable::print_statistics();
 327   }
 328   if (VerifyOops) {
 329     tty->print_cr("+VerifyOops count: %d", StubRoutines::verify_oop_count());
 330   }
 331 
 332   print_bytecode_count();
 333   if (PrintMallocStatistics) {
 334     tty->print("allocation stats: ");
 335     alloc_stats.print();
 336     tty->cr();
 337   }
 338 
 339   if (PrintSystemDictionaryAtExit) {
 340     SystemDictionary::print();
 341   }
 342 
 343   if (PrintBiasedLockingStatistics) {
 344     BiasedLocking::print_counters();
 345   }
 346 
 347 #ifdef ENABLE_ZAP_DEAD_LOCALS
 348 #ifdef COMPILER2
 349   if (ZapDeadCompiledLocals) {
 350     tty->print_cr("Compile::CompiledZap_count = %d", Compile::CompiledZap_count);
 351     tty->print_cr("OptoRuntime::ZapDeadCompiledLocals_count = %d", OptoRuntime::ZapDeadCompiledLocals_count);
 352   }
 353 #endif // COMPILER2
 354 #endif // ENABLE_ZAP_DEAD_LOCALS
 355   // Native memory tracking data
 356   if (PrintNMTStatistics) {
 357     if (MemTracker::is_on()) {
 358       BaselineTTYOutputer outputer(tty);
 359       MemTracker::print_memory_usage(outputer, K, false);
 360     } else {
 361       tty->print_cr(MemTracker::reason());
 362     }
 363   }
 364 }
 365 
 366 #else // PRODUCT MODE STATISTICS
 367 
 368 void print_statistics() {
 369 
 370   if (CITime) {
 371     CompileBroker::print_times();
 372   }
 373 
 374   if (PrintCodeCache) {
 375     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 376     CodeCache::print();
 377   }
 378 
 379   if (PrintMethodFlushingStatistics) {
 380     NMethodSweeper::print();
 381   }
 382 
 383 #ifdef COMPILER2
 384   if (PrintPreciseBiasedLockingStatistics) {
 385     OptoRuntime::print_named_counters();
 386   }
 387 #endif
 388   if (PrintBiasedLockingStatistics) {
 389     BiasedLocking::print_counters();
 390   }
 391 
 392   // Native memory tracking data
 393   if (PrintNMTStatistics) {
 394     if (MemTracker::is_on()) {
 395       BaselineTTYOutputer outputer(tty);
 396       MemTracker::print_memory_usage(outputer, K, false);
 397     } else {
 398       tty->print_cr(MemTracker::reason());
 399     }
 400   }
 401 }
 402 
 403 #endif
 404 
 405 
 406 // Helper class for registering on_exit calls through JVM_OnExit
 407 
 408 extern "C" {
 409     typedef void (*__exit_proc)(void);
 410 }
 411 
 412 class ExitProc : public CHeapObj<mtInternal> {
 413  private:
 414   __exit_proc _proc;
 415   // void (*_proc)(void);
 416   ExitProc* _next;
 417  public:
 418   // ExitProc(void (*proc)(void)) {
 419   ExitProc(__exit_proc proc) {
 420     _proc = proc;
 421     _next = NULL;
 422   }
 423   void evaluate()               { _proc(); }
 424   ExitProc* next() const        { return _next; }
 425   void set_next(ExitProc* next) { _next = next; }
 426 };
 427 
 428 
 429 // Linked list of registered on_exit procedures
 430 
 431 static ExitProc* exit_procs = NULL;
 432 
 433 
 434 extern "C" {
 435   void register_on_exit_function(void (*func)(void)) {
 436     ExitProc *entry = new ExitProc(func);
 437     // Classic vm does not throw an exception in case the allocation failed,
 438     if (entry != NULL) {
 439       entry->set_next(exit_procs);
 440       exit_procs = entry;
 441     }
 442   }
 443 }
 444 
 445 // Note: before_exit() can be executed only once, if more than one threads
 446 //       are trying to shutdown the VM at the same time, only one thread
 447 //       can run before_exit() and all other threads must wait.
 448 void before_exit(JavaThread * thread) {
 449   #define BEFORE_EXIT_NOT_RUN 0
 450   #define BEFORE_EXIT_RUNNING 1
 451   #define BEFORE_EXIT_DONE    2
 452   static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;
 453 
 454   // Note: don't use a Mutex to guard the entire before_exit(), as
 455   // JVMTI post_thread_end_event and post_vm_death_event will run native code.
 456   // A CAS or OSMutex would work just fine but then we need to manipulate
 457   // thread state for Safepoint. Here we use Monitor wait() and notify_all()
 458   // for synchronization.
 459   { MutexLocker ml(BeforeExit_lock);
 460     switch (_before_exit_status) {
 461     case BEFORE_EXIT_NOT_RUN:
 462       _before_exit_status = BEFORE_EXIT_RUNNING;
 463       break;
 464     case BEFORE_EXIT_RUNNING:
 465       while (_before_exit_status == BEFORE_EXIT_RUNNING) {
 466         BeforeExit_lock->wait();
 467       }
 468       assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
 469       return;
 470     case BEFORE_EXIT_DONE:
 471       return;
 472     }
 473   }
 474 
 475   // The only difference between this and Win32's _onexit procs is that
 476   // this version is invoked before any threads get killed.
 477   ExitProc* current = exit_procs;
 478   while (current != NULL) {
 479     ExitProc* next = current->next();
 480     current->evaluate();
 481     delete current;
 482     current = next;
 483   }
 484 
 485   // Hang forever on exit if we're reporting an error.
 486   if (ShowMessageBoxOnError && is_error_reported()) {
 487     os::infinite_sleep();
 488   }
 489 
 490   // Terminate watcher thread - must before disenrolling any periodic task
 491   if (PeriodicTask::num_tasks() > 0)
 492     WatcherThread::stop();
 493 
 494   // Print statistics gathered (profiling ...)
 495   if (Arguments::has_profile()) {
 496     FlatProfiler::disengage();
 497     FlatProfiler::print(10);
 498   }
 499 
 500   // shut down the StatSampler task
 501   StatSampler::disengage();
 502   StatSampler::destroy();
 503 
 504   // We do not need to explicitly stop concurrent GC threads because the
 505   // JVM will be taken down at a safepoint when such threads are inactive --
 506   // except for some concurrent G1 threads, see (comment in)
 507   // Threads::destroy_vm().
 508 
 509   // Print GC/heap related information.
 510   if (PrintGCDetails) {
 511     Universe::print();
 512     AdaptiveSizePolicyOutput(0);
 513     if (Verbose) {
 514       ClassLoaderDataGraph::dump_on(gclog_or_tty);
 515     }
 516   }
 517 
 518   if (PrintBytecodeHistogram) {
 519     BytecodeHistogram::print();
 520   }
 521 
 522   if (JvmtiExport::should_post_thread_life()) {
 523     JvmtiExport::post_thread_end(thread);
 524   }
 525 
 526 
 527   EventThreadEnd event;
 528   if (event.should_commit()) {
 529       event.set_javalangthread(java_lang_Thread::thread_id(thread->threadObj()));
 530       event.commit();
 531   }
 532 
 533   // Always call even when there are not JVMTI environments yet, since environments
 534   // may be attached late and JVMTI must track phases of VM execution
 535   JvmtiExport::post_vm_death();
 536   Threads::shutdown_vm_agents();
 537 
 538   // Terminate the signal thread
 539   // Note: we don't wait until it actually dies.
 540   os::terminate_signal_thread();
 541 
 542   print_statistics();
 543   Universe::heap()->print_tracing_info();
 544 
 545   { MutexLocker ml(BeforeExit_lock);
 546     _before_exit_status = BEFORE_EXIT_DONE;
 547     BeforeExit_lock->notify_all();
 548   }
 549 
 550   // Shutdown NMT before exit. Otherwise,
 551   // it will run into trouble when system destroys static variables.
 552   MemTracker::shutdown(MemTracker::NMT_normal);
 553 
 554   #undef BEFORE_EXIT_NOT_RUN
 555   #undef BEFORE_EXIT_RUNNING
 556   #undef BEFORE_EXIT_DONE
 557 }
 558 
 559 void vm_exit(int code) {
 560   Thread* thread = ThreadLocalStorage::is_initialized() ?
 561     ThreadLocalStorage::get_thread_slow() : NULL;
 562   if (thread == NULL) {
 563     // we have serious problems -- just exit
 564     vm_direct_exit(code);
 565   }
 566 
 567   if (VMThread::vm_thread() != NULL) {
 568     // Fire off a VM_Exit operation to bring VM to a safepoint and exit
 569     VM_Exit op(code);
 570     if (thread->is_Java_thread())
 571       ((JavaThread*)thread)->set_thread_state(_thread_in_vm);
 572     VMThread::execute(&op);
 573     // should never reach here; but in case something wrong with VM Thread.
 574     vm_direct_exit(code);
 575   } else {
 576     // VM thread is gone, just exit
 577     vm_direct_exit(code);
 578   }
 579   ShouldNotReachHere();
 580 }
 581 
 582 void notify_vm_shutdown() {
 583   // For now, just a dtrace probe.
 584 #ifndef USDT2
 585   HS_DTRACE_PROBE(hotspot, vm__shutdown);
 586   HS_DTRACE_WORKAROUND_TAIL_CALL_BUG();
 587 #else /* USDT2 */
 588   HOTSPOT_VM_SHUTDOWN();
 589 #endif /* USDT2 */
 590 }
 591 
 592 void vm_direct_exit(int code) {
 593   notify_vm_shutdown();
 594   os::wait_for_keypress_at_exit();
 595   ::exit(code);
 596 }
 597 
 598 void vm_perform_shutdown_actions() {
 599   // Warning: do not call 'exit_globals()' here. All threads are still running.
 600   // Calling 'exit_globals()' will disable thread-local-storage and cause all
 601   // kinds of assertions to trigger in debug mode.
 602   if (is_init_completed()) {
 603     Thread* thread = ThreadLocalStorage::is_initialized() ?
 604                      ThreadLocalStorage::get_thread_slow() : NULL;
 605     if (thread != NULL && thread->is_Java_thread()) {
 606       // We are leaving the VM, set state to native (in case any OS exit
 607       // handlers call back to the VM)
 608       JavaThread* jt = (JavaThread*)thread;
 609       // Must always be walkable or have no last_Java_frame when in
 610       // thread_in_native
 611       jt->frame_anchor()->make_walkable(jt);
 612       jt->set_thread_state(_thread_in_native);
 613     }
 614   }
 615   notify_vm_shutdown();
 616 }
 617 
 618 void vm_shutdown()
 619 {
 620   vm_perform_shutdown_actions();
 621   os::wait_for_keypress_at_exit();
 622   os::shutdown();
 623 }
 624 
 625 void vm_abort(bool dump_core) {
 626   vm_perform_shutdown_actions();
 627   os::wait_for_keypress_at_exit();
 628   os::abort(dump_core);
 629   ShouldNotReachHere();
 630 }
 631 
 632 void vm_notify_during_shutdown(const char* error, const char* message) {
 633   if (error != NULL) {
 634     tty->print_cr("Error occurred during initialization of VM");
 635     tty->print("%s", error);
 636     if (message != NULL) {
 637       tty->print_cr(": %s", message);
 638     }
 639     else {
 640       tty->cr();
 641     }
 642   }
 643   if (ShowMessageBoxOnError && WizardMode) {
 644     fatal("Error occurred during initialization of VM");
 645   }
 646 }
 647 
 648 void vm_exit_during_initialization(Handle exception) {
 649   tty->print_cr("Error occurred during initialization of VM");
 650   // If there are exceptions on this thread it must be cleared
 651   // first and here. Any future calls to EXCEPTION_MARK requires
 652   // that no pending exceptions exist.
 653   Thread *THREAD = Thread::current();
 654   if (HAS_PENDING_EXCEPTION) {
 655     CLEAR_PENDING_EXCEPTION;
 656   }
 657   java_lang_Throwable::print(exception, tty);
 658   tty->cr();
 659   java_lang_Throwable::print_stack_trace(exception(), tty);
 660   tty->cr();
 661   vm_notify_during_shutdown(NULL, NULL);
 662 
 663   // Failure during initialization, we don't want to dump core
 664   vm_abort(false);
 665 }
 666 
 667 void vm_exit_during_initialization(Symbol* ex, const char* message) {
 668   ResourceMark rm;
 669   vm_notify_during_shutdown(ex->as_C_string(), message);
 670 
 671   // Failure during initialization, we don't want to dump core
 672   vm_abort(false);
 673 }
 674 
 675 void vm_exit_during_initialization(const char* error, const char* message) {
 676   vm_notify_during_shutdown(error, message);
 677 
 678   // Failure during initialization, we don't want to dump core
 679   vm_abort(false);
 680 }
 681 
 682 void vm_shutdown_during_initialization(const char* error, const char* message) {
 683   vm_notify_during_shutdown(error, message);
 684   vm_shutdown();
 685 }
 686 
 687 JDK_Version JDK_Version::_current;
 688 const char* JDK_Version::_runtime_name;
 689 const char* JDK_Version::_runtime_version;
 690 
 691 void JDK_Version::initialize() {
 692   jdk_version_info info;
 693   assert(!_current.is_valid(), "Don't initialize twice");
 694 
 695   void *lib_handle = os::native_java_library();
 696   jdk_version_info_fn_t func = CAST_TO_FN_PTR(jdk_version_info_fn_t,
 697      os::dll_lookup(lib_handle, "JDK_GetVersionInfo0"));
 698 
 699   if (func == NULL) {
 700     // JDK older than 1.6
 701     _current._partially_initialized = true;
 702   } else {
 703     (*func)(&info, sizeof(info));
 704 
 705     int major = JDK_VERSION_MAJOR(info.jdk_version);
 706     int minor = JDK_VERSION_MINOR(info.jdk_version);
 707     int micro = JDK_VERSION_MICRO(info.jdk_version);
 708     int build = JDK_VERSION_BUILD(info.jdk_version);
 709     if (major == 1 && minor > 4) {
 710       // We represent "1.5.0" as "5.0", but 1.4.2 as itself.
 711       major = minor;
 712       minor = micro;
 713       micro = 0;
 714     }
 715     _current = JDK_Version(major, minor, micro, info.update_version,
 716                            info.special_update_version, build,
 717                            info.thread_park_blocker == 1,
 718                            info.post_vm_init_hook_enabled == 1,
 719                            info.pending_list_uses_discovered_field == 1);
 720   }
 721 }
 722 
 723 void JDK_Version::fully_initialize(
 724     uint8_t major, uint8_t minor, uint8_t micro, uint8_t update) {
 725   // This is only called when current is less than 1.6 and we've gotten
 726   // far enough in the initialization to determine the exact version.
 727   assert(major < 6, "not needed for JDK version >= 6");
 728   assert(is_partially_initialized(), "must not initialize");
 729   if (major < 5) {
 730     // JDK verison sequence: 1.2.x, 1.3.x, 1.4.x, 5.0.x, 6.0.x, etc.
 731     micro = minor;
 732     minor = major;
 733     major = 1;
 734   }
 735   _current = JDK_Version(major, minor, micro, update);
 736 }
 737 
 738 void JDK_Version_init() {
 739   JDK_Version::initialize();
 740 }
 741 
 742 static int64_t encode_jdk_version(const JDK_Version& v) {
 743   return
 744     ((int64_t)v.major_version()          << (BitsPerByte * 5)) |
 745     ((int64_t)v.minor_version()          << (BitsPerByte * 4)) |
 746     ((int64_t)v.micro_version()          << (BitsPerByte * 3)) |
 747     ((int64_t)v.update_version()         << (BitsPerByte * 2)) |
 748     ((int64_t)v.special_update_version() << (BitsPerByte * 1)) |
 749     ((int64_t)v.build_number()           << (BitsPerByte * 0));
 750 }
 751 
 752 int JDK_Version::compare(const JDK_Version& other) const {
 753   assert(is_valid() && other.is_valid(), "Invalid version (uninitialized?)");
 754   if (!is_partially_initialized() && other.is_partially_initialized()) {
 755     return -(other.compare(*this)); // flip the comparators
 756   }
 757   assert(!other.is_partially_initialized(), "Not initialized yet");
 758   if (is_partially_initialized()) {
 759     assert(other.major_version() >= 6,
 760            "Invalid JDK version comparison during initialization");
 761     return -1;
 762   } else {
 763     uint64_t e = encode_jdk_version(*this);
 764     uint64_t o = encode_jdk_version(other);
 765     return (e > o) ? 1 : ((e == o) ? 0 : -1);
 766   }
 767 }
 768 
 769 void JDK_Version::to_string(char* buffer, size_t buflen) const {
 770   size_t index = 0;
 771   if (!is_valid()) {
 772     jio_snprintf(buffer, buflen, "%s", "(uninitialized)");
 773   } else if (is_partially_initialized()) {
 774     jio_snprintf(buffer, buflen, "%s", "(uninitialized) pre-1.6.0");
 775   } else {
 776     index += jio_snprintf(
 777         &buffer[index], buflen - index, "%d.%d", _major, _minor);
 778     if (_micro > 0) {
 779       index += jio_snprintf(&buffer[index], buflen - index, ".%d", _micro);
 780     }
 781     if (_update > 0) {
 782       index += jio_snprintf(&buffer[index], buflen - index, "_%02d", _update);
 783     }
 784     if (_special > 0) {
 785       index += jio_snprintf(&buffer[index], buflen - index, "%c", _special);
 786     }
 787     if (_build > 0) {
 788       index += jio_snprintf(&buffer[index], buflen - index, "-b%02d", _build);
 789     }
 790   }
 791 }