1 /*
  2  * Copyright (c) 1997, 2026, 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 "cds/aotMetaspace.hpp"
 26 #include "cds/cds_globals.hpp"
 27 #include "cds/cdsConfig.hpp"
 28 #include "cds/classListWriter.hpp"
 29 #include "cds/dynamicArchive.hpp"

 30 #include "classfile/classLoader.hpp"
 31 #include "classfile/classLoaderDataGraph.hpp"
 32 #include "classfile/javaClasses.hpp"
 33 #include "classfile/stringTable.hpp"
 34 #include "classfile/symbolTable.hpp"
 35 #include "classfile/systemDictionary.hpp"

 36 #include "code/codeCache.hpp"
 37 #include "compiler/compilationMemoryStatistic.hpp"
 38 #include "compiler/compilationPolicy.hpp"
 39 #include "compiler/compileBroker.hpp"
 40 #include "compiler/compilerOracle.hpp"
 41 #include "gc/shared/collectedHeap.hpp"
 42 #include "gc/shared/stringdedup/stringDedup.hpp"
 43 #include "interpreter/bytecodeHistogram.hpp"

 44 #include "jfr/jfrEvents.hpp"
 45 #include "jfr/support/jfrThreadId.hpp"
 46 #include "jvm.h"
 47 #include "logging/log.hpp"
 48 #include "logging/logStream.hpp"
 49 #include "memory/metaspaceUtils.hpp"
 50 #include "memory/oopFactory.hpp"
 51 #include "memory/resourceArea.hpp"
 52 #include "memory/universe.hpp"
 53 #include "nmt/memMapPrinter.hpp"
 54 #include "nmt/memTracker.hpp"
 55 #include "oops/constantPool.hpp"
 56 #include "oops/generateOopMap.hpp"
 57 #include "oops/instanceKlass.hpp"
 58 #include "oops/instanceOop.hpp"
 59 #include "oops/klassVtable.hpp"
 60 #include "oops/method.inline.hpp"
 61 #include "oops/objArrayOop.hpp"
 62 #include "oops/oop.inline.hpp"
 63 #include "oops/symbol.hpp"

 64 #include "prims/jvmtiAgentList.hpp"
 65 #include "prims/jvmtiExport.hpp"

 66 #include "runtime/continuation.hpp"
 67 #include "runtime/deoptimization.hpp"
 68 #include "runtime/flags/flagSetting.hpp"

 69 #include "runtime/handles.inline.hpp"
 70 #include "runtime/init.hpp"
 71 #include "runtime/interfaceSupport.inline.hpp"
 72 #include "runtime/java.hpp"
 73 #include "runtime/javaThread.hpp"
 74 #include "runtime/os.hpp"
 75 #include "runtime/sharedRuntime.hpp"
 76 #include "runtime/stubRoutines.hpp"
 77 #include "runtime/task.hpp"
 78 #include "runtime/threads.hpp"
 79 #include "runtime/timer.hpp"
 80 #include "runtime/trimNativeHeap.hpp"
 81 #include "runtime/vm_version.hpp"
 82 #include "runtime/vmOperations.hpp"
 83 #include "runtime/vmThread.hpp"
 84 #include "sanitizers/leak.hpp"
 85 #include "utilities/dtrace.hpp"
 86 #include "utilities/events.hpp"
 87 #include "utilities/globalDefinitions.hpp"
 88 #include "utilities/macros.hpp"
 89 #include "utilities/vmError.hpp"
 90 #ifdef COMPILER1
 91 #include "c1/c1_Compiler.hpp"
 92 #include "c1/c1_Runtime1.hpp"
 93 #endif
 94 #ifdef COMPILER2
 95 #include "code/compiledIC.hpp"
 96 #include "opto/compile.hpp"
 97 #include "opto/indexSet.hpp"
 98 #include "opto/runtime.hpp"
 99 #endif
100 #if INCLUDE_JFR
101 #include "jfr/jfr.hpp"
102 #endif
103 #if INCLUDE_JVMCI
104 #include "jvmci/jvmci.hpp"
105 #endif
106 
107 GrowableArray<Method*>* collected_profiled_methods;
108 
109 static int compare_methods(Method** a, Method** b) {
110   // compiled_invocation_count() returns int64_t, forcing the entire expression
111   // to be evaluated as int64_t. Overflow is not an issue.
112   int64_t diff = (((*b)->invocation_count() + (*b)->compiled_invocation_count())
113                 - ((*a)->invocation_count() + (*a)->compiled_invocation_count()));
114   return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
115 }
116 
117 static void collect_profiled_methods(Method* m) {
118   Thread* thread = Thread::current();
119   methodHandle mh(thread, m);
120   if ((m->method_data() != nullptr) &&
121       (PrintMethodData || CompilerOracle::should_print(mh))) {
122     collected_profiled_methods->push(m);
123   }
124 }
125 
126 static void print_method_profiling_data() {
127   if ((ProfileInterpreter COMPILER1_PRESENT(|| C1UpdateMethodData)) &&
128      (PrintMethodData || CompilerOracle::should_print_methods())) {
129     ResourceMark rm;
130     collected_profiled_methods = new GrowableArray<Method*>(1024);
131     SystemDictionary::methods_do(collect_profiled_methods);
132     collected_profiled_methods->sort(&compare_methods);
133 
134     int count = collected_profiled_methods->length();
135     int total_size = 0;
136     if (count > 0) {
137       for (int index = 0; index < count; index++) {
138         Method* m = collected_profiled_methods->at(index);
139 
140         // Instead of taking tty lock, we collect all lines into a string stream
141         // and then print them all at once.
142         ResourceMark rm2;
143         stringStream ss;
144 
145         ss.print_cr("------------------------------------------------------------------------");
146         m->print_invocation_count(&ss);
147         ss.print_cr("  mdo size: %d bytes", m->method_data()->size_in_bytes());
148         ss.cr();
149         // Dump data on parameters if any
150         if (m->method_data() != nullptr && m->method_data()->parameters_type_data() != nullptr) {
151           ss.fill_to(2);
152           m->method_data()->parameters_type_data()->print_data_on(&ss);
153         }
154         m->print_codes_on(&ss);
155         tty->print("%s", ss.as_string()); // print all at once
156         total_size += m->method_data()->size_in_bytes();
157       }
158       tty->print_cr("------------------------------------------------------------------------");
159       tty->print_cr("Total MDO size: %d bytes", total_size);
160     }
161   }
162 }
163 


















































































164 #ifndef PRODUCT
165 
166 // Statistics printing (method invocation histogram)
167 
168 GrowableArray<Method*>* collected_invoked_methods;
169 
170 static void collect_invoked_methods(Method* m) {
171   if (m->invocation_count() + m->compiled_invocation_count() >= 1) {
172     collected_invoked_methods->push(m);
173   }
174 }
175 
176 
177 // Invocation count accumulators should be unsigned long to shift the
178 // overflow border. Longer-running workloads tend to create invocation
179 // counts which already overflow 32-bit counters for individual methods.
180 static void print_method_invocation_histogram() {
181   ResourceMark rm;
182   collected_invoked_methods = new GrowableArray<Method*>(1024);
183   SystemDictionary::methods_do(collect_invoked_methods);
184   collected_invoked_methods->sort(&compare_methods);
185   //
186   tty->cr();
187   tty->print_cr("Histogram Over Method Invocation Counters (cutoff = %zd):", MethodHistogramCutoff);
188   tty->cr();
189   tty->print_cr("____Count_(I+C)____Method________________________Module_________________");
190   uint64_t total        = 0,
191            int_total    = 0,
192            comp_total   = 0,
193            special_total= 0,
194            static_total = 0,
195            final_total  = 0,
196            synch_total  = 0,
197            native_total = 0,
198            access_total = 0;
199   for (int index = 0; index < collected_invoked_methods->length(); index++) {
200     // Counter values returned from getter methods are signed int.
201     // To shift the overflow border by a factor of two, we interpret
202     // them here as unsigned long. A counter can't be negative anyway.
203     Method* m = collected_invoked_methods->at(index);
204     uint64_t iic = (uint64_t)m->invocation_count();
205     uint64_t cic = (uint64_t)m->compiled_invocation_count();
206     if ((iic + cic) >= (uint64_t)MethodHistogramCutoff) m->print_invocation_count(tty);
207     int_total  += iic;
208     comp_total += cic;
209     if (m->is_final())        final_total  += iic + cic;
210     if (m->is_static())       static_total += iic + cic;
211     if (m->is_synchronized()) synch_total  += iic + cic;
212     if (m->is_native())       native_total += iic + cic;
213     if (m->is_accessor())     access_total += iic + cic;
214   }
215   tty->cr();
216   total = int_total + comp_total;
217   special_total = final_total + static_total +synch_total + native_total + access_total;
218   tty->print_cr("Invocations summary for %d methods:", collected_invoked_methods->length());
219   double total_div = (double)total;
220   tty->print_cr("\t" UINT64_FORMAT_W(12) " (100%%)  total",           total);
221   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%) |- interpreted", int_total,     100.0 * (double)int_total    / total_div);
222   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%) |- compiled",    comp_total,    100.0 * (double)comp_total   / total_div);
223   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%) |- special methods (interpreted and compiled)",
224                                                                          special_total, 100.0 * (double)special_total/ total_div);
225   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- synchronized",synch_total,   100.0 * (double)synch_total  / total_div);
226   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- final",       final_total,   100.0 * (double)final_total  / total_div);
227   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- static",      static_total,  100.0 * (double)static_total / total_div);
228   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- native",      native_total,  100.0 * (double)native_total / total_div);
229   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- accessor",    access_total,  100.0 * (double)access_total / total_div);
230   tty->cr();
231   SharedRuntime::print_call_statistics(comp_total);
232 }
233 
234 static void print_bytecode_count() {
235   if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
236     tty->print_cr("[BytecodeCounter::counter_value = %zu]", BytecodeCounter::counter_value());
237   }
238 }
239 
240 #else
241 
242 static void print_method_invocation_histogram() {}
243 static void print_bytecode_count() {}
244 
245 #endif // PRODUCT
246 
247 
248 // General statistics printing (profiling ...)
249 void print_statistics() {





250   if (CITime) {
251     CompileBroker::print_times();
252   }
253 
254 #ifdef COMPILER1
255   if ((PrintC1Statistics || LogVMOutput || LogCompilation) && UseCompiler) {
256     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintC1Statistics);
257     Runtime1::print_statistics();
258     SharedRuntime::print_statistics();
259   }
260 #endif /* COMPILER1 */
261 
262 #ifdef COMPILER2
263   if ((PrintOptoStatistics || LogVMOutput || LogCompilation) && UseCompiler) {
264     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintOptoStatistics);
265     Compile::print_statistics();
266     Deoptimization::print_statistics();
267 #ifndef COMPILER1
268     SharedRuntime::print_statistics();
269 #endif //COMPILER1
270   }
271 
272   if (PrintLockStatistics) {
273     OptoRuntime::print_named_counters();
274   }
275 #ifdef ASSERT
276   if (CollectIndexSetStatistics) {
277     IndexSet::print_statistics();
278   }
279 #endif // ASSERT
280 #else // COMPILER2
281 #if INCLUDE_JVMCI
282 #ifndef COMPILER1
283   if ((TraceDeoptimization || LogVMOutput || LogCompilation) && UseCompiler) {
284     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && TraceDeoptimization);
285     Deoptimization::print_statistics();
286     SharedRuntime::print_statistics();
287   }
288 #endif // COMPILER1
289 #endif // INCLUDE_JVMCI
290 #endif // COMPILER2
291 
292   if (PrintNMethodStatistics) {
293     nmethod::print_statistics();
294   }
295   if (CountCompiledCalls) {
296     print_method_invocation_histogram();
297   }
298 
299   print_method_profiling_data();
300 
301   if (TimeOopMap) {
302     GenerateOopMap::print_time();
303   }
304   if (PrintSymbolTableSizeHistogram) {
305     SymbolTable::print_histogram();
306   }
307   if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
308     BytecodeCounter::print();
309   }
310   if (PrintBytecodePairHistogram) {
311     BytecodePairHistogram::print();
312   }
313 
314   if (PrintCodeCache) {
315     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
316     CodeCache::print();
317   }
318 
319   // CodeHeap State Analytics.
320   if (PrintCodeHeapAnalytics) {
321     CompileBroker::print_heapinfo(nullptr, "all", 4096); // details
322   }
323 






