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

      9: /*@C
     10:    PetscIgnoreErrorHandler - Deprecated, use PetscReturnErrorHandler(). Ignores the error, allows program to continue as if error did not occure

     12:    Not Collective

     14:    Input Parameters:
     15: +  comm - communicator over which error occurred
     16: .  line - the line number of the error (indicated by __LINE__)
     17: .  file - the file in which the error was detected (indicated by __FILE__)
     18: .  mess - an error text string, usually just printed to the screen
     19: .  n - the generic error number
     20: .  p - specific error number
     21: -  ctx - error handler context

     23:    Level: developer

     25:    Notes:
     26:    Most users need not directly employ this routine and the other error
     27:    handlers, but can instead use the simplified interface SETERRQ, which has
     28:    the calling sequence
     29: $     SETERRQ(comm,number,p,mess)

     31: .seealso:  PetscReturnErrorHandler()
     32:  @*/
     33: PetscErrorCode  PetscIgnoreErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
     34: {
     35:   return n;
     36: }

     38: /* ---------------------------------------------------------------------------------------*/

     40: static char      arch[128],hostname[128],username[128],pname[PETSC_MAX_PATH_LEN],date[128];
     41: static PetscBool PetscErrorPrintfInitializeCalled = PETSC_FALSE;
     42: static char      version[256];

     44: /*
     45:    Initializes arch, hostname, username, date so that system calls do NOT need
     46:    to be made during the error handler.
     47: */
     48: PetscErrorCode  PetscErrorPrintfInitialize(void)
     49: {
     50:   PetscBool      use_stdout = PETSC_FALSE,use_none = PETSC_FALSE;

     52:   PetscGetArchType(arch,sizeof(arch));
     53:   PetscGetHostName(hostname,sizeof(hostname));
     54:   PetscGetUserName(username,sizeof(username));
     55:   PetscGetProgramName(pname,sizeof(pname));
     56:   PetscGetDate(date,sizeof(date));
     57:   PetscGetVersion(version,sizeof(version));

     59:   PetscOptionsGetBool(NULL,NULL,"-error_output_stdout",&use_stdout,NULL);
     60:   if (use_stdout) PETSC_STDERR = PETSC_STDOUT;
     61:   PetscOptionsGetBool(NULL,NULL,"-error_output_none",&use_none,NULL);
     62:   if (use_none) PetscErrorPrintf = PetscErrorPrintfNone;
     63:   PetscErrorPrintfInitializeCalled = PETSC_TRUE;
     64:   return 0;
     65: }

     67: PetscErrorCode  PetscErrorPrintfNone(const char format[],...)
     68: {
     69:   return 0;
     70: }

     72: PetscErrorCode  PetscErrorPrintfDefault(const char format[],...)
     73: {
     74:   va_list          Argp;
     75:   static PetscBool PetscErrorPrintfCalled = PETSC_FALSE;

     77:   /*
     79:     it may be called by PetscStackView().

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

     84:   if (!PetscErrorPrintfCalled) {
     85:     PetscErrorPrintfCalled = PETSC_TRUE;

     87:     /*
     88:         On the SGI machines and Cray T3E, if errors are generated  "simultaneously" by
     89:       different processors, the messages are printed all jumbled up; to try to
     90:       prevent this we have each processor wait based on their rank
     91:     */
     92: #if defined(PETSC_CAN_SLEEP_AFTER_ERROR)
     93:     {
     94:       PetscMPIInt rank;
     95:       if (PetscGlobalRank > 8) rank = 8;
     96:       else rank = PetscGlobalRank;
     97:       PetscSleep((PetscReal)rank);
     98:     }
     99: #endif
    100:   }

    102:   PetscFPrintf(PETSC_COMM_SELF,PETSC_STDERR,"[%d]PETSC ERROR: ",PetscGlobalRank);
    103:   va_start(Argp,format);
    104:   (*PetscVFPrintf)(PETSC_STDERR,format,Argp);
    105:   va_end(Argp);
    106:   return 0;
    107: }

    109: /*
    110:    On some systems when the stderr is nested through several levels of shell script
    111:    before being passed to a file the isatty() falsely returns true resulting in
    112:    the screen highlight variables being passed through the test harness. Therefore
    113:    simply do not highlight when the PETSC_STDERR is PETSC_STDOUT.
    114: */
    115: static void PetscErrorPrintfHilight(void)
    116: {
    117: #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
    118:   if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
    119:     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR,"\033[1;31m");
    120:   }
    121: #endif
    122: }

    124: static void PetscErrorPrintfNormal(void)
    125: {
    126: #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
    127:   if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
    128:     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR,"\033[0;39m\033[0;49m");
    129:   }
    130: #endif
    131: }

    133: PETSC_EXTERN PetscErrorCode  PetscOptionsViewError(void);

    135: /*@C

    137:    PetscTraceBackErrorHandler - Default error handler routine that generates
    138:    a traceback on error detection.

    140:    Not Collective

    142:    Input Parameters:
    143: +  comm - communicator over which error occurred
    144: .  line - the line number of the error (indicated by __LINE__)
    145: .  file - the file in which the error was detected (indicated by __FILE__)
    146: .  mess - an error text string, usually just printed to the screen
    147: .  n - the generic error number
    148: .  p - PETSC_ERROR_INITIAL if this is the first call the error handler, otherwise PETSC_ERROR_REPEAT
    149: -  ctx - error handler context

    151:   Options Database:
    152: +  -error_output_stdout - output the error messages to stdout instead of the default stderr
    153: -  -error_output_none - do not output the error messages

    155:    Notes:
    156:    Most users need not directly employ this routine and the other error
    157:    handlers, but can instead use the simplified interface SETERRQ, which has
    158:    the calling sequence
    159: $     SETERRQ(comm,number,n,mess)

    161:    Notes for experienced users:
    162:    Use PetscPushErrorHandler() to set the desired error handler.

    164:    Level: developer

    166: .seealso: PetscError(), PetscPushErrorHandler(), PetscPopErrorHandler(), PetscAttachDebuggerErrorHandler(),
    167:           PetscAbortErrorHandler(), PetscMPIAbortErrorHandler(), PetscReturnErrorHandler(), PetscEmacsClientErrorHandler()
    168:  @*/
    169: PetscErrorCode  PetscTraceBackErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
    170: {
    171:   PetscLogDouble mem,rss;
    172:   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
    173:   PetscMPIInt    rank = 0;

    175:   if (comm != PETSC_COMM_SELF) MPI_Comm_rank(comm,&rank);

    177:   if (rank == 0) {
    178:     PetscBool  ismain;
    179:     static int cnt = 1;

    181:     if (cnt == 1) {
    182:       PetscErrorPrintfHilight();
    183:       (*PetscErrorPrintf)("--------------------- Error Message --------------------------------------------------------------\n");
    184:       PetscErrorPrintfNormal();
    185:       if (n == PETSC_ERR_MEM) {
    186:         (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n");
    187:         (*PetscErrorPrintf)("too large an object or bleeding by not properly\n");
    188:         (*PetscErrorPrintf)("destroying unneeded objects.\n");
    189:         PetscMallocGetCurrentUsage(&mem);
    190:         PetscMemoryGetCurrentUsage(&rss);
    191:         PetscOptionsGetBool(NULL,NULL,"-malloc_dump",&flg1,NULL);
    192:         PetscOptionsGetBool(NULL,NULL,"-malloc_view",&flg2,NULL);
    193:         PetscOptionsHasName(NULL,NULL,"-malloc_view_threshold",&flg3);
    194:         if (flg2 || flg3) PetscMallocView(stdout);
    195:         else {
    196:           (*PetscErrorPrintf)("Memory allocated %.0f Memory used by process %.0f\n",mem,rss);
    197:           if (flg1) PetscMallocDump(stdout);
    198:           else (*PetscErrorPrintf)("Try running with -malloc_dump or -malloc_view for info.\n");
    199:         }
    200:       } else {
    201:         const char *text;
    202:         PetscErrorMessage(n,&text,NULL);
    203:         if (text) (*PetscErrorPrintf)("%s\n",text);
    204:       }
    205:       if (mess) (*PetscErrorPrintf)("%s\n",mess);
    206:       (*PetscErrorPrintf)("See https://petsc.org/release/faq/ for trouble shooting.\n");
    207:       (*PetscErrorPrintf)("%s\n",version);
    208:       if (PetscErrorPrintfInitializeCalled) (*PetscErrorPrintf)("%s on a %s named %s by %s %s\n",pname,arch,hostname,username,date);
    209:       (*PetscErrorPrintf)("Configure options %s\n",petscconfigureoptions);
    210:     }
    211:     /* print line of stack trace */
    212:     (*PetscErrorPrintf)("#%d %s() at %s:%d\n",cnt++,fun,file,line);
    213:     PetscStrncmp(fun,"main",4,&ismain);
    214:     if (ismain) {
    215:       if ((n <= PETSC_ERR_MIN_VALUE) || (n >= PETSC_ERR_MAX_VALUE)) {
    216:         (*PetscErrorPrintf)("Reached the main program with an out-of-range error code %d. This should never happen\n",n);
    217:       }
    218:       PetscOptionsViewError();
    219:       PetscErrorPrintfHilight();
    220:       (*PetscErrorPrintf)("----------------End of Error Message -------send entire error message to petsc-maint@mcs.anl.gov----------\n");
    221:       PetscErrorPrintfNormal();
    222:     }
    223:   } else {
    224:     /* do not print error messages since process 0 will print them, sleep before aborting so will not accidentally kill process 0*/
    225:     PetscSleep(10.0);
    226:     exit(0);
    227:   }
    228:   return n;
    229: }