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: /*
     20:    Initializes arch, hostname, username, date so that system calls do NOT need
     21:    to be made during the error handler.
     22: */
     23: PetscErrorCode PetscErrorPrintfInitialize(void)
     24: {
     25:   PetscBool use_stdout = PETSC_FALSE, use_none = PETSC_FALSE;

     27:   PetscFunctionBegin;
     28:   PetscCall(PetscGetArchType(arch, sizeof(arch)));
     29:   PetscCall(PetscGetHostName(hostname, sizeof(hostname)));
     30:   PetscCall(PetscGetUserName(username, sizeof(username)));
     31:   PetscCall(PetscGetProgramName(pname, sizeof(pname)));
     32:   PetscCall(PetscGetDate(date, sizeof(date)));
     33:   PetscCall(PetscGetVersion(version, sizeof(version)));

     35:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-error_output_stdout", &use_stdout, NULL));
     36:   if (use_stdout) PETSC_STDERR = PETSC_STDOUT;
     37:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-error_output_none", &use_none, NULL));
     38:   if (use_none) PetscErrorPrintf = PetscErrorPrintfNone;
     39:   PetscErrorPrintfInitializeCalled = PETSC_TRUE;
     40:   PetscFunctionReturn(PETSC_SUCCESS);
     41: }

     43: PetscErrorCode PetscErrorPrintfNone(const char format[], ...)
     44: {
     45:   return PETSC_SUCCESS;
     46: }

     48: PetscErrorCode PetscErrorPrintfDefault(const char format[], ...)
     49: {
     50:   va_list          Argp;
     51:   static PetscBool PetscErrorPrintfCalled = PETSC_FALSE;

     53:   /*
     54:       This function does not call PetscFunctionBegin and PetscFunctionReturn() because
     55:     it may be called by PetscStackView().

     57:       This function does not do error checking because it is called by the error handlers.
     58:   */

     60:   if (!PetscErrorPrintfCalled) {
     61:     PetscErrorPrintfCalled = PETSC_TRUE;

     63:     /*
     64:         On the SGI machines and Cray T3E, if errors are generated  "simultaneously" by
     65:       different processors, the messages are printed all jumbled up; to try to
     66:       prevent this we have each processor wait based on their rank
     67:     */
     68: #if defined(PETSC_CAN_SLEEP_AFTER_ERROR)
     69:     {
     70:       PetscMPIInt rank = PetscGlobalRank > 8 ? 8 : PetscGlobalRank;
     71:       (void)PetscSleep((PetscReal)rank);
     72:     }
     73: #endif
     74:   }

     76:   (void)PetscFPrintf(PETSC_COMM_SELF, PETSC_STDERR, "[%d]PETSC ERROR: ", PetscGlobalRank);
     77:   va_start(Argp, format);
     78:   (void)(*PetscVFPrintf)(PETSC_STDERR, format, Argp);
     79:   va_end(Argp);
     80:   return PETSC_SUCCESS;
     81: }

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

     98: static void PetscErrorPrintfNormal(void)
     99: {
    100: #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
    101:   if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
    102:     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR, "\033[0;39m\033[0;49m");
    103:   }
    104: #endif
    105: }

    107: PETSC_EXTERN PetscErrorCode PetscOptionsViewError(void);

    109: static PETSC_TLS PetscBool petsc_traceback_error_silent = PETSC_FALSE;

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

    114:   Not Collective, No Fortran Support

    116:   Input Parameters:
    117: + comm - communicator over which error occurred
    118: . line - the line number of the error (usually indicated by `__LINE__` in the calling routine)
    119: . fun  - the function name
    120: . file - the file in which the error was detected (usually indicated by `__FILE__` in the calling routine)
    121: . mess - an error text string, usually just printed to the screen
    122: . n    - the generic error number
    123: . p    - `PETSC_ERROR_INITIAL` if this is the first call the error handler, otherwise `PETSC_ERROR_REPEAT`
    124: - ctx  - error handler context

    126:   Options Database Keys:
    127: + -error_output_stdout - output the error messages to `stdout` instead of the default `stderr`
    128: - -error_output_none   - do not output the error messages

    130:   Notes:
    131:   Users do not directly call this routine

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

    135:   Level: developer

    137: .seealso: `PetscError()`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`,
    138:           `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscReturnErrorHandler()`, `PetscEmacsClientErrorHandler()`,
    139:            `PETSC_ERROR_INITIAL`, `PETSC_ERROR_REPEAT`, `PetscErrorCode`, `PetscErrorType`
    140:  @*/
    141: PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, PetscCtx ctx)
    142: {
    143:   PetscMPIInt rank = 0;

    145:   (void)ctx;
    146:   if (comm != PETSC_COMM_SELF) MPI_Comm_rank(comm, &rank);

    148:   // reinitialize the error handler when a new initializing error is detected
    149:   if (p != PETSC_ERROR_REPEAT) {
    150:     petsc_traceback_error_silent = PETSC_FALSE;
    151:     if (PetscCIEnabledPortableErrorOutput) {
    152:       PetscMPIInt size = 1;

    154:       if (comm != MPI_COMM_NULL) MPI_Comm_size(comm, &size);
    155:       petscabortmpifinalize = (size == PetscGlobalSize) ? PETSC_TRUE : PETSC_FALSE;
    156:     }
    157:   }

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

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

    199:         (void)(*PetscErrorPrintf)("%s\n", version);
    200:         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);
    201:         (void)PetscStrlen(petscconfigureoptions, &clen);
    202:         (void)(*PetscErrorPrintf)("Configure options: %s\n", clen ? petscconfigureoptions : "none used");
    203:       }
    204:     }
    205:     /* print line of stack trace */
    206:     if (fun) (void)(*PetscErrorPrintf)("#%d %s() at %s:%d\n", cnt++, fun, PetscCIFilename(file), PetscCILinenumber(line));
    207:     else if (file) (void)(*PetscErrorPrintf)("#%d %s:%d\n", cnt++, PetscCIFilename(file), PetscCILinenumber(line));
    208:     if (fun) {
    209:       PetscBool ismain = PETSC_FALSE;

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

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

    233:            (Unless this is running CI, in which case do not sleep because
    234:            we expect all processes to call MPI_Finalize() and make a clean
    235:            exit.) */
    236:         if (!PetscCIEnabledPortableErrorOutput) (void)PetscSleep(10.0);
    237:       }
    238:     }
    239:   }
    240:   return n;
    241: }