Actual source code: errtrace.c
1: #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for fileno() */
2: #include <petscsys.h>
3: #include <petsc/private/petscimpl.h>
4: #include <petscconfiginfo.h>
5: #if PetscDefined(HAVE_UNISTD_H)
6: #include <unistd.h>
7: #endif
8: #include "err.h"
9: #include <petsc/private/logimpl.h>
11: #if PetscDefined(HAVE_CUPM)
12: #include <petsc/private/deviceimpl.h>
13: #endif
15: static char arch[128], hostname[128], username[128], pname[PETSC_MAX_PATH_LEN], date[128];
16: static PetscBool PetscErrorPrintfInitializeCalled = PETSC_FALSE;
17: static char version[256];
19: /*@C
20: PetscErrorPrintfInitialize - Cache the architecture, host name, user name, program name and date so that PETSc's error-traceback printer does not need to make system calls from inside a signal handler
22: Collective
24: Level: developer
26: Note:
27: Called from `PetscInitialize()`; users normally do not need to call this directly.
29: .seealso: `PetscErrorPrintf`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`
30: @*/
31: PetscErrorCode PetscErrorPrintfInitialize(void)
32: {
33: PetscBool use_stdout = PETSC_FALSE, use_none = PETSC_FALSE;
35: PetscFunctionBegin;
36: PetscCall(PetscGetArchType(arch, sizeof(arch)));
37: PetscCall(PetscGetHostName(hostname, sizeof(hostname)));
38: PetscCall(PetscGetUserName(username, sizeof(username)));
39: PetscCall(PetscGetProgramName(pname, sizeof(pname)));
40: PetscCall(PetscGetDate(date, sizeof(date)));
41: PetscCall(PetscGetVersion(version, sizeof(version)));
43: PetscCall(PetscOptionsGetBool(NULL, NULL, "-error_output_stdout", &use_stdout, NULL));
44: if (use_stdout) PETSC_STDERR = PETSC_STDOUT;
45: PetscCall(PetscOptionsGetBool(NULL, NULL, "-error_output_none", &use_none, NULL));
46: if (use_none) PetscErrorPrintf = PetscErrorPrintfNone;
47: PetscErrorPrintfInitializeCalled = PETSC_TRUE;
48: PetscFunctionReturn(PETSC_SUCCESS);
49: }
51: PetscErrorCode PetscErrorPrintfNone(const char format[], ...)
52: {
53: return PETSC_SUCCESS;
54: }
56: PetscErrorCode PetscErrorPrintfDefault(const char format[], ...)
57: {
58: va_list Argp;
59: static PetscBool PetscErrorPrintfCalled = PETSC_FALSE;
61: /*
62: This function does not call PetscFunctionBegin and PetscFunctionReturn() because
63: it may be called by PetscStackView().
65: This function does not do error checking because it is called by the error handlers.
66: */
68: if (!PetscErrorPrintfCalled) PetscErrorPrintfCalled = PETSC_TRUE;
70: (void)PetscFPrintf(PETSC_COMM_SELF, PETSC_STDERR, "[%d]PETSC ERROR: ", PetscGlobalRank);
71: va_start(Argp, format);
72: (void)(*PetscVFPrintf)(PETSC_STDERR, format, Argp);
73: va_end(Argp);
74: return PETSC_SUCCESS;
75: }
77: /*
78: On some systems when the stderr is nested through several levels of shell script
79: before being passed to a file the isatty() falsely returns true resulting in
80: the screen highlight variables being passed through the test harness. Therefore
81: simply do not highlight when the PETSC_STDERR is PETSC_STDOUT.
82: */
83: static void PetscErrorPrintfHilight(void)
84: {
85: #if PetscDefined(HAVE_UNISTD_H) && PetscDefined(USE_ISATTY)
86: if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
87: if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR, "\033[1;31m");
88: }
89: #endif
90: }
92: static void PetscErrorPrintfNormal(void)
93: {
94: #if PetscDefined(HAVE_UNISTD_H) && PetscDefined(USE_ISATTY)
95: if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
96: if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR, "\033[0;39m\033[0;49m");
97: }
98: #endif
99: }
101: PETSC_EXTERN PetscErrorCode PetscOptionsViewError(void);
103: static PETSC_TLS PetscBool petsc_traceback_error_silent = PETSC_FALSE;
105: /*@C
106: PetscTraceBackErrorHandler - Default error handler routine that generates a traceback on error detection.
108: Not Collective, No Fortran Support
110: Input Parameters:
111: + comm - communicator over which error occurred
112: . line - the line number of the error (usually indicated by `__LINE__` in the calling routine)
113: . fun - the function name
114: . file - the file in which the error was detected (usually indicated by `__FILE__` in the calling routine)
115: . mess - an error text string, usually just printed to the screen
116: . n - the generic error number
117: . p - `PETSC_ERROR_INITIAL` if this is the first call the error handler, otherwise `PETSC_ERROR_REPEAT`
118: - ctx - error handler context
120: Options Database Keys:
121: + -error_output_stdout - output the error messages to `stdout` instead of the default `stderr`
122: - -error_output_none - do not output the error messages
124: Notes:
125: Users do not directly call this routine
127: Use `PetscPushErrorHandler()` to set the desired error handler.
129: Level: developer
131: .seealso: `PetscError()`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`,
132: `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscReturnErrorHandler()`, `PetscEmacsClientErrorHandler()`,
133: `PETSC_ERROR_INITIAL`, `PETSC_ERROR_REPEAT`, `PetscErrorCode`, `PetscErrorType`
134: @*/
135: PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, PetscCtx ctx)
136: {
137: PetscMPIInt rank = 0;
139: (void)ctx;
140: if (comm != PETSC_COMM_SELF) MPI_Comm_rank(comm, &rank);
142: // reinitialize the error handler when a new initializing error is detected
143: if (p != PETSC_ERROR_REPEAT) {
144: petsc_traceback_error_silent = PETSC_FALSE;
145: if (PetscCIEnabledPortableErrorOutput) {
146: PetscMPIInt size = 1;
148: if (comm != MPI_COMM_NULL) MPI_Comm_size(comm, &size);
149: petscabortmpifinalize = (size == PetscGlobalSize) ? PETSC_TRUE : PETSC_FALSE;
150: }
151: }
153: if (rank == 0 && (!PetscCIEnabledPortableErrorOutput || PetscGlobalRank == 0) && (p != PETSC_ERROR_REPEAT || !petsc_traceback_error_silent)) {
154: static int cnt = 1;
155: PetscBool python = (n == PETSC_ERR_PYTHON && cnt == 1) ? PETSC_TRUE : PETSC_FALSE;
157: if (p == PETSC_ERROR_INITIAL || python) {
158: PetscErrorPrintfHilight();
159: (void)(*PetscErrorPrintf)("--------------------- Error Message --------------------------------------------------------------\n");
160: PetscErrorPrintfNormal();
161: if (cnt > 1) {
162: (void)(*PetscErrorPrintf)(" It appears a new error in the code was triggered after a previous error, possibly because:\n");
163: (void)(*PetscErrorPrintf)(" - The first error was not properly handled via (for example) the use of\n");
164: (void)(*PetscErrorPrintf)(" PetscCall(TheFunctionThatErrors()); or\n");
165: (void)(*PetscErrorPrintf)(" - The second error was triggered while handling the first error.\n");
166: (void)(*PetscErrorPrintf)(" Above is the traceback for the previous unhandled error, below the traceback for the next error\n");
167: (void)(*PetscErrorPrintf)(" ALL ERRORS in the PETSc libraries are fatal, you should add the appropriate error checking to the code\n");
168: cnt = 1;
169: }
170: }
171: if (cnt == 1) {
172: if (n == PETSC_ERR_MEM || n == PETSC_ERR_MEM_LEAK) (void)PetscErrorMemoryMessage(n);
173: else {
174: const char *text;
175: (void)PetscErrorMessage(n, &text, NULL);
176: if (text) (void)(*PetscErrorPrintf)("%s\n", text);
177: }
178: if (python) (void)PetscPythonPrintError();
179: else if (mess) (void)(*PetscErrorPrintf)("%s\n", mess);
180: #if defined(PETSC_PKG_CUDA_MIN_ARCH)
181: int confCudaArch = PETSC_PKG_CUDA_MIN_ARCH; // if PETSc was configured with numbered CUDA arches, get the min arch.
182: int runCudaArch = PetscDeviceCUPMRuntimeArch; // 0 indicates the code has never initialized a cuda device.
183: if (runCudaArch && confCudaArch > runCudaArch) {
184: (void)(*PetscErrorPrintf)("WARNING! Run on a CUDA device with GPU architecture %d, but PETSc was configured with a minimal GPU architecture %d.\n", runCudaArch, confCudaArch);
185: (void)(*PetscErrorPrintf)("If it is a cudaErrorNoKernelImageForDevice error, you may need to reconfigure PETSc with --with-cuda-arch=%d or --with-cuda-arch=%d,%d\n", runCudaArch, runCudaArch, confCudaArch);
186: }
187: #endif
188: (void)PetscOptionsLeftError();
189: (void)(*PetscErrorPrintf)("See https://petsc.org/release/faq/ for trouble shooting.\n");
190: if (!PetscCIEnabledPortableErrorOutput) {
191: size_t clen;
193: (void)(*PetscErrorPrintf)("%s\n", version);
194: if (PetscErrorPrintfInitializeCalled) (void)(*PetscErrorPrintf)("%s with %d MPI process(es) and PETSC_ARCH %s on %s by %s %s\n", pname, PetscGlobalSize, arch, hostname, username, date);
195: (void)PetscStrlen(petscconfigureoptions, &clen);
196: (void)(*PetscErrorPrintf)("Configure options: %s\n", clen ? petscconfigureoptions : "none used");
197: }
198: }
199: /* print line of stack trace */
200: if (fun) (void)(*PetscErrorPrintf)("#%d %s() at %s:%d\n", cnt++, fun, PetscCIFilename(file), PetscCILinenumber(line));
201: else if (file) (void)(*PetscErrorPrintf)("#%d %s:%d\n", cnt++, PetscCIFilename(file), PetscCILinenumber(line));
202: if (fun) {
203: PetscBool ismain = PETSC_FALSE;
205: (void)PetscStrncmp(fun, "main", 4, &ismain);
206: if (ismain) {
207: if ((n <= PETSC_ERR_MIN_VALUE) || (n >= PETSC_ERR_MAX_VALUE)) (void)(*PetscErrorPrintf)("Reached the main program with an out-of-range error code %d. This should never happen\n", n);
208: (void)PetscOptionsViewError();
209: PetscErrorPrintfHilight();
210: (void)(*PetscErrorPrintf)("----------------End of Error Message -------send entire error message to petsc-maint@mcs.anl.gov----------\n");
211: PetscErrorPrintfNormal();
212: }
213: }
214: } else {
215: // silence this process's stacktrace if it is not the root of an originating error
216: if (p != PETSC_ERROR_REPEAT && rank) petsc_traceback_error_silent = PETSC_TRUE;
217: if (fun) {
218: PetscBool ismain = PETSC_FALSE;
220: (void)PetscStrncmp(fun, "main", 4, &ismain);
221: if (ismain && petsc_traceback_error_silent) {
222: /* This results from PetscError() being called in main: PETSCABORT()
223: will be called after the error handler. But this thread is not the
224: root rank of the communicator that initialized the error. So sleep
225: to allow the root thread to finish its printing.
227: (Unless this is running CI, in which case do not sleep because
228: we expect all processes to call MPI_Finalize() and make a clean
229: exit.) */
230: if (!PetscCIEnabledPortableErrorOutput) (void)PetscSleep(10.0);
231: }
232: }
233: }
234: return n;
235: }