Actual source code: checkptr.c

petsc-3.8.4 2018-03-24
Report Typos and Errors
  1:  #include <petsc/private/petscimpl.h>
  2:  #include <petscvalgrind.h>

  4: static PetscInt petsc_checkpointer_intensity = 1;

  6: /*@
  8:    confirm whether the address is valid.  An intensity of 0 never uses signal handlers, 1 uses them when not in a "hot"
  9:    function, and intensity of 2 always uses a signal handler.

 11:    Not Collective

 13:    Input Arguments:
 14: .  intensity - how much to check pointers for validity

 16:    Options Database Keys:
 17: .  -check_pointer_intensity - intensity (0, 1, or 2)

 19:    Level: advanced

 22: @*/
 24: {

 27:   switch (intensity) {
 28:   case 0:
 29:   case 1:
 30:   case 2:
 31:     petsc_checkpointer_intensity = intensity;
 32:     break;
 33:   default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Intensity %D not in 0,1,2",intensity);
 34:   }
 35:   return(0);
 36: }

 38: /* ---------------------------------------------------------------------------------------*/
 39: #if defined(PETSC_HAVE_SETJMP_H) && defined(PETSC_HAVE_SIGINFO_T)
 40: #include <signal.h>
 41: #include <setjmp.h>
 42: PETSC_INTERN jmp_buf PetscSegvJumpBuf;
 43: PETSC_INTERN void PetscSegv_sigaction(int, siginfo_t*, void *);

 45: /*@C

 48:    Not Collective

 50:    Input Parameters:
 51: +     ptr - the pointer
 52: -     dtype - the type of data the pointer is suppose to point to

 54:    Level: developer

 57: @*/
 59: {
 60:   struct sigaction sa,oldsa;

 62:   if (PETSC_RUNNING_ON_VALGRIND) return PETSC_TRUE;
 63:   if (!ptr) return PETSC_FALSE;
 64:   if (petsc_checkpointer_intensity < 1) return PETSC_TRUE;

 66:   /* Skip the verbose check if we are inside a hot function. */
 67:   if (petscstack && petscstack->hotdepth > 0 && petsc_checkpointer_intensity < 2) return PETSC_TRUE;

 69:   sigemptyset(&sa.sa_mask);
 70:   sa.sa_sigaction = PetscSegv_sigaction;
 71:   sa.sa_flags   = SA_SIGINFO;
 72:   sigaction(SIGSEGV, &sa, &oldsa);

 74:   if (setjmp(PetscSegvJumpBuf)) {
 75:     /* A segv was triggered in the code below hence we return with an error code */
 76:     sigaction(SIGSEGV, &oldsa, NULL);/* reset old signal hanlder */
 77:     return PETSC_FALSE;
 78:   } else {
 79:     switch (dtype) {
 80:     case PETSC_INT:{
 81:       PETSC_UNUSED PetscInt x = (PetscInt)*(volatile PetscInt*)ptr;
 82:       break;
 83:     }
 84: #if defined(PETSC_USE_COMPLEX)
 85:     case PETSC_SCALAR:{         /* C++ is seriously dysfunctional with volatile std::complex. */
 86:       PetscReal xreal = ((volatile PetscReal*)ptr)[0],ximag = ((volatile PetscReal*)ptr)[1];
 87:       PETSC_UNUSED volatile PetscScalar x = xreal + PETSC_i*ximag;
 88:       break;
 89:     }
 90: #endif
 91:     case PETSC_REAL:{
 92:       PETSC_UNUSED PetscReal x = *(volatile PetscReal*)ptr;
 93:       break;
 94:     }
 95:     case PETSC_BOOL:{
 96:       PETSC_UNUSED PetscBool x = *(volatile PetscBool*)ptr;
 97:       break;
 98:     }
 99:     case PETSC_ENUM:{
100:       PETSC_UNUSED PetscEnum x = *(volatile PetscEnum*)ptr;
101:       break;
102:     }
103:     case PETSC_CHAR:{
104:       PETSC_UNUSED char x = *(volatile char*)ptr;
105:       break;
106:     }
107:     case PETSC_OBJECT:{
108:       PETSC_UNUSED volatile PetscClassId classid = ((PetscObject)ptr)->classid;
109:       break;
110:     }
111:     default:;
112:     }
113:   }
114:   sigaction(SIGSEGV, &oldsa, NULL); /* reset old signal hanlder */
115:   return PETSC_TRUE;
116: }
117: #else
119: {
120:   if (!ptr) return PETSC_FALSE;
121:   return PETSC_TRUE;
122: }
123: #endif