Skip to content
Navigation Menu
{{ message }}
forked from GoogleCloudPlatform/cloud-debug-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjvm_class_metadata_reader.cc
More file actions
361 lines (302 loc) · 10.2 KB
/
Copy pathjvm_class_metadata_reader.cc
File metadata and controls
361 lines (302 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jvm_class_metadata_reader.h"
#include <algorithm>
#include "jni_utils.h"
#include "jvm_instance_field_reader.h"
#include "jvm_static_field_reader.h"
#include "jvmti_buffer.h"
#include "jni_proxy_object.h"
namespace devtools {
namespace cdbg {
static void ReleaseEntryRefs(jobject cls, ClassMetadataReader::Entry* entry) {
for (auto& static_field_reader : entry->static_fields) {
static_field_reader->ReleaseRef();
}
}
JvmClassMetadataReader::JvmClassMetadataReader(
MemberVisibilityPolicy* member_visibility_policy)
: member_visibility_policy_(member_visibility_policy),
cls_cache_(ReleaseEntryRefs) {
}
JvmClassMetadataReader::~JvmClassMetadataReader() {
}
const ClassMetadataReader::Entry&
JvmClassMetadataReader::GetClassMetadata(jclass cls) {
// Safeguard against null references.
DCHECK(cls != nullptr);
if (cls == nullptr) {
static Entry empty_class_metadata;
return empty_class_metadata;
}
// Case 1: the class information is cached.
{
MutexLock reader_lock(&mu_);
const Entry* metadata = cls_cache_.Find(cls);
if (metadata != nullptr) {
// It is safe to return reference to "jobject_map" entry. "jobject_map"
// guarantees that the elements are not moved when data structure is
// expanded or contracted.
return *metadata;
}
}
// Case 2: we need to load the class information.
{
Entry metadata;
LoadClassMetadata(cls, &metadata);
{
MutexLock writer_lock(&mu_);
cls_cache_.Insert(cls, std::move(metadata));
// Return currently or previously inserted entry. Returning reference to
// "jobject_map" entry as in case 1.
return *cls_cache_.Find(cls);
}
}
}
void JvmClassMetadataReader::LoadClassMetadata(jclass cls, Entry* metadata) {
string signature = GetClassSignature(cls);
if (signature.empty()) {
return;
}
metadata->signature = JSignatureFromSignature(signature);
// Start from the current class and go down the inheritance chain.
JniLocalRef current_class_ref = JniNewLocalRef(cls);
// Maintain set of methods we discovered in superclasses to ignore those
// in base classes. The pair stores method name and signature.
std::set<std::pair<string, string>> registered_methods;
while (current_class_ref != nullptr) {
LoadSingleClassMetadata(
static_cast<jclass>(current_class_ref.get()),
®istered_methods,
metadata);
LoadImplementedInterfacesMetadata(
static_cast<jclass>(current_class_ref.get()),
®istered_methods,
metadata);
// Free the current local reference and allocate a new local reference
// corresponding to the superclass of "current_class_ref".
JniLocalRef next(
jni()->GetSuperclass(static_cast<jclass>(current_class_ref.get())));
// Don't skip "java.lang.Object" even if "cls" is an interface.
if ((next == nullptr) &&
!jni()->IsSameObject(current_class_ref.get(),
jniproxy::Object()->GetClass())) {
LoadSingleClassMetadata(
jniproxy::Object()->GetClass(),
®istered_methods,
metadata);
}
current_class_ref = std::move(next);
}
// Reverse the the lists to accomodate for LoadFieldInfo appending new
// elements to the end of the list.
std::reverse(
metadata->instance_fields.begin(),
metadata->instance_fields.end());
std::reverse(
metadata->static_fields.begin(),
metadata->static_fields.end());
}
void JvmClassMetadataReader::LoadImplementedInterfacesMetadata(
jclass parent,
std::set<std::pair<string, string>>* registered_methods,
Entry* metadata) {
jvmtiError err = JVMTI_ERROR_NONE;
jint count = 0;
JvmtiBuffer<jclass> interfaces;
err = jvmti()->GetImplementedInterfaces(parent, &count, interfaces.ref());
if (err != JVMTI_ERROR_NONE) {
LOG(ERROR) << "GetImplementedInterfaces failed, error: " << err;
return;
}
for (int i = 0; i < count; ++i) {
jclass interface = interfaces.get()[i];
LoadSingleClassMetadata(interface, registered_methods, metadata);
LoadImplementedInterfacesMetadata(interface, registered_methods, metadata);
}
}
void JvmClassMetadataReader::LoadSingleClassMetadata(
jclass cls,
std::set<std::pair<string, string>>* registered_methods,
Entry* metadata) {
jvmtiError err = JVMTI_ERROR_NONE;
string class_signature = GetClassSignature(cls);
if (class_signature.empty()) {
return;
}
// Load list of all the fields of the class. This includes both member
// and static fields.
jint cls_fields_count = 0;
JvmtiBuffer<jfieldID> cls_fields;
err = jvmti()->GetClassFields(cls, &cls_fields_count, cls_fields.ref());
if (err != JVMTI_ERROR_NONE) {
LOG(ERROR) << "GetClassFields failed, error: " << err;
} else {
// Walk fields in reverse order so that the fields from the base class
// show up before the fields from the subclass.
for (int i = cls_fields_count - 1; i >= 0; --i) {
LoadFieldInfo(
cls,
class_signature,
cls_fields.get()[i],
metadata);
}
}
// Load list of all the methods of the class.
jint methods_count = 0;
JvmtiBuffer<jmethodID> methods;
err = jvmti()->GetClassMethods(
cls,
&methods_count,
methods.ref());
if (err != JVMTI_ERROR_NONE) {
LOG(ERROR) << "GetClassMethods failed, error: " << err;
} else {
// Load metadata of each method. We don't care about the order of methods
// in the list.
for (int i = 0; i < methods_count; ++i) {
Method method_metadata = LoadMethodInfo(
cls,
class_signature,
methods.get()[i]);
if (method_metadata.name.empty()) {
continue;
}
// If two instance methods have the same arguments, the one in the
// superclass overrides the one in the base class. This will be true
// even if return types are difference (this is called covariance).
std::pair<string, string> key;
if (method_metadata.is_static()) {
key = {
method_metadata.name,
method_metadata.signature
};
} else {
key = {
method_metadata.name,
TrimReturnType(method_metadata.signature)
};
}
// Skip base class methods that the inherited classes overwrote.
if (registered_methods->find(key) != registered_methods->end()) {
continue;
}
// Add the method to the registry.
registered_methods->insert(std::move(key));
metadata->methods.push_back(std::move(method_metadata));
}
}
}
void JvmClassMetadataReader::LoadFieldInfo(
jclass cls,
const string& class_signature,
jfieldID field_id,
Entry* metadata) {
int err = JVMTI_ERROR_NONE;
jint field_modifiers = 0;
err = jvmti()->GetFieldModifiers(cls, field_id, &field_modifiers);
if (err != JVMTI_ERROR_NONE) {
LOG(ERROR) << "GetFieldModifiers failed, error: " << err;
return;
}
JvmtiBuffer<char> field_name_buffer;
JvmtiBuffer<char> field_signature_buffer;
err = jvmti()->GetFieldName(
cls,
field_id,
field_name_buffer.ref(),
field_signature_buffer.ref(),
nullptr);
if (err != JVMTI_ERROR_NONE) {
LOG(ERROR) << "GetFieldName failed, error: " << err;
return;
}
if ((field_signature_buffer.get() == nullptr) ||
(field_signature_buffer.get()[0] == '\0')) {
LOG(ERROR) << "Empty field signature is unexpected";
return;
}
string field_name = field_name_buffer.get();
string field_signature = field_signature_buffer.get();
if (!member_visibility_policy_->IsFieldDebuggerVisible(
cls,
class_signature,
field_modifiers,
field_name,
field_signature)) {
if ((field_modifiers & JVM_ACC_STATIC) == 0) {
metadata->instance_fields_omitted = true;
}
return; // Field is invisible.
}
if ((field_modifiers & JVM_ACC_STATIC) == 0) {
// Instance field.
std::unique_ptr<InstanceFieldReader> reader(
new JvmInstanceFieldReader(
field_name,
field_id,
JSignatureFromSignature(field_signature)));
metadata->instance_fields.push_back(std::move(reader));
} else {
// Static field.
std::unique_ptr<StaticFieldReader> reader(
new JvmStaticFieldReader(
cls,
field_name,
field_id,
JSignatureFromSignature(field_signature)));
metadata->static_fields.push_back(std::move(reader));
}
}
JvmClassMetadataReader::Method JvmClassMetadataReader::LoadMethodInfo(
jclass cls,
const string& class_signature,
jmethodID method_id) {
int err = JVMTI_ERROR_NONE;
JvmtiBuffer<char> method_name;
JvmtiBuffer<char> method_signature;
err = jvmti()->GetMethodName(
method_id,
method_name.ref(),
method_signature.ref(),
nullptr);
if (err != JVMTI_ERROR_NONE) {
LOG(ERROR) << "GetMethodName failed, error: " << err;
return Method();
}
jint method_modifiers = 0;
err = jvmti()->GetMethodModifiers(method_id, &method_modifiers);
if (err != JVMTI_ERROR_NONE) {
LOG(ERROR) << "GetMethodModifiers failed, error: " << err;
return Method();
}
Method method;
method.class_signature = JSignatureFromSignature(class_signature);
method.name = method_name.get();
method.signature = method_signature.get();
method.modifiers = method_modifiers;
if (!member_visibility_policy_->IsMethodDebuggerVisible(
cls,
class_signature,
method_modifiers,
method.name,
method.signature)) {
return Method(); // Method is invisible.
}
return method;
}
} // namespace cdbg
} // namespace devtools
You can’t perform that action at this time.
