Skip to content

Instantly share code, notes, and snippets.

@AtheMathmo
Created September 19, 2016 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AtheMathmo/c123fcb262927c3e264d42545feeb7be to your computer and use it in GitHub Desktop.
Save AtheMathmo/c123fcb262927c3e264d42545feeb7be to your computer and use it in GitHub Desktop.
extern crate futures;
extern crate backtrace;
extern crate tokio_timer;
extern crate nix;
extern crate libc;
pub mod timeout;
use tokio_timer::Timer;
use futures::Future;
use std::time::{Duration, Instant};
use std::thread;
use nix::sys::signal;
/// Here is our handler
///
/// Right now it just prints the backtrace up to a max of 10 items
extern "C" fn handle_sigprof(_: i32) {
let mut i = 0;
backtrace::trace(|frame| {
let ip = frame.ip();
let mut not_done = true;
backtrace::resolve(ip, |symbol| {
if let Some(name) = symbol.name() {
println!("Symbol {0}: {1:?}", i, name);
} else {
not_done = false;
}
});
i += 1;
not_done
});
}
fn main() {
// Create handler for SIGPROF
let handler = signal::SigHandler::Handler(handle_sigprof);
let flags = signal::SA_RESTART | signal::SA_SIGINFO;
let sig_action =
signal::SigAction::new(handler, flags, signal::SigSet::empty());
// Register our handler for SIGPROF
unsafe {
signal::sigaction(signal::SIGPROF, &sig_action).unwrap();
}
// This function will raise the SIGPROF signal every 2 seconds
fn recurse(_: ()) -> Box<Future<Item = (), Error = tokio_timer::TimerError> + Send> {
signal::raise(signal::SIGPROF).unwrap();
Timer::default().sleep(Duration::new(2, 0)).and_then(recurse).boxed()
}
// Spawn a new thread for the signal raising
thread::spawn(|| {
recurse(()).wait().unwrap();
});
// Do some work for 10 seconds
let then = Instant::now();
loop {
let now = Instant::now();
if now.duration_since(then) > Duration::new(10, 0) {
break;
}
}
println!("Out...");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment