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