model: optimize get_last_conflict() search
[c11tester.git] / common.cc
1 #include <execinfo.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <fcntl.h>
7
8 #include <model-assert.h>
9
10 #include "common.h"
11 #include "model.h"
12 #include "stacktrace.h"
13 #include "output.h"
14
15 #define MAX_TRACE_LEN 100
16
17 FILE *model_out;
18 int fd_user_out; /**< @brief File descriptor from which to read user program output */
19
20 #define CONFIG_STACKTRACE
21 /** Print a backtrace of the current program state. */
22 void print_trace(void)
23 {
24 #ifdef CONFIG_STACKTRACE
25         print_stacktrace(stdout);
26 #else
27         void *array[MAX_TRACE_LEN];
28         char **strings;
29         int size, i;
30
31         size = backtrace(array, MAX_TRACE_LEN);
32         strings = backtrace_symbols(array, size);
33
34         model_print("\nDumping stack trace (%d frames):\n", size);
35
36         for (i = 0; i < size; i++)
37                 model_print("\t%s\n", strings[i]);
38
39         free(strings);
40 #endif /* CONFIG_STACKTRACE */
41 }
42
43 void model_print_summary(void)
44 {
45         model->print_summary();
46 }
47
48 void assert_hook(void)
49 {
50         model_print("Add breakpoint to line %u in file %s.\n",__LINE__,__FILE__);
51 }
52
53 void model_assert(bool expr, const char *file, int line)
54 {
55         if (!expr) {
56                 char msg[100];
57                 sprintf(msg, "Program has hit assertion in file %s at line %d\n",
58                                 file, line);
59                 model->assert_user_bug(msg);
60         }
61 }
62
63 /**
64  * @brief Setup output redirecting
65  *
66  * Redirects user program's stdout to a pipe so that we can dump it
67  * selectively, when displaying bugs, etc.
68  * Also connects a special file 'model_out' directly to stdout, for printing
69  * data when needed.
70  *
71  * The model-checker can selectively choose to print/hide the user program
72  * output.
73  * @see clear_program_output
74  * @see print_program_output
75  *
76  * Note that the user program's pipe has limited memory, so if a program will
77  * output much data, we will need to buffer it in user-space during execution.
78  * This also means that if ModelChecker decides not to print an execution, it
79  * should promptly clear the pipe.
80  *
81  * This function should only be called once.
82  */
83 void redirect_output()
84 {
85         int fd;
86
87         /* Save stdout for later use */
88         fd = dup(fileno(stdout));
89         model_out = fdopen(fd, "w");
90
91         /* Redirect program output to a pipe */
92         int pipefd[2];
93         if (pipe(pipefd) < 0) {
94                 perror("pipe");
95                 exit(EXIT_FAILURE);
96         }
97         fd = dup2(pipefd[1], fileno(stdout)); // STDOUT_FILENO
98         close(pipefd[1]);
99
100         /* Save the "read" side of the pipe for use later */
101         fcntl(pipefd[0], F_SETFL, O_NONBLOCK);
102         fd_user_out = pipefd[0];
103 }
104
105 /**
106  * @brief Wrapper for reading data to buffer
107  *
108  * Besides a simple read, this handles the subtleties of EOF and nonblocking
109  * input (if fd is O_NONBLOCK).
110  *
111  * @param fd The file descriptor to read.
112  * @param buf Buffer to read to.
113  * @param maxlen Maximum data to read to buffer
114  * @return The length of data read. If zero, then we hit EOF or ran out of data
115  * (non-blocking)
116  */
117 static ssize_t read_to_buf(int fd, char *buf, size_t maxlen)
118 {
119         ssize_t ret = read(fd, buf, maxlen);
120         if (ret < 0) {
121                 if (errno == EAGAIN || errno == EWOULDBLOCK) {
122                         return 0;
123                 } else {
124                         perror("read");
125                         exit(EXIT_FAILURE);
126                 }
127         }
128         return ret;
129 }
130
131 /** @brief Dump any pending program output without printing */
132 void clear_program_output()
133 {
134         fflush(stdout);
135         char buf[200];
136         while (read_to_buf(fd_user_out, buf, sizeof(buf)));
137 }
138
139 /** @brief Print out any pending program output */
140 void print_program_output()
141 {
142         char buf[200];
143
144         /* Gather all program output */
145         fflush(stdout);
146
147         /* Read program output pipe and write to (real) stdout */
148         int ret;
149         while (1) {
150                 ret = read_to_buf(fd_user_out, buf, sizeof(buf));
151                 if (!ret)
152                         break;
153                 while (ret > 0) {
154                         int res = fwrite(buf, 1, ret, model_out);
155                         if (res < 0) {
156                                 errno = ferror(model_out);
157                                 perror("fwrite");
158                                 exit(EXIT_FAILURE);
159                         }
160                         ret -= res;
161                 }
162         }
163 }