xref: /petsc/src/sys/error/errtrace.c (revision 5f3a2c8d2eb854e17dd5fe8a5a2771eb05a911d7)
1e5c89e4eSSatish Balay 
2c6db04a5SJed Brown #include <petscsys.h>        /*I "petscsys.h" I*/
3c6db04a5SJed Brown #include <petscconfiginfo.h>
4114011d0SBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
5114011d0SBarry Smith #include <unistd.h>
6114011d0SBarry Smith #endif
7e5c89e4eSSatish Balay 
8e5c89e4eSSatish Balay #undef __FUNCT__
9e5c89e4eSSatish Balay #define __FUNCT__ "PetscIgnoreErrorHandler"
10e5c89e4eSSatish Balay /*@C
11e5c89e4eSSatish Balay    PetscIgnoreErrorHandler - Ignores the error, allows program to continue as if error did not occure
12e5c89e4eSSatish Balay 
13e5c89e4eSSatish Balay    Not Collective
14e5c89e4eSSatish Balay 
15e5c89e4eSSatish Balay    Input Parameters:
16e32f2f54SBarry Smith +  comm - communicator over which error occurred
17e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
18e5c89e4eSSatish Balay .  func - the function where error is detected (indicated by __FUNCT__)
19e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
20e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
21e5c89e4eSSatish Balay .  n - the generic error number
22e5c89e4eSSatish Balay .  p - specific error number
23e5c89e4eSSatish Balay -  ctx - error handler context
24e5c89e4eSSatish Balay 
25e5c89e4eSSatish Balay    Level: developer
26e5c89e4eSSatish Balay 
27e5c89e4eSSatish Balay    Notes:
28e5c89e4eSSatish Balay    Most users need not directly employ this routine and the other error
29e5c89e4eSSatish Balay    handlers, but can instead use the simplified interface SETERRQ, which has
30e5c89e4eSSatish Balay    the calling sequence
31e32f2f54SBarry Smith $     SETERRQ(comm,number,p,mess)
32e5c89e4eSSatish Balay 
33e5c89e4eSSatish Balay    Notes for experienced users:
34e5c89e4eSSatish Balay    Use PetscPushErrorHandler() to set the desired error handler.  The
35e5c89e4eSSatish Balay    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
36e8fb0fc0SBarry Smith    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler()
37e5c89e4eSSatish Balay 
38e5c89e4eSSatish Balay    Concepts: error handler^traceback
39e5c89e4eSSatish Balay    Concepts: traceback^generating
40e5c89e4eSSatish Balay 
41e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
42e5c89e4eSSatish Balay           PetscAbortErrorHandler(), PetscTraceBackErrorHandler()
43e5c89e4eSSatish Balay  @*/
44efca3c55SSatish Balay PetscErrorCode  PetscIgnoreErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
45e5c89e4eSSatish Balay {
46e5c89e4eSSatish Balay   PetscFunctionBegin;
47e5c89e4eSSatish Balay   PetscFunctionReturn(n);
48e5c89e4eSSatish Balay }
49e5c89e4eSSatish Balay 
50107894f0SSatish Balay /* ---------------------------------------------------------------------------------------*/
51107894f0SSatish Balay 
525abee1b0SJed Brown static char      arch[128],hostname[128],username[128],pname[PETSC_MAX_PATH_LEN],date[128];
53ace3abfcSBarry Smith static PetscBool PetscErrorPrintfInitializeCalled = PETSC_FALSE;
543f6e4ae9SSatish Balay static char      version[256];
55107894f0SSatish Balay 
56107894f0SSatish Balay #undef __FUNCT__
57107894f0SSatish Balay #define __FUNCT__ "PetscErrorPrintfInitialize"
58107894f0SSatish Balay /*
59107894f0SSatish Balay    Initializes arch, hostname, username,date so that system calls do NOT need
60107894f0SSatish Balay    to be made during the error handler.
61107894f0SSatish Balay */
627087cfbeSBarry Smith PetscErrorCode  PetscErrorPrintfInitialize()
63107894f0SSatish Balay {
64107894f0SSatish Balay   PetscErrorCode ierr;
65ace3abfcSBarry Smith   PetscBool      use_stdout = PETSC_FALSE,use_none = PETSC_FALSE;
66107894f0SSatish Balay 
67107894f0SSatish Balay   PetscFunctionBegin;
685abee1b0SJed Brown   ierr = PetscGetArchType(arch,sizeof(arch));CHKERRQ(ierr);
695abee1b0SJed Brown   ierr = PetscGetHostName(hostname,sizeof(hostname));CHKERRQ(ierr);
705abee1b0SJed Brown   ierr = PetscGetUserName(username,sizeof(username));CHKERRQ(ierr);
71107894f0SSatish Balay   ierr = PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
725abee1b0SJed Brown   ierr = PetscGetDate(date,sizeof(date));CHKERRQ(ierr);
735abee1b0SJed Brown   ierr = PetscGetVersion(version,sizeof(version));CHKERRQ(ierr);
74e8fb0fc0SBarry Smith 
750298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-error_output_stdout",&use_stdout,NULL);CHKERRQ(ierr);
76a297a907SKarl Rupp   if (use_stdout) PETSC_STDERR = PETSC_STDOUT;
770298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-error_output_none",&use_none,NULL);CHKERRQ(ierr);
78a297a907SKarl Rupp   if (use_none) PetscErrorPrintf = PetscErrorPrintfNone;
79107894f0SSatish Balay   PetscErrorPrintfInitializeCalled = PETSC_TRUE;
80107894f0SSatish Balay   PetscFunctionReturn(0);
81107894f0SSatish Balay }
82107894f0SSatish Balay 
83e8fb0fc0SBarry Smith #undef __FUNCT__
84e8fb0fc0SBarry Smith #define __FUNCT__ "PetscErrorPrintfNone"
857087cfbeSBarry Smith PetscErrorCode  PetscErrorPrintfNone(const char format[],...)
86e8fb0fc0SBarry Smith {
87e8fb0fc0SBarry Smith   return 0;
88e8fb0fc0SBarry Smith }
89e8fb0fc0SBarry Smith 
90e8fb0fc0SBarry Smith #undef __FUNCT__
91e8fb0fc0SBarry Smith #define __FUNCT__ "PetscErrorPrintfDefault"
927087cfbeSBarry Smith PetscErrorCode  PetscErrorPrintfDefault(const char format[],...)
93e8fb0fc0SBarry Smith {
94e8fb0fc0SBarry Smith   va_list          Argp;
95ace3abfcSBarry Smith   static PetscBool PetscErrorPrintfCalled = PETSC_FALSE;
96e8fb0fc0SBarry Smith 
97e8fb0fc0SBarry Smith   /*
98e8fb0fc0SBarry Smith       This function does not call PetscFunctionBegin and PetscFunctionReturn() because
99e8fb0fc0SBarry Smith     it may be called by PetscStackView().
100e8fb0fc0SBarry Smith 
101e8fb0fc0SBarry Smith       This function does not do error checking because it is called by the error handlers.
102e8fb0fc0SBarry Smith   */
103e8fb0fc0SBarry Smith 
104e8fb0fc0SBarry Smith   if (!PetscErrorPrintfCalled) {
105e8fb0fc0SBarry Smith     PetscErrorPrintfCalled = PETSC_TRUE;
106e8fb0fc0SBarry Smith 
107e8fb0fc0SBarry Smith     /*
108e8fb0fc0SBarry Smith         On the SGI machines and Cray T3E, if errors are generated  "simultaneously" by
109e8fb0fc0SBarry Smith       different processors, the messages are printed all jumbled up; to try to
110e8fb0fc0SBarry Smith       prevent this we have each processor wait based on their rank
111e8fb0fc0SBarry Smith     */
112e8fb0fc0SBarry Smith #if defined(PETSC_CAN_SLEEP_AFTER_ERROR)
113e8fb0fc0SBarry Smith     {
114e8fb0fc0SBarry Smith       PetscMPIInt rank;
115a297a907SKarl Rupp       if (PetscGlobalRank > 8) rank = 8;
116a297a907SKarl Rupp       else rank = PetscGlobalRank;
117a6d0e24fSJed Brown       PetscSleep((PetscReal)rank);
118e8fb0fc0SBarry Smith     }
119e8fb0fc0SBarry Smith #endif
120e8fb0fc0SBarry Smith   }
121e8fb0fc0SBarry Smith 
1221179db26SBarry Smith   PetscFPrintf(PETSC_COMM_SELF,PETSC_STDERR,"[%d]PETSC ERROR: ",PetscGlobalRank);
123e8fb0fc0SBarry Smith   va_start(Argp,format);
1241179db26SBarry Smith   (*PetscVFPrintf)(PETSC_STDERR,format,Argp);
125e8fb0fc0SBarry Smith   va_end(Argp);
126e8fb0fc0SBarry Smith   return 0;
127e8fb0fc0SBarry Smith }
128e8fb0fc0SBarry Smith 
129114011d0SBarry Smith static void PetscErrorPrintfHilight(void)
130114011d0SBarry Smith {
131114011d0SBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
132114011d0SBarry Smith   if (PetscErrorPrintf == PetscErrorPrintfDefault) {
133114011d0SBarry Smith     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR,"\033[1;31m");
134114011d0SBarry Smith   }
135114011d0SBarry Smith #endif
136114011d0SBarry Smith }
137114011d0SBarry Smith 
138114011d0SBarry Smith static void PetscErrorPrintfNormal(void)
139114011d0SBarry Smith {
140114011d0SBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
141114011d0SBarry Smith   if (PetscErrorPrintf == PetscErrorPrintfDefault) {
142114011d0SBarry Smith     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR,"\033[0;39m\033[0;49m");
143114011d0SBarry Smith   }
144114011d0SBarry Smith #endif
145114011d0SBarry Smith }
146114011d0SBarry Smith 
147114011d0SBarry Smith 
148e5c89e4eSSatish Balay #undef __FUNCT__
149e5c89e4eSSatish Balay #define __FUNCT__ "PetscTraceBackErrorHandler"
150e5c89e4eSSatish Balay /*@C
151e5c89e4eSSatish Balay 
152e5c89e4eSSatish Balay    PetscTraceBackErrorHandler - Default error handler routine that generates
153e5c89e4eSSatish Balay    a traceback on error detection.
154e5c89e4eSSatish Balay 
155e5c89e4eSSatish Balay    Not Collective
156e5c89e4eSSatish Balay 
157e5c89e4eSSatish Balay    Input Parameters:
158e32f2f54SBarry Smith +  comm - communicator over which error occurred
159e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
160e5c89e4eSSatish Balay .  func - the function where error is detected (indicated by __FUNCT__)
161e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
162e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
163e5c89e4eSSatish Balay .  n - the generic error number
164d736bfebSBarry Smith .  p - PETSC_ERROR_INITIAL if this is the first call the the error handler, otherwise PETSC_ERROR_REPEAT
165e5c89e4eSSatish Balay -  ctx - error handler context
166e5c89e4eSSatish Balay 
167e5c89e4eSSatish Balay    Level: developer
168e5c89e4eSSatish Balay 
169e5c89e4eSSatish Balay    Notes:
170e5c89e4eSSatish Balay    Most users need not directly employ this routine and the other error
171e5c89e4eSSatish Balay    handlers, but can instead use the simplified interface SETERRQ, which has
172e5c89e4eSSatish Balay    the calling sequence
173d736bfebSBarry Smith $     SETERRQ(comm,number,n,mess)
174e5c89e4eSSatish Balay 
175e5c89e4eSSatish Balay    Notes for experienced users:
176e5c89e4eSSatish Balay    Use PetscPushErrorHandler() to set the desired error handler.  The
177e5c89e4eSSatish Balay    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
178e8fb0fc0SBarry Smith    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler()
179e5c89e4eSSatish Balay 
180e5c89e4eSSatish Balay    Concepts: error handler^traceback
181e5c89e4eSSatish Balay    Concepts: traceback^generating
182e5c89e4eSSatish Balay 
183e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
184e5c89e4eSSatish Balay           PetscAbortErrorHandler()
185e5c89e4eSSatish Balay  @*/
186efca3c55SSatish Balay PetscErrorCode  PetscTraceBackErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
187e5c89e4eSSatish Balay {
188e5c89e4eSSatish Balay   PetscLogDouble mem,rss;
189574034a9SJed Brown   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
190997adca8SBarry Smith   PetscMPIInt    rank = 0;
191e5c89e4eSSatish Balay 
192e5c89e4eSSatish Balay   PetscFunctionBegin;
193a297a907SKarl Rupp   if (comm != PETSC_COMM_SELF) MPI_Comm_rank(comm,&rank);
194a297a907SKarl Rupp 
195997adca8SBarry Smith   if (!rank) {
196114011d0SBarry Smith     PetscBool  ismain,isunknown;
197114011d0SBarry Smith     static int cnt = 1;
198114011d0SBarry Smith 
199668f157eSBarry Smith     if (p == PETSC_ERROR_INITIAL) {
200114011d0SBarry Smith       PetscErrorPrintfHilight();
201114011d0SBarry Smith       (*PetscErrorPrintf)("--------------------- Error Message --------------------------------------------------------------\n");
202*5f3a2c8dSBarry Smith       PetscErrorPrintfNormal();
203e5c89e4eSSatish Balay       if (n == PETSC_ERR_MEM) {
204e5c89e4eSSatish Balay         (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n");
205e5c89e4eSSatish Balay         (*PetscErrorPrintf)("too large an object or bleeding by not properly\n");
206e5c89e4eSSatish Balay         (*PetscErrorPrintf)("destroying unneeded objects.\n");
207e5c89e4eSSatish Balay         PetscMallocGetCurrentUsage(&mem);
208e5c89e4eSSatish Balay         PetscMemoryGetCurrentUsage(&rss);
2090298fd71SBarry Smith         PetscOptionsGetBool(NULL,"-malloc_dump",&flg1,NULL);
2100298fd71SBarry Smith         PetscOptionsGetBool(NULL,"-malloc_log",&flg2,NULL);
2110298fd71SBarry Smith         PetscOptionsHasName(NULL,"-malloc_log_threshold",&flg3);
212a297a907SKarl Rupp         if (flg2 || flg3) PetscMallocDumpLog(stdout);
213a297a907SKarl Rupp         else {
214b85f3346SJed Brown           (*PetscErrorPrintf)("Memory allocated %.0f Memory used by process %.0f\n",mem,rss);
215a297a907SKarl Rupp           if (flg1) PetscMallocDump(stdout);
216a297a907SKarl Rupp           else (*PetscErrorPrintf)("Try running with -malloc_dump or -malloc_log for info.\n");
217e5c89e4eSSatish Balay         }
218e5c89e4eSSatish Balay       } else {
219e5c89e4eSSatish Balay         const char *text;
2200298fd71SBarry Smith         PetscErrorMessage(n,&text,NULL);
221114011d0SBarry Smith         if (text) (*PetscErrorPrintf)("%s\n",text);
222e5c89e4eSSatish Balay       }
223114011d0SBarry Smith       if (mess) (*PetscErrorPrintf)("%s\n",mess);
224114011d0SBarry Smith       (*PetscErrorPrintf)("See http://http://www.mcs.anl.gov/petsc/documentation/faq.html for trouble shooting.\n");
225107894f0SSatish Balay       (*PetscErrorPrintf)("%s\n",version);
226a297a907SKarl Rupp       if (PetscErrorPrintfInitializeCalled) (*PetscErrorPrintf)("%s on a %s named %s by %s %s\n",pname,arch,hostname,username,date);
227107894f0SSatish Balay       (*PetscErrorPrintf)("Configure options %s\n",petscconfigureoptions);
228107894f0SSatish Balay     }
229997adca8SBarry Smith     /* print line of stack trace */
230114011d0SBarry Smith     (*PetscErrorPrintf)("#%d %s() line %d in %s\n",cnt++,fun,line,file);
231114011d0SBarry Smith     PetscStrncmp(fun,"main",4,&ismain);
232114011d0SBarry Smith     PetscStrncmp(fun,"unknown",7,&isunknown);
233114011d0SBarry Smith     if (ismain || isunknown) {
234114011d0SBarry Smith       PetscErrorPrintfHilight();
235114011d0SBarry Smith       (*PetscErrorPrintf)("----------------End of Error Message -------send entire error message to petsc-maint@mcs.anl.gov----------\033[0;39m\033[0;49m\n");
236114011d0SBarry Smith       PetscErrorPrintfNormal();
237114011d0SBarry Smith     }
238997adca8SBarry Smith   } else {
239997adca8SBarry Smith     /* do not print error messages since process 0 will print them, sleep before aborting so will not accidently kill process 0*/
240997adca8SBarry Smith     PetscSleep(10.0);
241997adca8SBarry Smith     abort();
242997adca8SBarry Smith   }
243e5c89e4eSSatish Balay   PetscFunctionReturn(n);
244e5c89e4eSSatish Balay }
245e5c89e4eSSatish Balay 
246