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

Most upvoted comments

A janky solution seems to work for now is to do the following after calls to ee.initialize/ee.addObjectFile:

if(auto* entry = cpptrace::detail::__jit_debug_descriptor.relevant_entry) { cpptrace::register_jit_object(entry->symfile_addr, entry->symfile_size); }

I tested with adding this at the end of processor::load_object and processor::load_ir_module.

👍 👍

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!

Image

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_descriptor linked list and dump out the in-memory object files. I didn’t find any debug symbols in these with dwarfdump but readelf showed they have .symtab sections. 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

#4  in jank_throw at c_api.cpp:876
#5  
#6  in jank::runtime::obj::jit_function::call at jit_function.cpp:71

This frame has raw address 0x7f33aed44090

This seems to correspond to the .text section of the second object file to be registered via the jit interface

obj_1.o:        file format elf64-x86-64

Sections:
Idx Name            Size     VMA              Type
  0                 00000000 0000000000000000 
  1 .strtab         00000187 0000000000000000 
  2 .text           000000ac 00007f33aed44000 TEXT
  3 .rela.text      00000198 0000000000000000 
  4 .bss            00000018 0000000000000000 BSS
  5 .rodata.str1.1  00000043 00007f33aed42000 DATA
  6 .note.GNU-stack 00000000 0000000000000000 
  7 .eh_frame       00000078 00007f33aed42048 
  8 .rela.eh_frame  00000060 0000000000000000 
  9 .symtab         000001e0 0000000000000000 

0x7f33aed44090 - 0x7f33aed44000 is 0x90

$ readelf --symbols obj_1.o

Symbol table '.symtab' contains 20 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS test-test$test_f[...]
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    2 .text
     3: 0000000000000000     5 OBJECT  LOCAL  DEFAULT    5 .L__unnamed_1
     4: 0000000000000000     8 OBJECT  LOCAL  DEFAULT    4 string_2025564121
     5: 0000000000000005    62 OBJECT  LOCAL  DEFAULT    5 .L__unnamed_2
     6: 0000000000000010     8 OBJECT  LOCAL  DEFAULT    4 string_1142059953
     7: 0000000000000008     8 OBJECT  LOCAL  DEFAULT    4 data_1437529847
     8: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 .bss
     9: 0000000000000000    60 FUNC    GLOBAL DEFAULT    2 test_jank_global[...]
    10: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND jank_string_create
    11: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND jank_read_string
    12: 0000000000000040    62 FUNC    GLOBAL DEFAULT    2 test_repl_fn_4_0
    13: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND jank_function_bu[...]
    14: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND jank_function_create
    15: 0000000000000080    17 FUNC    GLOBAL DEFAULT    2 test_foo_1_0
    16: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND jank_function_se[...]
    17: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND jank_set_meta
    18: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND jank_throw
    19: 00000000000000a0    12 FUNC    GLOBAL HIDDEN     2 __orc_init_func.

So this falls within the test_foo_1_0 symbol, which is what we expect. Awesome 😄

  • Options for the cpptrace interface
    1. Require registration via some cpptrace:: API in addition to the semi-standard JIT interface
      • This might be a hassle with llvm since llvm is doing the JIT interface part and I don’t know if there’s an easy way for you to get the in-memory object file handle
    2. Have cpptrace walk the __jit_debug_descriptor
      • This would likely result in an O(n) walk every time a collection of frames is resolved but
      • cpptrace can cache object files here (though maybe there’s a risk of a memory location being reused if an in-memory object file is unloaded)
      • gdb/lldb avoid this by setting a breakpoints on __jit_debug_register_code and only walk the linked list then
        • I probably don’t want to try to instrument cpptrace to do a breakpoint of its own, and that might interfere with a debugger
        • It might be better to require a function like cpptrace::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.
  • Accessing __jit_debug_descriptor comes 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.
    • cpptrace could try to dlsym it, but this might have problems of its own since it’d require the symbol to be a dynamic export

Path forward

  • cpptrace does its own elf / mach-o parsing for symbol table stuff, these will need to be instrumented for in-memory object files in addition to reading from disk
    • (this may work well with another goal I’ve had in mind which is having the elf/mach-o parsers use mmap)
  • look into libdwarf stuff (and fortunately libdwarf does have support for in-memory object files)

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

Thanks! A different libstdc++ makes sense. I was able to get it to build with an ubuntu 24.10 container like you suggested.

I was also able to get the example to run and see the expected missing frames. Will start diving into the JIT interface stuff next.

Niiice! Great job. Thanks again for taking the time for this, Jeremy. 🙂 I owe you one.

As a side note, I’m a huge fan of Jank 😃

My man! Not much overlap of C++ and Clojure folks. 😃

I’d be happy to accept a PR if you’re interested or I might be able to take a look next week.

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.