xref: /petsc/include/petscerror.h (revision 742f4fedf06130d95e6a4f4e997a7089c4cd80d4)
154a8ef01SBarry Smith /*
24f227f7cSBarry Smith     Contains all error handling code for PETSc.
354a8ef01SBarry Smith */
445d48df9SBarry Smith #if !defined(__PETSCERROR_H)
545d48df9SBarry Smith #define __PETSCERROR_H
6c22c1629SBarry Smith #include "petsc.h"
7e9fa29b7SSatish Balay PETSC_EXTERN_CXX_BEGIN
8c22c1629SBarry Smith 
954a8ef01SBarry Smith /*
1054a8ef01SBarry Smith    Defines the directory where the compiled source is located; used
115e97870eSBarry Smith    in printing error messages. Each makefile has an entry
125e97870eSBarry Smith    LOCDIR	  =  thedirectory
130cd5afcaSLois Curfman McInnes    and bmake/common_variables includes in CCPPFLAGS -D__SDIR__='"${LOCDIR}"'
145e97870eSBarry Smith    which is a flag passed to the C/C++ compilers.
1554a8ef01SBarry Smith */
16c22c1629SBarry Smith #if !defined(__SDIR__)
172ee1dbe0SBarry Smith #define __SDIR__ "unknowndirectory/"
1854a8ef01SBarry Smith #endif
1954a8ef01SBarry Smith 
2054a8ef01SBarry Smith /*
214f227f7cSBarry Smith    Defines the function where the compiled source is located; used
224f227f7cSBarry Smith    in printing error messages.
234f227f7cSBarry Smith */
244a2ae208SSatish Balay #if !defined(__FUNCT__)
25da9b6338SBarry Smith #define __FUNCT__ "User provided function"
264f227f7cSBarry Smith #endif
274f227f7cSBarry Smith 
284f227f7cSBarry Smith /*
29329ffe3dSLois Curfman McInnes      These are the generic error codes. These error codes are used
30e2d1d2b7SBarry Smith      many different places in the PETSc source code. The string versions are
31e2d1d2b7SBarry Smith      at src/sys/src/error/err.c any changes here must also be made there
3245d48df9SBarry Smith 
3354a8ef01SBarry Smith */
3445d48df9SBarry Smith #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
35106f7b34SBarry Smith #define PETSC_ERR_MEM_MALLOC_0     85   /* cannot malloc zero size */
3647794344SBarry Smith #define PETSC_ERR_SUP              56   /* no support for requested operation */
37e2d1d2b7SBarry Smith #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
38e2d1d2b7SBarry Smith #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
3945d48df9SBarry Smith #define PETSC_ERR_SIG              59   /* signal received */
40f1caa5a4SBarry Smith #define PETSC_ERR_FP               72   /* floating point exception */
41a8c6a408SBarry Smith #define PETSC_ERR_COR              74   /* corrupted PETSc object */
42a8c6a408SBarry Smith #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
43329ffe3dSLois Curfman McInnes #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
44329ffe3dSLois Curfman McInnes #define PETSC_ERR_MEMC             78   /* memory corruption */
45b3cc6726SBarry Smith #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
461302d50aSBarry Smith #define PETSC_ERR_USER             83   /* user has not provided needed function */
4745d48df9SBarry Smith 
4845d48df9SBarry Smith #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
4945d48df9SBarry Smith #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
50a8c6a408SBarry Smith #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
5145d48df9SBarry Smith #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
5245d48df9SBarry Smith #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
534f227f7cSBarry Smith #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
544f227f7cSBarry Smith #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
556831982aSBarry Smith #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
56d252947aSBarry Smith #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
57a8c6a408SBarry Smith #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
584482741eSBarry Smith #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
59958c9bccSBarry Smith #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
604f227f7cSBarry Smith 
614f227f7cSBarry Smith #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
624f227f7cSBarry Smith #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
634f227f7cSBarry Smith #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
64a8c6a408SBarry Smith #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
6545d48df9SBarry Smith 
66329ffe3dSLois Curfman McInnes #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
679e3b2f23SBarry Smith #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
6854a8ef01SBarry Smith 
69aa482453SBarry Smith #if defined(PETSC_USE_DEBUG)
7030de9b25SBarry Smith 
7130de9b25SBarry Smith /*MC
7230de9b25SBarry Smith    SETERRQ - Macro that is called when an error has been detected,
7330de9b25SBarry Smith 
7430de9b25SBarry Smith    Not Collective
7530de9b25SBarry Smith 
7630de9b25SBarry Smith    Synopsis:
77a7cc72afSBarry Smith    void SETERRQ(PetscErrorCode errorcode,char *message)
7830de9b25SBarry Smith 
7930de9b25SBarry Smith 
8030de9b25SBarry Smith    Input Parameters:
8130de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
8230de9b25SBarry Smith -  message - error message
8330de9b25SBarry Smith 
8430de9b25SBarry Smith   Level: beginner
8530de9b25SBarry Smith 
8630de9b25SBarry Smith    Notes:
8730de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
8830de9b25SBarry Smith 
8930de9b25SBarry Smith     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
9030de9b25SBarry Smith 
9130de9b25SBarry Smith 
9230de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
9330de9b25SBarry Smith 
9430de9b25SBarry Smith    Concepts: error^setting condition
9530de9b25SBarry Smith 
9691d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
9730de9b25SBarry Smith M*/
984a2ae208SSatish Balay #define SETERRQ(n,s)              {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);}
9930de9b25SBarry Smith 
10030de9b25SBarry Smith /*MC
10130de9b25SBarry Smith    SETERRQ1 - Macro that is called when an error has been detected,
10230de9b25SBarry Smith 
10330de9b25SBarry Smith    Not Collective
10430de9b25SBarry Smith 
10530de9b25SBarry Smith    Synopsis:
106a7cc72afSBarry Smith    void SETERRQ1(PetscErrorCode errorcode,char *formatmessage,arg)
10730de9b25SBarry Smith 
10830de9b25SBarry Smith 
10930de9b25SBarry Smith    Input Parameters:
11030de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
11130de9b25SBarry Smith .  message - error message in the printf format
11230de9b25SBarry Smith -  arg - argument (for example an integer, string or double)
11330de9b25SBarry Smith 
11430de9b25SBarry Smith   Level: beginner
11530de9b25SBarry Smith 
11630de9b25SBarry Smith    Notes:
11730de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
11830de9b25SBarry Smith 
11930de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
12030de9b25SBarry Smith 
12130de9b25SBarry Smith    Concepts: error^setting condition
12230de9b25SBarry Smith 
12391d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
12430de9b25SBarry Smith M*/
1254a2ae208SSatish Balay #define SETERRQ1(n,s,a1)          {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1);}
12630de9b25SBarry Smith 
12730de9b25SBarry Smith /*MC
12830de9b25SBarry Smith    SETERRQ2 - Macro that is called when an error has been detected,
12930de9b25SBarry Smith 
13030de9b25SBarry Smith    Not Collective
13130de9b25SBarry Smith 
13230de9b25SBarry Smith    Synopsis:
133a7cc72afSBarry Smith    void SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
13430de9b25SBarry Smith 
13530de9b25SBarry Smith 
13630de9b25SBarry Smith    Input Parameters:
13730de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
13830de9b25SBarry Smith .  message - error message in the printf format
13930de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
14030de9b25SBarry Smith -  arg2 - argument (for example an integer, string or double)
14130de9b25SBarry Smith 
14230de9b25SBarry Smith   Level: beginner
14330de9b25SBarry Smith 
14430de9b25SBarry Smith    Notes:
14530de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
14630de9b25SBarry Smith 
14730de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
14830de9b25SBarry Smith 
14930de9b25SBarry Smith    Concepts: error^setting condition
15030de9b25SBarry Smith 
15191d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
15230de9b25SBarry Smith M*/
1534a2ae208SSatish Balay #define SETERRQ2(n,s,a1,a2)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2);}
15430de9b25SBarry Smith 
15530de9b25SBarry Smith /*MC
15630de9b25SBarry Smith    SETERRQ3 - Macro that is called when an error has been detected,
15730de9b25SBarry Smith 
15830de9b25SBarry Smith    Not Collective
15930de9b25SBarry Smith 
16030de9b25SBarry Smith    Synopsis:
161a7cc72afSBarry Smith    void SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)
16230de9b25SBarry Smith 
16330de9b25SBarry Smith 
16430de9b25SBarry Smith    Input Parameters:
16530de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
16630de9b25SBarry Smith .  message - error message in the printf format
16730de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
16830de9b25SBarry Smith .  arg2 - argument (for example an integer, string or double)
16930de9b25SBarry Smith -  arg3 - argument (for example an integer, string or double)
17030de9b25SBarry Smith 
17130de9b25SBarry Smith   Level: beginner
17230de9b25SBarry Smith 
17330de9b25SBarry Smith    Notes:
17430de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
17530de9b25SBarry Smith 
17630de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
17730de9b25SBarry Smith 
17830de9b25SBarry Smith    Concepts: error^setting condition
17930de9b25SBarry Smith 
18091d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
18130de9b25SBarry Smith M*/
1824a2ae208SSatish Balay #define SETERRQ3(n,s,a1,a2,a3)    {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3);}
18330de9b25SBarry Smith 
1844a2ae208SSatish Balay #define SETERRQ4(n,s,a1,a2,a3,a4) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4);}
185a30c184eSMatthew Knepley #define SETERRQ5(n,s,a1,a2,a3,a4,a5)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5);}
186a30c184eSMatthew Knepley #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);}
187a30c184eSMatthew Knepley #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);}
188e855a17bSBarry Smith #define SETERRABORT(comm,n,s)     {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);MPI_Abort(comm,n);}
1899a00fa46SSatish Balay 
19030de9b25SBarry Smith /*MC
19130de9b25SBarry Smith    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
19230de9b25SBarry Smith 
19330de9b25SBarry Smith    Not Collective
19430de9b25SBarry Smith 
19530de9b25SBarry Smith    Synopsis:
196a7cc72afSBarry Smith    void CHKERRQ(PetscErrorCode errorcode)
19730de9b25SBarry Smith 
19830de9b25SBarry Smith 
19930de9b25SBarry Smith    Input Parameters:
20030de9b25SBarry Smith .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
20130de9b25SBarry Smith 
20230de9b25SBarry Smith   Level: beginner
20330de9b25SBarry Smith 
20430de9b25SBarry Smith    Notes:
20530de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
20630de9b25SBarry Smith 
20730de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
2085324ea47SKris Buschelman 
2095324ea47SKris Buschelman     CHKERRQ(n) is fundamentally a macro replacement for:
2105324ea47SKris Buschelman          if (n) return(PetscError(...,n,...));
2115324ea47SKris Buschelman 
2125324ea47SKris Buschelman     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
2135324ea47SKris Buschelman     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
2145324ea47SKris Buschelman     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
2155324ea47SKris Buschelman     a more appropriate construct for using PETSc Error Handling would be:
2165324ea47SKris Buschelman          if (n) {PetscError(....); return(YourReturnType);}
21730de9b25SBarry Smith 
21830de9b25SBarry Smith    Concepts: error^setting condition
21930de9b25SBarry Smith 
22091d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
22130de9b25SBarry Smith M*/
222c3be3f59SMatthew Knepley #define CHKERRQ(n)             if (n) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
22330de9b25SBarry Smith 
22494a56cdfSMatthew Knepley #define CHKERRABORT(comm,n)    if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");MPI_Abort(comm,n);}
22594a56cdfSMatthew Knepley #define CHKERRCONTINUE(n)      if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
22685614651SBarry Smith 
22730de9b25SBarry Smith /*MC
22830de9b25SBarry Smith    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
22930de9b25SBarry Smith 
23030de9b25SBarry Smith    Not Collective
23130de9b25SBarry Smith 
23230de9b25SBarry Smith    Synopsis:
23391d3bdf4SKris Buschelman    CHKMEMQ;
23430de9b25SBarry Smith 
23530de9b25SBarry Smith   Level: beginner
23630de9b25SBarry Smith 
23730de9b25SBarry Smith    Notes:
238ff002950SBarry Smith     Must run with the option -malloc_debug to enable this option
23930de9b25SBarry Smith 
24030de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
24130de9b25SBarry Smith 
24230de9b25SBarry Smith     By defaults prints location where memory that is corrupted was allocated.
24330de9b25SBarry Smith 
24430de9b25SBarry Smith    Concepts: memory corruption
24530de9b25SBarry Smith 
24691d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2(),
247ff002950SBarry Smith           PetscMallocValidate()
24830de9b25SBarry Smith M*/
249ff002950SBarry Smith #define CHKMEMQ {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);CHKERRQ(_7_ierr);}
25085614651SBarry Smith 
251f1af5d2fSBarry Smith #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
2526849ba73SBarry Smith extern  PetscErrorCode __gierr;
253f1af5d2fSBarry Smith #define _   __gierr =
254ac355199SBarry Smith #define ___  CHKERRQ(__gierr);
255f1af5d2fSBarry Smith #endif
256f1af5d2fSBarry Smith 
25754a8ef01SBarry Smith #else
25890d37a7cSBarry Smith #define SETERRQ(n,s) ;
25990d37a7cSBarry Smith #define SETERRQ1(n,s,a1) ;
26090d37a7cSBarry Smith #define SETERRQ2(n,s,a1,a2) ;
26190d37a7cSBarry Smith #define SETERRQ3(n,s,a1,a2,a3) ;
26290d37a7cSBarry Smith #define SETERRQ4(n,s,a1,a2,a3,a4) ;
2639bd8d7fdSSatish Balay #define SETERRQ5(n,s,a1,a2,a3,a4,a5) ;
264*742f4fedSDinesh Kaushik #define SETERRQ6(n,s,a1,a2,a3,a4,a5,a6);
2651ee4faa0SMatthew Knepley #define SETERRABORT(comm,n,s) ;
26685614651SBarry Smith 
2674f227f7cSBarry Smith #define CHKERRQ(n)     ;
2681ee4faa0SMatthew Knepley #define CHKERRABORT(comm,n) ;
2691ee4faa0SMatthew Knepley #define CHKERRCONTINUE(n) ;
27085614651SBarry Smith 
27185614651SBarry Smith #define CHKMEMQ        ;
27285614651SBarry Smith 
273f1af5d2fSBarry Smith #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
274f1af5d2fSBarry Smith #define _
275f1af5d2fSBarry Smith #define ___
276f1af5d2fSBarry Smith #endif
277f1af5d2fSBarry Smith 
27854a8ef01SBarry Smith #endif
27954a8ef01SBarry Smith 
28077431f27SBarry Smith EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
281dfbe8321SBarry Smith EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
282dfbe8321SBarry Smith EXTERN PetscErrorCode PetscTraceBackErrorHandler(int,const char*,const char*,const char*,int,int,const char*,void*);
283dfbe8321SBarry Smith EXTERN PetscErrorCode PetscIgnoreErrorHandler(int,const char*,const char*,const char*,int,int,const char*,void*);
284dfbe8321SBarry Smith EXTERN PetscErrorCode PetscEmacsClientErrorHandler(int,const char*,const char*,const char*,int,int,const char*,void*);
285dfbe8321SBarry Smith EXTERN PetscErrorCode PetscStopErrorHandler(int,const char*,const char*,const char*,int,int,const char*,void*);
286dfbe8321SBarry Smith EXTERN PetscErrorCode PetscAbortErrorHandler(int,const char*,const char*,const char*,int,int,const char*,void*);
287dfbe8321SBarry Smith EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(int,const char*,const char*,const char*,int,int,const char*,void*);
288dfbe8321SBarry Smith EXTERN PetscErrorCode PetscError(int,const char*,const char*,const char*,int,int,const char*,...) PETSC_PRINTF_FORMAT_CHECK(7,8);
2896849ba73SBarry Smith EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(int,const char*,const char*,const char*,int,int,const char*,void*),void*);
290dfbe8321SBarry Smith EXTERN PetscErrorCode PetscPopErrorHandler(void);
291dfbe8321SBarry Smith EXTERN PetscErrorCode PetscDefaultSignalHandler(int,void*);
2926849ba73SBarry Smith EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
293dfbe8321SBarry Smith EXTERN PetscErrorCode PetscPopSignalHandler(void);
294329f5518SBarry Smith 
295329f5518SBarry Smith typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
296dfbe8321SBarry Smith EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
29754a8ef01SBarry Smith 
2983a40ed3dSBarry Smith /*
2993a40ed3dSBarry Smith       Allows the code to build a stack frame as it runs
3003a40ed3dSBarry Smith */
30151d3a450SBarry Smith #if defined(PETSC_USE_DEBUG)
3023a40ed3dSBarry Smith 
303184914b5SBarry Smith #define PETSCSTACKSIZE 15
304184914b5SBarry Smith 
3053a40ed3dSBarry Smith typedef struct  {
3060e33f6ddSBarry Smith   const char *function[PETSCSTACKSIZE];
3070e33f6ddSBarry Smith   const char *file[PETSCSTACKSIZE];
3080e33f6ddSBarry Smith   const char *directory[PETSCSTACKSIZE];
309184914b5SBarry Smith         int  line[PETSCSTACKSIZE];
310184914b5SBarry Smith         int currentsize;
3113a40ed3dSBarry Smith } PetscStack;
3123a40ed3dSBarry Smith 
3133a40ed3dSBarry Smith extern PetscStack *petscstack;
314dfbe8321SBarry Smith EXTERN PetscErrorCode PetscStackCopy(PetscStack*,PetscStack*);
315dfbe8321SBarry Smith EXTERN PetscErrorCode PetscStackPrint(PetscStack*,FILE* fp);
316184914b5SBarry Smith 
317184914b5SBarry Smith #define PetscStackActive (petscstack != 0)
3183a40ed3dSBarry Smith 
3196d385327SIbrahima Ba 
32030de9b25SBarry Smith /*MC
32130de9b25SBarry Smith    PetscFunctionBegin - First executable line of each PETSc function
32230de9b25SBarry Smith         used for error handling.
32330de9b25SBarry Smith 
32430de9b25SBarry Smith    Synopsis:
32530de9b25SBarry Smith    void PetscFunctionBegin;
32630de9b25SBarry Smith 
32730de9b25SBarry Smith    Usage:
32830de9b25SBarry Smith .vb
32930de9b25SBarry Smith      int something;
33030de9b25SBarry Smith 
33130de9b25SBarry Smith      PetscFunctionBegin;
33230de9b25SBarry Smith .ve
33330de9b25SBarry Smith 
33430de9b25SBarry Smith    Notes:
33530de9b25SBarry Smith      Not available in Fortran
33630de9b25SBarry Smith 
33730de9b25SBarry Smith    Level: developer
33830de9b25SBarry Smith 
33930de9b25SBarry Smith .seealso: PetscFunctionReturn()
34030de9b25SBarry Smith 
34130de9b25SBarry Smith .keywords: traceback, error handling
34230de9b25SBarry Smith M*/
3433a40ed3dSBarry Smith #define PetscFunctionBegin \
344beb17490SBarry Smith   {\
345184914b5SBarry Smith    if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
3464a2ae208SSatish Balay     petscstack->function[petscstack->currentsize]  = __FUNCT__; \
347184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = __FILE__; \
348184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = __SDIR__; \
349184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = __LINE__; \
350184914b5SBarry Smith     petscstack->currentsize++; \
3513a40ed3dSBarry Smith   }}
3523a40ed3dSBarry Smith 
3535cd90555SBarry Smith #define PetscStackPush(n) \
354184914b5SBarry Smith   {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
355184914b5SBarry Smith     petscstack->function[petscstack->currentsize]  = n; \
356184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = "unknown"; \
357184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = "unknown"; \
358184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = 0; \
359184914b5SBarry Smith     petscstack->currentsize++; \
3605cd90555SBarry Smith   }}
3613a40ed3dSBarry Smith 
362d64ed03dSBarry Smith #define PetscStackPop \
363184914b5SBarry Smith   {if (petscstack && petscstack->currentsize > 0) {     \
364184914b5SBarry Smith     petscstack->currentsize--; \
365184914b5SBarry Smith     petscstack->function[petscstack->currentsize]  = 0; \
366184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = 0; \
367184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = 0; \
368184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = 0; \
3695cd90555SBarry Smith   }};
370d64ed03dSBarry Smith 
37130de9b25SBarry Smith /*MC
37230de9b25SBarry Smith    PetscFunctionReturn - Last executable line of each PETSc function
37330de9b25SBarry Smith         used for error handling. Replaces return()
37430de9b25SBarry Smith 
37530de9b25SBarry Smith    Synopsis:
37630de9b25SBarry Smith    void PetscFunctionReturn(0);
37730de9b25SBarry Smith 
37830de9b25SBarry Smith    Usage:
37930de9b25SBarry Smith .vb
38030de9b25SBarry Smith     ....
38130de9b25SBarry Smith      PetscFunctionReturn(0);
38230de9b25SBarry Smith    }
38330de9b25SBarry Smith .ve
38430de9b25SBarry Smith 
38530de9b25SBarry Smith    Notes:
38630de9b25SBarry Smith      Not available in Fortran
38730de9b25SBarry Smith 
38830de9b25SBarry Smith    Level: developer
38930de9b25SBarry Smith 
39030de9b25SBarry Smith .seealso: PetscFunctionBegin()
39130de9b25SBarry Smith 
39230de9b25SBarry Smith .keywords: traceback, error handling
39330de9b25SBarry Smith M*/
3945cd90555SBarry Smith #define PetscFunctionReturn(a) \
395beb17490SBarry Smith   {\
39606d1fe2cSBarry Smith   PetscStackPop; \
3975cd90555SBarry Smith   return(a);}
398d64ed03dSBarry Smith 
399ff94ddecSSatish Balay #define PetscFunctionReturnVoid() \
400ff94ddecSSatish Balay   {\
4011fceb228SKris Buschelman   PetscStackPop; \
4021fceb228SKris Buschelman   return;}
403ff94ddecSSatish Balay 
4046d385327SIbrahima Ba 
4056d385327SIbrahima Ba #else
4066d385327SIbrahima Ba 
4073a40ed3dSBarry Smith #define PetscFunctionBegin
4083a40ed3dSBarry Smith #define PetscFunctionReturn(a)  return(a)
4091fceb228SKris Buschelman #define PetscFunctionReturnVoid() return()
410d64ed03dSBarry Smith #define PetscStackPop
411d64ed03dSBarry Smith #define PetscStackPush(f)
412d64ed03dSBarry Smith #define PetscStackActive        0
4133a40ed3dSBarry Smith 
4143a40ed3dSBarry Smith #endif
4153a40ed3dSBarry Smith 
416dfbe8321SBarry Smith EXTERN PetscErrorCode PetscStackCreate(void);
417dfbe8321SBarry Smith EXTERN PetscErrorCode PetscStackView(PetscViewer);
418dfbe8321SBarry Smith EXTERN PetscErrorCode PetscStackDestroy(void);
419dfbe8321SBarry Smith EXTERN PetscErrorCode PetscStackPublish(void);
420dfbe8321SBarry Smith EXTERN PetscErrorCode PetscStackDepublish(void);
4213a40ed3dSBarry Smith 
42206d1fe2cSBarry Smith 
423e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END
42406d1fe2cSBarry Smith #endif
425