• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 "jvmti_helper.h"
18 #include "jvmti.h"
19 #include "test_env.h"
20 
21 #include <dlfcn.h>
22 
23 #include <algorithm>
24 #include <cstdio>
25 #include <cstring>
26 #include <sstream>
27 
28 #include "android-base/logging.h"
29 #include "scoped_local_ref.h"
30 
31 namespace art {
32 
CheckJvmtiError(jvmtiEnv * env,jvmtiError error)33 void CheckJvmtiError(jvmtiEnv* env, jvmtiError error) {
34   if (error != JVMTI_ERROR_NONE) {
35     char* error_name;
36     jvmtiError name_error = env->GetErrorName(error, &error_name);
37     if (name_error != JVMTI_ERROR_NONE) {
38       LOG(FATAL) << "Unable to get error name for " << error;
39     }
40     LOG(FATAL) << "Unexpected error: " << error_name;
41   }
42 }
43 
44 // These are a set of capabilities we will enable in all situations. These are chosen since they
45 // will not affect the runtime in any significant way if they are enabled.
46 static const jvmtiCapabilities standard_caps = {
47     .can_tag_objects                                 = 1,
48     .can_generate_field_modification_events          = 1,
49     .can_generate_field_access_events                = 1,
50     .can_get_bytecodes                               = 1,
51     .can_get_synthetic_attribute                     = 1,
52     .can_get_owned_monitor_info                      = 0,
53     .can_get_current_contended_monitor               = 1,
54     .can_get_monitor_info                            = 1,
55     .can_pop_frame                                   = 0,
56     .can_redefine_classes                            = 1,
57     .can_signal_thread                               = 1,
58     .can_get_source_file_name                        = 1,
59     .can_get_line_numbers                            = 1,
60     .can_get_source_debug_extension                  = 1,
61     .can_access_local_variables                      = 0,
62     .can_maintain_original_method_order              = 1,
63     .can_generate_single_step_events                 = 1,
64     .can_generate_exception_events                   = 0,
65     .can_generate_frame_pop_events                   = 0,
66     .can_generate_breakpoint_events                  = 1,
67     .can_suspend                                     = 1,
68     .can_redefine_any_class                          = 0,
69     .can_get_current_thread_cpu_time                 = 0,
70     .can_get_thread_cpu_time                         = 0,
71     .can_generate_method_entry_events                = 1,
72     .can_generate_method_exit_events                 = 1,
73     .can_generate_all_class_hook_events              = 0,
74     .can_generate_compiled_method_load_events        = 0,
75     .can_generate_monitor_events                     = 0,
76     .can_generate_vm_object_alloc_events             = 1,
77     .can_generate_native_method_bind_events          = 1,
78     .can_generate_garbage_collection_events          = 1,
79     .can_generate_object_free_events                 = 1,
80     .can_force_early_return                          = 0,
81     .can_get_owned_monitor_stack_depth_info          = 0,
82     .can_get_constant_pool                           = 0,
83     .can_set_native_method_prefix                    = 0,
84     .can_retransform_classes                         = 1,
85     .can_retransform_any_class                       = 0,
86     .can_generate_resource_exhaustion_heap_events    = 0,
87     .can_generate_resource_exhaustion_threads_events = 0,
88 };
89 
GetStandardCapabilities()90 jvmtiCapabilities GetStandardCapabilities() {
91   return standard_caps;
92 }
93 
SetStandardCapabilities(jvmtiEnv * env)94 void SetStandardCapabilities(jvmtiEnv* env) {
95   if (IsJVM()) {
96     // RI is more strict about adding capabilities at runtime then ART so just give it everything.
97     SetAllCapabilities(env);
98     return;
99   }
100   jvmtiCapabilities caps = GetStandardCapabilities();
101   CheckJvmtiError(env, env->AddCapabilities(&caps));
102 }
103 
SetAllCapabilities(jvmtiEnv * env)104 void SetAllCapabilities(jvmtiEnv* env) {
105   jvmtiCapabilities caps;
106   CheckJvmtiError(env, env->GetPotentialCapabilities(&caps));
107   CheckJvmtiError(env, env->AddCapabilities(&caps));
108 }
109 
JvmtiErrorToException(JNIEnv * env,jvmtiEnv * jvmtienv,jvmtiError error)110 bool JvmtiErrorToException(JNIEnv* env, jvmtiEnv* jvmtienv, jvmtiError error) {
111   if (error == JVMTI_ERROR_NONE) {
112     return false;
113   }
114 
115   ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException"));
116   if (rt_exception.get() == nullptr) {
117     // CNFE should be pending.
118     return true;
119   }
120 
121   char* err;
122   CheckJvmtiError(jvmtienv, jvmtienv->GetErrorName(error, &err));
123 
124   env->ThrowNew(rt_exception.get(), err);
125 
126   Deallocate(jvmtienv, err);
127   return true;
128 }
129 
operator <<(std::ostream & os,const jvmtiError & rhs)130 std::ostream& operator<<(std::ostream& os, const jvmtiError& rhs) {
131   switch (rhs) {
132     case JVMTI_ERROR_NONE:
133       return os << "NONE";
134     case JVMTI_ERROR_INVALID_THREAD:
135       return os << "INVALID_THREAD";
136     case JVMTI_ERROR_INVALID_THREAD_GROUP:
137       return os << "INVALID_THREAD_GROUP";
138     case JVMTI_ERROR_INVALID_PRIORITY:
139       return os << "INVALID_PRIORITY";
140     case JVMTI_ERROR_THREAD_NOT_SUSPENDED:
141       return os << "THREAD_NOT_SUSPENDED";
142     case JVMTI_ERROR_THREAD_SUSPENDED:
143       return os << "THREAD_SUSPENDED";
144     case JVMTI_ERROR_THREAD_NOT_ALIVE:
145       return os << "THREAD_NOT_ALIVE";
146     case JVMTI_ERROR_INVALID_OBJECT:
147       return os << "INVALID_OBJECT";
148     case JVMTI_ERROR_INVALID_CLASS:
149       return os << "INVALID_CLASS";
150     case JVMTI_ERROR_CLASS_NOT_PREPARED:
151       return os << "CLASS_NOT_PREPARED";
152     case JVMTI_ERROR_INVALID_METHODID:
153       return os << "INVALID_METHODID";
154     case JVMTI_ERROR_INVALID_LOCATION:
155       return os << "INVALID_LOCATION";
156     case JVMTI_ERROR_INVALID_FIELDID:
157       return os << "INVALID_FIELDID";
158     case JVMTI_ERROR_NO_MORE_FRAMES:
159       return os << "NO_MORE_FRAMES";
160     case JVMTI_ERROR_OPAQUE_FRAME:
161       return os << "OPAQUE_FRAME";
162     case JVMTI_ERROR_TYPE_MISMATCH:
163       return os << "TYPE_MISMATCH";
164     case JVMTI_ERROR_INVALID_SLOT:
165       return os << "INVALID_SLOT";
166     case JVMTI_ERROR_DUPLICATE:
167       return os << "DUPLICATE";
168     case JVMTI_ERROR_NOT_FOUND:
169       return os << "NOT_FOUND";
170     case JVMTI_ERROR_INVALID_MONITOR:
171       return os << "INVALID_MONITOR";
172     case JVMTI_ERROR_NOT_MONITOR_OWNER:
173       return os << "NOT_MONITOR_OWNER";
174     case JVMTI_ERROR_INTERRUPT:
175       return os << "INTERRUPT";
176     case JVMTI_ERROR_INVALID_CLASS_FORMAT:
177       return os << "INVALID_CLASS_FORMAT";
178     case JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION:
179       return os << "CIRCULAR_CLASS_DEFINITION";
180     case JVMTI_ERROR_FAILS_VERIFICATION:
181       return os << "FAILS_VERIFICATION";
182     case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED:
183       return os << "UNSUPPORTED_REDEFINITION_METHOD_ADDED";
184     case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED:
185       return os << "UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED";
186     case JVMTI_ERROR_INVALID_TYPESTATE:
187       return os << "INVALID_TYPESTATE";
188     case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED:
189       return os << "UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED";
190     case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED:
191       return os << "UNSUPPORTED_REDEFINITION_METHOD_DELETED";
192     case JVMTI_ERROR_UNSUPPORTED_VERSION:
193       return os << "UNSUPPORTED_VERSION";
194     case JVMTI_ERROR_NAMES_DONT_MATCH:
195       return os << "NAMES_DONT_MATCH";
196     case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED:
197       return os << "UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED";
198     case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED:
199       return os << "UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED";
200     case JVMTI_ERROR_UNMODIFIABLE_CLASS:
201       return os << "JVMTI_ERROR_UNMODIFIABLE_CLASS";
202     case JVMTI_ERROR_NOT_AVAILABLE:
203       return os << "NOT_AVAILABLE";
204     case JVMTI_ERROR_MUST_POSSESS_CAPABILITY:
205       return os << "MUST_POSSESS_CAPABILITY";
206     case JVMTI_ERROR_NULL_POINTER:
207       return os << "NULL_POINTER";
208     case JVMTI_ERROR_ABSENT_INFORMATION:
209       return os << "ABSENT_INFORMATION";
210     case JVMTI_ERROR_INVALID_EVENT_TYPE:
211       return os << "INVALID_EVENT_TYPE";
212     case JVMTI_ERROR_ILLEGAL_ARGUMENT:
213       return os << "ILLEGAL_ARGUMENT";
214     case JVMTI_ERROR_NATIVE_METHOD:
215       return os << "NATIVE_METHOD";
216     case JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED:
217       return os << "CLASS_LOADER_UNSUPPORTED";
218     case JVMTI_ERROR_OUT_OF_MEMORY:
219       return os << "OUT_OF_MEMORY";
220     case JVMTI_ERROR_ACCESS_DENIED:
221       return os << "ACCESS_DENIED";
222     case JVMTI_ERROR_WRONG_PHASE:
223       return os << "WRONG_PHASE";
224     case JVMTI_ERROR_INTERNAL:
225       return os << "INTERNAL";
226     case JVMTI_ERROR_UNATTACHED_THREAD:
227       return os << "UNATTACHED_THREAD";
228     case JVMTI_ERROR_INVALID_ENVIRONMENT:
229       return os << "INVALID_ENVIRONMENT";
230   }
231   LOG(FATAL) << "Unexpected error type " << static_cast<int>(rhs);
232   __builtin_unreachable();
233 }
234 
DeallocParams(jvmtiEnv * env,jvmtiParamInfo * params,jint n_params)235 void DeallocParams(jvmtiEnv* env, jvmtiParamInfo* params, jint n_params) {
236   for (jint i = 0; i < n_params; i++) {
237     Dealloc(env, params[i].name);
238   }
239 }
240 
GetExtensionEventId(jvmtiEnv * jvmti,const std::string_view & name)241 jint GetExtensionEventId(jvmtiEnv* jvmti, const std::string_view& name) {
242   jint n_ext = 0;
243   jint res = -1;
244   bool found_res = false;
245   jvmtiExtensionEventInfo* infos = nullptr;
246   CHECK_EQ(jvmti->GetExtensionEvents(&n_ext, &infos), JVMTI_ERROR_NONE);
247   for (jint i = 0; i < n_ext; i++) {
248     const jvmtiExtensionEventInfo& info = infos[i];
249     if (name == info.id) {
250       res = info.extension_event_index;
251       found_res = true;
252     }
253     DeallocParams(jvmti, info.params, info.param_count);
254     Dealloc(jvmti, info.short_description, info.id, info.params);
255   }
256   Dealloc(jvmti, infos);
257   CHECK(found_res);
258   return res;
259 }
260 
GetExtensionFunctionVoid(JNIEnv * env,jvmtiEnv * jvmti,const std::string_view & name)261 void* GetExtensionFunctionVoid(JNIEnv* env, jvmtiEnv* jvmti, const std::string_view& name) {
262   jint n_ext = 0;
263   void* res = nullptr;
264   jvmtiExtensionFunctionInfo* infos = nullptr;
265   if (JvmtiErrorToException(env, jvmti, jvmti->GetExtensionFunctions(&n_ext, &infos))) {
266     return nullptr;
267   }
268   for (jint i = 0; i < n_ext; i++) {
269     const jvmtiExtensionFunctionInfo& info = infos[i];
270     if (name == info.id) {
271       res = reinterpret_cast<void*>(info.func);
272     }
273     DeallocParams(jvmti, info.params, info.param_count);
274     Dealloc(jvmti, info.short_description, info.errors, info.id, info.params);
275   }
276   Dealloc(jvmti, infos);
277   if (res == nullptr) {
278     JvmtiErrorToException(env, jvmti, JVMTI_ERROR_NOT_FOUND);
279   }
280   return res;
281 }
282 
283 }  // namespace art
284