klee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Common.cpp
Go to the documentation of this file.
1 //===-- Common.cpp --------------------------------------------------------===//
2 //
3 // The KLEE Symbolic Virtual Machine
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Common.h"
11 
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <stdarg.h>
15 #include <assert.h>
16 #include <string.h>
17 
18 #include <set>
19 
20 using namespace klee;
21 
24 
25 static void klee_vfmessage(FILE *fp, const char *pfx, const char *msg,
26  va_list ap) {
27  if (!fp)
28  return;
29 
30  fprintf(fp, "KLEE: ");
31  if (pfx) fprintf(fp, "%s: ", pfx);
32  vfprintf(fp, msg, ap);
33  fprintf(fp, "\n");
34  fflush(fp);
35 }
36 
37 /* Prints a message/warning.
38 
39  If pfx is NULL, this is a regular message, and it's sent to
40  klee_message_file (messages.txt). Otherwise, it is sent to
41  klee_warning_file (warnings.txt).
42 
43  Iff onlyToFile is false, the message is also printed on stderr.
44 */
45 static void klee_vmessage(const char *pfx, bool onlyToFile, const char *msg,
46  va_list ap) {
47  if (!onlyToFile) {
48  va_list ap2;
49  va_copy(ap2, ap);
50  klee_vfmessage(stderr, pfx, msg, ap2);
51  va_end(ap2);
52  }
53 
54  klee_vfmessage(pfx ? klee_warning_file : klee_message_file, pfx, msg, ap);
55 }
56 
57 
58 void klee::klee_message(const char *msg, ...) {
59  va_list ap;
60  va_start(ap, msg);
61  klee_vmessage(NULL, false, msg, ap);
62  va_end(ap);
63 }
64 
65 /* Message to be written only to file */
66 void klee::klee_message_to_file(const char *msg, ...) {
67  va_list ap;
68  va_start(ap, msg);
69  klee_vmessage(NULL, true, msg, ap);
70  va_end(ap);
71 }
72 
73 void klee::klee_error(const char *msg, ...) {
74  va_list ap;
75  va_start(ap, msg);
76  klee_vmessage("ERROR", false, msg, ap);
77  va_end(ap);
78  exit(1);
79 }
80 
81 void klee::klee_warning(const char *msg, ...) {
82  va_list ap;
83  va_start(ap, msg);
84  klee_vmessage("WARNING", false, msg, ap);
85  va_end(ap);
86 }
87 
88 
89 /* Prints a warning once per message. */
90 void klee::klee_warning_once(const void *id, const char *msg, ...) {
91  static std::set< std::pair<const void*, const char*> > keys;
92  std::pair<const void*, const char*> key;
93 
94 
95  /* "calling external" messages contain the actual arguments with
96  which we called the external function, so we need to ignore them
97  when computing the key. */
98  if (strncmp(msg, "calling external", strlen("calling external")) != 0)
99  key = std::make_pair(id, msg);
100  else key = std::make_pair(id, "calling external");
101 
102  if (!keys.count(key)) {
103  keys.insert(key);
104 
105  va_list ap;
106  va_start(ap, msg);
107  klee_vmessage("WARNING ONCE", false, msg, ap);
108  va_end(ap);
109  }
110 }
void void void void klee_warning_once(const void *id, const char *msg,...) __attribute__((format(printf
Definition: Common.cpp:90
FILE * klee_message_file
Definition: Common.cpp:23
void klee_error(const char *msg,...) __attribute__((format(printf
Definition: Common.cpp:73
void void void klee_warning(const char *msg,...) __attribute__((format(printf
Definition: Common.cpp:81
static void klee_vfmessage(FILE *fp, const char *pfx, const char *msg, va_list ap)
Definition: Common.cpp:25
void klee_message(const char *msg,...) __attribute__((format(printf
Definition: Common.cpp:58
void void klee_message_to_file(const char *msg,...) __attribute__((format(printf
Definition: Common.cpp:66
FILE * klee_warning_file
Definition: Common.cpp:22
static void klee_vmessage(const char *pfx, bool onlyToFile, const char *msg, va_list ap)
Definition: Common.cpp:45