17

I'm currently toying around with a simple LLVM frontend written in Rust. I'm now trying to emit debug information.

How can I emit this debug information (source locations and variables) through the C bindings? Is it even possible? Do I need to write a C++ wrapper?

There seems to be a function for inserting source locations (LLVMSetCurrentDebugLocation; LLVM; Rust), but I don't know how to construct a proper LLVMValue containing this information. I guess it needs some kind of metadata.

dotjpg3141
  • 305
  • 1
  • 10
  • 1
    I would ask that on some `llvm` mailing list, perhaps [llvm-dev](http://lists.llvm.org/mailman/listinfo/llvm-dev) – Basile Starynkevitch Jul 06 '18 at 19:29
  • I use vim as my IDE with a plugin called YouCompleteMe. That plugin provides debug information using LLVM but is not written in Rust. Maybe looking at that code will help? The plugin's author is also very responsive and his code is on github. – Jason Enochs Jul 06 '18 at 20:15
  • 2
    You can also try looking at the Rust compiler's debuginfo generation; I'm not sure if we use the C bindings or not though: https://github.com/rust-lang/rust/blob/master/src/librustc_codegen_llvm/debuginfo/mod.rs. – Mark Rousskov Jul 07 '18 at 14:55
  • 1
    Please share the answer if you manage to find it! – Carson Harmon Sep 25 '18 at 19:27

1 Answers1

3

See DebugInfo.h for the mappings from the C++ LLVM debug info APIs to the C bindings. Examples that you'll need are:

  • new DIBuilder -> LLVMCreateDIBuilder()
  • DIBuilder::createFile() -> LLVMDIBuilderCreateFile()
  • DIBuilder::createCompileUnit() -> LLVMDIBuilderCreateCompileUnit()
  • DIBuilder::createBasicType() -> LLVMDIBuilderCreateBasicType()

(use those functions to setup the dwarf context for your compiler)

The LLVMSetCurrentDebugLocation() function you mentioned is the equivalent of IRBuilder<>::SetCurrentDebugLocation()

For each debug expression, you want a debug location, and DWARF metadata for the expression. That's done like the following (C++ fragment):

auto loc_glc = DebugLoc::get( line, column, dwFunc );
m_dwBuilder->insertDeclare( r, dwVar_gr, m_dwBuilder->createExpression(), loc_glc, fooBB );
m_builder.SetCurrentDebugLocation( loc_glc );

you'll want to associate the debug location with the DWARF expression, and then "anchor" that to your IRBuilder using LLVMSetCurrentDebugLocation().

Peeter Joot
  • 7,848
  • 7
  • 48
  • 82