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 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;
  94     }
  95 
  96     result = (char*) malloc(len);
  97     if (result == NULL) {
  98         free(pMN);
  99         return NULL;
 100     }
 101 
 102     snprintf(result, len, format, pMN->classSig, pMN->methodName, location);
 103 
 104     free(pMN);
 105     return result;
 106 }
 107 
 108 void * getTLS(jvmtiEnv * pJvmtiEnv, jthread thread, jsize sizeToAllocate) {
 109     void * tls;
 110     if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetThreadLocalStorage(thread, &tls)))
 111         return NULL;
 112 
 113     if (!tls) {
 114         if (!NSK_VERIFY((tls = malloc(sizeToAllocate)) != NULL))
 115             return NULL;
 116 
 117         memset(tls, 0, sizeToAllocate);
 118 
 119         if (!NSK_JVMTI_VERIFY(pJvmtiEnv->SetThreadLocalStorage(thread, tls)))
 120             return NULL;
 121     }
 122 
 123     return tls;
 124 }
 125 
 126 }