Skip to content

Commit e415cad

Browse files
committed
Implement a crude stack printing mechanism
1 parent 984b46c commit e415cad

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/range_map.rs

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ impl<T> RangeMap<T> {
8787
self.v.iter_mut().map(|elem| &mut elem.data)
8888
}
8989

90+
pub fn iter_all(&self) -> impl Iterator<Item = (ops::Range<u64>, &T)> {
91+
self.v.iter().map(|elem| (elem.range.clone(), &elem.data))
92+
}
93+
9094
// Splits the element situated at the given `index`, such that the 2nd one starts at offset
9195
// `split_offset`. Do nothing if the element already starts there.
9296
// Returns whether a split was necessary.

src/shims/foreign_items.rs

+13
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,19 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
363363
// shim, add it to the corresponding submodule.
364364
match link_name.as_str() {
365365
// Miri-specific extern functions
366+
"miri_get_alloc_id" => {
367+
let [ptr] = this.check_shim(abi, Abi::Rust, link_name, args)?;
368+
let ptr = this.read_pointer(ptr)?;
369+
let (alloc_id, _, _) = this.ptr_get_alloc_id(ptr)?;
370+
this.write_scalar(Scalar::from_u64(alloc_id.0.get()), dest)?;
371+
}
372+
"miri_print_stacks" => {
373+
let [id] = this.check_shim(abi, Abi::Rust, link_name, args)?;
374+
let id = this.read_scalar(id)?.to_u64()?;
375+
if let Some(id) = std::num::NonZeroU64::new(id) {
376+
this.print_stacks(AllocId(id))?;
377+
}
378+
}
366379
"miri_static_root" => {
367380
let [ptr] = this.check_shim(abi, Abi::Rust, link_name, args)?;
368381
let ptr = this.read_pointer(ptr)?;

src/stacked_borrows.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1105,4 +1105,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
11051105
}
11061106
}
11071107
}
1108+
1109+
fn print_stacks(&mut self, alloc_id: AllocId) -> InterpResult<'tcx> {
1110+
let this = self.eval_context_mut();
1111+
let alloc_extra = this.get_alloc_extra(alloc_id)?;
1112+
let stacks = alloc_extra.stacked_borrows.as_ref().unwrap().borrow();
1113+
for (range, stack) in stacks.stacks.iter_all() {
1114+
print!("{:?}:", range);
1115+
for i in 0..stack.len() {
1116+
print!(" {:?}", stack.get(i).unwrap());
1117+
}
1118+
println!("");
1119+
}
1120+
Ok(())
1121+
}
11081122
}

0 commit comments

Comments
 (0)