forked from GoogleCloudPlatform/cloud-debug-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_type_evaluator.h
More file actions
243 lines (189 loc) · 7.28 KB
/
array_type_evaluator.h
File metadata and controls
243 lines (189 loc) · 7.28 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
/**
* 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.
*/
#ifndef DEVTOOLS_CDBG_DEBUGLETS_JAVA_ARRAY_TYPE_EVALUATOR_H_
#define DEVTOOLS_CDBG_DEBUGLETS_JAVA_ARRAY_TYPE_EVALUATOR_H_
#include "common.h"
#include "instance_field_reader.h"
#include "jni_utils.h"
#include "jvariant.h"
#include "type_evaluator.h"
namespace devtools {
namespace cdbg {
static constexpr char kArrayLengthName[] = "length";
class ClassMetadataReader;
struct NamedJVariant;
// Captures content of Java native array.
template <typename TArrayType>
class ArrayTypeEvaluator : public TypeEvaluator {
public:
ArrayTypeEvaluator()
:max_capture_expression_elements_(kMaxCaptureExpressionElements),
max_capture_local_object_elements_(kMaxCaptureObjectElements),
max_capture_local_primitive_elements_(kMaxCapturePrimitiveElements) {}
// This constructor is for testing only, it is never used in production code
ArrayTypeEvaluator(int max_capture_expression_elements,
int max_capture_local_object_elements,
int max_capture_local_primitive_elements)
:max_capture_expression_elements_(max_capture_expression_elements),
max_capture_local_object_elements_(max_capture_local_object_elements),
max_capture_local_primitive_elements_(max_capture_local_primitive_elements)
{}
~ArrayTypeEvaluator() override {}
string GetEvaluatorName() override;
// "method_caller" is not used.
void Evaluate(
MethodCaller* method_caller,
const ClassMetadataReader::Entry& class_metadata,
jobject obj,
bool is_watch_expression,
std::vector<NamedJVariant>* members) override;
private:
const int max_capture_expression_elements_;
const int max_capture_local_object_elements_;
const int max_capture_local_primitive_elements_;
DISALLOW_COPY_AND_ASSIGN(ArrayTypeEvaluator);
};
template <>
void ArrayTypeEvaluator<jobject>::Evaluate(
MethodCaller* method_caller,
const ClassMetadataReader::Entry& class_metadata,
jobject obj,
bool is_watch_expression,
std::vector<NamedJVariant>* members) {
DCHECK(IsArrayObjectType(class_metadata.signature));
WellKnownJClass element_well_known_jclass =
WellKnownJClassFromSignature(
GetArrayElementJSignature(class_metadata.signature));
// Evaluate the array.
const jsize array_len = jni()->GetArrayLength(static_cast<jarray>(obj));
const int count = is_watch_expression ?
std::min<int>(max_capture_expression_elements_, array_len) :
std::min<int>(max_capture_local_object_elements_, array_len);
const bool is_trimmed = count < array_len;
// We reserve 1st element for array length, and last element for
// status message of trimmed array. If array is not trimmed, we don't
// need the message.
*members = std::vector<NamedJVariant>(is_trimmed ? count + 2 : count + 1);
(*members)[0].name = kArrayLengthName;
(*members)[0].value = JVariant::Int(array_len);
for (int i = 0; i < count; ++i) {
JniLocalRef jitem(
jni()->GetObjectArrayElement(static_cast<jobjectArray>(obj), i));
(*members)[i + 1].name = FormatArrayIndexName(i);
(*members)[i + 1].value.assign_new_ref(
JVariant::ReferenceKind::Global,
jitem.get());
(*members)[i + 1].well_known_jclass = element_well_known_jclass;
}
// For trimmed array we reserved one extra space for status message
if (is_trimmed) {
(*members)[count + 1] = NamedJVariant::InfoStatus({
is_watch_expression ?
ExpressionCollectionNotAllItemsCaptured :
LocalCollectionNotAllItemsCaptured,
{ std::to_string(count) }
});
}
}
template <typename TArrayType>
void ArrayTypeEvaluator<TArrayType>::Evaluate(
MethodCaller* method_caller,
const ClassMetadataReader::Entry& class_metadata,
jobject obj,
bool is_watch_expression,
std::vector<NamedJVariant>* members) {
DCHECK(IsArrayObjectType(class_metadata.signature));
const jsize array_len = jni()->GetArrayLength(static_cast<jarray>(obj));
//
// Note: the function must not block or make any calls to JNI in
// between GetPrimitiveArrayCritical and ReleasePrimitiveArrayCritical.
//
if (array_len > 0) {
const TArrayType* array_data = static_cast<TArrayType*>(
jni()->GetPrimitiveArrayCritical(static_cast<jarray>(obj), nullptr));
if (array_data != nullptr) {
const int count = is_watch_expression ?
std::min<int>(max_capture_expression_elements_, array_len) :
std::min<int>(max_capture_local_primitive_elements_, array_len);
const bool is_trimmed = count < array_len;
// We reserve 1st element for array length, and last element for
// status message of trimmed array. If array is not trimmed, we don't
// need the message.
*members = std::vector<NamedJVariant>(is_trimmed ? count + 2 : count + 1);
for (int i = 0; i < count; ++i) {
(*members)[i + 1].name = FormatArrayIndexName(i);
(*members)[i + 1].value =
JVariant::Primitive<TArrayType>(array_data[i]);
}
// For trimmed array we reserved one extra space for status message
if (is_trimmed) {
(*members)[count + 1] = NamedJVariant::InfoStatus({
is_watch_expression ?
ExpressionCollectionNotAllItemsCaptured :
LocalCollectionNotAllItemsCaptured,
{ std::to_string(count) }
});
}
jni()->ReleasePrimitiveArrayCritical(
static_cast<jarray>(obj),
const_cast<TArrayType*>(array_data),
0);
}
} else {
*members = std::vector<NamedJVariant>(1);
}
(*members)[0].name = kArrayLengthName;
(*members)[0].value = JVariant::Int(array_len);
}
template <>
inline string ArrayTypeEvaluator<jboolean>::GetEvaluatorName() {
return "ArrayTypeEvaluator<jboolean>";
}
template <>
inline string ArrayTypeEvaluator<jchar>::GetEvaluatorName() {
return "ArrayTypeEvaluator<jchar>";
}
template <>
inline string ArrayTypeEvaluator<jbyte>::GetEvaluatorName() {
return "ArrayTypeEvaluator<jbyte>";
}
template <>
inline string ArrayTypeEvaluator<jshort>::GetEvaluatorName() {
return "ArrayTypeEvaluator<jshort>";
}
template <>
inline string ArrayTypeEvaluator<jint>::GetEvaluatorName() {
return "ArrayTypeEvaluator<jint>";
}
template <>
inline string ArrayTypeEvaluator<jlong>::GetEvaluatorName() {
return "ArrayTypeEvaluator<jlong>";
}
template <>
inline string ArrayTypeEvaluator<jfloat>::GetEvaluatorName() {
return "ArrayTypeEvaluator<jfloat>";
}
template <>
inline string ArrayTypeEvaluator<jdouble>::GetEvaluatorName() {
return "ArrayTypeEvaluator<jdouble>";
}
template <>
inline string ArrayTypeEvaluator<jobject>::GetEvaluatorName() {
return "ArrayTypeEvaluator<jobject>";
}
} // namespace cdbg
} // namespace devtools
#endif // DEVTOOLS_CDBG_DEBUGLETS_JAVA_ARRAY_TYPE_EVALUATOR_H_