title: Get list of source files (and locations) from binary
body: <p>I'm working in a linux distro. I'm writing a C/C++ program that requires a list of source files that a binary (executable) was compiled with. I compiled the binary using GCC with the -g flag, of course. Using <strong>gdb</strong> I found out the format of the binary is DWARF2:</p>
  
  <pre><code>(gdb) info source
  Current source file is src/main.cpp
  Compilation directory is /path/to/source
  Located in /path/to/source/src/main.cpp 
  Contains 43 lines.
  Source language is c++.
  Compiled with DWARF 2 debugging format.
  Does not include preprocessor macro info.
  </code></pre>
  
  <p>Using <strong>objdump</strong> or <strong>elfread</strong> I see the information that I need:</p>
  
  <pre><code>bash&gt; objdump -W binary
  ...
  The File Name Table:
    Entry Dir Time    Size    Name
    1 1   0   0   main.cpp
    2 2   0   0   curses.h
    3 3   0   0   tprint.h
    4 3   0   0   twindow.h
    5 4   0   0   locale.h
  ...
  </code></pre>
  
  <p>Using <strong>dwarfdump</strong> I see that the variables of interest are: <strong>DW_AT_comp_dir</strong> and <strong>DW_AT_decl_file</strong>. Using a simple bash script (a few <em>grep</em> and <em>sed</em> calls) I was able to get the source list. What I would like to do, is get this source list from within a C/C++ program. For this purpose I have installed <strong>libdwarf</strong>, but with lack of any usage examples I'm unable to easily implement what I want.</p>
  
  <p>My questions are:
  1) Could someone provide a C/C++ example that reads debug information from a binary? It does not have to be with libdwarf if there are other libraries that can do this.
  2) Can gdb provide a source list? Eventually I would like to build my own interface to gdb and scroll though available source files.</p>
  
  <p>Regards</p>