1 /*
   2  * Copyright (c) 1997, 2019, 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 "jvm.h"
  27 #include "aot/aotLoader.hpp"
  28 #include "classfile/classLoader.hpp"
  29 #include "classfile/classLoaderDataGraph.hpp"
  30 #include "classfile/stringTable.hpp"
  31 #include "classfile/symbolTable.hpp"
  32 #include "classfile/systemDictionary.hpp"
  33 #include "code/codeCache.hpp"
  34 #include "compiler/compileBroker.hpp"
  35 #include "compiler/compilerOracle.hpp"
  36 #include "interpreter/bytecodeHistogram.hpp"
  37 #include "jfr/jfrEvents.hpp"
  38 #include "jfr/support/jfrThreadId.hpp"
  39 #if INCLUDE_JVMCI
  40 #include "jvmci/jvmci.hpp"
  41 #endif
  42 #include "logging/log.hpp"
  43 #include "logging/logStream.hpp"
  44 #include "memory/oopFactory.hpp"
  45 #include "memory/resourceArea.hpp"
  46 #include "memory/dynamicArchive.hpp"
  47 #include "memory/universe.hpp"
  48 #include "oops/constantPool.hpp"
  49 #include "oops/generateOopMap.hpp"
  50 #include "oops/instanceKlass.hpp"
  51 #include "oops/instanceOop.hpp"
  52 #include "oops/method.hpp"
  53 #include "oops/objArrayOop.hpp"
  54 #include "oops/oop.inline.hpp"
  55 #include "oops/symbol.hpp"
  56 #include "prims/jvmtiExport.hpp"
  57 #include "runtime/arguments.hpp"
  58 #include "runtime/biasedLocking.hpp"
  59 #include "runtime/compilationPolicy.hpp"
  60 #include "runtime/continuation.hpp"
  61 #include "runtime/deoptimization.hpp"
  62 #include "runtime/flags/flagSetting.hpp"
  63 #include "runtime/handles.inline.hpp"
  64 #include "runtime/init.hpp"
  65 #include "runtime/interfaceSupport.inline.hpp"
  66 #include "runtime/java.hpp"
  67 #include "runtime/memprofiler.hpp"
  68 #include "runtime/sharedRuntime.hpp"
  69 #include "runtime/statSampler.hpp"
  70 #include "runtime/sweeper.hpp"
  71 #include "runtime/task.hpp"
  72 #include "runtime/thread.inline.hpp"
  73 #include "runtime/timer.hpp"
  74 #include "runtime/vmOperations.hpp"
  75 #include "services/memTracker.hpp"
  76 #include "utilities/dtrace.hpp"
  77 #include "utilities/globalDefinitions.hpp"
  78 #include "utilities/histogram.hpp"
  79 #include "utilities/macros.hpp"
  80 #include "utilities/vmError.hpp"
  81 #ifdef COMPILER1
  82 #include "c1/c1_Compiler.hpp"
  83 #include "c1/c1_Runtime1.hpp"
  84 #endif
  85 #ifdef COMPILER2
  86 #include "code/compiledIC.hpp"
  87 #include "opto/compile.hpp"
  88 #include "opto/indexSet.hpp"
  89 #include "opto/runtime.hpp"
  90 #endif
  91 #if INCLUDE_JFR
  92 #include "jfr/jfr.hpp"
  93 #endif
  94 
  95 GrowableArray<Method*>* collected_profiled_methods;
  96 
  97 int compare_methods(Method** a, Method** b) {
  98   // %%% there can be 32-bit overflow here
  99   return ((*b)->invocation_count() + (*b)->compiled_invocation_count())
 100        - ((*a)->invocation_count() + (*a)->compiled_invocation_count());
 101 }
 102 
 103 void collect_profiled_methods(Method* m) {
 104   Thread* thread = Thread::current();
 105   methodHandle mh(thread, m);
 106   if ((m->method_data() != NULL) &&
 107       (PrintMethodData || CompilerOracle::should_print(mh))) {
 108     collected_profiled_methods->push(m);
 109   }
 110 }
 111 
 112 void print_method_profiling_data() {
 113   if (ProfileInterpreter COMPILER1_PRESENT(|| C1UpdateMethodData) &&
 114      (PrintMethodData || CompilerOracle::should_print_methods())) {
 115     ResourceMark rm;
 116     HandleMark hm;
 117     collected_profiled_methods = new GrowableArray<Method*>(1024);
 118     SystemDictionary::methods_do(collect_profiled_methods);
 119     collected_profiled_methods->sort(&compare_methods);
 120 
 121     int count = collected_profiled_methods->length();
 122     int total_size = 0;
 123     if (count > 0) {
 124       for (int index = 0; index < count; index++) {
 125         Method* m = collected_profiled_methods->at(index);
 126         ttyLocker ttyl;
 127         tty->print_cr("------------------------------------------------------------------------");
 128         m->print_invocation_count();
 129         tty->print_cr("  mdo size: %d bytes", m->method_data()->size_in_bytes());
 130         tty->cr();
 131         // Dump data on parameters if any
 132         if (m->method_data() != NULL && m->method_data()->parameters_type_data() != NULL) {
 133           tty->fill_to(2);
 134           m->method_data()->parameters_type_data()->print_data_on(tty);
 135         }
 136         m->print_codes();
 137         total_size += m->method_data()->size_in_bytes();
 138       }
 139       tty->print_cr("------------------------------------------------------------------------");
 140       tty->print_cr("Total MDO size: %d bytes", total_size);
 141     }
 142   }
 143 }
 144 
 145 
 146 #ifndef PRODUCT
 147 
 148 // Statistics printing (method invocation histogram)
 149 
 150 GrowableArray<Method*>* collected_invoked_methods;
 151 
 152 void collect_invoked_methods(Method* m) {
 153   if (m->invocation_count() + m->compiled_invocation_count() >= 1 ) {
 154     collected_invoked_methods->push(m);
 155   }
 156 }
 157 
 158 
 159 
 160 
 161 void print_method_invocation_histogram() {
 162   ResourceMark rm;
 163   HandleMark hm;
 164   collected_invoked_methods = new GrowableArray<Method*>(1024);
 165   SystemDictionary::methods_do(collect_invoked_methods);
 166   collected_invoked_methods->sort(&compare_methods);
 167   //
 168   tty->cr();
 169   tty->print_cr("Histogram Over Method Invocation Counters (cutoff = " INTX_FORMAT "):", MethodHistogramCutoff);
 170   tty->cr();
 171   tty->print_cr("____Count_(I+C)____Method________________________Module_________________");
 172   unsigned total = 0, int_total = 0, comp_total = 0, static_total = 0, final_total = 0,
 173       synch_total = 0, nativ_total = 0, acces_total = 0;
 174   for (int index = 0; index < collected_invoked_methods->length(); index++) {
 175     Method* m = collected_invoked_methods->at(index);
 176     int c = m->invocation_count() + m->compiled_invocation_count();
 177     if (c >= MethodHistogramCutoff) m->print_invocation_count();
 178     int_total  += m->invocation_count();
 179     comp_total += m->compiled_invocation_count();
 180     if (m->is_final())        final_total  += c;
 181     if (m->is_static())       static_total += c;
 182     if (m->is_synchronized()) synch_total  += c;
 183     if (m->is_native())       nativ_total  += c;
 184     if (m->is_accessor())     acces_total  += c;
 185   }
 186   tty->cr();
 187   total = int_total + comp_total;
 188   tty->print_cr("Invocations summary:");
 189   tty->print_cr("\t%9d (%4.1f%%) interpreted",  int_total,    100.0 * int_total    / total);
 190   tty->print_cr("\t%9d (%4.1f%%) compiled",     comp_total,   100.0 * comp_total   / total);
 191   tty->print_cr("\t%9d (100%%)  total",         total);
 192   tty->print_cr("\t%9d (%4.1f%%) synchronized", synch_total,  100.0 * synch_total  / total);
 193   tty->print_cr("\t%9d (%4.1f%%) final",        final_total,  100.0 * final_total  / total);
 194   tty->print_cr("\t%9d (%4.1f%%) static",       static_total, 100.0 * static_total / total);
 195   tty->print_cr("\t%9d (%4.1f%%) native",       nativ_total,  100.0 * nativ_total  / total);
 196   tty->print_cr("\t%9d (%4.1f%%) accessor",     acces_total,  100.0 * acces_total  / total);
 197   tty->cr();
 198   SharedRuntime::print_call_statistics(comp_total);
 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 
 208 // General statistics printing (profiling ...)
 209 void print_statistics() {
 210 #ifdef ASSERT
 211 
 212   if (CountRuntimeCalls) {
 213     extern Histogram *RuntimeHistogram;
 214     RuntimeHistogram->print();
 215   }
 216 
 217   if (CountJNICalls) {
 218     extern Histogram *JNIHistogram;
 219     JNIHistogram->print();
 220   }
 221 
 222   if (CountJVMCalls) {
 223     extern Histogram *JVMHistogram;
 224     JVMHistogram->print();
 225   }
 226 
 227 #endif
 228 
 229   if (MemProfiling) {
 230     MemProfiler::disengage();
 231   }
 232 
 233   if (CITime) {
 234     CompileBroker::print_times();
 235   }
 236 
 237 #ifdef COMPILER1
 238   if ((PrintC1Statistics || LogVMOutput || LogCompilation) && UseCompiler) {
 239     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintC1Statistics);
 240     Runtime1::print_statistics();
 241     Deoptimization::print_statistics();
 242     SharedRuntime::print_statistics();
 243   }
 244 #endif /* COMPILER1 */
 245 
 246 #ifdef COMPILER2
 247   if ((PrintOptoStatistics || LogVMOutput || LogCompilation) && UseCompiler) {
 248     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintOptoStatistics);
 249     Compile::print_statistics();
 250 #ifndef COMPILER1
 251     Deoptimization::print_statistics();
 252     SharedRuntime::print_statistics();
 253 #endif //COMPILER1
 254     os::print_statistics();
 255   }
 256 
 257   if (PrintLockStatistics || PrintPreciseBiasedLockingStatistics || PrintPreciseRTMLockingStatistics) {
 258     OptoRuntime::print_named_counters();
 259   }
 260 #ifdef ASSERT
 261   if (CollectIndexSetStatistics) {
 262     IndexSet::print_statistics();
 263   }
 264 #endif // ASSERT
 265 #else // COMPILER2
 266 #if INCLUDE_JVMCI
 267 #ifndef COMPILER1
 268   if ((TraceDeoptimization || LogVMOutput || LogCompilation) && UseCompiler) {
 269     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && TraceDeoptimization);
 270     Deoptimization::print_statistics();
 271     SharedRuntime::print_statistics();
 272   }
 273 #endif // COMPILER1
 274 #endif // INCLUDE_JVMCI
 275 #endif // COMPILER2
 276 
 277   if (PrintAOTStatistics) {
 278     AOTLoader::print_statistics();
 279   }
 280 
 281   if (PrintNMethodStatistics) {
 282     nmethod::print_statistics();
 283   }
 284   if (CountCompiledCalls) {
 285     print_method_invocation_histogram();
 286   }
 287 
 288   print_method_profiling_data();
 289 
 290   if (TimeOopMap) {
 291     GenerateOopMap::print_time();
 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     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 305     CodeCache::print();
 306   }
 307 
 308   // CodeHeap State Analytics.
 309   // Does also call NMethodSweeper::print(tty)
 310   if (PrintCodeHeapAnalytics) {
 311     CompileBroker::print_heapinfo(NULL, "all", 4096); // details
 312   } else if (PrintMethodFlushingStatistics) {
 313     NMethodSweeper::print(tty);
 314   }
 315 
 316   if (PrintCodeCache2) {
 317     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 318     CodeCache::print_internals();
 319   }
 320 
 321   if (PrintVtableStats) {
 322     klassVtable::print_statistics();
 323     klassItable::print_statistics();
 324   }
 325   if (VerifyOops && Verbose) {
 326     tty->print_cr("+VerifyOops count: %d", StubRoutines::verify_oop_count());
 327   }
 328 
 329   print_bytecode_count();
 330 
 331   if (PrintSystemDictionaryAtExit) {
 332     ResourceMark rm;
 333     MutexLocker mcld(ClassLoaderDataGraph_lock);
 334     SystemDictionary::print();
 335     ClassLoaderDataGraph::print();
 336   }
 337 
 338   if (LogTouchedMethods && PrintTouchedMethodsAtExit) {
 339     Method::print_touched_methods(tty);
 340   }
 341 
 342   if (PrintBiasedLockingStatistics) {
 343     BiasedLocking::print_counters();
 344   }
 345 
 346   // Native memory tracking data
 347   if (PrintNMTStatistics) {
 348     MemTracker::final_report(tty);
 349   }
 350 
 351   ThreadsSMRSupport::log_statistics();
 352 }
 353 
 354 #else // PRODUCT MODE STATISTICS
 355 
 356 void print_statistics() {
 357 
 358   if (UseNewCode2) {
 359     Continuations::print_statistics();
 360   }
 361 
 362   if (PrintMethodData) {
 363     print_method_profiling_data();
 364   }
 365 
 366   if (CITime) {
 367     CompileBroker::print_times();
 368   }
 369 
 370   if (PrintCodeCache) {
 371     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 372     CodeCache::print();
 373   }
 374 
 375   // CodeHeap State Analytics.
 376   // Does also call NMethodSweeper::print(tty)
 377   if (PrintCodeHeapAnalytics) {
 378     CompileBroker::print_heapinfo(NULL, "all", 4096); // details
 379   } else if (PrintMethodFlushingStatistics) {
 380     NMethodSweeper::print(tty);
 381   }
 382 
 383 #ifdef COMPILER2
 384   if (PrintPreciseBiasedLockingStatistics || PrintPreciseRTMLockingStatistics) {
 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     MemTracker::final_report(tty);
 395   }
 396 
 397   if (LogTouchedMethods && PrintTouchedMethodsAtExit) {
 398     Method::print_touched_methods(tty);
 399   }
 400 
 401   ThreadsSMRSupport::log_statistics();
 402 }
 403 
 404 #endif
 405 
 406 // Note: before_exit() can be executed only once, if more than one threads
 407 //       are trying to shutdown the VM at the same time, only one thread
 408 //       can run before_exit() and all other threads must wait.
 409 void before_exit(JavaThread* thread) {
 410   #define BEFORE_EXIT_NOT_RUN 0
 411   #define BEFORE_EXIT_RUNNING 1
 412   #define BEFORE_EXIT_DONE    2
 413   static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;
 414 
 415   // Note: don't use a Mutex to guard the entire before_exit(), as
 416   // JVMTI post_thread_end_event and post_vm_death_event will run native code.
 417   // A CAS or OSMutex would work just fine but then we need to manipulate
 418   // thread state for Safepoint. Here we use Monitor wait() and notify_all()
 419   // for synchronization.
 420   { MonitorLocker ml(BeforeExit_lock);
 421     switch (_before_exit_status) {
 422     case BEFORE_EXIT_NOT_RUN:
 423       _before_exit_status = BEFORE_EXIT_RUNNING;
 424       break;
 425     case BEFORE_EXIT_RUNNING:
 426       while (_before_exit_status == BEFORE_EXIT_RUNNING) {
 427         ml.wait();
 428       }
 429       assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
 430       return;
 431     case BEFORE_EXIT_DONE:
 432       // need block to avoid SS compiler bug
 433       {
 434         return;
 435       }
 436     }
 437   }
 438 
 439 #if INCLUDE_JVMCI
 440   JVMCI::shutdown();
 441 #endif
 442 
 443   // Hang forever on exit if we're reporting an error.
 444   if (ShowMessageBoxOnError && VMError::is_error_reported()) {
 445     os::infinite_sleep();
 446   }
 447 
 448   EventThreadEnd event;
 449   if (event.should_commit()) {
 450     event.set_thread(JFR_THREAD_ID(thread));
 451     event.commit();
 452   }
 453 
 454   JFR_ONLY(Jfr::on_vm_shutdown();)
 455 
 456   // Stop the WatcherThread. We do this before disenrolling various
 457   // PeriodicTasks to reduce the likelihood of races.
 458   if (PeriodicTask::num_tasks() > 0) {
 459     WatcherThread::stop();
 460   }
 461 
 462   // shut down the StatSampler task
 463   StatSampler::disengage();
 464   StatSampler::destroy();
 465 
 466   // Stop concurrent GC threads
 467   Universe::heap()->stop();
 468 
 469   // Print GC/heap related information.
 470   Log(gc, heap, exit) log;
 471   if (log.is_info()) {
 472     ResourceMark rm;
 473     LogStream ls_info(log.info());
 474     Universe::print_on(&ls_info);
 475     if (log.is_trace()) {
 476       LogStream ls_trace(log.trace());
 477       MutexLocker mcld(ClassLoaderDataGraph_lock);
 478       ClassLoaderDataGraph::print_on(&ls_trace);
 479     }
 480   }
 481 
 482   if (PrintBytecodeHistogram) {
 483     BytecodeHistogram::print();
 484   }
 485 
 486   if (JvmtiExport::should_post_thread_life()) {
 487     JvmtiExport::post_thread_end(thread);
 488   }
 489 
 490   // Always call even when there are not JVMTI environments yet, since environments
 491   // may be attached late and JVMTI must track phases of VM execution
 492   JvmtiExport::post_vm_death();
 493   Threads::shutdown_vm_agents();
 494 
 495   // Terminate the signal thread
 496   // Note: we don't wait until it actually dies.
 497   os::terminate_signal_thread();
 498 
 499 #if INCLUDE_CDS
 500   if (DynamicDumpSharedSpaces) {
 501     DynamicArchive::dump();
 502   }
 503 #endif
 504 
 505   print_statistics();
 506   Universe::heap()->print_tracing_info();
 507 
 508   { MutexLocker ml(BeforeExit_lock);
 509     _before_exit_status = BEFORE_EXIT_DONE;
 510     BeforeExit_lock->notify_all();
 511   }
 512 
 513   if (VerifyStringTableAtExit) {
 514     size_t fail_cnt = StringTable::verify_and_compare_entries();
 515     if (fail_cnt != 0) {
 516       tty->print_cr("ERROR: fail_cnt=" SIZE_FORMAT, fail_cnt);
 517       guarantee(fail_cnt == 0, "unexpected StringTable verification failures");
 518     }
 519   }
 520 
 521   #undef BEFORE_EXIT_NOT_RUN
 522   #undef BEFORE_EXIT_RUNNING
 523   #undef BEFORE_EXIT_DONE
 524 }
 525 
 526 void vm_exit(int code) {
 527   Thread* thread =
 528       ThreadLocalStorage::is_initialized() ? Thread::current_or_null() : NULL;
 529   if (thread == NULL) {
 530     // very early initialization failure -- just exit
 531     vm_direct_exit(code);
 532   }
 533 
 534   // We'd like to add an entry to the XML log to show that the VM is
 535   // terminating, but we can't safely do that here. The logic to make
 536   // XML termination logging safe is tied to the termination of the
 537   // VMThread, and it doesn't terminate on this exit path. See 8222534.
 538 
 539   if (VMThread::vm_thread() != NULL) {
 540     if (thread->is_Java_thread()) {
 541       // We must be "in_vm" for the code below to work correctly.
 542       // Historically there must have been some exit path for which
 543       // that was not the case and so we set it explicitly - even
 544       // though we no longer know what that path may be.
 545       ((JavaThread*)thread)->set_thread_state(_thread_in_vm);
 546     }
 547 
 548     // Fire off a VM_Exit operation to bring VM to a safepoint and exit
 549     VM_Exit op(code);
 550 
 551     // 4945125 The vm thread comes to a safepoint during exit.
 552     // GC vm_operations can get caught at the safepoint, and the
 553     // heap is unparseable if they are caught. Grab the Heap_lock
 554     // to prevent this. The GC vm_operations will not be able to
 555     // queue until after we release it, but we never do that as we
 556     // are terminating the VM process.
 557     MutexLocker ml(Heap_lock);
 558 
 559     VMThread::execute(&op);
 560     // should never reach here; but in case something wrong with VM Thread.
 561     vm_direct_exit(code);
 562   } else {
 563     // VM thread is gone, just exit
 564     vm_direct_exit(code);
 565   }
 566   ShouldNotReachHere();
 567 }
 568 
 569 void notify_vm_shutdown() {
 570   // For now, just a dtrace probe.
 571   HOTSPOT_VM_SHUTDOWN();
 572   HS_DTRACE_WORKAROUND_TAIL_CALL_BUG();
 573 }
 574 
 575 void vm_direct_exit(int code) {
 576   notify_vm_shutdown();
 577   os::wait_for_keypress_at_exit();
 578   os::exit(code);
 579 }
 580 
 581 void vm_perform_shutdown_actions() {
 582   if (is_init_completed()) {
 583     Thread* thread = Thread::current_or_null();
 584     if (thread != NULL && thread->is_Java_thread()) {
 585       // We are leaving the VM, set state to native (in case any OS exit
 586       // handlers call back to the VM)
 587       JavaThread* jt = (JavaThread*)thread;
 588       // Must always be walkable or have no last_Java_frame when in
 589       // thread_in_native
 590       jt->frame_anchor()->make_walkable(jt);
 591       jt->set_thread_state(_thread_in_native);
 592     }
 593   }
 594   notify_vm_shutdown();
 595 }
 596 
 597 void vm_shutdown()
 598 {
 599   vm_perform_shutdown_actions();
 600   os::wait_for_keypress_at_exit();
 601   os::shutdown();
 602 }
 603 
 604 void vm_abort(bool dump_core) {
 605   vm_perform_shutdown_actions();
 606   os::wait_for_keypress_at_exit();
 607 
 608   // Flush stdout and stderr before abort.
 609   fflush(stdout);
 610   fflush(stderr);
 611 
 612   os::abort(dump_core);
 613   ShouldNotReachHere();
 614 }
 615 
 616 void vm_notify_during_cds_dumping(const char* error, const char* message) {
 617   if (error != NULL) {
 618     tty->print_cr("Error occurred during CDS dumping");
 619     tty->print("%s", error);
 620     if (message != NULL) {
 621       tty->print_cr(": %s", message);
 622     }
 623     else {
 624       tty->cr();
 625     }
 626   }
 627 }
 628 
 629 void vm_exit_during_cds_dumping(const char* error, const char* message) {
 630   vm_notify_during_cds_dumping(error, message);
 631 
 632   // Failure during CDS dumping, we don't want to dump core
 633   vm_abort(false);
 634 }
 635 
 636 void vm_notify_during_shutdown(const char* error, const char* message) {
 637   if (error != NULL) {
 638     tty->print_cr("Error occurred during initialization of VM");
 639     tty->print("%s", error);
 640     if (message != NULL) {
 641       tty->print_cr(": %s", message);
 642     }
 643     else {
 644       tty->cr();
 645     }
 646   }
 647   if (ShowMessageBoxOnError && WizardMode) {
 648     fatal("Error occurred during initialization of VM");
 649   }
 650 }
 651 
 652 void vm_exit_during_initialization() {
 653   vm_notify_during_shutdown(NULL, NULL);
 654 
 655   // Failure during initialization, we don't want to dump core
 656   vm_abort(false);
 657 }
 658 
 659 void vm_exit_during_initialization(Handle exception) {
 660   tty->print_cr("Error occurred during initialization of VM");
 661   // If there are exceptions on this thread it must be cleared
 662   // first and here. Any future calls to EXCEPTION_MARK requires
 663   // that no pending exceptions exist.
 664   Thread *THREAD = Thread::current(); // can't be NULL
 665   if (HAS_PENDING_EXCEPTION) {
 666     CLEAR_PENDING_EXCEPTION;
 667   }
 668   java_lang_Throwable::print_stack_trace(exception, tty);
 669   tty->cr();
 670   vm_notify_during_shutdown(NULL, NULL);
 671 
 672   // Failure during initialization, we don't want to dump core
 673   vm_abort(false);
 674 }
 675 
 676 void vm_exit_during_initialization(Symbol* ex, const char* message) {
 677   ResourceMark rm;
 678   vm_notify_during_shutdown(ex->as_C_string(), message);
 679 
 680   // Failure during initialization, we don't want to dump core
 681   vm_abort(false);
 682 }
 683 
 684 void vm_exit_during_initialization(const char* error, const char* message) {
 685   vm_notify_during_shutdown(error, message);
 686 
 687   // Failure during initialization, we don't want to dump core
 688   vm_abort(false);
 689 }
 690 
 691 void vm_shutdown_during_initialization(const char* error, const char* message) {
 692   vm_notify_during_shutdown(error, message);
 693   vm_shutdown();
 694 }
 695 
 696 JDK_Version JDK_Version::_current;
 697 const char* JDK_Version::_runtime_name;
 698 const char* JDK_Version::_runtime_version;
 699 
 700 void JDK_Version::initialize() {
 701   jdk_version_info info;
 702   assert(!_current.is_valid(), "Don't initialize twice");
 703 
 704   void *lib_handle = os::native_java_library();
 705   jdk_version_info_fn_t func = CAST_TO_FN_PTR(jdk_version_info_fn_t,
 706      os::dll_lookup(lib_handle, "JDK_GetVersionInfo0"));
 707 
 708   assert(func != NULL, "Support for JDK 1.5 or older has been removed after JEP-223");
 709 
 710   (*func)(&info, sizeof(info));
 711 
 712   int major = JDK_VERSION_MAJOR(info.jdk_version);
 713   int minor = JDK_VERSION_MINOR(info.jdk_version);
 714   int security = JDK_VERSION_SECURITY(info.jdk_version);
 715   int build = JDK_VERSION_BUILD(info.jdk_version);
 716 
 717   _current = JDK_Version(major, minor, security, info.patch_version, build);
 718 }
 719 
 720 void JDK_Version_init() {
 721   JDK_Version::initialize();
 722 }
 723 
 724 static int64_t encode_jdk_version(const JDK_Version& v) {
 725   return
 726     ((int64_t)v.major_version()          << (BitsPerByte * 4)) |
 727     ((int64_t)v.minor_version()          << (BitsPerByte * 3)) |
 728     ((int64_t)v.security_version()       << (BitsPerByte * 2)) |
 729     ((int64_t)v.patch_version()          << (BitsPerByte * 1)) |
 730     ((int64_t)v.build_number()           << (BitsPerByte * 0));
 731 }
 732 
 733 int JDK_Version::compare(const JDK_Version& other) const {
 734   assert(is_valid() && other.is_valid(), "Invalid version (uninitialized?)");
 735   uint64_t e = encode_jdk_version(*this);
 736   uint64_t o = encode_jdk_version(other);
 737   return (e > o) ? 1 : ((e == o) ? 0 : -1);
 738 }
 739 
 740 void JDK_Version::to_string(char* buffer, size_t buflen) const {
 741   assert(buffer && buflen > 0, "call with useful buffer");
 742   size_t index = 0;
 743 
 744   if (!is_valid()) {
 745     jio_snprintf(buffer, buflen, "%s", "(uninitialized)");
 746   } else {
 747     int rc = jio_snprintf(
 748         &buffer[index], buflen - index, "%d.%d", _major, _minor);
 749     if (rc == -1) return;
 750     index += rc;
 751     if (_security > 0) {
 752       rc = jio_snprintf(&buffer[index], buflen - index, ".%d", _security);
 753       if (rc == -1) return;
 754       index += rc;
 755     }
 756     if (_patch > 0) {
 757       rc = jio_snprintf(&buffer[index], buflen - index, ".%d", _patch);
 758       if (rc == -1) return;
 759       index += rc;
 760     }
 761     if (_build > 0) {
 762       rc = jio_snprintf(&buffer[index], buflen - index, "+%d", _build);
 763       if (rc == -1) return;
 764       index += rc;
 765     }
 766   }
 767 }