324 #ifndef PRODUCT
325   if (PrintCodeCache2) {
326     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
327     CodeCache::print_internals();
328   }
329 #endif
330 
331   if (VerifyOops && Verbose) {
332     tty->print_cr("+VerifyOops count: %d", StubRoutines::verify_oop_count());
333   }
334 
335   print_bytecode_count();
336 
337   if (PrintVMInfoAtExit) {
338     // Use an intermediate stream to prevent deadlocking on tty_lock
339     stringStream ss;
340     VMError::print_vm_info(&ss);
341     tty->print_raw(ss.base());
342   }
343 
344   if (PrintSystemDictionaryAtExit) {
345     ResourceMark rm;
346     MutexLocker mcld(ClassLoaderDataGraph_lock);
347     SystemDictionary::print();
348   }
349 
350   if (PrintClassLoaderDataGraphAtExit) {
351     ResourceMark rm;
352     MutexLocker mcld(ClassLoaderDataGraph_lock);
353     ClassLoaderDataGraph::print();
354   }
355 
356   // Native memory tracking data
357   if (PrintNMTStatistics) {
358     MemTracker::final_report(tty);
359   }
360 
361   if (PrintMetaspaceStatisticsAtExit) {
362     MetaspaceUtils::print_basic_report(tty, 0);
363   }
364 
365   if (PrintCompilerMemoryStatisticsAtExit) {
366     CompilationMemoryStatistic::print_final_report(tty);
367   }
368 
369   ThreadsSMRSupport::log_statistics();
370 


