xref: /petsc/src/sys/error/errtrace.c (revision 35f00c147f54e333863026c4b3407d5f5899c648)
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
89beb8f72SToby Isaac #include "err.h"
9*35f00c14SToby Isaac #include <petsc/private/logimpl.h> // PETSC_TLS
10e5c89e4eSSatish Balay 
11e5c89e4eSSatish Balay /*@C
12aaa8cc7dSPierre Jolivet   PetscIgnoreErrorHandler - Deprecated, use `PetscReturnErrorHandler()`. Ignores the error, allows program to continue as if error did not occur
13e5c89e4eSSatish Balay 
14e5c89e4eSSatish Balay   Not Collective
15e5c89e4eSSatish Balay 
16e5c89e4eSSatish Balay   Input Parameters:
17e32f2f54SBarry Smith + comm - communicator over which error occurred
18e32f2f54SBarry Smith . line - the line number of the error (indicated by __LINE__)
1910450e9eSJacob Faibussowitsch . fun  - the function name
20e5c89e4eSSatish Balay . file - the file in which the error was detected (indicated by __FILE__)
21e5c89e4eSSatish Balay . mess - an error text string, usually just printed to the screen
22e5c89e4eSSatish Balay . n    - the generic error number
23e5c89e4eSSatish Balay . p    - specific error number
24e5c89e4eSSatish Balay - ctx  - error handler context
25e5c89e4eSSatish Balay 
26e5c89e4eSSatish Balay   Level: developer
27e5c89e4eSSatish Balay 
28811af0c4SBarry Smith   Note:
29811af0c4SBarry Smith   Users do not directly call this routine
30e5c89e4eSSatish Balay 
31db781477SPatrick Sanan .seealso: `PetscReturnErrorHandler()`
32e5c89e4eSSatish Balay  @*/
33d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, void *ctx)
34d71ae5a4SJacob Faibussowitsch {
3510450e9eSJacob Faibussowitsch   (void)comm;
3610450e9eSJacob Faibussowitsch   (void)line;
3710450e9eSJacob Faibussowitsch   (void)fun;
3810450e9eSJacob Faibussowitsch   (void)file;
3910450e9eSJacob Faibussowitsch   (void)p;
4010450e9eSJacob Faibussowitsch   (void)mess;
4110450e9eSJacob Faibussowitsch   (void)ctx;
4211cc89d2SBarry Smith   return n;
43e5c89e4eSSatish Balay }
44e5c89e4eSSatish Balay 
45107894f0SSatish Balay /* ---------------------------------------------------------------------------------------*/
46107894f0SSatish Balay 
475abee1b0SJed Brown static char      arch[128], hostname[128], username[128], pname[PETSC_MAX_PATH_LEN], date[128];
48ace3abfcSBarry Smith static PetscBool PetscErrorPrintfInitializeCalled = PETSC_FALSE;
493f6e4ae9SSatish Balay static char      version[256];
50107894f0SSatish Balay 
51107894f0SSatish Balay /*
52107894f0SSatish Balay    Initializes arch, hostname, username, date so that system calls do NOT need
53107894f0SSatish Balay    to be made during the error handler.
54107894f0SSatish Balay */
55d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscErrorPrintfInitialize(void)
56d71ae5a4SJacob Faibussowitsch {
57ace3abfcSBarry Smith   PetscBool use_stdout = PETSC_FALSE, use_none = PETSC_FALSE;
58107894f0SSatish Balay 
59107894f0SSatish Balay   PetscFunctionBegin;
609566063dSJacob Faibussowitsch   PetscCall(PetscGetArchType(arch, sizeof(arch)));
619566063dSJacob Faibussowitsch   PetscCall(PetscGetHostName(hostname, sizeof(hostname)));
629566063dSJacob Faibussowitsch   PetscCall(PetscGetUserName(username, sizeof(username)));
639566063dSJacob Faibussowitsch   PetscCall(PetscGetProgramName(pname, sizeof(pname)));
649566063dSJacob Faibussowitsch   PetscCall(PetscGetDate(date, sizeof(date)));
659566063dSJacob Faibussowitsch   PetscCall(PetscGetVersion(version, sizeof(version)));
66e8fb0fc0SBarry Smith 
679566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(NULL, NULL, "-error_output_stdout", &use_stdout, NULL));
68a297a907SKarl Rupp   if (use_stdout) PETSC_STDERR = PETSC_STDOUT;
699566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(NULL, NULL, "-error_output_none", &use_none, NULL));
70a297a907SKarl Rupp   if (use_none) PetscErrorPrintf = PetscErrorPrintfNone;
71107894f0SSatish Balay   PetscErrorPrintfInitializeCalled = PETSC_TRUE;
723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
73107894f0SSatish Balay }
74107894f0SSatish Balay 
75d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscErrorPrintfNone(const char format[], ...)
76d71ae5a4SJacob Faibussowitsch {
773ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
78e8fb0fc0SBarry Smith }
79e8fb0fc0SBarry Smith 
80d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscErrorPrintfDefault(const char format[], ...)
81d71ae5a4SJacob Faibussowitsch {
82e8fb0fc0SBarry Smith   va_list          Argp;
83ace3abfcSBarry Smith   static PetscBool PetscErrorPrintfCalled = PETSC_FALSE;
843ba16761SJacob Faibussowitsch   PetscErrorCode   ierr;
85e8fb0fc0SBarry Smith 
86e8fb0fc0SBarry Smith   /*
87e8fb0fc0SBarry Smith       This function does not call PetscFunctionBegin and PetscFunctionReturn() because
88e8fb0fc0SBarry Smith     it may be called by PetscStackView().
89e8fb0fc0SBarry Smith 
90e8fb0fc0SBarry Smith       This function does not do error checking because it is called by the error handlers.
91e8fb0fc0SBarry Smith   */
92e8fb0fc0SBarry Smith 
93e8fb0fc0SBarry Smith   if (!PetscErrorPrintfCalled) {
94e8fb0fc0SBarry Smith     PetscErrorPrintfCalled = PETSC_TRUE;
95e8fb0fc0SBarry Smith 
96e8fb0fc0SBarry Smith     /*
97e8fb0fc0SBarry Smith         On the SGI machines and Cray T3E, if errors are generated  "simultaneously" by
98e8fb0fc0SBarry Smith       different processors, the messages are printed all jumbled up; to try to
99e8fb0fc0SBarry Smith       prevent this we have each processor wait based on their rank
100e8fb0fc0SBarry Smith     */
101e8fb0fc0SBarry Smith #if defined(PETSC_CAN_SLEEP_AFTER_ERROR)
102e8fb0fc0SBarry Smith     {
1033ba16761SJacob Faibussowitsch       PetscMPIInt rank = PetscGlobalRank > 8 ? 8 : PetscGlobalRank;
1043ba16761SJacob Faibussowitsch       ierr             = PetscSleep((PetscReal)rank);
1053ba16761SJacob Faibussowitsch       (void)ierr;
106e8fb0fc0SBarry Smith     }
107e8fb0fc0SBarry Smith #endif
108e8fb0fc0SBarry Smith   }
109e8fb0fc0SBarry Smith 
1103ba16761SJacob Faibussowitsch   ierr = PetscFPrintf(PETSC_COMM_SELF, PETSC_STDERR, "[%d]PETSC ERROR: ", PetscGlobalRank);
111e8fb0fc0SBarry Smith   va_start(Argp, format);
1123ba16761SJacob Faibussowitsch   ierr = (*PetscVFPrintf)(PETSC_STDERR, format, Argp);
1133ba16761SJacob Faibussowitsch   (void)ierr;
114e8fb0fc0SBarry Smith   va_end(Argp);
1153ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
116e8fb0fc0SBarry Smith }
117e8fb0fc0SBarry Smith 
118c2eed0edSBarry Smith /*
119c2eed0edSBarry Smith    On some systems when the stderr is nested through several levels of shell script
120c2eed0edSBarry Smith    before being passed to a file the isatty() falsely returns true resulting in
121c2eed0edSBarry Smith    the screen highlight variables being passed through the test harness. Therefore
122c2eed0edSBarry Smith    simply do not highlight when the PETSC_STDERR is PETSC_STDOUT.
123c2eed0edSBarry Smith */
124d71ae5a4SJacob Faibussowitsch static void PetscErrorPrintfHilight(void)
125d71ae5a4SJacob Faibussowitsch {
12698ed35c3SBarry Smith #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
127c2eed0edSBarry Smith   if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
128114011d0SBarry Smith     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR, "\033[1;31m");
129114011d0SBarry Smith   }
130114011d0SBarry Smith #endif
131114011d0SBarry Smith }
132114011d0SBarry Smith 
133d71ae5a4SJacob Faibussowitsch static void PetscErrorPrintfNormal(void)
134d71ae5a4SJacob Faibussowitsch {
13598ed35c3SBarry Smith #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
136c2eed0edSBarry Smith   if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
137114011d0SBarry Smith     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR, "\033[0;39m\033[0;49m");
138114011d0SBarry Smith   }
139114011d0SBarry Smith #endif
140114011d0SBarry Smith }
141114011d0SBarry Smith 
14295c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscOptionsViewError(void);
143114011d0SBarry Smith 
144*35f00c14SToby Isaac static PETSC_TLS PetscBool petsc_traceback_error_silent = PETSC_FALSE;
145*35f00c14SToby Isaac 
146e5c89e4eSSatish Balay /*@C
147e5c89e4eSSatish Balay 
148e5c89e4eSSatish Balay   PetscTraceBackErrorHandler - Default error handler routine that generates
149e5c89e4eSSatish Balay   a traceback on error detection.
150e5c89e4eSSatish Balay 
151e5c89e4eSSatish Balay   Not Collective
152e5c89e4eSSatish Balay 
153e5c89e4eSSatish Balay   Input Parameters:
154e32f2f54SBarry Smith + comm - communicator over which error occurred
155e32f2f54SBarry Smith . line - the line number of the error (indicated by __LINE__)
15610450e9eSJacob Faibussowitsch . fun  - the function name
157e5c89e4eSSatish Balay . file - the file in which the error was detected (indicated by __FILE__)
158e5c89e4eSSatish Balay . mess - an error text string, usually just printed to the screen
159e5c89e4eSSatish Balay . n    - the generic error number
160811af0c4SBarry Smith . p    - `PETSC_ERROR_INITIAL` if this is the first call the error handler, otherwise `PETSC_ERROR_REPEAT`
161e5c89e4eSSatish Balay - ctx  - error handler context
162e5c89e4eSSatish Balay 
163811af0c4SBarry Smith   Options Database Keys:
16445b666d6SBarry Smith + -error_output_stdout - output the error messages to stdout instead of the default stderr
16545b666d6SBarry Smith - -error_output_none   - do not output the error messages
166e5c89e4eSSatish Balay 
167e5c89e4eSSatish Balay   Notes:
168811af0c4SBarry Smith   Users do not directly call this routine
169e5c89e4eSSatish Balay 
170811af0c4SBarry Smith   Use `PetscPushErrorHandler()` to set the desired error handler.
171e5c89e4eSSatish Balay 
17245b666d6SBarry Smith   Level: developer
173e5c89e4eSSatish Balay 
174db781477SPatrick Sanan .seealso: `PetscError()`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`,
175db781477SPatrick Sanan           `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscReturnErrorHandler()`, `PetscEmacsClientErrorHandler()`
176e5c89e4eSSatish Balay  @*/
177d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, void *ctx)
178d71ae5a4SJacob Faibussowitsch {
1793ba16761SJacob Faibussowitsch   PetscErrorCode ierr;
180997adca8SBarry Smith   PetscMPIInt    rank = 0;
181e5c89e4eSSatish Balay 
18210450e9eSJacob Faibussowitsch   (void)ctx;
183a297a907SKarl Rupp   if (comm != PETSC_COMM_SELF) MPI_Comm_rank(comm, &rank);
184a297a907SKarl Rupp 
185*35f00c14SToby Isaac   // reinitialize the error handler when a new initializing error is detected
186*35f00c14SToby Isaac   if (p != PETSC_ERROR_REPEAT) {
187*35f00c14SToby Isaac     petsc_traceback_error_silent = PETSC_FALSE;
188*35f00c14SToby Isaac     if (PetscCIEnabledPortableErrorOutput) {
189*35f00c14SToby Isaac       PetscMPIInt size = 1;
190*35f00c14SToby Isaac 
191*35f00c14SToby Isaac       if (comm != MPI_COMM_NULL) MPI_Comm_size(comm, &size);
192*35f00c14SToby Isaac       petscabortmpifinalize = (size == PetscGlobalSize) ? PETSC_TRUE : PETSC_FALSE;
193*35f00c14SToby Isaac     }
194*35f00c14SToby Isaac   }
195*35f00c14SToby Isaac 
196*35f00c14SToby Isaac   if (rank == 0 && (!PetscCIEnabledPortableErrorOutput || PetscGlobalRank == 0) && (p != PETSC_ERROR_REPEAT || !petsc_traceback_error_silent)) {
197114011d0SBarry Smith     static int cnt = 1;
198114011d0SBarry Smith 
1994e29e845SStefano Zampini     if (p == PETSC_ERROR_INITIAL) {
200114011d0SBarry Smith       PetscErrorPrintfHilight();
2013ba16761SJacob Faibussowitsch       ierr = (*PetscErrorPrintf)("--------------------- Error Message --------------------------------------------------------------\n");
2025f3a2c8dSBarry Smith       PetscErrorPrintfNormal();
2034e29e845SStefano Zampini       if (cnt > 1) {
2049beb8f72SToby Isaac         ierr = (*PetscErrorPrintf)("  It appears a new error in the code was triggered after a previous error, possibly because:\n");
2059beb8f72SToby Isaac         ierr = (*PetscErrorPrintf)("  -  The first error was not properly handled via (for example) the use of\n");
2069beb8f72SToby Isaac         ierr = (*PetscErrorPrintf)("     PetscCall(TheFunctionThatErrors()); or\n");
2079beb8f72SToby Isaac         ierr = (*PetscErrorPrintf)("  -  The second error was triggered while handling the first error.\n");
2089beb8f72SToby Isaac         ierr = (*PetscErrorPrintf)("  Above is the traceback for the previous unhandled error, below the traceback for the next error\n");
2094e29e845SStefano Zampini         ierr = (*PetscErrorPrintf)("  ALL ERRORS in the PETSc libraries are fatal, you should add the appropriate error checking to the code\n");
2104e29e845SStefano Zampini         cnt  = 1;
2114e29e845SStefano Zampini       }
2124e29e845SStefano Zampini     }
2134e29e845SStefano Zampini     if (cnt == 1) {
2149beb8f72SToby Isaac       if (n == PETSC_ERR_MEM || n == PETSC_ERR_MEM_LEAK) ierr = PetscErrorMemoryMessage(n);
215a297a907SKarl Rupp       else {
216e5c89e4eSSatish Balay         const char *text;
2173ba16761SJacob Faibussowitsch         ierr = PetscErrorMessage(n, &text, NULL);
2183ba16761SJacob Faibussowitsch         if (text) ierr = (*PetscErrorPrintf)("%s\n", text);
219e5c89e4eSSatish Balay       }
2203ba16761SJacob Faibussowitsch       if (mess) ierr = (*PetscErrorPrintf)("%s\n", mess);
2213ba16761SJacob Faibussowitsch       ierr = PetscOptionsLeftError();
2223ba16761SJacob Faibussowitsch       ierr = (*PetscErrorPrintf)("See https://petsc.org/release/faq/ for trouble shooting.\n");
223660278c0SBarry Smith       if (!PetscCIEnabledPortableErrorOutput) {
2243ba16761SJacob Faibussowitsch         ierr = (*PetscErrorPrintf)("%s\n", version);
2253ba16761SJacob Faibussowitsch         if (PetscErrorPrintfInitializeCalled) ierr = (*PetscErrorPrintf)("%s on a %s named %s by %s %s\n", pname, arch, hostname, username, date);
2263ba16761SJacob Faibussowitsch         ierr = (*PetscErrorPrintf)("Configure options %s\n", petscconfigureoptions);
227107894f0SSatish Balay       }
228660278c0SBarry Smith     }
229997adca8SBarry Smith     /* print line of stack trace */
2303ba16761SJacob Faibussowitsch     if (fun) ierr = (*PetscErrorPrintf)("#%d %s() at %s:%d\n", cnt++, fun, PetscCIFilename(file), PetscCILinenumber(line));
2313ba16761SJacob Faibussowitsch     else if (file) ierr = (*PetscErrorPrintf)("#%d %s:%d\n", cnt++, PetscCIFilename(file), PetscCILinenumber(line));
23249c86fc7SBarry Smith     if (fun) {
233bbcf679cSJacob Faibussowitsch       PetscBool ismain = PETSC_FALSE;
234bbcf679cSJacob Faibussowitsch 
2353ba16761SJacob Faibussowitsch       ierr = PetscStrncmp(fun, "main", 4, &ismain);
236fbfcfee5SBarry Smith       if (ismain) {
2373ba16761SJacob Faibussowitsch         if ((n <= PETSC_ERR_MIN_VALUE) || (n >= PETSC_ERR_MAX_VALUE)) ierr = (*PetscErrorPrintf)("Reached the main program with an out-of-range error code %d. This should never happen\n", n);
2383ba16761SJacob Faibussowitsch         ierr = PetscOptionsViewError();
239114011d0SBarry Smith         PetscErrorPrintfHilight();
2403ba16761SJacob Faibussowitsch         ierr = (*PetscErrorPrintf)("----------------End of Error Message -------send entire error message to petsc-maint@mcs.anl.gov----------\n");
241114011d0SBarry Smith         PetscErrorPrintfNormal();
242114011d0SBarry Smith       }
24349c86fc7SBarry Smith     }
244997adca8SBarry Smith   } else {
245*35f00c14SToby Isaac     // silence this process's stacktrace if it is not the root of an originating error
246*35f00c14SToby Isaac     if (p != PETSC_ERROR_REPEAT && rank) petsc_traceback_error_silent = PETSC_TRUE;
247*35f00c14SToby Isaac     if (fun) {
248*35f00c14SToby Isaac       PetscBool ismain = PETSC_FALSE;
249*35f00c14SToby Isaac 
250*35f00c14SToby Isaac       ierr = PetscStrncmp(fun, "main", 4, &ismain);
251*35f00c14SToby Isaac       if (ismain && petsc_traceback_error_silent) {
252*35f00c14SToby Isaac         /* This results from PetscError() being called in main: PETSCABORT()
253*35f00c14SToby Isaac            will be called after the error handler.  But this thread is not the
254*35f00c14SToby Isaac            root rank of the communicator that initialized the error.  So sleep
255*35f00c14SToby Isaac            to allow the root thread to finish its printing.
256*35f00c14SToby Isaac 
257*35f00c14SToby Isaac            (Unless this is running CI, in which case do not sleep because
258*35f00c14SToby Isaac            we expect all processes to call MPI_Finalize() and make a clean
259*35f00c14SToby Isaac            exit.) */
260*35f00c14SToby Isaac         if (!PetscCIEnabledPortableErrorOutput) ierr = PetscSleep(10.0);
261*35f00c14SToby Isaac       }
262*35f00c14SToby Isaac     }
263997adca8SBarry Smith   }
2643ba16761SJacob Faibussowitsch   (void)ierr;
265362febeeSStefano Zampini   return n;
266e5c89e4eSSatish Balay }
267