Fix Loop Exit Bug
[satcheck.git] / common.cc
1 /*      Copyright (c) 2015 Regents of the University of California
2  *
3  *      Author: Brian Demsky <bdemsky@uci.edu>
4  *
5  *      This program is free software; you can redistribute it and/or
6  *      modify it under the terms of the GNU General Public License
7  *      version 2 as published by the Free Software Foundation.
8  */
9
10 #include <execinfo.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <fcntl.h>
16
17 #include <model-assert.h>
18
19 #include "common.h"
20 #include "model.h"
21 #include "stacktrace.h"
22 #include "output.h"
23
24 #define MAX_TRACE_LEN 100
25
26 /** @brief Model-checker output file descriptor; default to stdout until redirected */
27 int model_out = STDOUT_FILENO;
28
29 #define CONFIG_STACKTRACE
30 /** Print a backtrace of the current program state. */
31 void print_trace(void)
32 {
33 #ifdef CONFIG_STACKTRACE
34         print_stacktrace(model_out);
35 #else
36         void *array[MAX_TRACE_LEN];
37         char **strings;
38         int size, i;
39
40         size = backtrace(array, MAX_TRACE_LEN);
41         strings = backtrace_symbols(array, size);
42
43         model_print("\nDumping stack trace (%d frames):\n", size);
44
45         for (i = 0; i < size; i++)
46                 model_print("\t%s\n", strings[i]);
47
48         free(strings);
49 #endif /* CONFIG_STACKTRACE */
50 }
51
52 void assert_hook(void)
53 {
54         model_print("Add breakpoint to line %u in file %s.\n", __LINE__, __FILE__);
55 }
56
57 void model_assert(bool expr, const char *file, int line)
58 {
59         if (!expr) {
60                 printf("Program has hit assertion in file %s at line %d\n",
61                                          file, line);
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         model_print("---- BEGIN PROGRAM OUTPUT ----\n");
158
159         /* Gather all program output */
160         fflush(stdout);
161
162         /* Read program output pipe and write to (real) stdout */
163         ssize_t ret;
164         while (1) {
165                 ret = read_to_buf(fd_user_out, buf, sizeof(buf));
166                 if (!ret)
167                         break;
168                 while (ret > 0) {
169                         ssize_t res = write(model_out, buf, ret);
170                         if (res < 0) {
171                                 perror("write");
172                                 exit(EXIT_FAILURE);
173                         }
174                         ret -= res;
175                 }
176         }
177
178         model_print("---- END PROGRAM OUTPUT   ----\n");
179 }
180 #endif /* ! CONFIG_DEBUG */