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 #include <string>
21 
22 namespace listextensions {
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 constexpr jint kArtTiVersion = JVMTI_VERSION_1_2 | 0x40000000;
29 
Dealloc(jvmtiEnv * env,T * t)30 template <typename T> void Dealloc(jvmtiEnv* env, T* t) {
31   env->Deallocate(reinterpret_cast<unsigned char*>(t));
32 }
33 
Dealloc(jvmtiEnv * env,T * t,Rest...rs)34 template <typename T, typename... Rest> void Dealloc(jvmtiEnv* env, T* t, Rest... rs) {
35   Dealloc(env, t);
36   Dealloc(env, rs...);
37 }
38 
DeallocParams(jvmtiEnv * env,jvmtiParamInfo * params,jint n_params)39 void DeallocParams(jvmtiEnv* env, jvmtiParamInfo* params, jint n_params) {
40   for (jint i = 0; i < n_params; i++) {
41     Dealloc(env, params[i].name);
42   }
43 }
44 
operator <<(std::ostream & os,const jvmtiParamInfo & param)45 std::ostream& operator<<(std::ostream& os, const jvmtiParamInfo& param) {
46   os << param.name << " (";
47 #define CASE(type, name)       \
48   case JVMTI_##type##_##name:  \
49     os << #name;               \
50     break
51   switch (param.kind) {
52     CASE(KIND, IN);
53     CASE(KIND, IN_PTR);
54     CASE(KIND, IN_BUF);
55     CASE(KIND, ALLOC_BUF);
56     CASE(KIND, ALLOC_ALLOC_BUF);
57     CASE(KIND, OUT);
58     CASE(KIND, OUT_BUF);
59   }
60   os << ", ";
61   switch (param.base_type) {
62     CASE(TYPE, JBYTE);
63     CASE(TYPE, JCHAR);
64     CASE(TYPE, JSHORT);
65     CASE(TYPE, JINT);
66     CASE(TYPE, JLONG);
67     CASE(TYPE, JFLOAT);
68     CASE(TYPE, JDOUBLE);
69     CASE(TYPE, JBOOLEAN);
70     CASE(TYPE, JOBJECT);
71     CASE(TYPE, JTHREAD);
72     CASE(TYPE, JCLASS);
73     CASE(TYPE, JVALUE);
74     CASE(TYPE, JFIELDID);
75     CASE(TYPE, JMETHODID);
76     CASE(TYPE, CCHAR);
77     CASE(TYPE, CVOID);
78     CASE(TYPE, JNIENV);
79   }
80 #undef CASE
81   os << ")";
82   return os;
83 }
84 
SetupJvmtiEnv(JavaVM * vm)85 jint SetupJvmtiEnv(JavaVM* vm) {
86   jint res = 0;
87   jvmtiEnv* env = nullptr;
88   res = vm->GetEnv(reinterpret_cast<void**>(&env), JVMTI_VERSION_1_1);
89 
90   if (res != JNI_OK || env == nullptr) {
91     LOG(ERROR) << "Unable to access JVMTI, error code " << res;
92     res = vm->GetEnv(reinterpret_cast<void**>(&env), kArtTiVersion);
93     if (res != JNI_OK) {
94       return res;
95     }
96   }
97 
98   // Get the extensions.
99   jint n_ext = 0;
100   jvmtiExtensionFunctionInfo* infos = nullptr;
101   if (env->GetExtensionFunctions(&n_ext, &infos) != JVMTI_ERROR_NONE) {
102     return JNI_ERR;
103   }
104   LOG(INFO) << "Found " << n_ext << " extension functions";
105   for (jint i = 0; i < n_ext; i++) {
106     const jvmtiExtensionFunctionInfo& info = infos[i];
107     LOG(INFO) << info.id;
108     LOG(INFO) << "\tdesc: " << info.short_description;
109     LOG(INFO) << "\targuments: (count: " << info.param_count << ")";
110     for (jint j = 0; j < info.param_count; j++) {
111       const jvmtiParamInfo& param = info.params[j];
112       LOG(INFO) << "\t\t" << param;
113     }
114     LOG(INFO) << "\tErrors: (count: " << info.error_count << ")";
115     for (jint j = 0; j < info.error_count; j++) {
116       char* name;
117       CHECK_EQ(JVMTI_ERROR_NONE, env->GetErrorName(info.errors[j], &name));
118       LOG(INFO) << "\t\t" << name;
119       Dealloc(env, name);
120     }
121     DeallocParams(env, info.params, info.param_count);
122     Dealloc(env, info.short_description, info.id, info.errors, info.params);
123   }
124   // Cleanup the array.
125   Dealloc(env, infos);
126   jvmtiExtensionEventInfo* events = nullptr;
127   if (env->GetExtensionEvents(&n_ext, &events) != JVMTI_ERROR_NONE) {
128     return JNI_ERR;
129   }
130   LOG(INFO) << "Found " << n_ext << " extension events";
131   for (jint i = 0; i < n_ext; i++) {
132     const jvmtiExtensionEventInfo& info = events[i];
133     LOG(INFO) << info.id;
134     LOG(INFO) << "\tindex: " << info.extension_event_index;
135     LOG(INFO) << "\tdesc: " << info.short_description;
136     LOG(INFO) << "\tevent arguments: (count: " << info.param_count << ")";
137     for (jint j = 0; j < info.param_count; j++) {
138       const jvmtiParamInfo& param = info.params[j];
139       LOG(INFO) << "\t\t" << param;
140     }
141     DeallocParams(env, info.params, info.param_count);
142     Dealloc(env, info.short_description, info.id, info.params);
143   }
144   // Cleanup the array.
145   Dealloc(env, events);
146   env->DisposeEnvironment();
147   return JNI_OK;
148 }
149 
AgentStart(JavaVM * vm,char * options ATTRIBUTE_UNUSED,void * reserved ATTRIBUTE_UNUSED)150 jint AgentStart(JavaVM* vm, char* options ATTRIBUTE_UNUSED, void* reserved ATTRIBUTE_UNUSED) {
151   if (SetupJvmtiEnv(vm) != JNI_OK) {
152     LOG(ERROR) << "Could not get JVMTI env or ArtTiEnv!";
153     return JNI_ERR;
154   }
155   return JNI_OK;
156 }
157 
158 }  // namespace
159 
160 // Late attachment (e.g. 'am attach-agent').
Agent_OnAttach(JavaVM * vm,char * options,void * reserved)161 extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
162   return AgentStart(vm, options, reserved);
163 }
164 
165 // Early attachment
Agent_OnLoad(JavaVM * jvm,char * options,void * reserved)166 extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* jvm, char* options, void* reserved) {
167   return AgentStart(jvm, options, reserved);
168 }
169 
170 }  // namespace listextensions
171