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