Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 1 | /* Copyright (C) 2017 The Android Open Source Project |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | * |
| 4 | * This file implements interfaces from the file jvmti.h. This implementation |
| 5 | * is licensed under the same terms as the file jvmti.h. The |
| 6 | * copyright and license information for the file jvmti.h follows. |
| 7 | * |
| 8 | * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
| 9 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 10 | * |
| 11 | * This code is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License version 2 only, as |
| 13 | * published by the Free Software Foundation. Oracle designates this |
| 14 | * particular file as subject to the "Classpath" exception as provided |
| 15 | * by Oracle in the LICENSE file that accompanied this code. |
| 16 | * |
| 17 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 20 | * version 2 for more details (a copy is included in the LICENSE file that |
| 21 | * accompanied this code). |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License version |
| 24 | * 2 along with this work; if not, write to the Free Software Foundation, |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 25 | * |
| 26 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 27 | * or visit www.oracle.com if you need additional information or have any |
| 28 | * questions. |
| 29 | */ |
| 30 | |
| 31 | #include <vector> |
| 32 | |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 33 | #include "jvmti.h" |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 34 | #include "ti_extension.h" |
| 35 | |
| 36 | #include "art_jvmti.h" |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 37 | #include "events.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 38 | #include "jni_id_type.h" |
| 39 | #include "runtime-inl.h" |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 40 | #include "ti_allocator.h" |
Alex Light | c758875 | 2018-02-20 11:15:54 -0800 | [diff] [blame] | 41 | #include "ti_class.h" |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 42 | #include "ti_ddms.h" |
Alex Light | b214694 | 2019-03-12 15:46:40 +0000 | [diff] [blame] | 43 | #include "ti_dump.h" |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 44 | #include "ti_heap.h" |
Alex Light | ae45cbb | 2018-10-18 15:49:56 -0700 | [diff] [blame] | 45 | #include "ti_logging.h" |
Alex Light | c98f83e | 2018-07-26 08:28:36 -0700 | [diff] [blame] | 46 | #include "ti_monitor.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 47 | #include "ti_redefine.h" |
Alex Light | 639e73b | 2019-05-17 21:44:36 +0000 | [diff] [blame] | 48 | #include "ti_search.h" |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 49 | #include "transform.h" |
Alex Light | ae45cbb | 2018-10-18 15:49:56 -0700 | [diff] [blame] | 50 | |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 51 | #include "thread-inl.h" |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 52 | |
| 53 | namespace openjdkjvmti { |
| 54 | |
| 55 | struct CParamInfo { |
| 56 | const char* name; |
| 57 | jvmtiParamKind kind; |
| 58 | jvmtiParamTypes base_type; |
| 59 | jboolean null_ok; |
| 60 | |
| 61 | jvmtiParamInfo ToParamInfo(jvmtiEnv* env, |
| 62 | /*out*/std::vector<JvmtiUniquePtr<char[]>>* char_buffers, |
| 63 | /*out*/jvmtiError* err) const { |
| 64 | JvmtiUniquePtr<char[]> param_name = CopyString(env, name, err); |
| 65 | char* name_ptr = param_name.get(); |
| 66 | char_buffers->push_back(std::move(param_name)); |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 67 | return jvmtiParamInfo{ name_ptr, kind, base_type, null_ok }; |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 68 | } |
| 69 | }; |
| 70 | |
| 71 | jvmtiError ExtensionUtil::GetExtensionFunctions(jvmtiEnv* env, |
| 72 | jint* extension_count_ptr, |
| 73 | jvmtiExtensionFunctionInfo** extensions) { |
| 74 | if (extension_count_ptr == nullptr || extensions == nullptr) { |
| 75 | return ERR(NULL_POINTER); |
| 76 | } |
| 77 | |
| 78 | std::vector<jvmtiExtensionFunctionInfo> ext_vector; |
| 79 | |
| 80 | // Holders for allocated values. |
| 81 | std::vector<JvmtiUniquePtr<char[]>> char_buffers; |
| 82 | std::vector<JvmtiUniquePtr<jvmtiParamInfo[]>> param_buffers; |
| 83 | std::vector<JvmtiUniquePtr<jvmtiError[]>> error_buffers; |
| 84 | |
| 85 | auto add_extension = [&](jvmtiExtensionFunction func, |
| 86 | const char* id, |
| 87 | const char* short_description, |
| 88 | const std::vector<CParamInfo>& params, |
| 89 | const std::vector<jvmtiError>& errors) { |
| 90 | jvmtiExtensionFunctionInfo func_info; |
| 91 | jvmtiError error; |
| 92 | |
| 93 | func_info.func = func; |
| 94 | |
| 95 | JvmtiUniquePtr<char[]> id_ptr = CopyString(env, id, &error); |
| 96 | if (id_ptr == nullptr) { |
| 97 | return error; |
| 98 | } |
| 99 | func_info.id = id_ptr.get(); |
| 100 | char_buffers.push_back(std::move(id_ptr)); |
| 101 | |
| 102 | JvmtiUniquePtr<char[]> descr = CopyString(env, short_description, &error); |
| 103 | if (descr == nullptr) { |
| 104 | return error; |
| 105 | } |
| 106 | func_info.short_description = descr.get(); |
| 107 | char_buffers.push_back(std::move(descr)); |
| 108 | |
| 109 | func_info.param_count = params.size(); |
| 110 | if (!params.empty()) { |
| 111 | JvmtiUniquePtr<jvmtiParamInfo[]> params_ptr = |
| 112 | AllocJvmtiUniquePtr<jvmtiParamInfo[]>(env, params.size(), &error); |
| 113 | if (params_ptr == nullptr) { |
| 114 | return error; |
| 115 | } |
| 116 | func_info.params = params_ptr.get(); |
| 117 | param_buffers.push_back(std::move(params_ptr)); |
| 118 | |
| 119 | for (jint i = 0; i != func_info.param_count; ++i) { |
| 120 | func_info.params[i] = params[i].ToParamInfo(env, &char_buffers, &error); |
| 121 | if (error != OK) { |
| 122 | return error; |
| 123 | } |
| 124 | } |
| 125 | } else { |
| 126 | func_info.params = nullptr; |
| 127 | } |
| 128 | |
| 129 | func_info.error_count = errors.size(); |
| 130 | if (!errors.empty()) { |
| 131 | JvmtiUniquePtr<jvmtiError[]> errors_ptr = |
| 132 | AllocJvmtiUniquePtr<jvmtiError[]>(env, errors.size(), &error); |
| 133 | if (errors_ptr == nullptr) { |
| 134 | return error; |
| 135 | } |
| 136 | func_info.errors = errors_ptr.get(); |
| 137 | error_buffers.push_back(std::move(errors_ptr)); |
| 138 | |
| 139 | for (jint i = 0; i != func_info.error_count; ++i) { |
| 140 | func_info.errors[i] = errors[i]; |
| 141 | } |
| 142 | } else { |
| 143 | func_info.errors = nullptr; |
| 144 | } |
| 145 | |
| 146 | ext_vector.push_back(func_info); |
| 147 | |
| 148 | return ERR(NONE); |
| 149 | }; |
| 150 | |
| 151 | jvmtiError error; |
| 152 | |
| 153 | // Heap extensions. |
| 154 | error = add_extension( |
| 155 | reinterpret_cast<jvmtiExtensionFunction>(HeapExtensions::GetObjectHeapId), |
| 156 | "com.android.art.heap.get_object_heap_id", |
| 157 | "Retrieve the heap id of the the object tagged with the given argument. An " |
| 158 | "arbitrary object is chosen if multiple objects exist with the same tag.", |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 159 | { |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 160 | { "tag", JVMTI_KIND_IN, JVMTI_TYPE_JLONG, false}, |
| 161 | { "heap_id", JVMTI_KIND_OUT, JVMTI_TYPE_JINT, false} |
| 162 | }, |
| 163 | { JVMTI_ERROR_NOT_FOUND }); |
| 164 | if (error != ERR(NONE)) { |
| 165 | return error; |
| 166 | } |
| 167 | |
| 168 | error = add_extension( |
| 169 | reinterpret_cast<jvmtiExtensionFunction>(HeapExtensions::GetHeapName), |
| 170 | "com.android.art.heap.get_heap_name", |
| 171 | "Retrieve the name of the heap with the given id.", |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 172 | { |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 173 | { "heap_id", JVMTI_KIND_IN, JVMTI_TYPE_JINT, false}, |
| 174 | { "heap_name", JVMTI_KIND_ALLOC_BUF, JVMTI_TYPE_CCHAR, false} |
| 175 | }, |
| 176 | { JVMTI_ERROR_ILLEGAL_ARGUMENT }); |
| 177 | if (error != ERR(NONE)) { |
| 178 | return error; |
| 179 | } |
| 180 | |
| 181 | error = add_extension( |
| 182 | reinterpret_cast<jvmtiExtensionFunction>(HeapExtensions::IterateThroughHeapExt), |
| 183 | "com.android.art.heap.iterate_through_heap_ext", |
| 184 | "Iterate through a heap. This is equivalent to the standard IterateThroughHeap function," |
| 185 | " except for additionally passing the heap id of the current object. The jvmtiHeapCallbacks" |
| 186 | " structure is reused, with the callbacks field overloaded to a signature of " |
| 187 | "jint (*)(jlong, jlong, jlong*, jint length, void*, jint).", |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 188 | { |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 189 | { "heap_filter", JVMTI_KIND_IN, JVMTI_TYPE_JINT, false}, |
| 190 | { "klass", JVMTI_KIND_IN, JVMTI_TYPE_JCLASS, true}, |
| 191 | { "callbacks", JVMTI_KIND_IN_PTR, JVMTI_TYPE_CVOID, false}, |
| 192 | { "user_data", JVMTI_KIND_IN_PTR, JVMTI_TYPE_CVOID, true} |
| 193 | }, |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 194 | { |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 195 | ERR(MUST_POSSESS_CAPABILITY), |
| 196 | ERR(INVALID_CLASS), |
| 197 | ERR(NULL_POINTER), |
| 198 | }); |
| 199 | if (error != ERR(NONE)) { |
| 200 | return error; |
| 201 | } |
| 202 | |
| 203 | error = add_extension( |
| 204 | reinterpret_cast<jvmtiExtensionFunction>(AllocUtil::GetGlobalJvmtiAllocationState), |
| 205 | "com.android.art.alloc.get_global_jvmti_allocation_state", |
| 206 | "Returns the total amount of memory currently allocated by all jvmtiEnvs through the" |
| 207 | " 'Allocate' jvmti function. This does not include any memory that has been deallocated" |
| 208 | " through the 'Deallocate' function. This number is approximate and might not correspond" |
| 209 | " exactly to the sum of the sizes of all not freed allocations.", |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 210 | { |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 211 | { "currently_allocated", JVMTI_KIND_OUT, JVMTI_TYPE_JLONG, false}, |
| 212 | }, |
| 213 | { ERR(NULL_POINTER) }); |
| 214 | if (error != ERR(NONE)) { |
| 215 | return error; |
| 216 | } |
| 217 | |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 218 | // DDMS extension |
| 219 | error = add_extension( |
| 220 | reinterpret_cast<jvmtiExtensionFunction>(DDMSUtil::HandleChunk), |
| 221 | "com.android.art.internal.ddm.process_chunk", |
| 222 | "Handles a single ddms chunk request and returns a response. The reply data is in the ddms" |
| 223 | " chunk format. It returns the processed chunk. This is provided for backwards compatibility" |
| 224 | " reasons only. Agents should avoid making use of this extension when possible and instead" |
| 225 | " use the other JVMTI entrypoints explicitly.", |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 226 | { |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 227 | { "type_in", JVMTI_KIND_IN, JVMTI_TYPE_JINT, false }, |
| 228 | { "length_in", JVMTI_KIND_IN, JVMTI_TYPE_JINT, false }, |
Alex Light | 6f2a634 | 2017-12-12 09:55:05 -0800 | [diff] [blame] | 229 | { "data_in", JVMTI_KIND_IN_BUF, JVMTI_TYPE_JBYTE, true }, |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 230 | { "type_out", JVMTI_KIND_OUT, JVMTI_TYPE_JINT, false }, |
| 231 | { "data_len_out", JVMTI_KIND_OUT, JVMTI_TYPE_JINT, false }, |
| 232 | { "data_out", JVMTI_KIND_ALLOC_BUF, JVMTI_TYPE_JBYTE, false } |
| 233 | }, |
| 234 | { ERR(NULL_POINTER), ERR(ILLEGAL_ARGUMENT), ERR(OUT_OF_MEMORY) }); |
| 235 | if (error != ERR(NONE)) { |
| 236 | return error; |
| 237 | } |
| 238 | |
Alex Light | c758875 | 2018-02-20 11:15:54 -0800 | [diff] [blame] | 239 | // GetClassLoaderClassDescriptors extension |
| 240 | error = add_extension( |
| 241 | reinterpret_cast<jvmtiExtensionFunction>(ClassUtil::GetClassLoaderClassDescriptors), |
| 242 | "com.android.art.class.get_class_loader_class_descriptors", |
| 243 | "Retrieves a list of all the classes (as class descriptors) that the given class loader is" |
| 244 | " capable of being the defining class loader for. The return format is a list of" |
| 245 | " null-terminated descriptor strings of the form \"L/java/lang/Object;\". Each descriptor" |
| 246 | " will be in the list at most once. If the class_loader is null the bootclassloader will be" |
| 247 | " used. If the class_loader is not null it must either be a java.lang.BootClassLoader, a" |
| 248 | " dalvik.system.BaseDexClassLoader or a derived type. The data_out list and all elements" |
| 249 | " must be deallocated by the caller.", |
| 250 | { |
| 251 | { "class_loader", JVMTI_KIND_IN, JVMTI_TYPE_JOBJECT, true }, |
| 252 | { "class_descriptor_count_out", JVMTI_KIND_OUT, JVMTI_TYPE_JINT, false }, |
| 253 | { "data_out", JVMTI_KIND_ALLOC_ALLOC_BUF, JVMTI_TYPE_CCHAR, false }, |
| 254 | }, |
| 255 | { |
| 256 | ERR(NULL_POINTER), |
| 257 | ERR(ILLEGAL_ARGUMENT), |
| 258 | ERR(OUT_OF_MEMORY), |
| 259 | ERR(NOT_IMPLEMENTED), |
| 260 | }); |
| 261 | if (error != ERR(NONE)) { |
| 262 | return error; |
| 263 | } |
Alex Light | c98f83e | 2018-07-26 08:28:36 -0700 | [diff] [blame] | 264 | |
| 265 | // Raw monitors no suspend |
| 266 | error = add_extension( |
| 267 | reinterpret_cast<jvmtiExtensionFunction>(MonitorUtil::RawMonitorEnterNoSuspend), |
| 268 | "com.android.art.concurrent.raw_monitor_enter_no_suspend", |
| 269 | "Normally entering a monitor will not return until both the monitor is locked and the" |
| 270 | " current thread is not suspended. This method will return once the monitor is locked" |
| 271 | " even if the thread is suspended. Note that using rawMonitorWait will wait until the" |
| 272 | " thread is not suspended again on wakeup and so should be avoided.", |
| 273 | { |
| 274 | { "raw_monitor", JVMTI_KIND_IN_PTR, JVMTI_TYPE_CVOID, false }, |
| 275 | }, |
| 276 | { |
| 277 | ERR(NULL_POINTER), |
| 278 | ERR(INVALID_MONITOR), |
| 279 | }); |
| 280 | if (error != ERR(NONE)) { |
| 281 | return error; |
| 282 | } |
Alex Light | ae45cbb | 2018-10-18 15:49:56 -0700 | [diff] [blame] | 283 | |
| 284 | // GetLastError extension |
| 285 | error = add_extension( |
| 286 | reinterpret_cast<jvmtiExtensionFunction>(LogUtil::GetLastError), |
| 287 | "com.android.art.misc.get_last_error_message", |
| 288 | "In some cases the jvmti plugin will log data about errors to the android logcat. These can" |
| 289 | " be useful to tools so we make (some) of the messages available here as well. This will" |
| 290 | " fill the given 'msg' buffer with the last non-fatal message associated with this" |
| 291 | " jvmti-env. Note this is best-effort only, not all log messages will be accessible through" |
| 292 | " this API. This will return the last error-message from all threads. Care should be taken" |
| 293 | " interpreting the return value when used with a multi-threaded program. The error message" |
| 294 | " will only be cleared by a call to 'com.android.art.misc.clear_last_error_message' and will" |
| 295 | " not be cleared by intervening successful calls. If no (tracked) error message has been" |
| 296 | " sent since the last call to clear_last_error_message this API will return" |
| 297 | " JVMTI_ERROR_ABSENT_INFORMATION. Not all failures will cause an error message to be" |
| 298 | " recorded.", |
| 299 | { |
| 300 | { "msg", JVMTI_KIND_ALLOC_BUF, JVMTI_TYPE_CCHAR, false }, |
| 301 | }, |
| 302 | { |
| 303 | ERR(NULL_POINTER), |
| 304 | ERR(ABSENT_INFORMATION), |
| 305 | }); |
| 306 | if (error != ERR(NONE)) { |
| 307 | return error; |
| 308 | } |
| 309 | |
| 310 | // ClearLastError extension |
| 311 | error = add_extension( |
| 312 | reinterpret_cast<jvmtiExtensionFunction>(LogUtil::ClearLastError), |
| 313 | "com.android.art.misc.clear_last_error_message", |
| 314 | "Clears the error message returned by 'com.android.art.misc.get_last_error_message'.", |
| 315 | { }, |
| 316 | { }); |
| 317 | if (error != ERR(NONE)) { |
| 318 | return error; |
| 319 | } |
| 320 | |
Alex Light | b214694 | 2019-03-12 15:46:40 +0000 | [diff] [blame] | 321 | // DumpInternalState |
| 322 | error = add_extension( |
| 323 | reinterpret_cast<jvmtiExtensionFunction>(DumpUtil::DumpInternalState), |
| 324 | "com.android.art.misc.get_plugin_internal_state", |
| 325 | "Gets internal state about the plugin and serializes it to the given msg. " |
| 326 | "There is no particular format to this message beyond being human readable.", |
| 327 | { |
| 328 | { "msg", JVMTI_KIND_ALLOC_BUF, JVMTI_TYPE_CCHAR, false }, |
| 329 | }, |
| 330 | { ERR(NULL_POINTER) }); |
| 331 | if (error != ERR(NONE)) { |
| 332 | return error; |
| 333 | } |
| 334 | |
Alex Light | 639e73b | 2019-05-17 21:44:36 +0000 | [diff] [blame] | 335 | // AddToDexClassLoader |
| 336 | error = add_extension( |
| 337 | reinterpret_cast<jvmtiExtensionFunction>(SearchUtil::AddToDexClassLoader), |
| 338 | "com.android.art.classloader.add_to_dex_class_loader", |
| 339 | "Adds a dexfile to a given dalvik.system.BaseDexClassLoader in a manner similar to" |
| 340 | " AddToSystemClassLoader.", |
| 341 | { |
| 342 | { "classloader", JVMTI_KIND_IN, JVMTI_TYPE_JOBJECT, false }, |
| 343 | { "segment", JVMTI_KIND_IN_PTR, JVMTI_TYPE_CCHAR, false }, |
| 344 | }, |
| 345 | { |
| 346 | ERR(NULL_POINTER), |
| 347 | ERR(CLASS_LOADER_UNSUPPORTED), |
| 348 | ERR(ILLEGAL_ARGUMENT), |
| 349 | ERR(WRONG_PHASE), |
| 350 | }); |
| 351 | if (error != ERR(NONE)) { |
| 352 | return error; |
| 353 | } |
| 354 | |
| 355 | // AddToDexClassLoaderInMemory |
| 356 | error = add_extension( |
| 357 | reinterpret_cast<jvmtiExtensionFunction>(SearchUtil::AddToDexClassLoaderInMemory), |
| 358 | "com.android.art.classloader.add_to_dex_class_loader_in_memory", |
| 359 | "Adds a dexfile buffer to a given dalvik.system.BaseDexClassLoader in a manner similar to" |
| 360 | " AddToSystemClassLoader. This may only be done during the LIVE phase. The buffer is copied" |
| 361 | " and the caller is responsible for deallocating it after this call.", |
| 362 | { |
| 363 | { "classloader", JVMTI_KIND_IN, JVMTI_TYPE_JOBJECT, false }, |
| 364 | { "dex_bytes", JVMTI_KIND_IN_BUF, JVMTI_TYPE_CCHAR, false }, |
| 365 | { "dex_bytes_len", JVMTI_KIND_IN, JVMTI_TYPE_JINT, false }, |
| 366 | }, |
| 367 | { |
| 368 | ERR(NULL_POINTER), |
| 369 | ERR(CLASS_LOADER_UNSUPPORTED), |
| 370 | ERR(ILLEGAL_ARGUMENT), |
| 371 | ERR(WRONG_PHASE), |
| 372 | }); |
| 373 | if (error != ERR(NONE)) { |
| 374 | return error; |
| 375 | } |
| 376 | |
Alex Light | c14ec8f | 2019-07-18 16:08:41 -0700 | [diff] [blame] | 377 | // ChangeArraySize |
| 378 | error = add_extension( |
| 379 | reinterpret_cast<jvmtiExtensionFunction>(HeapExtensions::ChangeArraySize), |
| 380 | "com.android.art.heap.change_array_size", |
| 381 | "Changes the size of a java array. As far as all JNI and java code is concerned this is" |
| 382 | " atomic. Must have can_tag_objects capability. If the new length of the array is smaller" |
| 383 | " than the original length, then the array will be truncated to the new length. Otherwise," |
| 384 | " all new slots will be filled with null, 0, or False as appropriate for the array type.", |
| 385 | { |
| 386 | { "array", JVMTI_KIND_IN, JVMTI_TYPE_JOBJECT, false }, |
| 387 | { "new_size", JVMTI_KIND_IN, JVMTI_TYPE_JINT, false }, |
| 388 | }, |
| 389 | { |
| 390 | ERR(NULL_POINTER), |
| 391 | ERR(MUST_POSSESS_CAPABILITY), |
| 392 | ERR(ILLEGAL_ARGUMENT), |
| 393 | ERR(OUT_OF_MEMORY), |
| 394 | }); |
| 395 | if (error != ERR(NONE)) { |
| 396 | return error; |
| 397 | } |
| 398 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 399 | // These require index-ids and debuggable to function |
| 400 | art::Runtime* runtime = art::Runtime::Current(); |
| 401 | if (runtime->GetJniIdType() == art::JniIdType::kIndices && |
| 402 | (runtime->GetInstrumentation()->IsForcedInterpretOnly() || runtime->IsJavaDebuggable())) { |
| 403 | // IsStructurallyModifiableClass |
| 404 | error = add_extension( |
| 405 | reinterpret_cast<jvmtiExtensionFunction>(Redefiner::IsStructurallyModifiableClass), |
| 406 | "com.android.art.class.is_structurally_modifiable_class", |
| 407 | "Returns whether a class can potentially be 'structurally' redefined using the various" |
| 408 | " structural redefinition extensions provided.", |
| 409 | { |
| 410 | { "klass", JVMTI_KIND_IN, JVMTI_TYPE_JCLASS, false }, |
| 411 | { "result", JVMTI_KIND_OUT, JVMTI_TYPE_JBOOLEAN, false }, |
| 412 | }, |
| 413 | { |
| 414 | ERR(INVALID_CLASS), |
| 415 | ERR(NULL_POINTER), |
| 416 | }); |
| 417 | if (error != ERR(NONE)) { |
| 418 | return error; |
| 419 | } |
| 420 | |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 421 | // StructurallyRedefineClasses |
| 422 | error = add_extension( |
| 423 | reinterpret_cast<jvmtiExtensionFunction>(Redefiner::StructurallyRedefineClasses), |
| 424 | "com.android.art.class.structurally_redefine_classes", |
| 425 | "Entrypoint for structural class redefinition. Has the same signature as RedefineClasses." |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame^] | 426 | " Currently does not support redefining a class and any of its supertypes at the same time." |
| 427 | " Only supports additive changes, methods and fields may not be removed. Supertypes and" |
| 428 | " implemented interfaces may not be changed. After calling this" |
| 429 | " com.android.art.structural_dex_file_load_hook events will be triggered, followed by" |
| 430 | " re-transformable ClassFileLoadHook events. After this method completes subsequent" |
| 431 | " RetransformClasses calls will use the input to this function as the initial class" |
| 432 | " definition.", |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 433 | { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame^] | 434 | { "num_classes", JVMTI_KIND_IN, JVMTI_TYPE_JINT, false }, |
| 435 | { "class_definitions", JVMTI_KIND_IN_BUF, JVMTI_TYPE_CVOID, false }, |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 436 | }, |
| 437 | { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame^] | 438 | ERR(CLASS_LOADER_UNSUPPORTED), |
| 439 | ERR(FAILS_VERIFICATION), |
| 440 | ERR(ILLEGAL_ARGUMENT), |
| 441 | ERR(INVALID_CLASS), |
| 442 | ERR(MUST_POSSESS_CAPABILITY), |
| 443 | ERR(MUST_POSSESS_CAPABILITY), |
| 444 | ERR(NULL_POINTER), |
| 445 | ERR(OUT_OF_MEMORY), |
| 446 | ERR(UNMODIFIABLE_CLASS), |
| 447 | ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), |
| 448 | ERR(UNSUPPORTED_REDEFINITION_METHOD_ADDED), |
| 449 | ERR(UNSUPPORTED_REDEFINITION_METHOD_DELETED), |
| 450 | ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED), |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 451 | }); |
| 452 | if (error != ERR(NONE)) { |
| 453 | return error; |
| 454 | } |
| 455 | |
| 456 | // StructurallyRedefineClassDirect |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 457 | error = add_extension( |
| 458 | reinterpret_cast<jvmtiExtensionFunction>(Redefiner::StructurallyRedefineClassDirect), |
| 459 | "com.android.art.UNSAFE.class.structurally_redefine_class_direct", |
| 460 | "Temporary prototype entrypoint for redefining a single class structurally. Currently this" |
| 461 | " only supports adding new static fields to a class without any instances." |
| 462 | " ClassFileLoadHook events will NOT be triggered. This does not currently support creating" |
| 463 | " obsolete methods. This function only has rudimentary error checking. This should not be" |
| 464 | " used except for testing.", |
| 465 | { |
| 466 | { "klass", JVMTI_KIND_IN, JVMTI_TYPE_JCLASS, false }, |
| 467 | { "new_def", JVMTI_KIND_IN_BUF, JVMTI_TYPE_CCHAR, false }, |
| 468 | { "new_def_len", JVMTI_KIND_IN, JVMTI_TYPE_JINT, false }, |
| 469 | }, |
| 470 | { |
| 471 | ERR(CLASS_LOADER_UNSUPPORTED), |
| 472 | ERR(FAILS_VERIFICATION), |
| 473 | ERR(ILLEGAL_ARGUMENT), |
| 474 | ERR(INVALID_CLASS), |
| 475 | ERR(MUST_POSSESS_CAPABILITY), |
| 476 | ERR(MUST_POSSESS_CAPABILITY), |
| 477 | ERR(NULL_POINTER), |
| 478 | ERR(OUT_OF_MEMORY), |
| 479 | ERR(UNMODIFIABLE_CLASS), |
| 480 | ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), |
| 481 | ERR(UNSUPPORTED_REDEFINITION_METHOD_ADDED), |
| 482 | ERR(UNSUPPORTED_REDEFINITION_METHOD_DELETED), |
| 483 | ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED), |
| 484 | }); |
| 485 | if (error != ERR(NONE)) { |
| 486 | return error; |
| 487 | } |
| 488 | } else { |
| 489 | LOG(INFO) << "debuggable & jni-type indices are required to implement structural " |
| 490 | << "class redefinition extensions."; |
| 491 | } |
| 492 | |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 493 | // Copy into output buffer. |
| 494 | |
| 495 | *extension_count_ptr = ext_vector.size(); |
| 496 | JvmtiUniquePtr<jvmtiExtensionFunctionInfo[]> out_data = |
| 497 | AllocJvmtiUniquePtr<jvmtiExtensionFunctionInfo[]>(env, ext_vector.size(), &error); |
| 498 | if (out_data == nullptr) { |
| 499 | return error; |
| 500 | } |
| 501 | memcpy(out_data.get(), |
| 502 | ext_vector.data(), |
| 503 | ext_vector.size() * sizeof(jvmtiExtensionFunctionInfo)); |
| 504 | *extensions = out_data.release(); |
| 505 | |
| 506 | // Release all the buffer holders, we're OK now. |
| 507 | for (auto& holder : char_buffers) { |
| 508 | holder.release(); |
| 509 | } |
| 510 | for (auto& holder : param_buffers) { |
| 511 | holder.release(); |
| 512 | } |
| 513 | for (auto& holder : error_buffers) { |
| 514 | holder.release(); |
| 515 | } |
| 516 | |
| 517 | return OK; |
| 518 | } |
| 519 | |
| 520 | |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 521 | jvmtiError ExtensionUtil::GetExtensionEvents(jvmtiEnv* env, |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 522 | jint* extension_count_ptr, |
| 523 | jvmtiExtensionEventInfo** extensions) { |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 524 | std::vector<jvmtiExtensionEventInfo> ext_vector; |
| 525 | |
| 526 | // Holders for allocated values. |
| 527 | std::vector<JvmtiUniquePtr<char[]>> char_buffers; |
| 528 | std::vector<JvmtiUniquePtr<jvmtiParamInfo[]>> param_buffers; |
| 529 | |
| 530 | auto add_extension = [&](ArtJvmtiEvent extension_event_index, |
| 531 | const char* id, |
| 532 | const char* short_description, |
| 533 | const std::vector<CParamInfo>& params) { |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 534 | DCHECK(IsExtensionEvent(extension_event_index)) << static_cast<jint>(extension_event_index); |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 535 | jvmtiExtensionEventInfo event_info; |
| 536 | jvmtiError error; |
| 537 | |
| 538 | event_info.extension_event_index = static_cast<jint>(extension_event_index); |
| 539 | |
| 540 | JvmtiUniquePtr<char[]> id_ptr = CopyString(env, id, &error); |
| 541 | if (id_ptr == nullptr) { |
| 542 | return error; |
| 543 | } |
| 544 | event_info.id = id_ptr.get(); |
| 545 | char_buffers.push_back(std::move(id_ptr)); |
| 546 | |
| 547 | JvmtiUniquePtr<char[]> descr = CopyString(env, short_description, &error); |
| 548 | if (descr == nullptr) { |
| 549 | return error; |
| 550 | } |
| 551 | event_info.short_description = descr.get(); |
| 552 | char_buffers.push_back(std::move(descr)); |
| 553 | |
| 554 | event_info.param_count = params.size(); |
| 555 | if (!params.empty()) { |
| 556 | JvmtiUniquePtr<jvmtiParamInfo[]> params_ptr = |
| 557 | AllocJvmtiUniquePtr<jvmtiParamInfo[]>(env, params.size(), &error); |
| 558 | if (params_ptr == nullptr) { |
| 559 | return error; |
| 560 | } |
| 561 | event_info.params = params_ptr.get(); |
| 562 | param_buffers.push_back(std::move(params_ptr)); |
| 563 | |
| 564 | for (jint i = 0; i != event_info.param_count; ++i) { |
| 565 | event_info.params[i] = params[i].ToParamInfo(env, &char_buffers, &error); |
| 566 | if (error != OK) { |
| 567 | return error; |
| 568 | } |
| 569 | } |
| 570 | } else { |
| 571 | event_info.params = nullptr; |
| 572 | } |
| 573 | |
| 574 | ext_vector.push_back(event_info); |
| 575 | |
| 576 | return ERR(NONE); |
| 577 | }; |
| 578 | |
| 579 | jvmtiError error; |
| 580 | error = add_extension( |
| 581 | ArtJvmtiEvent::kDdmPublishChunk, |
| 582 | "com.android.art.internal.ddm.publish_chunk", |
| 583 | "Called when there is new ddms information that the agent or other clients can use. The" |
| 584 | " agent is given the 'type' of the ddms chunk and a 'data_size' byte-buffer in 'data'." |
| 585 | " The 'data' pointer is only valid for the duration of the publish_chunk event. The agent" |
| 586 | " is responsible for interpreting the information present in the 'data' buffer. This is" |
| 587 | " provided for backwards-compatibility support only. Agents should prefer to use relevant" |
| 588 | " JVMTI events and functions above listening for this event.", |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 589 | { |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 590 | { "jni_env", JVMTI_KIND_IN_PTR, JVMTI_TYPE_JNIENV, false }, |
| 591 | { "type", JVMTI_KIND_IN, JVMTI_TYPE_JINT, false }, |
| 592 | { "data_size", JVMTI_KIND_IN, JVMTI_TYPE_JINT, false }, |
| 593 | { "data", JVMTI_KIND_IN_BUF, JVMTI_TYPE_JBYTE, false }, |
| 594 | }); |
| 595 | if (error != OK) { |
| 596 | return error; |
| 597 | } |
Alex Light | 72d7e94 | 2019-07-23 13:10:20 -0700 | [diff] [blame] | 598 | error = add_extension( |
| 599 | ArtJvmtiEvent::kObsoleteObjectCreated, |
| 600 | "com.android.art.heap.obsolete_object_created", |
| 601 | "Called when an obsolete object is created.\n" |
| 602 | "An object becomes obsolete when, due to some jvmti function call all references to the" |
| 603 | " object are replaced with a reference to a different object. After this call finishes there" |
| 604 | " will be no strong references to the obsolete object anywere. If the object is retrieved" |
| 605 | " using GetObjectsWithTags its type (class) may have changed and any data it contains may" |
| 606 | " have been deleted. This is primarily designed to support memory tracking agents which make" |
| 607 | " use of the ObjectFree and VMObjectAlloc events for tracking. To support this use-case if" |
| 608 | " this event is not being handled it will by default act as though the following code was" |
| 609 | " registered as a handler:\n" |
| 610 | "\n" |
| 611 | " void HandleObsoleteObjectCreated(jvmtiEnv* env, jlong* obsolete_tag, jlong* new_tag) {\n" |
| 612 | " jlong temp = *obsolete_tag;\n" |
| 613 | " *obsolete_tag = *new_tag;\n" |
| 614 | " *new_tag = temp;\n" |
| 615 | " }\n" |
| 616 | "\n" |
| 617 | "Note that this event does not support filtering based on thread. This event has the same" |
| 618 | " restrictions on JNI and JVMTI function calls as the ObjectFree event.\n" |
| 619 | "\n" |
| 620 | "Arguments:\n" |
| 621 | " obsolete_tag: Pointer to the tag the old object (now obsolete) has. Setting the pointer" |
| 622 | " will update the tag value.\n" |
| 623 | " new_tag: Pointer to the tag the new object (replacing the obsolete one) has. Setting the" |
| 624 | " pointer will update the tag value.", |
| 625 | { |
| 626 | { "obsolete_tag", JVMTI_KIND_IN_PTR, JVMTI_TYPE_JLONG, false }, |
| 627 | { "new_tag", JVMTI_KIND_IN_PTR, JVMTI_TYPE_JLONG, false }, |
| 628 | }); |
| 629 | if (error != OK) { |
| 630 | return error; |
| 631 | } |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 632 | art::Runtime* runtime = art::Runtime::Current(); |
| 633 | if (runtime->GetJniIdType() == art::JniIdType::kIndices && |
| 634 | (runtime->GetInstrumentation()->IsForcedInterpretOnly() || runtime->IsJavaDebuggable())) { |
| 635 | error = add_extension( |
| 636 | ArtJvmtiEvent::kStructuralDexFileLoadHook, |
| 637 | "com.android.art.class.structural_dex_file_load_hook", |
| 638 | "Called during class load, after a 'RetransformClasses' call, or after a 'RedefineClasses'" |
| 639 | " call in order to allow the agent to modify the class. This event is called after any" |
| 640 | " non-can_retransform_classes ClassFileLoadHookEvents and before any" |
| 641 | " can_retransform_classes ClassFileLoadHookEvents. The transformations applied are" |
| 642 | " restricted in the same way that transformations applied via the " |
| 643 | " 'com.android.art.class.structurally_redefine_classes' extension function. The arguments" |
| 644 | " to the event are identical to the ones in the ClassFileLoadHook and have the same" |
| 645 | " semantics.", |
| 646 | { |
| 647 | { "jni_env", JVMTI_KIND_IN, JVMTI_TYPE_JNIENV, false }, |
| 648 | { "class_being_redefined", JVMTI_KIND_IN, JVMTI_TYPE_JCLASS, true }, |
| 649 | { "loader", JVMTI_KIND_IN, JVMTI_TYPE_JOBJECT, false }, |
| 650 | { "name", JVMTI_KIND_IN_PTR, JVMTI_TYPE_CCHAR, false }, |
| 651 | { "protection_domain", JVMTI_KIND_IN, JVMTI_TYPE_JOBJECT, true }, |
| 652 | { "dex_data_len", JVMTI_KIND_IN, JVMTI_TYPE_JINT, false }, |
| 653 | { "dex_data", JVMTI_KIND_IN_BUF, JVMTI_TYPE_CCHAR, false }, |
| 654 | { "new_dex_data_len", JVMTI_KIND_OUT, JVMTI_TYPE_JINT, false }, |
| 655 | { "new_dex_data", JVMTI_KIND_ALLOC_BUF, JVMTI_TYPE_CCHAR, true }, |
| 656 | }); |
| 657 | } else { |
| 658 | LOG(INFO) << "debuggable & jni-type indices are required to implement structural " |
| 659 | << "class redefinition extensions."; |
| 660 | } |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 661 | // Copy into output buffer. |
| 662 | |
| 663 | *extension_count_ptr = ext_vector.size(); |
| 664 | JvmtiUniquePtr<jvmtiExtensionEventInfo[]> out_data = |
| 665 | AllocJvmtiUniquePtr<jvmtiExtensionEventInfo[]>(env, ext_vector.size(), &error); |
| 666 | if (out_data == nullptr) { |
| 667 | return error; |
| 668 | } |
| 669 | memcpy(out_data.get(), |
| 670 | ext_vector.data(), |
| 671 | ext_vector.size() * sizeof(jvmtiExtensionEventInfo)); |
| 672 | *extensions = out_data.release(); |
| 673 | |
| 674 | // Release all the buffer holders, we're OK now. |
| 675 | for (auto& holder : char_buffers) { |
| 676 | holder.release(); |
| 677 | } |
| 678 | for (auto& holder : param_buffers) { |
| 679 | holder.release(); |
| 680 | } |
| 681 | |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 682 | return OK; |
| 683 | } |
| 684 | |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 685 | jvmtiError ExtensionUtil::SetExtensionEventCallback(jvmtiEnv* env, |
| 686 | jint extension_event_index, |
| 687 | jvmtiExtensionEvent callback, |
| 688 | EventHandler* event_handler) { |
| 689 | if (!IsExtensionEvent(extension_event_index)) { |
| 690 | return ERR(ILLEGAL_ARGUMENT); |
| 691 | } |
| 692 | ArtJvmTiEnv* art_env = ArtJvmTiEnv::AsArtJvmTiEnv(env); |
| 693 | jvmtiEventMode mode = callback == nullptr ? JVMTI_DISABLE : JVMTI_ENABLE; |
| 694 | // Lock the event_info_mutex_ while we set the event to make sure it isn't lost by a concurrent |
| 695 | // change to the normal callbacks. |
| 696 | { |
| 697 | art::WriterMutexLock lk(art::Thread::Current(), art_env->event_info_mutex_); |
| 698 | if (art_env->event_callbacks.get() == nullptr) { |
| 699 | art_env->event_callbacks.reset(new ArtJvmtiEventCallbacks()); |
| 700 | } |
| 701 | jvmtiError err = art_env->event_callbacks->Set(extension_event_index, callback); |
| 702 | if (err != OK) { |
| 703 | return err; |
| 704 | } |
| 705 | } |
| 706 | return event_handler->SetEvent(art_env, |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 707 | /*thread=*/nullptr, |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 708 | static_cast<ArtJvmtiEvent>(extension_event_index), |
| 709 | mode); |
Alex Light | 3f33c0a | 2017-11-08 10:17:37 -0800 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | } // namespace openjdkjvmti |