xref: /aosp_15_r20/art/test/996-breakpoint-obsolete/obsolete_breakpoints.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2013 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 <inttypes.h>
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <cstdio>
20*795d594fSAndroid Build Coastguard Worker #include <memory>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "android-base/logging.h"
23*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker #include "jni.h"
26*795d594fSAndroid Build Coastguard Worker #include "jvmti.h"
27*795d594fSAndroid Build Coastguard Worker #include "scoped_local_ref.h"
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker // Test infrastructure
30*795d594fSAndroid Build Coastguard Worker #include "jni_binder.h"
31*795d594fSAndroid Build Coastguard Worker #include "jni_helper.h"
32*795d594fSAndroid Build Coastguard Worker #include "jvmti_helper.h"
33*795d594fSAndroid Build Coastguard Worker #include "test_env.h"
34*795d594fSAndroid Build Coastguard Worker #include "ti_macros.h"
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker namespace art {
37*795d594fSAndroid Build Coastguard Worker namespace Test996ObsoleteBreakpoints {
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker static constexpr jint kNumFrames = 10;
40*795d594fSAndroid Build Coastguard Worker 
GetFirstObsoleteMethod(JNIEnv * env,jvmtiEnv * jvmti_env)41*795d594fSAndroid Build Coastguard Worker static jmethodID GetFirstObsoleteMethod(JNIEnv* env, jvmtiEnv* jvmti_env) {
42*795d594fSAndroid Build Coastguard Worker   jint frame_count;
43*795d594fSAndroid Build Coastguard Worker   jvmtiFrameInfo frames[kNumFrames];
44*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env, jvmti_env,
45*795d594fSAndroid Build Coastguard Worker                             jvmti_env->GetStackTrace(nullptr,  // current thread
46*795d594fSAndroid Build Coastguard Worker                                                      0,
47*795d594fSAndroid Build Coastguard Worker                                                      kNumFrames,
48*795d594fSAndroid Build Coastguard Worker                                                      frames,
49*795d594fSAndroid Build Coastguard Worker                                                      &frame_count))) {
50*795d594fSAndroid Build Coastguard Worker     return nullptr;
51*795d594fSAndroid Build Coastguard Worker   }
52*795d594fSAndroid Build Coastguard Worker   for (jint i = 0; i < frame_count; i++) {
53*795d594fSAndroid Build Coastguard Worker     jboolean is_obsolete = false;
54*795d594fSAndroid Build Coastguard Worker     if (JvmtiErrorToException(env, jvmti_env,
55*795d594fSAndroid Build Coastguard Worker                               jvmti_env->IsMethodObsolete(frames[i].method, &is_obsolete))) {
56*795d594fSAndroid Build Coastguard Worker       return nullptr;
57*795d594fSAndroid Build Coastguard Worker     }
58*795d594fSAndroid Build Coastguard Worker     if (is_obsolete) {
59*795d594fSAndroid Build Coastguard Worker       return frames[i].method;
60*795d594fSAndroid Build Coastguard Worker     }
61*795d594fSAndroid Build Coastguard Worker   }
62*795d594fSAndroid Build Coastguard Worker   ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException"));
63*795d594fSAndroid Build Coastguard Worker   env->ThrowNew(rt_exception.get(), "Unable to find obsolete method!");
64*795d594fSAndroid Build Coastguard Worker   return nullptr;
65*795d594fSAndroid Build Coastguard Worker }
66*795d594fSAndroid Build Coastguard Worker 
Java_art_Test996_setBreakpointOnObsoleteMethod(JNIEnv * env,jclass k,jlong loc)67*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_Test996_setBreakpointOnObsoleteMethod(
68*795d594fSAndroid Build Coastguard Worker     JNIEnv* env, [[maybe_unused]] jclass k, jlong loc) {
69*795d594fSAndroid Build Coastguard Worker   jmethodID method = GetFirstObsoleteMethod(env, jvmti_env);
70*795d594fSAndroid Build Coastguard Worker   if (method == nullptr) {
71*795d594fSAndroid Build Coastguard Worker     return;
72*795d594fSAndroid Build Coastguard Worker   }
73*795d594fSAndroid Build Coastguard Worker   JvmtiErrorToException(env, jvmti_env, jvmti_env->SetBreakpoint(method, loc));
74*795d594fSAndroid Build Coastguard Worker }
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker }  // namespace Test996ObsoleteBreakpoints
77*795d594fSAndroid Build Coastguard Worker }  // namespace art
78