371   if (log_is_enabled(Info, perf, class, link)) {
372     LogStreamHandle(Info, perf, class, link) log;
373     log.print_cr("At VM exit:");
374     ClassLoader::print_counters(&log);
375   }
376 }
377 
378 // Note: before_exit() can be executed only once, if more than one threads
379 //       are trying to shutdown the VM at the same time, only one thread
380 //       can run before_exit() and all other threads must wait.
381 void before_exit(JavaThread* thread, bool halt) {
382   #define BEFORE_EXIT_NOT_RUN 0
383   #define BEFORE_EXIT_RUNNING 1
384   #define BEFORE_EXIT_DONE    2
385   static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;
386 
387   Events::log(thread, "Before exit entered");
388 
389   // Note: don't use a Mutex to guard the entire before_exit(), as
390   // JVMTI post_thread_end_event and post_vm_death_event will run native code.
391   // A CAS or OSMutex would work just fine but then we need to manipulate
392   // thread state for Safepoint. Here we use Monitor wait() and notify_all()
393   // for synchronization.
394   { MonitorLocker ml(BeforeExit_lock);
395     switch (_before_exit_status) {
396     case BEFORE_EXIT_NOT_RUN:
397       _before_exit_status = BEFORE_EXIT_RUNNING;
398       break;
399     case BEFORE_EXIT_RUNNING:
400       while (_before_exit_status == BEFORE_EXIT_RUNNING) {
401         ml.wait();
402       }
403       assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
404       return;
405     case BEFORE_EXIT_DONE:
406       // need block to avoid SS compiler bug
407       {
408         return;
409       }
410     }
411   }
412 
413   // At this point only one thread is executing this logic. Any other threads
414   // attempting to invoke before_exit() will wait above and return early once
415   // this thread finishes before_exit().
416 
417   // Do not add any additional shutdown logic between the above mutex logic and
418   // leak sanitizer logic below. Any additional shutdown code which performs some
419   // cleanup should be added after the leak sanitizer logic below.
420 
421 #ifdef LEAK_SANITIZER
422   // If we are built with LSan, we need to perform leak checking. If we are
423   // terminating normally, not halting and no VM error, we perform a normal
424   // leak check which terminates if leaks are found. If we are not terminating
425   // normally, halting or VM error, we perform a recoverable leak check which
426   // prints leaks but will not terminate.
427   if (!halt && !VMError::is_error_reported()) {
428     LSAN_DO_LEAK_CHECK();
429   } else {
430     // Ignore the return value.
431     static_cast<void>(LSAN_DO_RECOVERABLE_LEAK_CHECK());
432   }
433 #endif
434 
435 #if INCLUDE_CDS

436   // Dynamic CDS dumping must happen whilst we can still reliably
437   // run Java code.
438   DynamicArchive::dump_at_exit(thread);
439   assert(!thread->has_pending_exception(), "must be");
440 #endif
441 
442   // Actual shutdown logic begins here.
443 
444 #if INCLUDE_JVMCI
445   if (EnableJVMCI) {
446     JVMCI::shutdown(thread);
447   }
448 #endif
449 
450 #if INCLUDE_CDS
451   ClassListWriter::write_resolved_constants();
452 

453   if (CDSConfig::is_dumping_preimage_static_archive()) {
454     AOTMetaspace::dump_static_archive(thread);
455   }
456 #endif
457 
458   // Hang forever on exit if we're reporting an error.
459   if (ShowMessageBoxOnError && VMError::is_error_reported()) {
460     os::infinite_sleep();
461   }
462 
463   EventThreadEnd event;
464   if (event.should_commit()) {
465     event.set_thread(JFR_JVM_THREAD_ID(thread));
466     event.commit();
467   }
468 
469   JFR_ONLY(Jfr::on_vm_shutdown(false, halt);)
470 
471   // Stop the WatcherThread. We do this before disenrolling various
472   // PeriodicTasks to reduce the likelihood of races.
473   WatcherThread::stop();
474 
475   NativeHeapTrimmer::cleanup();
476 
477   if (JvmtiExport::should_post_thread_life()) {
478     JvmtiExport::post_thread_end(thread);
479   }
480 
481   // Always call even when there are not JVMTI environments yet, since environments
482   // may be attached late and JVMTI must track phases of VM execution.
483   JvmtiExport::post_vm_death();
484   JvmtiAgentList::unload_agents();
485 
486   // No user code can be executed in the current thread after this point.
487 
488   // Run before exit and then stop concurrent GC threads.
489   Universe::before_exit();
490 
491   if (PrintBytecodeHistogram) {
492     BytecodeHistogram::print();
493   }
494 
495 #ifdef LINUX
496   if (DumpPerfMapAtExit) {
497     CodeCache::write_perf_map(nullptr, tty);
498   }
499   if (PrintMemoryMapAtExit) {
500     MemMapPrinter::print_all_mappings(tty);
501   }
502 #endif
503 
504   // Terminate the signal thread
505   // Note: we don't wait until it actually dies.
506   os::terminate_signal_thread();
507 
508   #if INCLUDE_CDS
509   if (AOTVerifyTrainingData) {
510     TrainingData::verify();
511   }
512   #endif
513 
514   print_statistics();
515 
516   { MutexLocker ml(BeforeExit_lock);
517     _before_exit_status = BEFORE_EXIT_DONE;
518     BeforeExit_lock->notify_all();
519   }
520 
521   if (VerifyStringTableAtExit) {
522     size_t fail_cnt = StringTable::verify_and_compare_entries();
523     if (fail_cnt != 0) {
524       tty->print_cr("ERROR: fail_cnt=%zu", fail_cnt);
525       guarantee(fail_cnt == 0, "unexpected StringTable verification failures");
526     }
527   }
528 
529   #undef BEFORE_EXIT_NOT_RUN
530   #undef BEFORE_EXIT_RUNNING
531   #undef BEFORE_EXIT_DONE
532 }
533 
534 void vm_exit(int code) {
535   Thread* thread =
536       ThreadLocalStorage::is_initialized() ? Thread::current_or_null() : nullptr;
537   if (thread == nullptr) {
538     // very early initialization failure -- just exit
539     vm_direct_exit(code);
540   }
541 
542   // We'd like to add an entry to the XML log to show that the VM is
543   // terminating, but we can't safely do that here. The logic to make
544   // XML termination logging safe is tied to the termination of the
545   // VMThread, and it doesn't terminate on this exit path. See 8222534.
546 
547   if (VMThread::vm_thread() != nullptr) {
548     if (thread->is_Java_thread()) {
549       // We must be "in_vm" for the code below to work correctly.
550       // Historically there must have been some exit path for which
551       // that was not the case and so we set it explicitly - even
552       // though we no longer know what that path may be.
553       JavaThread::cast(thread)->set_thread_state(_thread_in_vm);
554     }
555 
556     // Fire off a VM_Exit operation to bring VM to a safepoint and exit
557     VM_Exit op(code);
558 
559     // 4945125 The vm thread comes to a safepoint during exit.
560     // GC vm_operations can get caught at the safepoint, and the
561     // heap is unparseable if they are caught. Grab the Heap_lock
562     // to prevent this. The GC vm_operations will not be able to
563     // queue until after we release it, but we never do that as we
564     // are terminating the VM process.
565     MutexLocker ml(Heap_lock);
566 
567     VMThread::execute(&op);
568     // should never reach here; but in case something wrong with VM Thread.
569     vm_direct_exit(code);
570   } else {
571     // VM thread is gone, just exit
572     vm_direct_exit(code);
573   }
574   ShouldNotReachHere();
575 }
576 
577 void notify_vm_shutdown() {
578   // For now, just a dtrace probe.
579   HOTSPOT_VM_SHUTDOWN();
580 }
581 
582 void vm_direct_exit(int code) {
583   notify_vm_shutdown();
584   os::wait_for_keypress_at_exit();
585   os::exit(code);
586 }
587 
588 void vm_direct_exit(int code, const char* message) {
589   if (message != nullptr) {
590     tty->print_cr("%s", message);
591   }
592   vm_direct_exit(code);
593 }
594 
595 static void vm_perform_shutdown_actions() {
596   if (is_init_completed()) {
597     Thread* thread = Thread::current_or_null();
598     if (thread != nullptr && thread->is_Java_thread()) {
599       // We are leaving the VM, set state to native (in case any OS exit
600       // handlers call back to the VM)
601       JavaThread* jt = JavaThread::cast(thread);
602       // Must always be walkable or have no last_Java_frame when in
603       // thread_in_native
604       jt->frame_anchor()->make_walkable();
605       jt->set_thread_state(_thread_in_native);
606     }
607   }
608   notify_vm_shutdown();
609 }
610 
611 void vm_shutdown()
612 {
613   vm_perform_shutdown_actions();
614   os::wait_for_keypress_at_exit();
615   os::shutdown();
616 }
617 
618 void vm_abort(bool dump_core) {
619   vm_perform_shutdown_actions();
620   os::wait_for_keypress_at_exit();
621 
622   // Flush stdout and stderr before abort.
623   fflush(stdout);
624   fflush(stderr);
625 
626   os::abort(dump_core);
627   ShouldNotReachHere();
628 }
629 
630 static void vm_notify_during_cds_dumping(const char* error, const char* message) {
631   if (error != nullptr) {
632     tty->print_cr("Error occurred during CDS dumping");
633     tty->print("%s", error);
634     if (message != nullptr) {
635       tty->print_cr(": %s", message);
636     }
637     else {
638       tty->cr();
639     }
640   }
641 }
642 
643 void vm_exit_during_cds_dumping(const char* error, const char* message) {
644   vm_notify_during_cds_dumping(error, message);
645 
646   // Failure during CDS dumping, we don't want to dump core
647   vm_abort(false);
648 }
649 
650 static void vm_notify_during_shutdown(const char* error, const char* message) {
651   if (error != nullptr) {
652     tty->print_cr("Error occurred during initialization of VM");
653     tty->print("%s", error);
654     if (message != nullptr) {
655       tty->print_cr(": %s", message);
656     }
657     else {
658       tty->cr();
659     }
660   }
661   if (ShowMessageBoxOnError && WizardMode) {
662     fatal("Error occurred during initialization of VM");
663   }
664 }
665 
666 void vm_exit_during_initialization() {
667   vm_notify_during_shutdown(nullptr, nullptr);
668 
669   // Failure during initialization, we don't want to dump core
670   vm_abort(false);
671 }
672 
673 void vm_exit_during_initialization(Handle exception) {
674   tty->print_cr("Error occurred during initialization of VM");
675   // If there are exceptions on this thread it must be cleared
676   // first and here. Any future calls to EXCEPTION_MARK requires
677   // that no pending exceptions exist.
678   JavaThread* THREAD = JavaThread::current(); // can't be null
679   if (HAS_PENDING_EXCEPTION) {
680     CLEAR_PENDING_EXCEPTION;
681   }
682   java_lang_Throwable::print_stack_trace(exception, tty);
683   tty->cr();
684   vm_notify_during_shutdown(nullptr, nullptr);
685 
686   // Failure during initialization, we don't want to dump core
687   vm_abort(false);
688 }
689 
690 void vm_exit_during_initialization(Symbol* ex, const char* message) {
691   ResourceMark rm;
692   vm_notify_during_shutdown(ex->as_C_string(), message);
693 
694   // Failure during initialization, we don't want to dump core
695   vm_abort(false);
696 }
697 
698 void vm_exit_during_initialization(const char* error, const char* message) {
699   vm_notify_during_shutdown(error, message);
700 
701   // Failure during initialization, we don't want to dump core
702   vm_abort(false);
703 }
704 
705 void vm_shutdown_during_initialization(const char* error, const char* message) {
706   vm_notify_during_shutdown(error, message);
707   vm_shutdown();
708 }
709 
710 JDK_Version JDK_Version::_current;
711 const char* JDK_Version::_java_version;
712 const char* JDK_Version::_runtime_name;
713 const char* JDK_Version::_runtime_version;
714 const char* JDK_Version::_runtime_vendor_version;
715 const char* JDK_Version::_runtime_vendor_vm_bug_url;
716 
717 void JDK_Version::initialize() {
718   assert(!_current.is_valid(), "Don't initialize twice");
719 
720   int major = VM_Version::vm_major_version();
721   int minor = VM_Version::vm_minor_version();
722   int security = VM_Version::vm_security_version();
723   int build = VM_Version::vm_build_number();
724   int patch = VM_Version::vm_patch_version();
725   _current = JDK_Version(major, minor, security, patch, build);
726 }
727 
728 void JDK_Version_init() {
729   JDK_Version::initialize();
730 }
731 
732 static int64_t encode_jdk_version(const JDK_Version& v) {
733   return
734     ((int64_t)v.major_version()          << (BitsPerByte * 4)) |
735     ((int64_t)v.minor_version()          << (BitsPerByte * 3)) |
736     ((int64_t)v.security_version()       << (BitsPerByte * 2)) |
737     ((int64_t)v.patch_version()          << (BitsPerByte * 1)) |
738     ((int64_t)v.build_number()           << (BitsPerByte * 0));
739 }
740 
741 int JDK_Version::compare(const JDK_Version& other) const {
742   assert(is_valid() && other.is_valid(), "Invalid version (uninitialized?)");
743   uint64_t e = encode_jdk_version(*this);
744   uint64_t o = encode_jdk_version(other);
745   return (e > o) ? 1 : ((e == o) ? 0 : -1);
746 }
747 
748 /* See JEP 223 */
749 void JDK_Version::to_string(char* buffer, size_t buflen) const {
750   assert((buffer != nullptr) && (buflen > 0), "call with useful buffer");
751   stringStream ss{buffer, buflen};
752   if (!is_valid()) {
753     ss.print_raw("(uninitialized)");
754   } else {
755     ss.print("%d.%d", _major, _minor);
756     if (_patch > 0) {
757       ss.print(".%d.%d", _security, _patch);
758     } else if (_security > 0) {
759       ss.print(".%d", _security);
760     }
761     if (_build > 0) {
762       ss.print("+%d", _build);
763     }
764   }
765 }
766 
767 void JDK_Version::set_java_version(const char* version) {
768   _java_version = os::strdup(version);
769 }
770 
771 void JDK_Version::set_runtime_name(const char* name) {
772   _runtime_name = os::strdup(name);
773 }
774 
775 void JDK_Version::set_runtime_version(const char* version) {
776   _runtime_version = os::strdup(version);
777 }
778 
779 void JDK_Version::set_runtime_vendor_version(const char* vendor_version) {
780   _runtime_vendor_version = os::strdup(vendor_version);
781 }
782 
783 void JDK_Version::set_runtime_vendor_vm_bug_url(const char* vendor_vm_bug_url) {
784   _runtime_vendor_vm_bug_url = os::strdup(vendor_vm_bug_url);
785 }
--- EOF ---