1 #include <execinfo.h>
2 #include <inttypes.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdint.h>
6
7
8 static int
compare(const void * p1,const void * p2)9 compare (const void *p1, const void *p2)
10 {
11 void *ba[20];
12 int n = backtrace (ba, sizeof (ba) / sizeof (ba[0]));
13 if (n != 0)
14 {
15 char **names = backtrace_symbols (ba, n);
16 if (names != NULL)
17 {
18 int i;
19 printf ("called from %s\n", names[0]);
20 for (i = 1; i < n; ++i)
21 printf (" %s\n", names[i]);
22 free (names);
23 }
24 }
25
26 return *(const uint32_t *) p1 - *(const uint32_t *) p2;
27 }
28
29
30 int
main(int argc,char * argv[])31 main (int argc, char *argv[])
32 {
33 uint32_t arr[20];
34 size_t cnt;
35
36 for (cnt = 0; cnt < sizeof (arr) / sizeof (arr[0]); ++cnt)
37 arr[cnt] = random ();
38
39 qsort (arr, sizeof (arr) / sizeof (arr[0]), sizeof (arr[0]), compare);
40
41 for (cnt = 0; cnt < sizeof (arr) / sizeof (arr[0]); ++cnt)
42 printf ("%" PRIx32 "\n", arr[cnt]);
43
44 return 0;
45 }
46