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 defined(PETSC_HAVE_UNISTD_H)
      6:   #include <unistd.h>
      7: #endif
      8: #include "err.h"
      9: #include <petsc/private/logimpl.h>

     11: #if defined(PETSC_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) {
     69:     PetscErrorPrintfCalled = PETSC_TRUE;

     71:     /*
     72:         On the SGI machines and Cray T3E, if errors are generated  "simultaneously" by
     73:       different processors, the messages are printed all jumbled up; to try to
     74:       prevent this we have each processor wait based on their rank
     75:     */
     76: #if defined(PETSC_CAN_SLEEP_AFTER_ERROR)
     77:     {
     78:       PetscMPIInt rank = PetscGlobalRank > 8 ? 8 : PetscGlobalRank;
     79:       (void)PetscSleep((PetscReal)rank);
     80:     }
     81: #endif
     82:   }

     84:   (void)PetscFPrintf(PETSC_COMM_SELF, PETSC_STDERR, "[%d]PETSC ERROR: ", PetscGlobalRank);
     85:   va_start(Argp, format);
     86:   (void)(*PetscVFPrintf)(PETSC_STDERR, format, Argp);
     87:   va_end(Argp);
     88:   return PETSC_SUCCESS;
     89: }

     91: /*
     92:    On some systems when the stderr is nested through several levels of shell script
     93:    before being passed to a file the isatty() falsely returns true resulting in
     94:    the screen highlight variables being passed through the test harness. Therefore
     95:    simply do not highlight when the PETSC_STDERR is PETSC_STDOUT.
     96: */
     97: static void PetscErrorPrintfHilight(void)
     98: {
     99: #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
    100:   if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
    101:     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR, "\033[1;31m");
    102:   }
    103: #endif
    104: }

    106: static void PetscErrorPrintfNormal(void)
    107: {
    108: #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
    109:   if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
    110:     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR, "\033[0;39m\033[0;49m");
    111:   }
    112: #endif
    113: }

    115: PETSC_EXTERN PetscErrorCode PetscOptionsViewError(void);

    117: static PETSC_TLS PetscBool petsc_traceback_error_silent = PETSC_FALSE;

    119: /*@C
    120:   PetscTraceBackErrorHandler - Default error handler routine that generates a traceback on error detection.

    122:   Not Collective, No Fortran Support

    124:   Input Parameters:
    125: + comm - communicator over which error occurred
    126: . line - the line number of the error (usually indicated by `__LINE__` in the calling routine)
    127: . fun  - the function name
    128: . file - the file in which the error was detected (usually indicated by `__FILE__` in the calling routine)
    129: . mess - an error text string, usually just printed to the screen
    130: . n    - the generic error number
    131: . p    - `PETSC_ERROR_INITIAL` if this is the first call the error handler, otherwise `PETSC_ERROR_REPEAT`
    132: - ctx  - error handler context

    134:   Options Database Keys:
    135: + -error_output_stdout - output the error messages to `stdout` instead of the default `stderr`
    136: - -error_output_none   - do not output the error messages

    138:   Notes:
    139:   Users do not directly call this routine

    141:   Use `PetscPushErrorHandler()` to set the desired error handler.

    143:   Level: developer

    145: .seealso: `PetscError()`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`,
    146:           `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscReturnErrorHandler()`, `PetscEmacsClientErrorHandler()`,
    147:            `PETSC_ERROR_INITIAL`, `PETSC_ERROR_REPEAT`, `PetscErrorCode`, `PetscErrorType`
    148:  @*/
    149: PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, PetscCtx ctx)
    150: {
    151:   PetscMPIInt rank = 0;

    153:   (void)ctx;
    154:   if (comm != PETSC_COMM_SELF) MPI_Comm_rank(comm, &rank);

    156:   // reinitialize the error handler when a new initializing error is detected
    157:   if (p != PETSC_ERROR_REPEAT) {
    158:     petsc_traceback_error_silent = PETSC_FALSE;
    159:     if (PetscCIEnabledPortableErrorOutput) {
    160:       PetscMPIInt size = 1;

    162:       if (comm != MPI_COMM_NULL) MPI_Comm_size(comm, &size);
    163:       petscabortmpifinalize = (size == PetscGlobalSize) ? PETSC_TRUE : PETSC_FALSE;
    164:     }
    165:   }

    167:   if (rank == 0 && (!PetscCIEnabledPortableErrorOutput || PetscGlobalRank == 0) && (p != PETSC_ERROR_REPEAT || !petsc_traceback_error_silent)) {
    168:     static int cnt    = 1;
    169:     PetscBool  python = (n == PETSC_ERR_PYTHON && cnt == 1) ? PETSC_TRUE : PETSC_FALSE;

    171:     if (p == PETSC_ERROR_INITIAL || python) {
    172:       PetscErrorPrintfHilight();
    173:       (void)(*PetscErrorPrintf)("--------------------- Error Message --------------------------------------------------------------\n");
    174:       PetscErrorPrintfNormal();
    175:       if (cnt > 1) {
    176:         (void)(*PetscErrorPrintf)("  It appears a new error in the code was triggered after a previous error, possibly because:\n");
    177:         (void)(*PetscErrorPrintf)("  -  The first error was not properly handled via (for example) the use of\n");
    178:         (void)(*PetscErrorPrintf)("     PetscCall(TheFunctionThatErrors()); or\n");
    179:         (void)(*PetscErrorPrintf)("  -  The second error was triggered while handling the first error.\n");
    180:         (void)(*PetscErrorPrintf)("  Above is the traceback for the previous unhandled error, below the traceback for the next error\n");
    181:         (void)(*PetscErrorPrintf)("  ALL ERRORS in the PETSc libraries are fatal, you should add the appropriate error checking to the code\n");
    182:         cnt = 1;
    183:       }
    184:     }
    185:     if (cnt == 1) {
    186:       if (n == PETSC_ERR_MEM || n == PETSC_ERR_MEM_LEAK) (void)PetscErrorMemoryMessage(n);
    187:       else {
    188:         const char *text;
    189:         (void)PetscErrorMessage(n, &text, NULL);
    190:         if (text) (void)(*PetscErrorPrintf)("%s\n", text);
    191:       }
    192:       if (python) (void)PetscPythonPrintError();
    193:       else if (mess) (void)(*PetscErrorPrintf)("%s\n", mess);
    194: #if defined(PETSC_PKG_CUDA_MIN_ARCH)
    195:       int confCudaArch = PETSC_PKG_CUDA_MIN_ARCH;    // if PETSc was configured with numbered CUDA arches, get the min arch.
    196:       int runCudaArch  = PetscDeviceCUPMRuntimeArch; // 0 indicates the code has never initialized a cuda device.
    197:       if (runCudaArch && confCudaArch > runCudaArch) {
    198:         (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);
    199:         (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);
    200:       }
    201: #endif
    202:       (void)PetscOptionsLeftError();
    203:       (void)(*PetscErrorPrintf)("See https://petsc.org/release/faq/ for trouble shooting.\n");
    204:       if (!PetscCIEnabledPortableErrorOutput) {
    205:         size_t clen;

    207:         (void)(*PetscErrorPrintf)("%s\n", version);
    208:         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);
    209:         (void)PetscStrlen(petscconfigureoptions, &clen);
    210:         (void)(*PetscErrorPrintf)("Configure options: %s\n", clen ? petscconfigureoptions : "none used");
    211:       }
    212:     }
    213:     /* print line of stack trace */
    214:     if (fun) (void)(*PetscErrorPrintf)("#%d %s() at %s:%d\n", cnt++, fun, PetscCIFilename(file), PetscCILinenumber(line));
    215:     else if (file) (void)(*PetscErrorPrintf)("#%d %s:%d\n", cnt++, PetscCIFilename(file), PetscCILinenumber(line));
    216:     if (fun) {
    217:       PetscBool ismain = PETSC_FALSE;

    219:       (void)PetscStrncmp(fun, "main", 4, &ismain);
    220:       if (ismain) {
    221:         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);
    222:         (void)PetscOptionsViewError();
    223:         PetscErrorPrintfHilight();
    224:         (void)(*PetscErrorPrintf)("----------------End of Error Message -------send entire error message to petsc-maint@mcs.anl.gov----------\n");
    225:         PetscErrorPrintfNormal();
    226:       }
    227:     }
    228:   } else {
    229:     // silence this process's stacktrace if it is not the root of an originating error
    230:     if (p != PETSC_ERROR_REPEAT && rank) petsc_traceback_error_silent = PETSC_TRUE;
    231:     if (fun) {
    232:       PetscBool ismain = PETSC_FALSE;

    234:       (void)PetscStrncmp(fun, "main", 4, &ismain);
    235:       if (ismain && petsc_traceback_error_silent) {
    236:         /* This results from PetscError() being called in main: PETSCABORT()
    237:            will be called after the error handler.  But this thread is not the
    238:            root rank of the communicator that initialized the error.  So sleep
    239:            to allow the root thread to finish its printing.

    241:            (Unless this is running CI, in which case do not sleep because
    242:            we expect all processes to call MPI_Finalize() and make a clean
    243:            exit.) */
    244:         if (!PetscCIEnabledPortableErrorOutput) (void)PetscSleep(10.0);
    245:       }
    246:     }
    247:   }
    248:   return n;
    249: }