Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/mlvm/share/mlvmJvmtiUtils.cpp
41155 views
1
/*
2
* Copyright (c) 2010, 2020, 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
51
/**
52
* Helper class to track JVMTI resources, deallocating the resource in the destructor.
53
*/
54
class JvmtiResource {
55
private:
56
jvmtiEnv* const _jvmtiEnv;
57
void* const _ptr;
58
59
public:
60
JvmtiResource(jvmtiEnv* jvmtiEnv, void* ptr) : _jvmtiEnv(jvmtiEnv), _ptr(ptr) { }
61
62
~JvmtiResource() {
63
NSK_JVMTI_VERIFY(_jvmtiEnv->Deallocate((unsigned char*)_ptr));
64
}
65
};
66
67
struct MethodName * getMethodName(jvmtiEnv * pJvmtiEnv, jmethodID method) {
68
char * szName;
69
char * szSignature;
70
jclass clazz;
71
struct MethodName * mn;
72
73
if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetMethodName(method, &szName, NULL, NULL))) {
74
return NULL;
75
}
76
77
JvmtiResource szNameResource(pJvmtiEnv, szName);
78
79
if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetMethodDeclaringClass(method, &clazz))) {
80
return NULL;
81
}
82
83
if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetClassSignature(clazz, &szSignature, NULL))) {
84
return NULL;
85
}
86
87
JvmtiResource szSignatureResource(pJvmtiEnv, szSignature);
88
89
if (strlen(szName) + 1 > sizeof(mn->methodName) ||
90
strlen(szSignature) + 1 > sizeof(mn->classSig)) {
91
return NULL;
92
}
93
94
mn = (MethodName*) malloc(sizeof(MethodNameStruct));
95
if (mn == NULL) {
96
return NULL;
97
}
98
99
strncpy(mn->methodName, szName, sizeof(mn->methodName) - 1);
100
mn->methodName[sizeof(mn->methodName) - 1] = '\0';
101
102
strncpy(mn->classSig, szSignature, sizeof(mn->classSig) - 1);
103
mn->classSig[sizeof(mn->classSig) - 1] = '\0';
104
105
return mn;
106
}
107
108
char * locationToString(jvmtiEnv * pJvmtiEnv, jmethodID method, jlocation location) {
109
struct MethodName * pMN;
110
int len;
111
char * result;
112
const char * const format = "%s .%s :" JLONG_FORMAT;
113
114
pMN = getMethodName(pJvmtiEnv, method);
115
if (!pMN)
116
return strdup("NONE");
117
118
len = snprintf(NULL, 0, format, pMN->classSig, pMN->methodName, location) + 1;
119
120
if (len <= 0) {
121
free(pMN);
122
return NULL;
123
}
124
125
result = (char*) malloc(len);
126
if (result == NULL) {
127
free(pMN);
128
return NULL;
129
}
130
131
snprintf(result, len, format, pMN->classSig, pMN->methodName, location);
132
133
free(pMN);
134
return result;
135
}
136
137
void * getTLS(jvmtiEnv * pJvmtiEnv, jthread thread, jsize sizeToAllocate) {
138
void * tls;
139
if (!NSK_JVMTI_VERIFY(pJvmtiEnv->GetThreadLocalStorage(thread, &tls)))
140
return NULL;
141
142
if (!tls) {
143
if (!NSK_VERIFY((tls = malloc(sizeToAllocate)) != NULL))
144
return NULL;
145
146
memset(tls, 0, sizeToAllocate);
147
148
if (!NSK_JVMTI_VERIFY(pJvmtiEnv->SetThreadLocalStorage(thread, tls)))
149
return NULL;
150
}
151
152
return tls;
153
}
154
155
}
156
157