title: How to make backtrace()/backtrace_symbols() print the function names?
body: <p>The Linux specific <code>backtrace()</code> and <code>backtrace_symbols()</code> allows you to produce a call trace of the program. However, it only prints function addresses, not their names for my program. How can I make them print the function names as well ? I've tried compiling the program with <code>-g</code> as well as <code>-ggdb</code>. The test case below just prints this:</p>
  
  <pre>
  
      BACKTRACE ------------
      ./a.out() [0x8048616]
      ./a.out() [0x8048623]
      /lib/libc.so.6(__libc_start_main+0xf3) [0x4a937413]
      ./a.out() [0x8048421]
      ----------------------
      </pre>
  
  <p>I'd want the first 2 items to also show the function names, <code>foo</code> and <code>main</code></p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;execinfo.h&gt;
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  
  static void full_write(int fd, const char *buf, size_t len)
  {
          while (len &gt; 0) {
                  ssize_t ret = write(fd, buf, len);
  
                  if ((ret == -1) &amp;&amp; (errno != EINTR))
                          break;
  
                  buf += (size_t) ret;
                  len -= (size_t) ret;
          }
  }
  
  void print_backtrace(void)
  {
          static const char start[] = "BACKTRACE ------------\n";
          static const char end[] = "----------------------\n";
  
          void *bt[1024];
          int bt_size;
          char **bt_syms;
          int i;
  
          bt_size = backtrace(bt, 1024);
          bt_syms = backtrace_symbols(bt, bt_size);
          full_write(STDERR_FILENO, start, strlen(start));
          for (i = 1; i &lt; bt_size; i++) {
                  size_t len = strlen(bt_syms[i]);
                  full_write(STDERR_FILENO, bt_syms[i], len);
                  full_write(STDERR_FILENO, "\n", 1);
          }
          full_write(STDERR_FILENO, end, strlen(end));
      free(bt_syms);
  }
  void foo()
  {
      print_backtrace();
  }
  
  int main()
  {
      foo();
      return 0;
  }
  </code></pre>