Actual source code: pstack.c


  2: #include <petsc/private/petscimpl.h>

  4: #if PetscDefined(USE_DEBUG)
  5: PetscStack petscstack;
  6: #endif

  8: #if defined(PETSC_HAVE_SAWS)
  9: #include <petscviewersaws.h>

 11: static PetscBool amsmemstack = PETSC_FALSE;

 13: /*@C
 14:    PetscStackSAWsGrantAccess - Grants access of the PETSc stack frames to the SAWs publisher

 16:    Collective on PETSC_COMM_WORLD?

 18:    Level: developer


 22: .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess()

 24: @*/
 25: void  PetscStackSAWsGrantAccess(void)
 26: {
 27:   if (amsmemstack) {
 28:     /* ignore any errors from SAWs */
 29:     SAWs_Unlock();
 30:   }
 31: }

 33: /*@C
 34:    PetscStackSAWsTakeAccess - Takes access of the PETSc stack frames to the SAWs publisher

 36:    Collective on PETSC_COMM_WORLD?

 38:    Level: developer


 42: .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess()

 44: @*/
 45: void  PetscStackSAWsTakeAccess(void)
 46: {
 47:   if (amsmemstack) {
 48:     /* ignore any errors from SAWs */
 49:     SAWs_Lock();
 50:   }
 51: }

 53: PetscErrorCode PetscStackViewSAWs(void)
 54: {
 55:   PetscMPIInt    rank;

 57:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 58:   if (rank) return 0;
 59: #if PetscDefined(USE_DEBUG)
 60:   PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/functions",petscstack.function,20,SAWs_READ,SAWs_STRING));
 61:   PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/__current_size",&petscstack.currentsize,1,SAWs_READ,SAWs_INT));
 62: #endif
 63:   amsmemstack = PETSC_TRUE;
 64:   return 0;
 65: }

 67: PetscErrorCode PetscStackSAWsViewOff(void)
 68: {
 69:   if (!amsmemstack) return 0;
 70:   PetscStackCallSAWs(SAWs_Delete,("/PETSc/Stack"));
 71:   amsmemstack = PETSC_FALSE;
 72:   return 0;
 73: }
 74: #endif /* PETSC_HAVE_SAWS */

 76: #if PetscDefined(USE_DEBUG)
 77: PetscErrorCode PetscStackSetCheck(PetscBool check)
 78: {
 79:   petscstack.check = check;
 80:   return 0;
 81: }

 83: PetscErrorCode PetscStackReset(void)
 84: {
 85:   memset(&petscstack,0,sizeof(petscstack));
 86:   return 0;
 87: }

 89: PetscErrorCode  PetscStackView(FILE *file)
 90: {
 91:   if (!file) file = PETSC_STDOUT;
 92:   if (petscstack.currentsize < 0) {
 93:     /* < 0 is absolutely a corrupted stack, but this function is usually called in an error
 94:      * handler, which are not capable of recovering from errors so best we can do is print
 95:      * this warning */
 96:     fprintf(file,"PetscStack is definitely corrupted with stack size %d\n",petscstack.currentsize);
 97:   } else if (petscstack.currentsize == 0) {
 98:     if (file == PETSC_STDOUT) {
 99:       (*PetscErrorPrintf)("No error traceback is available, the problem could be in the main program. \n");
100:     } else {
101:       fprintf(file,"No error traceback is available, the problem could be in the main program. \n");
102:     }
103:   } else {
104:     if (file == PETSC_STDOUT) {
105:       (*PetscErrorPrintf)("The EXACT line numbers in the error traceback are not available.\n");
106:       (*PetscErrorPrintf)("instead the line number of the start of the function is given.\n");
107:       for (int i = petscstack.currentsize-1, j = 1; i >= 0; --i, ++j) {
108:         (*PetscErrorPrintf)("#%d %s() at %s:%d\n",j,petscstack.function[i],petscstack.file[i],petscstack.line[i]);
109:       }
110:     } else {
111:       fprintf(file,"The EXACT line numbers in the error traceback are not available.\n");
112:       fprintf(file,"Instead the line number of the start of the function is given.\n");
113:       for (int i = petscstack.currentsize-1, j = 1; i >= 0; --i, ++j) {
114:         fprintf(file,"[%d] #%d %s() at %s:%d\n",PetscGlobalRank,j,petscstack.function[i],petscstack.file[i],petscstack.line[i]);
115:       }
116:     }
117:   }
118:   return 0;
119: }

121: PetscErrorCode  PetscStackCopy(PetscStack *sint,PetscStack *sout)
122: {
123:   if (sint) {
124:     for (int i = 0; i < sint->currentsize; ++i) {
125:       sout->function[i]     = sint->function[i];
126:       sout->file[i]         = sint->file[i];
127:       sout->line[i]         = sint->line[i];
128:       sout->petscroutine[i] = sint->petscroutine[i];
129:     }
130:     sout->currentsize = sint->currentsize;
131:   } else {
132:     sout->currentsize = 0;
133:   }
134:   return 0;
135: }

137: PetscErrorCode  PetscStackPrint(PetscStack *sint,FILE *fp)
138: {
139:   if (sint) {
140:     for (int i = sint->currentsize-2; i >= 0; --i) {
141:       fprintf(fp,"      [%d]  %s() at %s:%d\n",PetscGlobalRank,sint->function[i],sint->file[i],sint->line[i]);
142:     }
143:   }
144:   return 0;
145: }
146: #endif /* PetscDefined(USE_DEBUG) */