cpptrace: JIT compiled frame symbols are missing
Jeremy! This project is great. I’m trying to add it to jank right now, as the fallback exception handler.
I have a fun twist for you. jank is a native Clojure dialect on LLVM, so I’m generating LLVM IR and then giving it to LLVM to JIT compile. It looks like cpptrace is unable to find the symbols for any JIT compiled functions.
However, given the same exact executable and flags, gdb can see the symbols. I’ve asked an LLVM why and I’ve provided the info below.
Details
CMake
-- Cpptrace auto config: Using libgcc unwind for unwinding
-- Cpptrace auto config: Using libdwarf for symbols
-- Cpptrace auto config: Using cxxabi for demangling
OS
Linux x86_64
Executable
❯ file build/jank
build/jank: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /nix/store/65h17wjrrlsj2rj540igylrx7fqcd6vq-glibc-2.40-36/lib/ld-linux-x86-64.so.2, for GNU/Linux 3.10.0, with debug_info, not stripped
What gdb/lldb are doing
I asked Lang Hames (the main LLVM JIT guy) on the LLVM Discord about this and here’s what he said.
gdb and lldb are getting the dwarf through https://sourceware.org/gdb/current/onlinedocs/gdb.html/JIT-Interface.html. That interface is pretty specialized: ORC adds a “debug object” (an object file containing debug info, and with the section addresses in the header updated to reflect their loaded addresses in the executing process) to a linked list headed by a global symbol, then it calls a special registration function. This is where it gets weird: The body of the registration function is a no-op, but GDB and LLDB know to set a breakpoint on that function if they see it in a process, and they use a breakpoint handler to react to the call, reading any new objects from the global linked list of debug objects. If cpptrace has an API that we can use to register dynamically loaded DWARF then I think we’d be better off adding a custom plugin to call that API.
What do you think? I’m hoping to get this working for both Linux and macOS (x86_64 and aarch64).
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 28 (28 by maintainers)
Commits related to this issue
- Add preliminary JIT support (#230) Resolves #226  — committed to jeremy-rifkin/cpptrace by jeremy-rifkin 2 years ago
- Manually register JIT frames with cpptrace Related to: https://github.com/jeremy-rifkin/cpptrace/issues/226 — committed to jank-lang/jank by jeaye 2 years ago
👍 👍
This is what we’re going with for now! I don’t see this code on
main. Will you please ping me once it’s available/merged and then I’ll update our cpptrace fork?Thank you, Jeremy! You’re the man.
Niiiiice!
Will this work with the existing LLVM 19 binaries, or does it involve LLVM changes for 20 or later?
My pleasure, thanks for the interest in the library! 😃
I didn’t have a ton of time to look into this today but exploring things a bit:
I instrumented cpptrace to walk the
__jit_debug_descriptorlinked list and dump out the in-memory object files. I didn’t find any debug symbols in these withdwarfdumpbutreadelfshowed they have.symtabsections. Just to confirm, is it expected jank isn’t emitting any dwarf debug symbols (which for cpptrace would mainly be used for line numbers) and instead it’s just function names from the symtab we’d expect to see? (this does seem to match what gdb is able to provide)Typing up some notes, mainly for myself but also if you have any thoughts I’d be happy to hear:
Looking at the missing frame 5 which is a jit function
This frame has raw address
0x7f33aed44090This seems to correspond to the .text section of the second object file to be registered via the jit interface
0x7f33aed44090 - 0x7f33aed44000is0x90So this falls within the
test_foo_1_0symbol, which is what we expect. Awesome 😄cpptrace::API in addition to the semi-standard JIT interface__jit_debug_descriptor__jit_debug_register_codeand only walk the linked list thencpptrace::load_jit_objects()to be called any time jit stuff changes so that cpptrace can avoid a possibly-fickle reliance on caching by memory location.__jit_debug_descriptorcomes down to two options:extern struct jit_descriptor __jit_debug_descriptor;works great for accessing the jit code entries, but, since not all programs will have this symbol it would require some compile-time configuration to turn on/off jit mode. And then if linking against jit mode cpptrace this symbol would have to be defined.dlsymit, but this might have problems of its own since it’d require the symbol to be a dynamic exportPath forward
If jank isn’t emitting dwarf symbols here what I’ll plan on doing is just setting up a preliminary interface for cpptrace and basic support for scanning in-memory symbol tables, and then dwarf support can hopefully be an easy addition later.
TL;DR: I think this may be easier than I feared and I’m hoping to have something this weekend
Niiice! Great job. Thanks again for taking the time for this, Jeremy. 🙂 I owe you one.
My man! Not much overlap of C++ and Clojure folks. 😃
If this is something you’re open to tackling, I’d rather leave this one to you. I’m happy to help test and I can also provide some guidance for how you can test this with some actual JIT compiled code.
If you ultimately don’t have the resources, I can look into getting someone from the jank community to help out.