common: add print_trace() for backtracing
[c11tester.git] / common.cc
1 #include <execinfo.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "common.h"
6
7 #define MAX_TRACE_LEN 100
8
9 /** Print a backtrace of the current program state. */
10 void print_trace(void)
11 {
12         void *array[MAX_TRACE_LEN];
13         char **strings;
14         int size, i;
15
16         size = backtrace(array, MAX_TRACE_LEN);
17         strings = backtrace_symbols(array, size);
18
19         printf("\nDumping stack trace (%d frames):\n", size);
20
21         for (i = 0; i < size; i++)
22                 printf("\t%s\n", strings[i]);
23
24         free(strings);
25 }