xref: /petsc/src/sys/error/errtrace.c (revision 589a23caa660d2a5f330cc8d1ed213e9cfaf51a7)
10039db0dSBarry Smith #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for fileno() */
2c6db04a5SJed Brown #include <petscsys.h>        /*I "petscsys.h" I*/
3f67a399dSBarry Smith #include <petsc/private/petscimpl.h>
4c6db04a5SJed Brown #include <petscconfiginfo.h>
5114011d0SBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
6114011d0SBarry Smith #include <unistd.h>
7114011d0SBarry Smith #endif
8e5c89e4eSSatish Balay 
9e5c89e4eSSatish Balay /*@C
10e5c89e4eSSatish Balay    PetscIgnoreErrorHandler - Ignores the error, allows program to continue as if error did not occure
11e5c89e4eSSatish Balay 
12e5c89e4eSSatish Balay    Not Collective
13e5c89e4eSSatish Balay 
14e5c89e4eSSatish Balay    Input Parameters:
15e32f2f54SBarry Smith +  comm - communicator over which error occurred
16e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
17e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
18e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
19e5c89e4eSSatish Balay .  n - the generic error number
20e5c89e4eSSatish Balay .  p - specific error number
21e5c89e4eSSatish Balay -  ctx - error handler context
22e5c89e4eSSatish Balay 
23e5c89e4eSSatish Balay    Level: developer
24e5c89e4eSSatish Balay 
25e5c89e4eSSatish Balay    Notes:
26e5c89e4eSSatish Balay    Most users need not directly employ this routine and the other error
27e5c89e4eSSatish Balay    handlers, but can instead use the simplified interface SETERRQ, which has
28e5c89e4eSSatish Balay    the calling sequence
29e32f2f54SBarry Smith $     SETERRQ(comm,number,p,mess)
30e5c89e4eSSatish Balay 
31e5c89e4eSSatish Balay    Notes for experienced users:
32e5c89e4eSSatish Balay    Use PetscPushErrorHandler() to set the desired error handler.  The
33e5c89e4eSSatish Balay    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
34e8fb0fc0SBarry Smith    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler()
35e5c89e4eSSatish Balay 
36e5c89e4eSSatish Balay 
37e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
38e5c89e4eSSatish Balay           PetscAbortErrorHandler(), PetscTraceBackErrorHandler()
39e5c89e4eSSatish Balay  @*/
40efca3c55SSatish Balay PetscErrorCode  PetscIgnoreErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
41e5c89e4eSSatish Balay {
42e5c89e4eSSatish Balay   PetscFunctionBegin;
43e5c89e4eSSatish Balay   PetscFunctionReturn(n);
44e5c89e4eSSatish Balay }
45e5c89e4eSSatish Balay 
46107894f0SSatish Balay /* ---------------------------------------------------------------------------------------*/
47107894f0SSatish Balay 
485abee1b0SJed Brown static char      arch[128],hostname[128],username[128],pname[PETSC_MAX_PATH_LEN],date[128];
49ace3abfcSBarry Smith static PetscBool PetscErrorPrintfInitializeCalled = PETSC_FALSE;
503f6e4ae9SSatish Balay static char      version[256];
51107894f0SSatish Balay 
52107894f0SSatish Balay /*
53107894f0SSatish Balay    Initializes arch, hostname, username,date so that system calls do NOT need
54107894f0SSatish Balay    to be made during the error handler.
55107894f0SSatish Balay */
56dd63322aSSatish Balay PetscErrorCode  PetscErrorPrintfInitialize(void)
57107894f0SSatish Balay {
58107894f0SSatish Balay   PetscErrorCode ierr;
59ace3abfcSBarry Smith   PetscBool      use_stdout = PETSC_FALSE,use_none = PETSC_FALSE;
60107894f0SSatish Balay 
61107894f0SSatish Balay   PetscFunctionBegin;
625abee1b0SJed Brown   ierr = PetscGetArchType(arch,sizeof(arch));CHKERRQ(ierr);
635abee1b0SJed Brown   ierr = PetscGetHostName(hostname,sizeof(hostname));CHKERRQ(ierr);
645abee1b0SJed Brown   ierr = PetscGetUserName(username,sizeof(username));CHKERRQ(ierr);
65*589a23caSBarry Smith   ierr = PetscGetProgramName(pname,sizeof(pname));CHKERRQ(ierr);
665abee1b0SJed Brown   ierr = PetscGetDate(date,sizeof(date));CHKERRQ(ierr);
675abee1b0SJed Brown   ierr = PetscGetVersion(version,sizeof(version));CHKERRQ(ierr);
68e8fb0fc0SBarry Smith 
69c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-error_output_stdout",&use_stdout,NULL);CHKERRQ(ierr);
70a297a907SKarl Rupp   if (use_stdout) PETSC_STDERR = PETSC_STDOUT;
71c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-error_output_none",&use_none,NULL);CHKERRQ(ierr);
72a297a907SKarl Rupp   if (use_none) PetscErrorPrintf = PetscErrorPrintfNone;
73107894f0SSatish Balay   PetscErrorPrintfInitializeCalled = PETSC_TRUE;
74107894f0SSatish Balay   PetscFunctionReturn(0);
75107894f0SSatish Balay }
76107894f0SSatish Balay 
777087cfbeSBarry Smith PetscErrorCode  PetscErrorPrintfNone(const char format[],...)
78e8fb0fc0SBarry Smith {
79e8fb0fc0SBarry Smith   return 0;
80e8fb0fc0SBarry Smith }
81e8fb0fc0SBarry Smith 
827087cfbeSBarry Smith PetscErrorCode  PetscErrorPrintfDefault(const char format[],...)
83e8fb0fc0SBarry Smith {
84e8fb0fc0SBarry Smith   va_list          Argp;
85ace3abfcSBarry Smith   static PetscBool PetscErrorPrintfCalled = PETSC_FALSE;
86e8fb0fc0SBarry Smith 
87e8fb0fc0SBarry Smith   /*
88e8fb0fc0SBarry Smith       This function does not call PetscFunctionBegin and PetscFunctionReturn() because
89e8fb0fc0SBarry Smith     it may be called by PetscStackView().
90e8fb0fc0SBarry Smith 
91e8fb0fc0SBarry Smith       This function does not do error checking because it is called by the error handlers.
92e8fb0fc0SBarry Smith   */
93e8fb0fc0SBarry Smith 
94e8fb0fc0SBarry Smith   if (!PetscErrorPrintfCalled) {
95e8fb0fc0SBarry Smith     PetscErrorPrintfCalled = PETSC_TRUE;
96e8fb0fc0SBarry Smith 
97e8fb0fc0SBarry Smith     /*
98e8fb0fc0SBarry Smith         On the SGI machines and Cray T3E, if errors are generated  "simultaneously" by
99e8fb0fc0SBarry Smith       different processors, the messages are printed all jumbled up; to try to
100e8fb0fc0SBarry Smith       prevent this we have each processor wait based on their rank
101e8fb0fc0SBarry Smith     */
102e8fb0fc0SBarry Smith #if defined(PETSC_CAN_SLEEP_AFTER_ERROR)
103e8fb0fc0SBarry Smith     {
104e8fb0fc0SBarry Smith       PetscMPIInt rank;
105a297a907SKarl Rupp       if (PetscGlobalRank > 8) rank = 8;
106a297a907SKarl Rupp       else rank = PetscGlobalRank;
107a6d0e24fSJed Brown       PetscSleep((PetscReal)rank);
108e8fb0fc0SBarry Smith     }
109e8fb0fc0SBarry Smith #endif
110e8fb0fc0SBarry Smith   }
111e8fb0fc0SBarry Smith 
1121179db26SBarry Smith   PetscFPrintf(PETSC_COMM_SELF,PETSC_STDERR,"[%d]PETSC ERROR: ",PetscGlobalRank);
113e8fb0fc0SBarry Smith   va_start(Argp,format);
1141179db26SBarry Smith   (*PetscVFPrintf)(PETSC_STDERR,format,Argp);
115e8fb0fc0SBarry Smith   va_end(Argp);
116e8fb0fc0SBarry Smith   return 0;
117e8fb0fc0SBarry Smith }
118e8fb0fc0SBarry Smith 
119114011d0SBarry Smith static void PetscErrorPrintfHilight(void)
120114011d0SBarry Smith {
12198ed35c3SBarry Smith #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
122114011d0SBarry Smith   if (PetscErrorPrintf == PetscErrorPrintfDefault) {
123114011d0SBarry Smith     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR,"\033[1;31m");
124114011d0SBarry Smith   }
125114011d0SBarry Smith #endif
126114011d0SBarry Smith }
127114011d0SBarry Smith 
128114011d0SBarry Smith static void PetscErrorPrintfNormal(void)
129114011d0SBarry Smith {
13098ed35c3SBarry Smith #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
131114011d0SBarry Smith   if (PetscErrorPrintf == PetscErrorPrintfDefault) {
132114011d0SBarry Smith     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR,"\033[0;39m\033[0;49m");
133114011d0SBarry Smith   }
134114011d0SBarry Smith #endif
135114011d0SBarry Smith }
136114011d0SBarry Smith 
13795c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode  PetscOptionsViewError(void);
138114011d0SBarry Smith 
139e5c89e4eSSatish Balay /*@C
140e5c89e4eSSatish Balay 
141e5c89e4eSSatish Balay    PetscTraceBackErrorHandler - Default error handler routine that generates
142e5c89e4eSSatish Balay    a traceback on error detection.
143e5c89e4eSSatish Balay 
144e5c89e4eSSatish Balay    Not Collective
145e5c89e4eSSatish Balay 
146e5c89e4eSSatish Balay    Input Parameters:
147e32f2f54SBarry Smith +  comm - communicator over which error occurred
148e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
149e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
150e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
151e5c89e4eSSatish Balay .  n - the generic error number
152df3898eeSBarry Smith .  p - PETSC_ERROR_INITIAL if this is the first call the error handler, otherwise PETSC_ERROR_REPEAT
153e5c89e4eSSatish Balay -  ctx - error handler context
154e5c89e4eSSatish Balay 
155e5c89e4eSSatish Balay    Level: developer
156e5c89e4eSSatish Balay 
157e5c89e4eSSatish Balay    Notes:
158e5c89e4eSSatish Balay    Most users need not directly employ this routine and the other error
159e5c89e4eSSatish Balay    handlers, but can instead use the simplified interface SETERRQ, which has
160e5c89e4eSSatish Balay    the calling sequence
161d736bfebSBarry Smith $     SETERRQ(comm,number,n,mess)
162e5c89e4eSSatish Balay 
163e5c89e4eSSatish Balay    Notes for experienced users:
164e5c89e4eSSatish Balay    Use PetscPushErrorHandler() to set the desired error handler.  The
165e5c89e4eSSatish Balay    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
166e8fb0fc0SBarry Smith    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler()
167e5c89e4eSSatish Balay 
168e5c89e4eSSatish Balay 
169e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
170e5c89e4eSSatish Balay           PetscAbortErrorHandler()
171e5c89e4eSSatish Balay  @*/
172efca3c55SSatish Balay PetscErrorCode  PetscTraceBackErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
173e5c89e4eSSatish Balay {
174e5c89e4eSSatish Balay   PetscLogDouble mem,rss;
175574034a9SJed Brown   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
176997adca8SBarry Smith   PetscMPIInt    rank = 0;
177e5c89e4eSSatish Balay 
178e5c89e4eSSatish Balay   PetscFunctionBegin;
179a297a907SKarl Rupp   if (comm != PETSC_COMM_SELF) MPI_Comm_rank(comm,&rank);
180a297a907SKarl Rupp 
181997adca8SBarry Smith   if (!rank) {
182fbfcfee5SBarry Smith     PetscBool  ismain;
183114011d0SBarry Smith     static int cnt = 1;
184114011d0SBarry Smith 
185668f157eSBarry Smith     if (p == PETSC_ERROR_INITIAL) {
186114011d0SBarry Smith       PetscErrorPrintfHilight();
187114011d0SBarry Smith       (*PetscErrorPrintf)("--------------------- Error Message --------------------------------------------------------------\n");
1885f3a2c8dSBarry Smith       PetscErrorPrintfNormal();
189e5c89e4eSSatish Balay       if (n == PETSC_ERR_MEM) {
190e5c89e4eSSatish Balay         (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n");
191e5c89e4eSSatish Balay         (*PetscErrorPrintf)("too large an object or bleeding by not properly\n");
192e5c89e4eSSatish Balay         (*PetscErrorPrintf)("destroying unneeded objects.\n");
193e5c89e4eSSatish Balay         PetscMallocGetCurrentUsage(&mem);
194e5c89e4eSSatish Balay         PetscMemoryGetCurrentUsage(&rss);
195c5929fdfSBarry Smith         PetscOptionsGetBool(NULL,NULL,"-malloc_dump",&flg1,NULL);
19692f119d6SBarry Smith         PetscOptionsGetBool(NULL,NULL,"-malloc_view",&flg2,NULL);
19792f119d6SBarry Smith         PetscOptionsHasName(NULL,NULL,"-malloc_view_threshold",&flg3);
19892f119d6SBarry Smith         if (flg2 || flg3) PetscMallocView(stdout);
199a297a907SKarl Rupp         else {
200b85f3346SJed Brown           (*PetscErrorPrintf)("Memory allocated %.0f Memory used by process %.0f\n",mem,rss);
201a297a907SKarl Rupp           if (flg1) PetscMallocDump(stdout);
20292f119d6SBarry Smith           else (*PetscErrorPrintf)("Try running with -malloc_dump or -malloc_view for info.\n");
203e5c89e4eSSatish Balay         }
204e5c89e4eSSatish Balay       } else {
205e5c89e4eSSatish Balay         const char *text;
2060298fd71SBarry Smith         PetscErrorMessage(n,&text,NULL);
207114011d0SBarry Smith         if (text) (*PetscErrorPrintf)("%s\n",text);
208e5c89e4eSSatish Balay       }
209114011d0SBarry Smith       if (mess) (*PetscErrorPrintf)("%s\n",mess);
210a8d69d7bSBarry Smith       (*PetscErrorPrintf)("See https://www.mcs.anl.gov/petsc/documentation/faq.html for trouble shooting.\n");
211107894f0SSatish Balay       (*PetscErrorPrintf)("%s\n",version);
212a297a907SKarl Rupp       if (PetscErrorPrintfInitializeCalled) (*PetscErrorPrintf)("%s on a %s named %s by %s %s\n",pname,arch,hostname,username,date);
213107894f0SSatish Balay       (*PetscErrorPrintf)("Configure options %s\n",petscconfigureoptions);
214107894f0SSatish Balay     }
215997adca8SBarry Smith     /* print line of stack trace */
216114011d0SBarry Smith     (*PetscErrorPrintf)("#%d %s() line %d in %s\n",cnt++,fun,line,file);
217114011d0SBarry Smith     PetscStrncmp(fun,"main",4,&ismain);
218fbfcfee5SBarry Smith     if (ismain) {
219e11779c2SBarry Smith       PetscOptionsViewError();
220114011d0SBarry Smith       PetscErrorPrintfHilight();
221f15a11d2SBarry Smith       (*PetscErrorPrintf)("----------------End of Error Message -------send entire error message to petsc-maint@mcs.anl.gov----------\n");
222114011d0SBarry Smith       PetscErrorPrintfNormal();
223114011d0SBarry Smith     }
224997adca8SBarry Smith   } else {
225997adca8SBarry Smith     /* do not print error messages since process 0 will print them, sleep before aborting so will not accidently kill process 0*/
226997adca8SBarry Smith     PetscSleep(10.0);
227997adca8SBarry Smith     abort();
228997adca8SBarry Smith   }
229e5c89e4eSSatish Balay   PetscFunctionReturn(n);
230e5c89e4eSSatish Balay }
231e5c89e4eSSatish Balay 
232