Stream: cranelift

Topic: Creating Rust-style backtraces with llvm-libunwind


view this post on Zulip Floppy (Oct 21 2024 at 10:23):

I'm trying to generate Rust-like backtraces (a list of function symbols is enough for now, no line number) with libunwind. However; whenever I try to fetch symbol names with unw_get_proc_name i get the general unspecified error error code. I suspect this is because I'm not generating unwind information correctly from cranelift. However; I cannot figure out where the problem sits.

For now I copied Rustc's cranelift backends implementation for adding unwind information and then add it after defining each function . Then in my language's standard library I invoke the libunwind functions which I think are meant to retrieve that information. After copying Rustc's libunwind code I no longer get the missing unwind information error code but now instead just always get EUNSPEC (unspecified error).

Strangely, when I try calling the unw_get_proc_name function the same way in Rust (on the musl target which has the unw_* symbols) I get exactly the same EUNSPEC error. https://github.com/luminalang/lumina/blob/libunwind/rust-replication/src/main.rs so this problem might not even be related to how I use cranelift. Or; Rust doesn't generate this unwind information either and does something completely different to create its backtraces?

Regardless; I'm very lost in what I'm supposed to do to achieve this goal and what I'm doing wrong in my attempts to do so.

Lumina is an eager-by-default natively compiled functional programming language with the core goals of readibility, practicality, compiler-driven development and simplicity. - luminalang/lumina
Lumina is an eager-by-default natively compiled functional programming language with the core goals of readibility, practicality, compiler-driven development and simplicity. - luminalang/lumina
Lumina is an eager-by-default natively compiled functional programming language with the core goals of readibility, practicality, compiler-driven development and simplicity. - luminalang/lumina
Lumina is an eager-by-default natively compiled functional programming language with the core goals of readibility, practicality, compiler-driven development and simplicity. - luminalang/lumina

view this post on Zulip bjorn3 (Oct 21 2024 at 11:13):

Rust uses the _Unwind_* family of functions to generate backtraces. llvm-libunwind supports this too. I have no idea how the unw_* family of functions is supposed to be used.

view this post on Zulip bjorn3 (Oct 21 2024 at 11:14):

Also for translating function addresses to human readable function names rust uses it's own debuginfo parsing code through the addr2line crate.

view this post on Zulip Floppy (Oct 21 2024 at 11:51):

hm ok. llvm-libunwind seems to use dladdr to get the symbol names which is probably very limited. Seems like parsing the dwarf debuginfo manually after unwinding, using the addresses given by libunwind would be the way to go. I'll look whether writing the minimal dwarf parsing code I'd need would be simple enough, if not I could just depend on addr2line as a hack for now.


Last updated: Oct 23 2024 at 20:03 UTC