Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/hotspot/share/code/codeBlob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ void CodeBlob::dump_for_addr(address addr, outputStream* st, bool verbose) const
nm->print_nmethod(true);
} else {
nm->print_on(st);
nm->print_code_snippet(st, addr);
}
return;
}
Expand Down
40 changes: 40 additions & 0 deletions src/hotspot/share/code/nmethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4308,6 +4308,46 @@ void nmethod::print_value_on_impl(outputStream* st) const {
#endif
}

void nmethod::print_code_snippet(outputStream* st, address addr) const {
if (entry_point() <= addr && addr < code_end()) {
// Pointing into the nmethod's code. Try to disassemble some instructions around addr.
// Determine conservative start and end points.
address start;
if (frame_complete_offset() != CodeOffsets::frame_never_safe &&
addr >= code_begin() + frame_complete_offset()) {
start = code_begin() + frame_complete_offset();
} else {
start = (addr < verified_entry_point()) ? entry_point() : verified_entry_point();
}
address start_for_hex_dump = start; // We can choose a different starting point for hex dump, below.
address end = code_end();

// Try using relocations to find closer instruction start and end points.
// (Some platforms have variable length instructions and can only
// disassemble correctly at instruction start addresses.)
RelocIterator iter((nmethod*)this, start);
while (iter.next() && iter.addr() < addr) { // find relocation before addr
// Note: There's a relocation which doesn't point to an instruction start:
// ZBarrierRelocationFormatStoreGoodAfterMov with ZGC on x86_64
// We could detect and skip it, but hex dump is still usable when
// disassembler produces garbage in such a very rare case.
start = iter.addr();
// We want at least 64 Bytes ahead in hex dump.
if (iter.addr() <= (addr - 64)) start_for_hex_dump = iter.addr();
}
if (iter.has_current()) {
if (iter.addr() == addr) iter.next(); // find relocation after addr
if (iter.has_current()) end = iter.addr();
}

Copy link
Member

Choose a reason for hiding this comment

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

IIUC, the size of the printout is somewhat random. In the extreme cases, this may be either (close to) start-of-method to end-of-method, so almost the whole method. Or, it may be from an address very close to the address, so a very small snippet.

Tying the end address to a relocation is not strictly necessary, no? We could just print to `MIN2(code end, addr + 64)? Disassembler should be fine if the printout stops in the middle of an instruction, as long as instruction addresses are correct?

And could we start printing at the relocation preceding-or-at addr - 64 instead, to ensure we have at least 64 bytes of printout before the crash address?

Copy link
Contributor Author

@TheRealMDoerr TheRealMDoerr Oct 23, 2025

Choose a reason for hiding this comment

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

Right, the size is somewhat random. Relocations seem to be the most fine-grained information we currently have. In addition, they typically point to some meaningful points in the code. This PR disassembles the smallest possible snippet around the given address using relocations as start and end.

Right, having a relocation as end address is technically not strictly required. However, I've seen that the disassembler on x86 produced garbage as well when the end is not an instruction boundary.

I agree with you that we usually want at least 64 Bytes ahead. On the other hand, some people don't want too much, either. See JDK-8274986.
So, I changed only the hex dump for which we can afford printing more without bloating the hs_err file too much. Please take a look at my new commit.

Btw. addr is typically not a crash address, but one which is referenced by a register and somehow related to a crash.

// Always print hex. Disassembler may still have problems when hitting an incorrect instruction start.
os::print_hex_dump(st, start_for_hex_dump, end, 1, /* print_ascii=*/false);
if (!Disassembler::is_abstract()) {
Disassembler::decode(start, end, st);
}
}
}

#ifndef PRODUCT

void nmethod::print_calls(outputStream* st) {
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/code/nmethod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,7 @@ class nmethod : public CodeBlob {
void print_on_impl(outputStream* st) const;
void print_code();
void print_value_on_impl(outputStream* st) const;
void print_code_snippet(outputStream* st, address addr) const;

#if defined(SUPPORT_DATA_STRUCTS)
// print output in opt build for disassembler library
Expand Down