xref: /petsc/include/petscerror.h (revision 704ba839cd484dc2900d48fe4907f851bfd76db1)
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_TYPENOTSET   89   /* the type of the object has not yet been set */
64 #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
65 #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
66 #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
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        90  /* 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     In Fortran MPI_Abort() is always called
101 
102     Experienced users can set the error handler with PetscPushErrorHandler().
103 
104    Concepts: error^setting condition
105 
106 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
107 M*/
108 #define SETERRQ(n,s)              {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);}
109 
110 /*MC
111    SETERRQ1 - Macro that is called when an error has been detected,
112 
113    Not Collective
114 
115    Synopsis:
116    PetscErrorCode SETERRQ1(PetscErrorCode errorcode,char *formatmessage,arg)
117 
118 
119    Input Parameters:
120 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
121 .  message - error message in the printf format
122 -  arg - argument (for example an integer, string or double)
123 
124   Level: beginner
125 
126    Notes:
127     Once the error handler is called the calling function is then returned from with the given error code.
128 
129    Experienced users can set the error handler with PetscPushErrorHandler().
130 
131    Concepts: error^setting condition
132 
133 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
134 M*/
135 #define SETERRQ1(n,s,a1)          {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1);}
136 
137 /*MC
138    SETERRQ2 - Macro that is called when an error has been detected,
139 
140    Not Collective
141 
142    Synopsis:
143    PetscErrorCode SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
144 
145 
146    Input Parameters:
147 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
148 .  message - error message in the printf format
149 .  arg1 - argument (for example an integer, string or double)
150 -  arg2 - argument (for example an integer, string or double)
151 
152   Level: beginner
153 
154    Notes:
155     Once the error handler is called the calling function is then returned from with the given error code.
156 
157    Experienced users can set the error handler with PetscPushErrorHandler().
158 
159    Concepts: error^setting condition
160 
161 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
162 M*/
163 #define SETERRQ2(n,s,a1,a2)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2);}
164 
165 /*MC
166    SETERRQ3 - Macro that is called when an error has been detected,
167 
168    Not Collective
169 
170    Synopsis:
171    PetscErrorCode SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)
172 
173 
174    Input Parameters:
175 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
176 .  message - error message in the printf format
177 .  arg1 - argument (for example an integer, string or double)
178 .  arg2 - argument (for example an integer, string or double)
179 -  arg3 - argument (for example an integer, string or double)
180 
181   Level: beginner
182 
183    Notes:
184     Once the error handler is called the calling function is then returned from with the given error code.
185 
186     There are also versions for 4, 5, 6 and 7 arguments.
187 
188    Experienced users can set the error handler with PetscPushErrorHandler().
189 
190    Concepts: error^setting condition
191 
192 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
193 M*/
194 #define SETERRQ3(n,s,a1,a2,a3)    {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3);}
195 
196 #define SETERRQ4(n,s,a1,a2,a3,a4) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4);}
197 #define SETERRQ5(n,s,a1,a2,a3,a4,a5)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5);}
198 #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);}
199 #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);}
200 #define SETERRABORT(comm,n,s)     {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);MPI_Abort(comm,n);}
201 
202 /*MC
203    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
204 
205    Not Collective
206 
207    Synopsis:
208    PetscErrorCode CHKERRQ(PetscErrorCode errorcode)
209 
210 
211    Input Parameters:
212 .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
213 
214   Level: beginner
215 
216    Notes:
217     Once the error handler is called the calling function is then returned from with the given error code.
218 
219     Experienced users can set the error handler with PetscPushErrorHandler().
220 
221     CHKERRQ(n) is fundamentally a macro replacement for
222          if (n) return(PetscError(...,n,...));
223 
224     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
225     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
226     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
227     you can use CHKERRV() which returns without an error code (bad idea since the error is ignored or
228          if (n) {PetscError(....); return(YourReturnType);}
229     where you may pass back a PETSC_NULL to indicate an error. You can also call CHKERRABORT(comm,n) to have
230     MPI_Abort() returned immediately.
231 
232     In Fortran MPI_Abort() is always called
233 
234    Concepts: error^setting condition
235 
236 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
237 M*/
238 #define CHKERRQ(n)             if (n) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
239 
240 #define CHKERRV(n)             if (n) {n = PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");return;}
241 #define CHKERRABORT(comm,n)    if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");MPI_Abort(comm,n);}
242 #define CHKERRCONTINUE(n)      if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
243 
244 #ifdef PETSC_CLANGUAGE_CXX
245 
246 #define CHKERRXX(n)            if (n) {PetscErrorCxx(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0);}
247 
248 #endif
249 
250 /*MC
251    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
252 
253    Not Collective
254 
255    Synopsis:
256    CHKMEMQ;
257 
258   Level: beginner
259 
260    Notes:
261     Must run with the option -malloc_debug to enable this option
262 
263     Once the error handler is called the calling function is then returned from with the given error code.
264 
265     By defaults prints location where memory that is corrupted was allocated.
266 
267     Use CHKMEMA for functions that return void
268 
269    Concepts: memory corruption
270 
271 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
272           PetscMallocValidate()
273 M*/
274 #define CHKMEMQ {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);CHKERRQ(_7_ierr);}
275 
276 #define CHKMEMA {PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);}
277 
278 #if defined(PETSC_UNDERSCORE_CHKERR)
279 extern  PetscErrorCode __gierr;
280 #define _   __gierr =
281 #define ___  CHKERRQ(__gierr);
282 #endif
283 
284 #define               PETSC_EXCEPTIONS_MAX  256
285 extern PetscErrorCode PetscErrorUncatchable[PETSC_EXCEPTIONS_MAX];
286 extern PetscInt       PetscErrorUncatchableCount;
287 extern PetscErrorCode PetscExceptions[PETSC_EXCEPTIONS_MAX];
288 extern PetscInt       PetscExceptionsCount;
289 
290 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscExceptionPush(PetscErrorCode);
291 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscExceptionPop(PetscErrorCode);
292 
293 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorSetCatchable(PetscErrorCode,PetscTruth);
294 EXTERN PetscTruth PETSC_DLLEXPORT PetscErrorIsCatchable(PetscErrorCode);
295 /*MC
296    PetscExceptionCaught - Indicates if a specific exception zierr was caught.
297 
298    Not Collective
299 
300    Synopsis:
301      PetscTruth PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr);
302 
303   Input Parameters:
304   + xierr - error code returned from PetscExceptionTry1() or other PETSc routine
305   - zierr - error code you want it to be
306 
307   Level: advanced
308 
309    Notes:
310     PETSc must not be configured using the option --with-errorchecking=0 for this to work
311 
312     Use PetscExceptionValue() to see if an error code is being "tried"
313 
314   Concepts: exceptions, exception handling
315 
316 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
317           CHKERRQ(), PetscExceptionTry1(), PetscExceptionValue()
318 M*/
319 PETSC_STATIC_INLINE PetscTruth PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr) {
320   PetscInt i;
321   if (xierr != zierr) return PETSC_FALSE;
322   for (i=0; i<PetscErrorUncatchableCount; i++) {
323     if (PetscErrorUncatchable[i] == zierr) {
324       return PETSC_FALSE;
325     }
326   }
327   return PETSC_TRUE;
328 }
329 
330 /*MC
331    PetscExceptionValue - Indicates if the error code is one that is currently being tried
332 
333    Not Collective
334 
335    Synopsis:
336      PetscTruth PetscExceptionValue(PetscErrorCode xierr);
337 
338   Input Parameters:
339   . xierr - error code
340 
341   Level: developer
342 
343    Notes:
344     PETSc must not be configured using the option --with-errorchecking=0 for this to work
345 
346     Use PetscExceptionCaught() to see if the current error code is EXACTLY the one you want
347 
348   Concepts: exceptions, exception hanlding
349 
350 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
351           CHKERRQ(), PetscExceptionTry1(), PetscExceptionCaught()
352 M*/
353 PETSC_STATIC_INLINE PetscTruth PetscExceptionValue(PetscErrorCode zierr) {
354   PetscInt i;
355   for (i=0; i<PetscExceptionsCount; i++) {
356     if (PetscExceptions[i] == zierr) {
357       return PETSC_TRUE;
358     }
359   }
360   return PETSC_FALSE;
361 }
362 
363 /*MC
364    PetscExceptionTry1 - Runs the routine, causing a particular error code to be treated as an exception,
365          rather than an error. That is if that error code is treated the program returns to this level,
366          but does not call the error handlers
367 
368    Not Collective
369 
370    Synopsis:
371      PetscErrorCode PetscExceptionTry1(PetscErrorCode routine(....),PetscErrorCode);
372 
373   Level: advanced
374 
375    No Fortran Equivalent (see PetscExceptionPush() for Fortran)
376 
377    Notes:
378     PETSc must not be configured using the option --with-errorchecking=0 for this to work
379 
380   Note: In general, the outer most try on an exception is the one that will be caught (that is trys down in
381         PETSc code will not usually handle an exception that was issued above). See SNESSolve() for an example
382         of how the local try is ignored if a higher (in the stack) one is also in effect.
383 
384   Concepts: exceptions, exception hanlding
385 
386 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
387           CHKERRQ(), PetscExceptionCaught(), PetscExceptionPush(), PetscExceptionPop()
388 M*/
389 extern PetscErrorCode PetscExceptionTmp,PetscExceptionTmp1;
390 #define PetscExceptionTry1(a,b) (PetscExceptionTmp1 = PetscExceptionPush(b)) ? PetscExceptionTmp1 : (PetscExceptionTmp1 = a, (PetscExceptionTmp = PetscExceptionPop(b)) ? PetscExceptionTmp : PetscExceptionTmp1)
391 
392 /*
393    Used by PetscExceptionTrySync(). Returns zierr on ALL processes in comm iff xierr is zierr on at least one process and zero on all others.
394 */
395 PETSC_STATIC_INLINE PetscErrorCode PetscExceptionTrySync_Private(MPI_Comm comm,PetscErrorCode xierr,PetscErrorCode zierr)
396 {
397   PetscReal      in[2],out[2];
398   PetscErrorCode ierr;
399 
400   if (xierr != zierr) return xierr;
401 
402   in[0] = xierr;
403   in[1] = 0.0;   /* dummy value */
404 
405   ierr = MPI_Allreduce(in,out,2,MPIU_REAL,0,comm); if (ierr) {;}
406   return xierr;
407 }
408 
409 /*MC
410    PetscExceptionTrySyncNorm - Runs the routine, causing a particular error code to be treated as an exception,
411          rather than an error. That is if that error code is treated the program returns to this level,
412          but does not call the error handlers
413 
414      Collective on Comm
415 
416    Synopsis:
417      PetscExceptionTrySyncNorm(MPI_Comm comm,PetscErrorCode routine(....),PetscErrorCode);
418 
419   Level: advanced
420 
421    Notes: This synchronizes the error code across all processes in the communicator IF the code matches PetscErrorCode. The next
422      call with an MPI_Reduce()/MPI_Allreduce() MUST be VecNorm() [We can added VecDot() and maybe others as needed].
423 
424     PETSc must not be configured using the option --with-errorchecking=0 for this to work
425 
426   Note: In general, the outer most try on an exception is the one that will be caught (that is trys down in
427         PETSc code will not usually handle an exception that was issued above). See SNESSolve() for an example
428         of how the local try is ignored if a higher (in the stack) one is also in effect.
429 
430   Concepts: exceptions, exception hanlding
431 
432 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
433           CHKERRQ(), PetscExceptionCaught(), PetscExceptionPush(), PetscExceptionPop(), PetscExceptionTry1()
434 M*/
435 extern PetscErrorCode PetscExceptionTmp;
436 #define PetscExceptionTrySyncNorm(comm,a,b) (PetscExceptionTmp = PetscExceptionPush(b)) ? PetscExceptionTmp : \
437                                         (PetscExceptionTmp = a , PetscExceptionPop(b),PetscExceptionTrySyncNorm_Private(comm,PetscExceptionTmp,b))
438 
439 #else
440 
441 /*
442     These are defined to be empty for when error checking is turned off, with config/configure.py --with-errorchecking=0
443 */
444 
445 #define SETERRQ(n,s) ;
446 #define SETERRQ1(n,s,a1) ;
447 #define SETERRQ2(n,s,a1,a2) ;
448 #define SETERRQ3(n,s,a1,a2,a3) ;
449 #define SETERRQ4(n,s,a1,a2,a3,a4) ;
450 #define SETERRQ5(n,s,a1,a2,a3,a4,a5) ;
451 #define SETERRQ6(n,s,a1,a2,a3,a4,a5,a6) ;
452 #define SETERRABORT(comm,n,s) ;
453 
454 #define CHKERRQ(n)     ;
455 #define CHKERRABORT(comm,n) ;
456 #define CHKERRCONTINUE(n) ;
457 #define CHKMEMQ        ;
458 
459 #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
460 #define _
461 #define ___
462 #endif
463 
464 #define PetscExceptionPush(a)                0
465 #define PetscExceptionPop(a)                 0
466 #define PetscErrorSetCatchable(a,b)          0
467 #define PetscErrorIsCatchable(a)             PETSC_FALSE
468 
469 #define PetscExceptionCaught(a,b)            PETSC_FALSE
470 #define PetscExceptionValue(a)               PETSC_FALSE
471 #define PetscExceptionTry1(a,b)              a
472 #define PetscExceptionTrySyncNorm(comm,a,b)  a
473 
474 #endif
475 
476 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorPrintfInitialize(void);
477 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorMessage(int,const char*[],char **);
478 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscTraceBackErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
479 #ifdef PETSC_CLANGUAGE_CXX
480 #include <sstream>
481 EXTERN void           PETSC_DLLEXPORT PetscTraceBackErrorHandlerCxx(int,const char *,const char *,const char *,PetscErrorCode,int, std::ostringstream&);
482 #endif
483 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscIgnoreErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
484 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscEmacsClientErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
485 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscMPIAbortErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
486 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscAbortErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
487 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscAttachDebuggerErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
488 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscReturnErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
489 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscError(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,...) PETSC_PRINTF_FORMAT_CHECK(7,8);
490 EXTERN void           PETSC_DLLEXPORT PetscErrorCxx(int,const char*,const char*,const char*,PetscErrorCode,int);
491 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPushErrorHandler(PetscErrorCode (*handler)(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*),void*);
492 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPopErrorHandler(void);
493 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscDefaultSignalHandler(int,void*);
494 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
495 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPopSignalHandler(void);
496 
497 typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
498 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscSetFPTrap(PetscFPTrap);
499 
500 /*
501       Allows the code to build a stack frame as it runs
502 */
503 #if defined(PETSC_USE_DEBUG)
504 
505 #define PETSCSTACKSIZE 15
506 
507 typedef struct  {
508   const char *function[PETSCSTACKSIZE];
509   const char *file[PETSCSTACKSIZE];
510   const char *directory[PETSCSTACKSIZE];
511         int  line[PETSCSTACKSIZE];
512         int currentsize;
513 } PetscStack;
514 
515 extern PETSC_DLLEXPORT PetscStack *petscstack;
516 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackCopy(PetscStack*,PetscStack*);
517 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackPrint(PetscStack*,FILE* fp);
518 
519 #define PetscStackActive (petscstack != 0)
520 
521 
522 /*MC
523    PetscFunctionBegin - First executable line of each PETSc function
524         used for error handling.
525 
526    Synopsis:
527    void PetscFunctionBegin;
528 
529    Usage:
530 .vb
531      int something;
532 
533      PetscFunctionBegin;
534 .ve
535 
536    Notes:
537      Not available in Fortran
538 
539    Level: developer
540 
541 .seealso: PetscFunctionReturn()
542 
543 .keywords: traceback, error handling
544 M*/
545 #define PetscFunctionBegin \
546   {\
547    if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
548     petscstack->function[petscstack->currentsize]  = __FUNCT__; \
549     petscstack->file[petscstack->currentsize]      = __FILE__; \
550     petscstack->directory[petscstack->currentsize] = __SDIR__; \
551     petscstack->line[petscstack->currentsize]      = __LINE__; \
552     petscstack->currentsize++; \
553   }}
554 
555 #define PetscStackPush(n) \
556   {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
557     petscstack->function[petscstack->currentsize]  = n; \
558     petscstack->file[petscstack->currentsize]      = "unknown"; \
559     petscstack->directory[petscstack->currentsize] = "unknown"; \
560     petscstack->line[petscstack->currentsize]      = 0; \
561     petscstack->currentsize++; \
562   }}
563 
564 #define PetscStackPop \
565   {if (petscstack && petscstack->currentsize > 0) {     \
566     petscstack->currentsize--; \
567     petscstack->function[petscstack->currentsize]  = 0; \
568     petscstack->file[petscstack->currentsize]      = 0; \
569     petscstack->directory[petscstack->currentsize] = 0; \
570     petscstack->line[petscstack->currentsize]      = 0; \
571   }};
572 
573 /*MC
574    PetscFunctionReturn - Last executable line of each PETSc function
575         used for error handling. Replaces return()
576 
577    Synopsis:
578    void PetscFunctionReturn(0);
579 
580    Usage:
581 .vb
582     ....
583      PetscFunctionReturn(0);
584    }
585 .ve
586 
587    Notes:
588      Not available in Fortran
589 
590    Level: developer
591 
592 .seealso: PetscFunctionBegin()
593 
594 .keywords: traceback, error handling
595 M*/
596 #define PetscFunctionReturn(a) \
597   {\
598   PetscStackPop; \
599   return(a);}
600 
601 #define PetscFunctionReturnVoid() \
602   {\
603   PetscStackPop; \
604   return;}
605 
606 
607 #else
608 
609 #define PetscFunctionBegin
610 #define PetscFunctionReturn(a)  return(a)
611 #define PetscFunctionReturnVoid() return
612 #define PetscStackPop
613 #define PetscStackPush(f)
614 #define PetscStackActive        0
615 
616 #endif
617 
618 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackCreate(void);
619 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackView(PetscViewer);
620 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackDestroy(void);
621 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackPublish(void);
622 EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackDepublish(void);
623 
624 
625 PETSC_EXTERN_CXX_END
626 #endif
627