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