rust/src/libcore/stackwalk.rs

109 lines
2.4 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[doc(hidden)]; // FIXME #3538
2012-06-12 00:44:55 -05:00
#[legacy_modes]; // tjc: remove after snapshot
2012-09-20 19:29:20 -05:00
// NB: transitionary, de-mode-ing.
// FIXME #4425: Can't forbid this because frame_address needs a deprecated
// mode.
2012-10-05 13:53:09 -05:00
#[allow(deprecated_mode)];
2012-09-20 19:29:20 -05:00
#[forbid(deprecated_pattern)];
2012-09-18 19:34:08 -05:00
use cast::reinterpret_cast;
2012-09-04 13:12:17 -05:00
use ptr::offset;
use sys::size_of;
2012-06-12 00:44:55 -05:00
pub type Word = uint;
2012-06-05 20:47:18 -05:00
pub struct Frame {
2012-09-06 21:40:15 -05:00
fp: *Word
2012-09-04 17:23:28 -05:00
}
2012-06-05 20:47:18 -05:00
pub fn Frame(fp: *Word) -> Frame {
2012-09-04 17:23:28 -05:00
Frame {
fp: fp
2012-06-05 20:47:18 -05:00
}
}
pub fn walk_stack(visit: fn(Frame) -> bool) {
2012-06-12 00:44:55 -05:00
2012-08-22 19:24:52 -05:00
debug!("beginning stack walk");
2012-06-12 00:44:55 -05:00
2012-06-30 18:19:07 -05:00
do frame_address |frame_pointer| {
let mut frame_address: *Word = unsafe {
2012-08-29 18:00:36 -05:00
reinterpret_cast(&frame_pointer)
2012-06-05 20:47:18 -05:00
};
2012-06-12 00:44:55 -05:00
loop {
let fr = Frame(frame_address);
2012-06-12 00:44:55 -05:00
2012-08-29 18:00:36 -05:00
debug!("frame: %x", unsafe { reinterpret_cast(&fr.fp) });
visit(fr);
2012-06-12 00:44:55 -05:00
unsafe {
2012-08-29 18:00:36 -05:00
let next_fp: **Word = reinterpret_cast(&frame_address);
2012-06-12 00:44:55 -05:00
frame_address = *next_fp;
if *frame_address == 0u {
2012-08-22 19:24:52 -05:00
debug!("encountered task_start_wrapper. ending walk");
2012-06-12 00:44:55 -05:00
// This is the task_start_wrapper_frame. There is
// no stack beneath it and it is a foreign frame.
2012-06-12 00:44:55 -05:00
break;
}
}
}
2012-06-05 20:47:18 -05:00
}
}
#[test]
2012-06-12 00:44:55 -05:00
fn test_simple() {
2012-06-30 18:19:07 -05:00
for walk_stack |_frame| {
2012-06-05 20:47:18 -05:00
}
}
2012-06-12 00:44:55 -05:00
#[test]
fn test_simple_deep() {
fn run(i: int) {
2012-08-01 19:30:05 -05:00
if i == 0 { return }
2012-06-12 00:44:55 -05:00
2012-06-30 18:19:07 -05:00
for walk_stack |_frame| {
2012-06-12 00:44:55 -05:00
unsafe {
breakpoint();
}
}
run(i - 1);
}
run(10);
}
2012-06-05 20:47:18 -05:00
fn breakpoint() {
unsafe {
rustrt::rust_dbg_breakpoint()
}
2012-06-05 20:47:18 -05:00
}
fn frame_address(f: fn(++x: *u8)) {
unsafe {
rusti::frame_address(f)
}
2012-06-05 20:47:18 -05:00
}
extern mod rustrt {
#[legacy_exports];
unsafe fn rust_dbg_breakpoint();
2012-06-05 20:47:18 -05:00
}
#[abi = "rust-intrinsic"]
extern mod rusti {
#[legacy_exports];
fn frame_address(f: &once fn(++x: *u8));
2012-06-05 20:47:18 -05:00
}