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