blob: 9261a9f9a1cc47842d9799f9a9e04f76ac00126f [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"
Alex Lighte6574242016-08-17 09:56:24 -070029#include "ti-agent/common_helper.h"
Andreas Gampecc13b222016-10-10 19:09:09 -070030#include "ti-agent/common_load.h"
Andreas Gampe27fa96c2016-10-07 15:05:24 -070031#include "utils.h"
32
33namespace art {
34namespace Test904ObjectAllocation {
35
Andreas Gampe27fa96c2016-10-07 15:05:24 -070036static 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
Andreas Gampecc13b222016-10-10 19:09:09 -070061extern "C" JNIEXPORT void JNICALL Java_Main_setupObjectAllocCallback(
Andreas Gampe2427aae2016-10-17 18:05:19 -070062 JNIEnv* env ATTRIBUTE_UNUSED, jclass klass ATTRIBUTE_UNUSED, jboolean enable) {
Andreas Gampe27fa96c2016-10-07 15:05:24 -070063 jvmtiEventCallbacks callbacks;
64 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
Andreas Gampe2427aae2016-10-17 18:05:19 -070065 callbacks.VMObjectAlloc = enable ? ObjectAllocated : nullptr;
Andreas Gampe27fa96c2016-10-07 15:05:24 -070066
67 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
68 if (ret != JVMTI_ERROR_NONE) {
69 char* err;
70 jvmti_env->GetErrorName(ret, &err);
71 printf("Error setting callbacks: %s\n", err);
72 }
73}
74
75extern "C" JNIEXPORT void JNICALL Java_Main_enableAllocationTracking(JNIEnv* env ATTRIBUTE_UNUSED,
76 jclass,
77 jthread thread,
78 jboolean enable) {
79 jvmtiError ret = jvmti_env->SetEventNotificationMode(
80 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
81 JVMTI_EVENT_VM_OBJECT_ALLOC,
82 thread);
83 if (ret != JVMTI_ERROR_NONE) {
84 char* err;
85 jvmti_env->GetErrorName(ret, &err);
Andreas Gampecc13b222016-10-10 19:09:09 -070086 printf("Error enabling/disabling allocation tracking: %s\n", err);
Andreas Gampe27fa96c2016-10-07 15:05:24 -070087 }
88}
89
90// Don't do anything
91jint OnLoad(JavaVM* vm,
92 char* options ATTRIBUTE_UNUSED,
93 void* reserved ATTRIBUTE_UNUSED) {
94 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
95 printf("Unable to get jvmti env!\n");
96 return 1;
97 }
Andreas Gampe2427aae2016-10-17 18:05:19 -070098 jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, nullptr);
Alex Lighte6574242016-08-17 09:56:24 -070099 SetAllCapabilities(jvmti_env);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700100 return 0;
101}
102
103} // namespace Test904ObjectAllocation
104} // namespace art
105