cyclegraph: return 'added' status for addEdge()
[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 stream; default to stdout until redirected */
18 FILE *model_out = stdout;
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 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 #ifndef CONFIG_DEBUG
64
65 static int fd_user_out; /**< @brief File descriptor from which to read user program output */
66
67 /**
68  * @brief Setup output redirecting
69  *
70  * Redirects user program's stdout to a pipe so that we can dump it
71  * selectively, when displaying bugs, etc.
72  * Also connects a special file 'model_out' directly to stdout, for printing
73  * data when needed.
74  *
75  * The model-checker can selectively choose to print/hide the user program
76  * output.
77  * @see clear_program_output
78  * @see print_program_output
79  *
80  * Note that the user program's pipe has limited memory, so if a program will
81  * output much data, we will need to buffer it in user-space during execution.
82  * This also means that if ModelChecker decides not to print an execution, it
83  * should promptly clear the pipe.
84  *
85  * This function should only be called once.
86  */
87 void redirect_output()
88 {
89         int fd;
90
91         /* Save stdout for later use */
92         fd = dup(fileno(stdout));
93         model_out = fdopen(fd, "w");
94
95         /* Redirect program output to a pipe */
96         int pipefd[2];
97         if (pipe(pipefd) < 0) {
98                 perror("pipe");
99                 exit(EXIT_FAILURE);
100         }
101         fd = dup2(pipefd[1], fileno(stdout)); // STDOUT_FILENO
102         close(pipefd[1]);
103
104         /* Save the "read" side of the pipe for use later */
105         fcntl(pipefd[0], F_SETFL, O_NONBLOCK);
106         fd_user_out = pipefd[0];
107 }
108
109 /**
110  * @brief Wrapper for reading data to buffer
111  *
112  * Besides a simple read, this handles the subtleties of EOF and nonblocking
113  * input (if fd is O_NONBLOCK).
114  *
115  * @param fd The file descriptor to read.
116  * @param buf Buffer to read to.
117  * @param maxlen Maximum data to read to buffer
118  * @return The length of data read. If zero, then we hit EOF or ran out of data
119  * (non-blocking)
120  */
121 static ssize_t read_to_buf(int fd, char *buf, size_t maxlen)
122 {
123         ssize_t ret = read(fd, buf, maxlen);
124         if (ret < 0) {
125                 if (errno == EAGAIN || errno == EWOULDBLOCK) {
126                         return 0;
127                 } else {
128                         perror("read");
129                         exit(EXIT_FAILURE);
130                 }
131         }
132         return ret;
133 }
134
135 /** @brief Dump any pending program output without printing */
136 void clear_program_output()
137 {
138         fflush(stdout);
139         char buf[200];
140         while (read_to_buf(fd_user_out, buf, sizeof(buf)));
141 }
142
143 /** @brief Print out any pending program output */
144 void print_program_output()
145 {
146         char buf[200];
147
148         /* Gather all program output */
149         fflush(stdout);
150
151         /* Read program output pipe and write to (real) stdout */
152         int ret;
153         while (1) {
154                 ret = read_to_buf(fd_user_out, buf, sizeof(buf));
155                 if (!ret)
156                         break;
157                 while (ret > 0) {
158                         int res = fwrite(buf, 1, ret, model_out);
159                         if (res < 0) {
160                                 errno = ferror(model_out);
161                                 perror("fwrite");
162                                 exit(EXIT_FAILURE);
163                         }
164                         ret -= res;
165                 }
166         }
167 }
168 #endif /* ! CONFIG_DEBUG */