1 /* 2 * Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 #include <stdio.h> 25 #include <string.h> 26 #include <stdlib.h> 27 #include "jvmti.h" 28 #include "agent_common.h" 29 #include "JVMTITools.h" 30 #include "jvmti_tools.h" 31 #include "mlvmJvmtiUtils.h" 32 33 void copyFromJString(JNIEnv * pEnv, jstring src, char ** dst) { 34 const char * pStr; 35 jsize len; 36 37 if ( ! NSK_VERIFY((pStr = NSK_CPP_STUB3(GetStringUTFChars, pEnv, src, NULL)) != NULL) ) { 38 return; 39 } 40 41 len = NSK_CPP_STUB2(GetStringUTFLength, pEnv, src) + 1; 42 *dst = malloc(len); 43 strncpy(*dst, pStr, len); 44 45 NSK_CPP_STUB3(ReleaseStringUTFChars, pEnv, src, pStr); 46 } 47 48 struct MethodName * getMethodName(jvmtiEnv * pJvmtiEnv, jmethodID method) { 49 char * szName; 50 char * szSignature; 51 jclass clazz; 52 struct MethodName * mn; 53 54 if ( ! NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName, pJvmtiEnv, method, &szName, NULL, NULL)) ) { 55 return NULL; 56 } 57 58 if ( ! NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetMethodDeclaringClass, pJvmtiEnv, method, &clazz)) ) { 59 NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, pJvmtiEnv, (void *) szName)); 60 return NULL; 61 } 62 63 if ( ! NSK_JVMTI_VERIFY(NSK_CPP_STUB4(GetClassSignature, pJvmtiEnv, clazz, &szSignature, NULL)) ) { 64 NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, pJvmtiEnv, (void *) szName)); 65 return NULL; 66 } 67 68 mn = malloc(sizeof(MethodNameStruct)); 69 strncpy(mn->methodName, szName, sizeof(mn->methodName)); 70 strncpy(mn->classSig, szSignature, sizeof(mn->classSig)); 71 72 NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, pJvmtiEnv, (void *) szName)); 73 NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, pJvmtiEnv, (void *) szSignature)); 74 return mn; 75 } 76 77 char * locationToString(jvmtiEnv * pJvmtiEnv, jmethodID method, jlocation location) { 78 struct MethodName * pMN; 79 // gcc 7.3 claims that snprintf below can output between 6 and 531 bytes. Setting buffer size to 600. 80 char r[600]; 81 82 pMN = getMethodName(pJvmtiEnv, method); 83 if ( ! pMN ) 84 return strdup("NONE"); 85 86 snprintf(r, sizeof(r), "%s .%s :%lx", pMN->classSig, pMN->methodName, (long) location); 87 88 free(pMN); 89 90 return strdup(r); 91 } 92 93 void * getTLS(jvmtiEnv * pJvmtiEnv, jthread thread, jsize sizeToAllocate) { 94 void * tls; 95 if ( ! NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetThreadLocalStorage, pJvmtiEnv, thread, &tls)) ) 96 return NULL; 97 98 if ( ! tls) { 99 if ( ! NSK_VERIFY((tls = malloc(sizeToAllocate)) != NULL) ) 100 return NULL; 101 102 memset(tls, 0, sizeToAllocate); 103 104 if ( ! NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetThreadLocalStorage, pJvmtiEnv, thread, tls)) ) 105 return NULL; 106 } 107 108 return tls; 109 }