execution: refactor common CV propagation into its own function
[cdsspec-compiler.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 /** @brief Model-checker output file descriptor; default to stdout until redirected */
18 int model_out = STDOUT_FILENO;
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(model_out);
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 assert_hook(void)
44 {
45         model_print("Add breakpoint to line %u in file %s.\n", __LINE__, __FILE__);
46 }
47
48 void model_assert(bool expr, const char *file, int line)
49 {
50         if (!expr) {
51                 char msg[100];
52                 sprintf(msg, "Program has hit assertion in file %s at line %d\n",
53                                 file, line);
54                 model->assert_user_bug(msg);
55         }
56 }
57
58 #ifndef CONFIG_DEBUG
59
60 static int fd_user_out; /**< @brief File descriptor from which to read user program output */
61
62 /**
63  * @brief Setup output redirecting
64  *
65  * Redirects user program's stdout to a pipe so that we can dump it
66  * selectively, when displaying bugs, etc.
67  * Also connects a file descriptor 'model_out' directly to stdout, for printing
68  * data when needed.
69  *
70  * The model-checker can selectively choose to print/hide the user program
71  * output.
72  * @see clear_program_output
73  * @see print_program_output
74  *
75  * Note that the user program's pipe has limited memory, so if a program will
76  * output much data, we will need to buffer it in user-space during execution.
77  * This also means that if ModelChecker decides not to print an execution, it
78  * should promptly clear the pipe.
79  *
80  * This function should only be called once.
81  */
82 void redirect_output()
83 {
84         /* Save stdout for later use */
85         model_out = dup(STDOUT_FILENO);
86         if (model_out < 0) {
87                 perror("dup");
88                 exit(EXIT_FAILURE);
89         }
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         if (dup2(pipefd[1], STDOUT_FILENO) < 0) {
98                 perror("dup2");
99                 exit(EXIT_FAILURE);
100         }
101         close(pipefd[1]);
102
103         /* Save the "read" side of the pipe for use later */
104         if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) < 0) {
105                 perror("fcntl");
106                 exit(EXIT_FAILURE);
107         }
108         fd_user_out = pipefd[0];
109 }
110
111 /**
112  * @brief Wrapper for reading data to buffer
113  *
114  * Besides a simple read, this handles the subtleties of EOF and nonblocking
115  * input (if fd is O_NONBLOCK).
116  *
117  * @param fd The file descriptor to read.
118  * @param buf Buffer to read to.
119  * @param maxlen Maximum data to read to buffer
120  * @return The length of data read. If zero, then we hit EOF or ran out of data
121  * (non-blocking)
122  */
123 static ssize_t read_to_buf(int fd, char *buf, size_t maxlen)
124 {
125         ssize_t ret = read(fd, buf, maxlen);
126         if (ret < 0) {
127                 if (errno == EAGAIN || errno == EWOULDBLOCK) {
128                         return 0;
129                 } else {
130                         perror("read");
131                         exit(EXIT_FAILURE);
132                 }
133         }
134         return ret;
135 }
136
137 /** @brief Dump any pending program output without printing */
138 void clear_program_output()
139 {
140         fflush(stdout);
141         char buf[200];
142         while (read_to_buf(fd_user_out, buf, sizeof(buf)));
143 }
144
145 /** @brief Print out any pending program output */
146 void print_program_output()
147 {
148         char buf[200];
149
150         /* Gather all program output */
151         fflush(stdout);
152
153         /* Read program output pipe and write to (real) stdout */
154         ssize_t ret;
155         while (1) {
156                 ret = read_to_buf(fd_user_out, buf, sizeof(buf));
157                 if (!ret)
158                         break;
159                 while (ret > 0) {
160                         ssize_t res = write(model_out, buf, ret);
161                         if (res < 0) {
162                                 perror("write");
163                                 exit(EXIT_FAILURE);
164                         }
165                         ret -= res;
166                 }
167         }
168 }
169 #endif /* ! CONFIG_DEBUG */