Files
openidea/crates/ide-heap/src/strings.rs
T
ntbang0901 8d09479a65 feat(debugger): heap snapshots of a debugged JVM (#2649)
Capture a heap dump of the debugged JVM over the attach listener (GC.heap_dump, after checking free disk space and the JDK's rules for suspended VMs) or open an .hprof file, and browse it in an editor tab: classes with shallow and retained sizes, biggest objects, dominators, the path from an object to a GC root, and a comparison of two snapshots. A snapshot captured at a stop adds a Retained Size column to that stop's Memory tab.

New crate ide-heap: HotSpot hprof parser (bounds-checked; unfinished or crafted dumps are refused), object graph, dominator tree (semi-NCA), retained sizes and GC-root paths. Snapshots live in private directories removed with their window; an opened dump is analyzed from a private copy so changes to the user's file can't affect the mapped data.

Co-authored-by: ntbang0901 <121003585+ntbang0901@users.noreply.github.com>
2026-11-11 17:43:07 +08:00

67 lines
2.8 KiB
Rust

//! Text of `java.lang.String` objects.
use crate::hprof::{BasicType, HeapRecord};
use crate::index::{HeapIndex, id_value, instance_fields};
const STRING: &str = "java.lang.String";
const LATIN1: u8 = 0;
const UTF16: u8 = 1;
/// The text of String `object`, cut to `max_chars` chars plus `…`. `None` for anything that is
/// not a String, or one whose `value` / `coder` the dump does not hold as expected.
///
/// JDK 9+ keeps a `byte[]` and a `coder`; UTF-16 bytes are in the JVM's native order, which
/// hprof copies as is, read here as little-endian (x86-64, AArch64; a big-endian JVM's text would
/// come out garbled). JDK 8 keeps a `char[]`, whose elements hprof writes big-endian.
pub(crate) fn string_preview(index: &HeapIndex, object: u32, max_chars: usize) -> Option<String> {
let slot = *index.types.get(object as usize)?;
if index.class_info.get(slot as usize)?.name != STRING {
return None;
}
let Some(HeapRecord::Instance { data, .. }) = index.record(object) else { return None };
let fields = instance_fields(index, slot, data);
let value = fields.iter().find(|f| f.name == "value" && f.ty == BasicType::Object)?;
let array = index.ids.rank(id_value(value.bytes).filter(|&id| id != 0)?)?;
let coder = fields.iter().find(|f| f.name == "coder" && f.ty == BasicType::Byte).and_then(|f| f.bytes.first());
let Some(HeapRecord::PrimitiveArray { ty, data, .. }) = index.record(array) else { return None };
match (ty, coder) {
(BasicType::Byte, Some(&LATIN1)) => Some(cut(data.iter().map(|&b| char::from(b)), max_chars)),
(BasicType::Byte, Some(&UTF16)) => utf16(data, u16::from_le_bytes, max_chars),
(BasicType::Char, _) => utf16(data, u16::from_be_bytes, max_chars),
_ => None,
}
}
/// The text of `char[]` `object`, as [`string_preview`] cuts it.
pub(crate) fn char_array_text(index: &HeapIndex, object: u32, max_chars: usize) -> Option<String> {
match index.record(object)? {
HeapRecord::PrimitiveArray { ty: BasicType::Char, data, .. } => utf16(data, u16::from_be_bytes, max_chars),
_ => None,
}
}
/// An odd byte count is `None`; a lone surrogate becomes U+FFFD.
fn utf16(bytes: &[u8], unit: fn([u8; 2]) -> u16, max_chars: usize) -> Option<String> {
if !bytes.len().is_multiple_of(2) {
return None;
}
let units = bytes.as_chunks::<2>().0.iter().map(|&pair| unit(pair));
Some(cut(char::decode_utf16(units).map(|c| c.unwrap_or(char::REPLACEMENT_CHARACTER)), max_chars))
}
/// Chars are whole code points, so a cut never splits a surrogate pair.
fn cut(chars: impl Iterator<Item = char>, max_chars: usize) -> String {
let mut out = String::new();
for (i, c) in chars.enumerate() {
if i == max_chars {
out.push('…');
break;
}
out.push(c);
}
out
}
#[cfg(test)]
mod tests;