Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/mlvm/share/mlvmJvmtiUtils.cpp
41155 views
/*1* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include <stdio.h>24#include <string.h>25#include <stdlib.h>26#include "jvmti.h"27#include "agent_common.h"28#include "JVMTITools.h"29#include "jvmti_tools.h"30#include "mlvmJvmtiUtils.h"3132extern "C" {3334void copyFromJString(JNIEnv * pEnv, jstring src, char ** dst) {35const char * pStr;36jsize len;3738if (!NSK_VERIFY((pStr = pEnv->GetStringUTFChars(src, NULL)) != NULL)) {39return;40}4142len = pEnv->GetStringUTFLength(src) + 1;43*dst = (char*) malloc(len);44strncpy(*dst, pStr, len);4546pEnv->ReleaseStringUTFChars(src, pStr);47}484950/**51* Helper class to track JVMTI resources, deallocating the resource in the destructor.52*/53class JvmtiResource {54private:55jvmtiEnv* const _jvmtiEnv;56void* const _ptr;5758public:59JvmtiResource(jvmtiEnv* jvmtiEnv, void* ptr) : _jvmtiEnv(jvmtiEnv), _ptr(ptr) { }6061~JvmtiResource() {62NSK_JVMTI_VERIFY(_jvmtiEnv->Deallocate((unsigned char*)_ptr));63}64};6566struct MethodName * getMethodName(jvmtiEnv * pJvmtiEnv, jmethodID method) {67char * szName;68char * szSignature;69jclass clazz;70struct MethodName * mn;7172if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetMethodName(method, &szName, NULL, NULL))) {73return NULL;74}7576JvmtiResource szNameResource(pJvmtiEnv, szName);7778if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetMethodDeclaringClass(method, &clazz))) {79return NULL;80}8182if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetClassSignature(clazz, &szSignature, NULL))) {83return NULL;84}8586JvmtiResource szSignatureResource(pJvmtiEnv, szSignature);8788if (strlen(szName) + 1 > sizeof(mn->methodName) ||89strlen(szSignature) + 1 > sizeof(mn->classSig)) {90return NULL;91}9293mn = (MethodName*) malloc(sizeof(MethodNameStruct));94if (mn == NULL) {95return NULL;96}9798strncpy(mn->methodName, szName, sizeof(mn->methodName) - 1);99mn->methodName[sizeof(mn->methodName) - 1] = '\0';100101strncpy(mn->classSig, szSignature, sizeof(mn->classSig) - 1);102mn->classSig[sizeof(mn->classSig) - 1] = '\0';103104return mn;105}106107char * locationToString(jvmtiEnv * pJvmtiEnv, jmethodID method, jlocation location) {108struct MethodName * pMN;109int len;110char * result;111const char * const format = "%s .%s :" JLONG_FORMAT;112113pMN = getMethodName(pJvmtiEnv, method);114if (!pMN)115return strdup("NONE");116117len = snprintf(NULL, 0, format, pMN->classSig, pMN->methodName, location) + 1;118119if (len <= 0) {120free(pMN);121return NULL;122}123124result = (char*) malloc(len);125if (result == NULL) {126free(pMN);127return NULL;128}129130snprintf(result, len, format, pMN->classSig, pMN->methodName, location);131132free(pMN);133return result;134}135136void * getTLS(jvmtiEnv * pJvmtiEnv, jthread thread, jsize sizeToAllocate) {137void * tls;138if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetThreadLocalStorage(thread, &tls)))139return NULL;140141if (!tls) {142if (!NSK_VERIFY((tls = malloc(sizeToAllocate)) != NULL))143return NULL;144145memset(tls, 0, sizeToAllocate);146147if (!NSK_JVMTI_VERIFY(pJvmtiEnv->SetThreadLocalStorage(thread, tls)))148return NULL;149}150151return tls;152}153154}155156157