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