xref: /petsc/include/petscerror.h (revision fcfd50eb5fbe12291db06fc1b33ad004ead64926)
154a8ef01SBarry Smith /*
2f621e05eSBarry Smith     Contains all error handling interfaces for PETSc.
354a8ef01SBarry Smith */
445d48df9SBarry Smith #if !defined(__PETSCERROR_H)
545d48df9SBarry Smith #define __PETSCERROR_H
66c7e564aSBarry Smith 
7*fcfd50ebSBarry Smith #if defined(PETSC_HAVE_STRING_H)
8*fcfd50ebSBarry Smith #include <string.h> /* for strcmp */
9*fcfd50ebSBarry Smith #endif
10*fcfd50ebSBarry Smith 
11e9fa29b7SSatish Balay PETSC_EXTERN_CXX_BEGIN
12c22c1629SBarry Smith 
1354a8ef01SBarry Smith /*
1454a8ef01SBarry Smith    Defines the directory where the compiled source is located; used
155e97870eSBarry Smith    in printing error messages. Each makefile has an entry
165e97870eSBarry Smith    LOCDIR	  =  thedirectory
17330cf3c9SBarry Smith    and bmake/common_variables includes in CCPPFLAGS -D__SDIR__=${LOCDIR}
18f621e05eSBarry Smith    which is a flag passed to the C/C++ compilers. This declaration below
19f621e05eSBarry Smith    is only needed if some code is compiled without the -D__SDIR__
2054a8ef01SBarry Smith */
21330cf3c9SBarry Smith #if !defined(__INSDIR__)
22330cf3c9SBarry Smith #define __INSDIR__ "unknowndirectory/"
2354a8ef01SBarry Smith #endif
2454a8ef01SBarry Smith 
2554a8ef01SBarry Smith /*
264f227f7cSBarry Smith    Defines the function where the compiled source is located; used
27f621e05eSBarry Smith    in printing error messages. This is defined here in case the user
28f621e05eSBarry Smith    does not declare it.
294f227f7cSBarry Smith */
304a2ae208SSatish Balay #if !defined(__FUNCT__)
31da9b6338SBarry Smith #define __FUNCT__ "User provided function"
324f227f7cSBarry Smith #endif
334f227f7cSBarry Smith 
344f227f7cSBarry Smith /*
35329ffe3dSLois Curfman McInnes      These are the generic error codes. These error codes are used
36e2d1d2b7SBarry Smith      many different places in the PETSc source code. The string versions are
370e5e90baSSatish Balay      at src/sys/error/err.c any changes here must also be made there
380f9cf654SBarry Smith      These are also define in include/finclude/petscerror.h any CHANGES here
390f9cf654SBarry Smith      must be also made there.
4045d48df9SBarry Smith 
4154a8ef01SBarry Smith */
422a6744ebSBarry Smith #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */
432a6744ebSBarry Smith 
4445d48df9SBarry Smith #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
4547794344SBarry Smith #define PETSC_ERR_SUP              56   /* no support for requested operation */
46e2d1d2b7SBarry Smith #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
47e2d1d2b7SBarry Smith #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
4845d48df9SBarry Smith #define PETSC_ERR_SIG              59   /* signal received */
49f1caa5a4SBarry Smith #define PETSC_ERR_FP               72   /* floating point exception */
50a8c6a408SBarry Smith #define PETSC_ERR_COR              74   /* corrupted PETSc object */
51a8c6a408SBarry Smith #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
52329ffe3dSLois Curfman McInnes #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
53329ffe3dSLois Curfman McInnes #define PETSC_ERR_MEMC             78   /* memory corruption */
54b3cc6726SBarry Smith #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
551302d50aSBarry Smith #define PETSC_ERR_USER             83   /* user has not provided needed function */
564e2ffeddSBarry Smith #define PETSC_ERR_SYS              88   /* error in system call */
5745d48df9SBarry Smith 
5845d48df9SBarry Smith #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
5945d48df9SBarry Smith #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
60a8c6a408SBarry Smith #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
6145d48df9SBarry Smith #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
6245d48df9SBarry Smith #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
634f227f7cSBarry Smith #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
644f227f7cSBarry Smith #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
656831982aSBarry Smith #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
66d252947aSBarry Smith #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
678cda6cd7SBarry Smith #define PETSC_ERR_ARG_TYPENOTSET   89   /* the type of the object has not yet been set */
68a8c6a408SBarry Smith #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
694482741eSBarry Smith #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
70958c9bccSBarry Smith #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
714f227f7cSBarry Smith 
724f227f7cSBarry Smith #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
734f227f7cSBarry Smith #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
744f227f7cSBarry Smith #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
75a8c6a408SBarry Smith #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
7645d48df9SBarry Smith 
77329ffe3dSLois Curfman McInnes #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
789e3b2f23SBarry Smith #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
7954a8ef01SBarry Smith 
80bf3909cdSBarry Smith #define PETSC_ERR_FLOP_COUNT       90
81e113a28aSBarry Smith #define PETSC_ERR_NOT_CONVERGED    91  /* solver did not converge */
82e113a28aSBarry Smith #define PETSC_ERR_MAX_VALUE        92  /* this is always the one more than the largest error code */
832a6744ebSBarry Smith 
8459aaf355SLisandro Dalcin #define PetscStringizeArg(a) #a
8559aaf355SLisandro Dalcin #define PetscStringize(a) PetscStringizeArg(a)
86330cf3c9SBarry Smith #define __SDIR__ PetscStringize(__INSDIR__)
87330cf3c9SBarry Smith 
88e8b7e333SSatish Balay #if defined(PETSC_USE_ERRORCHECKING)
89e8b7e333SSatish Balay 
9030de9b25SBarry Smith /*MC
9130de9b25SBarry Smith    SETERRQ - Macro that is called when an error has been detected,
9230de9b25SBarry Smith 
9330de9b25SBarry Smith    Synopsis:
94e7e72b3dSBarry Smith    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode errorcode,char *message)
9530de9b25SBarry Smith 
96eca87e8dSBarry Smith    Not Collective
9730de9b25SBarry Smith 
9830de9b25SBarry Smith    Input Parameters:
9930de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
10030de9b25SBarry Smith -  message - error message
10130de9b25SBarry Smith 
10230de9b25SBarry Smith   Level: beginner
10330de9b25SBarry Smith 
10430de9b25SBarry Smith    Notes:
10530de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
10630de9b25SBarry Smith 
10730de9b25SBarry Smith     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
10830de9b25SBarry Smith 
10958ebbce7SBarry Smith     In Fortran MPI_Abort() is always called
11030de9b25SBarry Smith 
11130de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
11230de9b25SBarry Smith 
11330de9b25SBarry Smith    Concepts: error^setting condition
11430de9b25SBarry Smith 
11591d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
11630de9b25SBarry Smith M*/
11753c77d0aSJed Brown #define SETERRQ(comm,n,s)              return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s)
11830de9b25SBarry Smith 
11930de9b25SBarry Smith /*MC
12030de9b25SBarry Smith    SETERRQ1 - Macro that is called when an error has been detected,
12130de9b25SBarry Smith 
12230de9b25SBarry Smith    Synopsis:
123e32f2f54SBarry Smith    PetscErrorCode SETERRQ1(MPI_Comm comm,PetscErrorCode errorcode,char *formatmessage,arg)
12430de9b25SBarry Smith 
125eca87e8dSBarry Smith    Not Collective
12630de9b25SBarry Smith 
12730de9b25SBarry Smith    Input Parameters:
12830de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
12930de9b25SBarry Smith .  message - error message in the printf format
13030de9b25SBarry Smith -  arg - argument (for example an integer, string or double)
13130de9b25SBarry Smith 
13230de9b25SBarry Smith   Level: beginner
13330de9b25SBarry Smith 
13430de9b25SBarry Smith    Notes:
13530de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
13630de9b25SBarry Smith 
13730de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
13830de9b25SBarry Smith 
13930de9b25SBarry Smith    Concepts: error^setting condition
14030de9b25SBarry Smith 
14191d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
14230de9b25SBarry Smith M*/
14353c77d0aSJed Brown #define SETERRQ1(comm,n,s,a1)          return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1)
14430de9b25SBarry Smith 
14530de9b25SBarry Smith /*MC
14630de9b25SBarry Smith    SETERRQ2 - Macro that is called when an error has been detected,
14730de9b25SBarry Smith 
14830de9b25SBarry Smith    Synopsis:
149f4442326SMatthew Knepley    PetscErrorCode SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
15030de9b25SBarry Smith 
151eca87e8dSBarry Smith    Not Collective
15230de9b25SBarry Smith 
15330de9b25SBarry Smith    Input Parameters:
15430de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
15530de9b25SBarry Smith .  message - error message in the printf format
15630de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
15730de9b25SBarry Smith -  arg2 - argument (for example an integer, string or double)
15830de9b25SBarry Smith 
15930de9b25SBarry Smith   Level: beginner
16030de9b25SBarry Smith 
16130de9b25SBarry Smith    Notes:
16230de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
16330de9b25SBarry Smith 
16430de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
16530de9b25SBarry Smith 
16630de9b25SBarry Smith    Concepts: error^setting condition
16730de9b25SBarry Smith 
1686024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
16930de9b25SBarry Smith M*/
17053c77d0aSJed Brown #define SETERRQ2(comm,n,s,a1,a2)       return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2)
17130de9b25SBarry Smith 
17230de9b25SBarry Smith /*MC
17330de9b25SBarry Smith    SETERRQ3 - Macro that is called when an error has been detected,
17430de9b25SBarry Smith 
17530de9b25SBarry Smith    Synopsis:
176f4442326SMatthew Knepley    PetscErrorCode SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)
17730de9b25SBarry Smith 
178eca87e8dSBarry Smith    Not Collective
17930de9b25SBarry Smith 
18030de9b25SBarry Smith    Input Parameters:
18130de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
18230de9b25SBarry Smith .  message - error message in the printf format
18330de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
18430de9b25SBarry Smith .  arg2 - argument (for example an integer, string or double)
18530de9b25SBarry Smith -  arg3 - argument (for example an integer, string or double)
18630de9b25SBarry Smith 
18730de9b25SBarry Smith   Level: beginner
18830de9b25SBarry Smith 
18930de9b25SBarry Smith    Notes:
19030de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
19130de9b25SBarry Smith 
192f621e05eSBarry Smith     There are also versions for 4, 5, 6 and 7 arguments.
193f621e05eSBarry Smith 
19430de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
19530de9b25SBarry Smith 
19630de9b25SBarry Smith    Concepts: error^setting condition
19730de9b25SBarry Smith 
1986024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
19930de9b25SBarry Smith M*/
20053c77d0aSJed Brown #define SETERRQ3(comm,n,s,a1,a2,a3)    return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3)
20130de9b25SBarry Smith 
20253c77d0aSJed Brown #define SETERRQ4(comm,n,s,a1,a2,a3,a4) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4)
20353c77d0aSJed Brown #define SETERRQ5(comm,n,s,a1,a2,a3,a4,a5)       return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5)
20453c77d0aSJed Brown #define SETERRQ6(comm,n,s,a1,a2,a3,a4,a5,a6)    return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6)
20553c77d0aSJed Brown #define SETERRQ7(comm,n,s,a1,a2,a3,a4,a5,a6,a7) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6,a7)
20653c77d0aSJed Brown #define SETERRABORT(comm,n,s)     do {PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s);MPI_Abort(comm,n);} while (0)
2079a00fa46SSatish Balay 
20830de9b25SBarry Smith /*MC
20930de9b25SBarry Smith    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
21030de9b25SBarry Smith 
21130de9b25SBarry Smith    Synopsis:
212f4442326SMatthew Knepley    PetscErrorCode CHKERRQ(PetscErrorCode errorcode)
21330de9b25SBarry Smith 
214eca87e8dSBarry Smith    Not Collective
21530de9b25SBarry Smith 
21630de9b25SBarry Smith    Input Parameters:
21730de9b25SBarry Smith .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
21830de9b25SBarry Smith 
21930de9b25SBarry Smith   Level: beginner
22030de9b25SBarry Smith 
22130de9b25SBarry Smith    Notes:
22230de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
22330de9b25SBarry Smith 
22430de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
2255324ea47SKris Buschelman 
226fcecf507SKris Buschelman     CHKERRQ(n) is fundamentally a macro replacement for
2275324ea47SKris Buschelman          if (n) return(PetscError(...,n,...));
2285324ea47SKris Buschelman 
2295324ea47SKris Buschelman     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
2305324ea47SKris Buschelman     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
2315324ea47SKris Buschelman     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
23258ebbce7SBarry Smith     you can use CHKERRV() which returns without an error code (bad idea since the error is ignored or
2335324ea47SKris Buschelman          if (n) {PetscError(....); return(YourReturnType);}
23458ebbce7SBarry Smith     where you may pass back a PETSC_NULL to indicate an error. You can also call CHKERRABORT(comm,n) to have
23558ebbce7SBarry Smith     MPI_Abort() returned immediately.
23658ebbce7SBarry Smith 
23758ebbce7SBarry Smith     In Fortran MPI_Abort() is always called
23830de9b25SBarry Smith 
23930de9b25SBarry Smith    Concepts: error^setting condition
24030de9b25SBarry Smith 
24191d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
24230de9b25SBarry Smith M*/
24353c77d0aSJed Brown #define CHKERRQ(n)             do {if (PetscUnlikely(n)) return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");} while (0)
24430de9b25SBarry Smith 
24553c77d0aSJed Brown #define CHKERRV(n)             do {if (PetscUnlikely(n)) {n = PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");return;}} while(0)
24653c77d0aSJed Brown #define CHKERRABORT(comm,n)    do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");MPI_Abort(comm,n);}} while (0)
24753c77d0aSJed Brown #define CHKERRCONTINUE(n)      do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");}} while (0)
24885614651SBarry Smith 
249fd705b32SMatthew Knepley #ifdef PETSC_CLANGUAGE_CXX
250fd705b32SMatthew Knepley 
251cc26af49SMatthew Knepley /*MC
252cc26af49SMatthew Knepley    CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
253cc26af49SMatthew Knepley 
254cc26af49SMatthew Knepley    Synopsis:
255cc26af49SMatthew Knepley    void CHKERRXX(PetscErrorCode errorcode)
256cc26af49SMatthew Knepley 
257eca87e8dSBarry Smith    Not Collective
258cc26af49SMatthew Knepley 
259cc26af49SMatthew Knepley    Input Parameters:
260cc26af49SMatthew Knepley .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
261cc26af49SMatthew Knepley 
262cc26af49SMatthew Knepley   Level: beginner
263cc26af49SMatthew Knepley 
264cc26af49SMatthew Knepley    Notes:
265cc26af49SMatthew Knepley     Once the error handler throws a ??? exception.
266cc26af49SMatthew Knepley 
267cc26af49SMatthew Knepley     You can use CHKERRV() which returns without an error code (bad idea since the error is ignored)
268cc26af49SMatthew Knepley     or CHKERRABORT(comm,n) to have MPI_Abort() returned immediately.
269cc26af49SMatthew Knepley 
270cc26af49SMatthew Knepley    Concepts: error^setting condition
271cc26af49SMatthew Knepley 
272cc26af49SMatthew Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKERRQ(), CHKMEMQ
273cc26af49SMatthew Knepley M*/
27453c77d0aSJed Brown #define CHKERRXX(n)            do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_IN_CXX,0);}} while(0)
275fd705b32SMatthew Knepley 
276fd705b32SMatthew Knepley #endif
277fd705b32SMatthew Knepley 
27830de9b25SBarry Smith /*MC
27930de9b25SBarry Smith    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
28030de9b25SBarry Smith 
28130de9b25SBarry Smith    Synopsis:
28291d3bdf4SKris Buschelman    CHKMEMQ;
28330de9b25SBarry Smith 
284eca87e8dSBarry Smith    Not Collective
285eca87e8dSBarry Smith 
28630de9b25SBarry Smith   Level: beginner
28730de9b25SBarry Smith 
28830de9b25SBarry Smith    Notes:
289ff002950SBarry Smith     Must run with the option -malloc_debug to enable this option
29030de9b25SBarry Smith 
29130de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
29230de9b25SBarry Smith 
29330de9b25SBarry Smith     By defaults prints location where memory that is corrupted was allocated.
29430de9b25SBarry Smith 
295f621e05eSBarry Smith     Use CHKMEMA for functions that return void
296f621e05eSBarry Smith 
29730de9b25SBarry Smith    Concepts: memory corruption
29830de9b25SBarry Smith 
2996024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
300ff002950SBarry Smith           PetscMallocValidate()
30130de9b25SBarry Smith M*/
30253c77d0aSJed Brown #define CHKMEMQ do {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__);CHKERRQ(_7_ierr);} while(0)
30385614651SBarry Smith 
30453c77d0aSJed Brown #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__)
305e68848bdSBarry Smith 
3069dd39edbSMatthew Knepley #if defined(PETSC_UNDERSCORE_CHKERR)
3076849ba73SBarry Smith extern  PetscErrorCode __gierr;
308f1af5d2fSBarry Smith #define _   __gierr =
309ac355199SBarry Smith #define ___  CHKERRQ(__gierr);
310f1af5d2fSBarry Smith #endif
311f1af5d2fSBarry Smith 
312d5e45103SBarry Smith #define               PETSC_EXCEPTIONS_MAX  256
313d5e45103SBarry Smith extern PetscErrorCode PetscErrorUncatchable[PETSC_EXCEPTIONS_MAX];
314d5e45103SBarry Smith extern PetscInt       PetscErrorUncatchableCount;
315d5e45103SBarry Smith extern PetscErrorCode PetscExceptions[PETSC_EXCEPTIONS_MAX];
316d5e45103SBarry Smith extern PetscInt       PetscExceptionsCount;
317d5e45103SBarry Smith 
3187087cfbeSBarry Smith extern PetscErrorCode  PetscExceptionPush(PetscErrorCode);
3197087cfbeSBarry Smith extern PetscErrorCode  PetscExceptionPop(PetscErrorCode);
320d5e45103SBarry Smith 
3217087cfbeSBarry Smith extern PetscErrorCode  PetscErrorSetCatchable(PetscErrorCode,PetscBool );
3227087cfbeSBarry Smith extern PetscBool   PetscErrorIsCatchable(PetscErrorCode);
32375febeb1SBarry Smith /*MC
32421dd7e22SBarry Smith    PetscExceptionCaught - Indicates if a specific exception zierr was caught.
32575febeb1SBarry Smith 
32675febeb1SBarry Smith    Synopsis:
327ace3abfcSBarry Smith      PetscBool  PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr);
32875febeb1SBarry Smith 
329eca87e8dSBarry Smith    Not Collective
330eca87e8dSBarry Smith 
33175febeb1SBarry Smith   Input Parameters:
3321853238fSBarry Smith   + xierr - error code returned from PetscExceptionTry1() or other PETSc routine
33375febeb1SBarry Smith   - zierr - error code you want it to be
33475febeb1SBarry Smith 
33575febeb1SBarry Smith   Level: advanced
33675febeb1SBarry Smith 
33775febeb1SBarry Smith    Notes:
33875febeb1SBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
33975febeb1SBarry Smith 
3401853238fSBarry Smith     Use PetscExceptionValue() to see if an error code is being "tried"
34121dd7e22SBarry Smith 
3421853238fSBarry Smith   Concepts: exceptions, exception handling
34375febeb1SBarry Smith 
34475febeb1SBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
34575febeb1SBarry Smith           CHKERRQ(), PetscExceptionTry1(), PetscExceptionValue()
34675febeb1SBarry Smith M*/
347ace3abfcSBarry Smith PETSC_STATIC_INLINE PetscBool  PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr)
348d0f46423SBarry Smith {
349d5e45103SBarry Smith   PetscInt i;
350d5e45103SBarry Smith   if (xierr != zierr) return PETSC_FALSE;
351d5e45103SBarry Smith   for (i=0; i<PetscErrorUncatchableCount; i++) {
352d5e45103SBarry Smith     if (PetscErrorUncatchable[i] == zierr) {
353d5e45103SBarry Smith       return PETSC_FALSE;
354d5e45103SBarry Smith     }
355d5e45103SBarry Smith   }
356d5e45103SBarry Smith   return PETSC_TRUE;
357d5e45103SBarry Smith }
358d5e45103SBarry Smith 
35975febeb1SBarry Smith /*MC
36075febeb1SBarry Smith    PetscExceptionValue - Indicates if the error code is one that is currently being tried
361d5e45103SBarry Smith 
36275febeb1SBarry Smith    Synopsis:
363ace3abfcSBarry Smith      PetscBool  PetscExceptionValue(PetscErrorCode xierr);
36475febeb1SBarry Smith 
365eca87e8dSBarry Smith    Not Collective
366eca87e8dSBarry Smith 
36775febeb1SBarry Smith   Input Parameters:
36875febeb1SBarry Smith   . xierr - error code
36975febeb1SBarry Smith 
37075febeb1SBarry Smith   Level: developer
37175febeb1SBarry Smith 
37275febeb1SBarry Smith    Notes:
37375febeb1SBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
37475febeb1SBarry Smith 
37521dd7e22SBarry Smith     Use PetscExceptionCaught() to see if the current error code is EXACTLY the one you want
37621dd7e22SBarry Smith 
37775febeb1SBarry Smith   Concepts: exceptions, exception hanlding
37875febeb1SBarry Smith 
37975febeb1SBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
38075febeb1SBarry Smith           CHKERRQ(), PetscExceptionTry1(), PetscExceptionCaught()
38175febeb1SBarry Smith M*/
382ace3abfcSBarry Smith PETSC_STATIC_INLINE PetscBool  PetscExceptionValue(PetscErrorCode zierr)
383d0f46423SBarry Smith {
384d5e45103SBarry Smith   PetscInt i;
385d5e45103SBarry Smith   for (i=0; i<PetscExceptionsCount; i++) {
386d5e45103SBarry Smith     if (PetscExceptions[i] == zierr) {
387d5e45103SBarry Smith       return PETSC_TRUE;
388d5e45103SBarry Smith     }
389d5e45103SBarry Smith   }
390d5e45103SBarry Smith   return PETSC_FALSE;
391d5e45103SBarry Smith }
3926024bd2cSBarry Smith 
3936024bd2cSBarry Smith /*MC
3946024bd2cSBarry Smith    PetscExceptionTry1 - Runs the routine, causing a particular error code to be treated as an exception,
3956024bd2cSBarry Smith          rather than an error. That is if that error code is treated the program returns to this level,
3966024bd2cSBarry Smith          but does not call the error handlers
3976024bd2cSBarry Smith 
3986024bd2cSBarry Smith    Synopsis:
3991853238fSBarry Smith      PetscErrorCode PetscExceptionTry1(PetscErrorCode routine(....),PetscErrorCode);
4006024bd2cSBarry Smith 
401eca87e8dSBarry Smith    Not Collective
402eca87e8dSBarry Smith 
4036024bd2cSBarry Smith   Level: advanced
4046024bd2cSBarry Smith 
4051853238fSBarry Smith    No Fortran Equivalent (see PetscExceptionPush() for Fortran)
4061853238fSBarry Smith 
4076024bd2cSBarry Smith    Notes:
4086024bd2cSBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
4096024bd2cSBarry Smith 
41075febeb1SBarry Smith   Note: In general, the outer most try on an exception is the one that will be caught (that is trys down in
41175febeb1SBarry Smith         PETSc code will not usually handle an exception that was issued above). See SNESSolve() for an example
41275febeb1SBarry Smith         of how the local try is ignored if a higher (in the stack) one is also in effect.
4136024bd2cSBarry Smith 
4146024bd2cSBarry Smith   Concepts: exceptions, exception hanlding
4156024bd2cSBarry Smith 
4166024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
41721dd7e22SBarry Smith           CHKERRQ(), PetscExceptionCaught(), PetscExceptionPush(), PetscExceptionPop()
4186024bd2cSBarry Smith M*/
4197b0215c6SHong Zhang extern PetscErrorCode PetscExceptionTmp,PetscExceptionTmp1;
4207b0215c6SHong Zhang #define PetscExceptionTry1(a,b) (PetscExceptionTmp1 = PetscExceptionPush(b)) ? PetscExceptionTmp1 : (PetscExceptionTmp1 = a, (PetscExceptionTmp = PetscExceptionPop(b)) ? PetscExceptionTmp : PetscExceptionTmp1)
4216024bd2cSBarry Smith 
422f70662edSBarry Smith /*
423f70662edSBarry Smith    Used by PetscExceptionTrySync(). Returns zierr on ALL processes in comm iff xierr is zierr on at least one process and zero on all others.
424f70662edSBarry Smith */
425f70662edSBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscExceptionTrySync_Private(MPI_Comm comm,PetscErrorCode xierr,PetscErrorCode zierr)
426f70662edSBarry Smith {
427f70662edSBarry Smith   PetscReal      in[2],out[2];
428f70662edSBarry Smith   PetscErrorCode ierr;
429f70662edSBarry Smith 
430f70662edSBarry Smith   if (xierr != zierr) return xierr;
431f70662edSBarry Smith 
432f70662edSBarry Smith   in[0] = xierr;
433f70662edSBarry Smith   in[1] = 0.0;   /* dummy value */
434f70662edSBarry Smith 
435f70662edSBarry Smith   ierr = MPI_Allreduce(in,out,2,MPIU_REAL,0,comm); if (ierr) {;}
436f70662edSBarry Smith   return xierr;
437f70662edSBarry Smith }
438f70662edSBarry Smith 
439f70662edSBarry Smith /*MC
440f70662edSBarry Smith    PetscExceptionTrySyncNorm - Runs the routine, causing a particular error code to be treated as an exception,
441f70662edSBarry Smith          rather than an error. That is if that error code is treated the program returns to this level,
442f70662edSBarry Smith          but does not call the error handlers
443f70662edSBarry Smith 
444f70662edSBarry Smith    Synopsis:
445f70662edSBarry Smith      PetscExceptionTrySyncNorm(MPI_Comm comm,PetscErrorCode routine(....),PetscErrorCode);
446f70662edSBarry Smith 
447eca87e8dSBarry Smith      Collective on Comm
448eca87e8dSBarry Smith 
449f70662edSBarry Smith   Level: advanced
450f70662edSBarry Smith 
451f70662edSBarry Smith    Notes: This synchronizes the error code across all processes in the communicator IF the code matches PetscErrorCode. The next
452f70662edSBarry Smith      call with an MPI_Reduce()/MPI_Allreduce() MUST be VecNorm() [We can added VecDot() and maybe others as needed].
453f70662edSBarry Smith 
454f70662edSBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
455f70662edSBarry Smith 
456f70662edSBarry Smith   Note: In general, the outer most try on an exception is the one that will be caught (that is trys down in
457f70662edSBarry Smith         PETSc code will not usually handle an exception that was issued above). See SNESSolve() for an example
458f70662edSBarry Smith         of how the local try is ignored if a higher (in the stack) one is also in effect.
459f70662edSBarry Smith 
460f70662edSBarry Smith   Concepts: exceptions, exception hanlding
461f70662edSBarry Smith 
462f70662edSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
463f70662edSBarry Smith           CHKERRQ(), PetscExceptionCaught(), PetscExceptionPush(), PetscExceptionPop(), PetscExceptionTry1()
464f70662edSBarry Smith M*/
465f70662edSBarry Smith #define PetscExceptionTrySyncNorm(comm,a,b) (PetscExceptionTmp = PetscExceptionPush(b)) ? PetscExceptionTmp : \
466f70662edSBarry Smith                                         (PetscExceptionTmp = a , PetscExceptionPop(b),PetscExceptionTrySyncNorm_Private(comm,PetscExceptionTmp,b))
467f70662edSBarry Smith 
46854a8ef01SBarry Smith #else
469f621e05eSBarry Smith 
470f621e05eSBarry Smith /*
471e2e64c6bSBarry Smith     These are defined to be empty for when error checking is turned off, with ./configure --with-errorchecking=0
472f621e05eSBarry Smith */
473f621e05eSBarry Smith 
4744b209cf6SBarry Smith #define SETERRQ(c,n,s)
4754b209cf6SBarry Smith #define SETERRQ1(c,n,s,a1)
4764b209cf6SBarry Smith #define SETERRQ2(c,n,s,a1,a2)
4774b209cf6SBarry Smith #define SETERRQ3(c,n,s,a1,a2,a3)
4784b209cf6SBarry Smith #define SETERRQ4(c,n,s,a1,a2,a3,a4)
4794b209cf6SBarry Smith #define SETERRQ5(c,n,s,a1,a2,a3,a4,a5)
4804b209cf6SBarry Smith #define SETERRQ6(c,n,s,a1,a2,a3,a4,a5,a6)
4814b209cf6SBarry Smith #define SETERRABORT(comm,n,s)
48285614651SBarry Smith 
4834f227f7cSBarry Smith #define CHKERRQ(n)     ;
4841ee4faa0SMatthew Knepley #define CHKERRABORT(comm,n) ;
4851ee4faa0SMatthew Knepley #define CHKERRCONTINUE(n) ;
48685614651SBarry Smith #define CHKMEMQ        ;
48785614651SBarry Smith 
4888cabf42eSMatthew G Knepley #ifdef PETSC_CLANGUAGE_CXX
4898cabf42eSMatthew G Knepley #define CHKERRXX(n) ;
4908cabf42eSMatthew G Knepley #endif
4918cabf42eSMatthew G Knepley 
492f1af5d2fSBarry Smith #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
493f1af5d2fSBarry Smith #define _
494f1af5d2fSBarry Smith #define ___
495f1af5d2fSBarry Smith #endif
496f1af5d2fSBarry Smith 
4978da67ba8SSatish Balay #define PetscExceptionPush(a)                0
4981853238fSBarry Smith #define PetscExceptionPop(a)                 0
499c132512eSBarry Smith #define PetscErrorSetCatchable(a,b)          0
5008da67ba8SSatish Balay #define PetscErrorIsCatchable(a)             PETSC_FALSE
5018da67ba8SSatish Balay 
502c132512eSBarry Smith #define PetscExceptionCaught(a,b)            PETSC_FALSE
503c132512eSBarry Smith #define PetscExceptionValue(a)               PETSC_FALSE
5046024bd2cSBarry Smith #define PetscExceptionTry1(a,b)              a
505f70662edSBarry Smith #define PetscExceptionTrySyncNorm(comm,a,b)  a
506f70662edSBarry Smith 
50754a8ef01SBarry Smith #endif
50854a8ef01SBarry Smith 
509668f157eSBarry Smith /*E
510668f157eSBarry Smith   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
511668f157eSBarry Smith 
512668f157eSBarry Smith   Level: advanced
513668f157eSBarry Smith 
514d736bfebSBarry Smith   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated
515d736bfebSBarry Smith 
516668f157eSBarry Smith   Developer Notes: This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandling()
517668f157eSBarry Smith 
518d736bfebSBarry Smith .seealso: PetscError(), SETERRXX()
519668f157eSBarry Smith E*/
520d736bfebSBarry Smith typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
5214b209cf6SBarry Smith 
5227087cfbeSBarry Smith extern PetscErrorCode  PetscErrorPrintfInitialize(void);
5237087cfbeSBarry Smith extern PetscErrorCode  PetscErrorMessage(int,const char*[],char **);
5247087cfbeSBarry Smith extern PetscErrorCode  PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
5250577ce7cSSatish Balay #if defined(PETSC_CLANGUAGE_CXX) && !defined(PETSC_USE_EXTERN_CXX)
526fd705b32SMatthew Knepley #include <sstream>
5277087cfbeSBarry Smith extern PetscErrorCode  PetscTraceBackErrorHandlerCxx(MPI_Comm,int,const char *,const char *,const char *,PetscErrorCode,PetscErrorType,const char*,void*);
528fd705b32SMatthew Knepley #endif
5297087cfbeSBarry Smith extern PetscErrorCode  PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
5307087cfbeSBarry Smith extern PetscErrorCode  PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
5317087cfbeSBarry Smith extern PetscErrorCode  PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
5327087cfbeSBarry Smith extern PetscErrorCode  PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
5337087cfbeSBarry Smith extern PetscErrorCode  PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
5347087cfbeSBarry Smith extern PetscErrorCode  PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
5357087cfbeSBarry Smith extern PetscErrorCode  PetscError(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...);
5367087cfbeSBarry Smith extern PetscErrorCode  PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
5377087cfbeSBarry Smith extern PetscErrorCode  PetscPopErrorHandler(void);
5387087cfbeSBarry Smith extern PetscErrorCode  PetscDefaultSignalHandler(int,void*);
5397087cfbeSBarry Smith extern PetscErrorCode  PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
5407087cfbeSBarry Smith extern PetscErrorCode  PetscPopSignalHandler(void);
541329f5518SBarry Smith 
542329f5518SBarry Smith typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
5437087cfbeSBarry Smith extern PetscErrorCode   PetscSetFPTrap(PetscFPTrap);
54454a8ef01SBarry Smith 
5453a40ed3dSBarry Smith /*
5463a40ed3dSBarry Smith       Allows the code to build a stack frame as it runs
5473a40ed3dSBarry Smith */
54863d6bff0SBarry Smith #if defined(PETSC_USE_DEBUG) && !defined(PETSC_USE_PTHREAD)
5493a40ed3dSBarry Smith 
55099cd645aSJed Brown #define PETSCSTACKSIZE 64
551184914b5SBarry Smith 
5523a40ed3dSBarry Smith typedef struct  {
5530e33f6ddSBarry Smith   const char *function[PETSCSTACKSIZE];
5540e33f6ddSBarry Smith   const char *file[PETSCSTACKSIZE];
5550e33f6ddSBarry Smith   const char *directory[PETSCSTACKSIZE];
556184914b5SBarry Smith         int  line[PETSCSTACKSIZE];
557184914b5SBarry Smith         int currentsize;
5583a40ed3dSBarry Smith } PetscStack;
5593a40ed3dSBarry Smith 
5607087cfbeSBarry Smith extern  PetscStack *petscstack;
5617087cfbeSBarry Smith extern PetscErrorCode   PetscStackCopy(PetscStack*,PetscStack*);
5627087cfbeSBarry Smith extern PetscErrorCode   PetscStackPrint(PetscStack*,FILE* fp);
563184914b5SBarry Smith 
564184914b5SBarry Smith #define PetscStackActive (petscstack != 0)
5653a40ed3dSBarry Smith 
56630de9b25SBarry Smith /*MC
56730de9b25SBarry Smith    PetscFunctionBegin - First executable line of each PETSc function
56830de9b25SBarry Smith         used for error handling.
56930de9b25SBarry Smith 
57030de9b25SBarry Smith    Synopsis:
57130de9b25SBarry Smith    void PetscFunctionBegin;
57230de9b25SBarry Smith 
573eca87e8dSBarry Smith    Not Collective
574eca87e8dSBarry Smith 
57530de9b25SBarry Smith    Usage:
57630de9b25SBarry Smith .vb
57730de9b25SBarry Smith      int something;
57830de9b25SBarry Smith 
57930de9b25SBarry Smith      PetscFunctionBegin;
58030de9b25SBarry Smith .ve
58130de9b25SBarry Smith 
58230de9b25SBarry Smith    Notes:
58330de9b25SBarry Smith      Not available in Fortran
58430de9b25SBarry Smith 
58530de9b25SBarry Smith    Level: developer
58630de9b25SBarry Smith 
58730de9b25SBarry Smith .seealso: PetscFunctionReturn()
58830de9b25SBarry Smith 
58930de9b25SBarry Smith .keywords: traceback, error handling
59030de9b25SBarry Smith M*/
5913a40ed3dSBarry Smith #define PetscFunctionBegin \
5928246ba0dSJed Brown   do {                                                                  \
593184914b5SBarry Smith     if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {     \
59453c77d0aSJed Brown       petscstack->function[petscstack->currentsize]  = PETSC_FUNCTION_NAME; \
595184914b5SBarry Smith       petscstack->file[petscstack->currentsize]      = __FILE__;        \
596184914b5SBarry Smith       petscstack->directory[petscstack->currentsize] = __SDIR__;        \
597184914b5SBarry Smith       petscstack->line[petscstack->currentsize]      = __LINE__;        \
598184914b5SBarry Smith       petscstack->currentsize++;                                        \
59953c77d0aSJed Brown     }                                                                   \
60053c77d0aSJed Brown     PetscCheck__FUNCT__();                                              \
60153c77d0aSJed Brown   } while (0)
60253c77d0aSJed Brown 
60353c77d0aSJed Brown #define PetscCheck__FUNCT__() do { \
60453c77d0aSJed Brown     if (strcmp(PETSC_FUNCTION_NAME,__FUNCT__) && strcmp(__FUNCT__,"User provided function")) { \
60553c77d0aSJed Brown       (*PetscErrorPrintf)("%s%s:%d: __FUNCT__=\"%s\" does not agree with %s=\"%s\"\n",__SDIR__,__FILE__,__LINE__,__FUNCT__,PetscStringize(PETSC_FUNCTION_NAME),PETSC_FUNCTION_NAME); \
60653c77d0aSJed Brown     }                                                                   \
60753c77d0aSJed Brown   } while (0)
6083a40ed3dSBarry Smith 
6095cd90555SBarry Smith #define PetscStackPush(n) \
6108246ba0dSJed Brown   do {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
611184914b5SBarry Smith     petscstack->function[petscstack->currentsize]  = n; \
612184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = "unknown"; \
613184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = "unknown"; \
614184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = 0; \
615184914b5SBarry Smith     petscstack->currentsize++; \
616812af9f3SBarry Smith   } CHKMEMQ;} while (0)
6173a40ed3dSBarry Smith 
618d64ed03dSBarry Smith #define PetscStackPop \
619812af9f3SBarry Smith   do {CHKMEMQ; if (petscstack && petscstack->currentsize > 0) {	\
620184914b5SBarry Smith     petscstack->currentsize--; \
621184914b5SBarry Smith     petscstack->function[petscstack->currentsize]  = 0; \
622184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = 0; \
623184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = 0; \
624184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = 0; \
6258246ba0dSJed Brown   }} while (0)
626d64ed03dSBarry Smith 
62730de9b25SBarry Smith /*MC
62830de9b25SBarry Smith    PetscFunctionReturn - Last executable line of each PETSc function
62930de9b25SBarry Smith         used for error handling. Replaces return()
63030de9b25SBarry Smith 
63130de9b25SBarry Smith    Synopsis:
63230de9b25SBarry Smith    void PetscFunctionReturn(0);
63330de9b25SBarry Smith 
634eca87e8dSBarry Smith    Not Collective
635eca87e8dSBarry Smith 
63630de9b25SBarry Smith    Usage:
63730de9b25SBarry Smith .vb
63830de9b25SBarry Smith     ....
63930de9b25SBarry Smith      PetscFunctionReturn(0);
64030de9b25SBarry Smith    }
64130de9b25SBarry Smith .ve
64230de9b25SBarry Smith 
64330de9b25SBarry Smith    Notes:
64430de9b25SBarry Smith      Not available in Fortran
64530de9b25SBarry Smith 
64630de9b25SBarry Smith    Level: developer
64730de9b25SBarry Smith 
64830de9b25SBarry Smith .seealso: PetscFunctionBegin()
64930de9b25SBarry Smith 
65030de9b25SBarry Smith .keywords: traceback, error handling
65130de9b25SBarry Smith M*/
6525cd90555SBarry Smith #define PetscFunctionReturn(a) \
6538246ba0dSJed Brown   do {\
654812af9f3SBarry Smith   if (petscstack && petscstack->currentsize > 0) {	\
655812af9f3SBarry Smith     petscstack->currentsize--; \
656812af9f3SBarry Smith     petscstack->function[petscstack->currentsize]  = 0; \
657812af9f3SBarry Smith     petscstack->file[petscstack->currentsize]      = 0; \
658812af9f3SBarry Smith     petscstack->directory[petscstack->currentsize] = 0; \
659812af9f3SBarry Smith     petscstack->line[petscstack->currentsize]      = 0; \
660812af9f3SBarry Smith   }\
6618246ba0dSJed Brown   return(a);} while (0)
662d64ed03dSBarry Smith 
663ff94ddecSSatish Balay #define PetscFunctionReturnVoid() \
6648246ba0dSJed Brown   do {\
665812af9f3SBarry Smith   if (petscstack && petscstack->currentsize > 0) {	\
666812af9f3SBarry Smith     petscstack->currentsize--; \
667812af9f3SBarry Smith     petscstack->function[petscstack->currentsize]  = 0; \
668812af9f3SBarry Smith     petscstack->file[petscstack->currentsize]      = 0; \
669812af9f3SBarry Smith     petscstack->directory[petscstack->currentsize] = 0; \
670812af9f3SBarry Smith     petscstack->line[petscstack->currentsize]      = 0; \
671812af9f3SBarry Smith   }\
6728246ba0dSJed Brown   return;} while (0)
673ff94ddecSSatish Balay 
6746d385327SIbrahima Ba #else
6756d385327SIbrahima Ba 
6763a40ed3dSBarry Smith #define PetscFunctionBegin
6773a40ed3dSBarry Smith #define PetscFunctionReturn(a)  return(a)
6785665465eSBarry Smith #define PetscFunctionReturnVoid() return
679812af9f3SBarry Smith #define PetscStackPop     CHKMEMQ
680812af9f3SBarry Smith #define PetscStackPush(f) CHKMEMQ
681d64ed03dSBarry Smith #define PetscStackActive        0
6823a40ed3dSBarry Smith 
6833a40ed3dSBarry Smith #endif
6843a40ed3dSBarry Smith 
685eb6b5d47SBarry Smith /*
686eb6b5d47SBarry Smith     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.
687eb6b5d47SBarry Smith 
688eb6b5d47SBarry Smith    Input Parameters:
689eb6b5d47SBarry Smith +   name - string that gives the name of the function being called
690eb6b5d47SBarry Smith -   routine - actual call to the routine
691eb6b5d47SBarry Smith 
692eb6b5d47SBarry Smith    Developer Note: this is so that when a user or external library routine results in a crash or corrupts memory, they get blamed instead of PETSc.
693eb6b5d47SBarry Smith 
694eb6b5d47SBarry Smith */
695eb6b5d47SBarry Smith #define PetscStackCall(name,routine) PetscStackPush(name);routine;PetscStackPop;
696eb6b5d47SBarry Smith 
6977087cfbeSBarry Smith extern PetscErrorCode   PetscStackCreate(void);
6987087cfbeSBarry Smith extern PetscErrorCode   PetscStackView(PetscViewer);
6997087cfbeSBarry Smith extern PetscErrorCode   PetscStackDestroy(void);
7007087cfbeSBarry Smith extern PetscErrorCode   PetscStackPublish(void);
7017087cfbeSBarry Smith extern PetscErrorCode   PetscStackDepublish(void);
7023a40ed3dSBarry Smith 
70306d1fe2cSBarry Smith 
704e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END
70506d1fe2cSBarry Smith #endif
706