Reputation: 305
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.
Upvotes: 17
Views: 895
Reputation: 8260
See DebugInfo.h for the mappings from the C++ LLVM debug info APIs to the C bindings. Examples that you'll need are:
(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):
DIBuilder * m_dwBuilder{};
LLVMContext m_context;
DISubprogram *dwFunc{};
Value *r{};
auto dwVar_gr = m_dwBuilder->createAutoVariable(...);
BasicBlock *fooBB = BasicBlock::Create( m_context, "", fooFunc );
...
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().
Upvotes: 3