xref: /petsc/include/petscerror.h (revision a4fd02ac6712e7c4a27c9ffdf1171591c41a72f0)
1 /*
2     Contains all error handling interfaces for PETSc.
3 */
4 #if !defined(__PETSCERROR_H)
5 #define __PETSCERROR_H
6 #include "petsc.h"
7 PETSC_EXTERN_CXX_BEGIN
8 
9 /*
10    Defines the directory where the compiled source is located; used
11    in printing error messages. Each makefile has an entry
12    LOCDIR	  =  thedirectory
13    and bmake/common_variables includes in CCPPFLAGS -D__SDIR__='"${LOCDIR}"'
14    which is a flag passed to the C/C++ compilers. This declaration below
15    is only needed if some code is compiled without the -D__SDIR__
16 */
17 #if !defined(__SDIR__)
18 #define __SDIR__ "unknowndirectory/"
19 #endif
20 
21 /*
22    Defines the function where the compiled source is located; used
23    in printing error messages. This is defined here in case the user
24    does not declare it.
25 */
26 #if !defined(__FUNCT__)
27 #define __FUNCT__ "User provided function"
28 #endif
29 
30 /*
31      These are the generic error codes. These error codes are used
32      many different places in the PETSc source code. The string versions are
33      at src/sys/error/err.c any changes here must also be made there
34      These are also define in include/finclude/petscerror.h any CHANGES here
35      must be also made there.
36 
37 */
38 #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */
39 
40 #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
41 #define PETSC_ERR_SUP              56   /* no support for requested operation */
42 #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
43 #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
44 #define PETSC_ERR_SIG              59   /* signal received */
45 #define PETSC_ERR_FP               72   /* floating point exception */
46 #define PETSC_ERR_COR              74   /* corrupted PETSc object */
47 #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
48 #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
49 #define PETSC_ERR_MEMC             78   /* memory corruption */
50 #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
51 #define PETSC_ERR_USER             83   /* user has not provided needed function */
52 #define PETSC_ERR_SYS              88   /* error in system call */
53 
54 #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
55 #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
56 #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
57 #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
58 #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
59 #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
60 #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
61 #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
62 #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
63 #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
64 #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
65 #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
66 #define PETSC_ERR_ARG_DOMAIN       87   /* argument is not in domain of function */
67 
68 #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
69 #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
70 #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
71 #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
72 
73 #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
74 #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
75 
76 #define PETSC_ERR_MAX_VALUE        89   /* this is always the one more than the largest error code */
77 
78 #if defined(PETSC_USE_ERRORCHECKING)
79 
80 /*MC
81    SETERRQ - Macro that is called when an error has been detected,
82 
83    Not Collective
84 
85    Synopsis:
86    PetscErrorCode SETERRQ(PetscErrorCode errorcode,char *message)
87 
88 
89    Input Parameters:
90 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
91 -  message - error message
92 
93   Level: beginner
94 
95    Notes:
96     Once the error handler is called the calling function is then returned from with the given error code.
97 
98     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
99 
100 
101    Experienced users can set the error handler with PetscPushErrorHandler().
102 
103    Concepts: error^setting condition
104 
105 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
106 M*/
107 #define SETERRQ(n,s)              {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);}
108 
109 /*MC
110    SETERRQ1 - Macro that is called when an error has been detected,
111 
112    Not Collective
113 
114    Synopsis:
115    PetscErrorCode SETERRQ1(PetscErrorCode errorcode,char *formatmessage,arg)
116 
117 
118    Input Parameters:
119 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
120 .  message - error message in the printf format
121 -  arg - argument (for example an integer, string or double)
122 
123   Level: beginner
124 
125    Notes:
126     Once the error handler is called the calling function is then returned from with the given error code.
127 
128    Experienced users can set the error handler with PetscPushErrorHandler().
129 
130    Concepts: error^setting condition
131 
132 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
133 M*/
134 #define SETERRQ1(n,s,a1)          {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1);}
135 
136 /*MC
137    SETERRQ2 - Macro that is called when an error has been detected,
138 
139    Not Collective
140 
141    Synopsis:
142    PetscErrorCode SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
143 
144 
145    Input Parameters:
146 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
147 .  message - error message in the printf format
148 .  arg1 - argument (for example an integer, string or double)
149 -  arg2 - argument (for example an integer, string or double)
150 
151   Level: beginner
152 
153    Notes:
154     Once the error handler is called the calling function is then returned from with the given error code.
155 
156    Experienced users can set the error handler with PetscPushErrorHandler().
157 
158    Concepts: error^setting condition
159 
160 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
161 M*/
162 #define SETERRQ2(n,s,a1,a2)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2);}
163 
164 /*MC
165    SETERRQ3 - Macro that is called when an error has been detected,
166 
167    Not Collective
168 
169    Synopsis:
170    PetscErrorCode SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)
171 
172 
173    Input Parameters:
174 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
175 .  message - error message in the printf format
176 .  arg1 - argument (for example an integer, string or double)
177 .  arg2 - argument (for example an integer, string or double)
178 -  arg3 - argument (for example an integer, string or double)
179 
180   Level: beginner
181 
182    Notes:
183     Once the error handler is called the calling function is then returned from with the given error code.
184 
185     There are also versions for 4, 5, 6 and 7 arguments.
186 
187    Experienced users can set the error handler with PetscPushErrorHandler().
188 
189    Concepts: error^setting condition
190 
191 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
192 M*/
193 #define SETERRQ3(n,s,a1,a2,a3)    {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3);}
194 
195 #define SETERRQ4(n,s,a1,a2,a3,a4) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4);}
196 #define SETERRQ5(n,s,a1,a2,a3,a4,a5)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5);}
197 #define SETERRQ6(n,s,a1,a2,a3,a4,a5,a6)    {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5,a6);}
198 #define SETERRQ7(n,s,a1,a2,a3,a4,a5,a6,a7) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5,a6,a7);}
199 #define SETERRABORT(comm,n,s)     {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);MPI_Abort(comm,n);}
200 
201 /*MC
202    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
203 
204    Not Collective
205 
206    Synopsis:
207    PetscErrorCode CHKERRQ(PetscErrorCode errorcode)
208 
209 
210    Input Parameters:
211 .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
212 
213   Level: beginner
214 
215    Notes:
216     Once the error handler is called the calling function is then returned from with the given error code.
217 
218     Experienced users can set the error handler with PetscPushErrorHandler().
219 
220     CHKERRQ(n) is fundamentally a macro replacement for
221          if (n) return(PetscError(...,n,...));
222 
223     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
224     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
225     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
226     a more appropriate construct for using PETSc Error Handling would be
227          if (n) {PetscError(....); return(YourReturnType);}
228 
229    Concepts: error^setting condition
230 
231 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
232 M*/
233 #define CHKERRQ(n)             if (n) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
234 
235 #define CHKERRV(n)             if (n) {n = PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");return;}
236 #define CHKERRABORT(comm,n)    if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");MPI_Abort(comm,n);}
237 #define CHKERRCONTINUE(n)      if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
238 
239 #define CHKFPQ(f) if (f != f) {SETERRQ(PETSC_ERR_FP, "Invalid value: NaN");}
240 
241 /*MC
242    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
243 
244    Not Collective
245 
246    Synopsis:
247    CHKMEMQ;
248 
249   Level: beginner
250 
251    Notes:
252     Must run with the option -malloc_debug to enable this option
253 
254     Once the error handler is called the calling function is then returned from with the given error code.
255 
256     By defaults prints location where memory that is corrupted was allocated.
257 
258     Use CHKMEMA for functions that return void
259 
260    Concepts: memory corruption
261 
262 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
263           PetscMallocValidate()
264 M*/
265 #define CHKMEMQ {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);CHKERRQ(_7_ierr);}
266 
267 #define CHKMEMA {PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);}
268 
269 #if defined(PETSC_UNDERSCORE_CHKERR)
270 extern  PetscErrorCode __gierr;
271 #define _   __gierr =
272 #define ___  CHKERRQ(__gierr);
273 #endif
274 
275 #define               PETSC_EXCEPTIONS_MAX  256
276 extern PetscErrorCode PetscErrorUncatchable[PETSC_EXCEPTIONS_MAX];
277 extern PetscInt       PetscErrorUncatchableCount;
278 extern PetscErrorCode PetscExceptions[PETSC_EXCEPTIONS_MAX];
279 extern PetscInt       PetscExceptionsCount;
280 
281 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscExceptionPush(PetscErrorCode);
282 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscExceptionPop(PetscErrorCode);
283 
284 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorSetCatchable(PetscErrorCode,PetscTruth);
285 EXTERN PetscTruth PETSC_DLLEXPORT PetscErrorIsCatchable(PetscErrorCode);
286 /*MC
287    PetscExceptionCaught - Indicates if a specific exception zierr was caught.
288 
289    Not Collective
290 
291    Synopsis:
292      PetscTruth PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr);
293 
294   Input Parameters:
295   + xierr - error code returned from PetscExceptionTry1() or other PETSc routine
296   - zierr - error code you want it to be
297 
298   Level: advanced
299 
300    Notes:
301     PETSc must not be configured using the option --with-errorchecking=0 for this to work
302 
303     Use PetscExceptionValue() to see if an error code is being "tried"
304 
305   Concepts: exceptions, exception handling
306 
307 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
308           CHKERRQ(), PetscExceptionTry1(), PetscExceptionValue()
309 M*/
310 PETSC_STATIC_INLINE PetscTruth PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr) {
311   PetscInt i;
312   if (xierr != zierr) return PETSC_FALSE;
313   for (i=0; i<PetscErrorUncatchableCount; i++) {
314     if (PetscErrorUncatchable[i] == zierr) {
315       return PETSC_FALSE;
316     }
317   }
318   return PETSC_TRUE;
319 }
320 
321 /*MC
322    PetscExceptionValue - Indicates if the error code is one that is currently being tried
323 
324    Not Collective
325 
326    Synopsis:
327      PetscTruth PetscExceptionValue(PetscErrorCode xierr);
328 
329   Input Parameters:
330   . xierr - error code
331 
332   Level: developer
333 
334    Notes:
335     PETSc must not be configured using the option --with-errorchecking=0 for this to work
336 
337     Use PetscExceptionCaught() to see if the current error code is EXACTLY the one you want
338 
339   Concepts: exceptions, exception hanlding
340 
341 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
342           CHKERRQ(), PetscExceptionTry1(), PetscExceptionCaught()
343 M*/
344 PETSC_STATIC_INLINE PetscTruth PetscExceptionValue(PetscErrorCode zierr) {
345   PetscInt i;
346   for (i=0; i<PetscExceptionsCount; i++) {
347     if (PetscExceptions[i] == zierr) {
348       return PETSC_TRUE;
349     }
350   }
351   return PETSC_FALSE;
352 }
353 
354 /*MC
355    PetscExceptionTry1 - Runs the routine, causing a particular error code to be treated as an exception,
356          rather than an error. That is if that error code is treated the program returns to this level,
357          but does not call the error handlers
358 
359    Not Collective
360 
361    Synopsis:
362      PetscErrorCode PetscExceptionTry1(PetscErrorCode routine(....),PetscErrorCode);
363 
364   Level: advanced
365 
366    No Fortran Equivalent (see PetscExceptionPush() for Fortran)
367 
368    Notes:
369     PETSc must not be configured using the option --with-errorchecking=0 for this to work
370 
371   Note: In general, the outer most try on an exception is the one that will be caught (that is trys down in
372         PETSc code will not usually handle an exception that was issued above). See SNESSolve() for an example
373         of how the local try is ignored if a higher (in the stack) one is also in effect.
374 
375   Concepts: exceptions, exception hanlding
376 
377 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
378           CHKERRQ(), PetscExceptionCaught(), PetscExceptionPush(), PetscExceptionPop()
379 M*/
380 extern PetscErrorCode PetscExceptionTmp;
381 #define PetscExceptionTry1(a,b) (PetscExceptionTmp = PetscExceptionPush(b)) ? PetscExceptionTmp : (PetscExceptionTmp = a, (PetscExceptionPop(b) || PetscExceptionTmp))
382 
383 /*
384    Used by PetscExceptionTrySync(). Returns zierr on ALL processes in comm iff xierr is zierr on at least one process and zero on all others.
385 */
386 PETSC_STATIC_INLINE PetscErrorCode PetscExceptionTrySync_Private(MPI_Comm comm,PetscErrorCode xierr,PetscErrorCode zierr)
387 {
388   PetscReal      in[2],out[2];
389   PetscErrorCode ierr;
390 
391   if (xierr != zierr) return xierr;
392 
393   in[0] = xierr;
394   in[1] = 0.0;   /* dummy value */
395 
396   ierr = MPI_Allreduce(in,out,2,MPIU_REAL,0,comm); if (ierr) {;}
397   return xierr;
398 }
399 
400 /*MC
401    PetscExceptionTrySyncNorm - Runs the routine, causing a particular error code to be treated as an exception,
402          rather than an error. That is if that error code is treated the program returns to this level,
403          but does not call the error handlers
404 
405      Collective on Comm
406 
407    Synopsis:
408      PetscExceptionTrySyncNorm(MPI_Comm comm,PetscErrorCode routine(....),PetscErrorCode);
409 
410   Level: advanced
411 
412    Notes: This synchronizes the error code across all processes in the communicator IF the code matches PetscErrorCode. The next
413      call with an MPI_Reduce()/MPI_Allreduce() MUST be VecNorm() [We can added VecDot() and maybe others as needed].
414 
415     PETSc must not be configured using the option --with-errorchecking=0 for this to work
416 
417   Note: In general, the outer most try on an exception is the one that will be caught (that is trys down in
418         PETSc code will not usually handle an exception that was issued above). See SNESSolve() for an example
419         of how the local try is ignored if a higher (in the stack) one is also in effect.
420 
421   Concepts: exceptions, exception hanlding
422 
423 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
424           CHKERRQ(), PetscExceptionCaught(), PetscExceptionPush(), PetscExceptionPop(), PetscExceptionTry1()
425 M*/
426 extern PetscErrorCode PetscExceptionTmp;
427 #define PetscExceptionTrySyncNorm(comm,a,b) (PetscExceptionTmp = PetscExceptionPush(b)) ? PetscExceptionTmp : \
428                                         (PetscExceptionTmp = a , PetscExceptionPop(b),PetscExceptionTrySyncNorm_Private(comm,PetscExceptionTmp,b))
429 
430 #else
431 
432 /*
433     These are defined to be empty for when error checking is turned off, with config/configure.py --with-errorchecking=0
434 */
435 
436 #define SETERRQ(n,s) ;
437 #define SETERRQ1(n,s,a1) ;
438 #define SETERRQ2(n,s,a1,a2) ;
439 #define SETERRQ3(n,s,a1,a2,a3) ;
440 #define SETERRQ4(n,s,a1,a2,a3,a4) ;
441 #define SETERRQ5(n,s,a1,a2,a3,a4,a5) ;
442 #define SETERRQ6(n,s,a1,a2,a3,a4,a5,a6) ;
443 #define SETERRABORT(comm,n,s) ;
444 
445 #define CHKERRQ(n)     ;
446 #define CHKERRABORT(comm,n) ;
447 #define CHKERRCONTINUE(n) ;
448 #define CHKFPQ(f) ;
449 #define CHKMEMQ        ;
450 
451 #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
452 #define _
453 #define ___
454 #endif
455 
456 #define PetscExceptionPush(a)                0
457 #define PetscExceptionPop(a)                 0
458 #define PetscErrorSetCatchable(a,b)          0
459 #define PetscErrorIsCatchable(a)             PETSC_FALSE
460 
461 #define PetscExceptionCaught(a,b)            PETSC_FALSE
462 #define PetscExceptionValue(a)               PETSC_FALSE
463 #define PetscExceptionTry1(a,b)              a
464 #define PetscExceptionTrySyncNorm(comm,a,b)  a
465 
466 #endif
467 
468 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorPrintfInitialize(void);
469 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorMessage(int,const char*[],char **);
470 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscTraceBackErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
471 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscIgnoreErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
472 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscEmacsClientErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
473 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscMPIAbortErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
474 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscAbortErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
475 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscAttachDebuggerErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
476 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscReturnErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
477 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscError(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,...) PETSC_PRINTF_FORMAT_CHECK(7,8);
478 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPushErrorHandler(PetscErrorCode (*handler)(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*),void*);
479 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPopErrorHandler(void);
480 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscDefaultSignalHandler(int,void*);
481 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
482 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPopSignalHandler(void);
483 
484 typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
485 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscSetFPTrap(PetscFPTrap);
486 
487 /*
488       Allows the code to build a stack frame as it runs
489 */
490 #if defined(PETSC_USE_DEBUG)
491 
492 #define PETSCSTACKSIZE 15
493 
494 typedef struct  {
495   const char *function[PETSCSTACKSIZE];
496   const char *file[PETSCSTACKSIZE];
497   const char *directory[PETSCSTACKSIZE];
498         int  line[PETSCSTACKSIZE];
499         int currentsize;
500 } PetscStack;
501 
502 extern PETSC_DLLEXPORT PetscStack *petscstack;
503 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackCopy(PetscStack*,PetscStack*);
504 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackPrint(PetscStack*,FILE* fp);
505 
506 #define PetscStackActive (petscstack != 0)
507 
508 
509 /*MC
510    PetscFunctionBegin - First executable line of each PETSc function
511         used for error handling.
512 
513    Synopsis:
514    void PetscFunctionBegin;
515 
516    Usage:
517 .vb
518      int something;
519 
520      PetscFunctionBegin;
521 .ve
522 
523    Notes:
524      Not available in Fortran
525 
526    Level: developer
527 
528 .seealso: PetscFunctionReturn()
529 
530 .keywords: traceback, error handling
531 M*/
532 #define PetscFunctionBegin \
533   {\
534    if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
535     petscstack->function[petscstack->currentsize]  = __FUNCT__; \
536     petscstack->file[petscstack->currentsize]      = __FILE__; \
537     petscstack->directory[petscstack->currentsize] = __SDIR__; \
538     petscstack->line[petscstack->currentsize]      = __LINE__; \
539     petscstack->currentsize++; \
540   }}
541 
542 #define PetscStackPush(n) \
543   {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
544     petscstack->function[petscstack->currentsize]  = n; \
545     petscstack->file[petscstack->currentsize]      = "unknown"; \
546     petscstack->directory[petscstack->currentsize] = "unknown"; \
547     petscstack->line[petscstack->currentsize]      = 0; \
548     petscstack->currentsize++; \
549   }}
550 
551 #define PetscStackPop \
552   {if (petscstack && petscstack->currentsize > 0) {     \
553     petscstack->currentsize--; \
554     petscstack->function[petscstack->currentsize]  = 0; \
555     petscstack->file[petscstack->currentsize]      = 0; \
556     petscstack->directory[petscstack->currentsize] = 0; \
557     petscstack->line[petscstack->currentsize]      = 0; \
558   }};
559 
560 /*MC
561    PetscFunctionReturn - Last executable line of each PETSc function
562         used for error handling. Replaces return()
563 
564    Synopsis:
565    void PetscFunctionReturn(0);
566 
567    Usage:
568 .vb
569     ....
570      PetscFunctionReturn(0);
571    }
572 .ve
573 
574    Notes:
575      Not available in Fortran
576 
577    Level: developer
578 
579 .seealso: PetscFunctionBegin()
580 
581 .keywords: traceback, error handling
582 M*/
583 #define PetscFunctionReturn(a) \
584   {\
585   PetscStackPop; \
586   return(a);}
587 
588 #define PetscFunctionReturnVoid() \
589   {\
590   PetscStackPop; \
591   return;}
592 
593 
594 #else
595 
596 #define PetscFunctionBegin
597 #define PetscFunctionReturn(a)  return(a)
598 #define PetscFunctionReturnVoid() return
599 #define PetscStackPop
600 #define PetscStackPush(f)
601 #define PetscStackActive        0
602 
603 #endif
604 
605 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackCreate(void);
606 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackView(PetscViewer);
607 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackDestroy(void);
608 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackPublish(void);
609 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackDepublish(void);
610 
611 
612 PETSC_EXTERN_CXX_END
613 #endif
614