Navigation Menu

Skip to content

Commit

Permalink
8272586: emit abstract machine code in hs-err logs
Browse files Browse the repository at this point in the history
8275031: runtime/ErrorHandling/MachCodeFramesInErrorFile.java fails when hsdis is present
8277102: Dubious PrintCompilation output

Reviewed-by: mbaesken
Backport-of: b60837a7d5d6f920d2fb968369564df155dc1018
  • Loading branch information
TheRealMDoerr committed Jun 20, 2023
1 parent d85a847 commit 393aeaf
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 44 deletions.
9 changes: 8 additions & 1 deletion src/hotspot/share/code/nmethod.cpp
Expand Up @@ -67,6 +67,7 @@
#include "runtime/sharedRuntime.hpp"
#include "runtime/signature.hpp"
#include "runtime/sweeper.hpp"
#include "runtime/threadWXSetters.inline.hpp"
#include "runtime/vmThread.hpp"
#include "utilities/align.hpp"
#include "utilities/copy.hpp"
Expand Down Expand Up @@ -2573,7 +2574,7 @@ void nmethod::print(outputStream* st) const {
st->print("(n/a) ");
}

print_on(tty, NULL);
print_on(st, NULL);

if (WizardMode) {
st->print("((nmethod*) " INTPTR_FORMAT ") ", p2i(this));
Expand Down Expand Up @@ -2924,6 +2925,9 @@ void nmethod::decode2(outputStream* ost) const {
AbstractDisassembler::show_block_comment());
#endif

// Decoding an nmethod can write to a PcDescCache (see PcDescCache::add_pc_desc)
MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, Thread::current());)

