1 /* Copyright (c) 2015 Regents of the University of California
3 * Author: Brian Demsky <bdemsky@uci.edu>
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.
17 #include <model-assert.h>
21 #include "stacktrace.h"
24 #define MAX_TRACE_LEN 100
26 /** @brief Model-checker output file descriptor; default to stdout until redirected */
27 int model_out = STDOUT_FILENO;
29 #define CONFIG_STACKTRACE
30 /** Print a backtrace of the current program state. */
31 void print_trace(void)
33 #ifdef CONFIG_STACKTRACE
34 print_stacktrace(model_out);
36 void *array[MAX_TRACE_LEN];
40 size = backtrace(array, MAX_TRACE_LEN);
41 strings = backtrace_symbols(array, size);
43 model_print("\nDumping stack trace (%d frames):\n", size);
45 for (i = 0; i < size; i++)
46 model_print("\t%s\n", strings[i]);
49 #endif /* CONFIG_STACKTRACE */
52 void assert_hook(void)
54 model_print("Add breakpoint to line %u in file %s.\n", __LINE__, __FILE__);
57 void model_assert(bool expr, const char *file, int line)
60 printf("Program has hit assertion in file %s at line %d\n",
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 model_print("---- BEGIN PROGRAM OUTPUT ----\n");
159 /* Gather all program output */
162 /* Read program output pipe and write to (real) stdout */
165 ret = read_to_buf(fd_user_out, buf, sizeof(buf));
169 ssize_t res = write(model_out, buf, ret);
178 model_print("---- END PROGRAM OUTPUT ----\n");
180 #endif /* ! CONFIG_DEBUG */