1 // Copyright (C) 2020 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 #include <android-base/logging.h>
16 #include <jni.h>
17 #include <jvmti.h>
18 
19 #include <iostream>
20 #include <string_view>
21 
22 namespace enablevlog {
23 
24 namespace {
25 
26 // Special art ti-version number. We will use this as a fallback if we cannot get a regular JVMTI
27 // env.
28 static constexpr jint kArtTiVersion = JVMTI_VERSION_1_2 | 0x40000000;
29 
30 // The extension that lets us change the VLOG flags.
31 static constexpr std::string_view kSetVerboseExtensionName =
32     "com.android.art.misc.set_verbose_flag_ext";
33 
34 // Extension prototype
35 using SetVerboseFlagExt = jvmtiError (*)(jvmtiEnv*, const char*, jboolean);
36 
37 template <typename T>
Deallocate(jvmtiEnv * env,T * mem)38 static inline jvmtiError Deallocate(jvmtiEnv* env, T* mem) {
39   return env->Deallocate(reinterpret_cast<unsigned char*>(mem));
40 }
41 
42 template <typename T>
Dealloc(jvmtiEnv * env,T * t)43 void Dealloc(jvmtiEnv* env, T* t) {
44   env->Deallocate(reinterpret_cast<unsigned char*>(t));
45 }
46 
47 template <typename T, typename... Rest>
Dealloc(jvmtiEnv * env,T * t,Rest...rs)48 void Dealloc(jvmtiEnv* env, T* t, Rest... rs) {
49   Dealloc(env, t);
50   Dealloc(env, rs...);
51 }
52 
DeallocParams(jvmtiEnv * env,jvmtiParamInfo * params,jint n_params)53 void DeallocParams(jvmtiEnv* env, jvmtiParamInfo* params, jint n_params) {
54   for (jint i = 0; i < n_params; i++) {
55     Dealloc(env, params[i].name);
56   }
57 }
58 
59 template <typename T>
GetExtensionFunction(jvmtiEnv * jvmti,const std::string_view & name)60 T GetExtensionFunction(jvmtiEnv* jvmti, const std::string_view& name) {
61   jint n_ext = 0;
62   void* res = nullptr;
63   jvmtiExtensionFunctionInfo* infos = nullptr;
64   if (jvmti->GetExtensionFunctions(&n_ext, &infos) != JVMTI_ERROR_NONE) {
65     LOG(FATAL) << "Unable to get extensions";
66   }
67   for (jint i = 0; i < n_ext; i++) {
68     const jvmtiExtensionFunctionInfo& info = infos[i];
69     if (name == info.id) {
70       res = reinterpret_cast<void*>(info.func);
71     }
72     DeallocParams(jvmti, info.params, info.param_count);
73     Dealloc(jvmti, info.short_description, info.errors, info.id, info.params);
74   }
75   Dealloc(jvmti, infos);
76   return reinterpret_cast<T>(res);
77 }
78 
SetupJvmtiEnv(JavaVM * vm,jvmtiEnv ** jvmti)79 static jint SetupJvmtiEnv(JavaVM* vm, jvmtiEnv** jvmti) {
80   jint res = 0;
81   res = vm->GetEnv(reinterpret_cast<void**>(jvmti), JVMTI_VERSION_1_1);
82 
83   if (res != JNI_OK || *jvmti == nullptr) {
84     LOG(ERROR) << "Unable to access JVMTI, error code " << res;
85     return vm->GetEnv(reinterpret_cast<void**>(jvmti), kArtTiVersion);
86   }
87   return res;
88 }
89 
90 }  // namespace
91 
AgentStart(JavaVM * vm,char * options,void * reserved ATTRIBUTE_UNUSED)92 static jint AgentStart(JavaVM* vm, char* options, void* reserved ATTRIBUTE_UNUSED) {
93   jvmtiEnv* jvmti = nullptr;
94   if (SetupJvmtiEnv(vm, &jvmti) != JNI_OK) {
95     LOG(ERROR) << "Could not get JVMTI env or ArtTiEnv!";
96     return JNI_ERR;
97   }
98   SetVerboseFlagExt svfe = GetExtensionFunction<SetVerboseFlagExt>(jvmti, kSetVerboseExtensionName);
99   if (svfe == nullptr) {
100     LOG(ERROR) << "Could not find extension " << kSetVerboseExtensionName;
101     return JNI_ERR;
102   } else if (svfe(jvmti, options, true) != JVMTI_ERROR_NONE) {
103     return JNI_ERR;
104   } else {
105     return JNI_OK;
106   }
107 }
108 
109 // Late attachment (e.g. 'am attach-agent').
Agent_OnAttach(JavaVM * vm,char * options,void * reserved)110 extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
111   return AgentStart(vm, options, reserved);
112 }
113 
114 // Early attachment
Agent_OnLoad(JavaVM * jvm,char * options,void * reserved)115 extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* jvm, char* options, void* reserved) {
116   return AgentStart(jvm, options, reserved);
117 }
118 
119 }  // namespace enablevlog
120