blob: 871902e19fdf3c8e2c05b937af508896a1c1ba41 [file] [log] [blame]
Andreas Gampe8da6d032016-10-31 19:31:03 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "heaps.h"
18
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070019#include <inttypes.h>
Andreas Gampe8da6d032016-10-31 19:31:03 -070020#include <stdio.h>
21#include <string.h>
22
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070023#include <vector>
24
25#include "base/logging.h"
Andreas Gampe8da6d032016-10-31 19:31:03 -070026#include "base/macros.h"
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070027#include "base/stringprintf.h"
Andreas Gampe5d139fc2016-11-09 22:54:25 -080028#include "jit/jit.h"
Andreas Gampe8da6d032016-10-31 19:31:03 -070029#include "jni.h"
Andreas Gampe216090d2016-11-15 12:33:18 -080030#include "native_stack_dump.h"
Andreas Gampe8da6d032016-10-31 19:31:03 -070031#include "openjdkjvmti/jvmti.h"
Andreas Gampe5d139fc2016-11-09 22:54:25 -080032#include "runtime.h"
33#include "thread-inl.h"
Andreas Gampe216090d2016-11-15 12:33:18 -080034#include "thread_list.h"
Alex Lighte6574242016-08-17 09:56:24 -070035
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070036#include "ti-agent/common_helper.h"
Andreas Gampe8da6d032016-10-31 19:31:03 -070037#include "ti-agent/common_load.h"
38
39namespace art {
40namespace Test913Heaps {
41
42extern "C" JNIEXPORT void JNICALL Java_Main_forceGarbageCollection(JNIEnv* env ATTRIBUTE_UNUSED,
43 jclass klass ATTRIBUTE_UNUSED) {
44 jvmtiError ret = jvmti_env->ForceGarbageCollection();
45 if (ret != JVMTI_ERROR_NONE) {
46 char* err;
47 jvmti_env->GetErrorName(ret, &err);
48 printf("Error forcing a garbage collection: %s\n", err);
49 }
50}
51
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070052class IterationConfig {
53 public:
54 IterationConfig() {}
55 virtual ~IterationConfig() {}
56
57 virtual jint Handle(jvmtiHeapReferenceKind reference_kind,
58 const jvmtiHeapReferenceInfo* reference_info,
59 jlong class_tag,
60 jlong referrer_class_tag,
61 jlong size,
62 jlong* tag_ptr,
63 jlong* referrer_tag_ptr,
64 jint length,
65 void* user_data) = 0;
66};
67
68static jint JNICALL HeapReferenceCallback(jvmtiHeapReferenceKind reference_kind,
69 const jvmtiHeapReferenceInfo* reference_info,
70 jlong class_tag,
71 jlong referrer_class_tag,
72 jlong size,
73 jlong* tag_ptr,
74 jlong* referrer_tag_ptr,
75 jint length,
76 void* user_data) {
77 IterationConfig* config = reinterpret_cast<IterationConfig*>(user_data);
78 return config->Handle(reference_kind,
79 reference_info,
80 class_tag,
81 referrer_class_tag,
82 size,
83 tag_ptr,
84 referrer_tag_ptr,
85 length,
86 user_data);
87}
88
89static bool Run(jint heap_filter,
90 jclass klass_filter,
91 jobject initial_object,
92 IterationConfig* config) {
93 jvmtiHeapCallbacks callbacks;
94 memset(&callbacks, 0, sizeof(jvmtiHeapCallbacks));
95 callbacks.heap_reference_callback = HeapReferenceCallback;
96
97 jvmtiError ret = jvmti_env->FollowReferences(heap_filter,
98 klass_filter,
99 initial_object,
100 &callbacks,
101 config);
102 if (ret != JVMTI_ERROR_NONE) {
103 char* err;
104 jvmti_env->GetErrorName(ret, &err);
105 printf("Failure running FollowReferences: %s\n", err);
106 return false;
107 }
108 return true;
109}
110
111extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_followReferences(JNIEnv* env,
112 jclass klass ATTRIBUTE_UNUSED,
113 jint heap_filter,
114 jclass klass_filter,
115 jobject initial_object,
116 jint stop_after,
117 jint follow_set,
118 jobject jniRef) {
119 class PrintIterationConfig FINAL : public IterationConfig {
120 public:
121 PrintIterationConfig(jint _stop_after, jint _follow_set)
122 : counter_(0),
123 stop_after_(_stop_after),
124 follow_set_(_follow_set) {
125 }
126
127 jint Handle(jvmtiHeapReferenceKind reference_kind,
128 const jvmtiHeapReferenceInfo* reference_info,
129 jlong class_tag,
130 jlong referrer_class_tag,
131 jlong size,
132 jlong* tag_ptr,
133 jlong* referrer_tag_ptr,
134 jint length,
135 void* user_data ATTRIBUTE_UNUSED) OVERRIDE {
136 jlong tag = *tag_ptr;
137 // Only check tagged objects.
138 if (tag == 0) {
139 return JVMTI_VISIT_OBJECTS;
140 }
141
142 Print(reference_kind,
143 reference_info,
144 class_tag,
145 referrer_class_tag,
146 size,
147 tag_ptr,
148 referrer_tag_ptr,
149 length);
150
151 counter_++;
152 if (counter_ == stop_after_) {
153 return JVMTI_VISIT_ABORT;
154 }
155
156 if (tag > 0 && tag < 32) {
157 bool should_visit_references = (follow_set_ & (1 << static_cast<int32_t>(tag))) != 0;
158 return should_visit_references ? JVMTI_VISIT_OBJECTS : 0;
159 }
160
161 return JVMTI_VISIT_OBJECTS;
162 }
163
164 void Print(jvmtiHeapReferenceKind reference_kind,
165 const jvmtiHeapReferenceInfo* reference_info,
166 jlong class_tag,
167 jlong referrer_class_tag,
168 jlong size,
169 jlong* tag_ptr,
170 jlong* referrer_tag_ptr,
171 jint length) {
172 std::string referrer_str;
173 if (referrer_tag_ptr == nullptr) {
174 referrer_str = "root@root";
175 } else {
176 referrer_str = StringPrintf("%" PRId64 "@%" PRId64, *referrer_tag_ptr, referrer_class_tag);
177 }
178
179 jlong adapted_size = size;
180 if (*tag_ptr >= 1000) {
181 // This is a class or interface, the size of which will be dependent on the architecture.
182 // Do not print the size, but detect known values and "normalize" for the golden file.
183 if ((sizeof(void*) == 4 && size == 180) || (sizeof(void*) == 8 && size == 232)) {
184 adapted_size = 123;
185 }
186 }
187
Andreas Gampedce591b2016-11-17 11:23:35 -0800188 std::string referree_str = StringPrintf("%" PRId64 "@%" PRId64, *tag_ptr, class_tag);
189
190 lines_.push_back(CreateElem(referrer_str,
191 referree_str,
192 reference_kind,
193 reference_info,
194 adapted_size,
195 length));
Andreas Gampe216090d2016-11-15 12:33:18 -0800196
197 if (reference_kind == JVMTI_HEAP_REFERENCE_THREAD && *tag_ptr == 1000) {
198 DumpStacks();
199 }
200 }
201
Andreas Gampedce591b2016-11-17 11:23:35 -0800202 std::vector<std::string> GetLines() const {
203 std::vector<std::string> ret;
204 for (const std::unique_ptr<Elem>& e : lines_) {
205 ret.push_back(e->Print());
206 }
207 return ret;
208 }
209
210 private:
211 // We need to postpone some printing, as required functions are not callback-safe.
212 class Elem {
213 public:
214 Elem(const std::string& referrer, const std::string& referree, jlong size, jint length)
215 : referrer_(referrer), referree_(referree), size_(size), length_(length) {}
216 virtual ~Elem() {}
217
218 std::string Print() const {
219 return StringPrintf("%s --(%s)--> %s [size=%" PRId64 ", length=%d]",
220 referrer_.c_str(),
221 PrintArrowType().c_str(),
222 referree_.c_str(),
223 size_,
224 length_);
225 }
226
227 protected:
228 virtual std::string PrintArrowType() const = 0;
229
230 private:
231 std::string referrer_;
232 std::string referree_;
233 jlong size_;
234 jint length_;
235 };
236
237 // For simple or unimplemented cases.
238 class StringElement : public Elem {
239 public:
240 StringElement(const std::string& referrer,
241 const std::string& referree,
242 jlong size,
243 jint length,
244 const std::string& string)
245 : Elem(referrer, referree, size, length), string_(string) {}
246
247 protected:
248 std::string PrintArrowType() const OVERRIDE {
249 return string_;
250 }
251
252 private:
253 const std::string string_;
254 };
255
256 static std::unique_ptr<Elem> CreateElem(const std::string& referrer,
257 const std::string& referree,
258 jvmtiHeapReferenceKind reference_kind,
259 const jvmtiHeapReferenceInfo* reference_info,
260 jlong size,
261 jint length) {
262 switch (reference_kind) {
263 case JVMTI_HEAP_REFERENCE_CLASS:
264 return std::unique_ptr<Elem>(new StringElement(referrer,
265 referree,
266 size,
267 length,
268 "class"));
269 case JVMTI_HEAP_REFERENCE_FIELD: {
270 std::string tmp = StringPrintf("field@%d", reference_info->field.index);
271 return std::unique_ptr<Elem>(new StringElement(referrer,
272 referree,
273 size,
274 length,
275 tmp));
276 }
277 case JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT: {
278 std::string tmp = StringPrintf("array-element@%d", reference_info->array.index);
279 return std::unique_ptr<Elem>(new StringElement(referrer,
280 referree,
281 size,
282 length,
283 tmp));
284 }
285 case JVMTI_HEAP_REFERENCE_CLASS_LOADER:
286 return std::unique_ptr<Elem>(new StringElement(referrer,
287 referree,
288 size,
289 length,
290 "classloader"));
291 case JVMTI_HEAP_REFERENCE_SIGNERS:
292 return std::unique_ptr<Elem>(new StringElement(referrer,
293 referree,
294 size,
295 length,
296 "signers"));
297 case JVMTI_HEAP_REFERENCE_PROTECTION_DOMAIN:
298 return std::unique_ptr<Elem>(new StringElement(referrer,
299 referree,
300 size,
301 length,
302 "protection-domain"));
303 case JVMTI_HEAP_REFERENCE_INTERFACE:
304 return std::unique_ptr<Elem>(new StringElement(referrer,
305 referree,
306 size,
307 length,
308 "interface"));
309 case JVMTI_HEAP_REFERENCE_STATIC_FIELD: {
310 std::string tmp = StringPrintf("array-element@%d", reference_info->array.index);
311 return std::unique_ptr<Elem>(new StringElement(referrer,
312 referree,
313 size,
314 length,
315 tmp));;
316 }
317 case JVMTI_HEAP_REFERENCE_CONSTANT_POOL:
318 return std::unique_ptr<Elem>(new StringElement(referrer,
319 referree,
320 size,
321 length,
322 "constant-pool"));
323 case JVMTI_HEAP_REFERENCE_SUPERCLASS:
324 return std::unique_ptr<Elem>(new StringElement(referrer,
325 referree,
326 size,
327 length,
328 "superclass"));
329 case JVMTI_HEAP_REFERENCE_JNI_GLOBAL:
330 return std::unique_ptr<Elem>(new StringElement(referrer,
331 referree,
332 size,
333 length,
334 "jni-global"));
335 case JVMTI_HEAP_REFERENCE_SYSTEM_CLASS:
336 return std::unique_ptr<Elem>(new StringElement(referrer,
337 referree,
338 size,
339 length,
340 "system-class"));
341 case JVMTI_HEAP_REFERENCE_MONITOR:
342 return std::unique_ptr<Elem>(new StringElement(referrer,
343 referree,
344 size,
345 length,
346 "monitor"));
347 case JVMTI_HEAP_REFERENCE_STACK_LOCAL:
348 return std::unique_ptr<Elem>(new StringElement(referrer,
349 referree,
350 size,
351 length,
352 "stack-local"));
353 case JVMTI_HEAP_REFERENCE_JNI_LOCAL:
354 return std::unique_ptr<Elem>(new StringElement(referrer,
355 referree,
356 size,
357 length,
358 "jni-local"));
359 case JVMTI_HEAP_REFERENCE_THREAD:
360 return std::unique_ptr<Elem>(new StringElement(referrer,
361 referree,
362 size,
363 length,
364 "thread"));
365 case JVMTI_HEAP_REFERENCE_OTHER:
366 return std::unique_ptr<Elem>(new StringElement(referrer,
367 referree,
368 size,
369 length,
370 "other"));
371 }
372 LOG(FATAL) << "Unknown kind";
373 UNREACHABLE();
374 }
375
Andreas Gampe216090d2016-11-15 12:33:18 -0800376 static void DumpStacks() NO_THREAD_SAFETY_ANALYSIS {
377 auto dump_function = [](art::Thread* t, void* data ATTRIBUTE_UNUSED) {
378 std::string name;
379 t->GetThreadName(name);
380 LOG(ERROR) << name;
381 art::DumpNativeStack(LOG_STREAM(ERROR), t->GetTid());
382 };
383 art::Runtime::Current()->GetThreadList()->ForEach(dump_function, nullptr);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700384 }
385
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700386 jint counter_;
387 const jint stop_after_;
388 const jint follow_set_;
Andreas Gampedce591b2016-11-17 11:23:35 -0800389
390 std::vector<std::unique_ptr<Elem>> lines_;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700391 };
392
Andreas Gampe8df06922016-11-16 10:58:11 -0800393 jit::ScopedJitSuspend sjs; // Wait to avoid JIT influence (e.g., JNI globals).
394
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700395 // If jniRef isn't null, add a local and a global ref.
396 ScopedLocalRef<jobject> jni_local_ref(env, nullptr);
397 jobject jni_global_ref = nullptr;
398 if (jniRef != nullptr) {
399 jni_local_ref.reset(env->NewLocalRef(jniRef));
400 jni_global_ref = env->NewGlobalRef(jniRef);
401 }
402
403 PrintIterationConfig config(stop_after, follow_set);
404 Run(heap_filter, klass_filter, initial_object, &config);
405
Andreas Gampedce591b2016-11-17 11:23:35 -0800406 std::vector<std::string> lines = config.GetLines();
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700407 jobjectArray ret = CreateObjectArray(env,
408 static_cast<jint>(lines.size()),
409 "java/lang/String",
410 [&](jint i) {
411 return env->NewStringUTF(lines[i].c_str());
412 });
413
414 if (jni_global_ref != nullptr) {
415 env->DeleteGlobalRef(jni_global_ref);
416 }
417
418 return ret;
419}
420
Andreas Gampe8da6d032016-10-31 19:31:03 -0700421// Don't do anything
422jint OnLoad(JavaVM* vm,
423 char* options ATTRIBUTE_UNUSED,
424 void* reserved ATTRIBUTE_UNUSED) {
425 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
426 printf("Unable to get jvmti env!\n");
427 return 1;
428 }
Alex Lighte6574242016-08-17 09:56:24 -0700429 SetAllCapabilities(jvmti_env);
Andreas Gampe8da6d032016-10-31 19:31:03 -0700430 return 0;
431}
432
Andreas Gampe8da6d032016-10-31 19:31:03 -0700433} // namespace Test913Heaps
434} // namespace art