blob: c392cd48a7494bbd88ec4fb91111139d4d4a387a [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
17#include "tracking.h"
18
19#include <iostream>
20#include <pthread.h>
21#include <stdio.h>
22#include <vector>
23
24#include "base/logging.h"
25#include "jni.h"
26#include "openjdkjvmti/jvmti.h"
27#include "ScopedLocalRef.h"
28#include "ScopedUtfChars.h"
29#include "utils.h"
30
31namespace art {
32namespace Test904ObjectAllocation {
33
34static jvmtiEnv* jvmti_env;
35
36static std::string GetClassName(JNIEnv* jni_env, jclass cls) {
37 ScopedLocalRef<jclass> class_class(jni_env, jni_env->GetObjectClass(cls));
38 jmethodID mid = jni_env->GetMethodID(class_class.get(), "getName", "()Ljava/lang/String;");
39 ScopedLocalRef<jstring> str(
40 jni_env, reinterpret_cast<jstring>(jni_env->CallObjectMethod(cls, mid)));
41 ScopedUtfChars utf_chars(jni_env, str.get());
42 return utf_chars.c_str();
43}
44
45static void JNICALL ObjectAllocated(jvmtiEnv* ti_env ATTRIBUTE_UNUSED,
46 JNIEnv* jni_env,
47 jthread thread ATTRIBUTE_UNUSED,
48 jobject object,
49 jclass object_klass,
50 jlong size) {
51 std::string object_klass_descriptor = GetClassName(jni_env, object_klass);
52 ScopedLocalRef<jclass> object_klass2(jni_env, jni_env->GetObjectClass(object));
53 std::string object_klass_descriptor2 = GetClassName(jni_env, object_klass2.get());
54
55 printf("ObjectAllocated type %s/%s size %zu\n",
56 object_klass_descriptor.c_str(),
57 object_klass_descriptor2.c_str(),
58 static_cast<size_t>(size));
59}
60
61extern "C" JNIEXPORT void JNICALL Java_Main_setupCallback(JNIEnv* env ATTRIBUTE_UNUSED, jclass) {
62 jvmtiEventCallbacks callbacks;
63 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
64 callbacks.VMObjectAlloc = ObjectAllocated;
65
66 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
67 if (ret != JVMTI_ERROR_NONE) {
68 char* err;
69 jvmti_env->GetErrorName(ret, &err);
70 printf("Error setting callbacks: %s\n", err);
71 }
72}
73
74extern "C" JNIEXPORT void JNICALL Java_Main_enableAllocationTracking(JNIEnv* env ATTRIBUTE_UNUSED,
75 jclass,
76 jthread thread,
77 jboolean enable) {
78 jvmtiError ret = jvmti_env->SetEventNotificationMode(
79 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
80 JVMTI_EVENT_VM_OBJECT_ALLOC,
81 thread);
82 if (ret != JVMTI_ERROR_NONE) {
83 char* err;
84 jvmti_env->GetErrorName(ret, &err);
85 printf("Error getting tag: %s\n", err);
86 }
87}
88
89// Don't do anything
90jint OnLoad(JavaVM* vm,
91 char* options ATTRIBUTE_UNUSED,
92 void* reserved ATTRIBUTE_UNUSED) {
93 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
94 printf("Unable to get jvmti env!\n");
95 return 1;
96 }
97 return 0;
98}
99
100} // namespace Test904ObjectAllocation
101} // namespace art
102