Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 17 | #include <pthread.h> |
| 18 | |
| 19 | #include <cstdio> |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 20 | #include <iostream> |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 21 | #include <mutex> |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 24 | #include "android-base/logging.h" |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 25 | #include "android-base/stringprintf.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 26 | #include "jni.h" |
Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame] | 27 | #include "jvmti.h" |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 28 | #include "scoped_local_ref.h" |
| 29 | #include "scoped_utf_chars.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 30 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 31 | // Test infrastructure |
| 32 | #include "jvmti_helper.h" |
| 33 | #include "test_env.h" |
| 34 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 35 | namespace art { |
| 36 | namespace Test904ObjectAllocation { |
| 37 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 38 | static std::string GetClassName(JNIEnv* jni_env, jclass cls) { |
| 39 | ScopedLocalRef<jclass> class_class(jni_env, jni_env->GetObjectClass(cls)); |
| 40 | jmethodID mid = jni_env->GetMethodID(class_class.get(), "getName", "()Ljava/lang/String;"); |
| 41 | ScopedLocalRef<jstring> str( |
| 42 | jni_env, reinterpret_cast<jstring>(jni_env->CallObjectMethod(cls, mid))); |
| 43 | ScopedUtfChars utf_chars(jni_env, str.get()); |
| 44 | return utf_chars.c_str(); |
| 45 | } |
| 46 | |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 47 | static std::mutex gEventsMutex; |
| 48 | static std::vector<std::string> gEvents; |
| 49 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 50 | static void JNICALL ObjectAllocated(jvmtiEnv* ti_env ATTRIBUTE_UNUSED, |
| 51 | JNIEnv* jni_env, |
| 52 | jthread thread ATTRIBUTE_UNUSED, |
| 53 | jobject object, |
| 54 | jclass object_klass, |
| 55 | jlong size) { |
| 56 | std::string object_klass_descriptor = GetClassName(jni_env, object_klass); |
| 57 | ScopedLocalRef<jclass> object_klass2(jni_env, jni_env->GetObjectClass(object)); |
| 58 | std::string object_klass_descriptor2 = GetClassName(jni_env, object_klass2.get()); |
| 59 | |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 60 | std::lock_guard<std::mutex> guard(gEventsMutex); |
| 61 | gEvents.push_back(android::base::StringPrintf("ObjectAllocated type %s/%s size %zu", |
| 62 | object_klass_descriptor.c_str(), |
| 63 | object_klass_descriptor2.c_str(), |
| 64 | static_cast<size_t>(size))); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 67 | extern "C" JNIEXPORT void JNICALL Java_art_Test904_setupObjectAllocCallback( |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 68 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jboolean enable) { |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 69 | jvmtiEventCallbacks callbacks; |
| 70 | memset(&callbacks, 0, sizeof(jvmtiEventCallbacks)); |
Andreas Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 71 | callbacks.VMObjectAlloc = enable ? ObjectAllocated : nullptr; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 72 | |
| 73 | jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 74 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 77 | extern "C" JNIEXPORT void JNICALL Java_art_Test904_enableAllocationTracking( |
| 78 | JNIEnv* env, jclass, jthread thread, jboolean enable) { |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 79 | jvmtiError ret = jvmti_env->SetEventNotificationMode( |
| 80 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 81 | JVMTI_EVENT_VM_OBJECT_ALLOC, |
| 82 | thread); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 83 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 86 | extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test904_getTrackingEventMessages( |
| 87 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) { |
| 88 | std::lock_guard<std::mutex> guard(gEventsMutex); |
| 89 | jobjectArray ret = CreateObjectArray(env, |
| 90 | static_cast<jint>(gEvents.size()), |
| 91 | "java/lang/String", |
| 92 | [&](jint i) { |
| 93 | return env->NewStringUTF(gEvents[i].c_str()); |
| 94 | }); |
| 95 | gEvents.clear(); |
| 96 | return ret; |
| 97 | } |
| 98 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 99 | } // namespace Test904ObjectAllocation |
| 100 | } // namespace art |