st->cr();
this->print(st);
st->cr();
Expand All @@ -2933,7 +2937,10 @@ void nmethod::decode2(outputStream* ost) const {
//---< Print real disassembly >---
//----------------------------------
if (! use_compressed_format) {
st->print_cr("[Disassembly]");
Disassembler::decode(const_cast<nmethod*>(this), st);
st->bol();
st->print_cr("[/Disassembly]");
return;
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/compiler/disassembler.cpp
Expand Up @@ -887,7 +887,7 @@ void Disassembler::decode(CodeBlob* cb, outputStream* st) {
if (cb->is_nmethod()) {
// If we have an nmethod at hand,
// call the specialized decoder directly.
decode((nmethod*)cb, st);
((nmethod*)cb)->decode2(st);
return;
}

Expand Down
166 changes: 125 additions & 41 deletions src/hotspot/share/utilities/vmError.cpp
Expand Up @@ -85,6 +85,7 @@ Thread* VMError::_thread;
address VMError::_pc;
void* VMError::_siginfo;
void* VMError::_context;
bool VMError::_print_native_stack_used = false;
const char* VMError::_filename;
int VMError::_lineno;
size_t VMError::_size;
Expand Down Expand Up @@ -240,6 +241,107 @@ void VMError::print_stack_trace(outputStream* st, JavaThread* jt,
#endif // ZERO
}

/**
* Adds `value` to `list` iff it's not already present and there is sufficient
* capacity (i.e. length(list) < `list_capacity`). The length of the list
* is the index of the first nullptr entry.
*
* @ return true if the value was added, false otherwise
*/
static bool add_if_absent(address value, address* list, int list_capacity) {
for (int i = 0; i < list_capacity; i++) {
if (list[i] == value) {
return false;
}
if (list[i] == nullptr) {
list[i] = value;
if (i + 1 < list_capacity) {
list[i + 1] = nullptr;
}
return true;
}
}
return false;
}

/**
* Prints the VM generated code unit, if any, containing `pc` if it has not already
* been printed. If the code unit is an InterpreterCodelet or StubCodeDesc, it is
* only printed if `is_crash_pc` is true.
*
* @param printed array of code units that have already been printed (delimited by NULL entry)
* @param printed_capacity the capacity of `printed`
* @return true if the code unit was printed, false otherwise
*/
static bool print_code(outputStream* st, Thread* thread, address pc, bool is_crash_pc,
address* printed, int printed_capacity) {
if (Interpreter::contains(pc)) {
if (is_crash_pc) {
// The interpreter CodeBlob is very large so try to print the codelet instead.
InterpreterCodelet* codelet = Interpreter::codelet_containing(pc);
if (codelet != nullptr) {
if (add_if_absent((address) codelet, printed, printed_capacity)) {
codelet->print_on(st);
Disassembler::decode(codelet->code_begin(), codelet->code_end(), st);
return true;
}
}
}
} else {
StubCodeDesc* desc = StubCodeDesc::desc_for(pc);
if (desc != nullptr) {
if (is_crash_pc) {
if (add_if_absent((address) desc, printed, printed_capacity)) {
desc->print_on(st);
Disassembler::decode(desc->begin(), desc->end(), st);
return true;
}
}
} else if (thread != nullptr) {
CodeBlob* cb = CodeCache::find_blob(pc);
if (cb != nullptr && add_if_absent((address) cb, printed, printed_capacity)) {
// Disassembling nmethod will incur resource memory allocation,
// only do so when thread is valid.
ResourceMark rm(thread);
Disassembler::decode(cb, st);
st->cr();
return true;
}
}
}
return false;
}

/**
* Gets the caller frame of `fr`.
*
* @returns an invalid frame (i.e. fr.pc() === 0) if the caller cannot be obtained
*/
static frame next_frame(frame fr, Thread* t) {
// Compiled code may use EBP register on x86 so it looks like
// non-walkable C frame. Use frame.sender() for java frames.
frame invalid;
if (t != nullptr && t->is_Java_thread()) {
// Catch very first native frame by using stack address.
// For JavaThread stack_base and stack_size should be set.
if (!t->is_in_full_stack((address)(fr.real_fp() + 1))) {
return invalid;
}
if (fr.is_java_frame() || fr.is_native_frame() || fr.is_runtime_frame()) {
RegisterMap map(t->as_Java_thread(), false); // No update
return fr.sender(&map);
} else {
// is_first_C_frame() does only simple checks for frame pointer,
// it will pass if java compiled code has a pointer in EBP.
if (os::is_first_C_frame(&fr)) return invalid;
return os::get_sender_for_C_frame(&fr);
}
} else {
if (os::is_first_C_frame(&fr)) return invalid;
return os::get_sender_for_C_frame(&fr);
}
}

void VMError::print_native_stack(outputStream* st, frame fr, Thread* t, char* buf, int buf_size) {

// see if it's a valid frame
Expand All @@ -257,26 +359,9 @@ void VMError::print_native_stack(outputStream* st, frame fr, Thread* t, char* bu
}
}
st->cr();
// Compiled code may use EBP register on x86 so it looks like
// non-walkable C frame. Use frame.sender() for java frames.
if (t && t->is_Java_thread()) {
// Catch very first native frame by using stack address.
// For JavaThread stack_base and stack_size should be set.
if (!t->is_in_full_stack((address)(fr.real_fp() + 1))) {
break;
}
if (fr.is_java_frame() || fr.is_native_frame() || fr.is_runtime_frame()) {
RegisterMap map(t->as_Java_thread(), false); // No update
fr = fr.sender(&map);
} else {
// is_first_C_frame() does only simple checks for frame pointer,
// it will pass if java compiled code has a pointer in EBP.
if (os::is_first_C_frame(&fr)) break;
fr = os::get_sender_for_C_frame(&fr);
}
} else {
if (os::is_first_C_frame(&fr)) break;
fr = os::get_sender_for_C_frame(&fr);
fr = next_frame(fr, t);
if (fr.pc() == nullptr) {
break;
}
}

Expand Down Expand Up @@ -746,6 +831,7 @@ void VMError::report(outputStream* st, bool _verbose) {
: os::current_frame();

print_native_stack(st, fr, _thread, buf, sizeof(buf));
_print_native_stack_used = true;
}
}

Expand Down Expand Up @@ -820,30 +906,28 @@ void VMError::report(outputStream* st, bool _verbose) {
st->cr();
}

STEP("printing code blob if possible")
STEP("printing code blobs if possible")

if (_verbose && _context) {
CodeBlob* cb = CodeCache::find_blob(_pc);
if (cb != NULL) {
if (Interpreter::contains(_pc)) {
// The interpreter CodeBlob is very large so try to print the codelet instead.
InterpreterCodelet* codelet = Interpreter::codelet_containing(_pc);
if (codelet != NULL) {
codelet->print_on(st);
Disassembler::decode(codelet->code_begin(), codelet->code_end(), st);
}
} else {
StubCodeDesc* desc = StubCodeDesc::desc_for(_pc);
if (desc != NULL) {
desc->print_on(st);
Disassembler::decode(desc->begin(), desc->end(), st);
} else if (_thread != NULL) {
// Disassembling nmethod will incur resource memory allocation,
// only do so when thread is valid.
ResourceMark rm(_thread);
Disassembler::decode(cb, st);
st->cr();
if (!_print_native_stack_used) {
// Only try print code of the crashing frame since
// we cannot walk the native stack using next_frame.
const int printed_capacity = 1;
address printed_singleton = nullptr;
address* printed = &printed_singleton;
print_code(st, _thread, _pc, true, printed, 1);
} else {
// Print up to the first 5 unique code units on the stack
const int printed_capacity = 5;
address printed[printed_capacity];
printed[0] = nullptr; // length(list) == index of first nullptr

frame fr = os::fetch_frame_from_context(_context);
for (int count = 0; count < printed_capacity && fr.pc() != nullptr; ) {
if (print_code(st, _thread, fr.pc(), fr.pc() == _pc, printed, printed_capacity)) {
count++;
}
fr = next_frame(fr, _thread);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/hotspot/share/utilities/vmError.hpp
Expand Up @@ -51,6 +51,10 @@ class VMError : public AllStatic {
static void* _context; // ContextRecord on Windows,
// ucontext_t on Solaris/Linux

// records if VMError::print_native_stack was used to
// print the native stack instead of os::platform_print_native_stack
static bool _print_native_stack_used;

// additional info for VM internal errors
static const char* _filename;
static int _lineno;
Expand Down
Expand Up @@ -101,7 +101,16 @@ private void check(CompileCodeTestCase testCase) {
// Therefore compare strings 2 and 3.
String str2 = CompilerToVMHelper.disassembleCodeBlob(installedCode);
String str3 = CompilerToVMHelper.disassembleCodeBlob(installedCode);
Asserts.assertEQ(str2, str3,
String[] str2Lines = str2.split(System.lineSeparator());
String[] str3Lines = str3.split(System.lineSeparator());
// skip the first two lines since it contains a timestamp that may vary from different invocations
// <empty-line>
// Compiled method (c2) 309 463 4 compiler.jvmci.compilerToVM.CompileCodeTestCase$Dummy::staticMethod (1 bytes)
// <empty-line>
// Compiled method (c2) 310 463 4 compiler.jvmci.compilerToVM.CompileCodeTestCase$Dummy::staticMethod (1 bytes)
for (int i = 2; i < str2Lines.length; i++) {
Asserts.assertEQ(str2Lines[i], str3Lines[i],
testCase + " : 3nd invocation returned different value from 2nd");
}
}
}

1 comment on commit 393aeaf

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.