Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 <mutex> |
| 18 | |
| 19 | #include "jni.h" |
| 20 | #include "jvmti.h" |
| 21 | |
| 22 | #include "android-base/logging.h" |
| 23 | #include "android-base/stringprintf.h" |
Jesse Schettler | 21bfeee | 2018-10-05 14:10:39 -0600 | [diff] [blame] | 24 | #include "jni_binder.h" |
Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 25 | #include "jvmti_helper.h" |
| 26 | #include "scoped_local_ref.h" |
| 27 | #include "scoped_utf_chars.h" |
Andreas Gampe | 7a10cb4 | 2017-04-05 10:20:25 -0700 | [diff] [blame] | 28 | #include "test_env.h" |
Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 29 | |
Andreas Gampe | 7a10cb4 | 2017-04-05 10:20:25 -0700 | [diff] [blame] | 30 | namespace art { |
Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 31 | |
| 32 | static std::string GetClassName(JNIEnv* jni_env, jclass cls) { |
| 33 | ScopedLocalRef<jclass> class_class(jni_env, jni_env->GetObjectClass(cls)); |
| 34 | jmethodID mid = jni_env->GetMethodID(class_class.get(), "getName", "()Ljava/lang/String;"); |
| 35 | ScopedLocalRef<jstring> str( |
| 36 | jni_env, reinterpret_cast<jstring>(jni_env->CallObjectMethod(cls, mid))); |
| 37 | ScopedUtfChars utf_chars(jni_env, str.get()); |
| 38 | return utf_chars.c_str(); |
| 39 | } |
| 40 | |
| 41 | static std::mutex gLock; |
| 42 | static std::string gCollection; |
| 43 | |
| 44 | static void JNICALL ObjectAllocated(jvmtiEnv* ti_env ATTRIBUTE_UNUSED, |
| 45 | JNIEnv* jni_env, |
| 46 | jthread thread ATTRIBUTE_UNUSED, |
| 47 | jobject object, |
| 48 | jclass object_klass, |
| 49 | jlong size) { |
| 50 | std::string object_klass_descriptor = GetClassName(jni_env, object_klass); |
| 51 | ScopedLocalRef<jclass> object_klass2(jni_env, jni_env->GetObjectClass(object)); |
| 52 | std::string object_klass_descriptor2 = GetClassName(jni_env, object_klass2.get()); |
| 53 | std::string result = android::base::StringPrintf("ObjectAllocated type %s/%s size %zu", |
| 54 | object_klass_descriptor.c_str(), |
| 55 | object_klass_descriptor2.c_str(), |
| 56 | static_cast<size_t>(size)); |
| 57 | std::unique_lock<std::mutex> mu(gLock); |
| 58 | gCollection += result + "#"; |
| 59 | } |
| 60 | |
| 61 | extern "C" JNIEXPORT void JNICALL Java_android_jvmti_cts_JvmtiTrackingTest_setupObjectAllocCallback( |
| 62 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jboolean enable) { |
| 63 | jvmtiEventCallbacks callbacks; |
| 64 | memset(&callbacks, 0, sizeof(jvmtiEventCallbacks)); |
| 65 | callbacks.VMObjectAlloc = enable ? ObjectAllocated : nullptr; |
| 66 | |
Andreas Gampe | 7a10cb4 | 2017-04-05 10:20:25 -0700 | [diff] [blame] | 67 | jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
| 68 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | extern "C" JNIEXPORT void JNICALL Java_android_jvmti_cts_JvmtiTrackingTest_enableAllocationTracking( |
| 72 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jthread thread, jboolean enable) { |
Andreas Gampe | 7a10cb4 | 2017-04-05 10:20:25 -0700 | [diff] [blame] | 73 | jvmtiError ret = jvmti_env->SetEventNotificationMode( |
Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 74 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 75 | JVMTI_EVENT_VM_OBJECT_ALLOC, |
| 76 | thread); |
Andreas Gampe | 7a10cb4 | 2017-04-05 10:20:25 -0700 | [diff] [blame] | 77 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | e205aab | 2017-04-03 17:27:12 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | extern "C" JNIEXPORT |
| 81 | jstring JNICALL Java_android_jvmti_cts_JvmtiTrackingTest_getAndResetAllocationTrackingString( |
| 82 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) { |
| 83 | // We will have a string allocation. So only do the C++ string retrieval under lock. |
| 84 | std::string result; |
| 85 | { |
| 86 | std::unique_lock<std::mutex> mu(gLock); |
| 87 | result.swap(gCollection); |
| 88 | } |
| 89 | |
| 90 | if (result.empty()) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
| 94 | return env->NewStringUTF(result.c_str()); |
| 95 | } |
| 96 | |
Jesse Schettler | 21bfeee | 2018-10-05 14:10:39 -0600 | [diff] [blame] | 97 | static JNINativeMethod gMethods[] = { |
| 98 | { "setupObjectAllocCallback", "(Z)V", |
| 99 | (void*)Java_android_jvmti_cts_JvmtiTrackingTest_setupObjectAllocCallback }, |
| 100 | |
| 101 | { "enableAllocationTracking", "(Ljava/lang/Thread;Z)V", |
| 102 | (void*)Java_android_jvmti_cts_JvmtiTrackingTest_enableAllocationTracking }, |
| 103 | |
| 104 | { "getAndResetAllocationTrackingString", "()Ljava/lang/String;", |
| 105 | (void*)Java_android_jvmti_cts_JvmtiTrackingTest_getAndResetAllocationTrackingString }, |
| 106 | }; |
| 107 | |
| 108 | void register_android_jvmti_cts_JvmtiTrackingTest(jvmtiEnv* jenv, JNIEnv* env) { |
Jesse Schettler | c2bcd1c | 2018-10-12 15:25:36 -0600 | [diff] [blame^] | 109 | ScopedLocalRef<jclass> klass(env, GetClass(jenv, env, |
Jesse Schettler | 21bfeee | 2018-10-05 14:10:39 -0600 | [diff] [blame] | 110 | "android/jvmti/cts/JvmtiTrackingTest", nullptr)); |
Jesse Schettler | 8709d30 | 2018-10-09 11:08:15 -0600 | [diff] [blame] | 111 | if (klass.get() == nullptr) { |
Jesse Schettler | 21bfeee | 2018-10-05 14:10:39 -0600 | [diff] [blame] | 112 | env->ExceptionClear(); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | env->RegisterNatives(klass.get(), gMethods, sizeof(gMethods) / sizeof(JNINativeMethod)); |
| 117 | if (env->ExceptionCheck()) { |
| 118 | env->ExceptionClear(); |
| 119 | LOG(ERROR) << "Could not register natives for JvmtiTrackingTest class"; |
| 120 | } |
| 121 | } |
| 122 | |
Andreas Gampe | 7a10cb4 | 2017-04-05 10:20:25 -0700 | [diff] [blame] | 123 | } // namespace art |