Is there a way to get a stack trace with a kill signal like with Go?


#1

With Go we can use kill -ABRT <pid> to kill a process and get a stack trace. It’s useful to debug hangs. Is there something similar with Rust? With a crate maybe.


#2

You can use gdb to attach to already running process in order to debug it.

~ $ rust-gdb -p [process pid]
...
0x000055995850fd36 in infinite_loop::main () at main.rs:3
3           loop {}
(gdb) bt
#0  0x0000564c5292ad36 in infinite_loop::main () at main.rs:3
#1  0x0000564c5293da2b in panic_unwind::__rust_maybe_catch_panic () at /checkout/src/libpanic_unwind/lib.rs:98
#2  0x0000564c52936569 in std::panicking::try<(),closure> () at /checkout/src/libstd/panicking.rs:433
#3  std::panic::catch_unwind<closure,()> () at /checkout/src/libstd/panic.rs:361
#4  std::rt::lang_start () at /checkout/src/libstd/rt.rs:56
#5  0x0000564c5292ad73 in main ()
(gdb)

#3

https://crates.io/crates/backtrace


#4

Do you know if I can have a backtrace of all the running threads with that?


#5

It will not do it for you, though you could probably make a crate which uses backtrace and ptrace to get backtraces for all threads.


#6

Servo automatically produces a backtace for the current thread when a segfault occurs: https://github.com/servo/servo/blob/f03ddf6c6c6e94e799ab2a3a89660aea4a01da6f/ports/servo/main.rs#L58-L79


#7

You can also use lldb:

(lldb) target stop-hook add
Enter your stop hook command(s). Type ‘DONE’ to end.

bt
disassemble --pc
DONE
Stop hook #1 added.

from http://lldb.llvm.org/lldb-gdb.html


#8

Considering how awesome the documentation community of Rust is; I would love to see some docs of how to use gdb, valgrind, and/or other-perf-tools for Rust development.

Maybe advanced debugging could be a new chapter of the Rust Book?