Debugging: Difference between revisions

From miki
Jump to navigation Jump to search
Line 333: Line 333:
printf("func symbol is '%s'\n",sym);
printf("func symbol is '%s'\n",sym);
free(buftofree);
free(buftofree);

return 0;
}
</source>|headerstyle=background:#ccccff;text-align:left;}} }}

A less efficient but more convenient version:
{{pl2|
{{hidden|<tt>main.c</tt>
([{{#filelink: main.c}} download])|{{#fileanchor: main.c}}
<source lang="c">
#include <stdio.h>
#include <execinfo.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

typedef void (*f_ptr)(void);


void callbackfunction(void)
{
printf("callbackfunction\n");
}

char * symfromaddr(void * fptr, char *sym, unsigned n)
{
char *beg,*end;
char ** strings;

strings=backtrace_symbols(&fptr, 1);
assert(strings!=NULL);

beg=end=strings[0];
while( '+' != (*end) )
{
if( '(' == *(end++) )
{
beg=end;
}
}
*end = 0;
strncpy(sym,beg,n);
if (n>0)
sym[n-1]=0;
free(strings);

return sym;
}


int main(int argc, char **argv) {
char sym[20];

printf("func symbol is '%s'\n",symfromaddr(callbackfunction,sym,sizeof(sym)));


return 0;
return 0;

Revision as of 21:31, 30 September 2011

This is a general page on debugging tools, techniques, tips, etc.

Linux tools

addr2line

See addr2line page.

gdb

See gdb page.

C/C++ - Debugging with gcc

backtrace, backtrace_symbols, backtrace_symbols_fd

See manual page, or gcc manual.

int     backtrace            (void **      buffer, int size)
char ** backtrace_symbols    (void *const *buffer, int size)
void    backtrace_symbols_fd (void *const *buffer, int size, int fd)

Object and executables need to build with gcc -g -rdynamic!

gcc -o0 -g -rdynamic main.c -o myprogram

Short example:

#include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>

void handler(int sig) {
  void *array[10];
  int size;

  size = backtrace(array, 10);                  // get void*'s for all entries on the stack

  fprintf(stderr, "Error: signal %d:\n", sig);  // print out all the frames to stderr
  backtrace_symbols_fd(array, size, 2);
  exit(1);
}

void baz() {
  int *foo = (int*)-1;                          // make a bad pointer
  printf("%d\n", *foo);                         // causes segfault
}

void bar() { baz(); }
void foo() { bar(); }

int main(int argc, char **argv) {
  signal(SIGSEGV, handler);                     // install our handler
  foo();                                        // this will call foo, bar, and baz.  baz segfaults.
  return 0;
}

It can also be used to print the symbol name of any function of which we know the address (a bit like SymFromAddr on windows):

#include <stdio.h>
#include <execinfo.h>

void foo(void) {
    printf("foo\n");
}

int main(int argc, char *argv[]) {
    void    *funptr = &foo;

    backtrace_symbols_fd(&funptr, 1, 1);

    return 0;
}


A very extensive answer on StackOverflow:

  • Using backtrace:
  • Demangling symbols in C++ by calling __cxa_demangle (like is done in tool c++filt):

Getting function name from its address

The following program show how to use the backtrace_symbols function to get the name of a function given its address. The program must be compiled with gcc options -g -rdynamic:

gcc -g -rdynamic -o0 main.c -o symfromaddr

A less efficient but more convenient version:

libunwind (non-gnu)