xref: /petsc/src/sys/error/errtrace.c (revision 0298fd7132830bec7daee99a80be0eddb2b310a5) !
1e5c89e4eSSatish Balay 
2c6db04a5SJed Brown #include <petscsys.h>        /*I "petscsys.h" I*/
3c6db04a5SJed Brown #include <petscconfiginfo.h>
4e5c89e4eSSatish Balay 
5e5c89e4eSSatish Balay #undef __FUNCT__
6e5c89e4eSSatish Balay #define __FUNCT__ "PetscIgnoreErrorHandler"
7e5c89e4eSSatish Balay /*@C
8e5c89e4eSSatish Balay    PetscIgnoreErrorHandler - Ignores the error, allows program to continue as if error did not occure
9e5c89e4eSSatish Balay 
10e5c89e4eSSatish Balay    Not Collective
11e5c89e4eSSatish Balay 
12e5c89e4eSSatish Balay    Input Parameters:
13e32f2f54SBarry Smith +  comm - communicator over which error occurred
14e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
15e5c89e4eSSatish Balay .  func - the function where error is detected (indicated by __FUNCT__)
16e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
17e5c89e4eSSatish Balay .  dir - the directory of the file (indicated by __SDIR__)
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    Concepts: error handler^traceback
37e5c89e4eSSatish Balay    Concepts: traceback^generating
38e5c89e4eSSatish Balay 
39e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
40e5c89e4eSSatish Balay           PetscAbortErrorHandler(), PetscTraceBackErrorHandler()
41e5c89e4eSSatish Balay  @*/
427087cfbeSBarry Smith PetscErrorCode  PetscIgnoreErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,const char *dir,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
43e5c89e4eSSatish Balay {
44e5c89e4eSSatish Balay   PetscFunctionBegin;
45e5c89e4eSSatish Balay   PetscFunctionReturn(n);
46e5c89e4eSSatish Balay }
47e5c89e4eSSatish Balay 
48107894f0SSatish Balay /* ---------------------------------------------------------------------------------------*/
49107894f0SSatish Balay 
505abee1b0SJed Brown static char      arch[128],hostname[128],username[128],pname[PETSC_MAX_PATH_LEN],date[128];
51ace3abfcSBarry Smith static PetscBool PetscErrorPrintfInitializeCalled = PETSC_FALSE;
523f6e4ae9SSatish Balay static char      version[256];
53107894f0SSatish Balay 
54107894f0SSatish Balay #undef __FUNCT__
55107894f0SSatish Balay #define __FUNCT__ "PetscErrorPrintfInitialize"
56107894f0SSatish Balay /*
57107894f0SSatish Balay    Initializes arch, hostname, username,date so that system calls do NOT need
58107894f0SSatish Balay    to be made during the error handler.
59107894f0SSatish Balay */
607087cfbeSBarry Smith PetscErrorCode  PetscErrorPrintfInitialize()
61107894f0SSatish Balay {
62107894f0SSatish Balay   PetscErrorCode ierr;
63ace3abfcSBarry Smith   PetscBool      use_stdout = PETSC_FALSE,use_none = PETSC_FALSE;
64107894f0SSatish Balay 
65107894f0SSatish Balay   PetscFunctionBegin;
665abee1b0SJed Brown   ierr = PetscGetArchType(arch,sizeof(arch));CHKERRQ(ierr);
675abee1b0SJed Brown   ierr = PetscGetHostName(hostname,sizeof(hostname));CHKERRQ(ierr);
685abee1b0SJed Brown   ierr = PetscGetUserName(username,sizeof(username));CHKERRQ(ierr);
69107894f0SSatish Balay   ierr = PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
705abee1b0SJed Brown   ierr = PetscGetDate(date,sizeof(date));CHKERRQ(ierr);
715abee1b0SJed Brown   ierr = PetscGetVersion(version,sizeof(version));CHKERRQ(ierr);
72e8fb0fc0SBarry Smith 
73*0298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-error_output_stdout",&use_stdout,NULL);CHKERRQ(ierr);
74a297a907SKarl Rupp   if (use_stdout) PETSC_STDERR = PETSC_STDOUT;
75*0298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-error_output_none",&use_none,NULL);CHKERRQ(ierr);
76a297a907SKarl Rupp   if (use_none) PetscErrorPrintf = PetscErrorPrintfNone;
77107894f0SSatish Balay   PetscErrorPrintfInitializeCalled = PETSC_TRUE;
78107894f0SSatish Balay   PetscFunctionReturn(0);
79107894f0SSatish Balay }
80107894f0SSatish Balay 
81e8fb0fc0SBarry Smith #undef __FUNCT__
82e8fb0fc0SBarry Smith #define __FUNCT__ "PetscErrorPrintfNone"
837087cfbeSBarry Smith PetscErrorCode  PetscErrorPrintfNone(const char format[],...)
84e8fb0fc0SBarry Smith {
85e8fb0fc0SBarry Smith   return 0;
86e8fb0fc0SBarry Smith }
87e8fb0fc0SBarry Smith 
88e8fb0fc0SBarry Smith #undef __FUNCT__
89e8fb0fc0SBarry Smith #define __FUNCT__ "PetscErrorPrintfDefault"
907087cfbeSBarry Smith PetscErrorCode  PetscErrorPrintfDefault(const char format[],...)
91e8fb0fc0SBarry Smith {
92e8fb0fc0SBarry Smith   va_list          Argp;
93ace3abfcSBarry Smith   static PetscBool PetscErrorPrintfCalled = PETSC_FALSE;
94e8fb0fc0SBarry Smith 
95e8fb0fc0SBarry Smith   /*
96e8fb0fc0SBarry Smith       This function does not call PetscFunctionBegin and PetscFunctionReturn() because
97e8fb0fc0SBarry Smith     it may be called by PetscStackView().
98e8fb0fc0SBarry Smith 
99e8fb0fc0SBarry Smith       This function does not do error checking because it is called by the error handlers.
100e8fb0fc0SBarry Smith   */
101e8fb0fc0SBarry Smith 
102e8fb0fc0SBarry Smith   if (!PetscErrorPrintfCalled) {
103e8fb0fc0SBarry Smith     PetscErrorPrintfCalled = PETSC_TRUE;
104e8fb0fc0SBarry Smith 
105e8fb0fc0SBarry Smith     /*
106e8fb0fc0SBarry Smith         On the SGI machines and Cray T3E, if errors are generated  "simultaneously" by
107e8fb0fc0SBarry Smith       different processors, the messages are printed all jumbled up; to try to
108e8fb0fc0SBarry Smith       prevent this we have each processor wait based on their rank
109e8fb0fc0SBarry Smith     */
110e8fb0fc0SBarry Smith #if defined(PETSC_CAN_SLEEP_AFTER_ERROR)
111e8fb0fc0SBarry Smith     {
112e8fb0fc0SBarry Smith       PetscMPIInt rank;
113a297a907SKarl Rupp       if (PetscGlobalRank > 8) rank = 8;
114a297a907SKarl Rupp       else rank = PetscGlobalRank;
115a6d0e24fSJed Brown       PetscSleep((PetscReal)rank);
116e8fb0fc0SBarry Smith     }
117e8fb0fc0SBarry Smith #endif
118e8fb0fc0SBarry Smith   }
119e8fb0fc0SBarry Smith 
1201179db26SBarry Smith   PetscFPrintf(PETSC_COMM_SELF,PETSC_STDERR,"[%d]PETSC ERROR: ",PetscGlobalRank);
121e8fb0fc0SBarry Smith   va_start(Argp,format);
1221179db26SBarry Smith   (*PetscVFPrintf)(PETSC_STDERR,format,Argp);
123e8fb0fc0SBarry Smith   va_end(Argp);
124e8fb0fc0SBarry Smith   return 0;
125e8fb0fc0SBarry Smith }
126e8fb0fc0SBarry Smith 
127e5c89e4eSSatish Balay #undef __FUNCT__
128e5c89e4eSSatish Balay #define __FUNCT__ "PetscTraceBackErrorHandler"
129e5c89e4eSSatish Balay /*@C
130e5c89e4eSSatish Balay 
131e5c89e4eSSatish Balay    PetscTraceBackErrorHandler - Default error handler routine that generates
132e5c89e4eSSatish Balay    a traceback on error detection.
133e5c89e4eSSatish Balay 
134e5c89e4eSSatish Balay    Not Collective
135e5c89e4eSSatish Balay 
136e5c89e4eSSatish Balay    Input Parameters:
137e32f2f54SBarry Smith +  comm - communicator over which error occurred
138e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
139e5c89e4eSSatish Balay .  func - the function where error is detected (indicated by __FUNCT__)
140e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
141e5c89e4eSSatish Balay .  dir - the directory of the file (indicated by __SDIR__)
142e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
143e5c89e4eSSatish Balay .  n - the generic error number
144d736bfebSBarry Smith .  p - PETSC_ERROR_INITIAL if this is the first call the the error handler, otherwise PETSC_ERROR_REPEAT
145e5c89e4eSSatish Balay -  ctx - error handler context
146e5c89e4eSSatish Balay 
147e5c89e4eSSatish Balay    Level: developer
148e5c89e4eSSatish Balay 
149e5c89e4eSSatish Balay    Notes:
150e5c89e4eSSatish Balay    Most users need not directly employ this routine and the other error
151e5c89e4eSSatish Balay    handlers, but can instead use the simplified interface SETERRQ, which has
152e5c89e4eSSatish Balay    the calling sequence
153d736bfebSBarry Smith $     SETERRQ(comm,number,n,mess)
154e5c89e4eSSatish Balay 
155e5c89e4eSSatish Balay    Notes for experienced users:
156e5c89e4eSSatish Balay    Use PetscPushErrorHandler() to set the desired error handler.  The
157e5c89e4eSSatish Balay    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
158e8fb0fc0SBarry Smith    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler()
159e5c89e4eSSatish Balay 
160e5c89e4eSSatish Balay    Concepts: error handler^traceback
161e5c89e4eSSatish Balay    Concepts: traceback^generating
162e5c89e4eSSatish Balay 
163e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
164e5c89e4eSSatish Balay           PetscAbortErrorHandler()
165e5c89e4eSSatish Balay  @*/
1667087cfbeSBarry Smith PetscErrorCode  PetscTraceBackErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,const char *dir,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
167e5c89e4eSSatish Balay {
168e5c89e4eSSatish Balay   PetscLogDouble mem,rss;
169574034a9SJed Brown   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
170997adca8SBarry Smith   PetscMPIInt    rank = 0;
171e5c89e4eSSatish Balay 
172e5c89e4eSSatish Balay   PetscFunctionBegin;
173a297a907SKarl Rupp   if (comm != PETSC_COMM_SELF) MPI_Comm_rank(comm,&rank);
174a297a907SKarl Rupp 
175997adca8SBarry Smith   if (!rank) {
176668f157eSBarry Smith     if (p == PETSC_ERROR_INITIAL) {
177107894f0SSatish Balay       (*PetscErrorPrintf)("--------------------- Error Message ------------------------------------\n");
178e5c89e4eSSatish Balay       if (n == PETSC_ERR_MEM) {
179e5c89e4eSSatish Balay         (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n");
180e5c89e4eSSatish Balay         (*PetscErrorPrintf)("too large an object or bleeding by not properly\n");
181e5c89e4eSSatish Balay         (*PetscErrorPrintf)("destroying unneeded objects.\n");
182e5c89e4eSSatish Balay         PetscMallocGetCurrentUsage(&mem);
183e5c89e4eSSatish Balay         PetscMemoryGetCurrentUsage(&rss);
184*0298fd71SBarry Smith         PetscOptionsGetBool(NULL,"-malloc_dump",&flg1,NULL);
185*0298fd71SBarry Smith         PetscOptionsGetBool(NULL,"-malloc_log",&flg2,NULL);
186*0298fd71SBarry Smith         PetscOptionsHasName(NULL,"-malloc_log_threshold",&flg3);
187a297a907SKarl Rupp         if (flg2 || flg3) PetscMallocDumpLog(stdout);
188a297a907SKarl Rupp         else {
189b85f3346SJed Brown           (*PetscErrorPrintf)("Memory allocated %.0f Memory used by process %.0f\n",mem,rss);
190a297a907SKarl Rupp           if (flg1) PetscMallocDump(stdout);
191a297a907SKarl Rupp           else (*PetscErrorPrintf)("Try running with -malloc_dump or -malloc_log for info.\n");
192e5c89e4eSSatish Balay         }
193e5c89e4eSSatish Balay       } else {
194e5c89e4eSSatish Balay         const char *text;
195*0298fd71SBarry Smith         PetscErrorMessage(n,&text,NULL);
196e5c89e4eSSatish Balay         if (text) (*PetscErrorPrintf)("%s!\n",text);
197e5c89e4eSSatish Balay       }
198a297a907SKarl Rupp       if (mess) (*PetscErrorPrintf)("%s!\n",mess);
199107894f0SSatish Balay       (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
200107894f0SSatish Balay       (*PetscErrorPrintf)("%s\n",version);
201107894f0SSatish Balay       (*PetscErrorPrintf)("See docs/changes/index.html for recent updates.\n");
202107894f0SSatish Balay       (*PetscErrorPrintf)("See docs/faq.html for hints about trouble shooting.\n");
203107894f0SSatish Balay       (*PetscErrorPrintf)("See docs/index.html for manual pages.\n");
204107894f0SSatish Balay       (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
205a297a907SKarl Rupp       if (PetscErrorPrintfInitializeCalled) (*PetscErrorPrintf)("%s on a %s named %s by %s %s\n",pname,arch,hostname,username,date);
206107894f0SSatish Balay       (*PetscErrorPrintf)("Libraries linked from %s\n",PETSC_LIB_DIR);
207107894f0SSatish Balay       (*PetscErrorPrintf)("Configure run at %s\n",petscconfigureruntime);
208107894f0SSatish Balay       (*PetscErrorPrintf)("Configure options %s\n",petscconfigureoptions);
209107894f0SSatish Balay       (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
210107894f0SSatish Balay     }
211997adca8SBarry Smith     /* print line of stack trace */
212f5ad10bcSSatish Balay     (*PetscErrorPrintf)("%s() line %d in %s%s\n",fun,line,dir,file);
213997adca8SBarry Smith   } else {
214997adca8SBarry Smith     /* do not print error messages since process 0 will print them, sleep before aborting so will not accidently kill process 0*/
215997adca8SBarry Smith     PetscSleep(10.0);
216997adca8SBarry Smith     abort();
217997adca8SBarry Smith   }
218e5c89e4eSSatish Balay   PetscFunctionReturn(n);
219e5c89e4eSSatish Balay }
220e5c89e4eSSatish Balay 
221