blob: 20b53281a22ab9c622eca01934573393c9e2f2d0 [file] [log] [blame]
Andreas Gampe27fa96c2016-10-07 15:05:24 -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
Andreas Gampe27fa96c2016-10-07 15:05:24 -070017#include <iostream>
Andreas Gampe115b4982017-04-12 19:06:12 -070018#include <mutex>
Andreas Gampe27fa96c2016-10-07 15:05:24 -070019#include <pthread.h>
20#include <stdio.h>
21#include <vector>
22
Andreas Gampe027444b2017-03-31 12:49:07 -070023#include "android-base/logging.h"
Andreas Gampe115b4982017-04-12 19:06:12 -070024#include "android-base/stringprintf.h"
Andreas Gampe27fa96c2016-10-07 15:05:24 -070025#include "jni.h"
Andreas Gampe5e03a302017-03-13 13:10:00 -070026#include "jvmti.h"
Andreas Gampe027444b2017-03-31 12:49:07 -070027#include "scoped_local_ref.h"
28#include "scoped_utf_chars.h"
Andreas Gampe27fa96c2016-10-07 15:05:24 -070029
Andreas Gampe3f46c962017-03-30 10:26:59 -070030// Test infrastructure
31#include "jvmti_helper.h"
32#include "test_env.h"
33
Andreas Gampe27fa96c2016-10-07 15:05:24 -070034namespace art {
35namespace Test904ObjectAllocation {
36
Andreas Gampe27fa96c2016-10-07 15:05:24 -070037static std::string GetClassName(JNIEnv* jni_env, jclass cls) {
38 ScopedLocalRef<jclass> class_class(jni_env, jni_env->GetObjectClass(cls));
39 jmethodID mid = jni_env->GetMethodID(class_class.get(), "getName", "()Ljava/lang/String;");
40 ScopedLocalRef<jstring> str(
41 jni_env, reinterpret_cast<jstring>(jni_env->CallObjectMethod(cls, mid)));
42 ScopedUtfChars utf_chars(jni_env, str.get());
43 return utf_chars.c_str();
44}
45
Andreas Gampe115b4982017-04-12 19:06:12 -070046static std::mutex gEventsMutex;
47static std::vector<std::string> gEvents;
48
Andreas Gampe27fa96c2016-10-07 15:05:24 -070049static void JNICALL ObjectAllocated(jvmtiEnv* ti_env ATTRIBUTE_UNUSED,
50 JNIEnv* jni_env,
51 jthread thread ATTRIBUTE_UNUSED,
52 jobject object,
53 jclass object_klass,
54 jlong size) {
55 std::string object_klass_descriptor = GetClassName(jni_env, object_klass);
56 ScopedLocalRef<jclass> object_klass2(jni_env, jni_env->GetObjectClass(object));
57 std::string object_klass_descriptor2 = GetClassName(jni_env, object_klass2.get());
58
Andreas Gampe115b4982017-04-12 19:06:12 -070059 std::lock_guard<std::mutex> guard(gEventsMutex);
60 gEvents.push_back(android::base::StringPrintf("ObjectAllocated type %s/%s size %zu",
61 object_klass_descriptor.c_str(),
62 object_klass_descriptor2.c_str(),
63 static_cast<size_t>(size)));
Andreas Gampe27fa96c2016-10-07 15:05:24 -070064}
65
Andreas Gampe46651672017-04-07 09:00:04 -070066extern "C" JNIEXPORT void JNICALL Java_art_Test904_setupObjectAllocCallback(
Andreas Gampe3f46c962017-03-30 10:26:59 -070067 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jboolean enable) {
Andreas Gampe27fa96c2016-10-07 15:05:24 -070068 jvmtiEventCallbacks callbacks;
69 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
Andreas Gampe2427aae2016-10-17 18:05:19 -070070 callbacks.VMObjectAlloc = enable ? ObjectAllocated : nullptr;
Andreas Gampe27fa96c2016-10-07 15:05:24 -070071
72 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
Andreas Gampe3f46c962017-03-30 10:26:59 -070073 JvmtiErrorToException(env, jvmti_env, ret);
Andreas Gampe27fa96c2016-10-07 15:05:24 -070074}
75
Andreas Gampe46651672017-04-07 09:00:04 -070076extern "C" JNIEXPORT void JNICALL Java_art_Test904_enableAllocationTracking(
77 JNIEnv* env, jclass, jthread thread, jboolean enable) {
Andreas Gampe27fa96c2016-10-07 15:05:24 -070078 jvmtiError ret = jvmti_env->SetEventNotificationMode(
79 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
80 JVMTI_EVENT_VM_OBJECT_ALLOC,
81 thread);
Andreas Gampe3f46c962017-03-30 10:26:59 -070082 JvmtiErrorToException(env, jvmti_env, ret);
Andreas Gampe27fa96c2016-10-07 15:05:24 -070083}
84
Andreas Gampe115b4982017-04-12 19:06:12 -070085extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test904_getTrackingEventMessages(
86 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
87 std::lock_guard<std::mutex> guard(gEventsMutex);
88 jobjectArray ret = CreateObjectArray(env,
89 static_cast<jint>(gEvents.size()),
90 "java/lang/String",
91 [&](jint i) {
92 return env->NewStringUTF(gEvents[i].c_str());
93 });
94 gEvents.clear();
95 return ret;
96}
97
Andreas Gampe27fa96c2016-10-07 15:05:24 -070098} // namespace Test904ObjectAllocation
99} // namespace art