1d9262e54SJed Brown 227104ee2SJacob Faibussowitsch #include <petsc/private/petscimpl.h> /*I "petscsys.h" I*/ 3d9262e54SJed Brown 427104ee2SJacob Faibussowitsch #if PetscDefined(USE_DEBUG) 527104ee2SJacob Faibussowitsch PetscStack petscstack; 627104ee2SJacob Faibussowitsch #endif 715681b3cSBarry Smith 8e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 9e04113cfSBarry Smith #include <petscviewersaws.h> 1015681b3cSBarry Smith 112657e9d9SBarry Smith static PetscBool amsmemstack = PETSC_FALSE; 1215681b3cSBarry Smith 1315681b3cSBarry Smith /*@C 14e04113cfSBarry Smith PetscStackSAWsGrantAccess - Grants access of the PETSc stack frames to the SAWs publisher 1515681b3cSBarry Smith 1615681b3cSBarry Smith Collective on PETSC_COMM_WORLD? 1715681b3cSBarry Smith 1815681b3cSBarry Smith Level: developer 1915681b3cSBarry Smith 20e04113cfSBarry Smith Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallSAWs() since it may be used within those routines 2115681b3cSBarry Smith 22db781477SPatrick Sanan .seealso: `PetscObjectSetName()`, `PetscObjectSAWsViewOff()`, `PetscObjectSAWsTakeAccess()` 2315681b3cSBarry Smith 2415681b3cSBarry Smith @*/ 25e04113cfSBarry Smith void PetscStackSAWsGrantAccess(void) 26d9262e54SJed Brown { 27ec957eceSBarry Smith if (amsmemstack) { 2816ad0300SBarry Smith /* ignore any errors from SAWs */ 299a492a5cSBarry Smith SAWs_Unlock(); 3015681b3cSBarry Smith } 31d9262e54SJed Brown } 32d9262e54SJed Brown 3315681b3cSBarry Smith /*@C 34*586f9135SBarry Smith PetscStackSAWsTakeAccess - Takes access of the PETSc stack frames from the SAWs publisher 3515681b3cSBarry Smith 3615681b3cSBarry Smith Collective on PETSC_COMM_WORLD? 3715681b3cSBarry Smith 3815681b3cSBarry Smith Level: developer 3915681b3cSBarry Smith 40e04113cfSBarry Smith Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallSAWs() since it may be used within those routines 4115681b3cSBarry Smith 42db781477SPatrick Sanan .seealso: `PetscObjectSetName()`, `PetscObjectSAWsViewOff()`, `PetscObjectSAWsTakeAccess()` 4315681b3cSBarry Smith 4415681b3cSBarry Smith @*/ 45e04113cfSBarry Smith void PetscStackSAWsTakeAccess(void) 46d9262e54SJed Brown { 47ec957eceSBarry Smith if (amsmemstack) { 4816ad0300SBarry Smith /* ignore any errors from SAWs */ 499a492a5cSBarry Smith SAWs_Lock(); 5015681b3cSBarry Smith } 5115681b3cSBarry Smith } 5215681b3cSBarry Smith 53e04113cfSBarry Smith PetscErrorCode PetscStackViewSAWs(void) 5415681b3cSBarry Smith { 55d45a07a7SBarry Smith PetscMPIInt rank; 5615681b3cSBarry Smith 579566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 58d45a07a7SBarry Smith if (rank) return 0; 5927104ee2SJacob Faibussowitsch #if PetscDefined(USE_DEBUG) 6027104ee2SJacob Faibussowitsch PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/functions",petscstack.function,20,SAWs_READ,SAWs_STRING)); 6127104ee2SJacob Faibussowitsch PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/__current_size",&petscstack.currentsize,1,SAWs_READ,SAWs_INT)); 6227104ee2SJacob Faibussowitsch #endif 632657e9d9SBarry Smith amsmemstack = PETSC_TRUE; 6415681b3cSBarry Smith return 0; 6515681b3cSBarry Smith } 6615681b3cSBarry Smith 67e04113cfSBarry Smith PetscErrorCode PetscStackSAWsViewOff(void) 6815681b3cSBarry Smith { 69d9262e54SJed Brown PetscFunctionBegin; 70d45a07a7SBarry Smith if (!amsmemstack) PetscFunctionReturn(0); 7116ad0300SBarry Smith PetscStackCallSAWs(SAWs_Delete,("/PETSc/Stack")); 722657e9d9SBarry Smith amsmemstack = PETSC_FALSE; 73d9262e54SJed Brown PetscFunctionReturn(0); 74d9262e54SJed Brown } 7527104ee2SJacob Faibussowitsch #endif /* PETSC_HAVE_SAWS */ 76d9262e54SJed Brown 7727104ee2SJacob Faibussowitsch #if PetscDefined(USE_DEBUG) 7827104ee2SJacob Faibussowitsch PetscErrorCode PetscStackSetCheck(PetscBool check) 7974b43855SShri Abhyankar { 8027104ee2SJacob Faibussowitsch petscstack.check = check; 8127104ee2SJacob Faibussowitsch return 0; 827fdeb8b9SBarry Smith } 8315681b3cSBarry Smith 8427104ee2SJacob Faibussowitsch PetscErrorCode PetscStackReset(void) 8515681b3cSBarry Smith { 8627104ee2SJacob Faibussowitsch memset(&petscstack,0,sizeof(petscstack)); 8774b43855SShri Abhyankar return 0; 8874b43855SShri Abhyankar } 8974b43855SShri Abhyankar 90*586f9135SBarry Smith /*@C 91*586f9135SBarry Smith PetscStackView - Print the current (default) PETSc stack to an ASCII file 92*586f9135SBarry Smith 93*586f9135SBarry Smith Not Collective 94*586f9135SBarry Smith 95*586f9135SBarry Smith Input Parameter: 96*586f9135SBarry Smith . file - the file pointer, or `NULL` to use `PETSC_STDOUT` 97*586f9135SBarry Smith 98*586f9135SBarry Smith Level: developer 99*586f9135SBarry Smith 100*586f9135SBarry Smith Notes: 101*586f9135SBarry Smith In debug mode PETSc maintains a stack of the current function calls that can be used to help to quickly see where a problem has 102*586f9135SBarry Smith occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to 103*586f9135SBarry Smith help debug the problem. 104*586f9135SBarry Smith 105*586f9135SBarry Smith The default stack is a global variable called `petscstack`. 106*586f9135SBarry Smith 107*586f9135SBarry Smith .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackPrint()`, `PetscStackSAWsGrantAccess()`, `PetscStackSAWsTakeAccess()` 108*586f9135SBarry Smith @*/ 109639ff905SBarry Smith PetscErrorCode PetscStackView(FILE *file) 110d9262e54SJed Brown { 111639ff905SBarry Smith if (!file) file = PETSC_STDOUT; 11227104ee2SJacob Faibussowitsch if (petscstack.currentsize < 0) { 11327104ee2SJacob Faibussowitsch /* < 0 is absolutely a corrupted stack, but this function is usually called in an error 11427104ee2SJacob Faibussowitsch * handler, which are not capable of recovering from errors so best we can do is print 11527104ee2SJacob Faibussowitsch * this warning */ 11627104ee2SJacob Faibussowitsch fprintf(file,"PetscStack is definitely corrupted with stack size %d\n",petscstack.currentsize); 11727104ee2SJacob Faibussowitsch } else if (petscstack.currentsize == 0) { 118d9262e54SJed Brown if (file == PETSC_STDOUT) { 1197a746cd8SPierre Jolivet (*PetscErrorPrintf)("No error traceback is available, the problem could be in the main program. \n"); 1206d07e311SBarry Smith } else { 1217a746cd8SPierre Jolivet fprintf(file,"No error traceback is available, the problem could be in the main program. \n"); 1226d07e311SBarry Smith } 1236d07e311SBarry Smith } else { 1246d07e311SBarry Smith if (file == PETSC_STDOUT) { 1256d07e311SBarry Smith (*PetscErrorPrintf)("The EXACT line numbers in the error traceback are not available.\n"); 1266d07e311SBarry Smith (*PetscErrorPrintf)("instead the line number of the start of the function is given.\n"); 12727104ee2SJacob Faibussowitsch for (int i = petscstack.currentsize-1, j = 1; i >= 0; --i, ++j) { 12827104ee2SJacob Faibussowitsch (*PetscErrorPrintf)("#%d %s() at %s:%d\n",j,petscstack.function[i],petscstack.file[i],petscstack.line[i]); 12927104ee2SJacob Faibussowitsch } 130d9262e54SJed Brown } else { 1316d07e311SBarry Smith fprintf(file,"The EXACT line numbers in the error traceback are not available.\n"); 1326d07e311SBarry Smith fprintf(file,"Instead the line number of the start of the function is given.\n"); 13327104ee2SJacob Faibussowitsch for (int i = petscstack.currentsize-1, j = 1; i >= 0; --i, ++j) { 13427104ee2SJacob Faibussowitsch fprintf(file,"[%d] #%d %s() at %s:%d\n",PetscGlobalRank,j,petscstack.function[i],petscstack.file[i],petscstack.line[i]); 135d9262e54SJed Brown } 1366d07e311SBarry Smith } 1377d5f7e0cSShri Abhyankar } 138d9262e54SJed Brown return 0; 139d9262e54SJed Brown } 140d9262e54SJed Brown 141*586f9135SBarry Smith /*@C 142*586f9135SBarry Smith PetscStackCopy - Copy the information from one PETSc stack to another 143*586f9135SBarry Smith 144*586f9135SBarry Smith Not Collective 145*586f9135SBarry Smith 146*586f9135SBarry Smith Input Parameter: 147*586f9135SBarry Smith . sint - the stack to be copied from 148*586f9135SBarry Smith 149*586f9135SBarry Smith Output Parameter: 150*586f9135SBarry Smith . sout - the stack to be copied to, this stack must already exist 151*586f9135SBarry Smith 152*586f9135SBarry Smith Level: developer 153*586f9135SBarry Smith 154*586f9135SBarry Smith Notes: 155*586f9135SBarry Smith In debug mode PETSc maintains a stack of the current function calls that can be used to help to quickly see where a problem has 156*586f9135SBarry Smith occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to 157*586f9135SBarry Smith help debug the problem. 158*586f9135SBarry Smith 159*586f9135SBarry Smith .seealso: `PetscAttachDebugger()`, `PetscStackView()` 160*586f9135SBarry Smith @*/ 1617087cfbeSBarry Smith PetscErrorCode PetscStackCopy(PetscStack *sint,PetscStack *sout) 162d9262e54SJed Brown { 16327104ee2SJacob Faibussowitsch if (sint) { 16427104ee2SJacob Faibussowitsch for (int i = 0; i < sint->currentsize; ++i) { 165d9262e54SJed Brown sout->function[i] = sint->function[i]; 166d9262e54SJed Brown sout->file[i] = sint->file[i]; 167d9262e54SJed Brown sout->line[i] = sint->line[i]; 168a8d2bbe5SBarry Smith sout->petscroutine[i] = sint->petscroutine[i]; 169d9262e54SJed Brown } 170d9262e54SJed Brown sout->currentsize = sint->currentsize; 17127104ee2SJacob Faibussowitsch } else { 17227104ee2SJacob Faibussowitsch sout->currentsize = 0; 173d9262e54SJed Brown } 174d9262e54SJed Brown return 0; 175d9262e54SJed Brown } 176d9262e54SJed Brown 177*586f9135SBarry Smith /*@C 178*586f9135SBarry Smith PetscStackPrint - Prints a given PETSc stack to an ASCII file 179*586f9135SBarry Smith 180*586f9135SBarry Smith Not Collective 181*586f9135SBarry Smith 182*586f9135SBarry Smith Input Parameters: 183*586f9135SBarry Smith + sint - the PETSc stack to print 184*586f9135SBarry Smith - file - the file pointer 185*586f9135SBarry Smith 186*586f9135SBarry Smith Level: developer 187*586f9135SBarry Smith 188*586f9135SBarry Smith Notes: 189*586f9135SBarry Smith In debug mode PETSc maintains a stack of the current function calls that can be used to help to quickly see where a problem has 190*586f9135SBarry Smith occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to 191*586f9135SBarry Smith help debug the problem. 192*586f9135SBarry Smith 193*586f9135SBarry Smith The default stack is a global variable called `petscstack`. 194*586f9135SBarry Smith 195*586f9135SBarry Smith Developer Note: 196*586f9135SBarry Smith `PetscStackPrint()` and `PetscStackView()` should be merged into a single API. 197*586f9135SBarry Smith 198*586f9135SBarry Smith .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()` 199*586f9135SBarry Smith @*/ 2007087cfbeSBarry Smith PetscErrorCode PetscStackPrint(PetscStack *sint,FILE *fp) 201d9262e54SJed Brown { 20227104ee2SJacob Faibussowitsch if (sint) { 20327104ee2SJacob Faibussowitsch for (int i = sint->currentsize-2; i >= 0; --i) { 20427104ee2SJacob Faibussowitsch fprintf(fp," [%d] %s() at %s:%d\n",PetscGlobalRank,sint->function[i],sint->file[i],sint->line[i]); 20527104ee2SJacob Faibussowitsch } 20627104ee2SJacob Faibussowitsch } 207d9262e54SJed Brown return 0; 208d9262e54SJed Brown } 20927104ee2SJacob Faibussowitsch #endif /* PetscDefined(USE_DEBUG) */ 210