1 /*
2 * Copyright (c) 2003, 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 "code/codeCache.hpp"
28 #include "compiler/compileBroker.hpp"
29 #include "compiler/disassembler.hpp"
30 #include "gc/shared/gcConfig.hpp"
31 #include "gc/shared/gcLogPrecious.hpp"
32 #include "logging/logConfiguration.hpp"
33 #include "memory/metaspace.hpp"
34 #include "memory/metaspaceShared.hpp"
35 #include "memory/resourceArea.inline.hpp"
36 #include "memory/universe.hpp"
37 #include "oops/compressedOops.hpp"
38 #include "prims/whitebox.hpp"
39 #include "runtime/arguments.hpp"
40 #include "runtime/atomic.hpp"
41 #include "runtime/frame.inline.hpp"
42 #include "runtime/init.hpp"
43 #include "runtime/os.hpp"
44 #include "runtime/safepointMechanism.hpp"
45 #include "runtime/thread.inline.hpp"
46 #include "runtime/threadSMR.hpp"
47 #include "runtime/vmThread.hpp"
48 #include "runtime/vmOperations.hpp"
49 #include "runtime/vm_version.hpp"
50 #include "runtime/flags/jvmFlag.hpp"
51 #include "services/memTracker.hpp"
52 #include "utilities/debug.hpp"
53 #include "utilities/decoder.hpp"
54 #include "utilities/defaultStream.hpp"
55 #include "utilities/events.hpp"
56 #include "utilities/vmError.hpp"
57 #include "utilities/macros.hpp"
58 #if INCLUDE_JFR
59 #include "jfr/jfr.hpp"
60 #endif
61
62 #ifndef PRODUCT
63 #include <signal.h>
64 #endif // PRODUCT
65
66 bool VMError::_error_reported = false;
67
68 // call this when the VM is dying--it might loosen some asserts
is_error_reported()69 bool VMError::is_error_reported() { return _error_reported; }
70
71 // returns an address which is guaranteed to generate a SIGSEGV on read,
72 // for test purposes, which is not NULL and contains bits in every word
get_segfault_address()73 void* VMError::get_segfault_address() {
74 return (void*)
75 #ifdef _LP64
76 0xABC0000000000ABCULL;
77 #else
78 0x00000ABC;
79 #endif
80 }
81
82 // List of environment variables that should be reported in error log file.
83 const char *env_list[] = {
84 // All platforms
85 "JAVA_HOME", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "CLASSPATH",
86 "PATH", "USERNAME",
87
88 // Env variables that are defined on Linux/BSD
89 "LD_LIBRARY_PATH", "LD_PRELOAD", "SHELL", "DISPLAY",
90 "HOSTTYPE", "OSTYPE", "ARCH", "MACHTYPE",
91 "LANG", "LC_ALL", "LC_CTYPE", "TZ",
92
93 // defined on AIX
94 "LIBPATH", "LDR_PRELOAD", "LDR_PRELOAD64",
95
96 // defined on Linux/AIX/BSD
97 "_JAVA_SR_SIGNUM",
98
99 // defined on Darwin
100 "DYLD_LIBRARY_PATH", "DYLD_FALLBACK_LIBRARY_PATH",
101 "DYLD_FRAMEWORK_PATH", "DYLD_FALLBACK_FRAMEWORK_PATH",
102 "DYLD_INSERT_LIBRARIES",
103
104 // defined on Windows
105 "OS", "PROCESSOR_IDENTIFIER", "_ALT_JAVA_HOME_DIR",
106
107 (const char *)0
108 };
109
110 // A simple parser for -XX:OnError, usage:
111 // ptr = OnError;
112 // while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != NULL)
113 // ... ...
next_OnError_command(char * buf,int buflen,const char ** ptr)114 static char* next_OnError_command(char* buf, int buflen, const char** ptr) {
115 if (ptr == NULL || *ptr == NULL) return NULL;
116
117 const char* cmd = *ptr;
118
119 // skip leading blanks or ';'
120 while (*cmd == ' ' || *cmd == ';') cmd++;
121
122 if (*cmd == '\0') return NULL;
123
124 const char * cmdend = cmd;
125 while (*cmdend != '\0' && *cmdend != ';') cmdend++;
126
127 Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen);
128
129 *ptr = (*cmdend == '\0' ? cmdend : cmdend + 1);
130 return buf;
131 }
132
print_bug_submit_message(outputStream * out,Thread * thread)133 static void print_bug_submit_message(outputStream *out, Thread *thread) {
134 if (out == NULL) return;
135 const char *url = Arguments::java_vendor_url_bug();
136 if (url == NULL || *url == '\0')
137 url = JDK_Version::runtime_vendor_vm_bug_url();
138 if (url != NULL && *url != '\0') {
139 out->print_raw_cr("# If you would like to submit a bug report, please visit:");
140 out->print_raw ("# ");
141 out->print_raw_cr(url);
142 }
143 // If the crash is in native code, encourage user to submit a bug to the
144 // provider of that code.
145 if (thread && thread->is_Java_thread() &&
146 !thread->is_hidden_from_external_view()) {
147 if (thread->as_Java_thread()->thread_state() == _thread_in_native) {
148 out->print_cr("# The crash happened outside the Java Virtual Machine in native code.\n# See problematic frame for where to report the bug.");
149 }
150 }
151 out->print_raw_cr("#");
152 }
153
154 bool VMError::coredump_status;
155 char VMError::coredump_message[O_BUFLEN];
156
record_coredump_status(const char * message,bool status)157 void VMError::record_coredump_status(const char* message, bool status) {
158 coredump_status = status;
159 strncpy(coredump_message, message, sizeof(coredump_message));
160 coredump_message[sizeof(coredump_message)-1] = 0;
161 }
162
163 // Return a string to describe the error
error_string(char * buf,int buflen)164 char* VMError::error_string(char* buf, int buflen) {
165 char signame_buf[64];
166 const char *signame = os::exception_name(_id, signame_buf, sizeof(signame_buf));
167
168 if (signame) {
169 jio_snprintf(buf, buflen,
170 "%s (0x%x) at pc=" PTR_FORMAT ", pid=%d, tid=" UINTX_FORMAT,
171 signame, _id, _pc,
172 os::current_process_id(), os::current_thread_id());
173 } else if (_filename != NULL && _lineno > 0) {
174 // skip directory names
175 char separator = os::file_separator()[0];
176 const char *p = strrchr(_filename, separator);
177 int n = jio_snprintf(buf, buflen,
178 "Internal Error at %s:%d, pid=%d, tid=" UINTX_FORMAT,
179 p ? p + 1 : _filename, _lineno,
180 os::current_process_id(), os::current_thread_id());
181 if (n >= 0 && n < buflen && _message) {
182 if (strlen(_detail_msg) > 0) {
183 jio_snprintf(buf + n, buflen - n, "%s%s: %s",
184 os::line_separator(), _message, _detail_msg);
185 } else {
186 jio_snprintf(buf + n, buflen - n, "%sError: %s",
187 os::line_separator(), _message);
188 }
189 }
190 } else {
191 jio_snprintf(buf, buflen,
192 "Internal Error (0x%x), pid=%d, tid=" UINTX_FORMAT,
193 _id, os::current_process_id(), os::current_thread_id());
194 }
195
196 return buf;
197 }
198
print_stack_trace(outputStream * st,JavaThread * jt,char * buf,int buflen,bool verbose)199 void VMError::print_stack_trace(outputStream* st, JavaThread* jt,
200 char* buf, int buflen, bool verbose) {
201 #ifdef ZERO
202 if (jt->zero_stack()->sp() && jt->top_zero_frame()) {
203 // StackFrameStream uses the frame anchor, which may not have
204 // been set up. This can be done at any time in Zero, however,
205 // so if it hasn't been set up then we just set it up now and
206 // clear it again when we're done.
207 bool has_last_Java_frame = jt->has_last_Java_frame();
208 if (!has_last_Java_frame)
209 jt->set_last_Java_frame();
210 st->print("Java frames:");
211 st->cr();
212
213 // Print the frames
214 StackFrameStream sfs(jt, true /* update */, true /* process_frames */);
215 for(int i = 0; !sfs.is_done(); sfs.next(), i++) {
216 sfs.current()->zero_print_on_error(i, st, buf, buflen);
217 st->cr();
218 }
219
220 // Reset the frame anchor if necessary
221 if (!has_last_Java_frame)
222 jt->reset_last_Java_frame();
223 }
224 #else
225 if (jt->has_last_Java_frame()) {
226 st->print_cr("Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)");
227 for (StackFrameStream sfs(jt, true /* update */, true /* process_frames */); !sfs.is_done(); sfs.next()) {
228 sfs.current()->print_on_error(st, buf, buflen, verbose);
229 st->cr();
230 }
231 }
232 #endif // ZERO
233 }
234
print_native_stack(outputStream * st,frame fr,Thread * t,char * buf,int buf_size)235 void VMError::print_native_stack(outputStream* st, frame fr, Thread* t, char* buf, int buf_size) {
236
237 // see if it's a valid frame
238 if (fr.pc()) {
239 st->print_cr("Native frames: (J=compiled Java code, A=aot compiled Java code, j=interpreted, Vv=VM code, C=native code)");
240
241 int count = 0;
242 while (count++ < StackPrintLimit) {
243 fr.print_on_error(st, buf, buf_size);
244 if (fr.pc()) { // print source file and line, if available
245 char buf[128];
246 int line_no;
247 if (Decoder::get_source_info(fr.pc(), buf, sizeof(buf), &line_no)) {
248 st->print(" (%s:%d)", buf, line_no);
249 }
250 }
251 st->cr();
252 // Compiled code may use EBP register on x86 so it looks like
253 // non-walkable C frame. Use frame.sender() for java frames.
254 if (t && t->is_Java_thread()) {
255 // Catch very first native frame by using stack address.
256 // For JavaThread stack_base and stack_size should be set.
257 if (!t->is_in_full_stack((address)(fr.real_fp() + 1))) {
258 break;
259 }
260 if (fr.is_java_frame() || fr.is_native_frame() || fr.is_runtime_frame()) {
261 RegisterMap map(t->as_Java_thread(), false); // No update
262 fr = fr.sender(&map);
263 } else {
264 // is_first_C_frame() does only simple checks for frame pointer,
265 // it will pass if java compiled code has a pointer in EBP.
266 if (os::is_first_C_frame(&fr)) break;
267 fr = os::get_sender_for_C_frame(&fr);
268 }
269 } else {
270 if (os::is_first_C_frame(&fr)) break;
271 fr = os::get_sender_for_C_frame(&fr);
272 }
273 }
274
275 if (count > StackPrintLimit) {
276 st->print_cr("...<more frames>...");
277 }
278
279 st->cr();
280 }
281 }
282
print_oom_reasons(outputStream * st)283 static void print_oom_reasons(outputStream* st) {
284 st->print_cr("# Possible reasons:");
285 st->print_cr("# The system is out of physical RAM or swap space");
286 if (UseCompressedOops) {
287 st->print_cr("# The process is running with CompressedOops enabled, and the Java Heap may be blocking the growth of the native heap");
288 }
289 if (LogBytesPerWord == 2) {
290 st->print_cr("# In 32 bit mode, the process size limit was hit");
291 }
292 st->print_cr("# Possible solutions:");
293 st->print_cr("# Reduce memory load on the system");
294 st->print_cr("# Increase physical memory or swap space");
295 st->print_cr("# Check if swap backing store is full");
296 if (LogBytesPerWord == 2) {
297 st->print_cr("# Use 64 bit Java on a 64 bit OS");
298 }
299 st->print_cr("# Decrease Java heap size (-Xmx/-Xms)");
300 st->print_cr("# Decrease number of Java threads");
301 st->print_cr("# Decrease Java thread stack sizes (-Xss)");
302 st->print_cr("# Set larger code cache with -XX:ReservedCodeCacheSize=");
303 if (UseCompressedOops) {
304 switch (CompressedOops::mode()) {
305 case CompressedOops::UnscaledNarrowOop:
306 st->print_cr("# JVM is running with Unscaled Compressed Oops mode in which the Java heap is");
307 st->print_cr("# placed in the first 4GB address space. The Java Heap base address is the");
308 st->print_cr("# maximum limit for the native heap growth. Please use -XX:HeapBaseMinAddress");
309 st->print_cr("# to set the Java Heap base and to place the Java Heap above 4GB virtual address.");
310 break;
311 case CompressedOops::ZeroBasedNarrowOop:
312 st->print_cr("# JVM is running with Zero Based Compressed Oops mode in which the Java heap is");
313 st->print_cr("# placed in the first 32GB address space. The Java Heap base address is the");
314 st->print_cr("# maximum limit for the native heap growth. Please use -XX:HeapBaseMinAddress");
315 st->print_cr("# to set the Java Heap base and to place the Java Heap above 32GB virtual address.");
316 break;
317 default:
318 break;
319 }
320 }
321 st->print_cr("# This output file may be truncated or incomplete.");
322 }
323
report_vm_version(outputStream * st,char * buf,int buflen)324 static void report_vm_version(outputStream* st, char* buf, int buflen) {
325 // VM version
326 st->print_cr("#");
327 JDK_Version::current().to_string(buf, buflen);
328 const char* runtime_name = JDK_Version::runtime_name() != NULL ?
329 JDK_Version::runtime_name() : "";
330 const char* runtime_version = JDK_Version::runtime_version() != NULL ?
331 JDK_Version::runtime_version() : "";
332 const char* vendor_version = JDK_Version::runtime_vendor_version() != NULL ?
333 JDK_Version::runtime_vendor_version() : "";
334 const char* jdk_debug_level = VM_Version::printable_jdk_debug_level() != NULL ?
335 VM_Version::printable_jdk_debug_level() : "";
336
337 st->print_cr("# JRE version: %s%s%s (%s) (%sbuild %s)", runtime_name,
338 (*vendor_version != '\0') ? " " : "", vendor_version,
339 buf, jdk_debug_level, runtime_version);
340
341 // This is the long version with some default settings added
342 st->print_cr("# Java VM: %s%s%s (%s%s, %s%s%s%s%s%s, %s, %s)",
343 VM_Version::vm_name(),
344 (*vendor_version != '\0') ? " " : "", vendor_version,
345 jdk_debug_level,
346 VM_Version::vm_release(),
347 VM_Version::vm_info_string(),
348 TieredCompilation ? ", tiered" : "",
349 #if INCLUDE_JVMCI
350 EnableJVMCI ? ", jvmci" : "",
351 UseJVMCICompiler ? ", jvmci compiler" : "",
352 #else
353 "", "",
354 #endif
355 UseCompressedOops ? ", compressed oops" : "",
356 UseCompressedClassPointers ? ", compressed class ptrs" : "",
357 GCConfig::hs_err_name(),
358 VM_Version::vm_platform_string()
359 );
360 }
361
362 // This is the main function to report a fatal error. Only one thread can
363 // call this function, so we don't need to worry about MT-safety. But it's
364 // possible that the error handler itself may crash or die on an internal
365 // error, for example, when the stack/heap is badly damaged. We must be
366 // able to handle recursive errors that happen inside error handler.
367 //
368 // Error reporting is done in several steps. If a crash or internal error
369 // occurred when reporting an error, the nested signal/exception handler
370 // can skip steps that are already (or partially) done. Error reporting will
371 // continue from the next step. This allows us to retrieve and print
372 // information that may be unsafe to get after a fatal error. If it happens,
373 // you may find nested report_and_die() frames when you look at the stack
374 // in a debugger.
375 //
376 // In general, a hang in error handler is much worse than a crash or internal
377 // error, as it's harder to recover from a hang. Deadlock can happen if we
378 // try to grab a lock that is already owned by current thread, or if the
379 // owner is blocked forever (e.g. in os::infinite_sleep()). If possible, the
380 // error handler and all the functions it called should avoid grabbing any
381 // lock. An important thing to notice is that memory allocation needs a lock.
382 //
383 // We should avoid using large stack allocated buffers. Many errors happen
384 // when stack space is already low. Making things even worse is that there
385 // could be nested report_and_die() calls on stack (see above). Only one
386 // thread can report error, so large buffers are statically allocated in data
387 // segment.
388
389 int VMError::_current_step;
390 const char* VMError::_current_step_info;
391
392 volatile jlong VMError::_reporting_start_time = -1;
393 volatile bool VMError::_reporting_did_timeout = false;
394 volatile jlong VMError::_step_start_time = -1;
395 volatile bool VMError::_step_did_timeout = false;
396
397 // Helper, return current timestamp for timeout handling.
get_current_timestamp()398 jlong VMError::get_current_timestamp() {
399 return os::javaTimeNanos();
400 }
401 // Factor to translate the timestamp to seconds.
402 #define TIMESTAMP_TO_SECONDS_FACTOR (1000 * 1000 * 1000)
403
record_reporting_start_time()404 void VMError::record_reporting_start_time() {
405 const jlong now = get_current_timestamp();
406 Atomic::store(&_reporting_start_time, now);
407 }
408
get_reporting_start_time()409 jlong VMError::get_reporting_start_time() {
410 return Atomic::load(&_reporting_start_time);
411 }
412
record_step_start_time()413 void VMError::record_step_start_time() {
414 const jlong now = get_current_timestamp();
415 Atomic::store(&_step_start_time, now);
416 }
417
get_step_start_time()418 jlong VMError::get_step_start_time() {
419 return Atomic::load(&_step_start_time);
420 }
421
clear_step_start_time()422 void VMError::clear_step_start_time() {
423 return Atomic::store(&_step_start_time, (jlong)0);
424 }
425
report(outputStream * st,bool _verbose)426 void VMError::report(outputStream* st, bool _verbose) {
427
428 # define BEGIN if (_current_step == 0) { _current_step = __LINE__;
429 # define STEP(s) } if (_current_step < __LINE__) { _current_step = __LINE__; _current_step_info = s; \
430 record_step_start_time(); _step_did_timeout = false;
431 # define END clear_step_start_time(); }
432
433 // don't allocate large buffer on stack
434 static char buf[O_BUFLEN];
435
436 BEGIN
437
438 STEP("printing fatal error message")
439
440 st->print_cr("#");
441 if (should_report_bug(_id)) {
442 st->print_cr("# A fatal error has been detected by the Java Runtime Environment:");
443 } else {
444 st->print_cr("# There is insufficient memory for the Java "
445 "Runtime Environment to continue.");
446 }
447
448 #ifndef PRODUCT
449 // Error handler self tests
450
451 // test secondary error handling. Test it twice, to test that resetting
452 // error handler after a secondary crash works.
453 STEP("test secondary crash 1")
454 if (_verbose && TestCrashInErrorHandler != 0) {
455 st->print_cr("Will crash now (TestCrashInErrorHandler=" UINTX_FORMAT ")...",
456 TestCrashInErrorHandler);
457 controlled_crash(TestCrashInErrorHandler);
458 }
459
460 STEP("test secondary crash 2")
461 if (_verbose && TestCrashInErrorHandler != 0) {
462 st->print_cr("Will crash now (TestCrashInErrorHandler=" UINTX_FORMAT ")...",
463 TestCrashInErrorHandler);
464 controlled_crash(TestCrashInErrorHandler);
465 }
466
467 // TestUnresponsiveErrorHandler: We want to test both step timeouts and global timeout.
468 // Step to global timeout ratio is 4:1, so in order to be absolutely sure we hit the
469 // global timeout, let's execute the timeout step five times.
470 // See corresponding test in test/runtime/ErrorHandling/TimeoutInErrorHandlingTest.java
471 STEP("setup for test unresponsive error reporting step")
472 if (_verbose && TestUnresponsiveErrorHandler) {
473 // We record reporting_start_time for this test here because we
474 // care about the time spent executing TIMEOUT_TEST_STEP and not
475 // about the time it took us to get here.
476 tty->print_cr("Recording reporting_start_time for TestUnresponsiveErrorHandler.");
477 record_reporting_start_time();
478 }
479
480 #define TIMEOUT_TEST_STEP STEP("test unresponsive error reporting step") \
481 if (_verbose && TestUnresponsiveErrorHandler) { os::infinite_sleep(); }
482 TIMEOUT_TEST_STEP
483 TIMEOUT_TEST_STEP
484 TIMEOUT_TEST_STEP
485 TIMEOUT_TEST_STEP
486 TIMEOUT_TEST_STEP
487
488 STEP("test safefetch in error handler")
489 // test whether it is safe to use SafeFetch32 in Crash Handler. Test twice
490 // to test that resetting the signal handler works correctly.
491 if (_verbose && TestSafeFetchInErrorHandler) {
492 st->print_cr("Will test SafeFetch...");
493 if (CanUseSafeFetch32()) {
494 int* const invalid_pointer = (int*) get_segfault_address();
495 const int x = 0x76543210;
496 int i1 = SafeFetch32(invalid_pointer, x);
497 int i2 = SafeFetch32(invalid_pointer, x);
498 if (i1 == x && i2 == x) {
499 st->print_cr("SafeFetch OK."); // Correctly deflected and returned default pattern
500 } else {
501 st->print_cr("??");
502 }
503 } else {
504 st->print_cr("not possible; skipped.");
505 }
506 }
507 #endif // PRODUCT
508
509 STEP("printing type of error")
510
511 switch(static_cast<unsigned int>(_id)) {
512 case OOM_MALLOC_ERROR:
513 case OOM_MMAP_ERROR:
514 case OOM_MPROTECT_ERROR:
515 if (_size) {
516 st->print("# Native memory allocation ");
517 st->print((_id == (int)OOM_MALLOC_ERROR) ? "(malloc) failed to allocate " :
518 (_id == (int)OOM_MMAP_ERROR) ? "(mmap) failed to map " :
519 "(mprotect) failed to protect ");
520 jio_snprintf(buf, sizeof(buf), SIZE_FORMAT, _size);
521 st->print("%s", buf);
522 st->print(" bytes");
523 if (strlen(_detail_msg) > 0) {
524 st->print(" for ");
525 st->print("%s", _detail_msg);
526 }
527 st->cr();
528 } else {
529 if (strlen(_detail_msg) > 0) {
530 st->print("# ");
531 st->print_cr("%s", _detail_msg);
532 }
533 }
534 // In error file give some solutions
535 if (_verbose) {
536 print_oom_reasons(st);
537 } else {
538 return; // that's enough for the screen
539 }
540 break;
541 case INTERNAL_ERROR:
542 default:
543 break;
544 }
545
546 STEP("printing exception/signal name")
547
548 st->print_cr("#");
549 st->print("# ");
550 // Is it an OS exception/signal?
551 if (os::exception_name(_id, buf, sizeof(buf))) {
552 st->print("%s", buf);
553 st->print(" (0x%x)", _id); // signal number
554 st->print(" at pc=" PTR_FORMAT, p2i(_pc));
555 if (_siginfo != NULL && os::signal_sent_by_kill(_siginfo)) {
556 st->print(" (sent by kill)");
557 }
558 } else {
559 if (should_report_bug(_id)) {
560 st->print("Internal Error");
561 } else {
562 st->print("Out of Memory Error");
563 }
564 if (_filename != NULL && _lineno > 0) {
565 #ifdef PRODUCT
566 // In product mode chop off pathname?
567 char separator = os::file_separator()[0];
568 const char *p = strrchr(_filename, separator);
569 const char *file = p ? p+1 : _filename;
570 #else
571 const char *file = _filename;
572 #endif
573 st->print(" (%s:%d)", file, _lineno);
574 } else {
575 st->print(" (0x%x)", _id);
576 }
577 }
578
579 STEP("printing current thread and pid")
580
581 // process id, thread id
582 st->print(", pid=%d", os::current_process_id());
583 st->print(", tid=" UINTX_FORMAT, os::current_thread_id());
584 st->cr();
585
586 STEP("printing error message")
587
588 if (should_report_bug(_id)) { // already printed the message.
589 // error message
590 if (strlen(_detail_msg) > 0) {
591 st->print_cr("# %s: %s", _message ? _message : "Error", _detail_msg);
592 } else if (_message) {
593 st->print_cr("# Error: %s", _message);
594 }
595 }
596
597 STEP("printing Java version string")
598
599 report_vm_version(st, buf, sizeof(buf));
600
601 STEP("printing problematic frame")
602
603 // Print current frame if we have a context (i.e. it's a crash)
604 if (_context) {
605 st->print_cr("# Problematic frame:");
606 st->print("# ");
607 frame fr = os::fetch_frame_from_context(_context);
608 fr.print_on_error(st, buf, sizeof(buf));
609 st->cr();
610 st->print_cr("#");
611 }
612
613 STEP("printing core file information")
614 st->print("# ");
615 if (CreateCoredumpOnCrash) {
616 if (coredump_status) {
617 st->print("Core dump will be written. Default location: %s", coredump_message);
618 } else {
619 st->print("No core dump will be written. %s", coredump_message);
620 }
621 } else {
622 st->print("CreateCoredumpOnCrash turned off, no core file dumped");
623 }
624 st->cr();
625 st->print_cr("#");
626
627 JFR_ONLY(STEP("printing jfr information"))
628 JFR_ONLY(Jfr::on_vm_error_report(st);)
629
630 STEP("printing bug submit message")
631
632 if (should_report_bug(_id) && _verbose) {
633 print_bug_submit_message(st, _thread);
634 }
635
636 STEP("printing summary")
637
638 if (_verbose) {
639 st->cr();
640 st->print_cr("--------------- S U M M A R Y ------------");
641 st->cr();
642 }
643
644 STEP("printing VM option summary")
645
646 if (_verbose) {
647 // VM options
648 Arguments::print_summary_on(st);
649 st->cr();
650 }
651
652 STEP("printing summary machine and OS info")
653
654 if (_verbose) {
655 os::print_summary_info(st, buf, sizeof(buf));
656 }
657
658
659 STEP("printing date and time")
660
661 if (_verbose) {
662 os::print_date_and_time(st, buf, sizeof(buf));
663 }
664
665 STEP("printing thread")
666
667 if (_verbose) {
668 st->cr();
669 st->print_cr("--------------- T H R E A D ---------------");
670 st->cr();
671 }
672
673 STEP("printing current thread")
674
675 // current thread
676 if (_verbose) {
677 if (_thread) {
678 st->print("Current thread (" PTR_FORMAT "): ", p2i(_thread));
679 _thread->print_on_error(st, buf, sizeof(buf));
680 st->cr();
681 } else {
682 st->print_cr("Current thread is native thread");
683 }
684 st->cr();
685 }
686
687 STEP("printing current compile task")
688
689 if (_verbose && _thread && _thread->is_Compiler_thread()) {
690 CompilerThread* t = (CompilerThread*)_thread;
691 if (t->task()) {
692 st->cr();
693 st->print_cr("Current CompileTask:");
694 t->task()->print_line_on_error(st, buf, sizeof(buf));
695 st->cr();
696 }
697 }
698
699
700 STEP("printing stack bounds")
701
702 if (_verbose) {
703 st->print("Stack: ");
704
705 address stack_top;
706 size_t stack_size;
707
708 if (_thread) {
709 stack_top = _thread->stack_base();
710 stack_size = _thread->stack_size();
711 } else {
712 stack_top = os::current_stack_base();
713 stack_size = os::current_stack_size();
714 }
715
716 address stack_bottom = stack_top - stack_size;
717 st->print("[" PTR_FORMAT "," PTR_FORMAT "]", p2i(stack_bottom), p2i(stack_top));
718
719 frame fr = _context ? os::fetch_frame_from_context(_context)
720 : os::current_frame();
721
722 if (fr.sp()) {
723 st->print(", sp=" PTR_FORMAT, p2i(fr.sp()));
724 size_t free_stack_size = pointer_delta(fr.sp(), stack_bottom, 1024);
725 st->print(", free space=" SIZE_FORMAT "k", free_stack_size);
726 }
727
728 st->cr();
729 }
730
731 STEP("printing native stack")
732
733 if (_verbose) {
734 if (os::platform_print_native_stack(st, _context, buf, sizeof(buf))) {
735 // We have printed the native stack in platform-specific code
736 // Windows/x64 needs special handling.
737 } else {
738 frame fr = _context ? os::fetch_frame_from_context(_context)
739 : os::current_frame();
740
741 print_native_stack(st, fr, _thread, buf, sizeof(buf));
742 }
743 }
744
745 STEP("printing Java stack")
746
747 if (_verbose && _thread && _thread->is_Java_thread()) {
748 print_stack_trace(st, _thread->as_Java_thread(), buf, sizeof(buf));
749 }
750
751 STEP("printing target Java thread stack")
752
753 // printing Java thread stack trace if it is involved in GC crash
754 if (_verbose && _thread && (_thread->is_Named_thread())) {
755 Thread* thread = ((NamedThread *)_thread)->processed_thread();
756 if (thread != NULL && thread->is_Java_thread()) {
757 JavaThread* jt = thread->as_Java_thread();
758 st->print_cr("JavaThread " PTR_FORMAT " (nid = %d) was being processed", p2i(jt), jt->osthread()->thread_id());
759 print_stack_trace(st, jt, buf, sizeof(buf), true);
760 }
761 }
762
763 STEP("printing siginfo")
764
765 // signal no, signal code, address that caused the fault
766 if (_verbose && _siginfo) {
767 st->cr();
768 os::print_siginfo(st, _siginfo);
769 st->cr();
770 }
771
772 STEP("CDS archive access warning")
773
774 // Print an explicit hint if we crashed on access to the CDS archive.
775 if (_verbose && _siginfo) {
776 check_failing_cds_access(st, _siginfo);
777 st->cr();
778 }
779
780 STEP("printing register info")
781
782 // decode register contents if possible
783 if (_verbose && _context && _thread && Universe::is_fully_initialized()) {
784 ResourceMark rm(_thread);
785 os::print_register_info(st, _context);
786 st->cr();
787 }
788
789 STEP("printing registers, top of stack, instructions near pc")
790
791 // registers, top of stack, instructions near pc
792 if (_verbose && _context) {
793 os::print_context(st, _context);
794 st->cr();
795 }
796
797 STEP("inspecting top of stack")
798
799 // decode stack contents if possible
800 if (_verbose && _context && _thread && Universe::is_fully_initialized()) {
801 frame fr = os::fetch_frame_from_context(_context);
802 const int slots = 8;
803 const intptr_t *start = fr.sp();
804 const intptr_t *end = start + slots;
805 if (is_aligned(start, sizeof(intptr_t)) && os::is_readable_range(start, end)) {
806 st->print_cr("Stack slot to memory mapping:");
807 for (int i = 0; i < slots; ++i) {
808 st->print("stack at sp + %d slots: ", i);
809 ResourceMark rm(_thread);
810 os::print_location(st, *(start + i));
811 }
812 }
813 st->cr();
814 }
815
816 STEP("printing code blob if possible")
817
818 if (_verbose && _context) {
819 CodeBlob* cb = CodeCache::find_blob(_pc);
820 if (cb != NULL) {
821 if (Interpreter::contains(_pc)) {
822 // The interpreter CodeBlob is very large so try to print the codelet instead.
823 InterpreterCodelet* codelet = Interpreter::codelet_containing(_pc);
824 if (codelet != NULL) {
825 codelet->print_on(st);
826 Disassembler::decode(codelet->code_begin(), codelet->code_end(), st);
827 }
828 } else {
829 StubCodeDesc* desc = StubCodeDesc::desc_for(_pc);
830 if (desc != NULL) {
831 desc->print_on(st);
832 Disassembler::decode(desc->begin(), desc->end(), st);
833 } else if (_thread != NULL) {
834 // Disassembling nmethod will incur resource memory allocation,
835 // only do so when thread is valid.
836 ResourceMark rm(_thread);
837 Disassembler::decode(cb, st);
838 st->cr();
839 }
840 }
841 }
842 }
843
844 STEP("printing VM operation")
845
846 if (_verbose && _thread && _thread->is_VM_thread()) {
847 VMThread* t = (VMThread*)_thread;
848 VM_Operation* op = t->vm_operation();
849 if (op) {
850 op->print_on_error(st);
851 st->cr();
852 st->cr();
853 }
854 }
855
856 STEP("printing process")
857
858 if (_verbose) {
859 st->cr();
860 st->print_cr("--------------- P R O C E S S ---------------");
861 st->cr();
862 }
863
864 #ifndef _WIN32
865 STEP("printing user info")
866
867 if (ExtensiveErrorReports && _verbose) {
868 os::Posix::print_user_info(st);
869 }
870 #endif
871
872 STEP("printing all threads")
873
874 // all threads
875 if (_verbose && _thread) {
876 Threads::print_on_error(st, _thread, buf, sizeof(buf));
877 st->cr();
878 }
879
880 STEP("printing VM state")
881
882 if (_verbose) {
883 // Safepoint state
884 st->print("VM state: ");
885
886 if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing");
887 else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint");
888 else st->print("not at safepoint");
889
890 // Also see if error occurred during initialization or shutdown
891 if (!Universe::is_fully_initialized()) {
892 st->print(" (not fully initialized)");
893 } else if (VM_Exit::vm_exited()) {
894 st->print(" (shutting down)");
895 } else {
896 st->print(" (normal execution)");
897 }
898 st->cr();
899 st->cr();
900 }
901
902 STEP("printing owned locks on error")
903
904 // mutexes/monitors that currently have an owner
905 if (_verbose) {
906 print_owned_locks_on_error(st);
907 st->cr();
908 }
909
910 STEP("printing number of OutOfMemoryError and StackOverflow exceptions")
911
912 if (_verbose && Exceptions::has_exception_counts()) {
913 st->print_cr("OutOfMemory and StackOverflow Exception counts:");
914 Exceptions::print_exception_counts_on_error(st);
915 st->cr();
916 }
917
918 #ifdef _LP64
919 STEP("printing compressed oops mode")
920
921 if (_verbose && UseCompressedOops) {
922 CompressedOops::print_mode(st);
923 st->cr();
924 }
925
926 STEP("printing compressed klass pointers mode")
927
928 if (_verbose && UseCompressedClassPointers) {
929 CDS_ONLY(MetaspaceShared::print_on(st);)
930 Metaspace::print_compressed_class_space(st);
931 CompressedKlassPointers::print_mode(st);
932 st->cr();
933 }
934 #endif
935
936 STEP("printing heap information")
937
938 if (_verbose) {
939 GCLogPrecious::print_on_error(st);
940
941 if (Universe::heap() != NULL) {
942 Universe::heap()->print_on_error(st);
943 st->cr();
944 }
945
946 if (Universe::is_fully_initialized()) {
947 st->print_cr("Polling page: " INTPTR_FORMAT, p2i(SafepointMechanism::get_polling_page()));
948 st->cr();
949 }
950 }
951
952 STEP("printing metaspace information")
953
954 if (_verbose && Universe::is_fully_initialized()) {
955 st->print_cr("Metaspace:");
956 MetaspaceUtils::print_basic_report(st, 0);
957 }
958
959 STEP("printing code cache information")
960
961 if (_verbose && Universe::is_fully_initialized()) {
962 // print code cache information before vm abort
963 CodeCache::print_summary(st);
964 st->cr();
965 }
966
967 STEP("printing ring buffers")
968
969 if (_verbose) {
970 Events::print_all(st);
971 st->cr();
972 }
973
974 STEP("printing dynamic libraries")
975
976 if (_verbose) {
977 // dynamic libraries, or memory map
978 os::print_dll_info(st);
979 st->cr();
980 }
981
982 STEP("printing native decoder state")
983
984 if (_verbose) {
985 Decoder::print_state_on(st);
986 st->cr();
987 }
988
989 STEP("printing VM options")
990
991 if (_verbose) {
992 // VM options
993 Arguments::print_on(st);
994 st->cr();
995 }
996
997 STEP("printing flags")
998
999 if (_verbose) {
1000 JVMFlag::printFlags(
1001 st,
1002 true, // with comments
1003 false, // no ranges
1004 true); // skip defaults
1005 st->cr();
1006 }
1007
1008 STEP("printing warning if internal testing API used")
1009
1010 if (WhiteBox::used()) {
1011 st->print_cr("Unsupported internal testing APIs have been used.");
1012 st->cr();
1013 }
1014
1015 STEP("printing log configuration")
1016 if (_verbose){
1017 st->print_cr("Logging:");
1018 LogConfiguration::describe_current_configuration(st);
1019 st->cr();
1020 }
1021
1022 STEP("printing all environment variables")
1023
1024 if (_verbose) {
1025 os::print_environment_variables(st, env_list);
1026 st->cr();
1027 }
1028
1029 STEP("printing signal handlers")
1030
1031 if (_verbose) {
1032 os::print_signal_handlers(st, buf, sizeof(buf));
1033 st->cr();
1034 }
1035
1036 STEP("Native Memory Tracking")
1037 if (_verbose) {
1038 MemTracker::error_report(st);
1039 }
1040
1041 STEP("printing system")
1042
1043 if (_verbose) {
1044 st->cr();
1045 st->print_cr("--------------- S Y S T E M ---------------");
1046 st->cr();
1047 }
1048
1049 STEP("printing OS information")
1050
1051 if (_verbose) {
1052 os::print_os_info(st);
1053 st->cr();
1054 }
1055
1056 STEP("printing CPU info")
1057 if (_verbose) {
1058 os::print_cpu_info(st, buf, sizeof(buf));
1059 st->cr();
1060 }
1061
1062 STEP("printing memory info")
1063
1064 if (_verbose) {
1065 os::print_memory_info(st);
1066 st->cr();
1067 }
1068
1069 STEP("printing internal vm info")
1070
1071 if (_verbose) {
1072 st->print_cr("vm_info: %s", VM_Version::internal_vm_info_string());
1073 st->cr();
1074 }
1075
1076 // print a defined marker to show that error handling finished correctly.
1077 STEP("printing end marker")
1078
1079 if (_verbose) {
1080 st->print_cr("END.");
1081 }
1082
1083 END
1084
1085 # undef BEGIN
1086 # undef STEP
1087 # undef END
1088 }
1089
1090 // Report for the vm_info_cmd. This prints out the information above omitting
1091 // crash and thread specific information. If output is added above, it should be added
1092 // here also, if it is safe to call during a running process.
print_vm_info(outputStream * st)1093 void VMError::print_vm_info(outputStream* st) {
1094
1095 char buf[O_BUFLEN];
1096 report_vm_version(st, buf, sizeof(buf));
1097
1098 // STEP("printing summary")
1099
1100 st->cr();
1101 st->print_cr("--------------- S U M M A R Y ------------");
1102 st->cr();
1103
1104 // STEP("printing VM option summary")
1105
1106 // VM options
1107 Arguments::print_summary_on(st);
1108 st->cr();
1109
1110 // STEP("printing summary machine and OS info")
1111
1112 os::print_summary_info(st, buf, sizeof(buf));
1113
1114 // STEP("printing date and time")
1115
1116 os::print_date_and_time(st, buf, sizeof(buf));
1117
1118 // Skip: STEP("printing thread")
1119
1120 // STEP("printing process")
1121
1122 st->cr();
1123 st->print_cr("--------------- P R O C E S S ---------------");
1124 st->cr();
1125
1126 // STEP("printing number of OutOfMemoryError and StackOverflow exceptions")
1127
1128 if (Exceptions::has_exception_counts()) {
1129 st->print_cr("OutOfMemory and StackOverflow Exception counts:");
1130 Exceptions::print_exception_counts_on_error(st);
1131 st->cr();
1132 }
1133
1134 #ifdef _LP64
1135 // STEP("printing compressed oops mode")
1136 if (UseCompressedOops) {
1137 CompressedOops::print_mode(st);
1138 st->cr();
1139 }
1140
1141 // STEP("printing compressed class ptrs mode")
1142 if (UseCompressedClassPointers) {
1143 CDS_ONLY(MetaspaceShared::print_on(st);)
1144 Metaspace::print_compressed_class_space(st);
1145 CompressedKlassPointers::print_mode(st);
1146 st->cr();
1147 }
1148 #endif
1149
1150 // STEP("printing heap information")
1151
1152 if (Universe::is_fully_initialized()) {
1153 MutexLocker hl(Heap_lock);
1154 GCLogPrecious::print_on_error(st);
1155 Universe::heap()->print_on_error(st);
1156 st->cr();
1157 st->print_cr("Polling page: " INTPTR_FORMAT, p2i(SafepointMechanism::get_polling_page()));
1158 st->cr();
1159 }
1160
1161 // STEP("printing metaspace information")
1162
1163 if (Universe::is_fully_initialized()) {
1164 st->print_cr("Metaspace:");
1165 MetaspaceUtils::print_basic_report(st, 0);
1166 }
1167
1168 // STEP("printing code cache information")
1169
1170 if (Universe::is_fully_initialized()) {
1171 // print code cache information before vm abort
1172 CodeCache::print_summary(st);
1173 st->cr();
1174 }
1175
1176 // STEP("printing ring buffers")
1177
1178 Events::print_all(st);
1179 st->cr();
1180
1181 // STEP("printing dynamic libraries")
1182
1183 // dynamic libraries, or memory map
1184 os::print_dll_info(st);
1185 st->cr();
1186
1187 // STEP("printing VM options")
1188
1189 // VM options
1190 Arguments::print_on(st);
1191 st->cr();
1192
1193 // STEP("printing warning if internal testing API used")
1194
1195 if (WhiteBox::used()) {
1196 st->print_cr("Unsupported internal testing APIs have been used.");
1197 st->cr();
1198 }
1199
1200 // STEP("printing log configuration")
1201 st->print_cr("Logging:");
1202 LogConfiguration::describe(st);
1203 st->cr();
1204
1205 // STEP("printing all environment variables")
1206
1207 os::print_environment_variables(st, env_list);
1208 st->cr();
1209
1210 // STEP("printing signal handlers")
1211
1212 os::print_signal_handlers(st, buf, sizeof(buf));
1213 st->cr();
1214
1215 // STEP("Native Memory Tracking")
1216
1217 MemTracker::error_report(st);
1218
1219 // STEP("printing system")
1220
1221 st->cr();
1222 st->print_cr("--------------- S Y S T E M ---------------");
1223 st->cr();
1224
1225 // STEP("printing OS information")
1226
1227 os::print_os_info(st);
1228 st->cr();
1229
1230 // STEP("printing CPU info")
1231
1232 os::print_cpu_info(st, buf, sizeof(buf));
1233 st->cr();
1234
1235 // STEP("printing memory info")
1236
1237 os::print_memory_info(st);
1238 st->cr();
1239
1240 // STEP("printing internal vm info")
1241
1242 st->print_cr("vm_info: %s", VM_Version::internal_vm_info_string());
1243 st->cr();
1244
1245 // print a defined marker to show that error handling finished correctly.
1246 // STEP("printing end marker")
1247
1248 st->print_cr("END.");
1249 }
1250
1251 volatile intptr_t VMError::_first_error_tid = -1;
1252
1253 /** Expand a pattern into a buffer starting at pos and open a file using constructed path */
expand_and_open(const char * pattern,bool overwrite_existing,char * buf,size_t buflen,size_t pos)1254 static int expand_and_open(const char* pattern, bool overwrite_existing, char* buf, size_t buflen, size_t pos) {
1255 int fd = -1;
1256 int mode = O_RDWR | O_CREAT;
1257 if (overwrite_existing) {
1258 mode |= O_TRUNC;
1259 } else {
1260 mode |= O_EXCL;
1261 }
1262 if (Arguments::copy_expand_pid(pattern, strlen(pattern), &buf[pos], buflen - pos)) {
1263 fd = open(buf, mode, 0666);
1264 }
1265 return fd;
1266 }
1267
1268 /**
1269 * Construct file name for a log file and return it's file descriptor.
1270 * Name and location depends on pattern, default_pattern params and access
1271 * permissions.
1272 */
prepare_log_file(const char * pattern,const char * default_pattern,bool overwrite_existing,char * buf,size_t buflen)1273 static int prepare_log_file(const char* pattern, const char* default_pattern, bool overwrite_existing, char* buf, size_t buflen) {
1274 int fd = -1;
1275
1276 // If possible, use specified pattern to construct log file name
1277 if (pattern != NULL) {
1278 fd = expand_and_open(pattern, overwrite_existing, buf, buflen, 0);
1279 }
1280
1281 // Either user didn't specify, or the user's location failed,
1282 // so use the default name in the current directory
1283 if (fd == -1) {
1284 const char* cwd = os::get_current_directory(buf, buflen);
1285 if (cwd != NULL) {
1286 size_t pos = strlen(cwd);
1287 int fsep_len = jio_snprintf(&buf[pos], buflen-pos, "%s", os::file_separator());
1288 pos += fsep_len;
1289 if (fsep_len > 0) {
1290 fd = expand_and_open(default_pattern, overwrite_existing, buf, buflen, pos);
1291 }
1292 }
1293 }
1294
1295 // try temp directory if it exists.
1296 if (fd == -1) {
1297 const char* tmpdir = os::get_temp_directory();
1298 if (tmpdir != NULL && strlen(tmpdir) > 0) {
1299 int pos = jio_snprintf(buf, buflen, "%s%s", tmpdir, os::file_separator());
1300 if (pos > 0) {
1301 fd = expand_and_open(default_pattern, overwrite_existing, buf, buflen, pos);
1302 }
1303 }
1304 }
1305
1306 return fd;
1307 }
1308
1309 int VMError::_id;
1310 const char* VMError::_message;
1311 char VMError::_detail_msg[1024];
1312 Thread* VMError::_thread;
1313 address VMError::_pc;
1314 void* VMError::_siginfo;
1315 void* VMError::_context;
1316 const char* VMError::_filename;
1317 int VMError::_lineno;
1318 size_t VMError::_size;
1319
report_and_die(Thread * thread,unsigned int sig,address pc,void * siginfo,void * context,const char * detail_fmt,...)1320 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, void* siginfo,
1321 void* context, const char* detail_fmt, ...)
1322 {
1323 va_list detail_args;
1324 va_start(detail_args, detail_fmt);
1325 report_and_die(sig, NULL, detail_fmt, detail_args, thread, pc, siginfo, context, NULL, 0, 0);
1326 va_end(detail_args);
1327 }
1328
report_and_die(Thread * thread,unsigned int sig,address pc,void * siginfo,void * context)1329 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, void* siginfo, void* context)
1330 {
1331 report_and_die(thread, sig, pc, siginfo, context, "%s", "");
1332 }
1333
report_and_die(Thread * thread,void * context,const char * filename,int lineno,const char * message,const char * detail_fmt,va_list detail_args)1334 void VMError::report_and_die(Thread* thread, void* context, const char* filename, int lineno, const char* message,
1335 const char* detail_fmt, va_list detail_args)
1336 {
1337 report_and_die(INTERNAL_ERROR, message, detail_fmt, detail_args, thread, NULL, NULL, context, filename, lineno, 0);
1338 }
1339
report_and_die(Thread * thread,const char * filename,int lineno,size_t size,VMErrorType vm_err_type,const char * detail_fmt,va_list detail_args)1340 void VMError::report_and_die(Thread* thread, const char* filename, int lineno, size_t size,
1341 VMErrorType vm_err_type, const char* detail_fmt, va_list detail_args) {
1342 report_and_die(vm_err_type, NULL, detail_fmt, detail_args, thread, NULL, NULL, NULL, filename, lineno, size);
1343 }
1344
report_and_die(int id,const char * message,const char * detail_fmt,va_list detail_args,Thread * thread,address pc,void * siginfo,void * context,const char * filename,int lineno,size_t size)1345 void VMError::report_and_die(int id, const char* message, const char* detail_fmt, va_list detail_args,
1346 Thread* thread, address pc, void* siginfo, void* context, const char* filename,
1347 int lineno, size_t size)
1348 {
1349 // A single scratch buffer to be used from here on.
1350 // Do not rely on it being preserved across function calls.
1351 static char buffer[O_BUFLEN];
1352
1353 // File descriptor to tty to print an error summary to.
1354 // Hard wired to stdout; see JDK-8215004 (compatibility concerns).
1355 static const int fd_out = 1; // stdout
1356
1357 // File descriptor to the error log file.
1358 static int fd_log = -1;
1359
1360 #ifdef CAN_SHOW_REGISTERS_ON_ASSERT
1361 // Disarm assertion poison page, since from this point on we do not need this mechanism anymore and it may
1362 // cause problems in error handling during native OOM, see JDK-8227275.
1363 disarm_assert_poison();
1364 #endif
1365
1366 // Use local fdStream objects only. Do not use global instances whose initialization
1367 // relies on dynamic initialization (see JDK-8214975). Do not rely on these instances
1368 // to carry over into recursions or invocations from other threads.
1369 fdStream out(fd_out);
1370 out.set_scratch_buffer(buffer, sizeof(buffer));
1371
1372 // Depending on the re-entrance depth at this point, fd_log may be -1 or point to an open hs-err file.
1373 fdStream log(fd_log);
1374 log.set_scratch_buffer(buffer, sizeof(buffer));
1375
1376 // How many errors occurred in error handler when reporting first_error.
1377 static int recursive_error_count;
1378
1379 // We will first print a brief message to standard out (verbose = false),
1380 // then save detailed information in log file (verbose = true).
1381 static bool out_done = false; // done printing to standard out
1382 static bool log_done = false; // done saving error log
1383
1384 intptr_t mytid = os::current_thread_id();
1385 if (_first_error_tid == -1 &&
1386 Atomic::cmpxchg(&_first_error_tid, (intptr_t)-1, mytid) == -1) {
1387
1388 if (SuppressFatalErrorMessage) {
1389 os::abort(CreateCoredumpOnCrash);
1390 }
1391
1392 // Initialize time stamps to use the same base.
1393 out.time_stamp().update_to(1);
1394 log.time_stamp().update_to(1);
1395
1396 _id = id;
1397 _message = message;
1398 _thread = thread;
1399 _pc = pc;
1400 _siginfo = siginfo;
1401 _context = context;
1402 _filename = filename;
1403 _lineno = lineno;
1404 _size = size;
1405 jio_vsnprintf(_detail_msg, sizeof(_detail_msg), detail_fmt, detail_args);
1406
1407 // first time
1408 _error_reported = true;
1409
1410 reporting_started();
1411 if (!TestUnresponsiveErrorHandler) {
1412 // Record reporting_start_time unless we're running the
1413 // TestUnresponsiveErrorHandler test. For that test we record
1414 // reporting_start_time at the beginning of the test.
1415 record_reporting_start_time();
1416 } else {
1417 out.print_raw_cr("Delaying recording reporting_start_time for TestUnresponsiveErrorHandler.");
1418 }
1419
1420 if (ShowMessageBoxOnError || PauseAtExit) {
1421 show_message_box(buffer, sizeof(buffer));
1422
1423 // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
1424 // WatcherThread can kill JVM if the error handler hangs.
1425 ShowMessageBoxOnError = false;
1426 }
1427
1428 os::check_dump_limit(buffer, sizeof(buffer));
1429
1430 // reset signal handlers or exception filter; make sure recursive crashes
1431 // are handled properly.
1432 reset_signal_handlers();
1433 } else {
1434 #if defined(_WINDOWS)
1435 // If UseOSErrorReporting we call this for each level of the call stack
1436 // while searching for the exception handler. Only the first level needs
1437 // to be reported.
1438 if (UseOSErrorReporting && log_done) return;
1439 #endif
1440
1441 // This is not the first error, see if it happened in a different thread
1442 // or in the same thread during error reporting.
1443 if (_first_error_tid != mytid) {
1444 if (!SuppressFatalErrorMessage) {
1445 char msgbuf[64];
1446 jio_snprintf(msgbuf, sizeof(msgbuf),
1447 "[thread " INTX_FORMAT " also had an error]",
1448 mytid);
1449 out.print_raw_cr(msgbuf);
1450 }
1451
1452 // Error reporting is not MT-safe, nor can we let the current thread
1453 // proceed, so we block it.
1454 os::infinite_sleep();
1455
1456 } else {
1457 if (recursive_error_count++ > 30) {
1458 if (!SuppressFatalErrorMessage) {
1459 out.print_raw_cr("[Too many errors, abort]");
1460 }
1461 os::die();
1462 }
1463
1464 if (SuppressFatalErrorMessage) {
1465 // If we already hit a secondary error during abort, then calling
1466 // it again is likely to hit another one. But eventually, if we
1467 // don't deadlock somewhere, we will call os::die() above.
1468 os::abort(CreateCoredumpOnCrash);
1469 }
1470
1471 outputStream* const st = log.is_open() ? &log : &out;
1472 st->cr();
1473
1474 // Timeout handling.
1475 if (_step_did_timeout) {
1476 // The current step had a timeout. Lets continue reporting with the next step.
1477 st->print_raw("[timeout occurred during error reporting in step \"");
1478 st->print_raw(_current_step_info);
1479 st->print_cr("\"] after " INT64_FORMAT " s.",
1480 (int64_t)
1481 ((get_current_timestamp() - _step_start_time) / TIMESTAMP_TO_SECONDS_FACTOR));
1482 } else if (_reporting_did_timeout) {
1483 // We hit ErrorLogTimeout. Reporting will stop altogether. Let's wrap things
1484 // up, the process is about to be stopped by the WatcherThread.
1485 st->print_cr("------ Timeout during error reporting after " INT64_FORMAT " s. ------",
1486 (int64_t)
1487 ((get_current_timestamp() - _reporting_start_time) / TIMESTAMP_TO_SECONDS_FACTOR));
1488 st->flush();
1489 // Watcherthread is about to call os::die. Lets just wait.
1490 os::infinite_sleep();
1491 } else {
1492 // Crash or assert during error reporting. Lets continue reporting with the next step.
1493 stringStream ss(buffer, sizeof(buffer));
1494 // Note: this string does get parsed by a number of jtreg tests,
1495 // see hotspot/jtreg/runtime/ErrorHandling.
1496 ss.print("[error occurred during error reporting (%s), id 0x%x",
1497 _current_step_info, id);
1498 char signal_name[64];
1499 if (os::exception_name(id, signal_name, sizeof(signal_name))) {
1500 ss.print(", %s (0x%x) at pc=" PTR_FORMAT, signal_name, id, p2i(pc));
1501 } else {
1502 if (should_report_bug(id)) {
1503 ss.print(", Internal Error (%s:%d)",
1504 filename == NULL ? "??" : filename, lineno);
1505 } else {
1506 ss.print(", Out of Memory Error (%s:%d)",
1507 filename == NULL ? "??" : filename, lineno);
1508 }
1509 }
1510 ss.print("]");
1511 st->print_raw_cr(buffer);
1512 st->cr();
1513 }
1514 }
1515 }
1516
1517 // Part 1: print an abbreviated version (the '#' section) to stdout.
1518 if (!out_done) {
1519 // Suppress this output if we plan to print Part 2 to stdout too.
1520 // No need to have the "#" section twice.
1521 if (!(ErrorFileToStdout && out.fd() == 1)) {
1522 report(&out, false);
1523 }
1524
1525 out_done = true;
1526
1527 _current_step = 0;
1528 _current_step_info = "";
1529 }
1530
1531 // Part 2: print a full error log file (optionally to stdout or stderr).
1532 // print to error log file
1533 if (!log_done) {
1534 // see if log file is already open
1535 if (!log.is_open()) {
1536 // open log file
1537 if (ErrorFileToStdout) {
1538 fd_log = 1;
1539 } else if (ErrorFileToStderr) {
1540 fd_log = 2;
1541 } else {
1542 fd_log = prepare_log_file(ErrorFile, "hs_err_pid%p.log", true,
1543 buffer, sizeof(buffer));
1544 if (fd_log != -1) {
1545 out.print_raw("# An error report file with more information is saved as:\n# ");
1546 out.print_raw_cr(buffer);
1547 } else {
1548 out.print_raw_cr("# Can not save log file, dump to screen..");
1549 fd_log = 1;
1550 }
1551 }
1552 log.set_fd(fd_log);
1553 }
1554
1555 report(&log, true);
1556 log_done = true;
1557 _current_step = 0;
1558 _current_step_info = "";
1559
1560 if (fd_log > 3) {
1561 close(fd_log);
1562 fd_log = -1;
1563 }
1564
1565 log.set_fd(-1);
1566 }
1567
1568 JFR_ONLY(Jfr::on_vm_shutdown(true);)
1569
1570 if (PrintNMTStatistics) {
1571 fdStream fds(fd_out);
1572 MemTracker::final_report(&fds);
1573 }
1574
1575 static bool skip_replay = ReplayCompiles; // Do not overwrite file during replay
1576 if (DumpReplayDataOnError && _thread && _thread->is_Compiler_thread() && !skip_replay) {
1577 skip_replay = true;
1578 ciEnv* env = ciEnv::current();
1579 if (env != NULL) {
1580 const bool overwrite = false; // We do not overwrite an existing replay file.
1581 int fd = prepare_log_file(ReplayDataFile, "replay_pid%p.log", overwrite, buffer, sizeof(buffer));
1582 if (fd != -1) {
1583 FILE* replay_data_file = os::open(fd, "w");
1584 if (replay_data_file != NULL) {
1585 fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
1586 env->dump_replay_data_unsafe(&replay_data_stream);
1587 out.print_raw("#\n# Compiler replay data is saved as:\n# ");
1588 out.print_raw_cr(buffer);
1589 } else {
1590 int e = errno;
1591 out.print_raw("#\n# Can't open file to dump replay data. Error: ");
1592 out.print_raw_cr(os::strerror(e));
1593 }
1594 }
1595 }
1596 }
1597
1598 static bool skip_bug_url = !should_report_bug(_id);
1599 if (!skip_bug_url) {
1600 skip_bug_url = true;
1601
1602 out.print_raw_cr("#");
1603 print_bug_submit_message(&out, _thread);
1604 }
1605
1606 static bool skip_OnError = false;
1607 if (!skip_OnError && OnError && OnError[0]) {
1608 skip_OnError = true;
1609
1610 // Flush output and finish logs before running OnError commands.
1611 ostream_abort();
1612
1613 out.print_raw_cr("#");
1614 out.print_raw ("# -XX:OnError=\"");
1615 out.print_raw (OnError);
1616 out.print_raw_cr("\"");
1617
1618 char* cmd;
1619 const char* ptr = OnError;
1620 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
1621 out.print_raw ("# Executing ");
1622 #if defined(LINUX) || defined(_ALLBSD_SOURCE)
1623 out.print_raw ("/bin/sh -c ");
1624 #elif defined(_WINDOWS)
1625 out.print_raw ("cmd /C ");
1626 #endif
1627 out.print_raw ("\"");
1628 out.print_raw (cmd);
1629 out.print_raw_cr("\" ...");
1630
1631 if (os::fork_and_exec(cmd) < 0) {
1632 out.print_cr("os::fork_and_exec failed: %s (%s=%d)",
1633 os::strerror(errno), os::errno_name(errno), errno);
1634 }
1635 }
1636
1637 // done with OnError
1638 OnError = NULL;
1639 }
1640
1641 if (WINDOWS_ONLY(!UseOSErrorReporting) NOT_WINDOWS(true)) {
1642 // os::abort() will call abort hooks, try it first.
1643 static bool skip_os_abort = false;
1644 if (!skip_os_abort) {
1645 skip_os_abort = true;
1646 bool dump_core = should_report_bug(_id);
1647 os::abort(dump_core && CreateCoredumpOnCrash, _siginfo, _context);
1648 }
1649
1650 // if os::abort() doesn't abort, try os::die();
1651 os::die();
1652 }
1653 }
1654
1655 /*
1656 * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this
1657 * ensures utilities such as jmap can observe the process is a consistent state.
1658 */
1659 class VM_ReportJavaOutOfMemory : public VM_Operation {
1660 private:
1661 const char* _message;
1662 public:
VM_ReportJavaOutOfMemory(const char * message)1663 VM_ReportJavaOutOfMemory(const char* message) { _message = message; }
type() const1664 VMOp_Type type() const { return VMOp_ReportJavaOutOfMemory; }
1665 void doit();
1666 };
1667
doit()1668 void VM_ReportJavaOutOfMemory::doit() {
1669 // Don't allocate large buffer on stack
1670 static char buffer[O_BUFLEN];
1671
1672 tty->print_cr("#");
1673 tty->print_cr("# java.lang.OutOfMemoryError: %s", _message);
1674 tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError);
1675
1676 // make heap parsability
1677 Universe::heap()->ensure_parsability(false); // no need to retire TLABs
1678
1679 char* cmd;
1680 const char* ptr = OnOutOfMemoryError;
1681 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
1682 tty->print("# Executing ");
1683 #if defined(LINUX)
1684 tty->print ("/bin/sh -c ");
1685 #endif
1686 tty->print_cr("\"%s\"...", cmd);
1687
1688 if (os::fork_and_exec(cmd, true) < 0) {
1689 tty->print_cr("os::fork_and_exec failed: %s (%s=%d)",
1690 os::strerror(errno), os::errno_name(errno), errno);
1691 }
1692 }
1693 }
1694
report_java_out_of_memory(const char * message)1695 void VMError::report_java_out_of_memory(const char* message) {
1696 if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
1697 MutexLocker ml(Heap_lock);
1698 VM_ReportJavaOutOfMemory op(message);
1699 VMThread::execute(&op);
1700 }
1701 }
1702
show_message_box(char * buf,int buflen)1703 void VMError::show_message_box(char *buf, int buflen) {
1704 bool yes;
1705 do {
1706 error_string(buf, buflen);
1707 yes = os::start_debugging(buf,buflen);
1708 } while (yes);
1709 }
1710
1711 // Timeout handling: check if a timeout happened (either a single step did
1712 // timeout or the whole of error reporting hit ErrorLogTimeout). Interrupt
1713 // the reporting thread if that is the case.
check_timeout()1714 bool VMError::check_timeout() {
1715
1716 if (ErrorLogTimeout == 0) {
1717 return false;
1718 }
1719
1720 // Do not check for timeouts if we still have a message box to show to the
1721 // user or if there are OnError handlers to be run.
1722 if (ShowMessageBoxOnError
1723 || (OnError != NULL && OnError[0] != '\0')
1724 || Arguments::abort_hook() != NULL) {
1725 return false;
1726 }
1727
1728 const jlong reporting_start_time_l = get_reporting_start_time();
1729 const jlong now = get_current_timestamp();
1730 // Timestamp is stored in nanos.
1731 if (reporting_start_time_l > 0) {
1732 const jlong end = reporting_start_time_l + (jlong)ErrorLogTimeout * TIMESTAMP_TO_SECONDS_FACTOR;
1733 if (end <= now && !_reporting_did_timeout) {
1734 // We hit ErrorLogTimeout and we haven't interrupted the reporting
1735 // thread yet.
1736 _reporting_did_timeout = true;
1737 interrupt_reporting_thread();
1738 return true; // global timeout
1739 }
1740 }
1741
1742 const jlong step_start_time_l = get_step_start_time();
1743 if (step_start_time_l > 0) {
1744 // A step times out after a quarter of the total timeout. Steps are mostly fast unless they
1745 // hang for some reason, so this simple rule allows for three hanging step and still
1746 // hopefully leaves time enough for the rest of the steps to finish.
1747 const jlong end = step_start_time_l + (jlong)ErrorLogTimeout * TIMESTAMP_TO_SECONDS_FACTOR / 4;
1748 if (end <= now && !_step_did_timeout) {
1749 // The step timed out and we haven't interrupted the reporting
1750 // thread yet.
1751 _step_did_timeout = true;
1752 interrupt_reporting_thread();
1753 return false; // (Not a global timeout)
1754 }
1755 }
1756
1757 return false;
1758
1759 }
1760
1761 #ifndef PRODUCT
1762 typedef void (*voidfun_t)();
1763
1764 // Crash with an authentic sigfpe
1765 volatile int sigfpe_int = 0;
crash_with_sigfpe()1766 static void crash_with_sigfpe() {
1767
1768 // generate a native synchronous SIGFPE where possible;
1769 sigfpe_int = sigfpe_int/sigfpe_int;
1770
1771 // if that did not cause a signal (e.g. on ppc), just
1772 // raise the signal.
1773 #ifndef _WIN32
1774 // OSX implements raise(sig) incorrectly so we need to
1775 // explicitly target the current thread
1776 pthread_kill(pthread_self(), SIGFPE);
1777 #endif
1778
1779 } // end: crash_with_sigfpe
1780
1781 // crash with sigsegv at non-null address.
crash_with_segfault()1782 static void crash_with_segfault() {
1783
1784 char* const crash_addr = (char*) VMError::get_segfault_address();
1785 *crash_addr = 'X';
1786
1787 } // end: crash_with_segfault
1788
test_error_handler()1789 void VMError::test_error_handler() {
1790 controlled_crash(ErrorHandlerTest);
1791 }
1792
1793 // crash in a controlled way:
1794 // how can be one of:
1795 // 1,2 - asserts
1796 // 3,4 - guarantee
1797 // 5-7 - fatal
1798 // 8 - vm_exit_out_of_memory
1799 // 9 - ShouldNotCallThis
1800 // 10 - ShouldNotReachHere
1801 // 11 - Unimplemented
1802 // 12,13 - (not guaranteed) crashes
1803 // 14 - SIGSEGV
1804 // 15 - SIGFPE
controlled_crash(int how)1805 void VMError::controlled_crash(int how) {
1806 if (how == 0) return;
1807
1808 // If asserts are disabled, use the corresponding guarantee instead.
1809 NOT_DEBUG(if (how <= 2) how += 2);
1810
1811 const char* const str = "hello";
1812 const size_t num = (size_t)os::vm_page_size();
1813
1814 const char* const eol = os::line_separator();
1815 const char* const msg = "this message should be truncated during formatting";
1816 char * const dataPtr = NULL; // bad data pointer
1817 const void (*funcPtr)(void); // bad function pointer
1818
1819 #if defined(PPC64) && !defined(ABI_ELFv2) && !defined(ZERO)
1820 struct FunctionDescriptor functionDescriptor;
1821
1822 functionDescriptor.set_entry((address) 0xF);
1823 funcPtr = (const void(*)()) &functionDescriptor;
1824 #else
1825 funcPtr = (const void(*)()) 0xF;
1826 #endif
1827
1828 // Keep this in sync with test/hotspot/jtreg/runtime/ErrorHandling/ErrorHandler.java
1829 // which tests cases 1 thru 13.
1830 // Case 14 is tested by test/hotspot/jtreg/runtime/ErrorHandling/SafeFetchInErrorHandlingTest.java.
1831 // Case 15 is tested by test/hotspot/jtreg/runtime/ErrorHandling/SecondaryErrorTest.java.
1832 // Case 16 is tested by test/hotspot/jtreg/runtime/ErrorHandling/ThreadsListHandleInErrorHandlingTest.java.
1833 // Case 17 is tested by test/hotspot/jtreg/runtime/ErrorHandling/NestedThreadsListHandleInErrorHandlingTest.java.
1834
1835 // We try to grab Threads_lock to keep ThreadsSMRSupport::print_info_on()
1836 // from racing with Threads::add() or Threads::remove() as we
1837 // generate the hs_err_pid file. This makes our ErrorHandling tests
1838 // more stable.
1839 if (!Threads_lock->owned_by_self()) {
1840 Threads_lock->try_lock();
1841 // The VM is going to die so no need to unlock Thread_lock.
1842 }
1843
1844 switch (how) {
1845 case 1: vmassert(str == NULL, "expected null"); break;
1846 case 2: vmassert(num == 1023 && *str == 'X',
1847 "num=" SIZE_FORMAT " str=\"%s\"", num, str); break;
1848 case 3: guarantee(str == NULL, "expected null"); break;
1849 case 4: guarantee(num == 1023 && *str == 'X',
1850 "num=" SIZE_FORMAT " str=\"%s\"", num, str); break;
1851 case 5: fatal("expected null"); break;
1852 case 6: fatal("num=" SIZE_FORMAT " str=\"%s\"", num, str); break;
1853 case 7: fatal("%s%s# %s%s# %s%s# %s%s# %s%s# "
1854 "%s%s# %s%s# %s%s# %s%s# %s%s# "
1855 "%s%s# %s%s# %s%s# %s%s# %s",
1856 msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,
1857 msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,
1858 msg, eol, msg, eol, msg, eol, msg, eol, msg); break;
1859 case 8: vm_exit_out_of_memory(num, OOM_MALLOC_ERROR, "ChunkPool::allocate"); break;
1860 case 9: ShouldNotCallThis(); break;
1861 case 10: ShouldNotReachHere(); break;
1862 case 11: Unimplemented(); break;
1863 // There's no guarantee the bad data pointer will crash us
1864 // so "break" out to the ShouldNotReachHere().
1865 case 12: *dataPtr = '\0'; break;
1866 // There's no guarantee the bad function pointer will crash us
1867 // so "break" out to the ShouldNotReachHere().
1868 case 13: (*funcPtr)(); break;
1869 case 14: crash_with_segfault(); break;
1870 case 15: crash_with_sigfpe(); break;
1871 case 16: {
1872 ThreadsListHandle tlh;
1873 fatal("Force crash with an active ThreadsListHandle.");
1874 }
1875 case 17: {
1876 ThreadsListHandle tlh;
1877 {
1878 ThreadsListHandle tlh2;
1879 fatal("Force crash with a nested ThreadsListHandle.");
1880 }
1881 }
1882 case 18: {
1883 // Check for assert when allocating from resource area without a
1884 // ResourceMark. There must not be a ResourceMark on the
1885 // current stack when invoking this test case.
1886 ResourceArea* area = Thread::current()->resource_area();
1887 assert(area->nesting() == 0, "unexpected ResourceMark");
1888 area->allocate_bytes(100);
1889 break;
1890 }
1891
1892 default: tty->print_cr("ERROR: %d: unexpected test_num value.", how);
1893 }
1894 tty->print_cr("VMError::controlled_crash: survived intentional crash. Did you suppress the assert?");
1895 ShouldNotReachHere();
1896 }
1897 #endif // !PRODUCT
1898