1 /*
2 * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2017, 2024 SAP SE. All rights reserved.
4 * Copyright (c) 2023, 2025, Red Hat, Inc. and/or its affiliates.
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 *
7 * This code is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 only, as
9 * published by the Free Software Foundation.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 */
26
27 #include "cds/aotMetaspace.hpp"
28 #include "code/codeCache.hpp"
29 #include "compiler/compilationFailureInfo.hpp"
30 #include "compiler/compilationMemoryStatistic.hpp"
31 #include "compiler/compileBroker.hpp"
32 #include "compiler/disassembler.hpp"
33 #include "gc/shared/gcConfig.hpp"
34 #include "gc/shared/gcLogPrecious.hpp"
35 #include "jvm.h"
36 #include "logging/logConfiguration.hpp"
37 #include "memory/allocation.hpp"
38 #include "memory/metaspace.hpp"
39 #include "memory/metaspaceUtils.hpp"
40 #include "memory/resourceArea.inline.hpp"
41 #include "memory/universe.hpp"
42 #include "nmt/memTracker.hpp"
43 #include "oops/compressedOops.hpp"
44 #include "prims/whitebox.hpp"
45 #include "runtime/arguments.hpp"
46 #include "runtime/atomicAccess.hpp"
47 #include "runtime/flags/jvmFlag.hpp"
48 #include "runtime/frame.inline.hpp"
49 #include "runtime/init.hpp"
50 #include "runtime/javaThread.inline.hpp"
51 #include "runtime/os.inline.hpp"
52 #include "runtime/osThread.hpp"
53 #include "runtime/safefetch.hpp"
54 #include "runtime/safepointMechanism.hpp"
55 #include "runtime/stackFrameStream.inline.hpp"
56 #include "runtime/stackOverflow.hpp"
57 #include "runtime/threads.hpp"
58 #include "runtime/threadSMR.hpp"
59 #include "runtime/trimNativeHeap.hpp"
60 #include "runtime/vm_version.hpp"
61 #include "runtime/vmOperations.hpp"
62 #include "runtime/vmThread.hpp"
63 #include "sanitizers/ub.hpp"
64 #include "utilities/debug.hpp"
65 #include "utilities/decoder.hpp"
66 #include "utilities/defaultStream.hpp"
67 #include "utilities/events.hpp"
68 #include "utilities/globalDefinitions.hpp"
69 #include "utilities/macros.hpp"
70 #include "utilities/nativeStackPrinter.hpp"
71 #include "utilities/ostream.hpp"
72 #include "utilities/vmError.hpp"
73 #if INCLUDE_JFR
74 #include "jfr/jfr.hpp"
75 #endif
76 #if INCLUDE_JVMCI
77 #include "jvmci/jvmci.hpp"
78 #endif
79
80 #ifndef PRODUCT
81 #include <signal.h>
82 #endif // PRODUCT
83
84 bool VMError::coredump_status;
85 char VMError::coredump_message[O_BUFLEN];
86 int VMError::_current_step;
87 const char* VMError::_current_step_info;
88 volatile jlong VMError::_reporting_start_time = -1;
89 volatile bool VMError::_reporting_did_timeout = false;
90 volatile jlong VMError::_step_start_time = -1;
91 volatile bool VMError::_step_did_timeout = false;
92 volatile bool VMError::_step_did_succeed = false;
93 volatile intptr_t VMError::_first_error_tid = -1;
94 int VMError::_id;
95 const char* VMError::_message;
96 char VMError::_detail_msg[1024];
97 Thread* VMError::_thread;
98 address VMError::_pc;
99 const void* VMError::_siginfo;
100 const void* VMError::_context;
101 bool VMError::_print_stack_from_frame_used = false;
102 const char* VMError::_filename;
103 int VMError::_lineno;
104 size_t VMError::_size;
105 const size_t VMError::_reattempt_required_stack_headroom = 64 * K;
106 const intptr_t VMError::segfault_address = pd_segfault_address;
107 Thread* volatile VMError::_handshake_timed_out_thread = nullptr;
108 Thread* volatile VMError::_safepoint_timed_out_thread = nullptr;
109
110 // List of environment variables that should be reported in error log file.
111 static const char* env_list[] = {
112 // All platforms
113 "JAVA_HOME", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "CLASSPATH",
114 "JDK_AOT_VM_OPTIONS",
115 "JAVA_OPTS", "PATH", "USERNAME",
116
117 "XDG_CACHE_HOME", "XDG_CONFIG_HOME", "FC_LANG", "FONTCONFIG_USE_MMAP",
118
119 // Env variables that are defined on Linux/BSD
120 "LD_LIBRARY_PATH", "LD_PRELOAD", "SHELL", "DISPLAY", "WAYLAND_DISPLAY",
121 "HOSTTYPE", "OSTYPE", "ARCH", "MACHTYPE",
122 "LANG", "LC_ALL", "LC_CTYPE", "LC_NUMERIC", "LC_TIME",
123 "TERM", "TMPDIR", "TZ",
124
125 // defined on AIX
126 "LIBPATH", "LDR_PRELOAD", "LDR_PRELOAD64",
127
128 // defined on Linux/AIX/BSD
129 "_JAVA_SR_SIGNUM",
130
131 // defined on Darwin
132 "DYLD_LIBRARY_PATH", "DYLD_FALLBACK_LIBRARY_PATH",
133 "DYLD_FRAMEWORK_PATH", "DYLD_FALLBACK_FRAMEWORK_PATH",
134 "DYLD_INSERT_LIBRARIES",
135
136 // defined on Windows
137 "OS", "PROCESSOR_IDENTIFIER", "_ALT_JAVA_HOME_DIR", "TMP", "TEMP",
138
139 nullptr // End marker.
140 };
141
142 // A simple parser for lists of commands such as -XX:OnError and -XX:OnOutOfMemoryError
143 // Command list (ptr) is expected to be a sequence of commands delineated by semicolons and/or newlines.
144 // Usage:
145 // ptr = OnError;
146 // while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != nullptr)
147 // ... ...
148 static char* next_OnError_command(char* buf, int buflen, const char** ptr) {
149 if (ptr == nullptr || *ptr == nullptr) return nullptr;
150
151 const char* cmd = *ptr;
152
153 // skip leading blanks, ';' or newlines
154 while (*cmd == ' ' || *cmd == ';' || *cmd == '\n') cmd++;
155
156 if (*cmd == '\0') return nullptr;
157
158 const char * cmdend = cmd;
159 while (*cmdend != '\0' && *cmdend != ';' && *cmdend != '\n') cmdend++;
160
161 Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen);
162
163 *ptr = (*cmdend == '\0' ? cmdend : cmdend + 1);
164 return buf;
165 }
166
167 static void print_bug_submit_message(outputStream *out, Thread *thread) {
168 if (out == nullptr) return;
169 const char *url = Arguments::java_vendor_url_bug();
170 if (url == nullptr || *url == '\0')
171 url = JDK_Version::runtime_vendor_vm_bug_url();
172 if (url != nullptr && *url != '\0') {
173 out->print_raw_cr("# If you would like to submit a bug report, please visit:");
174 out->print_raw ("# ");
175 out->print_raw_cr(url);
176 }
177 // If the crash is in native code, encourage user to submit a bug to the
178 // provider of that code.
179 if (thread && thread->is_Java_thread() &&
180 !thread->is_hidden_from_external_view()) {
181 if (JavaThread::cast(thread)->thread_state() == _thread_in_native) {
182 out->print_cr("# The crash happened outside the Java Virtual Machine in native code.\n# See problematic frame for where to report the bug.");
183 }
184 }
185 out->print_raw_cr("#");
186 }
187
188 static bool stack_has_headroom(size_t headroom) {
189 size_t stack_size = 0;
190 address stack_base = nullptr;
191 os::current_stack_base_and_size(&stack_base, &stack_size);
192
193 const size_t guard_size = StackOverflow::stack_guard_zone_size();
194 const size_t unguarded_stack_size = stack_size - guard_size;
195
196 if (unguarded_stack_size < headroom) {
197 return false;
198 }
199
200 const address unguarded_stack_end = stack_base - unguarded_stack_size;
201 const address stack_pointer = os::current_stack_pointer();
202
203 return stack_pointer >= unguarded_stack_end + headroom;
204 }
205
206 #ifdef ASSERT
207 PRAGMA_DIAG_PUSH
208 PRAGMA_INFINITE_RECURSION_IGNORED
209 void VMError::reattempt_test_hit_stack_limit(outputStream* st) {
210 if (stack_has_headroom(_reattempt_required_stack_headroom)) {
211 // Use all but (_reattempt_required_stack_headroom - K) unguarded stack space.
212 size_t stack_size = 0;
213 address stack_base = nullptr;
214 os::current_stack_base_and_size(&stack_base, &stack_size);
215
216 const size_t guard_size = StackOverflow::stack_guard_zone_size();
217 const address stack_pointer = os::current_stack_pointer();
218
219 const size_t unguarded_stack_size = stack_size - guard_size;
220 const address unguarded_stack_end = stack_base - unguarded_stack_size;
221 const size_t available_headroom = stack_pointer - unguarded_stack_end;
222 const size_t allocation_size = available_headroom - _reattempt_required_stack_headroom + K;
223
224 st->print_cr("Current Stack Pointer: " PTR_FORMAT " alloca %zu"
225 " of %zu bytes available unguarded stack space",
226 p2i(stack_pointer), allocation_size, available_headroom);
227
228 // Allocate byte blob on the stack. Make pointer volatile to avoid having
229 // the compiler removing later reads.
230 volatile char* stack_buffer = static_cast<char*>(alloca(allocation_size));
231 // Initialize the last byte.
232 stack_buffer[allocation_size - 1] = '\0';
233 // Recursive call should hit the stack limit.
234 reattempt_test_hit_stack_limit(st);
235 // Perform a volatile read of the last byte to avoid having the complier
236 // remove the allocation.
237 static_cast<void>(stack_buffer[allocation_size - 1] == '\0');
238 }
239 controlled_crash(14);
240 }
241 PRAGMA_DIAG_POP
242 #endif // ASSERT
243
244 bool VMError::can_reattempt_step(const char* &stop_reason) {
245 if (!stack_has_headroom(_reattempt_required_stack_headroom)) {
246 stop_reason = "Stack headroom limit reached";
247 return false;
248 }
249
250 if (_step_did_timeout) {
251 stop_reason = "Step time limit reached";
252 return false;
253 }
254
255 return true;
256 }
257
258 void VMError::record_coredump_status(const char* message, bool status) {
259 coredump_status = status;
260 strncpy(coredump_message, message, sizeof(coredump_message));
261 coredump_message[sizeof(coredump_message)-1] = 0;
262 }
263
264 // Return a string to describe the error
265 char* VMError::error_string(char* buf, int buflen) {
266 char signame_buf[64];
267 const char *signame = os::exception_name(_id, signame_buf, sizeof(signame_buf));
268
269 if (signame) {
270 jio_snprintf(buf, buflen,
271 "%s (0x%x) at pc=" PTR_FORMAT ", pid=%d, tid=%zu",
272 signame, _id, p2i(_pc),
273 os::current_process_id(), os::current_thread_id());
274 } else if (_filename != nullptr && _lineno > 0) {
275 // skip directory names
276 int n = jio_snprintf(buf, buflen,
277 "Internal Error at %s:%d, pid=%d, tid=%zu",
278 get_filename_only(), _lineno,
279 os::current_process_id(), os::current_thread_id());
280 if (n >= 0 && n < buflen && _message) {
281 if (strlen(_detail_msg) > 0) {
282 jio_snprintf(buf + n, buflen - n, "%s%s: %s",
283 os::line_separator(), _message, _detail_msg);
284 } else {
285 jio_snprintf(buf + n, buflen - n, "%sError: %s",
286 os::line_separator(), _message);
287 }
288 }
289 } else {
290 jio_snprintf(buf, buflen,
291 "Internal Error (0x%x), pid=%d, tid=%zu",
292 _id, os::current_process_id(), os::current_thread_id());
293 }
294
295 return buf;
296 }
297
298 void VMError::print_stack_trace(outputStream* st, JavaThread* jt,
299 char* buf, int buflen, bool verbose) {
300 #ifdef ZERO
301 if (jt->zero_stack()->sp() && jt->top_zero_frame()) {
302 // StackFrameStream uses the frame anchor, which may not have
303 // been set up. This can be done at any time in Zero, however,
304 // so if it hasn't been set up then we just set it up now and
305 // clear it again when we're done.
306 bool has_last_Java_frame = jt->has_last_Java_frame();
307 if (!has_last_Java_frame)
308 jt->set_last_Java_frame();
309 st->print("Java frames:");
310 st->cr();
311
312 // Print the frames
313 StackFrameStream sfs(jt, true /* update */, true /* process_frames */);
314 for(int i = 0; !sfs.is_done(); sfs.next(), i++) {
315 sfs.current()->zero_print_on_error(i, st, buf, buflen);
316 st->cr();
317 }
318
319 // Reset the frame anchor if necessary
320 if (!has_last_Java_frame)
321 jt->reset_last_Java_frame();
322 }
323 #else
324 if (jt->has_last_Java_frame()) {
325 st->print_cr("Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)");
326 for (StackFrameStream sfs(jt, true /* update */, true /* process_frames */); !sfs.is_done(); sfs.next()) {
327 sfs.current()->print_on_error(st, buf, buflen, verbose);
328 st->cr();
329 }
330 }
331 #endif // ZERO
332 }
333
334 const char* VMError::get_filename_only() {
335 char separator = os::file_separator()[0];
336 const char* p = strrchr(_filename, separator);
337 return p ? p + 1 : _filename;
338 }
339
340 /**
341 * Adds `value` to `list` iff it's not already present and there is sufficient
342 * capacity (i.e. length(list) < `list_capacity`). The length of the list
343 * is the index of the first nullptr entry or `list_capacity` if there are
344 * no nullptr entries.
345 *
346 * @ return true if the value was added, false otherwise
347 */
348 static bool add_if_absent(address value, address* list, int list_capacity) {
349 for (int i = 0; i < list_capacity; i++) {
350 if (list[i] == value) {
351 return false;
352 }
353 if (list[i] == nullptr) {
354 list[i] = value;
355 if (i + 1 < list_capacity) {
356 list[i + 1] = nullptr;
357 }
358 return true;
359 }
360 }
361 return false;
362 }
363
364 /**
365 * Prints the VM generated code unit, if any, containing `pc` if it has not already
366 * been printed. If the code unit is an InterpreterCodelet or StubCodeDesc, it is
367 * only printed if `is_crash_pc` is true.
368 *
369 * @param printed array of code units that have already been printed (delimited by nullptr entry)
370 * @param printed_capacity the capacity of `printed`
371 * @return true if the code unit was printed, false otherwise
372 */
373 static bool print_code(outputStream* st, Thread* thread, address pc, bool is_crash_pc,
374 address* printed, int printed_capacity) {
375 if (Interpreter::contains(pc)) {
376 if (is_crash_pc) {
377 // The interpreter CodeBlob is very large so try to print the codelet instead.
378 InterpreterCodelet* codelet = Interpreter::codelet_containing(pc);
379 if (codelet != nullptr) {
380 if (add_if_absent((address) codelet, printed, printed_capacity)) {
381 codelet->print_on(st);
382 Disassembler::decode(codelet->code_begin(), codelet->code_end(), st);
383 return true;
384 }
385 }
386 }
387 } else {
388 StubCodeDesc* desc = StubCodeDesc::desc_for(pc);
389 if (desc != nullptr) {
390 if (is_crash_pc) {
391 if (add_if_absent((address) desc, printed, printed_capacity)) {
392 desc->print_on(st);
393 Disassembler::decode(desc->begin(), desc->end(), st);
394 return true;
395 }
396 }
397 } else if (thread != nullptr) {
398 CodeBlob* cb = CodeCache::find_blob(pc);
399 if (cb != nullptr && add_if_absent((address) cb, printed, printed_capacity)) {
400 // Disassembling nmethod will incur resource memory allocation,
401 // only do so when thread is valid.
402 ResourceMark rm(thread);
403 Disassembler::decode(cb, st);
404 st->cr();
405 return true;
406 }
407 }
408 }
409 return false;
410 }
411
412 // Like above, but only try to figure out a short name. Return nullptr if not found.
413 static const char* find_code_name(address pc) {
414 if (Interpreter::contains(pc)) {
415 InterpreterCodelet* codelet = Interpreter::codelet_containing(pc);
416 if (codelet != nullptr) {
417 return codelet->description();
418 }
419 } else {
420 StubCodeDesc* desc = StubCodeDesc::desc_for(pc);
421 if (desc != nullptr) {
422 return desc->name();
423 } else {
424 CodeBlob* cb = CodeCache::find_blob(pc);
425 if (cb != nullptr) {
426 return cb->name();
427 }
428 }
429 }
430 return nullptr;
431 }
432
433 static void print_oom_reasons(outputStream* st) {
434 st->print_cr("# Possible reasons:");
435 st->print_cr("# The system is out of physical RAM or swap space");
436 #ifdef LINUX
437 st->print_cr("# This process has exceeded the maximum number of memory mappings (check below");
438 st->print_cr("# for `/proc/sys/vm/max_map_count` and `Total number of mappings`)");
439 #endif
440 if (UseCompressedOops) {
441 st->print_cr("# This process is running with CompressedOops enabled, and the Java Heap may be blocking the growth of the native heap");
442 }
443 if (LogBytesPerWord == 2) {
444 st->print_cr("# In 32 bit mode, the process size limit was hit");
445 }
446 st->print_cr("# Possible solutions:");
447 st->print_cr("# Reduce memory load on the system");
448 st->print_cr("# Increase physical memory or swap space");
449 st->print_cr("# Check if swap backing store is full");
450 if (LogBytesPerWord == 2) {
451 st->print_cr("# Use 64 bit Java on a 64 bit OS");
452 }
453 st->print_cr("# Decrease Java heap size (-Xmx/-Xms)");
454 st->print_cr("# Decrease number of Java threads");
455 st->print_cr("# Decrease Java thread stack sizes (-Xss)");
456 st->print_cr("# Set larger code cache with -XX:ReservedCodeCacheSize=");
457 if (UseCompressedOops) {
458 switch (CompressedOops::mode()) {
459 case CompressedOops::UnscaledNarrowOop:
460 st->print_cr("# JVM is running with Unscaled Compressed Oops mode in which the Java heap is");
461 st->print_cr("# placed in the first 4GB address space. The Java Heap base address is the");
462 st->print_cr("# maximum limit for the native heap growth. Please use -XX:HeapBaseMinAddress");
463 st->print_cr("# to set the Java Heap base and to place the Java Heap above 4GB virtual address.");
464 break;
465 case CompressedOops::ZeroBasedNarrowOop:
466 st->print_cr("# JVM is running with Zero Based Compressed Oops mode in which the Java heap is");
467 st->print_cr("# placed in the first 32GB address space. The Java Heap base address is the");
468 st->print_cr("# maximum limit for the native heap growth. Please use -XX:HeapBaseMinAddress");
469 st->print_cr("# to set the Java Heap base and to place the Java Heap above 32GB virtual address.");
470 break;
471 default:
472 break;
473 }
474 }
475 st->print_cr("# This output file may be truncated or incomplete.");
476 }
477
478 static void print_stack_location(outputStream* st, const void* context, int& continuation) {
479 const int number_of_stack_slots = 8;
480
481 int i = continuation;
482 // Update continuation with next index before fetching frame
483 continuation = i + 1;
484 const frame fr = os::fetch_frame_from_context(context);
485 while (i < number_of_stack_slots) {
486 // Update continuation with next index before printing location
487 continuation = i + 1;
488 // decode stack contents if possible
489 const intptr_t *sp = fr.sp();
490 const intptr_t *slot = sp + i;
491 if (!is_aligned(slot, sizeof(intptr_t))) {
492 st->print_cr("Misaligned sp: " PTR_FORMAT, p2i(sp));
493 break;
494 } else if (os::is_readable_pointer(slot)) {
495 st->print("stack at sp + %d slots: ", i);
496 os::print_location(st, *(slot));
497 } else {
498 st->print_cr("unreadable stack slot at sp + %d", i);
499 }
500 ++i;
501 }
502 }
503
504 static void report_vm_version(outputStream* st, char* buf, int buflen) {
505 // VM version
506 st->print_cr("#");
507 JDK_Version::current().to_string(buf, buflen);
508 const char* runtime_name = JDK_Version::runtime_name() != nullptr ?
509 JDK_Version::runtime_name() : "";
510 const char* runtime_version = JDK_Version::runtime_version() != nullptr ?
511 JDK_Version::runtime_version() : "";
512 const char* vendor_version = JDK_Version::runtime_vendor_version() != nullptr ?
513 JDK_Version::runtime_vendor_version() : "";
514 const char* jdk_debug_level = VM_Version::printable_jdk_debug_level() != nullptr ?
515 VM_Version::printable_jdk_debug_level() : "";
516
517 st->print_cr("# JRE version: %s%s%s (%s) (%sbuild %s)", runtime_name,
518 (*vendor_version != '\0') ? " " : "", vendor_version,
519 buf, jdk_debug_level, runtime_version);
520
521 // This is the long version with some default settings added
522 st->print_cr("# Java VM: %s%s%s (%s%s, %s%s%s%s%s%s, %s, %s)",
523 VM_Version::vm_name(),
524 (*vendor_version != '\0') ? " " : "", vendor_version,
525 jdk_debug_level,
526 VM_Version::vm_release(),
527 VM_Version::vm_info_string(),
528 TieredCompilation ? ", tiered" : "",
529 #if INCLUDE_JVMCI
530 EnableJVMCI ? ", jvmci" : "",
531 UseJVMCICompiler ? ", jvmci compiler" : "",
532 #else
533 "", "",
534 #endif
535 UseCompressedOops ? ", compressed oops" : "",
536 UseCompactObjectHeaders ? ", compact obj headers"
537 : (UseCompressedClassPointers ? ", compressed class ptrs" : ""),
538 GCConfig::hs_err_name(),
539 VM_Version::vm_platform_string()
540 );
541 }
542
543 // Returns true if at least one thread reported a fatal error and fatal error handling is in process.
544 bool VMError::is_error_reported() {
545 return _first_error_tid != -1;
546 }
547
548 // Returns true if the current thread reported a fatal error.
549 bool VMError::is_error_reported_in_current_thread() {
550 return _first_error_tid == os::current_thread_id();
551 }
552
553 // Helper, return current timestamp for timeout handling.
554 jlong VMError::get_current_timestamp() {
555 return os::javaTimeNanos();
556 }
557 // Factor to translate the timestamp to seconds.
558 #define TIMESTAMP_TO_SECONDS_FACTOR (1000 * 1000 * 1000)
559
560 void VMError::record_reporting_start_time() {
561 const jlong now = get_current_timestamp();
562 AtomicAccess::store(&_reporting_start_time, now);
563 }
564
565 jlong VMError::get_reporting_start_time() {
566 return AtomicAccess::load(&_reporting_start_time);
567 }
568
569 void VMError::record_step_start_time() {
570 const jlong now = get_current_timestamp();
571 AtomicAccess::store(&_step_start_time, now);
572 }
573
574 jlong VMError::get_step_start_time() {
575 return AtomicAccess::load(&_step_start_time);
576 }
577
578 void VMError::clear_step_start_time() {
579 return AtomicAccess::store(&_step_start_time, (jlong)0);
580 }
581
582 // This is the main function to report a fatal error. Only one thread can
583 // call this function, so we don't need to worry about MT-safety. But it's
584 // possible that the error handler itself may crash or die on an internal
585 // error, for example, when the stack/heap is badly damaged. We must be
586 // able to handle recursive errors that happen inside error handler.
587 //
588 // Error reporting is done in several steps. If a crash or internal error
589 // occurred when reporting an error, the nested signal/exception handler
590 // can skip steps that are already (or partially) done. Error reporting will
591 // continue from the next step. This allows us to retrieve and print
592 // information that may be unsafe to get after a fatal error. If it happens,
593 // you may find nested report_and_die() frames when you look at the stack
594 // in a debugger.
595 //
596 // In general, a hang in error handler is much worse than a crash or internal
597 // error, as it's harder to recover from a hang. Deadlock can happen if we
598 // try to grab a lock that is already owned by current thread, or if the
599 // owner is blocked forever (e.g. in os::infinite_sleep()). If possible, the
600 // error handler and all the functions it called should avoid grabbing any
601 // lock. An important thing to notice is that memory allocation needs a lock.
602 //
603 // We should avoid using large stack allocated buffers. Many errors happen
604 // when stack space is already low. Making things even worse is that there
605 // could be nested report_and_die() calls on stack (see above). Only one
606 // thread can report error, so large buffers are statically allocated in data
607 // segment.
608 void VMError::report(outputStream* st, bool _verbose) {
609 // Used by reattempt step logic
610 static int continuation = 0;
611 const char* stop_reattempt_reason = nullptr;
612 # define BEGIN \
613 if (_current_step == 0) { \
614 _step_did_succeed = false; \
615 _current_step = __LINE__; \
616 {
617 // [Begin logic]
618
619 # define STEP_IF(s, cond) \
620 } \
621 _step_did_succeed = true; \
622 } \
623 if (_current_step < __LINE__) { \
624 _step_did_succeed = false; \
625 _current_step = __LINE__; \
626 _current_step_info = s; \
627 if ((cond)) { \
628 record_step_start_time(); \
629 _step_did_timeout = false;
630 // [Step logic]
631
632 # define STEP(s) STEP_IF(s, true)
633
634 # define REATTEMPT_STEP_IF(s, cond) \
635 } \
636 _step_did_succeed = true; \
637 } \
638 if (_current_step < __LINE__ && !_step_did_succeed) { \
639 _current_step = __LINE__; \
640 _current_step_info = s; \
641 const bool cond_value = (cond); \
642 if (cond_value && !can_reattempt_step( \
643 stop_reattempt_reason)) { \
644 st->print_cr("[stop reattempt (%s) reason: %s]", \
645 _current_step_info, \
646 stop_reattempt_reason); \
647 } else if (cond_value) {
648 // [Continue Step logic]
649
650 # define END \
651 } \
652 _step_did_succeed = true; \
653 clear_step_start_time(); \
654 }
655
656 // don't allocate large buffer on stack
657 static char buf[O_BUFLEN];
658
659 // Native stack trace may get stuck. We try to handle the last pc if it
660 // belongs to VM generated code.
661 address lastpc = nullptr;
662
663 BEGIN
664 if (MemTracker::enabled() &&
665 NmtVirtualMemory_lock != nullptr &&
666 NmtVirtualMemory_lock->owned_by_self()) {
667 // Manually unlock to avoid reentrancy due to mallocs in detailed mode.
668 NmtVirtualMemory_lock->unlock();
669 }
670
671 STEP("printing fatal error message")
672 st->print_cr("#");
673 if (should_report_bug(_id)) {
674 st->print_cr("# A fatal error has been detected by the Java Runtime Environment:");
675 } else {
676 st->print_cr("# There is insufficient memory for the Java "
677 "Runtime Environment to continue.");
678 }
679
680 // avoid the cache update for malloc/mmap errors
681 if (should_report_bug(_id)) {
682 os::prepare_native_symbols();
683 }
684
685 #ifdef ASSERT
686 // Error handler self tests
687 // Meaning of codes passed through in the tests.
688 #define TEST_SECONDARY_CRASH 14
689 #define TEST_REATTEMPT_SECONDARY_CRASH 15
690 #define TEST_RESOURCE_MARK_CRASH 2
691
692 // test secondary error handling. Test it twice, to test that resetting
693 // error handler after a secondary crash works.
694 STEP_IF("test secondary crash 1", _verbose && TestCrashInErrorHandler == TEST_SECONDARY_CRASH)
695 st->print_cr("Will crash now (TestCrashInErrorHandler=%u)...",
696 TestCrashInErrorHandler);
697 controlled_crash(TestCrashInErrorHandler);
698
699 STEP_IF("test secondary crash 2", _verbose && TestCrashInErrorHandler == TEST_SECONDARY_CRASH)
700 st->print_cr("Will crash now (TestCrashInErrorHandler=%u)...",
701 TestCrashInErrorHandler);
702 controlled_crash(TestCrashInErrorHandler);
703
704 // See corresponding test in test/runtime/ErrorHandling/ReattemptErrorTest.java
705 STEP_IF("test reattempt secondary crash",
706 _verbose && TestCrashInErrorHandler == TEST_REATTEMPT_SECONDARY_CRASH)
707 st->print_cr("Will crash now (TestCrashInErrorHandler=%u)...",
708 TestCrashInErrorHandler);
709 controlled_crash(14);
710
711 REATTEMPT_STEP_IF("test reattempt secondary crash, attempt 2",
712 _verbose && TestCrashInErrorHandler == TEST_REATTEMPT_SECONDARY_CRASH)
713 st->print_cr("test reattempt secondary crash. attempt 2");
714
715 REATTEMPT_STEP_IF("test reattempt secondary crash, attempt 3",
716 _verbose && TestCrashInErrorHandler == TEST_REATTEMPT_SECONDARY_CRASH)
717 st->print_cr("test reattempt secondary crash. attempt 3");
718
719 STEP_IF("test reattempt timeout",
720 _verbose && TestCrashInErrorHandler == TEST_REATTEMPT_SECONDARY_CRASH)
721 st->print_cr("test reattempt timeout");
722 os::infinite_sleep();
723
724 REATTEMPT_STEP_IF("test reattempt timeout, attempt 2",
725 _verbose && TestCrashInErrorHandler == TEST_REATTEMPT_SECONDARY_CRASH)
726 st->print_cr("test reattempt timeout, attempt 2");
727
728 STEP_IF("test reattempt stack headroom",
729 _verbose && TestCrashInErrorHandler == TEST_REATTEMPT_SECONDARY_CRASH)
730 st->print_cr("test reattempt stack headroom");
731 reattempt_test_hit_stack_limit(st);
732
733 REATTEMPT_STEP_IF("test reattempt stack headroom, attempt 2",
734 _verbose && TestCrashInErrorHandler == TEST_REATTEMPT_SECONDARY_CRASH)
735 st->print_cr("test reattempt stack headroom, attempt 2");
736
737 STEP_IF("test missing ResourceMark does not crash",
738 _verbose && TestCrashInErrorHandler == TEST_RESOURCE_MARK_CRASH)
739 stringStream message;
740 message.print("This is a message with no ResourceMark");
741 tty->print_cr("%s", message.as_string());
742
743 // TestUnresponsiveErrorHandler: We want to test both step timeouts and global timeout.
744 // Step to global timeout ratio is 4:1, so in order to be absolutely sure we hit the
745 // global timeout, let's execute the timeout step five times.
746 // See corresponding test in test/runtime/ErrorHandling/TimeoutInErrorHandlingTest.java
747 STEP_IF("setup for test unresponsive error reporting step",
748 _verbose && TestUnresponsiveErrorHandler)
749 // We record reporting_start_time for this test here because we
750 // care about the time spent executing TIMEOUT_TEST_STEP and not
751 // about the time it took us to get here.
752 tty->print_cr("Recording reporting_start_time for TestUnresponsiveErrorHandler.");
753 record_reporting_start_time();
754
755 #define TIMEOUT_TEST_STEP STEP_IF("test unresponsive error reporting step", \
756 _verbose && TestUnresponsiveErrorHandler) \
757 os::infinite_sleep();
758 TIMEOUT_TEST_STEP
759 TIMEOUT_TEST_STEP
760 TIMEOUT_TEST_STEP
761 TIMEOUT_TEST_STEP
762 TIMEOUT_TEST_STEP
763
764 STEP_IF("test safefetch in error handler", _verbose && TestSafeFetchInErrorHandler)
765 // test whether it is safe to use SafeFetch32 in Crash Handler. Test twice
766 // to test that resetting the signal handler works correctly.
767 st->print_cr("Will test SafeFetch...");
768 int* const invalid_pointer = (int*)segfault_address;
769 const int x = 0x76543210;
770 int i1 = SafeFetch32(invalid_pointer, x);
771 int i2 = SafeFetch32(invalid_pointer, x);
772 if (i1 == x && i2 == x) {
773 st->print_cr("SafeFetch OK."); // Correctly deflected and returned default pattern
774 } else {
775 st->print_cr("??");
776 }
777 #endif // ASSERT
778
779 STEP("printing type of error")
780 switch(static_cast<unsigned int>(_id)) {
781 case OOM_MALLOC_ERROR:
782 case OOM_MMAP_ERROR:
783 case OOM_MPROTECT_ERROR:
784 if (_size) {
785 st->print("# Native memory allocation ");
786 st->print((_id == (int)OOM_MALLOC_ERROR) ? "(malloc) failed to allocate " :
787 (_id == (int)OOM_MMAP_ERROR) ? "(mmap) failed to map " :
788 "(mprotect) failed to protect ");
789 jio_snprintf(buf, sizeof(buf), "%zu", _size);
790 st->print("%s", buf);
791 st->print(" bytes.");
792 if (strlen(_detail_msg) > 0) {
793 st->print(" Error detail: ");
794 st->print("%s", _detail_msg);
795 }
796 st->cr();
797 } else {
798 if (strlen(_detail_msg) > 0) {
799 st->print("# ");
800 st->print_cr("%s", _detail_msg);
801 }
802 }
803 // In error file give some solutions
804 if (_verbose) {
805 print_oom_reasons(st);
806 } else {
807 return; // that's enough for the screen
808 }
809 break;
810 case INTERNAL_ERROR:
811 default:
812 break;
813 }
814
815 STEP("printing exception/signal name")
816 st->print_cr("#");
817 st->print("# ");
818 // Is it an OS exception/signal?
819 if (os::exception_name(_id, buf, sizeof(buf))) {
820 st->print("%s", buf);
821 st->print(" (0x%x)", _id); // signal number
822 st->print(" at pc=" PTR_FORMAT, p2i(_pc));
823 if (_siginfo != nullptr && os::signal_sent_by_kill(_siginfo)) {
824 if (get_handshake_timed_out_thread() == _thread) {
825 st->print(" (sent by handshake timeout handler)");
826 } else if (get_safepoint_timed_out_thread() == _thread) {
827 st->print(" (sent by safepoint timeout handler)");
828 } else {
829 st->print(" (sent by kill)");
830 }
831 }
832 } else {
833 if (should_report_bug(_id)) {
834 st->print("Internal Error");
835 } else {
836 st->print("Out of Memory Error");
837 }
838 if (_filename != nullptr && _lineno > 0) {
839 #ifdef PRODUCT
840 // In product mode chop off pathname
841 const char *file = get_filename_only();
842 #else
843 const char *file = _filename;
844 #endif
845 st->print(" (%s:%d)", file, _lineno);
846 } else {
847 st->print(" (0x%x)", _id);
848 }
849 }
850
851 STEP("printing current thread and pid")
852 // process id, thread id
853 st->print(", pid=%d", os::current_process_id());
854 st->print(", tid=%zu", os::current_thread_id());
855 st->cr();
856
857 STEP_IF("printing error message", should_report_bug(_id)) // already printed the message.
858 // error message
859 if (strlen(_detail_msg) > 0) {
860 st->print_cr("# %s: %s", _message ? _message : "Error", _detail_msg);
861 } else if (_message) {
862 st->print_cr("# Error: %s", _message);
863 }
864
865 STEP("printing Java version string")
866 report_vm_version(st, buf, sizeof(buf));
867
868 STEP_IF("printing problematic frame", _context != nullptr)
869 // Print current frame if we have a context (i.e. it's a crash)
870 st->print_cr("# Problematic frame:");
871 st->print("# ");
872 frame fr = os::fetch_frame_from_context(_context);
873 fr.print_on_error(st, buf, sizeof(buf));
874 st->cr();
875 st->print_cr("#");
876
877 STEP("printing core file information")
878 st->print("# ");
879 if (CreateCoredumpOnCrash) {
880 if (coredump_status) {
881 st->print("Core dump will be written. Default location: %s", coredump_message);
882 } else {
883 st->print("No core dump will be written. %s", coredump_message);
884 }
885 } else {
886 st->print("CreateCoredumpOnCrash turned off, no core file dumped");
887 }
888 st->cr();
889 st->print_cr("#");
890
891 JFR_ONLY(STEP("printing jfr information"))
892 JFR_ONLY(Jfr::on_vm_error_report(st);)
893
894 STEP_IF("printing bug submit message", should_submit_bug_report(_id) && _verbose)
895 print_bug_submit_message(st, _thread);
896
897 STEP_IF("printing summary", _verbose)
898 st->cr();
899 st->print_cr("--------------- S U M M A R Y ------------");
900 st->cr();
901
902 STEP_IF("printing VM option summary", _verbose)
903 // VM options
904 Arguments::print_summary_on(st);
905 st->cr();
906
907 STEP_IF("printing summary machine and OS info", _verbose)
908 os::print_summary_info(st, buf, sizeof(buf));
909
910 STEP_IF("printing date and time", _verbose)
911 os::print_date_and_time(st, buf, sizeof(buf));
912
913 STEP_IF("printing thread", _verbose)
914 st->cr();
915 st->print_cr("--------------- T H R E A D ---------------");
916 st->cr();
917
918 STEP_IF("printing current thread", _verbose)
919 // current thread
920 if (_thread) {
921 st->print("Current thread (" PTR_FORMAT "): ", p2i(_thread));
922 _thread->print_on_error(st, buf, sizeof(buf));
923 st->cr();
924 } else {
925 st->print_cr("Current thread is native thread");
926 }
927 st->cr();
928
929 STEP_IF("printing current compile task",
930 _verbose && _thread != nullptr && _thread->is_Compiler_thread())
931 CompilerThread* t = (CompilerThread*)_thread;
932 if (t->task()) {
933 st->cr();
934 st->print_cr("Current CompileTask:");
935 t->task()->print_line_on_error(st, buf, sizeof(buf));
936 st->cr();
937 }
938
939 STEP_IF("printing stack bounds", _verbose)
940 st->print("Stack: ");
941
942 address stack_top;
943 size_t stack_size;
944
945 if (_thread) {
946 stack_top = _thread->stack_base();
947 stack_size = _thread->stack_size();
948 } else {
949 os::current_stack_base_and_size(&stack_top, &stack_size);
950 }
951
952 address stack_bottom = stack_top - stack_size;
953 st->print("[" PTR_FORMAT "," PTR_FORMAT "]", p2i(stack_bottom), p2i(stack_top));
954
955 frame fr = _context ? os::fetch_frame_from_context(_context)
956 : os::current_frame();
957
958 if (fr.sp()) {
959 st->print(", sp=" PTR_FORMAT, p2i(fr.sp()));
960 size_t free_stack_size = pointer_delta(fr.sp(), stack_bottom, 1024);
961 st->print(", free space=%zuk", free_stack_size);
962 }
963
964 st->cr();
965
966 STEP_IF("printing native stack (with source info)", _verbose)
967
968 NativeStackPrinter nsp(_thread, _context, _filename != nullptr ? get_filename_only() : nullptr, _lineno);
969 if (nsp.print_stack(st, buf, sizeof(buf), lastpc,
970 true /*print_source_info */, -1 /* max stack */)) {
971 // We have printed the native stack in platform-specific code
972 // Windows/x64 needs special handling.
973 // Stack walking may get stuck. Try to find the calling code.
974 if (lastpc != nullptr) {
975 const char* name = find_code_name(lastpc);
976 if (name != nullptr) {
977 st->print_cr("The last pc belongs to %s (printed below).", name);
978 }
979 }
980 } else {
981 _print_stack_from_frame_used = true; // frame-based native stack walk done
982 }
983
984 REATTEMPT_STEP_IF("retry printing native stack (no source info)", _verbose)
985 st->cr();
986 st->print_cr("Retrying call stack printing without source information...");
987 NativeStackPrinter nsp(_thread, _context, get_filename_only(), _lineno);
988 nsp.print_stack_from_frame(st, buf, sizeof(buf),
989 false /*print_source_info */, -1 /* max stack */);
990 _print_stack_from_frame_used = true;
991
992 STEP_IF("printing Java stack", _verbose && _thread && _thread->is_Java_thread())
993 if (_verbose && _thread && _thread->is_Java_thread()) {
994 print_stack_trace(st, JavaThread::cast(_thread), buf, sizeof(buf));
995 }
996
997 STEP_IF("printing target Java thread stack",
998 _verbose && _thread != nullptr && (_thread->is_Named_thread()))
999 // printing Java thread stack trace if it is involved in GC crash
1000 Thread* thread = ((NamedThread *)_thread)->processed_thread();
1001 if (thread != nullptr && thread->is_Java_thread()) {
1002 JavaThread* jt = JavaThread::cast(thread);
1003 st->print_cr("JavaThread " PTR_FORMAT " (nid = %d) was being processed", p2i(jt), jt->osthread()->thread_id());
1004 print_stack_trace(st, jt, buf, sizeof(buf), true);
1005 }
1006
1007 STEP_IF("printing siginfo", _verbose && _siginfo != nullptr)
1008 // signal no, signal code, address that caused the fault
1009 st->cr();
1010 os::print_siginfo(st, _siginfo);
1011 st->cr();
1012
1013 STEP_IF("CDS archive access warning", _verbose && _siginfo != nullptr)
1014 // Print an explicit hint if we crashed on access to the CDS archive.
1015 check_failing_cds_access(st, _siginfo);
1016 st->cr();
1017
1018 #if defined(COMPILER1) || defined(COMPILER2)
1019 STEP_IF("printing pending compilation failure",
1020 _verbose && _thread != nullptr && _thread->is_Compiler_thread())
1021 CompilationFailureInfo::print_pending_compilation_failure(st);
1022 if (CompilationMemoryStatistic::enabled() && CompilationMemoryStatistic::in_oom_crash()) {
1023 st->cr();
1024 st->print_cr(">> Please see below for a detailed breakdown of compiler memory usage.");
1025 st->cr();
1026 }
1027 #endif
1028
1029 STEP_IF("printing registers", _verbose && _context != nullptr)
1030 // printing registers
1031 os::print_context(st, _context);
1032 st->cr();
1033
1034 STEP_IF("printing register info",
1035 _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1036 continuation = 0;
1037 ResourceMark rm(_thread);
1038 st->print_cr("Register to memory mapping:");
1039 st->cr();
1040 os::print_register_info(st, _context, continuation);
1041 st->cr();
1042
1043 REATTEMPT_STEP_IF("printing register info, attempt 2",
1044 _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1045 ResourceMark rm(_thread);
1046 os::print_register_info(st, _context, continuation);
1047 st->cr();
1048
1049 REATTEMPT_STEP_IF("printing register info, attempt 3",
1050 _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1051 ResourceMark rm(_thread);
1052 os::print_register_info(st, _context, continuation);
1053 st->cr();
1054
1055 STEP_IF("printing top of stack, instructions near pc", _verbose && _context != nullptr)
1056 // printing top of stack, instructions near pc
1057 os::print_tos_pc(st, _context);
1058 st->cr();
1059
1060 STEP_IF("inspecting top of stack",
1061 _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1062 continuation = 0;
1063 ResourceMark rm(_thread);
1064 st->print_cr("Stack slot to memory mapping:");
1065 st->cr();
1066 print_stack_location(st, _context, continuation);
1067 st->cr();
1068
1069 REATTEMPT_STEP_IF("inspecting top of stack, attempt 2",
1070 _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1071 ResourceMark rm(_thread);
1072 print_stack_location(st, _context, continuation);
1073 st->cr();
1074
1075 REATTEMPT_STEP_IF("inspecting top of stack, attempt 3",
1076 _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1077 ResourceMark rm(_thread);
1078 print_stack_location(st, _context, continuation);
1079 st->cr();
1080
1081 STEP_IF("printing lock stack", _verbose && _thread != nullptr && _thread->is_Java_thread());
1082 st->print_cr("Lock stack of current Java thread (top to bottom):");
1083 JavaThread::cast(_thread)->lock_stack().print_on(st);
1084 st->cr();
1085
1086 STEP_IF("printing code blobs if possible", _verbose)
1087 const int printed_capacity = max_error_log_print_code;
1088 address printed[printed_capacity];
1089 printed[0] = nullptr;
1090 int printed_len = 0;
1091 // Even though ErrorLogPrintCodeLimit is ranged checked
1092 // during argument parsing, there's no way to prevent it
1093 // subsequently (i.e., after parsing) being set to a
1094 // value outside the range.
1095 int limit = MIN2(ErrorLogPrintCodeLimit, printed_capacity);
1096 if (limit > 0) {
1097 // Check if a pc was found by native stack trace above.
1098 if (lastpc != nullptr) {
1099 if (print_code(st, _thread, lastpc, true, printed, printed_capacity)) {
1100 printed_len++;
1101 }
1102 }
1103
1104 // Scan the native stack
1105 if (!_print_stack_from_frame_used) {
1106 // Only try to print code of the crashing frame since
1107 // the native stack cannot be walked with next_frame.
1108 if (print_code(st, _thread, _pc, true, printed, printed_capacity)) {
1109 printed_len++;
1110 }
1111 } else {
1112 frame fr = _context ? os::fetch_frame_from_context(_context)
1113 : os::current_frame();
1114 while (printed_len < limit && fr.pc() != nullptr) {
1115 if (print_code(st, _thread, fr.pc(), fr.pc() == _pc, printed, printed_capacity)) {
1116 printed_len++;
1117 }
1118 fr = frame::next_frame(fr, _thread);
1119 }
1120 }
1121
1122 // Scan the Java stack
1123 if (_thread != nullptr && _thread->is_Java_thread()) {
1124 JavaThread* jt = JavaThread::cast(_thread);
1125 if (jt->has_last_Java_frame()) {
1126 for (StackFrameStream sfs(jt, true /* update */, true /* process_frames */); printed_len < limit && !sfs.is_done(); sfs.next()) {
1127 address pc = sfs.current()->pc();
1128 if (print_code(st, _thread, pc, pc == _pc, printed, printed_capacity)) {
1129 printed_len++;
1130 }
1131 }
1132 }
1133 }
1134 }
1135
1136 STEP_IF("printing VM operation", _verbose && _thread != nullptr && _thread->is_VM_thread())
1137 VMThread* t = (VMThread*)_thread;
1138 VM_Operation* op = t->vm_operation();
1139 if (op) {
1140 op->print_on_error(st);
1141 st->cr();
1142 st->cr();
1143 }
1144
1145 STEP_IF("printing registered callbacks", _verbose && _thread != nullptr);
1146 size_t count = 0;
1147 for (VMErrorCallback* callback = _thread->_vm_error_callbacks;
1148 callback != nullptr;
1149 callback = callback->_next) {
1150 st->print_cr("VMErrorCallback %zu:", ++count);
1151 callback->call(st);
1152 st->cr();
1153 }
1154
1155 STEP_IF("printing process", _verbose)
1156 st->cr();
1157 st->print_cr("--------------- P R O C E S S ---------------");
1158 st->cr();
1159
1160 STEP_IF("printing user info", ExtensiveErrorReports && _verbose)
1161 os::print_user_info(st);
1162
1163 STEP_IF("printing all threads", _verbose && _thread != nullptr)
1164 // all threads
1165 Threads::print_on_error(st, _thread, buf, sizeof(buf));
1166 st->cr();
1167
1168 STEP_IF("printing VM state", _verbose)
1169 // Safepoint state
1170 st->print("VM state: ");
1171
1172 if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing");
1173 else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint");
1174 else st->print("not at safepoint");
1175
1176 // Also see if error occurred during initialization or shutdown
1177 if (!Universe::is_fully_initialized()) {
1178 st->print(" (not fully initialized)");
1179 } else if (VM_Exit::vm_exited()) {
1180 st->print(" (shutting down)");
1181 } else {
1182 st->print(" (normal execution)");
1183 }
1184 st->cr();
1185 st->cr();
1186
1187 STEP_IF("printing owned locks on error", _verbose)
1188 // mutexes/monitors that currently have an owner
1189 Mutex::print_owned_locks_on_error(st);
1190 st->cr();
1191
1192 STEP_IF("printing number of OutOfMemoryError and StackOverflow exceptions",
1193 _verbose && Exceptions::has_exception_counts())
1194 st->print_cr("OutOfMemory and StackOverflow Exception counts:");
1195 Exceptions::print_exception_counts_on_error(st);
1196 st->cr();
1197
1198 #ifdef _LP64
1199 STEP_IF("printing compressed oops mode", _verbose && UseCompressedOops)
1200 CompressedOops::print_mode(st);
1201 st->cr();
1202
1203 STEP_IF("printing compressed klass pointers mode", _verbose && UseCompressedClassPointers)
1204 CDS_ONLY(AOTMetaspace::print_on(st);)
1205 Metaspace::print_compressed_class_space(st);
1206 CompressedKlassPointers::print_mode(st);
1207 st->cr();
1208 #endif
1209
1210 STEP_IF("printing heap information", _verbose)
1211 GCLogPrecious::print_on_error(st);
1212
1213 if (Universe::heap() != nullptr) {
1214 st->print_cr("Heap:");
1215 StreamIndentor si(st, 1);
1216 Universe::heap()->print_heap_on(st);
1217 st->cr();
1218 }
1219
1220 STEP_IF("printing GC information", _verbose)
1221 if (Universe::heap() != nullptr) {
1222 Universe::heap()->print_gc_on(st);
1223 st->cr();
1224 }
1225
1226 if (Universe::is_fully_initialized()) {
1227 st->print_cr("Polling page: " PTR_FORMAT, p2i(SafepointMechanism::get_polling_page()));
1228 st->cr();
1229 }
1230
1231 STEP_IF("printing metaspace information", _verbose && Universe::is_fully_initialized())
1232 st->print_cr("Metaspace:");
1233 MetaspaceUtils::print_on(st);
1234 MetaspaceUtils::print_basic_report(st, 0);
1235
1236 STEP_IF("printing code cache information", _verbose && Universe::is_fully_initialized())
1237 // print code cache information before vm abort
1238 CodeCache::print_summary(st);
1239 st->cr();
1240
1241 STEP_IF("printing ring buffers", _verbose)
1242 Events::print_all(st);
1243 st->cr();
1244
1245 STEP_IF("printing dynamic libraries", _verbose)
1246 // dynamic libraries, or memory map
1247 os::print_dll_info(st);
1248 st->cr();
1249
1250 #if INCLUDE_JVMTI
1251 STEP_IF("printing jvmti agent info", _verbose)
1252 os::print_jvmti_agent_info(st);
1253 st->cr();
1254 #endif
1255
1256 STEP_IF("printing native decoder state", _verbose)
1257 Decoder::print_state_on(st);
1258 st->cr();
1259
1260 STEP_IF("printing VM options", _verbose)
1261 // VM options
1262 Arguments::print_on(st);
1263 st->cr();
1264
1265 STEP_IF("printing flags", _verbose)
1266 JVMFlag::printFlags(
1267 st,
1268 true, // with comments
1269 false, // no ranges
1270 true); // skip defaults
1271 st->cr();
1272
1273 STEP_IF("printing warning if internal testing API used", WhiteBox::used())
1274 st->print_cr("Unsupported internal testing APIs have been used.");
1275 st->cr();
1276
1277 STEP_IF("printing log configuration", _verbose)
1278 st->print_cr("Logging:");
1279 LogConfiguration::describe_current_configuration(st);
1280 st->cr();
1281
1282 STEP_IF("printing release file content", _verbose)
1283 st->print_cr("Release file:");
1284 os::print_image_release_file(st);
1285
1286 STEP_IF("printing all environment variables", _verbose)
1287 os::print_environment_variables(st, env_list);
1288 st->cr();
1289
1290 STEP_IF("printing locale settings", _verbose)
1291 os::print_active_locale(st);
1292 st->cr();
1293
1294 STEP_IF("printing signal handlers", _verbose)
1295 os::print_signal_handlers(st, buf, sizeof(buf));
1296 st->cr();
1297
1298 STEP_IF("Native Memory Tracking", _verbose)
1299 MemTracker::error_report(st);
1300 st->cr();
1301
1302 STEP_IF("printing compiler memory info, if any", _verbose)
1303 CompilationMemoryStatistic::print_error_report(st);
1304 st->cr();
1305
1306 STEP_IF("printing periodic trim state", _verbose)
1307 NativeHeapTrimmer::print_state(st);
1308 st->cr();
1309
1310 STEP_IF("printing system", _verbose)
1311 st->print_cr("--------------- S Y S T E M ---------------");
1312 st->cr();
1313
1314 STEP_IF("printing OS information", _verbose)
1315 os::print_os_info(st);
1316 st->cr();
1317
1318 STEP_IF("printing CPU info", _verbose)
1319 os::print_cpu_info(st, buf, sizeof(buf));
1320 st->cr();
1321
1322 STEP_IF("printing memory info", _verbose)
1323 os::print_memory_info(st);
1324 st->cr();
1325
1326 STEP_IF("printing internal vm info", _verbose)
1327 st->print_cr("vm_info: %s", VM_Version::internal_vm_info_string());
1328 st->cr();
1329
1330 // print a defined marker to show that error handling finished correctly.
1331 STEP_IF("printing end marker", _verbose)
1332 st->print_cr("END.");
1333
1334 END
1335
1336 # undef BEGIN
1337 # undef STEP_IF
1338 # undef STEP
1339 # undef REATTEMPT_STEP_IF
1340 # undef END
1341 }
1342
1343 void VMError::set_handshake_timed_out_thread(Thread* thread) {
1344 // Only preserve the first thread to time-out this way. The atomic operation ensures
1345 // visibility to the target thread.
1346 AtomicAccess::replace_if_null(&_handshake_timed_out_thread, thread);
1347 }
1348
1349 void VMError::set_safepoint_timed_out_thread(Thread* thread) {
1350 // Only preserve the first thread to time-out this way. The atomic operation ensures
1351 // visibility to the target thread.
1352 AtomicAccess::replace_if_null(&_safepoint_timed_out_thread, thread);
1353 }
1354
1355 Thread* VMError::get_handshake_timed_out_thread() {
1356 return AtomicAccess::load(&_handshake_timed_out_thread);
1357 }
1358
1359 Thread* VMError::get_safepoint_timed_out_thread() {
1360 return AtomicAccess::load(&_safepoint_timed_out_thread);
1361 }
1362
1363 // Report for the vm_info_cmd. This prints out the information above omitting
1364 // crash and thread specific information. If output is added above, it should be added
1365 // here also, if it is safe to call during a running process.
1366 void VMError::print_vm_info(outputStream* st) {
1367
1368 char buf[O_BUFLEN];
1369 os::prepare_native_symbols();
1370
1371 report_vm_version(st, buf, sizeof(buf));
1372
1373 // STEP("printing summary")
1374
1375 st->cr();
1376 st->print_cr("--------------- S U M M A R Y ------------");
1377 st->cr();
1378
1379 // STEP("printing VM option summary")
1380
1381 // VM options
1382 Arguments::print_summary_on(st);
1383 st->cr();
1384
1385 // STEP("printing summary machine and OS info")
1386
1387 os::print_summary_info(st, buf, sizeof(buf));
1388
1389 // STEP("printing date and time")
1390
1391 os::print_date_and_time(st, buf, sizeof(buf));
1392
1393 // Skip: STEP("printing thread")
1394
1395 // STEP("printing process")
1396
1397 st->cr();
1398 st->print_cr("--------------- P R O C E S S ---------------");
1399 st->cr();
1400
1401 // STEP("printing number of OutOfMemoryError and StackOverflow exceptions")
1402
1403 if (Exceptions::has_exception_counts()) {
1404 st->print_cr("OutOfMemory and StackOverflow Exception counts:");
1405 Exceptions::print_exception_counts_on_error(st);
1406 st->cr();
1407 }
1408
1409 #ifdef _LP64
1410 // STEP("printing compressed oops mode")
1411 if (UseCompressedOops) {
1412 CompressedOops::print_mode(st);
1413 st->cr();
1414 }
1415 #endif
1416
1417 // STEP("printing compressed class ptrs mode")
1418 if (UseCompressedClassPointers) {
1419 CDS_ONLY(AOTMetaspace::print_on(st);)
1420 Metaspace::print_compressed_class_space(st);
1421 CompressedKlassPointers::print_mode(st);
1422 st->cr();
1423 }
1424
1425 // Take heap lock over heap, GC and metaspace printing so that information
1426 // is consistent.
1427 if (Universe::is_fully_initialized()) {
1428 MutexLocker ml(Heap_lock);
1429
1430 // STEP("printing heap information")
1431
1432 GCLogPrecious::print_on_error(st);
1433
1434 {
1435 st->print_cr("Heap:");
1436 StreamIndentor si(st, 1);
1437 Universe::heap()->print_heap_on(st);
1438 st->cr();
1439 }
1440
1441 // STEP("printing GC information")
1442
1443 Universe::heap()->print_gc_on(st);
1444 st->cr();
1445
1446 st->print_cr("Polling page: " PTR_FORMAT, p2i(SafepointMechanism::get_polling_page()));
1447 st->cr();
1448
1449 // STEP("printing metaspace information")
1450
1451 st->print_cr("Metaspace:");
1452 MetaspaceUtils::print_on(st);
1453 MetaspaceUtils::print_basic_report(st, 0);
1454 }
1455
1456 // STEP("printing code cache information")
1457
1458 if (Universe::is_fully_initialized()) {
1459 // print code cache information before vm abort
1460 CodeCache::print_summary(st);
1461 st->cr();
1462 }
1463
1464 // STEP("printing ring buffers")
1465
1466 Events::print_all(st);
1467 st->cr();
1468
1469 // STEP("printing dynamic libraries")
1470
1471 // dynamic libraries, or memory map
1472 os::print_dll_info(st);
1473 st->cr();
1474
1475 #if INCLUDE_JVMTI
1476 os::print_jvmti_agent_info(st);
1477 st->cr();
1478 #endif
1479
1480 // STEP("printing VM options")
1481
1482 // VM options
1483 Arguments::print_on(st);
1484 st->cr();
1485
1486 // STEP("printing warning if internal testing API used")
1487
1488 if (WhiteBox::used()) {
1489 st->print_cr("Unsupported internal testing APIs have been used.");
1490 st->cr();
1491 }
1492
1493 // STEP("printing log configuration")
1494 st->print_cr("Logging:");
1495 LogConfiguration::describe(st);
1496 st->cr();
1497
1498 // STEP("printing release file content")
1499 st->print_cr("Release file:");
1500 os::print_image_release_file(st);
1501
1502 // STEP("printing all environment variables")
1503
1504 os::print_environment_variables(st, env_list);
1505 st->cr();
1506
1507 // STEP("printing locale settings")
1508
1509 os::print_active_locale(st);
1510 st->cr();
1511
1512
1513 // STEP("printing signal handlers")
1514
1515 os::print_signal_handlers(st, buf, sizeof(buf));
1516 st->cr();
1517
1518 // STEP("Native Memory Tracking")
1519 MemTracker::error_report(st);
1520 st->cr();
1521
1522 // STEP("Compiler Memory Statistic")
1523 CompilationMemoryStatistic::print_final_report(st);
1524
1525 // STEP("printing periodic trim state")
1526 NativeHeapTrimmer::print_state(st);
1527 st->cr();
1528
1529
1530 // STEP("printing system")
1531 st->print_cr("--------------- S Y S T E M ---------------");
1532 st->cr();
1533
1534 // STEP("printing OS information")
1535
1536 os::print_os_info(st);
1537 st->cr();
1538
1539 // STEP("printing CPU info")
1540
1541 os::print_cpu_info(st, buf, sizeof(buf));
1542 st->cr();
1543
1544 // STEP("printing memory info")
1545
1546 os::print_memory_info(st);
1547 st->cr();
1548
1549 // STEP("printing internal vm info")
1550
1551 st->print_cr("vm_info: %s", VM_Version::internal_vm_info_string());
1552 st->cr();
1553
1554 // print a defined marker to show that error handling finished correctly.
1555 // STEP("printing end marker")
1556
1557 st->print_cr("END.");
1558 }
1559
1560 /** Expand a pattern into a buffer starting at pos and open a file using constructed path */
1561 static int expand_and_open(const char* pattern, bool overwrite_existing, char* buf, size_t buflen, size_t pos) {
1562 int fd = -1;
1563 int mode = O_RDWR | O_CREAT;
1564 if (overwrite_existing) {
1565 mode |= O_TRUNC;
1566 } else {
1567 mode |= O_EXCL;
1568 }
1569 if (Arguments::copy_expand_pid(pattern, strlen(pattern), &buf[pos], buflen - pos)) {
1570 fd = open(buf, mode, 0666);
1571 }
1572 return fd;
1573 }
1574
1575 /**
1576 * Construct file name for a log file and return it's file descriptor.
1577 * Name and location depends on pattern, default_pattern params and access
1578 * permissions.
1579 */
1580 int VMError::prepare_log_file(const char* pattern, const char* default_pattern, bool overwrite_existing, char* buf, size_t buflen) {
1581 int fd = -1;
1582
1583 // If possible, use specified pattern to construct log file name
1584 if (pattern != nullptr) {
1585 fd = expand_and_open(pattern, overwrite_existing, buf, buflen, 0);
1586 }
1587
1588 // Either user didn't specify, or the user's location failed,
1589 // so use the default name in the current directory
1590 if (fd == -1) {
1591 const char* cwd = os::get_current_directory(buf, buflen);
1592 if (cwd != nullptr) {
1593 size_t pos = strlen(cwd);
1594 int fsep_len = jio_snprintf(&buf[pos], buflen-pos, "%s", os::file_separator());
1595 pos += fsep_len;
1596 if (fsep_len > 0) {
1597 fd = expand_and_open(default_pattern, overwrite_existing, buf, buflen, pos);
1598 }
1599 }
1600 }
1601
1602 // try temp directory if it exists.
1603 if (fd == -1) {
1604 const char* tmpdir = os::get_temp_directory();
1605 if (tmpdir != nullptr && strlen(tmpdir) > 0) {
1606 int pos = jio_snprintf(buf, buflen, "%s%s", tmpdir, os::file_separator());
1607 if (pos > 0) {
1608 fd = expand_and_open(default_pattern, overwrite_existing, buf, buflen, pos);
1609 }
1610 }
1611 }
1612
1613 return fd;
1614 }
1615
1616 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, const void* siginfo,
1617 const void* context, const char* detail_fmt, ...)
1618 {
1619 va_list detail_args;
1620 va_start(detail_args, detail_fmt);
1621 report_and_die(sig, nullptr, detail_fmt, detail_args, thread, pc, siginfo, context, nullptr, 0, 0);
1622 va_end(detail_args);
1623 }
1624
1625 void VMError::report_and_die(Thread* thread, const void* context, const char* filename, int lineno, const char* message,
1626 const char* detail_fmt, ...) {
1627 va_list detail_args;
1628 va_start(detail_args, detail_fmt);
1629 report_and_die(thread, context, filename, lineno, message, detail_fmt, detail_args);
1630 va_end(detail_args);
1631 }
1632
1633 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, const void* siginfo, const void* context)
1634 {
1635 if (ExecutingUnitTests) {
1636 // See TEST_VM_CRASH_SIGNAL gtest macro
1637 char tmp[64];
1638 fprintf(stderr, "signaled: %s", os::exception_name(sig, tmp, sizeof(tmp)));
1639 }
1640
1641 report_and_die(thread, sig, pc, siginfo, context, "%s", "");
1642 }
1643
1644 void VMError::report_and_die(Thread* thread, const void* context, const char* filename, int lineno, const char* message,
1645 const char* detail_fmt, va_list detail_args)
1646 {
1647 report_and_die(INTERNAL_ERROR, message, detail_fmt, detail_args, thread, nullptr, nullptr, context, filename, lineno, 0);
1648 }
1649
1650 void VMError::report_and_die(Thread* thread, const char* filename, int lineno, size_t size,
1651 VMErrorType vm_err_type, const char* detail_fmt, va_list detail_args) {
1652 report_and_die(vm_err_type, nullptr, detail_fmt, detail_args, thread, nullptr, nullptr, nullptr, filename, lineno, size);
1653 }
1654
1655 void VMError::report_and_die(int id, const char* message, const char* detail_fmt, va_list detail_args,
1656 Thread* thread, address pc, const void* siginfo, const void* context, const char* filename,
1657 int lineno, size_t size)
1658 {
1659 // A single scratch buffer to be used from here on.
1660 // Do not rely on it being preserved across function calls.
1661 static char buffer[O_BUFLEN];
1662
1663 // File descriptor to tty to print an error summary to.
1664 // Hard wired to stdout; see JDK-8215004 (compatibility concerns).
1665 static const int fd_out = 1; // stdout
1666
1667 // File descriptor to the error log file.
1668 static int fd_log = -1;
1669
1670 #ifdef CAN_SHOW_REGISTERS_ON_ASSERT
1671 // Disarm assertion poison page, since from this point on we do not need this mechanism anymore and it may
1672 // cause problems in error handling during native OOM, see JDK-8227275.
1673 disarm_assert_poison();
1674 #endif
1675
1676 // Use local fdStream objects only. Do not use global instances whose initialization
1677 // relies on dynamic initialization (see JDK-8214975). Do not rely on these instances
1678 // to carry over into recursions or invocations from other threads.
1679 fdStream out(fd_out);
1680 out.set_scratch_buffer(buffer, sizeof(buffer));
1681
1682 // Depending on the re-entrance depth at this point, fd_log may be -1 or point to an open hs-err file.
1683 fdStream log(fd_log);
1684 log.set_scratch_buffer(buffer, sizeof(buffer));
1685
1686 // How many errors occurred in error handler when reporting first_error.
1687 static int recursive_error_count;
1688
1689 // We will first print a brief message to standard out (verbose = false),
1690 // then save detailed information in log file (verbose = true).
1691 static bool out_done = false; // done printing to standard out
1692 static bool log_done = false; // done saving error log
1693
1694 intptr_t mytid = os::current_thread_id();
1695 if (_first_error_tid == -1 &&
1696 AtomicAccess::cmpxchg(&_first_error_tid, (intptr_t)-1, mytid) == -1) {
1697
1698 if (SuppressFatalErrorMessage) {
1699 os::abort(CreateCoredumpOnCrash);
1700 }
1701
1702 // Initialize time stamps to use the same base.
1703 out.time_stamp().update_to(1);
1704 log.time_stamp().update_to(1);
1705
1706 _id = id;
1707 _message = message;
1708 _thread = thread;
1709 _pc = pc;
1710 _siginfo = siginfo;
1711 _context = context;
1712 _filename = filename;
1713 _lineno = lineno;
1714 _size = size;
1715 jio_vsnprintf(_detail_msg, sizeof(_detail_msg), detail_fmt, detail_args);
1716
1717 reporting_started();
1718 if (!TestUnresponsiveErrorHandler) {
1719 // Record reporting_start_time unless we're running the
1720 // TestUnresponsiveErrorHandler test. For that test we record
1721 // reporting_start_time at the beginning of the test.
1722 record_reporting_start_time();
1723 } else {
1724 out.print_raw_cr("Delaying recording reporting_start_time for TestUnresponsiveErrorHandler.");
1725 }
1726
1727 if (ShowMessageBoxOnError || PauseAtExit) {
1728 show_message_box(buffer, sizeof(buffer));
1729
1730 // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
1731 // WatcherThread can kill JVM if the error handler hangs.
1732 ShowMessageBoxOnError = false;
1733 }
1734
1735 os::check_core_dump_prerequisites(buffer, sizeof(buffer));
1736
1737 // reset signal handlers or exception filter; make sure recursive crashes
1738 // are handled properly.
1739 install_secondary_signal_handler();
1740 } else {
1741 // This is not the first error, see if it happened in a different thread
1742 // or in the same thread during error reporting.
1743 if (_first_error_tid != mytid) {
1744 if (!SuppressFatalErrorMessage) {
1745 char msgbuf[64];
1746 jio_snprintf(msgbuf, sizeof(msgbuf),
1747 "[thread %zd also had an error]",
1748 mytid);
1749 out.print_raw_cr(msgbuf);
1750 }
1751
1752 // Error reporting is not MT-safe, nor can we let the current thread
1753 // proceed, so we block it.
1754 os::infinite_sleep();
1755
1756 } else {
1757 if (recursive_error_count++ > 30) {
1758 if (!SuppressFatalErrorMessage) {
1759 out.print_raw_cr("[Too many errors, abort]");
1760 }
1761 os::die();
1762 }
1763
1764 if (SuppressFatalErrorMessage) {
1765 // If we already hit a secondary error during abort, then calling
1766 // it again is likely to hit another one. But eventually, if we
1767 // don't deadlock somewhere, we will call os::die() above.
1768 os::abort(CreateCoredumpOnCrash);
1769 }
1770
1771 outputStream* const st = log.is_open() ? &log : &out;
1772 st->cr();
1773
1774 // Timeout handling.
1775 if (_step_did_timeout) {
1776 // The current step had a timeout. Lets continue reporting with the next step.
1777 st->print_raw("[timeout occurred during error reporting in step \"");
1778 st->print_raw(_current_step_info);
1779 st->print_cr("\"] after " INT64_FORMAT " s.",
1780 (int64_t)
1781 ((get_current_timestamp() - _step_start_time) / TIMESTAMP_TO_SECONDS_FACTOR));
1782 } else if (_reporting_did_timeout) {
1783 // We hit ErrorLogTimeout. Reporting will stop altogether. Let's wrap things
1784 // up, the process is about to be stopped by the WatcherThread.
1785 st->print_cr("------ Timeout during error reporting after " INT64_FORMAT " s. ------",
1786 (int64_t)
1787 ((get_current_timestamp() - _reporting_start_time) / TIMESTAMP_TO_SECONDS_FACTOR));
1788 st->flush();
1789 // Watcherthread is about to call os::die. Lets just wait.
1790 os::infinite_sleep();
1791 } else {
1792 // A secondary error happened. Print brief information, but take care, since crashing
1793 // here would just recurse endlessly.
1794 // Any information (signal, context, siginfo etc) printed here should use the function
1795 // arguments, not the information stored in *this, since those describe the primary crash.
1796 static char tmp[256]; // cannot use global scratch buffer
1797 // Note: this string does get parsed by a number of jtreg tests,
1798 // see hotspot/jtreg/runtime/ErrorHandling.
1799 st->print("[error occurred during error reporting (%s), id 0x%x",
1800 _current_step_info, id);
1801 if (os::exception_name(id, tmp, sizeof(tmp))) {
1802 st->print(", %s (0x%x) at pc=" PTR_FORMAT, tmp, id, p2i(pc));
1803 } else {
1804 if (should_report_bug(id)) {
1805 st->print(", Internal Error (%s:%d)",
1806 filename == nullptr ? "??" : filename, lineno);
1807 } else {
1808 st->print(", Out of Memory Error (%s:%d)",
1809 filename == nullptr ? "??" : filename, lineno);
1810 }
1811 }
1812 st->print_cr("]");
1813 if (ErrorLogSecondaryErrorDetails) {
1814 static bool recursed = false;
1815 if (!recursed) {
1816 recursed = true;
1817 // Print even more information for secondary errors. This may generate a lot of output
1818 // and possibly disturb error reporting, therefore its optional and only available in debug builds.
1819 if (siginfo != nullptr) {
1820 st->print("[");
1821 os::print_siginfo(st, siginfo);
1822 st->print_cr("]");
1823 }
1824 st->print("[stack: ");
1825 NativeStackPrinter nsp(_thread, context, _filename != nullptr ? get_filename_only() : nullptr, _lineno);
1826 // Subsequent secondary errors build up stack; to avoid flooding the hs-err file with irrelevant
1827 // call stacks, limit the stack we print here (we are only interested in what happened before the
1828 // last assert/fault).
1829 const int max_stack_size = 15;
1830 nsp.print_stack_from_frame(st, tmp, sizeof(tmp), true /* print_source_info */, max_stack_size);
1831 st->print_cr("]");
1832 } // !recursed
1833 recursed = false; // Note: reset outside !recursed
1834 }
1835 }
1836 }
1837 }
1838
1839 // Part 1: print an abbreviated version (the '#' section) to stdout.
1840 if (!out_done) {
1841 // Suppress this output if we plan to print Part 2 to stdout too.
1842 // No need to have the "#" section twice.
1843 if (!(ErrorFileToStdout && out.fd() == 1)) {
1844 report(&out, false);
1845 }
1846
1847 out_done = true;
1848
1849 _current_step = 0;
1850 _current_step_info = "";
1851 }
1852
1853 // Part 2: print a full error log file (optionally to stdout or stderr).
1854 // print to error log file
1855 if (!log_done) {
1856 // see if log file is already open
1857 if (!log.is_open()) {
1858 // open log file
1859 if (ErrorFileToStdout) {
1860 fd_log = 1;
1861 } else if (ErrorFileToStderr) {
1862 fd_log = 2;
1863 } else {
1864 fd_log = prepare_log_file(ErrorFile, "hs_err_pid%p.log", true,
1865 buffer, sizeof(buffer));
1866 if (fd_log != -1) {
1867 out.print_raw("# An error report file with more information is saved as:\n# ");
1868 out.print_raw_cr(buffer);
1869 } else {
1870 out.print_raw_cr("# Can not save log file, dump to screen..");
1871 fd_log = 1;
1872 }
1873 }
1874 log.set_fd(fd_log);
1875 }
1876
1877 report(&log, true);
1878 log_done = true;
1879 _current_step = 0;
1880 _current_step_info = "";
1881
1882 if (fd_log > 3) {
1883 ::close(fd_log);
1884 fd_log = -1;
1885 }
1886
1887 log.set_fd(-1);
1888 }
1889
1890 JFR_ONLY(Jfr::on_vm_shutdown(static_cast<VMErrorType>(_id) == OOM_JAVA_HEAP_FATAL, true);)
1891
1892 if (PrintNMTStatistics) {
1893 fdStream fds(fd_out);
1894 MemTracker::final_report(&fds);
1895 }
1896
1897 static bool skip_replay = ReplayCompiles && !ReplayReduce; // Do not overwrite file during replay
1898 if (DumpReplayDataOnError && _thread && _thread->is_Compiler_thread() && !skip_replay) {
1899 skip_replay = true;
1900 ciEnv* env = ciEnv::current();
1901 if (env != nullptr && env->task() != nullptr) {
1902 const bool overwrite = false; // We do not overwrite an existing replay file.
1903 int fd = prepare_log_file(ReplayDataFile, "replay_pid%p.log", overwrite, buffer, sizeof(buffer));
1904 if (fd != -1) {
1905 FILE* replay_data_file = os::fdopen(fd, "w");
1906 if (replay_data_file != nullptr) {
1907 fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
1908 env->dump_replay_data_unsafe(&replay_data_stream);
1909 out.print_raw("#\n# Compiler replay data is saved as:\n# ");
1910 out.print_raw_cr(buffer);
1911 } else {
1912 int e = errno;
1913 out.print_raw("#\n# Can't open file to dump replay data. Error: ");
1914 out.print_raw_cr(os::strerror(e));
1915 close(fd);
1916 }
1917 }
1918 }
1919 }
1920
1921 #if INCLUDE_JVMCI
1922 if (JVMCI::fatal_log_filename() != nullptr) {
1923 out.print_raw("#\n# The JVMCI shared library error report file is saved as:\n# ");
1924 out.print_raw_cr(JVMCI::fatal_log_filename());
1925 }
1926 #endif
1927
1928 static bool skip_bug_url = !should_submit_bug_report(_id);
1929 if (!skip_bug_url) {
1930 skip_bug_url = true;
1931
1932 out.print_raw_cr("#");
1933 print_bug_submit_message(&out, _thread);
1934 }
1935
1936 static bool skip_OnError = false;
1937 if (!skip_OnError && OnError && OnError[0]) {
1938 skip_OnError = true;
1939
1940 // Flush output and finish logs before running OnError commands.
1941 ostream_abort();
1942
1943 out.print_raw_cr("#");
1944 out.print_raw ("# -XX:OnError=\"");
1945 out.print_raw (OnError);
1946 out.print_raw_cr("\"");
1947
1948 char* cmd;
1949 const char* ptr = OnError;
1950 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != nullptr){
1951 out.print_raw ("# Executing ");
1952 #if defined(LINUX) || defined(_ALLBSD_SOURCE)
1953 out.print_raw ("/bin/sh -c ");
1954 #elif defined(_WINDOWS)
1955 out.print_raw ("cmd /C ");
1956 #endif
1957 out.print_raw ("\"");
1958 out.print_raw (cmd);
1959 out.print_raw_cr("\" ...");
1960
1961 if (os::fork_and_exec(cmd) < 0) {
1962 out.print_cr("os::fork_and_exec failed: %s (%s=%d)",
1963 os::strerror(errno), os::errno_name(errno), errno);
1964 }
1965 }
1966
1967 // done with OnError
1968 OnError = nullptr;
1969 }
1970
1971 #if defined _WINDOWS
1972 if (UseOSErrorReporting) {
1973 raise_fail_fast(_siginfo, _context);
1974 }
1975 #endif // _WINDOWS
1976
1977 // os::abort() will call abort hooks, try it first.
1978 static bool skip_os_abort = false;
1979 if (!skip_os_abort) {
1980 skip_os_abort = true;
1981 bool dump_core = should_report_bug(_id);
1982 os::abort(dump_core && CreateCoredumpOnCrash, _siginfo, _context);
1983 // if os::abort() doesn't abort, try os::die();
1984 }
1985 os::die();
1986 }
1987
1988 /*
1989 * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this
1990 * ensures utilities such as jmap can observe the process is a consistent state.
1991 */
1992 class VM_ReportJavaOutOfMemory : public VM_Operation {
1993 private:
1994 const char* _message;
1995 public:
1996 VM_ReportJavaOutOfMemory(const char* message) { _message = message; }
1997 VMOp_Type type() const { return VMOp_ReportJavaOutOfMemory; }
1998 void doit();
1999 };
2000
2001 void VM_ReportJavaOutOfMemory::doit() {
2002 // Don't allocate large buffer on stack
2003 static char buffer[O_BUFLEN];
2004
2005 tty->print_cr("#");
2006 tty->print_cr("# java.lang.OutOfMemoryError: %s", _message);
2007 tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError);
2008
2009 // make heap parsability
2010 Universe::heap()->ensure_parsability(false); // no need to retire TLABs
2011
2012 char* cmd;
2013 const char* ptr = OnOutOfMemoryError;
2014 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != nullptr){
2015 tty->print("# Executing ");
2016 #if defined(LINUX)
2017 tty->print ("/bin/sh -c ");
2018 #endif
2019 tty->print_cr("\"%s\"...", cmd);
2020
2021 if (os::fork_and_exec(cmd) < 0) {
2022 tty->print_cr("os::fork_and_exec failed: %s (%s=%d)",
2023 os::strerror(errno), os::errno_name(errno), errno);
2024 }
2025 }
2026 }
2027
2028 void VMError::report_java_out_of_memory(const char* message) {
2029 if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
2030 MutexLocker ml(Heap_lock);
2031 VM_ReportJavaOutOfMemory op(message);
2032 VMThread::execute(&op);
2033 }
2034 }
2035
2036 void VMError::show_message_box(char *buf, int buflen) {
2037 bool yes;
2038 do {
2039 error_string(buf, buflen);
2040 yes = os::start_debugging(buf,buflen);
2041 } while (yes);
2042 }
2043
2044 // Fatal error handling is subject to several timeouts:
2045 // - a global timeout (controlled via ErrorLogTimeout)
2046 // - local error reporting step timeouts.
2047 //
2048 // The latter aims to "give the JVM a kick" if it gets stuck in one particular place during
2049 // error reporting. This prevents one error reporting step from hogging all the time allotted
2050 // to error reporting under ErrorLogTimeout.
2051 //
2052 // VMError::check_timeout() is called from the watcher thread and checks for either global
2053 // or step timeout. If a timeout happened, we interrupt the reporting thread and set either
2054 // _reporting_did_timeout or _step_did_timeout to signal which timeout fired. Function returns
2055 // true if the *global* timeout fired, which will cause WatcherThread to shut down the JVM
2056 // immediately.
2057 bool VMError::check_timeout() {
2058
2059 // This function is supposed to be called from watcher thread during fatal error handling only.
2060 assert(VMError::is_error_reported(), "Only call during error handling");
2061 assert(Thread::current()->is_Watcher_thread(), "Only call from watcher thread");
2062
2063 if (ErrorLogTimeout == 0) {
2064 return false;
2065 }
2066
2067 // There are three situations where we suppress the *global* error timeout:
2068 // - if the JVM is embedded and the launcher has its abort hook installed.
2069 // That must be allowed to run.
2070 // - if the user specified one or more OnError commands to run, and these
2071 // did not yet run. These must have finished.
2072 // - if the user (typically developer) specified ShowMessageBoxOnError,
2073 // and the error box has not yet been shown
2074 const bool ignore_global_timeout =
2075 (ShowMessageBoxOnError
2076 || (OnError != nullptr && OnError[0] != '\0')
2077 || Arguments::abort_hook() != nullptr);
2078
2079 const jlong now = get_current_timestamp();
2080
2081 // Global timeout hit?
2082 if (!ignore_global_timeout) {
2083 const jlong reporting_start_time = get_reporting_start_time();
2084 // Timestamp is stored in nanos.
2085 if (reporting_start_time > 0) {
2086 const jlong end = reporting_start_time + (jlong)ErrorLogTimeout * TIMESTAMP_TO_SECONDS_FACTOR;
2087 if (end <= now && !_reporting_did_timeout) {
2088 // We hit ErrorLogTimeout and we haven't interrupted the reporting
2089 // thread yet.
2090 _reporting_did_timeout = true;
2091 interrupt_reporting_thread();
2092 return true; // global timeout
2093 }
2094 }
2095 }
2096
2097 // Reporting step timeout?
2098 const jlong step_start_time = get_step_start_time();
2099 if (step_start_time > 0) {
2100 // A step times out after a quarter of the total timeout. Steps are mostly fast unless they
2101 // hang for some reason, so this simple rule allows for three hanging step and still
2102 // hopefully leaves time enough for the rest of the steps to finish.
2103 const int max_step_timeout_secs = 5;
2104 const jlong timeout_duration = MAX2((jlong)max_step_timeout_secs, (jlong)ErrorLogTimeout * TIMESTAMP_TO_SECONDS_FACTOR / 4);
2105 const jlong end = step_start_time + timeout_duration;
2106 if (end <= now && !_step_did_timeout) {
2107 // The step timed out and we haven't interrupted the reporting
2108 // thread yet.
2109 _step_did_timeout = true;
2110 interrupt_reporting_thread();
2111 return false; // (Not a global timeout)
2112 }
2113 }
2114
2115 return false;
2116
2117 }
2118
2119 #ifdef ASSERT
2120 typedef void (*voidfun_t)();
2121
2122 // Crash with an authentic sigfpe; behavior is subtly different from a real signal
2123 // compared to one generated with raise (asynchronous vs synchronous). See JDK-8065895.
2124 volatile int sigfpe_int = 0;
2125
2126 ATTRIBUTE_NO_UBSAN
2127 static void ALWAYSINLINE crash_with_sigfpe() {
2128
2129 // generate a native synchronous SIGFPE where possible;
2130 sigfpe_int = sigfpe_int/sigfpe_int;
2131
2132 // if that did not cause a signal (e.g. on ppc), just
2133 // raise the signal.
2134 #ifndef _WIN32
2135 // OSX implements raise(sig) incorrectly so we need to
2136 // explicitly target the current thread
2137 pthread_kill(pthread_self(), SIGFPE);
2138 #endif
2139
2140 } // end: crash_with_sigfpe
2141
2142 // crash with sigsegv at non-null address.
2143 static void ALWAYSINLINE crash_with_segfault() {
2144
2145 int* crash_addr = reinterpret_cast<int*>(VMError::segfault_address);
2146 *crash_addr = 1;
2147
2148 } // end: crash_with_segfault
2149
2150 // crash in a controlled way:
2151 // 1 - assert
2152 // 2 - guarantee
2153 // 14 - SIGSEGV
2154 // 15 - SIGFPE
2155 void VMError::controlled_crash(int how) {
2156
2157 // Case 14 is tested by test/hotspot/jtreg/runtime/ErrorHandling/SafeFetchInErrorHandlingTest.java.
2158 // Case 15 is tested by test/hotspot/jtreg/runtime/ErrorHandling/SecondaryErrorTest.java.
2159 // Case 16 is tested by test/hotspot/jtreg/runtime/ErrorHandling/ThreadsListHandleInErrorHandlingTest.java.
2160 // Case 17 is tested by test/hotspot/jtreg/runtime/ErrorHandling/NestedThreadsListHandleInErrorHandlingTest.java.
2161
2162 // We try to grab Threads_lock to keep ThreadsSMRSupport::print_info_on()
2163 // from racing with Threads::add() or Threads::remove() as we
2164 // generate the hs_err_pid file. This makes our ErrorHandling tests
2165 // more stable.
2166 if (!Threads_lock->owned_by_self()) {
2167 Threads_lock->try_lock();
2168 // The VM is going to die so no need to unlock Thread_lock.
2169 }
2170
2171 switch (how) {
2172 case 1: assert(how == 0, "test assert"); break;
2173 case 2: guarantee(how == 0, "test guarantee"); break;
2174
2175 // The other cases are unused.
2176 case 14: crash_with_segfault(); break;
2177 case 15: crash_with_sigfpe(); break;
2178 case 16: {
2179 ThreadsListHandle tlh;
2180 fatal("Force crash with an active ThreadsListHandle.");
2181 }
2182 case 17: {
2183 ThreadsListHandle tlh;
2184 {
2185 ThreadsListHandle tlh2;
2186 fatal("Force crash with a nested ThreadsListHandle.");
2187 }
2188 }
2189 default:
2190 // If another number is given, give a generic crash.
2191 fatal("Crashing with number %d", how);
2192 }
2193 tty->print_cr("controlled_crash: survived intentional crash. Did you suppress the assert?");
2194 ShouldNotReachHere();
2195 }
2196 #endif // !ASSERT
2197
2198 VMErrorCallbackMark::VMErrorCallbackMark(VMErrorCallback* callback)
2199 : _thread(Thread::current()) {
2200 callback->_next = _thread->_vm_error_callbacks;
2201 _thread->_vm_error_callbacks = callback;
2202 }
2203
2204 VMErrorCallbackMark::~VMErrorCallbackMark() {
2205 assert(_thread->_vm_error_callbacks != nullptr, "Popped too far");
2206 _thread->_vm_error_callbacks = _thread->_vm_error_callbacks->_next;
2207 }