1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "common_helper.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "jni.h"
20*795d594fSAndroid Build Coastguard Worker #include "jvmti.h"
21*795d594fSAndroid Build Coastguard Worker
22*795d594fSAndroid Build Coastguard Worker #include "jvmti_helper.h"
23*795d594fSAndroid Build Coastguard Worker #include "scoped_local_ref.h"
24*795d594fSAndroid Build Coastguard Worker #include "test_env.h"
25*795d594fSAndroid Build Coastguard Worker
26*795d594fSAndroid Build Coastguard Worker namespace art {
27*795d594fSAndroid Build Coastguard Worker namespace common_frame_pop {
28*795d594fSAndroid Build Coastguard Worker
29*795d594fSAndroid Build Coastguard Worker struct FramePopData {
30*795d594fSAndroid Build Coastguard Worker jclass test_klass;
31*795d594fSAndroid Build Coastguard Worker jmethodID pop_method;
32*795d594fSAndroid Build Coastguard Worker };
33*795d594fSAndroid Build Coastguard Worker
framePopCB(jvmtiEnv * jvmti,JNIEnv * jnienv,jthread thr,jmethodID method,jboolean was_popped_by_exception)34*795d594fSAndroid Build Coastguard Worker static void framePopCB(jvmtiEnv* jvmti,
35*795d594fSAndroid Build Coastguard Worker JNIEnv* jnienv,
36*795d594fSAndroid Build Coastguard Worker jthread thr,
37*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] jmethodID method,
38*795d594fSAndroid Build Coastguard Worker jboolean was_popped_by_exception) {
39*795d594fSAndroid Build Coastguard Worker FramePopData* data = nullptr;
40*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(jnienv, jvmti,
41*795d594fSAndroid Build Coastguard Worker jvmti->GetEnvironmentLocalStorage(reinterpret_cast<void**>(&data)))) {
42*795d594fSAndroid Build Coastguard Worker return;
43*795d594fSAndroid Build Coastguard Worker }
44*795d594fSAndroid Build Coastguard Worker jlong location;
45*795d594fSAndroid Build Coastguard Worker jmethodID frame_method;
46*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(jnienv,
47*795d594fSAndroid Build Coastguard Worker jvmti,
48*795d594fSAndroid Build Coastguard Worker jvmti->GetFrameLocation(thr, 0, &frame_method, &location))) {
49*795d594fSAndroid Build Coastguard Worker return;
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker CHECK(data->pop_method != nullptr);
52*795d594fSAndroid Build Coastguard Worker jobject method_arg = GetJavaMethod(jvmti, jnienv, frame_method);
53*795d594fSAndroid Build Coastguard Worker jnienv->CallStaticVoidMethod(data->test_klass,
54*795d594fSAndroid Build Coastguard Worker data->pop_method,
55*795d594fSAndroid Build Coastguard Worker method_arg,
56*795d594fSAndroid Build Coastguard Worker was_popped_by_exception,
57*795d594fSAndroid Build Coastguard Worker location);
58*795d594fSAndroid Build Coastguard Worker jnienv->DeleteLocalRef(method_arg);
59*795d594fSAndroid Build Coastguard Worker }
60*795d594fSAndroid Build Coastguard Worker
Java_art_FramePop_enableFramePopEvent(JNIEnv * env,jclass,jclass klass,jobject notify_method,jthread thr)61*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_FramePop_enableFramePopEvent(
62*795d594fSAndroid Build Coastguard Worker JNIEnv* env, jclass, jclass klass, jobject notify_method, jthread thr) {
63*795d594fSAndroid Build Coastguard Worker FramePopData* data = nullptr;
64*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env,
65*795d594fSAndroid Build Coastguard Worker jvmti_env,
66*795d594fSAndroid Build Coastguard Worker jvmti_env->Allocate(sizeof(FramePopData),
67*795d594fSAndroid Build Coastguard Worker reinterpret_cast<unsigned char**>(&data)))) {
68*795d594fSAndroid Build Coastguard Worker return;
69*795d594fSAndroid Build Coastguard Worker }
70*795d594fSAndroid Build Coastguard Worker memset(data, 0, sizeof(FramePopData));
71*795d594fSAndroid Build Coastguard Worker data->test_klass = reinterpret_cast<jclass>(env->NewGlobalRef(klass));
72*795d594fSAndroid Build Coastguard Worker data->pop_method = env->FromReflectedMethod(notify_method);
73*795d594fSAndroid Build Coastguard Worker if (env->ExceptionCheck()) {
74*795d594fSAndroid Build Coastguard Worker return;
75*795d594fSAndroid Build Coastguard Worker }
76*795d594fSAndroid Build Coastguard Worker void* old_data = nullptr;
77*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetEnvironmentLocalStorage(&old_data))) {
78*795d594fSAndroid Build Coastguard Worker return;
79*795d594fSAndroid Build Coastguard Worker } else if (old_data != nullptr) {
80*795d594fSAndroid Build Coastguard Worker ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException"));
81*795d594fSAndroid Build Coastguard Worker env->ThrowNew(rt_exception.get(), "Environment already has local storage set!");
82*795d594fSAndroid Build Coastguard Worker return;
83*795d594fSAndroid Build Coastguard Worker }
84*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env, jvmti_env, jvmti_env->SetEnvironmentLocalStorage(data))) {
85*795d594fSAndroid Build Coastguard Worker return;
86*795d594fSAndroid Build Coastguard Worker }
87*795d594fSAndroid Build Coastguard Worker jvmtiCapabilities caps;
88*795d594fSAndroid Build Coastguard Worker memset(&caps, 0, sizeof(caps));
89*795d594fSAndroid Build Coastguard Worker caps.can_generate_frame_pop_events = 1;
90*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env, jvmti_env, jvmti_env->AddCapabilities(&caps))) {
91*795d594fSAndroid Build Coastguard Worker return;
92*795d594fSAndroid Build Coastguard Worker }
93*795d594fSAndroid Build Coastguard Worker current_callbacks.FramePop = framePopCB;
94*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env,
95*795d594fSAndroid Build Coastguard Worker jvmti_env,
96*795d594fSAndroid Build Coastguard Worker jvmti_env->SetEventCallbacks(¤t_callbacks,
97*795d594fSAndroid Build Coastguard Worker sizeof(current_callbacks)))) {
98*795d594fSAndroid Build Coastguard Worker return;
99*795d594fSAndroid Build Coastguard Worker }
100*795d594fSAndroid Build Coastguard Worker JvmtiErrorToException(env,
101*795d594fSAndroid Build Coastguard Worker jvmti_env,
102*795d594fSAndroid Build Coastguard Worker jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
103*795d594fSAndroid Build Coastguard Worker JVMTI_EVENT_FRAME_POP,
104*795d594fSAndroid Build Coastguard Worker thr));
105*795d594fSAndroid Build Coastguard Worker }
106*795d594fSAndroid Build Coastguard Worker
Java_art_FramePop_makeJvmtiEnvForFramePop(JNIEnv * env,jclass)107*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jlong JNICALL Java_art_FramePop_makeJvmtiEnvForFramePop(JNIEnv* env, jclass) {
108*795d594fSAndroid Build Coastguard Worker JavaVM* vm;
109*795d594fSAndroid Build Coastguard Worker jvmtiEnv* out_jvmti_env = nullptr;
110*795d594fSAndroid Build Coastguard Worker if (env->GetJavaVM(&vm) != JNI_OK ||
111*795d594fSAndroid Build Coastguard Worker vm->GetEnv(reinterpret_cast<void**>(&out_jvmti_env), JVMTI_VERSION_1_0) != JNI_OK) {
112*795d594fSAndroid Build Coastguard Worker ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException"));
113*795d594fSAndroid Build Coastguard Worker if (rt_exception.get() == nullptr) {
114*795d594fSAndroid Build Coastguard Worker // CNFE should be pending.
115*795d594fSAndroid Build Coastguard Worker return 0L;
116*795d594fSAndroid Build Coastguard Worker }
117*795d594fSAndroid Build Coastguard Worker env->ThrowNew(rt_exception.get(), "Unable to create new jvmti_env");
118*795d594fSAndroid Build Coastguard Worker return 0L;
119*795d594fSAndroid Build Coastguard Worker }
120*795d594fSAndroid Build Coastguard Worker SetAllCapabilities(out_jvmti_env);
121*795d594fSAndroid Build Coastguard Worker return static_cast<jlong>(reinterpret_cast<intptr_t>(out_jvmti_env));
122*795d594fSAndroid Build Coastguard Worker }
123*795d594fSAndroid Build Coastguard Worker
Java_art_FramePop_notifyFramePop(JNIEnv * env,jclass,jthread thr,jint depth)124*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_FramePop_notifyFramePop(
125*795d594fSAndroid Build Coastguard Worker JNIEnv* env, jclass, jthread thr, jint depth) {
126*795d594fSAndroid Build Coastguard Worker JvmtiErrorToException(env, jvmti_env, jvmti_env->NotifyFramePop(thr, depth));
127*795d594fSAndroid Build Coastguard Worker }
128*795d594fSAndroid Build Coastguard Worker
129*795d594fSAndroid Build Coastguard Worker } // namespace common_frame_pop
130*795d594fSAndroid Build Coastguard Worker } // namespace art
131*795d594fSAndroid Build Coastguard Worker
132