How to emit debug information through LLVMs C bindings?
Asked Answered
S

1

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.

Sorbian answered 19/12, 2017 at 19:1 Comment(4)
I would ask that on some llvm mailing list, perhaps llvm-devSavina
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.Insalubrious
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: github.com/rust-lang/rust/blob/master/src/librustc_codegen_llvm/….Kathrynkathryne
Please share the answer if you manage to find it!Bankruptcy
M
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().

Morrie answered 27/10, 2019 at 14:19 Comment(1)
In that example, what are dwFunc, r, dwVar_gr and fooBB? The rest of things you don't define can probably be guessed at from their names, but those are completely opaque. I've been trying to read my way through IR/DIBuilder.h, but it is tough going with essentially no documentation, try to guess which methods do what from their cryptic names.Rigsby

© 2022 - 2024 — McMap. All rights reserved.