31 #include "mlvmJvmtiUtils.h" 32 33 extern "C" { 34 35 void copyFromJString(JNIEnv * pEnv, jstring src, char ** dst) { 36 const char * pStr; 37 jsize len; 38 39 if (!NSK_VERIFY((pStr = pEnv->GetStringUTFChars(src, NULL)) != NULL)) { 40 return; 41 } 42 43 len = pEnv->GetStringUTFLength(src) + 1; 44 *dst = (char*) malloc(len); 45 strncpy(*dst, pStr, len); 46 47 pEnv->ReleaseStringUTFChars(src, pStr); 48 } 49 50 struct MethodName * getMethodName(jvmtiEnv * pJvmtiEnv, jmethodID method) { 51 char * szName; 52 char * szSignature; 53 jclass clazz; 54 struct MethodName * mn; 55 56 if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetMethodName(method, &szName, NULL, NULL))) { 57 return NULL; 58 } 59 60 if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetMethodDeclaringClass(method, &clazz))) { 61 NSK_JVMTI_VERIFY(pJvmtiEnv->Deallocate((unsigned char*) szName)); 62 return NULL; 63 } 64 65 if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetClassSignature(clazz, &szSignature, NULL))) { 66 NSK_JVMTI_VERIFY(pJvmtiEnv->Deallocate((unsigned char*) szName)); 67 return NULL; 68 } 69 70 mn = (MethodName*) malloc(sizeof(MethodNameStruct)); 71 strncpy(mn->methodName, szName, sizeof(mn->methodName)); 72 strncpy(mn->classSig, szSignature, sizeof(mn->classSig)); 73 74 NSK_JVMTI_VERIFY(pJvmtiEnv->Deallocate((unsigned char*) szName)); 75 NSK_JVMTI_VERIFY(pJvmtiEnv->Deallocate((unsigned char*) szSignature)); 76 return mn; 77 } 78 79 char * locationToString(jvmtiEnv * pJvmtiEnv, jmethodID method, jlocation location) { 80 struct MethodName * pMN; 81 int len; 82 char * result; 83 const char * const format = "%s .%s :" JLONG_FORMAT; 84 85 pMN = getMethodName(pJvmtiEnv, method); 86 if (!pMN) 87 return strdup("NONE"); 88 89 len = snprintf(NULL, 0, format, pMN->classSig, pMN->methodName, location) + 1; 90 91 if (len <= 0) { 92 free(pMN); 93 return NULL; | 31 #include "mlvmJvmtiUtils.h" 32 33 extern "C" { 34 35 void copyFromJString(JNIEnv * pEnv, jstring src, char ** dst) { 36 const char * pStr; 37 jsize len; 38 39 if (!NSK_VERIFY((pStr = pEnv->GetStringUTFChars(src, NULL)) != NULL)) { 40 return; 41 } 42 43 len = pEnv->GetStringUTFLength(src) + 1; 44 *dst = (char*) malloc(len); 45 strncpy(*dst, pStr, len); 46 47 pEnv->ReleaseStringUTFChars(src, pStr); 48 } 49 50 struct MethodName * getMethodName(jvmtiEnv * pJvmtiEnv, jmethodID method) { 51 char * szName = NULL; 52 char * szSignature = NULL; 53 jclass clazz; 54 struct MethodName * mn = NULL; 55 56 if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetMethodName(method, &szName, NULL, NULL))) { 57 goto out; 58 } 59 60 if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetMethodDeclaringClass(method, &clazz))) { 61 goto out; 62 } 63 64 if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetClassSignature(clazz, &szSignature, NULL))) { 65 goto out; 66 } 67 68 if (strlen(szName) + 1 > sizeof(mn->methodName) || 69 strlen(szSignature) + 1 > sizeof(mn->classSig)) { 70 goto out; 71 } 72 73 mn = (MethodName*) malloc(sizeof(MethodNameStruct)); 74 if (mn == NULL) { 75 goto out; 76 } 77 78 strncpy(mn->methodName, szName, sizeof(mn->methodName)); 79 strncpy(mn->classSig, szSignature, sizeof(mn->classSig)); 80 81 out: 82 NSK_JVMTI_VERIFY(pJvmtiEnv->Deallocate((unsigned char*) szName)); 83 NSK_JVMTI_VERIFY(pJvmtiEnv->Deallocate((unsigned char*) szSignature)); 84 return mn; 85 } 86 87 char * locationToString(jvmtiEnv * pJvmtiEnv, jmethodID method, jlocation location) { 88 struct MethodName * pMN; 89 int len; 90 char * result; 91 const char * const format = "%s .%s :" JLONG_FORMAT; 92 93 pMN = getMethodName(pJvmtiEnv, method); 94 if (!pMN) 95 return strdup("NONE"); 96 97 len = snprintf(NULL, 0, format, pMN->classSig, pMN->methodName, location) + 1; 98 99 if (len <= 0) { 100 free(pMN); 101 return NULL; |