xref: /petsc/src/sys/error/errtrace.c (revision efca3c55b02548817e185e5069a2acfe20fa4458)
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 .  mess - an error text string, usually just printed to the screen
18e5c89e4eSSatish Balay .  n - the generic error number
19e5c89e4eSSatish Balay .  p - specific error number
20e5c89e4eSSatish Balay -  ctx - error handler context
21e5c89e4eSSatish Balay 
22e5c89e4eSSatish Balay    Level: developer
23e5c89e4eSSatish Balay 
24e5c89e4eSSatish Balay    Notes:
25e5c89e4eSSatish Balay    Most users need not directly employ this routine and the other error
26e5c89e4eSSatish Balay    handlers, but can instead use the simplified interface SETERRQ, which has
27e5c89e4eSSatish Balay    the calling sequence
28e32f2f54SBarry Smith $     SETERRQ(comm,number,p,mess)
29e5c89e4eSSatish Balay 
30e5c89e4eSSatish Balay    Notes for experienced users:
31e5c89e4eSSatish Balay    Use PetscPushErrorHandler() to set the desired error handler.  The
32e5c89e4eSSatish Balay    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
33e8fb0fc0SBarry Smith    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler()
34e5c89e4eSSatish Balay 
35e5c89e4eSSatish Balay    Concepts: error handler^traceback
36e5c89e4eSSatish Balay    Concepts: traceback^generating
37e5c89e4eSSatish Balay 
38e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
39e5c89e4eSSatish Balay           PetscAbortErrorHandler(), PetscTraceBackErrorHandler()
40e5c89e4eSSatish Balay  @*/
41*efca3c55SSatish Balay PetscErrorCode  PetscIgnoreErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
42e5c89e4eSSatish Balay {
43e5c89e4eSSatish Balay   PetscFunctionBegin;
44e5c89e4eSSatish Balay   PetscFunctionReturn(n);
45e5c89e4eSSatish Balay }
46e5c89e4eSSatish Balay 
47107894f0SSatish Balay /* ---------------------------------------------------------------------------------------*/
48107894f0SSatish Balay 
495abee1b0SJed Brown static char      arch[128],hostname[128],username[128],pname[PETSC_MAX_PATH_LEN],date[128];
50ace3abfcSBarry Smith static PetscBool PetscErrorPrintfInitializeCalled = PETSC_FALSE;
513f6e4ae9SSatish Balay static char      version[256];
52107894f0SSatish Balay 
53107894f0SSatish Balay #undef __FUNCT__
54107894f0SSatish Balay #define __FUNCT__ "PetscErrorPrintfInitialize"
55107894f0SSatish Balay /*
56107894f0SSatish Balay    Initializes arch, hostname, username,date so that system calls do NOT need
57107894f0SSatish Balay    to be made during the error handler.
58107894f0SSatish Balay */
597087cfbeSBarry Smith PetscErrorCode  PetscErrorPrintfInitialize()
60107894f0SSatish Balay {
61107894f0SSatish Balay   PetscErrorCode ierr;
62ace3abfcSBarry Smith   PetscBool      use_stdout = PETSC_FALSE,use_none = PETSC_FALSE;
63107894f0SSatish Balay 
64107894f0SSatish Balay   PetscFunctionBegin;
655abee1b0SJed Brown   ierr = PetscGetArchType(arch,sizeof(arch));CHKERRQ(ierr);
665abee1b0SJed Brown   ierr = PetscGetHostName(hostname,sizeof(hostname));CHKERRQ(ierr);
675abee1b0SJed Brown   ierr = PetscGetUserName(username,sizeof(username));CHKERRQ(ierr);
68107894f0SSatish Balay   ierr = PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
695abee1b0SJed Brown   ierr = PetscGetDate(date,sizeof(date));CHKERRQ(ierr);
705abee1b0SJed Brown   ierr = PetscGetVersion(version,sizeof(version));CHKERRQ(ierr);
71e8fb0fc0SBarry Smith 
720298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-error_output_stdout",&use_stdout,NULL);CHKERRQ(ierr);
73a297a907SKarl Rupp   if (use_stdout) PETSC_STDERR = PETSC_STDOUT;
740298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-error_output_none",&use_none,NULL);CHKERRQ(ierr);
75a297a907SKarl Rupp   if (use_none) PetscErrorPrintf = PetscErrorPrintfNone;
76107894f0SSatish Balay   PetscErrorPrintfInitializeCalled = PETSC_TRUE;
77107894f0SSatish Balay   PetscFunctionReturn(0);
78107894f0SSatish Balay }
79107894f0SSatish Balay 
80e8fb0fc0SBarry Smith #undef __FUNCT__
81e8fb0fc0SBarry Smith #define __FUNCT__ "PetscErrorPrintfNone"
827087cfbeSBarry Smith PetscErrorCode  PetscErrorPrintfNone(const char format[],...)
83e8fb0fc0SBarry Smith {
84e8fb0fc0SBarry Smith   return 0;
85e8fb0fc0SBarry Smith }
86e8fb0fc0SBarry Smith 
87e8fb0fc0SBarry Smith #undef __FUNCT__
88e8fb0fc0SBarry Smith #define __FUNCT__ "PetscErrorPrintfDefault"
897087cfbeSBarry Smith PetscErrorCode  PetscErrorPrintfDefault(const char format[],...)
90e8fb0fc0SBarry Smith {
91e8fb0fc0SBarry Smith   va_list          Argp;
92ace3abfcSBarry Smith   static PetscBool PetscErrorPrintfCalled = PETSC_FALSE;
93e8fb0fc0SBarry Smith 
94e8fb0fc0SBarry Smith   /*
95e8fb0fc0SBarry Smith       This function does not call PetscFunctionBegin and PetscFunctionReturn() because
96e8fb0fc0SBarry Smith     it may be called by PetscStackView().
97e8fb0fc0SBarry Smith 
98e8fb0fc0SBarry Smith       This function does not do error checking because it is called by the error handlers.
99e8fb0fc0SBarry Smith   */
100e8fb0fc0SBarry Smith 
101e8fb0fc0SBarry Smith   if (!PetscErrorPrintfCalled) {
102e8fb0fc0SBarry Smith     PetscErrorPrintfCalled = PETSC_TRUE;
103e8fb0fc0SBarry Smith 
104e8fb0fc0SBarry Smith     /*
105e8fb0fc0SBarry Smith         On the SGI machines and Cray T3E, if errors are generated  "simultaneously" by
106e8fb0fc0SBarry Smith       different processors, the messages are printed all jumbled up; to try to
107e8fb0fc0SBarry Smith       prevent this we have each processor wait based on their rank
108e8fb0fc0SBarry Smith     */
109e8fb0fc0SBarry Smith #if defined(PETSC_CAN_SLEEP_AFTER_ERROR)
110e8fb0fc0SBarry Smith     {
111e8fb0fc0SBarry Smith       PetscMPIInt rank;
112a297a907SKarl Rupp       if (PetscGlobalRank > 8) rank = 8;
113a297a907SKarl Rupp       else rank = PetscGlobalRank;
114a6d0e24fSJed Brown       PetscSleep((PetscReal)rank);
115e8fb0fc0SBarry Smith     }
116e8fb0fc0SBarry Smith #endif
117e8fb0fc0SBarry Smith   }
118e8fb0fc0SBarry Smith 
1191179db26SBarry Smith   PetscFPrintf(PETSC_COMM_SELF,PETSC_STDERR,"[%d]PETSC ERROR: ",PetscGlobalRank);
120e8fb0fc0SBarry Smith   va_start(Argp,format);
1211179db26SBarry Smith   (*PetscVFPrintf)(PETSC_STDERR,format,Argp);
122e8fb0fc0SBarry Smith   va_end(Argp);
123e8fb0fc0SBarry Smith   return 0;
124e8fb0fc0SBarry Smith }
125e8fb0fc0SBarry Smith 
126e5c89e4eSSatish Balay #undef __FUNCT__
127e5c89e4eSSatish Balay #define __FUNCT__ "PetscTraceBackErrorHandler"
128e5c89e4eSSatish Balay /*@C
129e5c89e4eSSatish Balay 
130e5c89e4eSSatish Balay    PetscTraceBackErrorHandler - Default error handler routine that generates
131e5c89e4eSSatish Balay    a traceback on error detection.
132e5c89e4eSSatish Balay 
133e5c89e4eSSatish Balay    Not Collective
134e5c89e4eSSatish Balay 
135e5c89e4eSSatish Balay    Input Parameters:
136e32f2f54SBarry Smith +  comm - communicator over which error occurred
137e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
138e5c89e4eSSatish Balay .  func - the function where error is detected (indicated by __FUNCT__)
139e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
140e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
141e5c89e4eSSatish Balay .  n - the generic error number
142d736bfebSBarry Smith .  p - PETSC_ERROR_INITIAL if this is the first call the the error handler, otherwise PETSC_ERROR_REPEAT
143e5c89e4eSSatish Balay -  ctx - error handler context
144e5c89e4eSSatish Balay 
145e5c89e4eSSatish Balay    Level: developer
146e5c89e4eSSatish Balay 
147e5c89e4eSSatish Balay    Notes:
148e5c89e4eSSatish Balay    Most users need not directly employ this routine and the other error
149e5c89e4eSSatish Balay    handlers, but can instead use the simplified interface SETERRQ, which has
150e5c89e4eSSatish Balay    the calling sequence
151d736bfebSBarry Smith $     SETERRQ(comm,number,n,mess)
152e5c89e4eSSatish Balay 
153e5c89e4eSSatish Balay    Notes for experienced users:
154e5c89e4eSSatish Balay    Use PetscPushErrorHandler() to set the desired error handler.  The
155e5c89e4eSSatish Balay    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
156e8fb0fc0SBarry Smith    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscMPIAbortErrorHandler()
157e5c89e4eSSatish Balay 
158e5c89e4eSSatish Balay    Concepts: error handler^traceback
159e5c89e4eSSatish Balay    Concepts: traceback^generating
160e5c89e4eSSatish Balay 
161e5c89e4eSSatish Balay .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
162e5c89e4eSSatish Balay           PetscAbortErrorHandler()
163e5c89e4eSSatish Balay  @*/
164*efca3c55SSatish Balay PetscErrorCode  PetscTraceBackErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
165e5c89e4eSSatish Balay {
166e5c89e4eSSatish Balay   PetscLogDouble mem,rss;
167574034a9SJed Brown   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
168997adca8SBarry Smith   PetscMPIInt    rank = 0;
169e5c89e4eSSatish Balay 
170e5c89e4eSSatish Balay   PetscFunctionBegin;
171a297a907SKarl Rupp   if (comm != PETSC_COMM_SELF) MPI_Comm_rank(comm,&rank);
172a297a907SKarl Rupp 
173997adca8SBarry Smith   if (!rank) {
174668f157eSBarry Smith     if (p == PETSC_ERROR_INITIAL) {
175107894f0SSatish Balay       (*PetscErrorPrintf)("--------------------- Error Message ------------------------------------\n");
176e5c89e4eSSatish Balay       if (n == PETSC_ERR_MEM) {
177e5c89e4eSSatish Balay         (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n");
178e5c89e4eSSatish Balay         (*PetscErrorPrintf)("too large an object or bleeding by not properly\n");
179e5c89e4eSSatish Balay         (*PetscErrorPrintf)("destroying unneeded objects.\n");
180e5c89e4eSSatish Balay         PetscMallocGetCurrentUsage(&mem);
181e5c89e4eSSatish Balay         PetscMemoryGetCurrentUsage(&rss);
1820298fd71SBarry Smith         PetscOptionsGetBool(NULL,"-malloc_dump",&flg1,NULL);
1830298fd71SBarry Smith         PetscOptionsGetBool(NULL,"-malloc_log",&flg2,NULL);
1840298fd71SBarry Smith         PetscOptionsHasName(NULL,"-malloc_log_threshold",&flg3);
185a297a907SKarl Rupp         if (flg2 || flg3) PetscMallocDumpLog(stdout);
186a297a907SKarl Rupp         else {
187b85f3346SJed Brown           (*PetscErrorPrintf)("Memory allocated %.0f Memory used by process %.0f\n",mem,rss);
188a297a907SKarl Rupp           if (flg1) PetscMallocDump(stdout);
189a297a907SKarl Rupp           else (*PetscErrorPrintf)("Try running with -malloc_dump or -malloc_log for info.\n");
190e5c89e4eSSatish Balay         }
191e5c89e4eSSatish Balay       } else {
192e5c89e4eSSatish Balay         const char *text;
1930298fd71SBarry Smith         PetscErrorMessage(n,&text,NULL);
194e5c89e4eSSatish Balay         if (text) (*PetscErrorPrintf)("%s!\n",text);
195e5c89e4eSSatish Balay       }
196a297a907SKarl Rupp       if (mess) (*PetscErrorPrintf)("%s!\n",mess);
197107894f0SSatish Balay       (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
198107894f0SSatish Balay       (*PetscErrorPrintf)("%s\n",version);
199107894f0SSatish Balay       (*PetscErrorPrintf)("See docs/changes/index.html for recent updates.\n");
200107894f0SSatish Balay       (*PetscErrorPrintf)("See docs/faq.html for hints about trouble shooting.\n");
201107894f0SSatish Balay       (*PetscErrorPrintf)("See docs/index.html for manual pages.\n");
202107894f0SSatish Balay       (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
203a297a907SKarl Rupp       if (PetscErrorPrintfInitializeCalled) (*PetscErrorPrintf)("%s on a %s named %s by %s %s\n",pname,arch,hostname,username,date);
204107894f0SSatish Balay       (*PetscErrorPrintf)("Libraries linked from %s\n",PETSC_LIB_DIR);
205107894f0SSatish Balay       (*PetscErrorPrintf)("Configure run at %s\n",petscconfigureruntime);
206107894f0SSatish Balay       (*PetscErrorPrintf)("Configure options %s\n",petscconfigureoptions);
207107894f0SSatish Balay       (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
208107894f0SSatish Balay     }
209997adca8SBarry Smith     /* print line of stack trace */
210*efca3c55SSatish Balay     (*PetscErrorPrintf)("%s() line %d in %s\n",fun,line,file);
211997adca8SBarry Smith   } else {
212997adca8SBarry Smith     /* do not print error messages since process 0 will print them, sleep before aborting so will not accidently kill process 0*/
213997adca8SBarry Smith     PetscSleep(10.0);
214997adca8SBarry Smith     abort();
215997adca8SBarry Smith   }
216e5c89e4eSSatish Balay   PetscFunctionReturn(n);
217e5c89e4eSSatish Balay }
218e5c89e4eSSatish Balay 
219