Common.cpp

Go to the documentation of this file.
00001 //===-- Common.cpp --------------------------------------------------------===//
00002 //
00003 //                     The KLEE Symbolic Virtual Machine
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "Common.h"
00011 
00012 #include <stdlib.h>
00013 #include <stdio.h>
00014 #include <stdarg.h>
00015 #include <assert.h>
00016 #include <string.h>
00017 
00018 #include <set>
00019 
00020 using namespace klee;
00021 
00022 FILE* klee::klee_warning_file = NULL;
00023 FILE* klee::klee_message_file = NULL;
00024 
00025 
00026 /* Prints a message/warning.
00027    
00028    If pfx is NULL, this is a regular message, and it's sent to
00029    klee_message_file (messages.txt).  Otherwise, it is sent to 
00030    klee_warning_file (warnings.txt).
00031 
00032    Iff onlyToFile is false, the message is also printed on stderr.
00033 */
00034 static void klee_vmessage(const char *pfx, bool onlyToFile, const char *msg, va_list ap) {
00035   FILE *f = stderr;
00036   if (!onlyToFile) {
00037     fprintf(f, "KLEE: ");
00038     if (pfx) fprintf(f, "%s: ", pfx);
00039     vfprintf(f, msg, ap);
00040     fprintf(f, "\n");
00041     fflush(f);
00042   }
00043 
00044   if (pfx == NULL)
00045     f = klee_message_file;
00046   else f = klee_warning_file;
00047     
00048   if (f) {
00049     fprintf(f, "KLEE: ");
00050     if (pfx) fprintf(f, "%s: ", pfx);
00051     vfprintf(f, msg, ap);
00052     fprintf(f, "\n");
00053     fflush(f);
00054   }
00055 }
00056 
00057 
00058 void klee::klee_message(const char *msg, ...) {
00059   va_list ap;
00060   va_start(ap, msg);
00061   klee_vmessage(NULL, false, msg, ap);
00062   va_end(ap);
00063 }
00064 
00065 /* Message to be written only to file */
00066 void klee::klee_message_to_file(const char *msg, ...) {
00067   va_list ap;
00068   va_start(ap, msg);
00069   klee_vmessage(NULL, true, msg, ap);
00070   va_end(ap);
00071 }
00072 
00073 void klee::klee_error(const char *msg, ...) {
00074   va_list ap;
00075   va_start(ap, msg);
00076   klee_vmessage("ERROR", false, msg, ap);
00077   va_end(ap);
00078   exit(1);
00079 }
00080 
00081 void klee::klee_warning(const char *msg, ...) {
00082   va_list ap;
00083   va_start(ap, msg);
00084   klee_vmessage("WARNING", false, msg, ap);
00085   va_end(ap);
00086 }
00087 
00088 
00089 /* Prints a warning once per message. */
00090 void klee::klee_warning_once(const void *id, const char *msg, ...) {
00091   static std::set< std::pair<const void*, const char*> > keys;
00092   std::pair<const void*, const char*> key;
00093 
00094 
00095   /* "calling external" messages contain the actual arguments with
00096      which we called the external function, so we need to ignore them
00097      when computing the key. */
00098   if (strncmp(msg, "calling external", strlen("calling external")) != 0)
00099     key = std::make_pair(id, msg);
00100   else key = std::make_pair(id, "calling external");
00101   
00102   if (!keys.count(key)) {
00103     keys.insert(key);
00104     
00105     va_list ap;
00106     va_start(ap, msg);
00107     klee_vmessage("WARNING", false, msg, ap);
00108     va_end(ap);
00109   }
00110 }

Generated on Fri Jun 5 03:31:31 2009 for klee by  doxygen 1.5.8