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