From a75309373cd301acad473bbb8ea04670facd6e2a Mon Sep 17 00:00:00 2001 From: Kunshan Wang Date: Tue, 3 Dec 2024 17:37:46 +0800 Subject: [PATCH] Minor changes for debugging. (#1245) Added `MMTK::debug_print_vm_map` which prints the memory ranges of spaces. `NullableObjectReference` now implements `Clone`, `Copy`, `Display` and `Debug`. This allows the binding to print its value like `Address` and `ObjectReference`, and is useful for logging API functions that involve `NullableObjectReference` parameters. --- src/mmtk.rs | 26 ++++++++++++++++++++++++++ src/util/api_util.rs | 13 +++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/mmtk.rs b/src/mmtk.rs index 23653ff769..24aa57d6c7 100644 --- a/src/mmtk.rs +++ b/src/mmtk.rs @@ -558,4 +558,30 @@ impl MMTK { }); ret } + + /// Print VM maps. It will print the memory ranges used by spaces as well as some attributes of + /// the spaces. + /// + /// - "I": The space is immortal. Its objects will never die. + /// - "N": The space is non-movable. Its objects will never move. + /// + /// Arguments: + /// * `out`: the place to print the VM maps. + /// * `space_name`: If `None`, print all spaces; + /// if `Some(n)`, only print the space whose name is `n`. + pub fn debug_print_vm_maps( + &self, + out: &mut impl std::fmt::Write, + space_name: Option<&str>, + ) -> Result<(), std::fmt::Error> { + let mut result_so_far = Ok(()); + self.get_plan().for_each_space(&mut |space| { + if result_so_far.is_ok() + && (space_name.is_none() || space_name == Some(space.get_name())) + { + result_so_far = crate::policy::space::print_vm_map(space, out); + } + }); + result_so_far + } } diff --git a/src/util/api_util.rs b/src/util/api_util.rs index a923dec720..2dfc7db021 100644 --- a/src/util/api_util.rs +++ b/src/util/api_util.rs @@ -15,6 +15,7 @@ use super::{Address, ObjectReference}; /// It is intended for passing an `Option` values to and from native programs /// (usually C or C++) that have null pointers. #[repr(transparent)] +#[derive(Clone, Copy)] pub struct NullableObjectReference(usize); impl From for Option { @@ -31,3 +32,15 @@ impl From> for NullableObjectReference { Self(encoded) } } + +impl std::fmt::Display for NullableObjectReference { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:#x}", self.0) + } +} + +impl std::fmt::Debug for NullableObjectReference { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self, f) + } +}