This blog is under construction

Saturday, 2 February 2013

backtrace example in C


Header file:
    execinfo.h

Synopsis:
     int backtrace(void **buffer, int size);

Description:
     Gets backtrace for the calling thread and places the symbols(list of pointers) into the given buffer.  The size argument represent the number of void * elements that would fit into buffer.  And it returns the actual number of entries obtained in buffer.

backtrace function C example


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

  void func5() {
        void *array[12];
        int size, i;
        size = backtrace(array, 12);
        for (i = 0; i < size; i++) {
                printf("0x0%08x\n", (int)array[i]);
        }
        printf("No of level in backtrace:%d\n", size);
  }

  void func4() {
        func5();
  }

  void func3() {
        func4();
  }

  void func2() {
        func3();
  }

  void func1() {
        func2();
  }

  int main() {
        func1();
        return 0;
  }




  Output:
  jp@jp-VirtualBox:$ ./a.out
  0x008048465
  0x00804844a
  0x00804843d
  0x008048430
  0x008048423
  0x00804840f
  0x00055dce7
  0x008048371
  No of level in backtrace:8




1 comment: