1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2022 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 "android-base/macros.h"
18*795d594fSAndroid Build Coastguard Worker #include "jni.h"
19*795d594fSAndroid Build Coastguard Worker #include "jvmti.h"
20*795d594fSAndroid Build Coastguard Worker #include "jvmti_helper.h"
21*795d594fSAndroid Build Coastguard Worker #include "test_env.h"
22*795d594fSAndroid Build Coastguard Worker
23*795d594fSAndroid Build Coastguard Worker namespace art {
24*795d594fSAndroid Build Coastguard Worker namespace Test2243SingleStepDefault {
25*795d594fSAndroid Build Coastguard Worker
26*795d594fSAndroid Build Coastguard Worker jmethodID interface_default_method;
27*795d594fSAndroid Build Coastguard Worker
singleStepCB(jvmtiEnv * jvmti,JNIEnv * env,jthread thr,jmethodID method,jlocation location)28*795d594fSAndroid Build Coastguard Worker static void singleStepCB(jvmtiEnv* jvmti,
29*795d594fSAndroid Build Coastguard Worker JNIEnv* env,
30*795d594fSAndroid Build Coastguard Worker jthread thr,
31*795d594fSAndroid Build Coastguard Worker jmethodID method,
32*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] jlocation location) {
33*795d594fSAndroid Build Coastguard Worker // We haven't reached the default method yet. Continue single stepping
34*795d594fSAndroid Build Coastguard Worker if (method != interface_default_method) {
35*795d594fSAndroid Build Coastguard Worker return;
36*795d594fSAndroid Build Coastguard Worker }
37*795d594fSAndroid Build Coastguard Worker
38*795d594fSAndroid Build Coastguard Worker // Disable single stepping
39*795d594fSAndroid Build Coastguard Worker jvmtiError err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, thr);
40*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env, jvmti_env, err)) {
41*795d594fSAndroid Build Coastguard Worker return;
42*795d594fSAndroid Build Coastguard Worker }
43*795d594fSAndroid Build Coastguard Worker
44*795d594fSAndroid Build Coastguard Worker // Inspect the frame.
45*795d594fSAndroid Build Coastguard Worker jint frame_count;
46*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetFrameCount(thr, &frame_count))) {
47*795d594fSAndroid Build Coastguard Worker return;
48*795d594fSAndroid Build Coastguard Worker }
49*795d594fSAndroid Build Coastguard Worker CHECK_GT(frame_count, 0);
50*795d594fSAndroid Build Coastguard Worker
51*795d594fSAndroid Build Coastguard Worker // Check that the method id from the stack frame is same as the one returned
52*795d594fSAndroid Build Coastguard Worker // by single step callback
53*795d594fSAndroid Build Coastguard Worker jmethodID m = nullptr;
54*795d594fSAndroid Build Coastguard Worker jlong loc = -1;
55*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetFrameLocation(thr, 0, &m, &loc))) {
56*795d594fSAndroid Build Coastguard Worker return;
57*795d594fSAndroid Build Coastguard Worker }
58*795d594fSAndroid Build Coastguard Worker CHECK_EQ(m, method) << "Method id from stack walk doesn't match id from single step callback";
59*795d594fSAndroid Build Coastguard Worker
60*795d594fSAndroid Build Coastguard Worker // Check that the method id is also present in the declaring class
61*795d594fSAndroid Build Coastguard Worker jclass klass = nullptr;
62*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetMethodDeclaringClass(m, &klass))) {
63*795d594fSAndroid Build Coastguard Worker return;
64*795d594fSAndroid Build Coastguard Worker }
65*795d594fSAndroid Build Coastguard Worker jint count = 0;
66*795d594fSAndroid Build Coastguard Worker jmethodID* methods = nullptr;
67*795d594fSAndroid Build Coastguard Worker jvmtiError result = jvmti_env->GetClassMethods(klass, &count, &methods);
68*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env, jvmti_env, result)) {
69*795d594fSAndroid Build Coastguard Worker return;
70*795d594fSAndroid Build Coastguard Worker }
71*795d594fSAndroid Build Coastguard Worker
72*795d594fSAndroid Build Coastguard Worker bool found_method_id = false;
73*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < count; i++) {
74*795d594fSAndroid Build Coastguard Worker if (methods[i] == method) {
75*795d594fSAndroid Build Coastguard Worker found_method_id = true;
76*795d594fSAndroid Build Coastguard Worker break;
77*795d594fSAndroid Build Coastguard Worker }
78*795d594fSAndroid Build Coastguard Worker }
79*795d594fSAndroid Build Coastguard Worker CHECK(found_method_id) << "Couldn't find the method id in the declaring class";
80*795d594fSAndroid Build Coastguard Worker
81*795d594fSAndroid Build Coastguard Worker // Check it isn't copied method.
82*795d594fSAndroid Build Coastguard Worker jint access_flags = 0;
83*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env, jvmti, jvmti->GetMethodModifiers(m, &access_flags))) {
84*795d594fSAndroid Build Coastguard Worker return;
85*795d594fSAndroid Build Coastguard Worker }
86*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kAccCopied = 0x01000000;
87*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kAccIntrinsic = 0x80000000;
88*795d594fSAndroid Build Coastguard Worker bool is_copied = ((access_flags & (kAccIntrinsic | kAccCopied)) == kAccCopied);
89*795d594fSAndroid Build Coastguard Worker CHECK(!is_copied) << "Got copied methodID. Missed canonicalizing?\n";
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker
Java_art_Test2243_setSingleStepCallback(JNIEnv * env)92*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_Test2243_setSingleStepCallback(JNIEnv* env) {
93*795d594fSAndroid Build Coastguard Worker jvmtiEventCallbacks callbacks;
94*795d594fSAndroid Build Coastguard Worker memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
95*795d594fSAndroid Build Coastguard Worker callbacks.SingleStep = singleStepCB;
96*795d594fSAndroid Build Coastguard Worker
97*795d594fSAndroid Build Coastguard Worker jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
98*795d594fSAndroid Build Coastguard Worker JvmtiErrorToException(env, jvmti_env, ret);
99*795d594fSAndroid Build Coastguard Worker }
100*795d594fSAndroid Build Coastguard Worker
Java_art_Test2243_enableSingleStep(JNIEnv * env,jclass cl,jthread thr)101*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_Test2243_enableSingleStep(JNIEnv* env,
102*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] jclass cl,
103*795d594fSAndroid Build Coastguard Worker jthread thr) {
104*795d594fSAndroid Build Coastguard Worker jvmtiError err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, thr);
105*795d594fSAndroid Build Coastguard Worker JvmtiErrorToException(env, jvmti_env, err);
106*795d594fSAndroid Build Coastguard Worker }
107*795d594fSAndroid Build Coastguard Worker
Java_art_Test2243_setSingleStepUntil(JNIEnv * env,jclass cl,jobject method)108*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_Test2243_setSingleStepUntil(JNIEnv* env,
109*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] jclass cl,
110*795d594fSAndroid Build Coastguard Worker jobject method) {
111*795d594fSAndroid Build Coastguard Worker interface_default_method = env->FromReflectedMethod(method);
112*795d594fSAndroid Build Coastguard Worker }
113*795d594fSAndroid Build Coastguard Worker
114*795d594fSAndroid Build Coastguard Worker } // namespace Test2243SingleStepDefault
115*795d594fSAndroid Build Coastguard Worker } // namespace art
116