title: How to dump DWARF information into a file while llvm compiles?
body: <p>I'd like to know how I might insert some code while llvm compiles IR so that a DWARF information file can be generated.</p>
  
  <p>(I'm not asking for using objdump/llvm-dwarfdump tools after the binary is generated)</p>
  
  <p>Thx in advance.</p>
  
  <hr>
  
  <p>I tried Eli's answer and I got seg fault here, probably I misused it?</p>
  
  <pre><code>clang-3.7 -g -emit-llvm -c main.c -o build/main.bc
  clang-3.7 -g -emit-llvm -c sum.c  -o build/sum.bc
  llvm-link-3.7 build/main.bc build/sum.bc -o build/main.linked.bc
  llc-3.7 -split-dwarf Enable build/main.linked.bc -o build/main
  </code></pre>
  
  <p>main.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int sum(int x, int y);
  
  int main()
  {
      int r = sum(3, 4);
      printf("r = %d\n", r);
      return 0;
  }
  </code></pre>
  
  <p>sum.c:</p>
  
  <pre><code>int sum(int x, int y) {
      return x + y;
  }
  </code></pre>
  
  <p>Seg fault info:</p>
  
  <pre><code>0  libLLVM-3.7.so.1 0x00002b332cdf0af8 llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;) + 56
  1  libLLVM-3.7.so.1 0x00002b332cdeff59
  2  libc.so.6        0x00002b332e0bdd40
  3  libLLVM-3.7.so.1 0x00002b332c86d146 llvm::MCExpr::print(llvm::raw_ostream&amp;, llvm::MCAsmInfo const*) const + 134
  4  libLLVM-3.7.so.1 0x00002b332c86d243 llvm::MCExpr::print(llvm::raw_ostream&amp;, llvm::MCAsmInfo const*) const + 387
  5  libLLVM-3.7.so.1 0x00002b332c84e356
  6  libLLVM-3.7.so.1 0x00002b332c2f4927 llvm::AsmPrinter::EmitLabelPlusOffset(llvm::MCSymbol const*, unsigned long, unsigned int, bool) const + 87
  7  libLLVM-3.7.so.1 0x00002b332c2fe6df llvm::AsmPrinter::emitDwarfDIE(llvm::DIE const&amp;) const + 111
  8  libLLVM-3.7.so.1 0x00002b332c2fe713 llvm::AsmPrinter::emitDwarfDIE(llvm::DIE const&amp;) const + 163
  9  libLLVM-3.7.so.1 0x00002b332c325a29 llvm::DwarfFile::emitUnits(bool) + 105
  10 libLLVM-3.7.so.1 0x00002b332c323bf0 llvm::DwarfDebug::endModule() + 992
  11 libLLVM-3.7.so.1 0x00002b332c2fc251 llvm::AsmPrinter::doFinalization(llvm::Module&amp;) + 513
  12 libLLVM-3.7.so.1 0x00002b332c6424b5 llvm::FPPassManager::doFinalization(llvm::Module&amp;) + 69
  13 libLLVM-3.7.so.1 0x00002b332c64c09d llvm::legacy::PassManagerImpl::run(llvm::Module&amp;) + 797
  14 llc-3.7          0x0000000000413d1f
  15 llc-3.7          0x000000000040ea50 main + 384
  16 libc.so.6        0x00002b332e0a8ec5 __libc_start_main + 245
  17 llc-3.7          0x000000000040eaa9
  Stack dump:
  0.  Program arguments: llc-3.7 -split-dwarf Enable build/main.linked.bc -o build/main
  </code></pre>