xref: /petsc/src/sys/error/errtrace.c (revision e32f2f54e699d0aa6e733466c00da7e34666fe5e)
1e5c89e4eSSatish Balay #define PETSC_DLL
2e5c89e4eSSatish Balay 
3107894f0SSatish Balay #include "petscsys.h"        /*I "petscsys.h" I*/
4107894f0SSatish Balay #include "petscconfiginfo.h"
5e5c89e4eSSatish Balay 
6e5c89e4eSSatish Balay #undef __FUNCT__
7e5c89e4eSSatish Balay #define __FUNCT__ "PetscIgnoreErrorHandler"
8e5c89e4eSSatish Balay /*@C
9e5c89e4eSSatish Balay    PetscIgnoreErrorHandler - Ignores the error, allows program to continue as if error did not occure
10e5c89e4eSSatish Balay 
11e5c89e4eSSatish Balay    Not Collective
12e5c89e4eSSatish Balay 
13e5c89e4eSSatish Balay    Input Parameters:
14*e32f2f54SBarry Smith +  comm - communicator over which error occurred
15*e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
16e5c89e4eSSatish Balay .  func - the function where error is detected (indicated by __FUNCT__)
17e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
18e5c89e4eSSatish Balay .  dir - the directory of the file (indicated by __SDIR__)
19e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
20e5c89e4eSSatish Balay .  n - the generic error number
21e5c89e4eSSatish Balay .  p - specific error number
22e5c89e4eSSatish Balay -  ctx - error handler context
23e5c89e4eSSatish Balay 
24e5c89e4eSSatish Balay    Level: developer
25e5c89e4eSSatish Balay 
26e5c89e4eSSatish Balay    Notes:
27e5c89e4eSSatish Balay    Most users need not directly employ this routine and the other error
28e5c89e4eSSatish Balay    handlers, but can instead use the simplified interface SETERRQ, which has
29e5c89e4eSSatish Balay    the calling sequence
30*e32f2f54SBarry Smith $     SETERRQ(comm,number,p,mess)
31e5c89e4eSSatish Balay 
32e5c89e4eSSatish Balay    Notes for experienced users:
33e5c89e4eSSatish Balay    Use PetscPushErrorHandler() to set the desired error handler.  The
34e5c89e4eSSatish Balay    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
35e8fb0fc0SBarry Smith    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler()
36e5c89e4eSSatish Balay 
37e5c89e4eSSatish Balay    Concepts: error handler^traceback
38e5c89e4eSSatish Balay    Concepts: traceback^generating
39e5c89e4eSSatish Balay 
40e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
41e5c89e4eSSatish Balay           PetscAbortErrorHandler(), PetscTraceBackErrorHandler()
42e5c89e4eSSatish Balay  @*/
43*e32f2f54SBarry Smith PetscErrorCode PETSC_DLLEXPORT PetscIgnoreErrorHandler(MPI_Comm comm,int line,const char *fun,const char* file,const char *dir,PetscErrorCode n,int p,const char *mess,void *ctx)
44e5c89e4eSSatish Balay {
45e5c89e4eSSatish Balay   PetscFunctionBegin;
46e5c89e4eSSatish Balay   PetscFunctionReturn(n);
47e5c89e4eSSatish Balay }
48e5c89e4eSSatish Balay 
49107894f0SSatish Balay /* ---------------------------------------------------------------------------------------*/
50107894f0SSatish Balay 
51107894f0SSatish Balay static char  arch[10],hostname[64],username[16],pname[PETSC_MAX_PATH_LEN],date[64];
52107894f0SSatish Balay static PetscTruth PetscErrorPrintfInitializeCalled = PETSC_FALSE;
533f6e4ae9SSatish Balay static char version[256];
54107894f0SSatish Balay 
55107894f0SSatish Balay #undef __FUNCT__
56107894f0SSatish Balay #define __FUNCT__ "PetscErrorPrintfInitialize"
57107894f0SSatish Balay /*
58107894f0SSatish Balay    Initializes arch, hostname, username,date so that system calls do NOT need
59107894f0SSatish Balay    to be made during the error handler.
60107894f0SSatish Balay */
61107894f0SSatish Balay PetscErrorCode PETSC_DLLEXPORT PetscErrorPrintfInitialize()
62107894f0SSatish Balay {
63107894f0SSatish Balay   PetscErrorCode ierr;
6490d69ab7SBarry Smith   PetscTruth     use_stdout = PETSC_FALSE,use_none = PETSC_FALSE;
65107894f0SSatish Balay 
66107894f0SSatish Balay   PetscFunctionBegin;
67107894f0SSatish Balay   ierr = PetscGetArchType(arch,10);CHKERRQ(ierr);
68107894f0SSatish Balay   ierr = PetscGetHostName(hostname,64);CHKERRQ(ierr);
69107894f0SSatish Balay   ierr = PetscGetUserName(username,16);CHKERRQ(ierr);
70107894f0SSatish Balay   ierr = PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
71107894f0SSatish Balay   ierr = PetscGetDate(date,64);CHKERRQ(ierr);
72a523d312SBarry Smith   ierr = PetscGetVersion(version,256);CHKERRQ(ierr);
73e8fb0fc0SBarry Smith 
7490d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-error_output_stdout",&use_stdout,PETSC_NULL);CHKERRQ(ierr);
751179db26SBarry Smith   if (use_stdout) {
761179db26SBarry Smith     PETSC_STDERR = PETSC_STDOUT;
771179db26SBarry Smith   }
7890d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-error_output_none",&use_none,PETSC_NULL);CHKERRQ(ierr);
791179db26SBarry Smith   if (use_none) {
801179db26SBarry Smith     PetscErrorPrintf = PetscErrorPrintfNone;
81e8fb0fc0SBarry Smith   }
82107894f0SSatish Balay   PetscErrorPrintfInitializeCalled = PETSC_TRUE;
83107894f0SSatish Balay   PetscFunctionReturn(0);
84107894f0SSatish Balay }
85107894f0SSatish Balay 
86e8fb0fc0SBarry Smith #undef __FUNCT__
87e8fb0fc0SBarry Smith #define __FUNCT__ "PetscErrorPrintfNone"
88e8fb0fc0SBarry Smith PetscErrorCode PETSC_DLLEXPORT PetscErrorPrintfNone(const char format[],...)
89e8fb0fc0SBarry Smith {
90e8fb0fc0SBarry Smith   return 0;
91e8fb0fc0SBarry Smith }
92e8fb0fc0SBarry Smith 
93e8fb0fc0SBarry Smith #undef __FUNCT__
94e8fb0fc0SBarry Smith #define __FUNCT__ "PetscErrorPrintfDefault"
95e8fb0fc0SBarry Smith PetscErrorCode PETSC_DLLEXPORT PetscErrorPrintfDefault(const char format[],...)
96e8fb0fc0SBarry Smith {
97e8fb0fc0SBarry Smith   va_list           Argp;
98e8fb0fc0SBarry Smith   static PetscTruth PetscErrorPrintfCalled = PETSC_FALSE;
99e8fb0fc0SBarry Smith 
100e8fb0fc0SBarry Smith   /*
101e8fb0fc0SBarry Smith       This function does not call PetscFunctionBegin and PetscFunctionReturn() because
102e8fb0fc0SBarry Smith     it may be called by PetscStackView().
103e8fb0fc0SBarry Smith 
104e8fb0fc0SBarry Smith       This function does not do error checking because it is called by the error handlers.
105e8fb0fc0SBarry Smith   */
106e8fb0fc0SBarry Smith 
107e8fb0fc0SBarry Smith   if (!PetscErrorPrintfCalled) {
108e8fb0fc0SBarry Smith     PetscErrorPrintfCalled = PETSC_TRUE;
109e8fb0fc0SBarry Smith 
110e8fb0fc0SBarry Smith     /*
111e8fb0fc0SBarry Smith         On the SGI machines and Cray T3E, if errors are generated  "simultaneously" by
112e8fb0fc0SBarry Smith       different processors, the messages are printed all jumbled up; to try to
113e8fb0fc0SBarry Smith       prevent this we have each processor wait based on their rank
114e8fb0fc0SBarry Smith     */
115e8fb0fc0SBarry Smith #if defined(PETSC_CAN_SLEEP_AFTER_ERROR)
116e8fb0fc0SBarry Smith     {
117e8fb0fc0SBarry Smith       PetscMPIInt rank;
118e8fb0fc0SBarry Smith       if (PetscGlobalRank > 8) rank = 8; else rank = PetscGlobalRank;
119a6d0e24fSJed Brown       PetscSleep((PetscReal)rank);
120e8fb0fc0SBarry Smith     }
121e8fb0fc0SBarry Smith #endif
122e8fb0fc0SBarry Smith   }
123e8fb0fc0SBarry Smith 
1241179db26SBarry Smith   PetscFPrintf(PETSC_COMM_SELF,PETSC_STDERR,"[%d]PETSC ERROR: ",PetscGlobalRank);
125e8fb0fc0SBarry Smith   va_start(Argp,format);
1261179db26SBarry Smith   (*PetscVFPrintf)(PETSC_STDERR,format,Argp);
127e8fb0fc0SBarry Smith   va_end(Argp);
128e8fb0fc0SBarry Smith   return 0;
129e8fb0fc0SBarry Smith }
130e8fb0fc0SBarry Smith 
131e5c89e4eSSatish Balay #undef __FUNCT__
132e5c89e4eSSatish Balay #define __FUNCT__ "PetscTraceBackErrorHandler"
133e5c89e4eSSatish Balay /*@C
134e5c89e4eSSatish Balay 
135e5c89e4eSSatish Balay    PetscTraceBackErrorHandler - Default error handler routine that generates
136e5c89e4eSSatish Balay    a traceback on error detection.
137e5c89e4eSSatish Balay 
138e5c89e4eSSatish Balay    Not Collective
139e5c89e4eSSatish Balay 
140e5c89e4eSSatish Balay    Input Parameters:
141*e32f2f54SBarry Smith +  comm - communicator over which error occurred
142*e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
143e5c89e4eSSatish Balay .  func - the function where error is detected (indicated by __FUNCT__)
144e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
145e5c89e4eSSatish Balay .  dir - the directory of the file (indicated by __SDIR__)
146e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
147e5c89e4eSSatish Balay .  n - the generic error number
148e5c89e4eSSatish Balay .  p - specific error number
149e5c89e4eSSatish Balay -  ctx - error handler context
150e5c89e4eSSatish Balay 
151e5c89e4eSSatish Balay    Level: developer
152e5c89e4eSSatish Balay 
153e5c89e4eSSatish Balay    Notes:
154e5c89e4eSSatish Balay    Most users need not directly employ this routine and the other error
155e5c89e4eSSatish Balay    handlers, but can instead use the simplified interface SETERRQ, which has
156e5c89e4eSSatish Balay    the calling sequence
157*e32f2f54SBarry Smith $     SETERRQ(comm,number,p,mess)
158e5c89e4eSSatish Balay 
159e5c89e4eSSatish Balay    Notes for experienced users:
160e5c89e4eSSatish Balay    Use PetscPushErrorHandler() to set the desired error handler.  The
161e5c89e4eSSatish Balay    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
162e8fb0fc0SBarry Smith    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler()
163e5c89e4eSSatish Balay 
164e5c89e4eSSatish Balay    Concepts: error handler^traceback
165e5c89e4eSSatish Balay    Concepts: traceback^generating
166e5c89e4eSSatish Balay 
167e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
168e5c89e4eSSatish Balay           PetscAbortErrorHandler()
169e5c89e4eSSatish Balay  @*/
170*e32f2f54SBarry Smith PetscErrorCode PETSC_DLLEXPORT PetscTraceBackErrorHandler(MPI_Comm comm,int line,const char *fun,const char* file,const char *dir,PetscErrorCode n,int p,const char *mess,void *ctx)
171e5c89e4eSSatish Balay {
172e5c89e4eSSatish Balay   PetscLogDouble    mem,rss;
17390d69ab7SBarry Smith   PetscTruth        flg1 = PETSC_FALSE,flg2 = PETSC_FALSE;
174e5c89e4eSSatish Balay 
175e5c89e4eSSatish Balay   PetscFunctionBegin;
176e5c89e4eSSatish Balay 
177e5c89e4eSSatish Balay   if (p == 1) {
178107894f0SSatish Balay     (*PetscErrorPrintf)("--------------------- Error Message ------------------------------------\n");
179e5c89e4eSSatish Balay     if (n == PETSC_ERR_MEM) {
180e5c89e4eSSatish Balay       (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n");
181e5c89e4eSSatish Balay       (*PetscErrorPrintf)("too large an object or bleeding by not properly\n");
182e5c89e4eSSatish Balay       (*PetscErrorPrintf)("destroying unneeded objects.\n");
183e5c89e4eSSatish Balay       PetscMallocGetCurrentUsage(&mem);
184e5c89e4eSSatish Balay       PetscMemoryGetCurrentUsage(&rss);
18590d69ab7SBarry Smith       PetscOptionsGetTruth(PETSC_NULL,"-malloc_dump",&flg1,PETSC_NULL);
18690d69ab7SBarry Smith       PetscOptionsGetTruth(PETSC_NULL,"-malloc_log",&flg2,PETSC_NULL);
187e5c89e4eSSatish Balay       if (flg2) {
188e5c89e4eSSatish Balay         PetscMallocDumpLog(stdout);
189e5c89e4eSSatish Balay       } else {
190e5c89e4eSSatish Balay         (*PetscErrorPrintf)("Memory allocated %D Memory used by process %D\n",(PetscInt)mem,(PetscInt)rss);
191e5c89e4eSSatish Balay         if (flg1) {
192e5c89e4eSSatish Balay           PetscMallocDump(stdout);
193e5c89e4eSSatish Balay         } else {
194e5c89e4eSSatish Balay           (*PetscErrorPrintf)("Try running with -malloc_dump or -malloc_log for info.\n");
195e5c89e4eSSatish Balay         }
196e5c89e4eSSatish Balay       }
197e5c89e4eSSatish Balay     } else {
198e5c89e4eSSatish Balay         const char *text;
199e5c89e4eSSatish Balay         PetscErrorMessage(n,&text,PETSC_NULL);
200e5c89e4eSSatish Balay         if (text) (*PetscErrorPrintf)("%s!\n",text);
201e5c89e4eSSatish Balay     }
202e5c89e4eSSatish Balay     if (mess) {
203e5c89e4eSSatish Balay       (*PetscErrorPrintf)("%s!\n",mess);
204e5c89e4eSSatish Balay     }
205107894f0SSatish Balay     (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
206107894f0SSatish Balay     (*PetscErrorPrintf)("%s\n",version);
207107894f0SSatish Balay     (*PetscErrorPrintf)("See docs/changes/index.html for recent updates.\n");
208107894f0SSatish Balay     (*PetscErrorPrintf)("See docs/faq.html for hints about trouble shooting.\n");
209107894f0SSatish Balay     (*PetscErrorPrintf)("See docs/index.html for manual pages.\n");
210107894f0SSatish Balay     (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
211107894f0SSatish Balay     if (PetscErrorPrintfInitializeCalled) {
212107894f0SSatish Balay       (*PetscErrorPrintf)("%s on a %s named %s by %s %s\n",pname,arch,hostname,username,date);
213e5c89e4eSSatish Balay     }
214107894f0SSatish Balay     (*PetscErrorPrintf)("Libraries linked from %s\n",PETSC_LIB_DIR);
215107894f0SSatish Balay     (*PetscErrorPrintf)("Configure run at %s\n",petscconfigureruntime);
216107894f0SSatish Balay     (*PetscErrorPrintf)("Configure options %s\n",petscconfigureoptions);
217107894f0SSatish Balay     (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
218107894f0SSatish Balay   }
219107894f0SSatish Balay 
220107894f0SSatish Balay 
221107894f0SSatish Balay   /* first line in stack trace? */
222f5ad10bcSSatish Balay   (*PetscErrorPrintf)("%s() line %d in %s%s\n",fun,line,dir,file);
223107894f0SSatish Balay 
224107894f0SSatish Balay 
225e5c89e4eSSatish Balay   PetscFunctionReturn(n);
226e5c89e4eSSatish Balay }
227e5c89e4eSSatish Balay 
2280577ce7cSSatish Balay #if defined(PETSC_CLANGUAGE_CXX) && !defined(PETSC_USE_EXTERN_CXX)
229fd705b32SMatthew Knepley #undef __FUNCT__
230fd705b32SMatthew Knepley #define __FUNCT__ "PetscTraceBackErrorHandlerCxx"
231fd705b32SMatthew Knepley /*@C
232fd705b32SMatthew Knepley 
233fd705b32SMatthew Knepley    PetscTraceBackErrorHandlerCxx - Default error handler routine that generates
234fd705b32SMatthew Knepley    a traceback on error detection.
235fd705b32SMatthew Knepley 
236fd705b32SMatthew Knepley    Not Collective
237fd705b32SMatthew Knepley 
238fd705b32SMatthew Knepley    Input Parameters:
239fd705b32SMatthew Knepley +  line - the line number of the error (indicated by __LINE__)
240fd705b32SMatthew Knepley .  func - the function where error is detected (indicated by __FUNCT__)
241fd705b32SMatthew Knepley .  file - the file in which the error was detected (indicated by __FILE__)
242fd705b32SMatthew Knepley .  dir - the directory of the file (indicated by __SDIR__)
243fd705b32SMatthew Knepley .  n - the generic error number
244fd705b32SMatthew Knepley .  p - specific error number
245fd705b32SMatthew Knepley -  msg - The message stream
246fd705b32SMatthew Knepley 
247fd705b32SMatthew Knepley    Level: developer
248fd705b32SMatthew Knepley 
249fd705b32SMatthew Knepley    Notes:
250fd705b32SMatthew Knepley    Most users need not directly employ this routine and the other error
251fd705b32SMatthew Knepley    handlers, but can instead use the simplified interface SETERROR, which has
252fd705b32SMatthew Knepley    the calling sequence
253fd705b32SMatthew Knepley $     SETERROR(number,p,mess)
254fd705b32SMatthew Knepley 
255fd705b32SMatthew Knepley    Concepts: error handler^traceback
256fd705b32SMatthew Knepley    Concepts: traceback^generating
257fd705b32SMatthew Knepley 
258fd705b32SMatthew Knepley .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler()
259fd705b32SMatthew Knepley  @*/
260fd705b32SMatthew Knepley void PETSC_DLLEXPORT PetscTraceBackErrorHandlerCxx(int line,const char *fun,const char* file,const char *dir,PetscErrorCode n,int p, std::ostringstream& msg)
261fd705b32SMatthew Knepley {
262fd705b32SMatthew Knepley   if (p == 1) {
263fd705b32SMatthew Knepley     PetscLogDouble mem, rss;
26490d69ab7SBarry Smith     PetscTruth     flg1 = PETSC_FALSE, flg2 = PETSC_FALSE;
265fd705b32SMatthew Knepley 
266fd705b32SMatthew Knepley     msg << "--------------------- Error Message ------------------------------------" << std::endl;
267fd705b32SMatthew Knepley     if (n == PETSC_ERR_MEM) {
268fd705b32SMatthew Knepley       msg << "Out of memory. This could be due to allocating" << std::endl;
269fd705b32SMatthew Knepley       msg << "too large an object or bleeding by not properly" << std::endl;
270fd705b32SMatthew Knepley       msg << "destroying unneeded objects." << std::endl;
271fd705b32SMatthew Knepley       PetscMallocGetCurrentUsage(&mem);
272fd705b32SMatthew Knepley       PetscMemoryGetCurrentUsage(&rss);
27390d69ab7SBarry Smith       PetscOptionsGetTruth(PETSC_NULL,"-malloc_dump",&flg1,PETSC_NULL);
27490d69ab7SBarry Smith       PetscOptionsGetTruth(PETSC_NULL,"-malloc_log",&flg2,PETSC_NULL);
275fd705b32SMatthew Knepley       if (flg2) {
276fd705b32SMatthew Knepley         //PetscMallocDumpLog(stdout);
277fd705b32SMatthew Knepley         msg << "Option -malloc_log does not work in C++." << std::endl;
278fd705b32SMatthew Knepley       } else {
279fd705b32SMatthew Knepley         msg << "Memory allocated " << mem << " Memory used by process " << rss << std::endl;
280fd705b32SMatthew Knepley         if (flg1) {
281fd705b32SMatthew Knepley           //PetscMallocDump(stdout);
282fd705b32SMatthew Knepley           msg << "Option -malloc_dump does not work in C++." << std::endl;
283fd705b32SMatthew Knepley         } else {
284fd705b32SMatthew Knepley           msg << "Try running with -malloc_dump or -malloc_log for info." << std::endl;
285fd705b32SMatthew Knepley         }
286fd705b32SMatthew Knepley       }
287fd705b32SMatthew Knepley     } else {
288fd705b32SMatthew Knepley       const char *text;
289fd705b32SMatthew Knepley 
290fd705b32SMatthew Knepley       PetscErrorMessage(n,&text,PETSC_NULL);
291fd705b32SMatthew Knepley       if (text) {msg << text << "!" << std::endl;}
292fd705b32SMatthew Knepley     }
293fd705b32SMatthew Knepley     msg << "------------------------------------------------------------------------" << std::endl;
294fd705b32SMatthew Knepley     msg << version << std::endl;
295fd705b32SMatthew Knepley     msg << "See docs/changes/index.html for recent updates." << std::endl;
296fd705b32SMatthew Knepley     msg << "See docs/faq.html for hints about trouble shooting." << std::endl;
297fd705b32SMatthew Knepley     msg << "See docs/index.html for manual pages." << std::endl;
298fd705b32SMatthew Knepley     msg << "------------------------------------------------------------------------" << std::endl;
299fd705b32SMatthew Knepley     if (PetscErrorPrintfInitializeCalled) {
300fd705b32SMatthew Knepley       msg << pname << " on a " << arch << " named " << hostname << " by " << username << " " << date << std::endl;
301fd705b32SMatthew Knepley     }
302fd705b32SMatthew Knepley     msg << "Libraries linked from " << PETSC_LIB_DIR << std::endl;
303fd705b32SMatthew Knepley     msg << "Configure run at " << petscconfigureruntime << std::endl;
304fd705b32SMatthew Knepley     msg << "Configure options " << petscconfigureoptions << std::endl;
305fd705b32SMatthew Knepley     msg << "------------------------------------------------------------------------" << std::endl;
306fd705b32SMatthew Knepley   } else {
307fd705b32SMatthew Knepley     msg << fun<<"() line " << line << " in " << dir << file << std::endl;
308fd705b32SMatthew Knepley   }
309fd705b32SMatthew Knepley }
310fd705b32SMatthew Knepley #endif
311