blob: ae1a8d34f39d7d803610083d95bb9bd85caae54f [file] [log] [blame]
Andreas Gampe3f46c962017-03-30 10:26:59 -07001/*
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"
Alex Lightc971eaf2019-08-13 10:50:38 -070018#include "jvmti.h"
Alex Light47d49b82017-07-25 14:06:34 -070019#include "test_env.h"
Andreas Gampe3f46c962017-03-30 10:26:59 -070020
Andreas Gampe3f46c962017-03-30 10:26:59 -070021#include <dlfcn.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022
23#include <algorithm>
24#include <cstdio>
25#include <cstring>
Andreas Gampe3f46c962017-03-30 10:26:59 -070026#include <sstream>
Andreas Gampe3f46c962017-03-30 10:26:59 -070027
28#include "android-base/logging.h"
29#include "scoped_local_ref.h"
30
31namespace art {
32
33void 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
Alex Light3d324fd2017-07-20 15:38:52 -070044// 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.
46static 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,
Alex Light41006c62017-09-14 09:51:14 -070053 .can_get_current_contended_monitor = 1,
Alex Lightce568642017-09-05 16:54:25 -070054 .can_get_monitor_info = 1,
Alex Light3d324fd2017-07-20 15:38:52 -070055 .can_pop_frame = 0,
56 .can_redefine_classes = 1,
Alex Light54d39dc2017-09-25 17:00:16 -070057 .can_signal_thread = 1,
Alex Light3d324fd2017-07-20 15:38:52 -070058 .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,
Alex Lightb566fed2017-07-28 15:17:00 -070062 .can_maintain_original_method_order = 1,
Alex Light3d324fd2017-07-20 15:38:52 -070063 .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
90jvmtiCapabilities GetStandardCapabilities() {
91 return standard_caps;
92}
93
94void SetStandardCapabilities(jvmtiEnv* env) {
Alex Light47d49b82017-07-25 14:06:34 -070095 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 }
Alex Light3d324fd2017-07-20 15:38:52 -0700100 jvmtiCapabilities caps = GetStandardCapabilities();
101 CheckJvmtiError(env, env->AddCapabilities(&caps));
102}
103
Andreas Gampe3f46c962017-03-30 10:26:59 -0700104void SetAllCapabilities(jvmtiEnv* env) {
105 jvmtiCapabilities caps;
Alex Light3d324fd2017-07-20 15:38:52 -0700106 CheckJvmtiError(env, env->GetPotentialCapabilities(&caps));
107 CheckJvmtiError(env, env->AddCapabilities(&caps));
Andreas Gampe3f46c962017-03-30 10:26:59 -0700108}
109
Alex Light47d49b82017-07-25 14:06:34 -0700110bool JvmtiErrorToException(JNIEnv* env, jvmtiEnv* jvmtienv, jvmtiError error) {
Andreas Gampe3f46c962017-03-30 10:26:59 -0700111 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;
Alex Light47d49b82017-07-25 14:06:34 -0700122 CheckJvmtiError(jvmtienv, jvmtienv->GetErrorName(error, &err));
Andreas Gampe3f46c962017-03-30 10:26:59 -0700123
124 env->ThrowNew(rt_exception.get(), err);
125
Alex Light47d49b82017-07-25 14:06:34 -0700126 Deallocate(jvmtienv, err);
Andreas Gampe3f46c962017-03-30 10:26:59 -0700127 return true;
128}
129
130std::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
Alex Lightc971eaf2019-08-13 10:50:38 -0700235void 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
241void* GetExtensionFunctionVoid(JNIEnv* env, jvmtiEnv* jvmti, const std::string_view& name) {
242 jint n_ext = 0;
243 void* res = nullptr;
244 jvmtiExtensionFunctionInfo* infos = nullptr;
245 if (JvmtiErrorToException(env, jvmti, jvmti->GetExtensionFunctions(&n_ext, &infos))) {
246 return nullptr;
247 }
248 for (jint i = 0; i < n_ext; i++) {
249 const jvmtiExtensionFunctionInfo& info = infos[i];
250 if (name == info.id) {
251 res = reinterpret_cast<void*>(info.func);
252 }
253 DeallocParams(jvmti, info.params, info.param_count);
254 Dealloc(jvmti, info.short_description, info.errors, info.id, info.params);
255 }
256 Dealloc(jvmti, infos);
257 if (res == nullptr) {
258 JvmtiErrorToException(env, jvmti, JVMTI_ERROR_NOT_FOUND);
259 }
260 return res;
261}
262
Andreas Gampe3f46c962017-03-30 10:26:59 -0700263} // namespace art