1 /*
2 * Copyright (C) 2022 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 "android-base/macros.h"
18 #include "android-base/logging.h"
19
20 #include "jni.h"
21 #include "jvmti.h"
22
23 namespace art {
24
25 jvmtiEnv* jvmti_env = nullptr;
26
CheckJvmtiError(jvmtiEnv * env,jvmtiError error)27 void CheckJvmtiError(jvmtiEnv* env, jvmtiError error) {
28 if (error != JVMTI_ERROR_NONE) {
29 char* error_name;
30 jvmtiError name_error = env->GetErrorName(error, &error_name);
31 if (name_error != JVMTI_ERROR_NONE) {
32 LOG(FATAL) << "Unable to get error name for " << error;
33 }
34 LOG(FATAL) << "Unexpected error: " << error_name;
35 }
36 }
37
VMInitCallback(jvmtiEnv * jenv,JNIEnv * jni_env,jthread thread)38 static void JNICALL VMInitCallback([[maybe_unused]] jvmtiEnv* jenv,
39 JNIEnv* jni_env,
40 [[maybe_unused]] jthread thread) {
41 // Set a breakpoint on a rare method that we won't expect to be hit.
42 // java.lang.Thread.stop is deprecated and not expected to be used.
43 jclass cl = jni_env->FindClass("java/lang/Thread");
44 if (cl == nullptr) {
45 LOG(FATAL) << "Cannot find class java/lang/Thread to set a breakpoint";
46 }
47
48 jmethodID method = jni_env->GetMethodID(cl, "stop", "()V");
49 if (method == nullptr) {
50 LOG(FATAL) << "Cannot find method to set a breapoint";
51 }
52
53 jlong start = 0;
54 jlong end;
55 CheckJvmtiError(jvmti_env, jvmti_env->GetMethodLocation(method, &start, &end));
56 CheckJvmtiError(jvmti_env, jvmti_env->SetBreakpoint(method, start));
57 }
58
Agent_OnLoad(JavaVM * vm,char * options,void * reserved)59 extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm,
60 [[maybe_unused]] char* options,
61 [[maybe_unused]] void* reserved) {
62 // Setup jvmti_env
63 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0) != 0) {
64 LOG(ERROR) << "Unable to get jvmti env!";
65 return 1;
66 }
67
68 // Enable breakpoint capability
69 jvmtiCapabilities capabilities;
70 memset(&capabilities, 0, sizeof(capabilities));
71 capabilities.can_generate_breakpoint_events = 1;
72 CheckJvmtiError(jvmti_env, jvmti_env->AddCapabilities(&capabilities));
73
74 // Set a callback for VM_INIT phase so we can set a breakpoint. We cannot just
75 // set a breakpoint here since vm isn't fully initialized here.
76 jvmtiEventCallbacks callbacks;
77 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
78 callbacks.VMInit = VMInitCallback;
79 CheckJvmtiError(jvmti_env, jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks)));
80 CheckJvmtiError(jvmti_env,
81 jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, nullptr));
82
83 return 0;
84 }
85
86 } // namespace art
87