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