.I think this code is more correct.
[glibc.git] / debug / backtrace-tst.c
blobce80685bf8f4d14904e40d1e8c24e89efe713931
1 #include <execinfo.h>
2 #include <inttypes.h>
3 #include <stdio.h>
4 #include <stdlib.h>
7 static int
8 compare (const void *p1, const void *p2)
10 void *ba[20];
11 int n = backtrace (ba, sizeof (ba) / sizeof (ba[0]));
12 if (n != 0)
14 char **names = backtrace_symbols (ba, n);
15 if (names != NULL)
17 int i;
18 printf ("called from %s\n", names[0]);
19 for (i = 1; i < n; ++i)
20 printf (" %s\n", names[i]);
21 free (names);
25 return *(const uint32_t *) p1 - *(const uint32_t *) p2;
29 int
30 main (int argc, char *argv[])
32 uint32_t arr[20];
33 size_t cnt;
35 for (cnt = 0; cnt < sizeof (arr) / sizeof (arr[0]); ++cnt)
36 arr[cnt] = random ();
38 qsort (arr, sizeof (arr) / sizeof (arr[0]), sizeof (arr[0]), compare);
40 for (cnt = 0; cnt < sizeof (arr) / sizeof (arr[0]); ++cnt)
41 printf ("%" PRIx32 "\n", arr[cnt]);
43 return 0;