Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emit debug information through LLVMs C bindings?

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.

like image 585
dotjpg3141 Avatar asked Dec 19 '17 19:12

dotjpg3141


1 Answers

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().

like image 96
Peeter Joot Avatar answered Oct 30 '22 14:10

Peeter Joot