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