f37f92f1ba7434cdddd365564048b711ddc40816
[c11tester.git] / common.cc
1 #include <execinfo.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include <model-assert.h>
6
7 #include "common.h"
8 #include "model.h"
9 #include "stacktrace.h"
10
11 #define MAX_TRACE_LEN 100
12
13 #define CONFIG_STACKTRACE
14 /** Print a backtrace of the current program state. */
15 void print_trace(void)
16 {
17 #ifdef CONFIG_STACKTRACE
18         print_stacktrace(stdout);
19 #else
20         void *array[MAX_TRACE_LEN];
21         char **strings;
22         int size, i;
23
24         size = backtrace(array, MAX_TRACE_LEN);
25         strings = backtrace_symbols(array, size);
26
27         printf("\nDumping stack trace (%d frames):\n", size);
28
29         for (i = 0; i < size; i++)
30                 printf("\t%s\n", strings[i]);
31
32         free(strings);
33 #endif /* CONFIG_STACKTRACE */
34 }
35
36 void model_print_summary(void)
37 {
38         model->print_summary();
39 }
40
41 void assert_hook(void)
42 {
43         printf("Add breakpoint to line %u in file %s.\n",__LINE__,__FILE__);
44 }
45
46 void model_assert(bool expr, const char *file, int line)
47 {
48         if (!expr) {
49                 char msg[100];
50                 sprintf(msg, "Program has hit assertion in file %s at line %d\n",
51                                 file, line);
52                 model->assert_user_bug(msg);
53         }
54 }