#backtrace #trace #printing #stack #runtime

backtrace

A library to acquire a stack trace (backtrace) at runtime in a Rust program

58 releases

✓ Uses Rust 2018 edition

0.3.46 Mar 24, 2020
0.3.44 Feb 11, 2020
0.3.40 Oct 17, 2019
0.3.34 Jul 31, 2019
0.1.4 Jul 16, 2015
Download history 158169/week @ 2019-12-17 101434/week @ 2019-12-24 121332/week @ 2019-12-31 179554/week @ 2020-01-07 199467/week @ 2020-01-14 189576/week @ 2020-01-21 200697/week @ 2020-01-28 198720/week @ 2020-02-04 185960/week @ 2020-02-11 177857/week @ 2020-02-18 184903/week @ 2020-02-25 190317/week @ 2020-03-03 187704/week @ 2020-03-10 188618/week @ 2020-03-17 194619/week @ 2020-03-24 173653/week @ 2020-03-31

770,909 downloads per month
Used in 2,440 crates (130 directly)

MIT/Apache

195KB
4K SLoC

backtrace-rs

Documentation

A library for acquiring backtraces at runtime for Rust. This library aims to enhance the support of the standard library by providing a programmatic interface to work with, but it also supports simply easily printing the current backtrace like libstd's panics.

Install

[dependencies]
backtrace = "0.3"

Note that this crate requires cc and ar to be present on Unix systems when libbacktrace is used (which is the default). For configuring C compilers see the cc crate documentation.

Usage

To simply capture a backtrace and defer dealing with it until a later time, you can use the top-level Backtrace type.

extern crate backtrace;

use backtrace::Backtrace;

fn main() {
    let bt = Backtrace::new();

    // do_some_work();

    println!("{:?}", bt);
}

If, however, you'd like more raw access to the actual tracing functionality, you can use the trace and resolve functions directly.

extern crate backtrace;

fn main() {
    backtrace::trace(|frame| {
        let ip = frame.ip();
        let symbol_address = frame.symbol_address();

        // Resolve this instruction pointer to a symbol name
        backtrace::resolve_frame(frame, |symbol| {
            if let Some(name) = symbol.name() {
                // ...
            }
            if let Some(filename) = symbol.filename() {
                // ...
            }
        });

        true // keep going to the next frame
    });
}

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in backtrace-rs by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0–1.3MB
~27K SLoC