[llvm-dev] [DebugInfo] DWARF C API

Son Tuan VU via llvm-dev llvm-dev at lists.llvm.org
Wed Apr 24 13:56:21 PDT 2019


Hi folks,

I am trying to implement the C bindings API for DebugInfo::DWARF::DWARFDie.
My goal is to have a C library that reads and parses DWARF debugging format
(just like how llvm-dwarfdump does, but maybe more than just dumping debug
info)

I've started with creating C structure for DebugInfo::DWARF::DWARFContext
which contains all DWARF DIEs in the object file. For this I used
```
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DWARFContext, LLVMDWARFContextRef)
```
then defined some C functions
```
LLVMDWARFContextRef LLVMCreateDWARFContext(LLVMBinaryRef Bin) {
  std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(*unwrap(Bin));
  return wrap(DICtx.release());
}

void LLVMPrintDWARFContext(LLVMDWARFContextRef C) {
  DIDumpOptions DumpOpts;
  DumpOpts.DumpType = DIDT_DebugInfo; // I only care about the .debug_info
section
  unwrap(C)->dump(outs(), DumpOpts);
}
```
However, I got a segfault when trying to dump the LLVMDWARFContextRef using
the second function. More precisely, it segfaulted when dumping the
attribute of the DWARFDie.

I tried to do this instead to see if the wrapping/unwrapping has bugs
```
LLVMDWARFContextRef LLVMCreateDWARFContext(LLVMBinaryRef Bin) {
  std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(*unwrap(Bin));
  LLVMDWARFContextRef C = wrap(DICtx.release());
  LLVMPrintDWARFContext(C);
  return C;
}
```
and the call to LLVMPrintDWARFContext did not segfault this time.

Can anybody who knows about DWARFContext and DWARFDie tell me what I did
wrong? Or is it a bug in DWARFDie::dump() method?

Thank you very much for your help,

Son Tuan Vu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190424/7b58b277/attachment.html>


More information about the llvm-dev mailing list