title: c++ : convert symbols to code line numbers programmatically
body: <p>I'm developing under <code>Linux/gcc</code></p>
  
  <p>I currently use the following to get a stack trace on custom thrown exceptions. <code>Demangled</code> functions names and <code>line numbers</code> are as expected, but I would like to avoid the use of <code>addr2line</code> to have a full control on the formatting of the output strings.</p>
  
  <pre><code>static void posix_print_stack_trace()
  {
      int i, trace_size = 0;
      char **messages = (char **)NULL;
  
      trace_size = backtrace(stack_traces, MAX_STACK_FRAMES);
      messages = backtrace_symbols(stack_traces, trace_size);
  
      for (i = 0; i &lt; trace_size; ++i)
      {
          if (addr2line(program_invocation_name, stack_traces[i]) != 0)
          {
              printf("  error determining line # for: %s\n", messages[i]);
          }
      }
      if (messages) { free(messages); }
  }
  
  static int addr2line(char const * const program_name, void const * const addr)
  {
      char addr2line_cmd[512] = {0}; 
      sprintf(addr2line_cmd,"addr2line -C -f -p -i -e %.256s %p", program_name, addr);
      return system(addr2line_cmd);
  }
  </code></pre>
  
  <p><strong>Note :</strong> The use of <code>-f</code> for displaying the functions names in play in the stack trace and <code>-C</code> to display them demangled.</p>
  
  <p><strong>Q :</strong> Does anyone could point me on a programmatic solution ? 
  (And if possibly give me some advices on how to get it working as well with <code>MinGW/gcc</code>).</p>
  
  <p>NB : Or may be simply using <code>gdb</code> in some way could help in getting more customized output ?</p>
  
  <p>Thanks for the help.</p>
  
  <p><strong>EDIT</strong> : It looks like for the windows part, it is doable that way : <a href="https://stackoverflow.com/a/6207030/1715716">https://stackoverflow.com/a/6207030/1715716</a></p>
  
  <p><strong>EDIT</strong> : The above points to a Microsoft Visual only solution, so is finally useless to me.</p>