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 address sp = (address)fr.sp();
970 if (sp != nullptr) {
971 st->print(", sp=" PTR_FORMAT, p2i(sp));
972 if (sp >= stack_bottom && sp < stack_top) {
973 size_t free_stack_size = pointer_delta(sp, stack_bottom, 1024);
974 st->print(", free space=%zuk", free_stack_size);
975 } else {
976 st->print(" **OUTSIDE STACK**.");
977 }
978 }
979
980 st->cr();
981
982 STEP_IF("printing native stack (with source info)", _verbose)
983
984 NativeStackPrinter nsp(_thread, _context, _filename != nullptr ? get_filename_only() : nullptr, _lineno);
985 if (nsp.print_stack(st, buf, sizeof(buf), lastpc,
986 true /*print_source_info */, -1 /* max stack */)) {
987 // We have printed the native stack in platform-specific code
988 // Windows/x64 needs special handling.
989 // Stack walking may get stuck. Try to find the calling code.
990 if (lastpc != nullptr) {
991 const char* name = find_code_name(lastpc);
992 if (name != nullptr) {
993 st->print_cr("The last pc belongs to %s (printed below).", name);
994 }
995 }
996 } else {
997 _print_stack_from_frame_used = true; // frame-based native stack walk done
998 }
999
1000 REATTEMPT_STEP_IF("retry printing native stack (no source info)", _verbose)
1001 st->cr();
1002 st->print_cr("Retrying call stack printing without source information...");
1003 NativeStackPrinter nsp(_thread, _context, get_filename_only(), _lineno);
1004 nsp.print_stack_from_frame(st, buf, sizeof(buf),
1005 false /*print_source_info */, -1 /* max stack */);
1006 _print_stack_from_frame_used = true;
1007
1008 STEP_IF("printing Java stack", _verbose && _thread && _thread->is_Java_thread())
1009 if (_verbose && _thread && _thread->is_Java_thread()) {
1010 print_stack_trace(st, JavaThread::cast(_thread), buf, sizeof(buf));
1011 }
1012
1013 STEP_IF("printing target Java thread stack",
1014 _verbose && _thread != nullptr && (_thread->is_Named_thread()))
1015 // printing Java thread stack trace if it is involved in GC crash
1016 Thread* thread = ((NamedThread *)_thread)->processed_thread();
1017 if (thread != nullptr && thread->is_Java_thread()) {
1018 JavaThread* jt = JavaThread::cast(thread);
1019 st->print_cr("JavaThread " PTR_FORMAT " (nid = %d) was being processed", p2i(jt), jt->osthread()->thread_id());
1020 print_stack_trace(st, jt, buf, sizeof(buf), true);
1021 }
1022
1023 STEP_IF("printing siginfo", _verbose && _siginfo != nullptr)
1024 // signal no, signal code, address that caused the fault
1025 st->cr();
1026 os::print_siginfo(st, _siginfo);
1027 st->cr();
1028
1029 STEP_IF("CDS archive access warning", _verbose && _siginfo != nullptr)
1030 // Print an explicit hint if we crashed on access to the CDS archive.
1031 check_failing_cds_access(st, _siginfo);
1032 st->cr();
1033
1034 #if defined(COMPILER1) || defined(COMPILER2)
1035 STEP_IF("printing pending compilation failure",
1036 _verbose && _thread != nullptr && _thread->is_Compiler_thread())
1037 CompilationFailureInfo::print_pending_compilation_failure(st);
1038 if (CompilationMemoryStatistic::enabled() && CompilationMemoryStatistic::in_oom_crash()) {
1039 st->cr();
1040 st->print_cr(">> Please see below for a detailed breakdown of compiler memory usage.");
1041 st->cr();
1042 }
1043 #endif
1044
1045 STEP_IF("printing registers", _verbose && _context != nullptr)
1046 // printing registers
1047 os::print_context(st, _context);
1048 st->cr();
1049
1050 STEP_IF("printing register info",
1051 _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1052 continuation = 0;
1053 ResourceMark rm(_thread);
1054 st->print_cr("Register to memory mapping:");
1055 st->cr();
1056 os::print_register_info(st, _context, continuation);
1057 st->cr();
1058
1059 REATTEMPT_STEP_IF("printing register info, attempt 2",
1060 _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1061 ResourceMark rm(_thread);
1062 os::print_register_info(st, _context, continuation);
1063 st->cr();
1064
1065 REATTEMPT_STEP_IF("printing register info, attempt 3",
1066 _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1067 ResourceMark rm(_thread);
1068 os::print_register_info(st, _context, continuation);
1069 st->cr();
1070
1071 STEP_IF("printing top of stack, instructions near pc", _verbose && _context != nullptr)
1072 // printing top of stack, instructions near pc
1073 os::print_tos_pc(st, _context);
1074 st->cr();
1075
1076 STEP_IF("inspecting top of stack",
1077 _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1078 continuation = 0;
1079 ResourceMark rm(_thread);
1080 st->print_cr("Stack slot to memory mapping:");
1081 st->cr();
1082 print_stack_location(st, _context, continuation);
1083 st->cr();
1084
1085 REATTEMPT_STEP_IF("inspecting top of stack, attempt 2",
1086 _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1087 ResourceMark rm(_thread);
1088 print_stack_location(st, _context, continuation);
1089 st->cr();
1090
1091 REATTEMPT_STEP_IF("inspecting top of stack, attempt 3",
1092 _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1093 ResourceMark rm(_thread);
1094 print_stack_location(st, _context, continuation);
1095 st->cr();
1096
1097 STEP_IF("printing lock stack", _verbose && _thread != nullptr && _thread->is_Java_thread());
1098 st->print_cr("Lock stack of current Java thread (top to bottom):");
1099 JavaThread::cast(_thread)->lock_stack().print_on(st);
1100 st->cr();
1101
1102 STEP_IF("printing code blobs if possible", _verbose)
1103 const int printed_capacity = max_error_log_print_code;
1104 address printed[printed_capacity];
1105 printed[0] = nullptr;
1106 int printed_len = 0;
1107 // Even though ErrorLogPrintCodeLimit is ranged checked
1108 // during argument parsing, there's no way to prevent it
1109 // subsequently (i.e., after parsing) being set to a
1110 // value outside the range.
1111 int limit = MIN2(ErrorLogPrintCodeLimit, printed_capacity);
1112 if (limit > 0) {
1113 // Check if a pc was found by native stack trace above.
1114 if (lastpc != nullptr) {
1115 if (print_code(st, _thread, lastpc, true, printed, printed_capacity)) {
1116 printed_len++;
1117 }
1118 }
1119
1120 // Scan the native stack
1121 if (!_print_stack_from_frame_used) {
1122 // Only try to print code of the crashing frame since
1123 // the native stack cannot be walked with next_frame.
1124 if (print_code(st, _thread, _pc, true, printed, printed_capacity)) {
1125 printed_len++;
1126 }
1127 } else {
1128 frame fr = _context ? os::fetch_frame_from_context(_context)
1129 : os::current_frame();
1130 while (printed_len < limit && fr.pc() != nullptr) {
1131 if (print_code(st, _thread, fr.pc(), fr.pc() == _pc, printed, printed_capacity)) {
1132 printed_len++;
1133 }
1134 fr = frame::next_frame(fr, _thread);
1135 }
1136 }
1137
1138 // Scan the Java stack
1139 if (_thread != nullptr && _thread->is_Java_thread()) {
1140 JavaThread* jt = JavaThread::cast(_thread);
1141 if (jt->has_last_Java_frame()) {
1142 for (StackFrameStream sfs(jt, true /* update */, true /* process_frames */); printed_len < limit && !sfs.is_done(); sfs.next()) {
1143 address pc = sfs.current()->pc();
1144 if (print_code(st, _thread, pc, pc == _pc, printed, printed_capacity)) {
1145 printed_len++;
1146 }
1147 }
1148 }
1149 }
1150 }
1151
1152 STEP_IF("printing VM operation", _verbose && _thread != nullptr && _thread->is_VM_thread())
1153 VMThread* t = (VMThread*)_thread;
1154 VM_Operation* op = t->vm_operation();
1155 if (op) {
1156 op->print_on_error(st);
1157 st->cr();
1158 st->cr();
1159 }
1160
1161 STEP_IF("printing registered callbacks", _verbose && _thread != nullptr);
1162 size_t count = 0;
1163 for (VMErrorCallback* callback = _thread->_vm_error_callbacks;
1164 callback != nullptr;
1165 callback = callback->_next) {
1166 st->print_cr("VMErrorCallback %zu:", ++count);
1167 callback->call(st);
1168 st->cr();
1169 }
1170
1171 STEP_IF("printing process", _verbose)
1172 st->cr();
1173 st->print_cr("--------------- P R O C E S S ---------------");
1174 st->cr();
1175
1176 STEP_IF("printing user info", ExtensiveErrorReports && _verbose)
1177 os::print_user_info(st);
1178
1179 STEP_IF("printing all threads", _verbose && _thread != nullptr)
1180 // all threads
1181 Threads::print_on_error(st, _thread, buf, sizeof(buf));
1182 st->cr();
1183
1184 STEP_IF("printing VM state", _verbose)
1185 // Safepoint state
1186 st->print("VM state: ");
1187
1188 if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing");
1189 else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint");
1190 else st->print("not at safepoint");
1191
1192 // Also see if error occurred during initialization or shutdown
1193 if (!Universe::is_fully_initialized()) {
1194 st->print(" (not fully initialized)");
1195 } else if (VM_Exit::vm_exited()) {
1196 st->print(" (shutting down)");
1197 } else {
1198 st->print(" (normal execution)");
1199 }
1200 st->cr();
1201 st->cr();
1202
1203 STEP_IF("printing owned locks on error", _verbose)
1204 // mutexes/monitors that currently have an owner
1205 Mutex::print_owned_locks_on_error(st);
1206 st->cr();
1207
1208 STEP_IF("printing number of OutOfMemoryError and StackOverflow exceptions",
1209 _verbose && Exceptions::has_exception_counts())
1210 st->print_cr("OutOfMemory and StackOverflow Exception counts:");
1211 Exceptions::print_exception_counts_on_error(st);
1212 st->cr();
1213
1214 #ifdef _LP64
1215 STEP_IF("printing compressed oops mode", _verbose && UseCompressedOops)
1216 CompressedOops::print_mode(st);
1217 st->cr();
1218
1219 STEP_IF("printing compressed klass pointers mode", _verbose && UseCompressedClassPointers)
1220 CDS_ONLY(AOTMetaspace::print_on(st);)
1221 Metaspace::print_compressed_class_space(st);
1222 CompressedKlassPointers::print_mode(st);
1223 st->cr();
1224 #endif
1225
1226 STEP_IF("printing heap information", _verbose)
1227 GCLogPrecious::print_on_error(st);
1228
1229 if (Universe::heap() != nullptr) {
1230 st->print_cr("Heap:");
1231 StreamIndentor si(st, 1);
1232 Universe::heap()->print_heap_on(st);
1233 st->cr();
1234 }
1235
1236 STEP_IF("printing GC information", _verbose)
1237 if (Universe::heap() != nullptr) {
1238 Universe::heap()->print_gc_on(st);
1239 st->cr();
1240 }
1241
1242 if (Universe::is_fully_initialized()) {
1243 st->print_cr("Polling page: " PTR_FORMAT, p2i(SafepointMechanism::get_polling_page()));
1244 st->cr();
1245 }
1246
1247 STEP_IF("printing metaspace information", _verbose && Universe::is_fully_initialized())
1248 st->print_cr("Metaspace:");
1249 MetaspaceUtils::print_on(st);
1250 MetaspaceUtils::print_basic_report(st, 0);
1251
1252 STEP_IF("printing code cache information", _verbose && Universe::is_fully_initialized())
1253 // print code cache information before vm abort
1254 CodeCache::print_summary(st);
1255 st->cr();
1256
1257 STEP_IF("printing ring buffers", _verbose)
1258 Events::print_all(st);
1259 st->cr();
1260
1261 STEP_IF("printing dynamic libraries", _verbose)
1262 // dynamic libraries, or memory map
1263 os::print_dll_info(st);
1264 st->cr();
1265
1266 #if INCLUDE_JVMTI
1267 STEP_IF("printing jvmti agent info", _verbose)
1268 os::print_jvmti_agent_info(st);
1269 st->cr();
1270 #endif
1271
1272 STEP_IF("printing native decoder state", _verbose)
1273 Decoder::print_state_on(st);
1274 st->cr();
1275
1276 STEP_IF("printing VM options", _verbose)
1277 // VM options
1278 Arguments::print_on(st);
1279 st->cr();
1280
1281 STEP_IF("printing flags", _verbose)
1282 JVMFlag::printFlags(
1283 st,
1284 true, // with comments
1285 false, // no ranges
1286 true); // skip defaults
1287 st->cr();
1288
1289 STEP_IF("printing warning if internal testing API used", WhiteBox::used())
1290 st->print_cr("Unsupported internal testing APIs have been used.");
1291 st->cr();
1292
1293 STEP_IF("printing log configuration", _verbose)
1294 st->print_cr("Logging:");
1295 LogConfiguration::describe_current_configuration(st);
1296 st->cr();
1297
1298 STEP_IF("printing release file content", _verbose)
1299 st->print_cr("Release file:");
1300 os::print_image_release_file(st);
1301
1302 STEP_IF("printing all environment variables", _verbose)
1303 os::print_environment_variables(st, env_list);
1304 st->cr();
1305
1306 STEP_IF("printing locale settings", _verbose)
1307 os::print_active_locale(st);
1308 st->cr();
1309
1310 STEP_IF("printing signal handlers", _verbose)
1311 os::print_signal_handlers(st, buf, sizeof(buf));
1312 st->cr();
1313
1314 STEP_IF("Native Memory Tracking", _verbose && _thread != nullptr)
1315 MemTracker::error_report(st);
1316 st->cr();
1317
1318 STEP_IF("printing compiler memory info, if any", _verbose)
1319 CompilationMemoryStatistic::print_error_report(st);
1320 st->cr();
1321
1322 STEP_IF("printing periodic trim state", _verbose)
1323 NativeHeapTrimmer::print_state(st);
1324 st->cr();
1325
1326 STEP_IF("printing system", _verbose)
1327 st->print_cr("--------------- S Y S T E M ---------------");
1328 st->cr();
1329
1330 STEP_IF("printing OS information", _verbose)
1331 os::print_os_info(st);
1332 st->cr();
1333
1334 STEP_IF("printing CPU info", _verbose)
1335 os::print_cpu_info(st, buf, sizeof(buf));
1336 st->cr();
1337
1338 STEP_IF("printing memory info", _verbose)
1339 os::print_memory_info(st);
1340 st->cr();
1341
1342 STEP_IF("printing internal vm info", _verbose)
1343 st->print_cr("vm_info: %s", VM_Version::internal_vm_info_string());
1344 st->cr();
1345
1346 // print a defined marker to show that error handling finished correctly.
1347 STEP_IF("printing end marker", _verbose)
1348 st->print_cr("END.");
1349
1350 END
1351
1352 # undef BEGIN
1353 # undef STEP_IF
1354 # undef STEP
1355 # undef REATTEMPT_STEP_IF
1356 # undef END
1357 }
1358
1359 void VMError::set_handshake_timed_out_thread(Thread* thread) {
1360 // Only preserve the first thread to time-out this way. The atomic operation ensures
1361 // visibility to the target thread.
1362 AtomicAccess::replace_if_null(&_handshake_timed_out_thread, thread);
1363 }
1364
1365 void VMError::set_safepoint_timed_out_thread(Thread* thread) {
1366 // Only preserve the first thread to time-out this way. The atomic operation ensures
1367 // visibility to the target thread.
1368 AtomicAccess::replace_if_null(&_safepoint_timed_out_thread, thread);
1369 }
1370
1371 Thread* VMError::get_handshake_timed_out_thread() {
1372 return AtomicAccess::load(&_handshake_timed_out_thread);
1373 }
1374
1375 Thread* VMError::get_safepoint_timed_out_thread() {
1376 return AtomicAccess::load(&_safepoint_timed_out_thread);
1377 }
1378
1379 // Report for the vm_info_cmd. This prints out the information above omitting
1380 // crash and thread specific information. If output is added above, it should be added
1381 // here also, if it is safe to call during a running process.
1382 void VMError::print_vm_info(outputStream* st) {
1383
1384 char buf[O_BUFLEN];
1385 os::prepare_native_symbols();
1386
1387 report_vm_version(st, buf, sizeof(buf));
1388
1389 // STEP("printing summary")
1390
1391 st->cr();
1392 st->print_cr("--------------- S U M M A R Y ------------");
1393 st->cr();
1394
1395 // STEP("printing VM option summary")
1396
1397 // VM options
1398 Arguments::print_summary_on(st);
1399 st->cr();
1400
1401 // STEP("printing summary machine and OS info")
1402
1403 os::print_summary_info(st, buf, sizeof(buf));
1404
1405 // STEP("printing date and time")
1406
1407 os::print_date_and_time(st, buf, sizeof(buf));
1408
1409 // Skip: STEP("printing thread")
1410
1411 // STEP("printing process")
1412
1413 st->cr();
1414 st->print_cr("--------------- P R O C E S S ---------------");
1415 st->cr();
1416
1417 // STEP("printing number of OutOfMemoryError and StackOverflow exceptions")
1418
1419 if (Exceptions::has_exception_counts()) {
1420 st->print_cr("OutOfMemory and StackOverflow Exception counts:");
1421 Exceptions::print_exception_counts_on_error(st);
1422 st->cr();
1423 }
1424
1425 #ifdef _LP64
1426 // STEP("printing compressed oops mode")
1427 if (UseCompressedOops) {
1428 CompressedOops::print_mode(st);
1429 st->cr();
1430 }
1431 #endif
1432
1433 // STEP("printing compressed class ptrs mode")
1434 if (UseCompressedClassPointers) {
1435 CDS_ONLY(AOTMetaspace::print_on(st);)
1436 Metaspace::print_compressed_class_space(st);
1437 CompressedKlassPointers::print_mode(st);
1438 st->cr();
1439 }
1440
1441 // Take heap lock over heap, GC and metaspace printing so that information
1442 // is consistent.
1443 if (Universe::is_fully_initialized()) {
1444 MutexLocker ml(Heap_lock);
1445
1446 // STEP("printing heap information")
1447
1448 GCLogPrecious::print_on_error(st);
1449
1450 {
1451 st->print_cr("Heap:");
1452 StreamIndentor si(st, 1);
1453 Universe::heap()->print_heap_on(st);
1454 st->cr();
1455 }
1456
1457 // STEP("printing GC information")
1458
1459 Universe::heap()->print_gc_on(st);
1460 st->cr();
1461
1462 st->print_cr("Polling page: " PTR_FORMAT, p2i(SafepointMechanism::get_polling_page()));
1463 st->cr();
1464
1465 // STEP("printing metaspace information")
1466
1467 st->print_cr("Metaspace:");
1468 MetaspaceUtils::print_on(st);
1469 MetaspaceUtils::print_basic_report(st, 0);
1470 }
1471
1472 // STEP("printing code cache information")
1473
1474 if (Universe::is_fully_initialized()) {
1475 // print code cache information before vm abort
1476 CodeCache::print_summary(st);
1477 st->cr();
1478 }
1479
1480 // STEP("printing ring buffers")
1481
1482 Events::print_all(st);
1483 st->cr();
1484
1485 // STEP("printing dynamic libraries")
1486
1487 // dynamic libraries, or memory map
1488 os::print_dll_info(st);
1489 st->cr();
1490
1491 #if INCLUDE_JVMTI
1492 os::print_jvmti_agent_info(st);
1493 st->cr();
1494 #endif
1495
1496 // STEP("printing VM options")
1497
1498 // VM options
1499 Arguments::print_on(st);
1500 st->cr();
1501
1502 // STEP("printing warning if internal testing API used")
1503
1504 if (WhiteBox::used()) {
1505 st->print_cr("Unsupported internal testing APIs have been used.");
1506 st->cr();
1507 }
1508
1509 // STEP("printing log configuration")
1510 st->print_cr("Logging:");
1511 LogConfiguration::describe(st);
1512 st->cr();
1513
1514 // STEP("printing release file content")
1515 st->print_cr("Release file:");
1516 os::print_image_release_file(st);
1517
1518 // STEP("printing all environment variables")
1519
1520 os::print_environment_variables(st, env_list);
1521 st->cr();
1522
1523 // STEP("printing locale settings")
1524
1525 os::print_active_locale(st);
1526 st->cr();
1527
1528
1529 // STEP("printing signal handlers")
1530
1531 os::print_signal_handlers(st, buf, sizeof(buf));
1532 st->cr();
1533
1534 // STEP("Native Memory Tracking")
1535 MemTracker::error_report(st);
1536 st->cr();
1537
1538 // STEP("Compiler Memory Statistic")
1539 CompilationMemoryStatistic::print_final_report(st);
1540
1541 // STEP("printing periodic trim state")
1542 NativeHeapTrimmer::print_state(st);
1543 st->cr();
1544
1545
1546 // STEP("printing system")
1547 st->print_cr("--------------- S Y S T E M ---------------");
1548 st->cr();
1549
1550 // STEP("printing OS information")
1551
1552 os::print_os_info(st);
1553 st->cr();
1554
1555 // STEP("printing CPU info")
1556
1557 os::print_cpu_info(st, buf, sizeof(buf));
1558 st->cr();
1559
1560 // STEP("printing memory info")
1561
1562 os::print_memory_info(st);
1563 st->cr();
1564
1565 // STEP("printing internal vm info")
1566
1567 st->print_cr("vm_info: %s", VM_Version::internal_vm_info_string());
1568 st->cr();
1569
1570 // print a defined marker to show that error handling finished correctly.
1571 // STEP("printing end marker")
1572
1573 st->print_cr("END.");
1574 }
1575
1576 /** Expand a pattern into a buffer starting at pos and open a file using constructed path */
1577 static int expand_and_open(const char* pattern, bool overwrite_existing, char* buf, size_t buflen, size_t pos) {
1578 int fd = -1;
1579 int mode = O_RDWR | O_CREAT;
1580 if (overwrite_existing) {
1581 mode |= O_TRUNC;
1582 } else {
1583 mode |= O_EXCL;
1584 }
1585 if (Arguments::copy_expand_pid(pattern, strlen(pattern), &buf[pos], buflen - pos)) {
1586 fd = open(buf, mode, 0666);
1587 }
1588 return fd;
1589 }
1590
1591 /**
1592 * Construct file name for a log file and return it's file descriptor.
1593 * Name and location depends on pattern, default_pattern params and access
1594 * permissions.
1595 */
1596 int VMError::prepare_log_file(const char* pattern, const char* default_pattern, bool overwrite_existing, char* buf, size_t buflen) {
1597 int fd = -1;
1598
1599 // If possible, use specified pattern to construct log file name
1600 if (pattern != nullptr) {
1601 fd = expand_and_open(pattern, overwrite_existing, buf, buflen, 0);
1602 }
1603
1604 // Either user didn't specify, or the user's location failed,
1605 // so use the default name in the current directory
1606 if (fd == -1) {
1607 const char* cwd = os::get_current_directory(buf, buflen);
1608 if (cwd != nullptr) {
1609 size_t pos = strlen(cwd);
1610 int fsep_len = jio_snprintf(&buf[pos], buflen-pos, "%s", os::file_separator());
1611 pos += fsep_len;
1612 if (fsep_len > 0) {
1613 fd = expand_and_open(default_pattern, overwrite_existing, buf, buflen, pos);
1614 }
1615 }
1616 }
1617
1618 // try temp directory if it exists.
1619 if (fd == -1) {
1620 const char* tmpdir = os::get_temp_directory();
1621 if (tmpdir != nullptr && strlen(tmpdir) > 0) {
1622 int pos = jio_snprintf(buf, buflen, "%s%s", tmpdir, os::file_separator());
1623 if (pos > 0) {
1624 fd = expand_and_open(default_pattern, overwrite_existing, buf, buflen, pos);
1625 }
1626 }
1627 }
1628
1629 return fd;
1630 }
1631
1632 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, const void* siginfo,
1633 const void* context, const char* detail_fmt, ...)
1634 {
1635 va_list detail_args;
1636 va_start(detail_args, detail_fmt);
1637 report_and_die(sig, nullptr, detail_fmt, detail_args, thread, pc, siginfo, context, nullptr, 0, 0);
1638 va_end(detail_args);
1639 }
1640
1641 void VMError::report_and_die(Thread* thread, const void* context, const char* filename, int lineno, const char* message,
1642 const char* detail_fmt, ...) {
1643 va_list detail_args;
1644 va_start(detail_args, detail_fmt);
1645 report_and_die(thread, context, filename, lineno, message, detail_fmt, detail_args);
1646 va_end(detail_args);
1647 }
1648
1649 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, const void* siginfo, const void* context)
1650 {
1651 if (ExecutingUnitTests) {
1652 // See TEST_VM_CRASH_SIGNAL gtest macro
1653 char tmp[64];
1654 fprintf(stderr, "signaled: %s", os::exception_name(sig, tmp, sizeof(tmp)));
1655 }
1656
1657 report_and_die(thread, sig, pc, siginfo, context, "%s", "");
1658 }
1659
1660 void VMError::report_and_die(Thread* thread, const void* context, const char* filename, int lineno, const char* message,
1661 const char* detail_fmt, va_list detail_args)
1662 {
1663 report_and_die(INTERNAL_ERROR, message, detail_fmt, detail_args, thread, nullptr, nullptr, context, filename, lineno, 0);
1664 }
1665
1666 void VMError::report_and_die(Thread* thread, const char* filename, int lineno, size_t size,
1667 VMErrorType vm_err_type, const char* detail_fmt, va_list detail_args) {
1668 report_and_die(vm_err_type, nullptr, detail_fmt, detail_args, thread, nullptr, nullptr, nullptr, filename, lineno, size);
1669 }
1670
1671 void VMError::report_and_die(int id, const char* message, const char* detail_fmt, va_list detail_args,
1672 Thread* thread, address pc, const void* siginfo, const void* context, const char* filename,
1673 int lineno, size_t size)
1674 {
1675 // A single scratch buffer to be used from here on.
1676 // Do not rely on it being preserved across function calls.
1677 static char buffer[O_BUFLEN];
1678
1679 // File descriptor to tty to print an error summary to.
1680 // Hard wired to stdout; see JDK-8215004 (compatibility concerns).
1681 static const int fd_out = 1; // stdout
1682
1683 // File descriptor to the error log file.
1684 static int fd_log = -1;
1685
1686 #ifdef CAN_SHOW_REGISTERS_ON_ASSERT
1687 // Disarm assertion poison page, since from this point on we do not need this mechanism anymore and it may
1688 // cause problems in error handling during native OOM, see JDK-8227275.
1689 disarm_assert_poison();
1690 #endif
1691
1692 // Use local fdStream objects only. Do not use global instances whose initialization
1693 // relies on dynamic initialization (see JDK-8214975). Do not rely on these instances
1694 // to carry over into recursions or invocations from other threads.
1695 fdStream out(fd_out);
1696 out.set_scratch_buffer(buffer, sizeof(buffer));
1697
1698 // Depending on the re-entrance depth at this point, fd_log may be -1 or point to an open hs-err file.
1699 fdStream log(fd_log);
1700 log.set_scratch_buffer(buffer, sizeof(buffer));
1701
1702 // How many errors occurred in error handler when reporting first_error.
1703 static int recursive_error_count;
1704
1705 // We will first print a brief message to standard out (verbose = false),
1706 // then save detailed information in log file (verbose = true).
1707 static bool out_done = false; // done printing to standard out
1708 static bool log_done = false; // done saving error log
1709
1710 intptr_t mytid = os::current_thread_id();
1711 if (_first_error_tid == -1 &&
1712 AtomicAccess::cmpxchg(&_first_error_tid, (intptr_t)-1, mytid) == -1) {
1713
1714 if (SuppressFatalErrorMessage) {
1715 os::abort(CreateCoredumpOnCrash);
1716 }
1717
1718 // Initialize time stamps to use the same base.
1719 out.time_stamp().update_to(1);
1720 log.time_stamp().update_to(1);
1721
1722 _id = id;
1723 _message = message;
1724 _thread = thread;
1725 _pc = pc;
1726 _siginfo = siginfo;
1727 _context = context;
1728 _filename = filename;
1729 _lineno = lineno;
1730 _size = size;
1731 jio_vsnprintf(_detail_msg, sizeof(_detail_msg), detail_fmt, detail_args);
1732
1733 reporting_started();
1734 if (!TestUnresponsiveErrorHandler) {
1735 // Record reporting_start_time unless we're running the
1736 // TestUnresponsiveErrorHandler test. For that test we record
1737 // reporting_start_time at the beginning of the test.
1738 record_reporting_start_time();
1739 } else {
1740 out.print_raw_cr("Delaying recording reporting_start_time for TestUnresponsiveErrorHandler.");
1741 }
1742
1743 if (ShowMessageBoxOnError || PauseAtExit) {
1744 show_message_box(buffer, sizeof(buffer));
1745
1746 // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
1747 // WatcherThread can kill JVM if the error handler hangs.
1748 ShowMessageBoxOnError = false;
1749 }
1750
1751 os::check_core_dump_prerequisites(buffer, sizeof(buffer));
1752
1753 // reset signal handlers or exception filter; make sure recursive crashes
1754 // are handled properly.
1755 install_secondary_signal_handler();
1756 } else {
1757 // This is not the first error, see if it happened in a different thread
1758 // or in the same thread during error reporting.
1759 if (_first_error_tid != mytid) {
1760 if (!SuppressFatalErrorMessage) {
1761 char msgbuf[64];
1762 jio_snprintf(msgbuf, sizeof(msgbuf),
1763 "[thread %zd also had an error]",
1764 mytid);
1765 out.print_raw_cr(msgbuf);
1766 }
1767
1768 // Error reporting is not MT-safe, nor can we let the current thread
1769 // proceed, so we block it.
1770 os::infinite_sleep();
1771
1772 } else {
1773 if (recursive_error_count++ > 30) {
1774 if (!SuppressFatalErrorMessage) {
1775 out.print_raw_cr("[Too many errors, abort]");
1776 }
1777 os::die();
1778 }
1779
1780 if (SuppressFatalErrorMessage) {
1781 // If we already hit a secondary error during abort, then calling
1782 // it again is likely to hit another one. But eventually, if we
1783 // don't deadlock somewhere, we will call os::die() above.
1784 os::abort(CreateCoredumpOnCrash);
1785 }
1786
1787 outputStream* const st = log.is_open() ? &log : &out;
1788 st->cr();
1789
1790 // Timeout handling.
1791 if (_step_did_timeout) {
1792 // The current step had a timeout. Lets continue reporting with the next step.
1793 st->print_raw("[timeout occurred during error reporting in step \"");
1794 st->print_raw(_current_step_info);
1795 st->print_cr("\"] after " INT64_FORMAT " s.",
1796 (int64_t)
1797 ((get_current_timestamp() - _step_start_time) / TIMESTAMP_TO_SECONDS_FACTOR));
1798 } else if (_reporting_did_timeout) {
1799 // We hit ErrorLogTimeout. Reporting will stop altogether. Let's wrap things
1800 // up, the process is about to be stopped by the WatcherThread.
1801 st->print_cr("------ Timeout during error reporting after " INT64_FORMAT " s. ------",
1802 (int64_t)
1803 ((get_current_timestamp() - _reporting_start_time) / TIMESTAMP_TO_SECONDS_FACTOR));
1804 st->flush();
1805 // Watcherthread is about to call os::die. Lets just wait.
1806 os::infinite_sleep();
1807 } else {
1808 // A secondary error happened. Print brief information, but take care, since crashing
1809 // here would just recurse endlessly.
1810 // Any information (signal, context, siginfo etc) printed here should use the function
1811 // arguments, not the information stored in *this, since those describe the primary crash.
1812 static char tmp[256]; // cannot use global scratch buffer
1813 // Note: this string does get parsed by a number of jtreg tests,
1814 // see hotspot/jtreg/runtime/ErrorHandling.
1815 st->print("[error occurred during error reporting (%s), id 0x%x",
1816 _current_step_info, id);
1817 if (os::exception_name(id, tmp, sizeof(tmp))) {
1818 st->print(", %s (0x%x) at pc=" PTR_FORMAT, tmp, id, p2i(pc));
1819 } else {
1820 if (should_report_bug(id)) {
1821 st->print(", Internal Error (%s:%d)",
1822 filename == nullptr ? "??" : filename, lineno);
1823 } else {
1824 st->print(", Out of Memory Error (%s:%d)",
1825 filename == nullptr ? "??" : filename, lineno);
1826 }
1827 }
1828 st->print_cr("]");
1829 if (ErrorLogSecondaryErrorDetails) {
1830 static bool recursed = false;
1831 if (!recursed) {
1832 recursed = true;
1833 // Print even more information for secondary errors. This may generate a lot of output
1834 // and possibly disturb error reporting, therefore its optional and only available in debug builds.
1835 if (siginfo != nullptr) {
1836 st->print("[");
1837 os::print_siginfo(st, siginfo);
1838 st->print_cr("]");
1839 }
1840 st->print("[stack: ");
1841 NativeStackPrinter nsp(_thread, context, _filename != nullptr ? get_filename_only() : nullptr, _lineno);
1842 // Subsequent secondary errors build up stack; to avoid flooding the hs-err file with irrelevant
1843 // call stacks, limit the stack we print here (we are only interested in what happened before the
1844 // last assert/fault).
1845 const int max_stack_size = 15;
1846 nsp.print_stack_from_frame(st, tmp, sizeof(tmp), true /* print_source_info */, max_stack_size);
1847 st->print_cr("]");
1848 } // !recursed
1849 recursed = false; // Note: reset outside !recursed
1850 }
1851 }
1852 }
1853 }
1854
1855 // Part 1: print an abbreviated version (the '#' section) to stdout.
1856 if (!out_done) {
1857 // Suppress this output if we plan to print Part 2 to stdout too.
1858 // No need to have the "#" section twice.
1859 if (!(ErrorFileToStdout && out.fd() == 1)) {
1860 report(&out, false);
1861 }
1862
1863 out_done = true;
1864
1865 _current_step = 0;
1866 _current_step_info = "";
1867 }
1868
1869 // Part 2: print a full error log file (optionally to stdout or stderr).
1870 // print to error log file
1871 if (!log_done) {
1872 // see if log file is already open
1873 if (!log.is_open()) {
1874 // open log file
1875 if (ErrorFileToStdout) {
1876 fd_log = 1;
1877 } else if (ErrorFileToStderr) {
1878 fd_log = 2;
1879 } else {
1880 fd_log = prepare_log_file(ErrorFile, "hs_err_pid%p.log", true,
1881 buffer, sizeof(buffer));
1882 if (fd_log != -1) {
1883 out.print_raw("# An error report file with more information is saved as:\n# ");
1884 out.print_raw_cr(buffer);
1885 } else {
1886 out.print_raw_cr("# Can not save log file, dump to screen..");
1887 fd_log = 1;
1888 }
1889 }
1890 log.set_fd(fd_log);
1891 }
1892
1893 report(&log, true);
1894 log_done = true;
1895 _current_step = 0;
1896 _current_step_info = "";
1897
1898 if (fd_log > 3) {
1899 ::close(fd_log);
1900 fd_log = -1;
1901 }
1902
1903 log.set_fd(-1);
1904 }
1905
1906 JFR_ONLY(Jfr::on_vm_shutdown(true, false, static_cast<VMErrorType>(_id) == OOM_JAVA_HEAP_FATAL);)
1907
1908 if (PrintNMTStatistics) {
1909 fdStream fds(fd_out);
1910 MemTracker::final_report(&fds);
1911 }
1912
1913 static bool skip_replay = ReplayCompiles && !ReplayReduce; // Do not overwrite file during replay
1914 if (DumpReplayDataOnError && _thread && _thread->is_Compiler_thread() && !skip_replay) {
1915 skip_replay = true;
1916 ciEnv* env = ciEnv::current();
1917 if (env != nullptr && env->task() != nullptr) {
1918 const bool overwrite = false; // We do not overwrite an existing replay file.
1919 int fd = prepare_log_file(ReplayDataFile, "replay_pid%p.log", overwrite, buffer, sizeof(buffer));
1920 if (fd != -1) {
1921 FILE* replay_data_file = os::fdopen(fd, "w");
1922 if (replay_data_file != nullptr) {
1923 fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
1924 env->dump_replay_data_unsafe(&replay_data_stream);
1925 out.print_raw("#\n# Compiler replay data is saved as:\n# ");
1926 out.print_raw_cr(buffer);
1927 } else {
1928 int e = errno;
1929 out.print_raw("#\n# Can't open file to dump replay data. Error: ");
1930 out.print_raw_cr(os::strerror(e));
1931 close(fd);
1932 }
1933 }
1934 }
1935 }
1936
1937 #if INCLUDE_JVMCI
1938 if (JVMCI::fatal_log_filename() != nullptr) {
1939 out.print_raw("#\n# The JVMCI shared library error report file is saved as:\n# ");
1940 out.print_raw_cr(JVMCI::fatal_log_filename());
1941 }
1942 #endif
1943
1944 static bool skip_bug_url = !should_submit_bug_report(_id);
1945 if (!skip_bug_url) {
1946 skip_bug_url = true;
1947
1948 out.print_raw_cr("#");
1949 print_bug_submit_message(&out, _thread);
1950 }
1951
1952 static bool skip_OnError = false;
1953 if (!skip_OnError && OnError && OnError[0]) {
1954 skip_OnError = true;
1955
1956 // Flush output and finish logs before running OnError commands.
1957 ostream_abort();
1958
1959 out.print_raw_cr("#");
1960 out.print_raw ("# -XX:OnError=\"");
1961 out.print_raw (OnError);
1962 out.print_raw_cr("\"");
1963
1964 char* cmd;
1965 const char* ptr = OnError;
1966 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != nullptr){
1967 out.print_raw ("# Executing ");
1968 #if defined(LINUX) || defined(_ALLBSD_SOURCE)
1969 out.print_raw ("/bin/sh -c ");
1970 #elif defined(_WINDOWS)
1971 out.print_raw ("cmd /C ");
1972 #endif
1973 out.print_raw ("\"");
1974 out.print_raw (cmd);
1975 out.print_raw_cr("\" ...");
1976
1977 if (os::fork_and_exec(cmd) < 0) {
1978 out.print_cr("os::fork_and_exec failed: %s (%s=%d)",
1979 os::strerror(errno), os::errno_name(errno), errno);
1980 }
1981 }
1982
1983 // done with OnError
1984 OnError = nullptr;
1985 }
1986
1987 #if defined _WINDOWS
1988 if (UseOSErrorReporting) {
1989 raise_fail_fast(_siginfo, _context);
1990 }
1991 #endif // _WINDOWS
1992
1993 // os::abort() will call abort hooks, try it first.
1994 static bool skip_os_abort = false;
1995 if (!skip_os_abort) {
1996 skip_os_abort = true;
1997 bool dump_core = should_report_bug(_id);
1998 os::abort(dump_core && CreateCoredumpOnCrash, _siginfo, _context);
1999 // if os::abort() doesn't abort, try os::die();
2000 }
2001 os::die();
2002 }
2003
2004 /*
2005 * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this
2006 * ensures utilities such as jmap can observe the process is a consistent state.
2007 */
2008 class VM_ReportJavaOutOfMemory : public VM_Operation {
2009 private:
2010 const char* _message;
2011 public:
2012 VM_ReportJavaOutOfMemory(const char* message) { _message = message; }
2013 VMOp_Type type() const { return VMOp_ReportJavaOutOfMemory; }
2014 void doit();
2015 };
2016
2017 void VM_ReportJavaOutOfMemory::doit() {
2018 // Don't allocate large buffer on stack
2019 static char buffer[O_BUFLEN];
2020
2021 tty->print_cr("#");
2022 tty->print_cr("# java.lang.OutOfMemoryError: %s", _message);
2023 tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError);
2024
2025 // make heap parsability
2026 Universe::heap()->ensure_parsability(false); // no need to retire TLABs
2027
2028 char* cmd;
2029 const char* ptr = OnOutOfMemoryError;
2030 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != nullptr){
2031 tty->print("# Executing ");
2032 #if defined(LINUX)
2033 tty->print ("/bin/sh -c ");
2034 #endif
2035 tty->print_cr("\"%s\"...", cmd);
2036
2037 if (os::fork_and_exec(cmd) < 0) {
2038 tty->print_cr("os::fork_and_exec failed: %s (%s=%d)",
2039 os::strerror(errno), os::errno_name(errno), errno);
2040 }
2041 }
2042 }
2043
2044 void VMError::report_java_out_of_memory(const char* message) {
2045 if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
2046 MutexLocker ml(Heap_lock);
2047 VM_ReportJavaOutOfMemory op(message);
2048 VMThread::execute(&op);
2049 }
2050 }
2051
2052 void VMError::show_message_box(char *buf, int buflen) {
2053 bool yes;
2054 do {
2055 error_string(buf, buflen);
2056 yes = os::start_debugging(buf,buflen);
2057 } while (yes);
2058 }
2059
2060 // Fatal error handling is subject to several timeouts:
2061 // - a global timeout (controlled via ErrorLogTimeout)
2062 // - local error reporting step timeouts.
2063 //
2064 // The latter aims to "give the JVM a kick" if it gets stuck in one particular place during
2065 // error reporting. This prevents one error reporting step from hogging all the time allotted
2066 // to error reporting under ErrorLogTimeout.
2067 //
2068 // VMError::check_timeout() is called from the watcher thread and checks for either global
2069 // or step timeout. If a timeout happened, we interrupt the reporting thread and set either
2070 // _reporting_did_timeout or _step_did_timeout to signal which timeout fired. Function returns
2071 // true if the *global* timeout fired, which will cause WatcherThread to shut down the JVM
2072 // immediately.
2073 bool VMError::check_timeout() {
2074
2075 // This function is supposed to be called from watcher thread during fatal error handling only.
2076 assert(VMError::is_error_reported(), "Only call during error handling");
2077 assert(Thread::current()->is_Watcher_thread(), "Only call from watcher thread");
2078
2079 if (ErrorLogTimeout == 0) {
2080 return false;
2081 }
2082
2083 // There are three situations where we suppress the *global* error timeout:
2084 // - if the JVM is embedded and the launcher has its abort hook installed.
2085 // That must be allowed to run.
2086 // - if the user specified one or more OnError commands to run, and these
2087 // did not yet run. These must have finished.
2088 // - if the user (typically developer) specified ShowMessageBoxOnError,
2089 // and the error box has not yet been shown
2090 const bool ignore_global_timeout =
2091 (ShowMessageBoxOnError
2092 || (OnError != nullptr && OnError[0] != '\0')
2093 || Arguments::abort_hook() != nullptr);
2094
2095 const jlong now = get_current_timestamp();
2096
2097 // Global timeout hit?
2098 if (!ignore_global_timeout) {
2099 const jlong reporting_start_time = get_reporting_start_time();
2100 // Timestamp is stored in nanos.
2101 if (reporting_start_time > 0) {
2102 const jlong end = reporting_start_time + (jlong)ErrorLogTimeout * TIMESTAMP_TO_SECONDS_FACTOR;
2103 if (end <= now && !_reporting_did_timeout) {
2104 // We hit ErrorLogTimeout and we haven't interrupted the reporting
2105 // thread yet.
2106 _reporting_did_timeout = true;
2107 interrupt_reporting_thread();
2108 return true; // global timeout
2109 }
2110 }
2111 }
2112
2113 // Reporting step timeout?
2114 const jlong step_start_time = get_step_start_time();
2115 if (step_start_time > 0) {
2116 // A step times out after a quarter of the total timeout. Steps are mostly fast unless they
2117 // hang for some reason, so this simple rule allows for three hanging step and still
2118 // hopefully leaves time enough for the rest of the steps to finish.
2119 const int max_step_timeout_secs = 5;
2120 const jlong timeout_duration = MAX2((jlong)max_step_timeout_secs, (jlong)ErrorLogTimeout * TIMESTAMP_TO_SECONDS_FACTOR / 4);
2121 const jlong end = step_start_time + timeout_duration;
2122 if (end <= now && !_step_did_timeout) {
2123 // The step timed out and we haven't interrupted the reporting
2124 // thread yet.
2125 _step_did_timeout = true;
2126 interrupt_reporting_thread();
2127 return false; // (Not a global timeout)
2128 }
2129 }
2130
2131 return false;
2132
2133 }
2134
2135 #ifdef ASSERT
2136 typedef void (*voidfun_t)();
2137
2138 // Crash with an authentic sigfpe; behavior is subtly different from a real signal
2139 // compared to one generated with raise (asynchronous vs synchronous). See JDK-8065895.
2140 volatile int sigfpe_int = 0;
2141
2142 ATTRIBUTE_NO_UBSAN
2143 static void ALWAYSINLINE crash_with_sigfpe() {
2144
2145 // generate a native synchronous SIGFPE where possible;
2146 sigfpe_int = sigfpe_int/sigfpe_int;
2147
2148 // if that did not cause a signal (e.g. on ppc), just
2149 // raise the signal.
2150 #ifndef _WIN32
2151 // OSX implements raise(sig) incorrectly so we need to
2152 // explicitly target the current thread
2153 pthread_kill(pthread_self(), SIGFPE);
2154 #endif
2155
2156 } // end: crash_with_sigfpe
2157
2158 // crash with sigsegv at non-null address.
2159 static void ALWAYSINLINE crash_with_segfault() {
2160
2161 int* crash_addr = reinterpret_cast<int*>(VMError::segfault_address);
2162 *crash_addr = 1;
2163
2164 } // end: crash_with_segfault
2165
2166 // crash in a controlled way:
2167 // 1 - assert
2168 // 2 - guarantee
2169 // 14 - SIGSEGV
2170 // 15 - SIGFPE
2171 void VMError::controlled_crash(int how) {
2172
2173 // Case 14 is tested by test/hotspot/jtreg/runtime/ErrorHandling/SafeFetchInErrorHandlingTest.java.
2174 // Case 15 is tested by test/hotspot/jtreg/runtime/ErrorHandling/SecondaryErrorTest.java.
2175 // Case 16 is tested by test/hotspot/jtreg/runtime/ErrorHandling/ThreadsListHandleInErrorHandlingTest.java.
2176 // Case 17 is tested by test/hotspot/jtreg/runtime/ErrorHandling/NestedThreadsListHandleInErrorHandlingTest.java.
2177
2178 // We try to grab Threads_lock to keep ThreadsSMRSupport::print_info_on()
2179 // from racing with Threads::add() or Threads::remove() as we
2180 // generate the hs_err_pid file. This makes our ErrorHandling tests
2181 // more stable.
2182 if (!Threads_lock->owned_by_self()) {
2183 Threads_lock->try_lock();
2184 // The VM is going to die so no need to unlock Thread_lock.
2185 }
2186
2187 switch (how) {
2188 case 1: assert(how == 0, "test assert"); break;
2189 case 2: guarantee(how == 0, "test guarantee"); break;
2190
2191 // The other cases are unused.
2192 case 14: crash_with_segfault(); break;
2193 case 15: crash_with_sigfpe(); break;
2194 case 16: {
2195 ThreadsListHandle tlh;
2196 fatal("Force crash with an active ThreadsListHandle.");
2197 }
2198 case 17: {
2199 ThreadsListHandle tlh;
2200 {
2201 ThreadsListHandle tlh2;
2202 fatal("Force crash with a nested ThreadsListHandle.");
2203 }
2204 }
2205 case 18: {
2206 // Trigger an error that should cause ASAN to report a double free or use-after-free.
2207 // Please note that this is not 100% bullet-proof since it assumes that this block
2208 // is not immediately repurposed by some other thread after free.
2209 void* const p = os::malloc(4096, mtTest);
2210 os::free(p);
2211 os::free(p);
2212 }
2213 default:
2214 // If another number is given, give a generic crash.
2215 fatal("Crashing with number %d", how);
2216 }
2217 tty->print_cr("controlled_crash: survived intentional crash. Did you suppress the assert?");
2218 ShouldNotReachHere();
2219 }
2220 #endif // !ASSERT
2221
2222 VMErrorCallbackMark::VMErrorCallbackMark(VMErrorCallback* callback)
2223 : _thread(Thread::current()) {
2224 callback->_next = _thread->_vm_error_callbacks;
2225 _thread->_vm_error_callbacks = callback;
2226 }
2227
2228 VMErrorCallbackMark::~VMErrorCallbackMark() {
2229 assert(_thread->_vm_error_callbacks != nullptr, "Popped too far");
2230 _thread->_vm_error_callbacks = _thread->_vm_error_callbacks->_next;
2231 }