8d38fa7312ccf45521d022021ef36b7737331449
[gem5.git] / src / base / misc.hh
1 /*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 * Dave Greene
30 */
31
32 #ifndef __BASE_MISC_HH__
33 #define __BASE_MISC_HH__
34
35 #include "base/compiler.hh"
36 #include "base/cprintf.hh"
37 #include "base/varargs.hh"
38
39 #if defined(__SUNPRO_CC)
40 #define __FUNCTION__ "how to fix me?"
41 #endif
42
43 //
44 // This implements a cprintf based panic() function. panic() should
45 // be called when something happens that should never ever happen
46 // regardless of what the user does (i.e., an acutal m5 bug). panic()
47 // calls abort which can dump core or enter the debugger.
48 //
49 //
50 void __panic(const char *func, const char *file, int line, const char *format,
51 CPRINTF_DECLARATION) M5_ATTR_NORETURN;
52 void __panic(const char *func, const char *file, int line,
53 const std::string &format, CPRINTF_DECLARATION)
54 M5_ATTR_NORETURN;
55
56 inline void
57 __panic(const char *func, const char *file, int line,
58 const std::string &format, CPRINTF_DEFINITION)
59 {
60 __panic(func, file, line, format.c_str(), VARARGS_ALLARGS);
61 }
62 M5_PRAGMA_NORETURN(__panic)
63 #define panic(...) __panic(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
64
65 //
66 // This implements a cprintf based fatal() function. fatal() should
67 // be called when the simulation cannot continue due to some condition
68 // that is the user's fault (bad configuration, invalid arguments,
69 // etc.) and not a simulator bug. fatal() calls exit(1), i.e., a
70 // "normal" exit with an error code, as opposed to abort() like
71 // panic() does.
72 //
73 void __fatal(const char *func, const char *file, int line, const char *format,
74 CPRINTF_DECLARATION) M5_ATTR_NORETURN;
75 void __fatal(const char *func, const char *file, int line,
76 const std::string &format, CPRINTF_DECLARATION)
77 M5_ATTR_NORETURN;
78
79 inline void
80 __fatal(const char *func, const char *file, int line,
81 const std::string &format, CPRINTF_DEFINITION)
82 {
83 __fatal(func, file, line, format.c_str(), VARARGS_ALLARGS);
84 }
85 M5_PRAGMA_NORETURN(__fatal)
86 #define fatal(...) __fatal(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
87
88 //
89 // This implements a cprintf based warn
90 //
91 void __warn(const char *func, const char *file, int line, const char *format,
92 CPRINTF_DECLARATION);
93 inline void
94 __warn(const char *func, const char *file, int line, const std::string &format,
95 CPRINTF_DECLARATION)
96 {
97 __warn(func, file, line, format, VARARGS_ALLARGS);
98 }
99 #define warn(...) __warn(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
100
101 // Only print the warning message the first time it is seen. This
102 // doesn't check the warning string itself, it just only lets one
103 // warning come from the statement. So, even if the arguments change
104 // and that would have resulted in a different warning message,
105 // subsequent messages would still be supressed.
106 #define warn_once(...) do { \
107 static bool once = false; \
108 if (!once) { \
109 warn(__VA_ARGS__); \
110 once = true; \
111 } \
112 } while (0)
113
114 #endif // __BASE_MISC_HH__