xref: /petsc/src/sys/error/errtrace.c (revision 95c0884e6f7665b705eebf88174e89dc920c2fc0)
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    Concepts: error handler^traceback
37e5c89e4eSSatish Balay    Concepts: traceback^generating
38e5c89e4eSSatish Balay 
39e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
40e5c89e4eSSatish Balay           PetscAbortErrorHandler(), PetscTraceBackErrorHandler()
41e5c89e4eSSatish Balay  @*/
42efca3c55SSatish Balay PetscErrorCode  PetscIgnoreErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,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 /*
55107894f0SSatish Balay    Initializes arch, hostname, username,date so that system calls do NOT need
56107894f0SSatish Balay    to be made during the error handler.
57107894f0SSatish Balay */
58dd63322aSSatish Balay PetscErrorCode  PetscErrorPrintfInitialize(void)
59107894f0SSatish Balay {
60107894f0SSatish Balay   PetscErrorCode ierr;
61ace3abfcSBarry Smith   PetscBool      use_stdout = PETSC_FALSE,use_none = PETSC_FALSE;
62107894f0SSatish Balay 
63107894f0SSatish Balay   PetscFunctionBegin;
645abee1b0SJed Brown   ierr = PetscGetArchType(arch,sizeof(arch));CHKERRQ(ierr);
655abee1b0SJed Brown   ierr = PetscGetHostName(hostname,sizeof(hostname));CHKERRQ(ierr);
665abee1b0SJed Brown   ierr = PetscGetUserName(username,sizeof(username));CHKERRQ(ierr);
67107894f0SSatish Balay   ierr = PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
685abee1b0SJed Brown   ierr = PetscGetDate(date,sizeof(date));CHKERRQ(ierr);
695abee1b0SJed Brown   ierr = PetscGetVersion(version,sizeof(version));CHKERRQ(ierr);
70e8fb0fc0SBarry Smith 
71c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-error_output_stdout",&use_stdout,NULL);CHKERRQ(ierr);
72a297a907SKarl Rupp   if (use_stdout) PETSC_STDERR = PETSC_STDOUT;
73c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-error_output_none",&use_none,NULL);CHKERRQ(ierr);
74a297a907SKarl Rupp   if (use_none) PetscErrorPrintf = PetscErrorPrintfNone;
75107894f0SSatish Balay   PetscErrorPrintfInitializeCalled = PETSC_TRUE;
76107894f0SSatish Balay   PetscFunctionReturn(0);
77107894f0SSatish Balay }
78107894f0SSatish Balay 
797087cfbeSBarry Smith PetscErrorCode  PetscErrorPrintfNone(const char format[],...)
80e8fb0fc0SBarry Smith {
81e8fb0fc0SBarry Smith   return 0;
82e8fb0fc0SBarry Smith }
83e8fb0fc0SBarry Smith 
847087cfbeSBarry Smith PetscErrorCode  PetscErrorPrintfDefault(const char format[],...)
85e8fb0fc0SBarry Smith {
86e8fb0fc0SBarry Smith   va_list          Argp;
87ace3abfcSBarry Smith   static PetscBool PetscErrorPrintfCalled = PETSC_FALSE;
88e8fb0fc0SBarry Smith 
89e8fb0fc0SBarry Smith   /*
90e8fb0fc0SBarry Smith       This function does not call PetscFunctionBegin and PetscFunctionReturn() because
91e8fb0fc0SBarry Smith     it may be called by PetscStackView().
92e8fb0fc0SBarry Smith 
93e8fb0fc0SBarry Smith       This function does not do error checking because it is called by the error handlers.
94e8fb0fc0SBarry Smith   */
95e8fb0fc0SBarry Smith 
96e8fb0fc0SBarry Smith   if (!PetscErrorPrintfCalled) {
97e8fb0fc0SBarry Smith     PetscErrorPrintfCalled = PETSC_TRUE;
98e8fb0fc0SBarry Smith 
99e8fb0fc0SBarry Smith     /*
100e8fb0fc0SBarry Smith         On the SGI machines and Cray T3E, if errors are generated  "simultaneously" by
101e8fb0fc0SBarry Smith       different processors, the messages are printed all jumbled up; to try to
102e8fb0fc0SBarry Smith       prevent this we have each processor wait based on their rank
103e8fb0fc0SBarry Smith     */
104e8fb0fc0SBarry Smith #if defined(PETSC_CAN_SLEEP_AFTER_ERROR)
105e8fb0fc0SBarry Smith     {
106e8fb0fc0SBarry Smith       PetscMPIInt rank;
107a297a907SKarl Rupp       if (PetscGlobalRank > 8) rank = 8;
108a297a907SKarl Rupp       else rank = PetscGlobalRank;
109a6d0e24fSJed Brown       PetscSleep((PetscReal)rank);
110e8fb0fc0SBarry Smith     }
111e8fb0fc0SBarry Smith #endif
112e8fb0fc0SBarry Smith   }
113e8fb0fc0SBarry Smith 
1141179db26SBarry Smith   PetscFPrintf(PETSC_COMM_SELF,PETSC_STDERR,"[%d]PETSC ERROR: ",PetscGlobalRank);
115e8fb0fc0SBarry Smith   va_start(Argp,format);
1161179db26SBarry Smith   (*PetscVFPrintf)(PETSC_STDERR,format,Argp);
117e8fb0fc0SBarry Smith   va_end(Argp);
118e8fb0fc0SBarry Smith   return 0;
119e8fb0fc0SBarry Smith }
120e8fb0fc0SBarry Smith 
121114011d0SBarry Smith static void PetscErrorPrintfHilight(void)
122114011d0SBarry Smith {
12398ed35c3SBarry Smith #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
124114011d0SBarry Smith   if (PetscErrorPrintf == PetscErrorPrintfDefault) {
125114011d0SBarry Smith     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR,"\033[1;31m");
126114011d0SBarry Smith   }
127114011d0SBarry Smith #endif
128114011d0SBarry Smith }
129114011d0SBarry Smith 
130114011d0SBarry Smith static void PetscErrorPrintfNormal(void)
131114011d0SBarry Smith {
13298ed35c3SBarry Smith #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
133114011d0SBarry Smith   if (PetscErrorPrintf == PetscErrorPrintfDefault) {
134114011d0SBarry Smith     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR,"\033[0;39m\033[0;49m");
135114011d0SBarry Smith   }
136114011d0SBarry Smith #endif
137114011d0SBarry Smith }
138114011d0SBarry Smith 
139*95c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode  PetscOptionsViewError(void);
140114011d0SBarry Smith 
141e5c89e4eSSatish Balay /*@C
142e5c89e4eSSatish Balay 
143e5c89e4eSSatish Balay    PetscTraceBackErrorHandler - Default error handler routine that generates
144e5c89e4eSSatish Balay    a traceback on error detection.
145e5c89e4eSSatish Balay 
146e5c89e4eSSatish Balay    Not Collective
147e5c89e4eSSatish Balay 
148e5c89e4eSSatish Balay    Input Parameters:
149e32f2f54SBarry Smith +  comm - communicator over which error occurred
150e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
151e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
152e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
153e5c89e4eSSatish Balay .  n - the generic error number
154df3898eeSBarry Smith .  p - PETSC_ERROR_INITIAL if this is the first call the error handler, otherwise PETSC_ERROR_REPEAT
155e5c89e4eSSatish Balay -  ctx - error handler context
156e5c89e4eSSatish Balay 
157e5c89e4eSSatish Balay    Level: developer
158e5c89e4eSSatish Balay 
159e5c89e4eSSatish Balay    Notes:
160e5c89e4eSSatish Balay    Most users need not directly employ this routine and the other error
161e5c89e4eSSatish Balay    handlers, but can instead use the simplified interface SETERRQ, which has
162e5c89e4eSSatish Balay    the calling sequence
163d736bfebSBarry Smith $     SETERRQ(comm,number,n,mess)
164e5c89e4eSSatish Balay 
165e5c89e4eSSatish Balay    Notes for experienced users:
166e5c89e4eSSatish Balay    Use PetscPushErrorHandler() to set the desired error handler.  The
167e5c89e4eSSatish Balay    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
168e8fb0fc0SBarry Smith    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler()
169e5c89e4eSSatish Balay 
170e5c89e4eSSatish Balay    Concepts: error handler^traceback
171e5c89e4eSSatish Balay    Concepts: traceback^generating
172e5c89e4eSSatish Balay 
173e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
174e5c89e4eSSatish Balay           PetscAbortErrorHandler()
175e5c89e4eSSatish Balay  @*/
176efca3c55SSatish Balay PetscErrorCode  PetscTraceBackErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
177e5c89e4eSSatish Balay {
178e5c89e4eSSatish Balay   PetscLogDouble mem,rss;
179574034a9SJed Brown   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
180997adca8SBarry Smith   PetscMPIInt    rank = 0;
181e5c89e4eSSatish Balay 
182e5c89e4eSSatish Balay   PetscFunctionBegin;
183a297a907SKarl Rupp   if (comm != PETSC_COMM_SELF) MPI_Comm_rank(comm,&rank);
184a297a907SKarl Rupp 
185997adca8SBarry Smith   if (!rank) {
186fbfcfee5SBarry Smith     PetscBool  ismain;
187114011d0SBarry Smith     static int cnt = 1;
188114011d0SBarry Smith 
189668f157eSBarry Smith     if (p == PETSC_ERROR_INITIAL) {
190114011d0SBarry Smith       PetscErrorPrintfHilight();
191114011d0SBarry Smith       (*PetscErrorPrintf)("--------------------- Error Message --------------------------------------------------------------\n");
1925f3a2c8dSBarry Smith       PetscErrorPrintfNormal();
193e5c89e4eSSatish Balay       if (n == PETSC_ERR_MEM) {
194e5c89e4eSSatish Balay         (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n");
195e5c89e4eSSatish Balay         (*PetscErrorPrintf)("too large an object or bleeding by not properly\n");
196e5c89e4eSSatish Balay         (*PetscErrorPrintf)("destroying unneeded objects.\n");
197e5c89e4eSSatish Balay         PetscMallocGetCurrentUsage(&mem);
198e5c89e4eSSatish Balay         PetscMemoryGetCurrentUsage(&rss);
199c5929fdfSBarry Smith         PetscOptionsGetBool(NULL,NULL,"-malloc_dump",&flg1,NULL);
200c5929fdfSBarry Smith         PetscOptionsGetBool(NULL,NULL,"-malloc_log",&flg2,NULL);
201c5929fdfSBarry Smith         PetscOptionsHasName(NULL,NULL,"-malloc_log_threshold",&flg3);
202a297a907SKarl Rupp         if (flg2 || flg3) PetscMallocDumpLog(stdout);
203a297a907SKarl Rupp         else {
204b85f3346SJed Brown           (*PetscErrorPrintf)("Memory allocated %.0f Memory used by process %.0f\n",mem,rss);
205a297a907SKarl Rupp           if (flg1) PetscMallocDump(stdout);
206a297a907SKarl Rupp           else (*PetscErrorPrintf)("Try running with -malloc_dump or -malloc_log for info.\n");
207e5c89e4eSSatish Balay         }
208e5c89e4eSSatish Balay       } else {
209e5c89e4eSSatish Balay         const char *text;
2100298fd71SBarry Smith         PetscErrorMessage(n,&text,NULL);
211114011d0SBarry Smith         if (text) (*PetscErrorPrintf)("%s\n",text);
212e5c89e4eSSatish Balay       }
213114011d0SBarry Smith       if (mess) (*PetscErrorPrintf)("%s\n",mess);
2140c8c14c5SBarry Smith       (*PetscErrorPrintf)("See http://www.mcs.anl.gov/petsc/documentation/faq.html for trouble shooting.\n");
215107894f0SSatish Balay       (*PetscErrorPrintf)("%s\n",version);
216a297a907SKarl Rupp       if (PetscErrorPrintfInitializeCalled) (*PetscErrorPrintf)("%s on a %s named %s by %s %s\n",pname,arch,hostname,username,date);
217107894f0SSatish Balay       (*PetscErrorPrintf)("Configure options %s\n",petscconfigureoptions);
218107894f0SSatish Balay     }
219997adca8SBarry Smith     /* print line of stack trace */
220114011d0SBarry Smith     (*PetscErrorPrintf)("#%d %s() line %d in %s\n",cnt++,fun,line,file);
221114011d0SBarry Smith     PetscStrncmp(fun,"main",4,&ismain);
222fbfcfee5SBarry Smith     if (ismain) {
223e11779c2SBarry Smith       PetscOptionsViewError();
224114011d0SBarry Smith       PetscErrorPrintfHilight();
225f15a11d2SBarry Smith       (*PetscErrorPrintf)("----------------End of Error Message -------send entire error message to petsc-maint@mcs.anl.gov----------\n");
226114011d0SBarry Smith       PetscErrorPrintfNormal();
227114011d0SBarry Smith     }
228997adca8SBarry Smith   } else {
229997adca8SBarry Smith     /* do not print error messages since process 0 will print them, sleep before aborting so will not accidently kill process 0*/
230997adca8SBarry Smith     PetscSleep(10.0);
231997adca8SBarry Smith     abort();
232997adca8SBarry Smith   }
233e5c89e4eSSatish Balay   PetscFunctionReturn(n);
234e5c89e4eSSatish Balay }
235e5c89e4eSSatish Balay 
236