1 /*
  2  * Copyright (c) 1997, 2024, 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 "cds/cds_globals.hpp"
 27 #include "cds/cdsConfig.hpp"
 28 #include "cds/classListWriter.hpp"
 29 #include "cds/classPrelinker.hpp"
 30 #include "cds/dynamicArchive.hpp"
 31 #include "cds/methodProfiler.hpp"
 32 #include "cds/metaspaceShared.hpp"
 33 #include "classfile/classLoader.hpp"
 34 #include "classfile/classLoaderDataGraph.hpp"
 35 #include "classfile/javaClasses.hpp"
 36 #include "classfile/stringTable.hpp"
 37 #include "classfile/symbolTable.hpp"
 38 #include "classfile/systemDictionary.hpp"
 39 #include "code/codeCache.hpp"
 40 #include "code/SCCache.hpp"
 41 #include "compiler/compilationMemoryStatistic.hpp"
 42 #include "compiler/compileBroker.hpp"
 43 #include "compiler/compilerOracle.hpp"
 44 #include "gc/shared/collectedHeap.hpp"
 45 #include "gc/shared/stringdedup/stringDedup.hpp"
 46 #include "interpreter/bytecodeHistogram.hpp"
 47 #include "interpreter/interpreterRuntime.hpp"
 48 #include "jfr/jfrEvents.hpp"
 49 #include "jfr/support/jfrThreadId.hpp"
 50 #include "jvm.h"
 51 #include "logging/log.hpp"
 52 #include "logging/logStream.hpp"
 53 #include "memory/metaspaceUtils.hpp"
 54 #include "memory/oopFactory.hpp"
 55 #include "memory/resourceArea.hpp"
 56 #include "memory/universe.hpp"
 57 #include "nmt/memTracker.hpp"
 58 #include "oops/constantPool.hpp"
 59 #include "oops/generateOopMap.hpp"
 60 #include "oops/instanceKlass.hpp"
 61 #include "oops/instanceOop.hpp"
 62 #include "oops/klassVtable.hpp"
 63 #include "oops/method.hpp"
 64 #include "oops/objArrayOop.hpp"
 65 #include "oops/oop.inline.hpp"
 66 #include "oops/symbol.hpp"
 67 #include "oops/trainingData.hpp"
 68 #include "prims/jvmtiAgentList.hpp"
 69 #include "prims/jvmtiExport.hpp"
 70 #include "prims/methodHandles.hpp"
 71 #include "runtime/continuation.hpp"
 72 #include "runtime/deoptimization.hpp"
 73 #include "runtime/flags/flagSetting.hpp"
 74 #include "runtime/globals_extension.hpp"
 75 #include "runtime/handles.inline.hpp"
 76 #include "runtime/init.hpp"
 77 #include "runtime/interfaceSupport.inline.hpp"
 78 #include "runtime/java.hpp"
 79 #include "runtime/javaThread.hpp"
 80 #include "runtime/sharedRuntime.hpp"
 81 #include "runtime/statSampler.hpp"
 82 #include "runtime/stubRoutines.hpp"
 83 #include "runtime/task.hpp"
 84 #include "runtime/threads.hpp"
 85 #include "runtime/timer.hpp"
 86 #include "runtime/trimNativeHeap.hpp"
 87 #include "runtime/vmOperations.hpp"
 88 #include "runtime/vmThread.hpp"
 89 #include "runtime/vm_version.hpp"
 90 #include "sanitizers/leak.hpp"
 91 #include "utilities/dtrace.hpp"
 92 #include "utilities/globalDefinitions.hpp"
 93 #include "utilities/macros.hpp"
 94 #include "utilities/vmError.hpp"
 95 #ifdef COMPILER1
 96 #include "c1/c1_Compiler.hpp"
 97 #include "c1/c1_Runtime1.hpp"
 98 #endif
 99 #ifdef COMPILER2
100 #include "code/compiledIC.hpp"
101 #include "opto/compile.hpp"
102 #include "opto/indexSet.hpp"
103 #include "opto/runtime.hpp"
104 #endif
105 #if INCLUDE_JFR
106 #include "jfr/jfr.hpp"
107 #endif
108 #if INCLUDE_JVMCI
109 #include "jvmci/jvmci.hpp"
110 #endif
111 
112 GrowableArray<Method*>* collected_profiled_methods;
113 
114 static int compare_methods(Method** a, Method** b) {
115   // compiled_invocation_count() returns int64_t, forcing the entire expression
116   // to be evaluated as int64_t. Overflow is not an issue.
117   int64_t diff = (((*b)->invocation_count() + (*b)->compiled_invocation_count())
118                 - ((*a)->invocation_count() + (*a)->compiled_invocation_count()));
119   return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
120 }
121 
122 static void collect_profiled_methods(Method* m) {
123   Thread* thread = Thread::current();
124   methodHandle mh(thread, m);
125   if ((m->method_data() != nullptr) &&
126       (PrintMethodData || CompilerOracle::should_print(mh))) {
127     collected_profiled_methods->push(m);
128   }
129 }
130 
131 static void print_method_profiling_data() {
132   if ((ProfileInterpreter COMPILER1_PRESENT(|| C1UpdateMethodData)) &&
133      (PrintMethodData || CompilerOracle::should_print_methods())) {
134     ResourceMark rm;
135     collected_profiled_methods = new GrowableArray<Method*>(1024);
136     SystemDictionary::methods_do(collect_profiled_methods);
137     collected_profiled_methods->sort(&compare_methods);
138 
139     int count = collected_profiled_methods->length();
140     int total_size = 0;
141     if (count > 0) {
142       for (int index = 0; index < count; index++) {
143         Method* m = collected_profiled_methods->at(index);
144 
145         // Instead of taking tty lock, we collect all lines into a string stream
146         // and then print them all at once.
147         ResourceMark rm2;
148         stringStream ss;
149 
150         ss.print_cr("------------------------------------------------------------------------");
151         m->print_invocation_count(&ss);
152         ss.print_cr("  mdo size: %d bytes", m->method_data()->size_in_bytes());
153         ss.cr();
154         // Dump data on parameters if any
155         if (m->method_data() != nullptr && m->method_data()->parameters_type_data() != nullptr) {
156           ss.fill_to(2);
157           m->method_data()->parameters_type_data()->print_data_on(&ss);
158         }
159         m->print_codes_on(&ss);
160         tty->print("%s", ss.as_string()); // print all at once
161         total_size += m->method_data()->size_in_bytes();
162       }
163       tty->print_cr("------------------------------------------------------------------------");
164       tty->print_cr("Total MDO size: %d bytes", total_size);
165     }
166   }
167 }
168 
169 void perf_jvm_print_on(outputStream* st);
170 
171 void log_vm_init_stats() {
172   LogStreamHandle(Info, init) log;
173   if (log.is_enabled()) {
174     SharedRuntime::print_counters_on(&log);
175     ClassLoader::print_counters();
176     ClassPrelinker::print_counters();
177     log.cr();
178     // FIXME: intermittent crashes
179 //    if (CountBytecodesPerThread) {
180 //      log.print_cr("Thread info:");
181 //      class PrintThreadInfo : public ThreadClosure {
182 //        outputStream* _st;
183 //      public:
184 //        PrintThreadInfo(outputStream* st) : ThreadClosure(), _st(st) {}
185 //        void do_thread(Thread* thread) {
186 //          JavaThread* jt = JavaThread::cast(thread);
187 //          if (jt->bc_counter_value() > 0) {
188 //            _st->print_cr("  Thread " INTPTR_FORMAT ": %ld bytecodes executed (clinit: %ld)",
189 //                          p2i(jt), /*jt->name(),*/ jt->bc_counter_value(), jt->clinit_bc_counter_value());
190 //          }
191 //        }
192 //      };
193 //      PrintThreadInfo cl(&log);
194 //      Threads::java_threads_do(&cl);
195 //    }
196 //    log.cr();
197     log.print("Deoptimization events: ");
198     Deoptimization::print_statistics_on(&log);
199     log.cr();
200 
201     log.print("Compilation statistics: ");
202     CompileBroker::print_statistics_on(&log);
203     log.cr();
204 
205     {
206       MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
207       log.print("Code cache statistics: ");
208       CodeCache::print_nmethod_statistics_on(&log);
209       log.cr();
210     }
211 
212     if (SCCache::is_on_for_read()) {
213       log.print_cr("Startup Code Cache: ");
214       SCCache::print_statistics_on(&log);
215       log.cr();
216       SCCache::print_timers_on(&log);
217     }
218 
219     VMThread::print_counters_on(&log);
220     log.cr();
221     MutexLockerImpl::print_counters_on(&log);
222     log.cr();
223     log.print("Runtime events for thread \"main\"");
224     if (ProfileRuntimeCalls) {
225       log.print_cr(" (%d nested events):", ProfileVMCallContext::nested_runtime_calls_count());
226 
227       Runtime1::print_counters_on(&log);
228       OptoRuntime::print_counters_on(&log);
229       InterpreterRuntime::print_counters_on(&log);
230       Deoptimization::print_counters_on(&log);
231     } else {
232       log.print_cr(": no info (%s is disabled)", (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData"));
233     }
234     log.cr();
235     perf_jvm_print_on(&log);
236     log.cr();
237     MethodHandles::print_counters_on(&log);
238   }
239 }
240 
241 void print_bytecode_count() {
242   if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
243     tty->print_cr("[BytecodeCounter::counter_value = %ld]", BytecodeCounter::counter_value());
244   }
245 }
246 
247 #ifndef PRODUCT
248 
249 // Statistics printing (method invocation histogram)
250 
251 GrowableArray<Method*>* collected_invoked_methods;
252 
253 static void collect_invoked_methods(Method* m) {
254   if (m->invocation_count() + m->compiled_invocation_count() >= 1) {
255     collected_invoked_methods->push(m);
256   }
257 }
258 
259 
260 // Invocation count accumulators should be unsigned long to shift the
261 // overflow border. Longer-running workloads tend to create invocation
262 // counts which already overflow 32-bit counters for individual methods.
263 static void print_method_invocation_histogram() {
264   ResourceMark rm;
265   collected_invoked_methods = new GrowableArray<Method*>(1024);
266   SystemDictionary::methods_do(collect_invoked_methods);
267   collected_invoked_methods->sort(&compare_methods);
268   //
269   tty->cr();
270   tty->print_cr("Histogram Over Method Invocation Counters (cutoff = " INTX_FORMAT "):", MethodHistogramCutoff);
271   tty->cr();
272   tty->print_cr("____Count_(I+C)____Method________________________Module_________________");
273   uint64_t total        = 0,
274            int_total    = 0,
275            comp_total   = 0,
276            special_total= 0,
277            static_total = 0,
278            final_total  = 0,
279            synch_total  = 0,
280            native_total = 0,
281            access_total = 0;
282   for (int index = 0; index < collected_invoked_methods->length(); index++) {
283     // Counter values returned from getter methods are signed int.
284     // To shift the overflow border by a factor of two, we interpret
285     // them here as unsigned long. A counter can't be negative anyway.
286     Method* m = collected_invoked_methods->at(index);
287     uint64_t iic = (uint64_t)m->invocation_count();
288     uint64_t cic = (uint64_t)m->compiled_invocation_count();
289     if ((iic + cic) >= (uint64_t)MethodHistogramCutoff) m->print_invocation_count(tty);
290     int_total  += iic;
291     comp_total += cic;
292     if (m->is_final())        final_total  += iic + cic;
293     if (m->is_static())       static_total += iic + cic;
294     if (m->is_synchronized()) synch_total  += iic + cic;
295     if (m->is_native())       native_total += iic + cic;
296     if (m->is_accessor())     access_total += iic + cic;
297   }
298   tty->cr();
299   total = int_total + comp_total;
300   special_total = final_total + static_total +synch_total + native_total + access_total;
301   tty->print_cr("Invocations summary for %d methods:", collected_invoked_methods->length());
302   double total_div = (double)total;
303   tty->print_cr("\t" UINT64_FORMAT_W(12) " (100%%)  total",           total);
304   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%) |- interpreted", int_total,     100.0 * (double)int_total    / total_div);
305   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%) |- compiled",    comp_total,    100.0 * (double)comp_total   / total_div);
306   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%) |- special methods (interpreted and compiled)",
307                                                                          special_total, 100.0 * (double)special_total/ total_div);
308   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- synchronized",synch_total,   100.0 * (double)synch_total  / total_div);
309   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- final",       final_total,   100.0 * (double)final_total  / total_div);
310   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- static",      static_total,  100.0 * (double)static_total / total_div);
311   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- native",      native_total,  100.0 * (double)native_total / total_div);
312   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- accessor",    access_total,  100.0 * (double)access_total / total_div);
313   tty->cr();
314   SharedRuntime::print_call_statistics_on(tty);
315 }
316 
317 #else
318 
319 static void print_method_invocation_histogram() {}
320 
321 #endif // PRODUCT
322 
323 
324 // General statistics printing (profiling ...)
325 void print_statistics() {
326   if (ReplayTraining && PrintTrainingInfo) {
327     TrainingData::print_archived_training_data_on(tty);
328   }
329   if (CITime) {
330     CompileBroker::print_times();
331   }
332 
333 #ifdef COMPILER1
334   if ((PrintC1Statistics || LogVMOutput || LogCompilation) && UseCompiler) {
335     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintC1Statistics);
336     Runtime1::print_statistics_on(tty);
337     SharedRuntime::print_statistics();
338   }
339 #endif /* COMPILER1 */
340 
341 #ifdef COMPILER2
342   if ((PrintOptoStatistics || LogVMOutput || LogCompilation) && UseCompiler) {
343     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintOptoStatistics);
344     Compile::print_statistics();
345     Deoptimization::print_statistics();
346 #ifndef COMPILER1
347     SharedRuntime::print_statistics();
348 #endif //COMPILER1
349   }
350 
351   if (PrintLockStatistics || PrintPreciseRTMLockingStatistics) {
352     OptoRuntime::print_named_counters();
353   }
354 #ifdef ASSERT
355   if (CollectIndexSetStatistics) {
356     IndexSet::print_statistics();
357   }
358 #endif // ASSERT
359 #else // COMPILER2
360 #if INCLUDE_JVMCI
361 #ifndef COMPILER1
362   if ((TraceDeoptimization || LogVMOutput || LogCompilation) && UseCompiler) {
363     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && TraceDeoptimization);
364     Deoptimization::print_statistics();
365     SharedRuntime::print_statistics();
366   }
367 #endif // COMPILER1
368 #endif // INCLUDE_JVMCI
369 #endif // COMPILER2
370 
371   if (PrintNMethodStatistics) {
372     nmethod::print_statistics();
373   }
374   if (CountCompiledCalls) {
375     print_method_invocation_histogram();
376   }
377 
378   print_method_profiling_data();
379 
380   if (TimeOopMap) {
381     GenerateOopMap::print_time();
382   }
383   if (PrintSymbolTableSizeHistogram) {
384     SymbolTable::print_histogram();
385   }
386   if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
387     BytecodeCounter::print();
388   }
389   if (PrintBytecodePairHistogram) {
390     BytecodePairHistogram::print();
391   }
392 
393   if (PrintCodeCache) {
394     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
395     CodeCache::print();
396   }
397 
398   // CodeHeap State Analytics.
399   if (PrintCodeHeapAnalytics) {
400     CompileBroker::print_heapinfo(nullptr, "all", 4096); // details
401   }
402 
403   LogStreamHandle(Debug, codecache, nmethod) log;
404   if (log.is_enabled()) {
405     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
406     CodeCache::print_nmethods_on(&log);
407   }
408 
409   if (PrintCodeCache2) {
410     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
411     CodeCache::print_internals();
412   }
413 
414   if (VerifyOops && Verbose) {
415     tty->print_cr("+VerifyOops count: %d", StubRoutines::verify_oop_count());
416   }
417 
418   print_bytecode_count();
419 
420   if (PrintSystemDictionaryAtExit) {
421     ResourceMark rm;
422     MutexLocker mcld(ClassLoaderDataGraph_lock);
423     SystemDictionary::print();
424   }
425 
426   if (PrintClassLoaderDataGraphAtExit) {
427     ResourceMark rm;
428     MutexLocker mcld(ClassLoaderDataGraph_lock);
429     ClassLoaderDataGraph::print();
430   }
431 
432   // Native memory tracking data
433   if (PrintNMTStatistics) {
434     MemTracker::final_report(tty);
435   }
436 
437   if (PrintMetaspaceStatisticsAtExit) {
438     MetaspaceUtils::print_basic_report(tty, 0);
439   }
440 
441   if (CompilerOracle::should_print_final_memstat_report()) {
442     CompilationMemoryStatistic::print_all_by_size(tty, false, 0);
443   }
444 
445   ThreadsSMRSupport::log_statistics();
446 
447   log_vm_init_stats();
448 }
449 
450 // Note: before_exit() can be executed only once, if more than one threads
451 //       are trying to shutdown the VM at the same time, only one thread
452 //       can run before_exit() and all other threads must wait.
453 void before_exit(JavaThread* thread, bool halt) {
454   #define BEFORE_EXIT_NOT_RUN 0
455   #define BEFORE_EXIT_RUNNING 1
456   #define BEFORE_EXIT_DONE    2
457   static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;
458 
459   // Note: don't use a Mutex to guard the entire before_exit(), as
460   // JVMTI post_thread_end_event and post_vm_death_event will run native code.
461   // A CAS or OSMutex would work just fine but then we need to manipulate
462   // thread state for Safepoint. Here we use Monitor wait() and notify_all()
463   // for synchronization.
464   { MonitorLocker ml(BeforeExit_lock);
465     switch (_before_exit_status) {
466     case BEFORE_EXIT_NOT_RUN:
467       _before_exit_status = BEFORE_EXIT_RUNNING;
468       break;
469     case BEFORE_EXIT_RUNNING:
470       while (_before_exit_status == BEFORE_EXIT_RUNNING) {
471         ml.wait();
472       }
473       assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
474       return;
475     case BEFORE_EXIT_DONE:
476       // need block to avoid SS compiler bug
477       {
478         return;
479       }
480     }
481   }
482 
483   // At this point only one thread is executing this logic. Any other threads
484   // attempting to invoke before_exit() will wait above and return early once
485   // this thread finishes before_exit().
486 
487   // Do not add any additional shutdown logic between the above mutex logic and
488   // leak sanitizer logic below. Any additional shutdown code which performs some
489   // cleanup should be added after the leak sanitizer logic below.
490 
491 #ifdef LEAK_SANITIZER
492   // If we are built with LSan, we need to perform leak checking. If we are
493   // terminating normally, not halting and no VM error, we perform a normal
494   // leak check which terminates if leaks are found. If we are not terminating
495   // normally, halting or VM error, we perform a recoverable leak check which
496   // prints leaks but will not terminate.
497   if (!halt && !VMError::is_error_reported()) {
498     LSAN_DO_LEAK_CHECK();
499   } else {
500     // Ignore the return value.
501     static_cast<void>(LSAN_DO_RECOVERABLE_LEAK_CHECK());
502   }
503 #endif
504 
505   MethodProfiler::process_method_hotness();
506 
507 #if INCLUDE_CDS
508   ClassListWriter::write_resolved_constants();
509   ClassListWriter::write_reflection_data();
510   ClassListWriter::write_loader_negative_lookup_cache();
511   // Dynamic CDS dumping must happen whilst we can still reliably
512   // run Java code.
513   if (CDSConfig::is_dumping_preimage_static_archive()) {
514     // Creating the hotspot.cds.preimage file
515     MetaspaceShared::preload_and_dump();
516   } else {
517     DynamicArchive::dump_at_exit(thread, ArchiveClassesAtExit);
518   }
519   assert(!thread->has_pending_exception(), "must be");
520 #endif
521 
522   // Actual shutdown logic begins here.
523 
524 #if INCLUDE_JVMCI
525   if (EnableJVMCI) {
526     JVMCI::shutdown(thread);
527   }
528 #endif
529 
530   SCCache::close(); // Write final data and close archive
531 
532   // Hang forever on exit if we're reporting an error.
533   if (ShowMessageBoxOnError && VMError::is_error_reported()) {
534     os::infinite_sleep();
535   }
536 
537   EventThreadEnd event;
538   if (event.should_commit()) {
539     event.set_thread(JFR_JVM_THREAD_ID(thread));
540     event.commit();
541   }
542 
543   JFR_ONLY(Jfr::on_vm_shutdown(false, halt);)
544 
545   // Stop the WatcherThread. We do this before disenrolling various
546   // PeriodicTasks to reduce the likelihood of races.
547   WatcherThread::stop();
548 
549   // shut down the StatSampler task
550   StatSampler::disengage();
551   StatSampler::destroy();
552 
553   NativeHeapTrimmer::cleanup();
554 
555   // Stop concurrent GC threads
556   Universe::heap()->stop();
557 
558   // Print GC/heap related information.
559   Log(gc, heap, exit) log;
560   if (log.is_info()) {
561     ResourceMark rm;
562     LogStream ls_info(log.info());
563     Universe::print_on(&ls_info);
564     if (log.is_trace()) {
565       LogStream ls_trace(log.trace());
566       MutexLocker mcld(ClassLoaderDataGraph_lock);
567       ClassLoaderDataGraph::print_on(&ls_trace);
568     }
569   }
570 
571   if (PrintBytecodeHistogram) {
572     BytecodeHistogram::print(PrintBytecodeHistogramCutoff);
573   }
574 
575 #ifdef LINUX
576   if (DumpPerfMapAtExit) {
577     CodeCache::write_perf_map();
578   }
579 #endif
580 
581   if (JvmtiExport::should_post_thread_life()) {
582     JvmtiExport::post_thread_end(thread);
583   }
584 
585   // Always call even when there are not JVMTI environments yet, since environments
586   // may be attached late and JVMTI must track phases of VM execution
587   JvmtiExport::post_vm_death();
588   JvmtiAgentList::unload_agents();
589 
590   // Terminate the signal thread
591   // Note: we don't wait until it actually dies.
592   os::terminate_signal_thread();
593 
594   print_statistics();
595   Universe::heap()->print_tracing_info();
596 
597   { MutexLocker ml(BeforeExit_lock);
598     _before_exit_status = BEFORE_EXIT_DONE;
599     BeforeExit_lock->notify_all();
600   }
601 
602   if (VerifyStringTableAtExit) {
603     size_t fail_cnt = StringTable::verify_and_compare_entries();
604     if (fail_cnt != 0) {
605       tty->print_cr("ERROR: fail_cnt=" SIZE_FORMAT, fail_cnt);
606       guarantee(fail_cnt == 0, "unexpected StringTable verification failures");
607     }
608   }
609 
610   #undef BEFORE_EXIT_NOT_RUN
611   #undef BEFORE_EXIT_RUNNING
612   #undef BEFORE_EXIT_DONE
613 }
614 
615 void vm_exit(int code) {
616   Thread* thread =
617       ThreadLocalStorage::is_initialized() ? Thread::current_or_null() : nullptr;
618   if (thread == nullptr) {
619     // very early initialization failure -- just exit
620     vm_direct_exit(code);
621   }
622 
623   // We'd like to add an entry to the XML log to show that the VM is
624   // terminating, but we can't safely do that here. The logic to make
625   // XML termination logging safe is tied to the termination of the
626   // VMThread, and it doesn't terminate on this exit path. See 8222534.
627 
628   if (VMThread::vm_thread() != nullptr) {
629     if (thread->is_Java_thread()) {
630       // We must be "in_vm" for the code below to work correctly.
631       // Historically there must have been some exit path for which
632       // that was not the case and so we set it explicitly - even
633       // though we no longer know what that path may be.
634       JavaThread::cast(thread)->set_thread_state(_thread_in_vm);
635     }
636 
637     // Fire off a VM_Exit operation to bring VM to a safepoint and exit
638     VM_Exit op(code);
639 
640     // 4945125 The vm thread comes to a safepoint during exit.
641     // GC vm_operations can get caught at the safepoint, and the
642     // heap is unparseable if they are caught. Grab the Heap_lock
643     // to prevent this. The GC vm_operations will not be able to
644     // queue until after we release it, but we never do that as we
645     // are terminating the VM process.
646     MutexLocker ml(Heap_lock);
647 
648     VMThread::execute(&op);
649     // should never reach here; but in case something wrong with VM Thread.
650     vm_direct_exit(code);
651   } else {
652     // VM thread is gone, just exit
653     vm_direct_exit(code);
654   }
655   ShouldNotReachHere();
656 }
657 
658 void notify_vm_shutdown() {
659   // For now, just a dtrace probe.
660   HOTSPOT_VM_SHUTDOWN();
661 }
662 
663 void vm_direct_exit(int code) {
664   notify_vm_shutdown();
665   os::wait_for_keypress_at_exit();
666   os::exit(code);
667 }
668 
669 void vm_direct_exit(int code, const char* message) {
670   if (message != nullptr) {
671     tty->print_cr("%s", message);
672   }
673   vm_direct_exit(code);
674 }
675 
676 static void vm_perform_shutdown_actions() {
677   if (is_init_completed()) {
678     Thread* thread = Thread::current_or_null();
679     if (thread != nullptr && thread->is_Java_thread()) {
680       // We are leaving the VM, set state to native (in case any OS exit
681       // handlers call back to the VM)
682       JavaThread* jt = JavaThread::cast(thread);
683       // Must always be walkable or have no last_Java_frame when in
684       // thread_in_native
685       jt->frame_anchor()->make_walkable();
686       jt->set_thread_state(_thread_in_native);
687     }
688   }
689   notify_vm_shutdown();
690 }
691 
692 void vm_shutdown()
693 {
694   vm_perform_shutdown_actions();
695   os::wait_for_keypress_at_exit();
696   os::shutdown();
697 }
698 
699 void vm_abort(bool dump_core) {
700   vm_perform_shutdown_actions();
701   os::wait_for_keypress_at_exit();
702 
703   // Flush stdout and stderr before abort.
704   fflush(stdout);
705   fflush(stderr);
706 
707   os::abort(dump_core);
708   ShouldNotReachHere();
709 }
710 
711 static void vm_notify_during_cds_dumping(const char* error, const char* message) {
712   if (error != nullptr) {
713     tty->print_cr("Error occurred during CDS dumping");
714     tty->print("%s", error);
715     if (message != nullptr) {
716       tty->print_cr(": %s", message);
717     }
718     else {
719       tty->cr();
720     }
721   }
722 }
723 
724 void vm_exit_during_cds_dumping(const char* error, const char* message) {
725   vm_notify_during_cds_dumping(error, message);
726 
727   // Failure during CDS dumping, we don't want to dump core
728   vm_abort(false);
729 }
730 
731 static void vm_notify_during_shutdown(const char* error, const char* message) {
732   if (error != nullptr) {
733     tty->print_cr("Error occurred during initialization of VM");
734     tty->print("%s", error);
735     if (message != nullptr) {
736       tty->print_cr(": %s", message);
737     }
738     else {
739       tty->cr();
740     }
741   }
742   if (ShowMessageBoxOnError && WizardMode) {
743     fatal("Error occurred during initialization of VM");
744   }
745 }
746 
747 void vm_exit_during_initialization() {
748   vm_notify_during_shutdown(nullptr, nullptr);
749 
750   // Failure during initialization, we don't want to dump core
751   vm_abort(false);
752 }
753 
754 void vm_exit_during_initialization(Handle exception) {
755   tty->print_cr("Error occurred during initialization of VM");
756   // If there are exceptions on this thread it must be cleared
757   // first and here. Any future calls to EXCEPTION_MARK requires
758   // that no pending exceptions exist.
759   JavaThread* THREAD = JavaThread::current(); // can't be null
760   if (HAS_PENDING_EXCEPTION) {
761     CLEAR_PENDING_EXCEPTION;
762   }
763   java_lang_Throwable::print_stack_trace(exception, tty);
764   tty->cr();
765   vm_notify_during_shutdown(nullptr, nullptr);
766 
767   // Failure during initialization, we don't want to dump core
768   vm_abort(false);
769 }
770 
771 void vm_exit_during_initialization(Symbol* ex, const char* message) {
772   ResourceMark rm;
773   vm_notify_during_shutdown(ex->as_C_string(), message);
774 
775   // Failure during initialization, we don't want to dump core
776   vm_abort(false);
777 }
778 
779 void vm_exit_during_initialization(const char* error, const char* message) {
780   vm_notify_during_shutdown(error, message);
781 
782   // Failure during initialization, we don't want to dump core
783   vm_abort(false);
784 }
785 
786 void vm_shutdown_during_initialization(const char* error, const char* message) {
787   vm_notify_during_shutdown(error, message);
788   vm_shutdown();
789 }
790 
791 JDK_Version JDK_Version::_current;
792 const char* JDK_Version::_java_version;
793 const char* JDK_Version::_runtime_name;
794 const char* JDK_Version::_runtime_version;
795 const char* JDK_Version::_runtime_vendor_version;
796 const char* JDK_Version::_runtime_vendor_vm_bug_url;
797 
798 void JDK_Version::initialize() {
799   assert(!_current.is_valid(), "Don't initialize twice");
800 
801   int major = VM_Version::vm_major_version();
802   int minor = VM_Version::vm_minor_version();
803   int security = VM_Version::vm_security_version();
804   int build = VM_Version::vm_build_number();
805   int patch = VM_Version::vm_patch_version();
806   _current = JDK_Version(major, minor, security, patch, build);
807 }
808 
809 void JDK_Version_init() {
810   JDK_Version::initialize();
811 }
812 
813 static int64_t encode_jdk_version(const JDK_Version& v) {
814   return
815     ((int64_t)v.major_version()          << (BitsPerByte * 4)) |
816     ((int64_t)v.minor_version()          << (BitsPerByte * 3)) |
817     ((int64_t)v.security_version()       << (BitsPerByte * 2)) |
818     ((int64_t)v.patch_version()          << (BitsPerByte * 1)) |
819     ((int64_t)v.build_number()           << (BitsPerByte * 0));
820 }
821 
822 int JDK_Version::compare(const JDK_Version& other) const {
823   assert(is_valid() && other.is_valid(), "Invalid version (uninitialized?)");
824   uint64_t e = encode_jdk_version(*this);
825   uint64_t o = encode_jdk_version(other);
826   return (e > o) ? 1 : ((e == o) ? 0 : -1);
827 }
828 
829 /* See JEP 223 */
830 void JDK_Version::to_string(char* buffer, size_t buflen) const {
831   assert(buffer && buflen > 0, "call with useful buffer");
832   size_t index = 0;
833 
834   if (!is_valid()) {
835     jio_snprintf(buffer, buflen, "%s", "(uninitialized)");
836   } else {
837     int rc = jio_snprintf(
838         &buffer[index], buflen - index, "%d.%d", _major, _minor);
839     if (rc == -1) return;
840     index += rc;
841     if (_patch > 0) {
842       rc = jio_snprintf(&buffer[index], buflen - index, ".%d.%d", _security, _patch);
843       if (rc == -1) return;
844       index += rc;
845     } else if (_security > 0) {
846       rc = jio_snprintf(&buffer[index], buflen - index, ".%d", _security);
847       if (rc == -1) return;
848       index += rc;
849     }
850     if (_build > 0) {
851       rc = jio_snprintf(&buffer[index], buflen - index, "+%d", _build);
852       if (rc == -1) return;
853       index += rc;
854     }
855   }
856 }