Back to index

glibc  2.9
backtrace-tst.c
Go to the documentation of this file.
00001 #include <execinfo.h>
00002 #include <inttypes.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 
00006 
00007 static int
00008 compare (const void *p1, const void *p2)
00009 {
00010   void *ba[20];
00011   int n = backtrace (ba, sizeof (ba) / sizeof (ba[0]));
00012   if (n != 0)
00013     {
00014       char **names = backtrace_symbols (ba, n);
00015       if (names != NULL)
00016        {
00017          int i;
00018          printf ("called from %s\n", names[0]);
00019          for (i = 1; i < n; ++i)
00020            printf ("            %s\n", names[i]);
00021          free (names);
00022        }
00023     }
00024 
00025   return *(const uint32_t *) p1 - *(const uint32_t *) p2;
00026 }
00027 
00028 
00029 int
00030 main (int argc, char *argv[])
00031 {
00032   uint32_t arr[20];
00033   size_t cnt;
00034 
00035   for (cnt = 0; cnt < sizeof (arr) / sizeof (arr[0]); ++cnt)
00036     arr[cnt] = random ();
00037 
00038   qsort (arr, sizeof (arr) / sizeof (arr[0]), sizeof (arr[0]), compare);
00039 
00040   for (cnt = 0; cnt < sizeof (arr) / sizeof (arr[0]); ++cnt)
00041     printf ("%" PRIx32 "\n", arr[cnt]);
00042 
00043   return 0;
00044 }