Skip to content

Commit 95db1de

Browse files
authored
Merge pull request #1005 from NordSecurity/print-backtrace-on-panic
Print backtrace on panic
2 parents 96eb1bb + 3918505 commit 95db1de

File tree

7 files changed

+41
-3
lines changed

7 files changed

+41
-3
lines changed

.unreleased/print-backtrace-on-panic

Whitespace-only changes.

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ debug = "full"
208208
[profile.dev]
209209
debug = 0
210210
strip = "debuginfo"
211-
overflow-checks = false
212211

213212
#TODO: remove this when anyone starts using telio-starcast
214213
[workspace.metadata.cargo-udeps.ignore]

crates/telio-utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ publish = false
1010
sn_fake_clock = ["dep:sn_fake_clock"]
1111

1212
[dependencies]
13+
backtrace = "0.3.74"
1314
blake3 = "1.5.4"
1415
futures.workspace = true
1516
hashlink.workspace = true

crates/telio-utils/src/backtrace.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::telio_log_debug;
2+
use backtrace;
3+
4+
/// Print to debug log current backtrace
5+
pub fn log_current_backtrace() {
6+
let mut counter = 0;
7+
backtrace::trace(|frame| {
8+
let ip = frame.ip();
9+
let symbol_address = frame.symbol_address();
10+
11+
// Resolve this instruction pointer to a symbol name
12+
backtrace::resolve_frame(frame, |symbol| {
13+
let name = symbol
14+
.name()
15+
.map(|symbol| symbol.to_string())
16+
.unwrap_or_else(|| format!("{symbol_address:?}"));
17+
18+
let filename = symbol
19+
.filename()
20+
.map(|filename| filename.to_string_lossy().into_owned())
21+
.unwrap_or_else(|| "uknown.file".to_owned());
22+
23+
let lineno = symbol.lineno().unwrap_or(0);
24+
25+
telio_log_debug!("backtrace: {counter:3} {ip:?} {filename}:{lineno} {name}");
26+
counter += 1;
27+
});
28+
29+
true
30+
});
31+
}

crates/telio-utils/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ pub mod const_ipnet;
5959

6060
/// Log censoring via postprocessing utilities
6161
pub mod log_censor;
62+
63+
/// Utilities for working with backtraces/stacktraces/callstacks
64+
pub mod backtrace;

src/ffi.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ use nat_detect::NatType;
3333

3434
// debug tools
3535
use telio_utils::{
36-
commit_sha, telio_log_debug, telio_log_error, telio_log_info, telio_log_warn, version_tag,
36+
backtrace::log_current_backtrace, commit_sha, telio_log_debug, telio_log_error, telio_log_info,
37+
telio_log_warn, version_tag,
3738
};
3839

3940
const DEFAULT_PANIC_MSG: &str = "libtelio panicked";
@@ -211,7 +212,9 @@ impl Telio {
211212
let events = panic_event_dispatcher;
212213
panic::set_hook(Box::new(move |info| {
213214
// We need it on the logs as well ...
214-
error!("{}", info);
215+
telio_log_error!("{}", info);
216+
217+
log_current_backtrace();
215218

216219
let err = {
217220
let message = {

0 commit comments

Comments
 (0)