misc: Add panic_if / fatal_if / chatty_assert
[gem5.git] / src / base / misc.hh
1 /*
2 * Copyright (c) 2014 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Nathan Binkert
41 * Dave Greene
42 */
43
44 #ifndef __BASE_MISC_HH__
45 #define __BASE_MISC_HH__
46
47 #include "base/compiler.hh"
48 #include "base/cprintf.hh"
49 #include "base/varargs.hh"
50
51 #if defined(__SUNPRO_CC)
52 #define __FUNCTION__ "how to fix me?"
53 #endif
54
55 // General exit message, these functions will never return and will
56 // either abort() if code is < 0 or exit with the code if >= 0
57 void __exit_message(const char *prefix, int code,
58 const char *func, const char *file, int line,
59 const char *format, CPRINTF_DECLARATION) M5_ATTR_NORETURN;
60
61 void __exit_message(const char *prefix, int code,
62 const char *func, const char *file, int line,
63 const std::string &format, CPRINTF_DECLARATION) M5_ATTR_NORETURN;
64
65 inline void
66 __exit_message(const char *prefix, int code,
67 const char *func, const char *file, int line,
68 const std::string& format, CPRINTF_DEFINITION)
69 {
70 __exit_message(prefix, code, func, file, line, format.c_str(),
71 VARARGS_ALLARGS);
72 }
73
74 M5_PRAGMA_NORETURN(__exit_message)
75 #define exit_message(prefix, code, ...) \
76 __exit_message(prefix, code, __FUNCTION__, __FILE__, __LINE__, \
77 __VA_ARGS__)
78
79 //
80 // This implements a cprintf based panic() function. panic() should
81 // be called when something happens that should never ever happen
82 // regardless of what the user does (i.e., an acutal m5 bug). panic()
83 // calls abort which can dump core or enter the debugger.
84 //
85 //
86 #define panic(...) exit_message("panic", -1, __VA_ARGS__)
87
88 //
89 // This implements a cprintf based fatal() function. fatal() should
90 // be called when the simulation cannot continue due to some condition
91 // that is the user's fault (bad configuration, invalid arguments,
92 // etc.) and not a simulator bug. fatal() calls abort() like
93 // panic() does.
94 //
95 #define fatal(...) exit_message("fatal", -1, __VA_ARGS__)
96
97 /**
98 * Conditional panic macro that checks the supplied condition and only panics
99 * if the condition is true and allows the programmer to specify diagnostic
100 * printout. Useful to replace if + panic, or if + print + assert, etc.
101 *
102 * @param cond Condition that is checked; if true -> panic
103 * @param ... Printf-based format string with arguments, extends printout.
104 */
105 #define panic_if(cond, ...) \
106 do { \
107 if ((cond)) \
108 exit_message("panic condition "#cond" occurred", -1, __VA_ARGS__); \
109 } while (0)
110
111
112 /**
113 * Conditional fatal macro that checks the supplied condition and only causes a
114 * fatal error if the condition is true and allows the programmer to specify
115 * diagnostic printout. Useful to replace if + fatal, or if + print + assert,
116 * etc.
117 *
118 * @param cond Condition that is checked; if true -> fatal
119 * @param ... Printf-based format string with arguments, extends printout.
120 */
121 #define fatal_if(cond, ...) \
122 do { \
123 if ((cond)) \
124 exit_message("fatal condition "#cond" occurred", 1, __VA_ARGS__); \
125 } while (0)
126
127
128 void
129 __base_message(std::ostream &stream, const char *prefix, bool verbose,
130 const char *func, const char *file, int line,
131 const char *format, CPRINTF_DECLARATION);
132
133 inline void
134 __base_message(std::ostream &stream, const char *prefix, bool verbose,
135 const char *func, const char *file, int line,
136 const std::string &format, CPRINTF_DECLARATION)
137 {
138 __base_message(stream, prefix, verbose, func, file, line, format.c_str(),
139 VARARGS_ALLARGS);
140 }
141
142 #define base_message(stream, prefix, verbose, ...) \
143 __base_message(stream, prefix, verbose, __FUNCTION__, __FILE__, __LINE__, \
144 __VA_ARGS__)
145
146 // Only print the message the first time this expression is
147 // encountered. i.e. This doesn't check the string itself and
148 // prevent duplicate strings, this prevents the statement from
149 // happening more than once. So, even if the arguments change and that
150 // would have resulted in a different message thoes messages would be
151 // supressed.
152 #define base_message_once(...) do { \
153 static bool once = false; \
154 if (!once) { \
155 base_message(__VA_ARGS__); \
156 once = true; \
157 } \
158 } while (0)
159
160 #define cond_message(cond, ...) do { \
161 if (cond) \
162 base_message(__VA_ARGS__); \
163 } while (0)
164
165 #define cond_message_once(cond, ...) do { \
166 static bool once = false; \
167 if (!once && cond) { \
168 base_message(__VA_ARGS__); \
169 once = true; \
170 } \
171 } while (0)
172
173
174 extern bool want_warn, warn_verbose;
175 extern bool want_info, info_verbose;
176 extern bool want_hack, hack_verbose;
177
178 #define warn(...) \
179 cond_message(want_warn, std::cerr, "warn", warn_verbose, __VA_ARGS__)
180 #define inform(...) \
181 cond_message(want_info, std::cout, "info", info_verbose, __VA_ARGS__)
182 #define hack(...) \
183 cond_message(want_hack, std::cerr, "hack", hack_verbose, __VA_ARGS__)
184
185 #define warn_once(...) \
186 cond_message_once(want_warn, std::cerr, "warn", warn_verbose, __VA_ARGS__)
187 #define inform_once(...) \
188 cond_message_once(want_info, std::cout, "info", info_verbose, __VA_ARGS__)
189 #define hack_once(...) \
190 cond_message_once(want_hack, std::cerr, "hack", hack_verbose, __VA_ARGS__)
191
192 /**
193 * The chatty assert macro will function like a normal assert, but will allow the
194 * specification of additional, helpful material to aid debugging why the
195 * assertion actually failed. Like the normal assertion, the chatty_assert
196 * will not be active in fast builds.
197 *
198 * @param cond Condition that is checked; if false -> assert
199 * @param ... Printf-based format string with arguments, extends printout.
200 */
201 #ifdef NDEBUG
202 #define chatty_assert(cond, ...)
203 #else //!NDEBUG
204 #define chatty_assert(cond, ...) \
205 do { \
206 if (!(cond)) { \
207 base_message(std::cerr, "assert("#cond") failing", 1, __VA_ARGS__); \
208 assert(cond); \
209 } \
210 } while (0)
211 #endif // NDEBUG
212 #endif // __BASE_MISC_HH__