| 1 | // Copyright (C) 2019 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | // |
| 15 | |
| 16 | #include <android-base/logging.h> |
| 17 | |
| 18 | #include <jni.h> |
| 19 | #include <jvmti.h> |
| 20 | |
| 21 | namespace dumpjvmti { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | // Special art ti-version number. We will use this as a fallback if we cannot get a regular JVMTI |
| 26 | // env. |
| 27 | static constexpr jint kArtTiVersion = JVMTI_VERSION_1_2 | 0x40000000; |
| 28 | |
| 29 | template <typename T> static void Dealloc(jvmtiEnv* env, T* t) { |
| 30 | env->Deallocate(reinterpret_cast<unsigned char*>(t)); |
| 31 | } |
| 32 | |
| 33 | template <typename T, typename... Rest> static void Dealloc(jvmtiEnv* env, T* t, Rest... rs) { |
| 34 | Dealloc(env, t); |
| 35 | Dealloc(env, rs...); |
| 36 | } |
| 37 | |
| 38 | static void DeallocParams(jvmtiEnv* env, jvmtiParamInfo* params, jint n_params) { |
| 39 | for (jint i = 0; i < n_params; i++) { |
| 40 | Dealloc(env, params[i].name); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // The extension function to get the internal data |
| 45 | static jvmtiError (*GetInternalData)(jvmtiEnv* env, unsigned char** data) = nullptr; |
| 46 | |
| 47 | static jint SetupJvmtiEnv(JavaVM* vm, jvmtiEnv** jvmti) { |
| 48 | jint res = 0; |
| 49 | res = vm->GetEnv(reinterpret_cast<void**>(jvmti), JVMTI_VERSION_1_1); |
| 50 | |
| 51 | if (res != JNI_OK || *jvmti == nullptr) { |
| 52 | LOG(ERROR) << "Unable to access JVMTI, error code " << res; |
| 53 | res = vm->GetEnv(reinterpret_cast<void**>(jvmti), kArtTiVersion); |
| 54 | if (res != JNI_OK) { |
| 55 | return res; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | jvmtiEnv* env = *jvmti; |
| 60 | |
| 61 | // Get the extensions. |
| 62 | jint n_ext = 0; |
| 63 | jvmtiExtensionFunctionInfo* infos = nullptr; |
| 64 | if (env->GetExtensionFunctions(&n_ext, &infos) != JVMTI_ERROR_NONE) { |
| 65 | return JNI_ERR; |
| 66 | } |
| 67 | for (jint i = 0; i < n_ext; i++) { |
| 68 | jvmtiExtensionFunctionInfo* cur_info = &infos[i]; |
| 69 | if (strcmp("com.android.art.misc.get_plugin_internal_state", cur_info->id) == 0) { |
| 70 | GetInternalData = reinterpret_cast<decltype(GetInternalData)>(cur_info->func); |
| 71 | } |
| 72 | // Cleanup the cur_info |
| 73 | DeallocParams(env, cur_info->params, cur_info->param_count); |
| 74 | Dealloc(env, cur_info->id, cur_info->short_description, cur_info->params, cur_info->errors); |
| 75 | } |
| 76 | // Cleanup the array. |
| 77 | Dealloc(env, infos); |
| 78 | return GetInternalData != nullptr ? JNI_OK : JNI_ERR; |
| 79 | } |
| 80 | |
| 81 | static void CbDataDump(jvmtiEnv* jvmti) { |
| 82 | unsigned char* data = nullptr; |
| 83 | if (JVMTI_ERROR_NONE == GetInternalData(jvmti, &data)) { |
| 84 | LOG(INFO) << data; |
| 85 | Dealloc(jvmti, data); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | } // namespace |
| 90 | |
| 91 | static jint AgentStart(JavaVM* vm, char* options ATTRIBUTE_UNUSED, void* reserved ATTRIBUTE_UNUSED) { |
| 92 | jvmtiEnv* jvmti = nullptr; |
| 93 | if (SetupJvmtiEnv(vm, &jvmti) != JNI_OK) { |
| 94 | LOG(ERROR) << "Could not get JVMTI env or ArtTiEnv!"; |
| 95 | return JNI_ERR; |
| 96 | } |
| 97 | jvmtiEventCallbacks cb{ |
| 98 | .DataDumpRequest = CbDataDump, |
| 99 | }; |
| 100 | jvmti->SetEventCallbacks(&cb, sizeof(cb)); |
| 101 | jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_DATA_DUMP_REQUEST, nullptr); |
| 102 | return JNI_OK; |
| 103 | } |
| 104 | |
| 105 | // Late attachment (e.g. 'am attach-agent'). |
| 106 | extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) { |
| 107 | return AgentStart(vm, options, reserved); |
| 108 | } |
| 109 | |
| 110 | // Early attachment |
| 111 | extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* jvm, char* options, void* reserved) { |
| 112 | return AgentStart(jvm, options, reserved); |
| 113 | } |
| 114 | |
| 115 | } // namespace dumpjvmti |