8 #include <model-assert.h>
12 #include "stacktrace.h"
15 #define MAX_TRACE_LEN 100
17 /** @brief Model-checker output file descriptor; default to stdout until redirected */
18 int model_out = STDOUT_FILENO;
20 #define CONFIG_STACKTRACE
21 /** Print a backtrace of the current program state. */
22 void print_trace(void)
24 #ifdef CONFIG_STACKTRACE
25 FILE *file = fdopen(model_out, "w");
26 print_stacktrace(file);
29 void *array[MAX_TRACE_LEN];
33 size = backtrace(array, MAX_TRACE_LEN);
34 strings = backtrace_symbols(array, size);
36 model_print("\nDumping stack trace (%d frames):\n", size);
38 for (i = 0; i < size; i++)
39 model_print("\t%s\n", strings[i]);
42 #endif /* CONFIG_STACKTRACE */
45 void model_print_summary(void)
47 model->print_summary();
50 void assert_hook(void)
52 model_print("Add breakpoint to line %u in file %s.\n", __LINE__, __FILE__);
55 void model_assert(bool expr, const char *file, int line)
59 sprintf(msg, "Program has hit assertion in file %s at line %d\n",
61 model->assert_user_bug(msg);
67 static int fd_user_out; /**< @brief File descriptor from which to read user program output */
70 * @brief Setup output redirecting
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
77 * The model-checker can selectively choose to print/hide the user program
79 * @see clear_program_output
80 * @see print_program_output
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.
87 * This function should only be called once.
89 void redirect_output()
91 /* Save stdout for later use */
92 model_out = dup(STDOUT_FILENO);
98 /* Redirect program output to a pipe */
100 if (pipe(pipefd) < 0) {
104 if (dup2(pipefd[1], STDOUT_FILENO) < 0) {
110 /* Save the "read" side of the pipe for use later */
111 if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) < 0) {
115 fd_user_out = pipefd[0];
119 * @brief Wrapper for reading data to buffer
121 * Besides a simple read, this handles the subtleties of EOF and nonblocking
122 * input (if fd is O_NONBLOCK).
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
130 static ssize_t read_to_buf(int fd, char *buf, size_t maxlen)
132 ssize_t ret = read(fd, buf, maxlen);
134 if (errno == EAGAIN || errno == EWOULDBLOCK) {
144 /** @brief Dump any pending program output without printing */
145 void clear_program_output()
149 while (read_to_buf(fd_user_out, buf, sizeof(buf)));
152 /** @brief Print out any pending program output */
153 void print_program_output()
157 /* Gather all program output */
160 /* Read program output pipe and write to (real) stdout */
163 ret = read_to_buf(fd_user_out, buf, sizeof(buf));
167 ssize_t res = write(model_out, buf, ret);
176 #endif /* ! CONFIG_DEBUG */