PIPS
misc-local.h
Go to the documentation of this file.
1 /*
2 
3  $Id: misc-local.h 23642 2021-03-10 09:40:38Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <setjmp.h>
28 
29 // different type of logs...
30 // pg: debug, log, notice, warning, error, fatal, panic
31 typedef enum {
32  none_log, // nought
33  info_log, // some information
34  warning_log, // just a warning
37  user_error_log, // phase is stopped because of user, exception
38  internal_error_log, // phase is stopped because of pips, exception
39  irrecoverable_error_log // phase is stopped because of pips + exit/abort
41 
42 // logic: 128 + SIGALRM==14 == 142
43 #define TIMEOUT_CODE (128+14)
44 
45 /* To generate a string from a macro: */
46 #define STRINGIFY_SECOND_STAGE(symbol) #symbol
47 /* If not using this 2-stage macro evaluation, the generated string is not
48  the value of the macro but the name of the macro... Who said C was a
49  simple language? :-/ */
50 #define STRINGIFY(symbol) STRINGIFY_SECOND_STAGE(symbol)
51 
52 // yep, override system abort/exit...
53 #define abort() pips_stop(internal_error_log, -1, "abort() called")
54 #define exit(code) pips_stop(user_error_log, code, "exit(%d) called", code)
55 
56 /* Measurement type for mem_spy.c */
58 
59 # if defined(CURRENT_FUNCTION)
60 # undef CURRENT_FUNCTION
61 # endif // CURRENT_FUNCTION
62 
63 #define pips_unknown_function "Unknown Function Name"
64 
65 # if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) // C99
66 # define CURRENT_FUNCTION __func__
67 # else
68 # if defined(__GNUC__) // GCC
69 # define CURRENT_FUNCTION __FUNCTION__
70 # else
71 # define CURRENT_FUNCTION pips_unknown_function
72 # endif // __GNUC__
73 # endif // C99
74 
75 /* these macros use the GNU extensions that allow variadic macros,
76  * including with an empty list.
77  *
78  * if not available, function calls are used.
79  *
80  * a slightly better job could be done if C99 is assumed.
81  */
82 
83 #if defined(__GNUC__) // GCC
84 
85 // Use the old "do {...} while(0)" C hack to allow a macro with arbitrary
86 // code to be used everywhere
87 
88 #define pips_debug(level, format, args...) \
89  do { \
90  ifdebug(level) { \
91  fprintf(stderr, "[%s] " format, CURRENT_FUNCTION, ##args); \
92  } \
93  } while(0)
94 
95 #define pips_user_warning(format, args...) \
96  pips_user_warning_func(CURRENT_FUNCTION, __FILE__, __LINE__, format, ##args)
97 
98 #define pips_user_error(format, args...) \
99  pips_user_error_func(CURRENT_FUNCTION, __FILE__, __LINE__, format, ##args)
100 
101 #define pips_user_irrecoverable_error(format, args...) \
102  pips_user_irrecoverable_error_func(CURRENT_FUNCTION, __FILE__, __LINE__, \
103  format, ##args)
104 
105 // internal errors add file/line information
106 #define pips_internal_error(format, args...) \
107  pips_internal_error_func(CURRENT_FUNCTION, __FILE__ , __LINE__, \
108  format, ##args)
109 
110 #define pips_log(args...) \
111  pips_log_func(CURRENT_FUNCTION, __FILE__, __LINE__, ##args)
112 
113 #define pips_stop(args...) \
114  pips_stop_func(CURRENT_FUNCTION, __FILE__, __LINE__, ##args)
115 
116 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) // C99
117 
118 #define pips_debug(level, ...) \
119  do { \
120  ifdebug(level) \
121  pips_debug_func(level, CURRENT_FUNCTION, __VA_ARGS__); \
122  } while (0)
123 
124 #define pips_user_warning(...) \
125  pips_user_warning_func(CURRENT_FUNCTION, __FILE__, __LINE__, __VA_ARGS__)
126 
127 #define pips_user_error(...) \
128  pips_user_error_func(CURRENT_FUNCTION, __FILE__, __LINE__, __VA_ARGS__)
129 
130 #define pips_user_irrecoverable_error(...) \
131  pips_user_irrecoverable_error_func(CURRENT_FUNCTION, __FILE__, __LINE__, \
132  __VA_ARGS__)
133 
134 #define pips_internal_error(...) \
135  pips_internal_error_func(CURRENT_FUNCTION, __FILE__ , __LINE__, \
136  __VA_ARGS__)
137 
138 #define pips_log(...) \
139  pips_log_func(CURRENT_FUNCTION, __FILE__, __LINE__, __VA_ARGS__)
140 
141 #define pips_stop(...) \
142  pips_log_func(CURRENT_FUNCTION, __FILE__, __LINE__, __VA_ARGS__)
143 
144 #else // others, function name, file and line is lost
145 #define pips_debug pips_debug_func2
146 #define pips_user_warning pips_user_warning_func2
147 #define pips_user_error pips_user_error_func2
148 #define pips_user_irrecoverable_error pips_user_irrecoverable_error_func2
149 #define pips_internal_error pips_internal_error_func2
150 #define pips_log pips_log_func2
151 #define pips_stop pips_stop_func2
152 #endif
153 
154 #define pips_where(out) \
155  fprintf(out, "[%s] (%s:%d) ", CURRENT_FUNCTION, __FILE__, __LINE__)
156 
157 #define debug_on(env) \
158  debug_on_function(env, CURRENT_FUNCTION, __FILE__, __LINE__)
159 
160 #define debug_off() \
161  debug_off_function(CURRENT_FUNCTION, __FILE__, __LINE__)
162 
163 /* common macros, two flavors depending on NDEBUG */
164 #ifdef NDEBUG
165 
166 #define pips_assert(what, predicate)
167 #define pips_user_assert(what, predicate)
168 #define ifdebug(l) if(0)
169 
170 #else
171 
172 #define pips_assert(what, predicate) \
173  do { \
174  if (!(predicate)) { \
175  pips_internal_error("assertion failed\n\n '%s' not verified\n\n", \
176  what); \
177  abort(); \
178  } \
179  } while(0)
180 
181 #define pips_user_assert(what, predicate) \
182  do { \
183  if (!(predicate)) { \
184  pips_user_error("assertion failed\n\n '%s' not verified\n\n" \
185  "this is a USER ERROR, I guess\n", what); \
186  } \
187  } while(0)
188 
189 #define ifdebug(l) if (the_current_debug_level>=(l))
190 
191 #endif
192 
193 #define pips_exit(code, format, args...) \
194  pips_user_warning(format, ##args), exit(code)
195 
196 /* FI:need to breakpoint while inlining is available */
197 /* #define same_string_p(s1, s2) (strcmp((s1), (s2)) == 0)*/
198 #define same_string_p(s1, s2) function_same_string_p(s1,s2)
199 #define same_stringn_p(a,b,c) (!strncmp((a),(b),(c)))
200 
201 /* MAXPATHLEN is defined in <sys/param.h> for SunOS... but not for all OS! */
202 #ifndef MAXPATHLEN
203 #define MAXPATHLEN 1024
204 #endif
205 
206 #define PIPS_CATCH(what) \
207  if (push_debug_status(), \
208  setjmp(*push_exception_on_stack(what, __CURRENT_FUNCTION_NAME__, \
209  __FILE__, __LINE__, pop_debug_status)))
210 
211 /* SG moved there from transformation.h */
212 #define SIGN_EQ(a,b) ((((a)>0 && (b)>0) || ((a)<0 && (b)<0)) ? true : false)
213 #define FORTRAN_DIV(n,d) (SIGN_EQ((n),(d)) ? ABS(n)/ABS(d) : -(ABS(n)/ABS(d)))
214 #define C_DIVISION(n,d) ((n)/(d))
215 #define FORTRAN_MOD(n,m) (SIGN_EQ((n),(m)) ? ABS(n)%ABS(m) : -(ABS(n)%ABS(m)))
216 #define C_MODULO(n,m) ((n)%(m))
217 
218 // redundant declaration to ease bootstrapping
219 extern int the_current_debug_level;
220 
221 // a reset functions is just something to call...
222 typedef void (*reset_func_t)(void);
223 
224 // to avoid compiler warnings...
225 #define asprintf safe_asprintf
226 #define vasprintf safe_vasprintf
227 
228 #ifndef __has_attribute
229 #define __has_attribute(att) 0
230 #endif // !__has_attribute
231 
232 #define _UNUSED_ __attribute__((unused))
233 #define _NORETURN_ __attribute__((noreturn))
234 
235 #if __has_attribute(fallthrough)
236 #define _FALLTHROUGH_ __attribute__((fallthrough))
237 #else
238 #define _FALLTHROUGH_
239 #endif // fallthrough
240 
241 /* expecititely declare "noreturn" functions...
242  * because cproto generated headers seem to ignore attributes
243  */
244 void _NORETURN_ pips_stop_func(const char *, const char *, const int,
245  pips_log_t, int, const string, ...);
246 void _NORETURN_ pips_internal_error_func(const char *, const char *, const int,
247  const char *, ...);
248 void _NORETURN_ pips_user_error_func(const char *, const char *, const int,
249  const char *, ...);
251  const char *, const char *, const int, const char *, va_list *);
253  const char *, const char *, const int, const char *, ...);
254 
255 void _NORETURN_ pips_stop_func2(pips_log_t, int, const string, ...);
256 void _NORETURN_ pips_internal_error_func2(const char *, ...);
257 void _NORETURN_ pips_user_error_func2(const char *, ...);
258 void _NORETURN_ pips_user_irrecoverable_error_func2(const char *, ...);
259 
260 /***************************************************************** OLD STUFF */
261 
262 #define user_warning(fn, ...) \
263  pips_user_warning_func(fn, __FILE__, __LINE__, __VA_ARGS__)
264 
265 #define user_error(fn, ...) \
266  pips_user_error_func(fn, __FILE__, __LINE__, __VA_ARGS__)
void _NORETURN_ pips_internal_error_func2(const char *,...)
Definition: message.c:408
void _NORETURN_ pips_user_irrecoverable_error_func2(const char *,...)
Definition: message.c:550
void(* reset_func_t)(void)
Definition: misc-local.h:222
void _NORETURN_ pips_user_irrecoverable_error_alist(const char *, const char *, const int, const char *, va_list *)
Definition: message.c:516
#define _NORETURN_
Definition: misc-local.h:233
measurement_type
Measurement type for mem_spy.c.
Definition: misc-local.h:57
@ SBRK_MEASURE
Definition: misc-local.h:57
@ GROSS_MEASURE
Definition: misc-local.h:57
@ NET_MEASURE
Definition: misc-local.h:57
pips_log_t
Definition: misc-local.h:31
@ warning_log
Definition: misc-local.h:34
@ internal_error_log
Definition: misc-local.h:38
@ user_error_log
Definition: misc-local.h:37
@ spear_warning_log
Definition: misc-local.h:35
@ spear_error_log
Definition: misc-local.h:36
@ none_log
Definition: misc-local.h:32
@ info_log
Definition: misc-local.h:33
@ irrecoverable_error_log
Definition: misc-local.h:39
void _NORETURN_ pips_internal_error_func(const char *, const char *, const int, const char *,...)
Definition: message.c:394
void _NORETURN_ pips_user_error_func(const char *, const char *, const int, const char *,...)
Definition: message.c:478
int the_current_debug_level
Debugging functions.
Definition: debug.c:53
void _NORETURN_ pips_user_error_func2(const char *,...)
Definition: message.c:492
void _NORETURN_ pips_stop_func(const char *, const char *, const int, pips_log_t, int, const string,...)
expecititely declare "noreturn" functions...
Definition: message.c:904
void _NORETURN_ pips_stop_func2(pips_log_t, int, const string,...)
Definition: message.c:924
void _NORETURN_ pips_user_irrecoverable_error_func(const char *, const char *, const int, const char *,...)
Definition: message.c:535