Skip to content

Commit

Permalink
Print backtrace when panic happens
Browse files Browse the repository at this point in the history
Backtrace is printed on the debug level
  • Loading branch information
tomaszklak committed Dec 2, 2024
1 parent fa41cb4 commit d6a85e3
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 2 deletions.
Empty file.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/telio-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ publish = false
sn_fake_clock = ["dep:sn_fake_clock"]

[dependencies]
backtrace = "0.3.74"
blake3 = "1.5.4"
futures.workspace = true
hashlink.workspace = true
Expand Down
31 changes: 31 additions & 0 deletions crates/telio-utils/src/backtrace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::telio_log_debug;
use backtrace;

/// Print to debug log current backtrace
pub fn log_current_backtrace() {
let mut counter = 0;
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| {
let name = symbol
.name()
.map(|symbol| symbol.to_string())
.unwrap_or_else(|| format!("{symbol_address:?}"));

let filename = symbol
.filename()
.map(|filename| filename.to_string_lossy().into_owned())
.unwrap_or_else(|| "uknown.file".to_owned());

let lineno = symbol.lineno().unwrap_or(0);

telio_log_debug!("backtrace: {counter:3} {ip:?} {filename}:{lineno} {name}");
counter += 1;
});

true
});
}
3 changes: 3 additions & 0 deletions crates/telio-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ pub mod const_ipnet;

/// Log censoring via postprocessing utilities
pub mod log_censor;

/// Utilities for working with backtraces/stacktraces/callstacks
pub mod backtrace;
7 changes: 5 additions & 2 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ use nat_detect::NatType;

// debug tools
use telio_utils::{
commit_sha, telio_log_debug, telio_log_error, telio_log_info, telio_log_warn, version_tag,
backtrace::log_current_backtrace, commit_sha, telio_log_debug, telio_log_error, telio_log_info,
telio_log_warn, version_tag,
};

const DEFAULT_PANIC_MSG: &str = "libtelio panicked";
Expand Down Expand Up @@ -211,7 +212,9 @@ impl Telio {
let events = panic_event_dispatcher;
panic::set_hook(Box::new(move |info| {
// We need it on the logs as well ...
error!("{}", info);
telio_log_error!("{}", info);

log_current_backtrace();

let err = {
let message = {
Expand Down

0 comments on commit d6a85e3

Please sign in to comment.