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"
10: #if PetscDefined(HAVE_CUPM)
11: #include <petsc/private/deviceimpl.h>
12: #endif
14: static char arch[128], hostname[128], username[128], pname[PETSC_MAX_PATH_LEN], date[128];
15: static PetscBool PetscErrorPrintfInitializeCalled = PETSC_FALSE;
16: static char version[256];
18: /*@
19: 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
21: Collective
23: Level: developer
25: Note:
26: Called from `PetscInitialize()`; users normally do not need to call this directly.
28: .seealso: `PetscErrorPrintf`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`
29: @*/
30: PetscErrorCode PetscErrorPrintfInitialize(void)
31: {
32: PetscBool use_stdout = PETSC_FALSE, use_none = PETSC_FALSE;
34: PetscFunctionBegin;
35: PetscCall(PetscGetArchType(arch, sizeof(arch)));
36: PetscCall(PetscGetHostName(hostname, sizeof(hostname)));
37: PetscCall(PetscGetUserName(username, sizeof(username)));
38: PetscCall(PetscGetProgramName(pname, sizeof(pname)));
39: PetscCall(PetscGetDate(date, sizeof(date)));
40: PetscCall(PetscGetVersion(version, sizeof(version)));
42: PetscCall(PetscOptionsGetBool(NULL, NULL, "-error_output_stdout", &use_stdout, NULL));
43: if (use_stdout) PETSC_STDERR = PETSC_STDOUT;
44: PetscCall(PetscOptionsGetBool(NULL, NULL, "-error_output_none", &use_none, NULL));
45: if (use_none) PetscErrorPrintf = PetscErrorPrintfNone;
46: PetscErrorPrintfInitializeCalled = PETSC_TRUE;
47: PetscFunctionReturn(PETSC_SUCCESS);
48: }
50: /*@
51: PetscErrorPrintfNone - An alternative to `PetscErrorPrintfDefault()` that prints no error messages
53: Not Collective; No Fortran Support
55: Input Parameter:
56: . format - the usual `printf()` format string
58: Options Database Key:
59: . -error_output_none - install this function; the error is still handled in the same way, only the message is suppressed
61: Level: developer
63: .seealso: `PetscErrorPrintf()`, `PetscErrorPrintfDefault()`, `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscPrintf()`,
64: `PetscPushErrorHandler()`, `PetscVFPrintf()`, `PetscHelpPrintf()`
65: @*/
66: PetscErrorCode PetscErrorPrintfNone(const char format[], ...)
67: {
68: return PETSC_SUCCESS;
69: }
71: /*@
72: PetscErrorPrintfDefault - The default error printing used by `PetscErrorPrintf()`
74: Not Collective; No Fortran Support
76: Input Parameter:
77: . format - the usual `printf()` format string
79: Options Database Key:
80: . -error_output_stdout - write the error messages to `stdout` instead of the default `stderr`
82: Level: developer
84: .seealso: `PetscErrorPrintf()`, `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscPrintf()`, `PetscPushErrorHandler()`,
85: `PetscVFPrintf()`, `PetscHelpPrintf()`, `PetscErrorPrintfNone()`
86: @*/
87: PetscErrorCode PetscErrorPrintfDefault(const char format[], ...)
88: {
89: va_list Argp;
91: /*
92: This function does not call PetscFunctionBegin and PetscFunctionReturn() because
93: it may be called by PetscStackView().
95: This function does not do error checking because it is called by the error handlers.
96: */
98: (void)PetscFPrintf(PETSC_COMM_SELF, PETSC_STDERR, "[%d]PETSC ERROR: ", PetscGlobalRank);
99: va_start(Argp, format);
100: (void)(*PetscVFPrintf)(PETSC_STDERR, format, Argp);
101: va_end(Argp);
102: return PETSC_SUCCESS;
103: }
105: /*
106: On some systems when the stderr is nested through several levels of shell script
107: before being passed to a file the isatty() falsely returns true resulting in
108: the screen highlight variables being passed through the test harness. Therefore
109: simply do not highlight when the PETSC_STDERR is PETSC_STDOUT.
110: */
111: static void PetscErrorPrintfHilight(void)
112: {
113: #if PetscDefined(HAVE_UNISTD_H) && PetscDefined(USE_ISATTY)
114: if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
115: if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR, "\033[1;31m");
116: }
117: #endif
118: }
120: static void PetscErrorPrintfNormal(void)
121: {
122: #if PetscDefined(HAVE_UNISTD_H) && PetscDefined(USE_ISATTY)
123: if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
124: if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR, "\033[0;39m\033[0;49m");
125: }
126: #endif
127: }
129: PETSC_EXTERN PetscErrorCode PetscOptionsViewError(void);
131: static PETSC_TLS PetscBool petsc_traceback_error_silent = PETSC_FALSE;
133: /*@
134: PetscTraceBackErrorHandler - Default error handler routine that generates a traceback on error detection.
136: Not Collective, No Fortran Support
138: Input Parameters:
139: + comm - communicator over which error occurred
140: . line - the line number of the error (usually indicated by `__LINE__` in the calling routine)
141: . fun - the function name
142: . file - the file in which the error was detected (usually indicated by `__FILE__` in the calling routine)
143: . mess - an error text string, usually just printed to the screen
144: . n - the generic error number
145: . p - `PETSC_ERROR_INITIAL` if this is the first call the error handler, otherwise `PETSC_ERROR_REPEAT`
146: - ctx - error handler context
148: Options Database Keys:
149: + -error_output_stdout - output the error messages to `stdout` instead of the default `stderr`
150: - -error_output_none - do not output the error messages
152: Notes:
153: Users do not directly call this routine
155: Use `PetscPushErrorHandler()` to set the desired error handler.
157: Level: developer
159: .seealso: `PetscError()`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`,
160: `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscReturnErrorHandler()`, `PetscEmacsClientErrorHandler()`,
161: `PETSC_ERROR_INITIAL`, `PETSC_ERROR_REPEAT`, `PetscErrorCode`, `PetscErrorType`
162: @*/
163: PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, PetscCtx ctx)
164: {
165: PetscMPIInt rank = 0;
167: (void)ctx;
168: if (comm != PETSC_COMM_SELF) MPI_Comm_rank(comm, &rank);
170: // reinitialize the error handler when a new initializing error is detected
171: if (p != PETSC_ERROR_REPEAT) {
172: petsc_traceback_error_silent = PETSC_FALSE;
173: if (PetscCIEnabledPortableErrorOutput) {
174: PetscMPIInt size = 1;
176: if (comm != MPI_COMM_NULL) MPI_Comm_size(comm, &size);
177: petscabortmpifinalize = (size == PetscGlobalSize) ? PETSC_TRUE : PETSC_FALSE;
178: }
179: }
181: if (rank == 0 && (!PetscCIEnabledPortableErrorOutput || PetscGlobalRank == 0) && (p != PETSC_ERROR_REPEAT || !petsc_traceback_error_silent)) {
182: static int cnt = 1;
183: PetscBool python = (n == PETSC_ERR_PYTHON && cnt == 1) ? PETSC_TRUE : PETSC_FALSE;
185: if (p == PETSC_ERROR_INITIAL || python) {
186: PetscErrorPrintfHilight();
187: (void)(*PetscErrorPrintf)("--------------------- Error Message --------------------------------------------------------------\n");
188: PetscErrorPrintfNormal();
189: if (cnt > 1) {
190: (void)(*PetscErrorPrintf)(" It appears a new error in the code was triggered after a previous error, possibly because:\n");
191: (void)(*PetscErrorPrintf)(" - The first error was not properly handled via (for example) the use of\n");
192: (void)(*PetscErrorPrintf)(" PetscCall(TheFunctionThatErrors()); or\n");
193: (void)(*PetscErrorPrintf)(" - The second error was triggered while handling the first error.\n");
194: (void)(*PetscErrorPrintf)(" Above is the traceback for the previous unhandled error, below the traceback for the next error\n");
195: (void)(*PetscErrorPrintf)(" ALL ERRORS in the PETSc libraries are fatal, you should add the appropriate error checking to the code\n");
196: cnt = 1;
197: }
198: }
199: if (cnt == 1) {
200: if (n == PETSC_ERR_MEM || n == PETSC_ERR_MEM_LEAK) (void)PetscErrorMemoryMessage(n);
201: else {
202: const char *text;
203: (void)PetscErrorMessage(n, &text, NULL);
204: if (text) (void)(*PetscErrorPrintf)("%s\n", text);
205: }
206: if (python) (void)PetscPythonPrintError();
207: else if (mess) (void)(*PetscErrorPrintf)("%s\n", mess);
208: #if defined(PETSC_PKG_CUDA_MIN_ARCH)
209: int confCudaArch = PETSC_PKG_CUDA_MIN_ARCH; // if PETSc was configured with numbered CUDA arches, get the min arch.
210: int runCudaArch = PetscDeviceCUPMRuntimeArch; // 0 indicates the code has never initialized a cuda device.
211: if (runCudaArch && confCudaArch > runCudaArch) {
212: (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);
213: (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);
214: }
215: #endif
216: (void)PetscOptionsLeftError();
217: (void)(*PetscErrorPrintf)("See https://petsc.org/release/faq/ for trouble shooting.\n");
218: if (!PetscCIEnabledPortableErrorOutput) {
219: size_t clen;
221: (void)(*PetscErrorPrintf)("%s\n", version);
222: 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);
223: (void)PetscStrlen(petscconfigureoptions, &clen);
224: (void)(*PetscErrorPrintf)("Configure options: %s\n", clen ? petscconfigureoptions : "none used");
225: }
226: }
227: /* print line of stack trace */
228: if (fun) (void)(*PetscErrorPrintf)("#%d %s() at %s:%d\n", cnt++, fun, PetscCIFilename(file), PetscCILinenumber(line));
229: else if (file) (void)(*PetscErrorPrintf)("#%d %s:%d\n", cnt++, PetscCIFilename(file), PetscCILinenumber(line));
230: if (fun) {
231: PetscBool ismain = PETSC_FALSE;
233: (void)PetscStrncmp(fun, "main", 4, &ismain);
234: if (ismain) {
235: 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);
236: (void)PetscOptionsViewError();
237: PetscErrorPrintfHilight();
238: (void)(*PetscErrorPrintf)("----------------End of Error Message -------send entire error message to petsc-maint@mcs.anl.gov----------\n");
239: PetscErrorPrintfNormal();
240: }
241: }
242: } else {
243: // silence this process's stacktrace if it is not the root of an originating error
244: if (p != PETSC_ERROR_REPEAT && rank) petsc_traceback_error_silent = PETSC_TRUE;
245: if (fun) {
246: PetscBool ismain = PETSC_FALSE;
248: (void)PetscStrncmp(fun, "main", 4, &ismain);
249: if (ismain && petsc_traceback_error_silent) {
250: /* This results from PetscError() being called in main: PETSCABORT()
251: will be called after the error handler. But this thread is not the
252: root rank of the communicator that initialized the error. So sleep
253: to allow the root thread to finish its printing.
255: (Unless this is running CI, in which case do not sleep because
256: we expect all processes to call MPI_Finalize() and make a clean
257: exit.) */
258: if (!PetscCIEnabledPortableErrorOutput) (void)PetscSleep(10.0);
259: }
260: }
261: }
262: return n;
263: }