rust/src/libcore/stackwalk.rs
Niko Matsakis 6965fe4bce Add AbiSet and integrate it into the AST.
I believe this patch incorporates all expected syntax changes from extern
function reform (#3678). You can now write things like:

    extern "<abi>" fn foo(s: S) -> T { ... }
    extern "<abi>" mod { ... }
    extern "<abi>" fn(S) -> T

The ABI for foreign functions is taken from this syntax (rather than from an
annotation).  We support the full ABI specification I described on the mailing
list.  The correct ABI is chosen based on the target architecture.

Calls by pointer to C functions are not yet supported, and the Rust type of
crust fns is still *u8.
2013-03-29 18:36:20 -07:00

102 lines
2.2 KiB
Rust

// 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
use cast::reinterpret_cast;
pub type Word = uint;
pub struct Frame {
fp: *Word
}
pub fn Frame(fp: *Word) -> Frame {
Frame {
fp: fp
}
}
pub fn walk_stack(visit: &fn(Frame) -> bool) {
debug!("beginning stack walk");
do frame_address |frame_pointer| {
let mut frame_address: *Word = unsafe {
reinterpret_cast(&frame_pointer)
};
loop {
let fr = Frame(frame_address);
debug!("frame: %x", unsafe { reinterpret_cast(&fr.fp) });
visit(fr);
unsafe {
let next_fp: **Word = reinterpret_cast(&frame_address);
frame_address = *next_fp;
if *frame_address == 0u {
debug!("encountered task_start_wrapper. ending walk");
// This is the task_start_wrapper_frame. There is
// no stack beneath it and it is a foreign frame.
break;
}
}
}
}
}
#[test]
fn test_simple() {
for walk_stack |_frame| {
}
}
#[test]
fn test_simple_deep() {
fn run(i: int) {
if i == 0 { return }
for walk_stack |_frame| {
unsafe {
breakpoint();
}
}
run(i - 1);
}
run(10);
}
fn breakpoint() {
unsafe {
rustrt::rust_dbg_breakpoint()
}
}
fn frame_address(f: &fn(x: *u8)) {
unsafe {
rusti::frame_address(f)
}
}
pub mod rustrt {
pub extern {
pub unsafe fn rust_dbg_breakpoint();
}
}
pub mod rusti {
#[abi = "rust-intrinsic"]
pub extern "rust-intrinsic" {
pub fn frame_address(f: &once fn(x: *u8));
}
}