blob: 75c314f017f2ce5d9d02d87468f550ccb17bac5c [file] [log] [blame]
Alex Light49948e92016-08-11 15:35:28 -07001/* Copyright (C) 2016 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,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
Alex Light9c20a142016-08-23 15:05:12 -070032#include <string>
33#include <vector>
34
Alex Light49948e92016-08-11 15:35:28 -070035#include <jni.h>
Alex Light9c20a142016-08-23 15:05:12 -070036
Alex Light49948e92016-08-11 15:35:28 -070037#include "openjdkjvmti/jvmti.h"
38
Andreas Gampedb6dcb62016-09-13 09:05:59 -070039#include "art_jvmti.h"
Andreas Gampe77708d92016-10-07 11:48:21 -070040#include "base/mutex.h"
41#include "events-inl.h"
Alex Light49948e92016-08-11 15:35:28 -070042#include "jni_env_ext-inl.h"
Andreas Gampe6dee92e2016-09-12 19:58:13 -070043#include "obj_ptr-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080044#include "object_tagging.h"
Alex Light9c20a142016-08-23 15:05:12 -070045#include "runtime.h"
Andreas Gampe6dee92e2016-09-12 19:58:13 -070046#include "scoped_thread_state_change-inl.h"
Andreas Gampe77708d92016-10-07 11:48:21 -070047#include "thread-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080048#include "thread_list.h"
Andreas Gampee492ae32016-10-28 19:34:57 -070049#include "ti_class.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080050#include "ti_field.h"
Andreas Gampeba8df692016-11-01 10:30:44 -070051#include "ti_heap.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070052#include "ti_method.h"
Alex Lighta01de592016-11-15 10:43:06 -080053#include "ti_redefine.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070054#include "ti_stack.h"
Alex Light9c20a142016-08-23 15:05:12 -070055#include "transform.h"
Alex Light49948e92016-08-11 15:35:28 -070056
57// TODO Remove this at some point by annotating all the methods. It was put in to make the skeleton
58// easier to create.
59#pragma GCC diagnostic ignored "-Wunused-parameter"
60
61namespace openjdkjvmti {
62
Andreas Gampe77708d92016-10-07 11:48:21 -070063EventHandler gEventHandler;
Andreas Gampecc13b222016-10-10 19:09:09 -070064ObjectTagTable gObjectTagTable(&gEventHandler);
Andreas Gampe6dee92e2016-09-12 19:58:13 -070065
Alex Lighte6574242016-08-17 09:56:24 -070066#define ENSURE_NON_NULL(n) \
67 do { \
68 if ((n) == nullptr) { \
69 return ERR(NULL_POINTER); \
70 } \
71 } while (false)
72
Alex Light49948e92016-08-11 15:35:28 -070073class JvmtiFunctions {
74 private:
75 static bool IsValidEnv(jvmtiEnv* env) {
76 return env != nullptr;
77 }
78
Alex Lighte6574242016-08-17 09:56:24 -070079#define ENSURE_VALID_ENV(env) \
80 do { \
81 if (!IsValidEnv(env)) { \
82 return ERR(INVALID_ENVIRONMENT); \
83 } \
84 } while (false)
85
86#define ENSURE_HAS_CAP(env, cap) \
87 do { \
88 ENSURE_VALID_ENV(env); \
89 if (ArtJvmTiEnv::AsArtJvmTiEnv(env)->capabilities.cap != 1) { \
90 return ERR(MUST_POSSESS_CAPABILITY); \
91 } \
92 } while (false)
93
Alex Light49948e92016-08-11 15:35:28 -070094 public:
95 static jvmtiError Allocate(jvmtiEnv* env, jlong size, unsigned char** mem_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -070096 ENSURE_VALID_ENV(env);
97 ENSURE_NON_NULL(mem_ptr);
Alex Light49948e92016-08-11 15:35:28 -070098 if (size < 0) {
99 return ERR(ILLEGAL_ARGUMENT);
100 } else if (size == 0) {
101 *mem_ptr = nullptr;
102 return OK;
103 }
104 *mem_ptr = static_cast<unsigned char*>(malloc(size));
105 return (*mem_ptr != nullptr) ? OK : ERR(OUT_OF_MEMORY);
106 }
107
108 static jvmtiError Deallocate(jvmtiEnv* env, unsigned char* mem) {
Alex Lighte6574242016-08-17 09:56:24 -0700109 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -0700110 if (mem != nullptr) {
111 free(mem);
112 }
113 return OK;
114 }
115
116 static jvmtiError GetThreadState(jvmtiEnv* env, jthread thread, jint* thread_state_ptr) {
117 return ERR(NOT_IMPLEMENTED);
118 }
119
120 static jvmtiError GetCurrentThread(jvmtiEnv* env, jthread* thread_ptr) {
121 return ERR(NOT_IMPLEMENTED);
122 }
123
124 static jvmtiError GetAllThreads(jvmtiEnv* env, jint* threads_count_ptr, jthread** threads_ptr) {
125 return ERR(NOT_IMPLEMENTED);
126 }
127
128 static jvmtiError SuspendThread(jvmtiEnv* env, jthread thread) {
129 return ERR(NOT_IMPLEMENTED);
130 }
131
132 static jvmtiError SuspendThreadList(jvmtiEnv* env,
133 jint request_count,
134 const jthread* request_list,
135 jvmtiError* results) {
136 return ERR(NOT_IMPLEMENTED);
137 }
138
139 static jvmtiError ResumeThread(jvmtiEnv* env, jthread thread) {
140 return ERR(NOT_IMPLEMENTED);
141 }
142
143 static jvmtiError ResumeThreadList(jvmtiEnv* env,
144 jint request_count,
145 const jthread* request_list,
146 jvmtiError* results) {
147 return ERR(NOT_IMPLEMENTED);
148 }
149
150 static jvmtiError StopThread(jvmtiEnv* env, jthread thread, jobject exception) {
151 return ERR(NOT_IMPLEMENTED);
152 }
153
154 static jvmtiError InterruptThread(jvmtiEnv* env, jthread thread) {
155 return ERR(NOT_IMPLEMENTED);
156 }
157
158 static jvmtiError GetThreadInfo(jvmtiEnv* env, jthread thread, jvmtiThreadInfo* info_ptr) {
159 return ERR(NOT_IMPLEMENTED);
160 }
161
162 static jvmtiError GetOwnedMonitorInfo(jvmtiEnv* env,
163 jthread thread,
164 jint* owned_monitor_count_ptr,
165 jobject** owned_monitors_ptr) {
166 return ERR(NOT_IMPLEMENTED);
167 }
168
169 static jvmtiError GetOwnedMonitorStackDepthInfo(jvmtiEnv* env,
170 jthread thread,
171 jint* monitor_info_count_ptr,
172 jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
173 return ERR(NOT_IMPLEMENTED);
174 }
175
176 static jvmtiError GetCurrentContendedMonitor(jvmtiEnv* env,
177 jthread thread,
178 jobject* monitor_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700179 return ERR(NOT_IMPLEMENTED);
Alex Light49948e92016-08-11 15:35:28 -0700180 }
181
182 static jvmtiError RunAgentThread(jvmtiEnv* env,
183 jthread thread,
184 jvmtiStartFunction proc,
185 const void* arg,
186 jint priority) {
187 return ERR(NOT_IMPLEMENTED);
188 }
189
190 static jvmtiError SetThreadLocalStorage(jvmtiEnv* env, jthread thread, const void* data) {
191 return ERR(NOT_IMPLEMENTED);
192 }
193
194 static jvmtiError GetThreadLocalStorage(jvmtiEnv* env, jthread thread, void** data_ptr) {
195 return ERR(NOT_IMPLEMENTED);
196 }
197
198 static jvmtiError GetTopThreadGroups(jvmtiEnv* env,
199 jint* group_count_ptr,
200 jthreadGroup** groups_ptr) {
201 return ERR(NOT_IMPLEMENTED);
202 }
203
204 static jvmtiError GetThreadGroupInfo(jvmtiEnv* env,
205 jthreadGroup group,
206 jvmtiThreadGroupInfo* info_ptr) {
207 return ERR(NOT_IMPLEMENTED);
208 }
209
210 static jvmtiError GetThreadGroupChildren(jvmtiEnv* env,
211 jthreadGroup group,
212 jint* thread_count_ptr,
213 jthread** threads_ptr,
214 jint* group_count_ptr,
215 jthreadGroup** groups_ptr) {
216 return ERR(NOT_IMPLEMENTED);
217 }
218
219 static jvmtiError GetStackTrace(jvmtiEnv* env,
220 jthread thread,
221 jint start_depth,
222 jint max_frame_count,
223 jvmtiFrameInfo* frame_buffer,
224 jint* count_ptr) {
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700225 return StackUtil::GetStackTrace(env,
226 thread,
227 start_depth,
228 max_frame_count,
229 frame_buffer,
230 count_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700231 }
232
233 static jvmtiError GetAllStackTraces(jvmtiEnv* env,
234 jint max_frame_count,
235 jvmtiStackInfo** stack_info_ptr,
236 jint* thread_count_ptr) {
237 return ERR(NOT_IMPLEMENTED);
238 }
239
240 static jvmtiError GetThreadListStackTraces(jvmtiEnv* env,
241 jint thread_count,
242 const jthread* thread_list,
243 jint max_frame_count,
244 jvmtiStackInfo** stack_info_ptr) {
245 return ERR(NOT_IMPLEMENTED);
246 }
247
248 static jvmtiError GetFrameCount(jvmtiEnv* env, jthread thread, jint* count_ptr) {
249 return ERR(NOT_IMPLEMENTED);
250 }
251
252 static jvmtiError PopFrame(jvmtiEnv* env, jthread thread) {
253 return ERR(NOT_IMPLEMENTED);
254 }
255
256 static jvmtiError GetFrameLocation(jvmtiEnv* env,
257 jthread thread,
258 jint depth,
259 jmethodID* method_ptr,
260 jlocation* location_ptr) {
261 return ERR(NOT_IMPLEMENTED);
262 }
263
264 static jvmtiError NotifyFramePop(jvmtiEnv* env, jthread thread, jint depth) {
265 return ERR(NOT_IMPLEMENTED);
266 }
267
268 static jvmtiError ForceEarlyReturnObject(jvmtiEnv* env, jthread thread, jobject value) {
269 return ERR(NOT_IMPLEMENTED);
270 }
271
272 static jvmtiError ForceEarlyReturnInt(jvmtiEnv* env, jthread thread, jint value) {
273 return ERR(NOT_IMPLEMENTED);
274 }
275
276 static jvmtiError ForceEarlyReturnLong(jvmtiEnv* env, jthread thread, jlong value) {
277 return ERR(NOT_IMPLEMENTED);
278 }
279
280 static jvmtiError ForceEarlyReturnFloat(jvmtiEnv* env, jthread thread, jfloat value) {
281 return ERR(NOT_IMPLEMENTED);
282 }
283
284 static jvmtiError ForceEarlyReturnDouble(jvmtiEnv* env, jthread thread, jdouble value) {
285 return ERR(NOT_IMPLEMENTED);
286 }
287
288 static jvmtiError ForceEarlyReturnVoid(jvmtiEnv* env, jthread thread) {
289 return ERR(NOT_IMPLEMENTED);
290 }
291
292 static jvmtiError FollowReferences(jvmtiEnv* env,
293 jint heap_filter,
294 jclass klass,
295 jobject initial_object,
296 const jvmtiHeapCallbacks* callbacks,
297 const void* user_data) {
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700298 HeapUtil heap_util(&gObjectTagTable);
299 return heap_util.FollowReferences(env,
300 heap_filter,
301 klass,
302 initial_object,
303 callbacks,
304 user_data);
Alex Light49948e92016-08-11 15:35:28 -0700305 }
306
307 static jvmtiError IterateThroughHeap(jvmtiEnv* env,
308 jint heap_filter,
309 jclass klass,
310 const jvmtiHeapCallbacks* callbacks,
311 const void* user_data) {
Alex Lighte6574242016-08-17 09:56:24 -0700312 ENSURE_HAS_CAP(env, can_tag_objects);
Andreas Gampee54d9922016-10-11 19:55:37 -0700313 HeapUtil heap_util(&gObjectTagTable);
314 return heap_util.IterateThroughHeap(env, heap_filter, klass, callbacks, user_data);
Alex Light49948e92016-08-11 15:35:28 -0700315 }
316
317 static jvmtiError GetTag(jvmtiEnv* env, jobject object, jlong* tag_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700318 ENSURE_HAS_CAP(env, can_tag_objects);
Andreas Gampe6dee92e2016-09-12 19:58:13 -0700319
320 JNIEnv* jni_env = GetJniEnv(env);
321 if (jni_env == nullptr) {
322 return ERR(INTERNAL);
323 }
324
325 art::ScopedObjectAccess soa(jni_env);
326 art::ObjPtr<art::mirror::Object> obj = soa.Decode<art::mirror::Object>(object);
327 if (!gObjectTagTable.GetTag(obj.Ptr(), tag_ptr)) {
328 *tag_ptr = 0;
329 }
330
331 return ERR(NONE);
Alex Light49948e92016-08-11 15:35:28 -0700332 }
333
334 static jvmtiError SetTag(jvmtiEnv* env, jobject object, jlong tag) {
Alex Lighte6574242016-08-17 09:56:24 -0700335 ENSURE_HAS_CAP(env, can_tag_objects);
336
Andreas Gampe6dee92e2016-09-12 19:58:13 -0700337 if (object == nullptr) {
338 return ERR(NULL_POINTER);
339 }
340
341 JNIEnv* jni_env = GetJniEnv(env);
342 if (jni_env == nullptr) {
343 return ERR(INTERNAL);
344 }
345
346 art::ScopedObjectAccess soa(jni_env);
347 art::ObjPtr<art::mirror::Object> obj = soa.Decode<art::mirror::Object>(object);
Andreas Gampee54eee12016-10-20 19:03:58 -0700348 gObjectTagTable.Set(obj.Ptr(), tag);
Andreas Gampe6dee92e2016-09-12 19:58:13 -0700349
350 return ERR(NONE);
Alex Light49948e92016-08-11 15:35:28 -0700351 }
352
353 static jvmtiError GetObjectsWithTags(jvmtiEnv* env,
354 jint tag_count,
355 const jlong* tags,
356 jint* count_ptr,
357 jobject** object_result_ptr,
358 jlong** tag_result_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700359 ENSURE_HAS_CAP(env, can_tag_objects);
360
Andreas Gampe5e6046b2016-10-25 12:05:53 -0700361 JNIEnv* jni_env = GetJniEnv(env);
362 if (jni_env == nullptr) {
363 return ERR(INTERNAL);
364 }
365
366 art::ScopedObjectAccess soa(jni_env);
367 return gObjectTagTable.GetTaggedObjects(env,
368 tag_count,
369 tags,
370 count_ptr,
371 object_result_ptr,
372 tag_result_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700373 }
374
375 static jvmtiError ForceGarbageCollection(jvmtiEnv* env) {
Andreas Gampe8da6d032016-10-31 19:31:03 -0700376 return HeapUtil::ForceGarbageCollection(env);
Alex Light49948e92016-08-11 15:35:28 -0700377 }
378
379 static jvmtiError IterateOverObjectsReachableFromObject(
380 jvmtiEnv* env,
381 jobject object,
382 jvmtiObjectReferenceCallback object_reference_callback,
383 const void* user_data) {
384 return ERR(NOT_IMPLEMENTED);
385 }
386
387 static jvmtiError IterateOverReachableObjects(jvmtiEnv* env,
388 jvmtiHeapRootCallback heap_root_callback,
389 jvmtiStackReferenceCallback stack_ref_callback,
390 jvmtiObjectReferenceCallback object_ref_callback,
391 const void* user_data) {
392 return ERR(NOT_IMPLEMENTED);
393 }
394
395 static jvmtiError IterateOverHeap(jvmtiEnv* env,
396 jvmtiHeapObjectFilter object_filter,
397 jvmtiHeapObjectCallback heap_object_callback,
398 const void* user_data) {
399 return ERR(NOT_IMPLEMENTED);
400 }
401
402 static jvmtiError IterateOverInstancesOfClass(jvmtiEnv* env,
403 jclass klass,
404 jvmtiHeapObjectFilter object_filter,
405 jvmtiHeapObjectCallback heap_object_callback,
406 const void* user_data) {
407 return ERR(NOT_IMPLEMENTED);
408 }
409
410 static jvmtiError GetLocalObject(jvmtiEnv* env,
411 jthread thread,
412 jint depth,
413 jint slot,
414 jobject* value_ptr) {
415 return ERR(NOT_IMPLEMENTED);
416 }
417
418 static jvmtiError GetLocalInstance(jvmtiEnv* env,
419 jthread thread,
420 jint depth,
421 jobject* value_ptr) {
422 return ERR(NOT_IMPLEMENTED);
423 }
424
425 static jvmtiError GetLocalInt(jvmtiEnv* env,
426 jthread thread,
427 jint depth,
428 jint slot,
429 jint* value_ptr) {
430 return ERR(NOT_IMPLEMENTED);
431 }
432
433 static jvmtiError GetLocalLong(jvmtiEnv* env,
434 jthread thread,
435 jint depth,
436 jint slot,
437 jlong* value_ptr) {
438 return ERR(NOT_IMPLEMENTED);
439 }
440
441 static jvmtiError GetLocalFloat(jvmtiEnv* env,
442 jthread thread,
443 jint depth,
444 jint slot,
445 jfloat* value_ptr) {
446 return ERR(NOT_IMPLEMENTED);
447 }
448
449 static jvmtiError GetLocalDouble(jvmtiEnv* env,
450 jthread thread,
451 jint depth,
452 jint slot,
453 jdouble* value_ptr) {
454 return ERR(NOT_IMPLEMENTED);
455 }
456
457 static jvmtiError SetLocalObject(jvmtiEnv* env,
458 jthread thread,
459 jint depth,
460 jint slot,
461 jobject value) {
462 return ERR(NOT_IMPLEMENTED);
463 }
464
465 static jvmtiError SetLocalInt(jvmtiEnv* env,
466 jthread thread,
467 jint depth,
468 jint slot,
469 jint value) {
470 return ERR(NOT_IMPLEMENTED);
471 }
472
473 static jvmtiError SetLocalLong(jvmtiEnv* env,
474 jthread thread,
475 jint depth,
476 jint slot,
477 jlong value) {
478 return ERR(NOT_IMPLEMENTED);
479 }
480
481 static jvmtiError SetLocalFloat(jvmtiEnv* env,
482 jthread thread,
483 jint depth,
484 jint slot,
485 jfloat value) {
486 return ERR(NOT_IMPLEMENTED);
487 }
488
489 static jvmtiError SetLocalDouble(jvmtiEnv* env,
490 jthread thread,
491 jint depth,
492 jint slot,
493 jdouble value) {
494 return ERR(NOT_IMPLEMENTED);
495 }
496
497 static jvmtiError SetBreakpoint(jvmtiEnv* env, jmethodID method, jlocation location) {
498 return ERR(NOT_IMPLEMENTED);
499 }
500
501 static jvmtiError ClearBreakpoint(jvmtiEnv* env, jmethodID method, jlocation location) {
502 return ERR(NOT_IMPLEMENTED);
503 }
504
505 static jvmtiError SetFieldAccessWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
506 return ERR(NOT_IMPLEMENTED);
507 }
508
509 static jvmtiError ClearFieldAccessWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
510 return ERR(NOT_IMPLEMENTED);
511 }
512
513 static jvmtiError SetFieldModificationWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
514 return ERR(NOT_IMPLEMENTED);
515 }
516
517 static jvmtiError ClearFieldModificationWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
518 return ERR(NOT_IMPLEMENTED);
519 }
520
521 static jvmtiError GetLoadedClasses(jvmtiEnv* env, jint* class_count_ptr, jclass** classes_ptr) {
Andreas Gampeaa8b60c2016-10-12 12:51:25 -0700522 HeapUtil heap_util(&gObjectTagTable);
523 return heap_util.GetLoadedClasses(env, class_count_ptr, classes_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700524 }
525
526 static jvmtiError GetClassLoaderClasses(jvmtiEnv* env,
527 jobject initiating_loader,
528 jint* class_count_ptr,
529 jclass** classes_ptr) {
530 return ERR(NOT_IMPLEMENTED);
531 }
532
533 static jvmtiError GetClassSignature(jvmtiEnv* env,
534 jclass klass,
535 char** signature_ptr,
536 char** generic_ptr) {
Andreas Gampee492ae32016-10-28 19:34:57 -0700537 return ClassUtil::GetClassSignature(env, klass, signature_ptr, generic_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700538 }
539
540 static jvmtiError GetClassStatus(jvmtiEnv* env, jclass klass, jint* status_ptr) {
Andreas Gampeff9d2092017-01-06 09:12:49 -0800541 return ClassUtil::GetClassStatus(env, klass, status_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700542 }
543
544 static jvmtiError GetSourceFileName(jvmtiEnv* env, jclass klass, char** source_name_ptr) {
545 return ERR(NOT_IMPLEMENTED);
546 }
547
548 static jvmtiError GetClassModifiers(jvmtiEnv* env, jclass klass, jint* modifiers_ptr) {
Andreas Gampe64013e52017-01-06 13:07:19 -0800549 return ClassUtil::GetClassModifiers(env, klass, modifiers_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700550 }
551
552 static jvmtiError GetClassMethods(jvmtiEnv* env,
553 jclass klass,
554 jint* method_count_ptr,
555 jmethodID** methods_ptr) {
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800556 return ClassUtil::GetClassMethods(env, klass, method_count_ptr, methods_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700557 }
558
559 static jvmtiError GetClassFields(jvmtiEnv* env,
560 jclass klass,
561 jint* field_count_ptr,
562 jfieldID** fields_ptr) {
Andreas Gampeac587272017-01-05 15:21:34 -0800563 return ClassUtil::GetClassFields(env, klass, field_count_ptr, fields_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700564 }
565
566 static jvmtiError GetImplementedInterfaces(jvmtiEnv* env,
567 jclass klass,
568 jint* interface_count_ptr,
569 jclass** interfaces_ptr) {
570 return ERR(NOT_IMPLEMENTED);
571 }
572
573 static jvmtiError GetClassVersionNumbers(jvmtiEnv* env,
574 jclass klass,
575 jint* minor_version_ptr,
576 jint* major_version_ptr) {
577 return ERR(NOT_IMPLEMENTED);
578 }
579
580 static jvmtiError GetConstantPool(jvmtiEnv* env,
581 jclass klass,
582 jint* constant_pool_count_ptr,
583 jint* constant_pool_byte_count_ptr,
584 unsigned char** constant_pool_bytes_ptr) {
585 return ERR(NOT_IMPLEMENTED);
586 }
587
588 static jvmtiError IsInterface(jvmtiEnv* env, jclass klass, jboolean* is_interface_ptr) {
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800589 return ClassUtil::IsInterface(env, klass, is_interface_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700590 }
591
592 static jvmtiError IsArrayClass(jvmtiEnv* env,
593 jclass klass,
594 jboolean* is_array_class_ptr) {
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800595 return ClassUtil::IsArrayClass(env, klass, is_array_class_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700596 }
597
598 static jvmtiError IsModifiableClass(jvmtiEnv* env,
599 jclass klass,
600 jboolean* is_modifiable_class_ptr) {
601 return ERR(NOT_IMPLEMENTED);
602 }
603
604 static jvmtiError GetClassLoader(jvmtiEnv* env, jclass klass, jobject* classloader_ptr) {
605 return ERR(NOT_IMPLEMENTED);
606 }
607
608 static jvmtiError GetSourceDebugExtension(jvmtiEnv* env,
609 jclass klass,
610 char** source_debug_extension_ptr) {
611 return ERR(NOT_IMPLEMENTED);
612 }
613
614 static jvmtiError RetransformClasses(jvmtiEnv* env, jint class_count, const jclass* classes) {
615 return ERR(NOT_IMPLEMENTED);
616 }
617
618 static jvmtiError RedefineClasses(jvmtiEnv* env,
619 jint class_count,
620 const jvmtiClassDefinition* class_definitions) {
621 return ERR(NOT_IMPLEMENTED);
622 }
623
624 static jvmtiError GetObjectSize(jvmtiEnv* env, jobject object, jlong* size_ptr) {
625 return ERR(NOT_IMPLEMENTED);
626 }
627
628 static jvmtiError GetObjectHashCode(jvmtiEnv* env, jobject object, jint* hash_code_ptr) {
629 return ERR(NOT_IMPLEMENTED);
630 }
631
632 static jvmtiError GetObjectMonitorUsage(jvmtiEnv* env,
633 jobject object,
634 jvmtiMonitorUsage* info_ptr) {
635 return ERR(NOT_IMPLEMENTED);
636 }
637
638 static jvmtiError GetFieldName(jvmtiEnv* env,
639 jclass klass,
640 jfieldID field,
641 char** name_ptr,
642 char** signature_ptr,
643 char** generic_ptr) {
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800644 return FieldUtil::GetFieldName(env, klass, field, name_ptr, signature_ptr, generic_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700645 }
646
647 static jvmtiError GetFieldDeclaringClass(jvmtiEnv* env,
648 jclass klass,
649 jfieldID field,
650 jclass* declaring_class_ptr) {
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800651 return FieldUtil::GetFieldDeclaringClass(env, klass, field, declaring_class_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700652 }
653
654 static jvmtiError GetFieldModifiers(jvmtiEnv* env,
655 jclass klass,
656 jfieldID field,
657 jint* modifiers_ptr) {
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800658 return FieldUtil::GetFieldModifiers(env, klass, field, modifiers_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700659 }
660
661 static jvmtiError IsFieldSynthetic(jvmtiEnv* env,
662 jclass klass,
663 jfieldID field,
664 jboolean* is_synthetic_ptr) {
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800665 return FieldUtil::IsFieldSynthetic(env, klass, field, is_synthetic_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700666 }
667
668 static jvmtiError GetMethodName(jvmtiEnv* env,
669 jmethodID method,
670 char** name_ptr,
671 char** signature_ptr,
672 char** generic_ptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700673 return MethodUtil::GetMethodName(env, method, name_ptr, signature_ptr, generic_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700674 }
675
676 static jvmtiError GetMethodDeclaringClass(jvmtiEnv* env,
677 jmethodID method,
678 jclass* declaring_class_ptr) {
Andreas Gampe368a2082016-10-28 17:33:13 -0700679 return MethodUtil::GetMethodDeclaringClass(env, method, declaring_class_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700680 }
681
682 static jvmtiError GetMethodModifiers(jvmtiEnv* env,
683 jmethodID method,
684 jint* modifiers_ptr) {
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700685 return MethodUtil::GetMethodModifiers(env, method, modifiers_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700686 }
687
688 static jvmtiError GetMaxLocals(jvmtiEnv* env,
689 jmethodID method,
690 jint* max_ptr) {
691 return ERR(NOT_IMPLEMENTED);
692 }
693
694 static jvmtiError GetArgumentsSize(jvmtiEnv* env,
695 jmethodID method,
696 jint* size_ptr) {
697 return ERR(NOT_IMPLEMENTED);
698 }
699
700 static jvmtiError GetLineNumberTable(jvmtiEnv* env,
701 jmethodID method,
702 jint* entry_count_ptr,
703 jvmtiLineNumberEntry** table_ptr) {
Andreas Gampeda3e5612016-12-13 19:00:53 -0800704 return MethodUtil::GetLineNumberTable(env, method, entry_count_ptr, table_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700705 }
706
707 static jvmtiError GetMethodLocation(jvmtiEnv* env,
708 jmethodID method,
709 jlocation* start_location_ptr,
710 jlocation* end_location_ptr) {
711 return ERR(NOT_IMPLEMENTED);
712 }
713
714 static jvmtiError GetLocalVariableTable(jvmtiEnv* env,
715 jmethodID method,
716 jint* entry_count_ptr,
717 jvmtiLocalVariableEntry** table_ptr) {
718 return ERR(NOT_IMPLEMENTED);
719 }
720
721 static jvmtiError GetBytecodes(jvmtiEnv* env,
722 jmethodID method,
723 jint* bytecode_count_ptr,
724 unsigned char** bytecodes_ptr) {
725 return ERR(NOT_IMPLEMENTED);
726 }
727
728 static jvmtiError IsMethodNative(jvmtiEnv* env, jmethodID method, jboolean* is_native_ptr) {
729 return ERR(NOT_IMPLEMENTED);
730 }
731
732 static jvmtiError IsMethodSynthetic(jvmtiEnv* env, jmethodID method, jboolean* is_synthetic_ptr) {
733 return ERR(NOT_IMPLEMENTED);
734 }
735
736 static jvmtiError IsMethodObsolete(jvmtiEnv* env, jmethodID method, jboolean* is_obsolete_ptr) {
737 return ERR(NOT_IMPLEMENTED);
738 }
739
740 static jvmtiError SetNativeMethodPrefix(jvmtiEnv* env, const char* prefix) {
741 return ERR(NOT_IMPLEMENTED);
742 }
743
744 static jvmtiError SetNativeMethodPrefixes(jvmtiEnv* env, jint prefix_count, char** prefixes) {
745 return ERR(NOT_IMPLEMENTED);
746 }
747
748 static jvmtiError CreateRawMonitor(jvmtiEnv* env, const char* name, jrawMonitorID* monitor_ptr) {
749 return ERR(NOT_IMPLEMENTED);
750 }
751
752 static jvmtiError DestroyRawMonitor(jvmtiEnv* env, jrawMonitorID monitor) {
753 return ERR(NOT_IMPLEMENTED);
754 }
755
756 static jvmtiError RawMonitorEnter(jvmtiEnv* env, jrawMonitorID monitor) {
757 return ERR(NOT_IMPLEMENTED);
758 }
759
760 static jvmtiError RawMonitorExit(jvmtiEnv* env, jrawMonitorID monitor) {
761 return ERR(NOT_IMPLEMENTED);
762 }
763
764 static jvmtiError RawMonitorWait(jvmtiEnv* env, jrawMonitorID monitor, jlong millis) {
765 return ERR(NOT_IMPLEMENTED);
766 }
767
768 static jvmtiError RawMonitorNotify(jvmtiEnv* env, jrawMonitorID monitor) {
769 return ERR(NOT_IMPLEMENTED);
770 }
771
772 static jvmtiError RawMonitorNotifyAll(jvmtiEnv* env, jrawMonitorID monitor) {
773 return ERR(NOT_IMPLEMENTED);
774 }
775
776 static jvmtiError SetJNIFunctionTable(jvmtiEnv* env, const jniNativeInterface* function_table) {
777 return ERR(NOT_IMPLEMENTED);
778 }
779
780 static jvmtiError GetJNIFunctionTable(jvmtiEnv* env, jniNativeInterface** function_table) {
781 return ERR(NOT_IMPLEMENTED);
782 }
783
Andreas Gampe77708d92016-10-07 11:48:21 -0700784 // TODO: This will require locking, so that an agent can't remove callbacks when we're dispatching
785 // an event.
Alex Light49948e92016-08-11 15:35:28 -0700786 static jvmtiError SetEventCallbacks(jvmtiEnv* env,
787 const jvmtiEventCallbacks* callbacks,
788 jint size_of_callbacks) {
Alex Lighte6574242016-08-17 09:56:24 -0700789 ENSURE_VALID_ENV(env);
Andreas Gampe77708d92016-10-07 11:48:21 -0700790 if (size_of_callbacks < 0) {
791 return ERR(ILLEGAL_ARGUMENT);
792 }
793
794 if (callbacks == nullptr) {
795 ArtJvmTiEnv::AsArtJvmTiEnv(env)->event_callbacks.reset();
796 return ERR(NONE);
797 }
798
799 std::unique_ptr<jvmtiEventCallbacks> tmp(new jvmtiEventCallbacks());
800 memset(tmp.get(), 0, sizeof(jvmtiEventCallbacks));
801 size_t copy_size = std::min(sizeof(jvmtiEventCallbacks),
802 static_cast<size_t>(size_of_callbacks));
803 copy_size = art::RoundDown(copy_size, sizeof(void*));
804 memcpy(tmp.get(), callbacks, copy_size);
805
806 ArtJvmTiEnv::AsArtJvmTiEnv(env)->event_callbacks = std::move(tmp);
807
808 return ERR(NONE);
Alex Light49948e92016-08-11 15:35:28 -0700809 }
810
811 static jvmtiError SetEventNotificationMode(jvmtiEnv* env,
812 jvmtiEventMode mode,
813 jvmtiEvent event_type,
814 jthread event_thread,
815 ...) {
Alex Lighte6574242016-08-17 09:56:24 -0700816 ENSURE_VALID_ENV(env);
817 // TODO: Check for capabilities.
Andreas Gampe77708d92016-10-07 11:48:21 -0700818 art::Thread* art_thread = nullptr;
819 if (event_thread != nullptr) {
820 // TODO: Need non-aborting call here, to return JVMTI_ERROR_INVALID_THREAD.
821 art::ScopedObjectAccess soa(art::Thread::Current());
822 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
823 art_thread = art::Thread::FromManagedThread(soa, event_thread);
824
825 if (art_thread == nullptr || // The thread hasn't been started or is already dead.
826 art_thread->IsStillStarting()) {
827 // TODO: We may want to let the EventHandler know, so it could clean up masks, potentially.
828 return ERR(THREAD_NOT_ALIVE);
829 }
830 }
831
832 return gEventHandler.SetEvent(ArtJvmTiEnv::AsArtJvmTiEnv(env), art_thread, event_type, mode);
Alex Light49948e92016-08-11 15:35:28 -0700833 }
834
835 static jvmtiError GenerateEvents(jvmtiEnv* env, jvmtiEvent event_type) {
836 return ERR(NOT_IMPLEMENTED);
837 }
838
839 static jvmtiError GetExtensionFunctions(jvmtiEnv* env,
840 jint* extension_count_ptr,
841 jvmtiExtensionFunctionInfo** extensions) {
842 return ERR(NOT_IMPLEMENTED);
843 }
844
845 static jvmtiError GetExtensionEvents(jvmtiEnv* env,
846 jint* extension_count_ptr,
847 jvmtiExtensionEventInfo** extensions) {
848 return ERR(NOT_IMPLEMENTED);
849 }
850
851 static jvmtiError SetExtensionEventCallback(jvmtiEnv* env,
852 jint extension_event_index,
853 jvmtiExtensionEvent callback) {
854 return ERR(NOT_IMPLEMENTED);
855 }
856
857 static jvmtiError GetPotentialCapabilities(jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700858 ENSURE_VALID_ENV(env);
859 ENSURE_NON_NULL(capabilities_ptr);
860 *capabilities_ptr = kPotentialCapabilities;
861 return OK;
Alex Light49948e92016-08-11 15:35:28 -0700862 }
863
864 static jvmtiError AddCapabilities(jvmtiEnv* env, const jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700865 ENSURE_VALID_ENV(env);
866 ENSURE_NON_NULL(capabilities_ptr);
867 ArtJvmTiEnv* art_env = static_cast<ArtJvmTiEnv*>(env);
868 jvmtiError ret = OK;
869#define ADD_CAPABILITY(e) \
870 do { \
871 if (capabilities_ptr->e == 1) { \
872 if (kPotentialCapabilities.e == 1) { \
873 art_env->capabilities.e = 1;\
874 } else { \
875 ret = ERR(NOT_AVAILABLE); \
876 } \
877 } \
878 } while (false)
879
880 ADD_CAPABILITY(can_tag_objects);
881 ADD_CAPABILITY(can_generate_field_modification_events);
882 ADD_CAPABILITY(can_generate_field_access_events);
883 ADD_CAPABILITY(can_get_bytecodes);
884 ADD_CAPABILITY(can_get_synthetic_attribute);
885 ADD_CAPABILITY(can_get_owned_monitor_info);
886 ADD_CAPABILITY(can_get_current_contended_monitor);
887 ADD_CAPABILITY(can_get_monitor_info);
888 ADD_CAPABILITY(can_pop_frame);
889 ADD_CAPABILITY(can_redefine_classes);
890 ADD_CAPABILITY(can_signal_thread);
891 ADD_CAPABILITY(can_get_source_file_name);
892 ADD_CAPABILITY(can_get_line_numbers);
893 ADD_CAPABILITY(can_get_source_debug_extension);
894 ADD_CAPABILITY(can_access_local_variables);
895 ADD_CAPABILITY(can_maintain_original_method_order);
896 ADD_CAPABILITY(can_generate_single_step_events);
897 ADD_CAPABILITY(can_generate_exception_events);
898 ADD_CAPABILITY(can_generate_frame_pop_events);
899 ADD_CAPABILITY(can_generate_breakpoint_events);
900 ADD_CAPABILITY(can_suspend);
901 ADD_CAPABILITY(can_redefine_any_class);
902 ADD_CAPABILITY(can_get_current_thread_cpu_time);
903 ADD_CAPABILITY(can_get_thread_cpu_time);
904 ADD_CAPABILITY(can_generate_method_entry_events);
905 ADD_CAPABILITY(can_generate_method_exit_events);
906 ADD_CAPABILITY(can_generate_all_class_hook_events);
907 ADD_CAPABILITY(can_generate_compiled_method_load_events);
908 ADD_CAPABILITY(can_generate_monitor_events);
909 ADD_CAPABILITY(can_generate_vm_object_alloc_events);
910 ADD_CAPABILITY(can_generate_native_method_bind_events);
911 ADD_CAPABILITY(can_generate_garbage_collection_events);
912 ADD_CAPABILITY(can_generate_object_free_events);
913 ADD_CAPABILITY(can_force_early_return);
914 ADD_CAPABILITY(can_get_owned_monitor_stack_depth_info);
915 ADD_CAPABILITY(can_get_constant_pool);
916 ADD_CAPABILITY(can_set_native_method_prefix);
917 ADD_CAPABILITY(can_retransform_classes);
918 ADD_CAPABILITY(can_retransform_any_class);
919 ADD_CAPABILITY(can_generate_resource_exhaustion_heap_events);
920 ADD_CAPABILITY(can_generate_resource_exhaustion_threads_events);
921#undef ADD_CAPABILITY
922 return ret;
Alex Light49948e92016-08-11 15:35:28 -0700923 }
924
925 static jvmtiError RelinquishCapabilities(jvmtiEnv* env,
926 const jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700927 ENSURE_VALID_ENV(env);
928 ENSURE_NON_NULL(capabilities_ptr);
929 ArtJvmTiEnv* art_env = reinterpret_cast<ArtJvmTiEnv*>(env);
930#define DEL_CAPABILITY(e) \
931 do { \
932 if (capabilities_ptr->e == 1) { \
933 art_env->capabilities.e = 0;\
934 } \
935 } while (false)
936
937 DEL_CAPABILITY(can_tag_objects);
938 DEL_CAPABILITY(can_generate_field_modification_events);
939 DEL_CAPABILITY(can_generate_field_access_events);
940 DEL_CAPABILITY(can_get_bytecodes);
941 DEL_CAPABILITY(can_get_synthetic_attribute);
942 DEL_CAPABILITY(can_get_owned_monitor_info);
943 DEL_CAPABILITY(can_get_current_contended_monitor);
944 DEL_CAPABILITY(can_get_monitor_info);
945 DEL_CAPABILITY(can_pop_frame);
946 DEL_CAPABILITY(can_redefine_classes);
947 DEL_CAPABILITY(can_signal_thread);
948 DEL_CAPABILITY(can_get_source_file_name);
949 DEL_CAPABILITY(can_get_line_numbers);
950 DEL_CAPABILITY(can_get_source_debug_extension);
951 DEL_CAPABILITY(can_access_local_variables);
952 DEL_CAPABILITY(can_maintain_original_method_order);
953 DEL_CAPABILITY(can_generate_single_step_events);
954 DEL_CAPABILITY(can_generate_exception_events);
955 DEL_CAPABILITY(can_generate_frame_pop_events);
956 DEL_CAPABILITY(can_generate_breakpoint_events);
957 DEL_CAPABILITY(can_suspend);
958 DEL_CAPABILITY(can_redefine_any_class);
959 DEL_CAPABILITY(can_get_current_thread_cpu_time);
960 DEL_CAPABILITY(can_get_thread_cpu_time);
961 DEL_CAPABILITY(can_generate_method_entry_events);
962 DEL_CAPABILITY(can_generate_method_exit_events);
963 DEL_CAPABILITY(can_generate_all_class_hook_events);
964 DEL_CAPABILITY(can_generate_compiled_method_load_events);
965 DEL_CAPABILITY(can_generate_monitor_events);
966 DEL_CAPABILITY(can_generate_vm_object_alloc_events);
967 DEL_CAPABILITY(can_generate_native_method_bind_events);
968 DEL_CAPABILITY(can_generate_garbage_collection_events);
969 DEL_CAPABILITY(can_generate_object_free_events);
970 DEL_CAPABILITY(can_force_early_return);
971 DEL_CAPABILITY(can_get_owned_monitor_stack_depth_info);
972 DEL_CAPABILITY(can_get_constant_pool);
973 DEL_CAPABILITY(can_set_native_method_prefix);
974 DEL_CAPABILITY(can_retransform_classes);
975 DEL_CAPABILITY(can_retransform_any_class);
976 DEL_CAPABILITY(can_generate_resource_exhaustion_heap_events);
977 DEL_CAPABILITY(can_generate_resource_exhaustion_threads_events);
978#undef DEL_CAPABILITY
979 return OK;
Alex Light49948e92016-08-11 15:35:28 -0700980 }
981
982 static jvmtiError GetCapabilities(jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700983 ENSURE_VALID_ENV(env);
984 ENSURE_NON_NULL(capabilities_ptr);
985 ArtJvmTiEnv* artenv = reinterpret_cast<ArtJvmTiEnv*>(env);
986 *capabilities_ptr = artenv->capabilities;
987 return OK;
Alex Light49948e92016-08-11 15:35:28 -0700988 }
989
990 static jvmtiError GetCurrentThreadCpuTimerInfo(jvmtiEnv* env, jvmtiTimerInfo* info_ptr) {
991 return ERR(NOT_IMPLEMENTED);
992 }
993
994 static jvmtiError GetCurrentThreadCpuTime(jvmtiEnv* env, jlong* nanos_ptr) {
995 return ERR(NOT_IMPLEMENTED);
996 }
997
998 static jvmtiError GetThreadCpuTimerInfo(jvmtiEnv* env, jvmtiTimerInfo* info_ptr) {
999 return ERR(NOT_IMPLEMENTED);
1000 }
1001
1002 static jvmtiError GetThreadCpuTime(jvmtiEnv* env, jthread thread, jlong* nanos_ptr) {
1003 return ERR(NOT_IMPLEMENTED);
1004 }
1005
1006 static jvmtiError GetTimerInfo(jvmtiEnv* env, jvmtiTimerInfo* info_ptr) {
1007 return ERR(NOT_IMPLEMENTED);
1008 }
1009
1010 static jvmtiError GetTime(jvmtiEnv* env, jlong* nanos_ptr) {
1011 return ERR(NOT_IMPLEMENTED);
1012 }
1013
1014 static jvmtiError GetAvailableProcessors(jvmtiEnv* env, jint* processor_count_ptr) {
1015 return ERR(NOT_IMPLEMENTED);
1016 }
1017
1018 static jvmtiError AddToBootstrapClassLoaderSearch(jvmtiEnv* env, const char* segment) {
1019 return ERR(NOT_IMPLEMENTED);
1020 }
1021
1022 static jvmtiError AddToSystemClassLoaderSearch(jvmtiEnv* env, const char* segment) {
1023 return ERR(NOT_IMPLEMENTED);
1024 }
1025
1026 static jvmtiError GetSystemProperties(jvmtiEnv* env, jint* count_ptr, char*** property_ptr) {
1027 return ERR(NOT_IMPLEMENTED);
1028 }
1029
1030 static jvmtiError GetSystemProperty(jvmtiEnv* env, const char* property, char** value_ptr) {
1031 return ERR(NOT_IMPLEMENTED);
1032 }
1033
1034 static jvmtiError SetSystemProperty(jvmtiEnv* env, const char* property, const char* value) {
1035 return ERR(NOT_IMPLEMENTED);
1036 }
1037
1038 static jvmtiError GetPhase(jvmtiEnv* env, jvmtiPhase* phase_ptr) {
1039 return ERR(NOT_IMPLEMENTED);
1040 }
1041
1042 static jvmtiError DisposeEnvironment(jvmtiEnv* env) {
Alex Lighte6574242016-08-17 09:56:24 -07001043 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001044 delete env;
1045 return OK;
1046 }
1047
1048 static jvmtiError SetEnvironmentLocalStorage(jvmtiEnv* env, const void* data) {
Alex Lighte6574242016-08-17 09:56:24 -07001049 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001050 reinterpret_cast<ArtJvmTiEnv*>(env)->local_data = const_cast<void*>(data);
1051 return OK;
1052 }
1053
1054 static jvmtiError GetEnvironmentLocalStorage(jvmtiEnv* env, void** data_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001055 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001056 *data_ptr = reinterpret_cast<ArtJvmTiEnv*>(env)->local_data;
1057 return OK;
1058 }
1059
1060 static jvmtiError GetVersionNumber(jvmtiEnv* env, jint* version_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001061 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001062 *version_ptr = JVMTI_VERSION;
1063 return OK;
1064 }
1065
1066 static jvmtiError GetErrorName(jvmtiEnv* env, jvmtiError error, char** name_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001067 ENSURE_NON_NULL(name_ptr);
Alex Light49948e92016-08-11 15:35:28 -07001068 switch (error) {
1069#define ERROR_CASE(e) case (JVMTI_ERROR_ ## e) : do { \
1070 *name_ptr = const_cast<char*>("JVMTI_ERROR_"#e); \
1071 return OK; \
1072 } while (false)
1073 ERROR_CASE(NONE);
1074 ERROR_CASE(INVALID_THREAD);
1075 ERROR_CASE(INVALID_THREAD_GROUP);
1076 ERROR_CASE(INVALID_PRIORITY);
1077 ERROR_CASE(THREAD_NOT_SUSPENDED);
1078 ERROR_CASE(THREAD_NOT_ALIVE);
1079 ERROR_CASE(INVALID_OBJECT);
1080 ERROR_CASE(INVALID_CLASS);
1081 ERROR_CASE(CLASS_NOT_PREPARED);
1082 ERROR_CASE(INVALID_METHODID);
1083 ERROR_CASE(INVALID_LOCATION);
1084 ERROR_CASE(INVALID_FIELDID);
1085 ERROR_CASE(NO_MORE_FRAMES);
1086 ERROR_CASE(OPAQUE_FRAME);
1087 ERROR_CASE(TYPE_MISMATCH);
1088 ERROR_CASE(INVALID_SLOT);
1089 ERROR_CASE(DUPLICATE);
1090 ERROR_CASE(NOT_FOUND);
1091 ERROR_CASE(INVALID_MONITOR);
1092 ERROR_CASE(NOT_MONITOR_OWNER);
1093 ERROR_CASE(INTERRUPT);
1094 ERROR_CASE(INVALID_CLASS_FORMAT);
1095 ERROR_CASE(CIRCULAR_CLASS_DEFINITION);
1096 ERROR_CASE(FAILS_VERIFICATION);
1097 ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_ADDED);
1098 ERROR_CASE(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED);
1099 ERROR_CASE(INVALID_TYPESTATE);
1100 ERROR_CASE(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED);
1101 ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_DELETED);
1102 ERROR_CASE(UNSUPPORTED_VERSION);
1103 ERROR_CASE(NAMES_DONT_MATCH);
1104 ERROR_CASE(UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED);
1105 ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED);
1106 ERROR_CASE(UNMODIFIABLE_CLASS);
1107 ERROR_CASE(NOT_AVAILABLE);
1108 ERROR_CASE(MUST_POSSESS_CAPABILITY);
1109 ERROR_CASE(NULL_POINTER);
1110 ERROR_CASE(ABSENT_INFORMATION);
1111 ERROR_CASE(INVALID_EVENT_TYPE);
1112 ERROR_CASE(ILLEGAL_ARGUMENT);
1113 ERROR_CASE(NATIVE_METHOD);
1114 ERROR_CASE(CLASS_LOADER_UNSUPPORTED);
1115 ERROR_CASE(OUT_OF_MEMORY);
1116 ERROR_CASE(ACCESS_DENIED);
1117 ERROR_CASE(WRONG_PHASE);
1118 ERROR_CASE(INTERNAL);
1119 ERROR_CASE(UNATTACHED_THREAD);
1120 ERROR_CASE(INVALID_ENVIRONMENT);
1121#undef ERROR_CASE
1122 default: {
1123 *name_ptr = const_cast<char*>("JVMTI_ERROR_UNKNOWN");
1124 return ERR(ILLEGAL_ARGUMENT);
1125 }
1126 }
1127 }
1128
1129 static jvmtiError SetVerboseFlag(jvmtiEnv* env, jvmtiVerboseFlag flag, jboolean value) {
1130 return ERR(NOT_IMPLEMENTED);
1131 }
1132
1133 static jvmtiError GetJLocationFormat(jvmtiEnv* env, jvmtiJlocationFormat* format_ptr) {
1134 return ERR(NOT_IMPLEMENTED);
1135 }
Alex Light9c20a142016-08-23 15:05:12 -07001136
1137 // TODO Remove this once events are working.
1138 static jvmtiError RetransformClassWithHook(jvmtiEnv* env,
1139 jclass klass,
1140 jvmtiEventClassFileLoadHook hook) {
1141 std::vector<jclass> classes;
1142 classes.push_back(klass);
1143 return RetransformClassesWithHook(reinterpret_cast<ArtJvmTiEnv*>(env), classes, hook);
1144 }
1145
Alex Light1e07ca62016-12-02 11:40:56 -08001146 static jvmtiError RedefineClassDirect(ArtJvmTiEnv* env,
1147 jclass klass,
1148 jint dex_size,
1149 unsigned char* dex_file) {
1150 if (!IsValidEnv(env)) {
1151 return ERR(INVALID_ENVIRONMENT);
1152 }
1153 jvmtiError ret = OK;
1154 std::string location;
1155 if ((ret = GetClassLocation(env, klass, &location)) != OK) {
1156 // TODO Do something more here? Maybe give log statements?
1157 return ret;
1158 }
1159 std::string error;
1160 ret = Redefiner::RedefineClass(env,
1161 art::Runtime::Current(),
1162 art::Thread::Current(),
1163 klass,
1164 location,
1165 dex_size,
1166 reinterpret_cast<uint8_t*>(dex_file),
1167 &error);
1168 if (ret != OK) {
1169 LOG(ERROR) << "FAILURE TO REDEFINE " << error;
1170 }
1171 return ret;
1172 }
1173
Alex Light9c20a142016-08-23 15:05:12 -07001174 // TODO This will be called by the event handler for the art::ti Event Load Event
1175 static jvmtiError RetransformClassesWithHook(ArtJvmTiEnv* env,
1176 const std::vector<jclass>& classes,
1177 jvmtiEventClassFileLoadHook hook) {
1178 if (!IsValidEnv(env)) {
1179 return ERR(INVALID_ENVIRONMENT);
1180 }
Alex Lighta01de592016-11-15 10:43:06 -08001181 jvmtiError res = OK;
1182 std::string error;
Alex Light9c20a142016-08-23 15:05:12 -07001183 for (jclass klass : classes) {
1184 JNIEnv* jni_env = nullptr;
1185 jobject loader = nullptr;
1186 std::string name;
1187 jobject protection_domain = nullptr;
1188 jint data_len = 0;
1189 unsigned char* dex_data = nullptr;
1190 jvmtiError ret = OK;
1191 std::string location;
1192 if ((ret = GetTransformationData(env,
1193 klass,
1194 /*out*/&location,
1195 /*out*/&jni_env,
1196 /*out*/&loader,
1197 /*out*/&name,
1198 /*out*/&protection_domain,
1199 /*out*/&data_len,
1200 /*out*/&dex_data)) != OK) {
1201 // TODO Do something more here? Maybe give log statements?
1202 return ret;
1203 }
1204 jint new_data_len = 0;
1205 unsigned char* new_dex_data = nullptr;
1206 hook(env,
1207 jni_env,
1208 klass,
1209 loader,
1210 name.c_str(),
1211 protection_domain,
1212 data_len,
1213 dex_data,
1214 /*out*/&new_data_len,
1215 /*out*/&new_dex_data);
1216 // Check if anything actually changed.
1217 if ((new_data_len != 0 || new_dex_data != nullptr) && new_dex_data != dex_data) {
Alex Lighta01de592016-11-15 10:43:06 -08001218 res = Redefiner::RedefineClass(env,
1219 art::Runtime::Current(),
1220 art::Thread::Current(),
1221 klass,
1222 location,
1223 new_data_len,
1224 new_dex_data,
1225 &error);
Alex Light9c20a142016-08-23 15:05:12 -07001226 env->Deallocate(new_dex_data);
1227 }
1228 // Deallocate the old dex data.
1229 env->Deallocate(dex_data);
Alex Lighta01de592016-11-15 10:43:06 -08001230 if (res != OK) {
1231 LOG(ERROR) << "FAILURE TO REDEFINE " << error;
1232 return res;
1233 }
Alex Light9c20a142016-08-23 15:05:12 -07001234 }
1235 return OK;
1236 }
Alex Light49948e92016-08-11 15:35:28 -07001237};
1238
1239static bool IsJvmtiVersion(jint version) {
1240 return version == JVMTI_VERSION_1 ||
1241 version == JVMTI_VERSION_1_0 ||
1242 version == JVMTI_VERSION_1_1 ||
1243 version == JVMTI_VERSION_1_2 ||
1244 version == JVMTI_VERSION;
1245}
1246
1247// Creates a jvmtiEnv and returns it with the art::ti::Env that is associated with it. new_art_ti
1248// is a pointer to the uninitialized memory for an art::ti::Env.
1249static void CreateArtJvmTiEnv(art::JavaVMExt* vm, /*out*/void** new_jvmtiEnv) {
1250 struct ArtJvmTiEnv* env = new ArtJvmTiEnv(vm);
1251 *new_jvmtiEnv = env;
Andreas Gampe77708d92016-10-07 11:48:21 -07001252
1253 gEventHandler.RegisterArtJvmTiEnv(env);
Alex Light49948e92016-08-11 15:35:28 -07001254}
1255
1256// A hook that the runtime uses to allow plugins to handle GetEnv calls. It returns true and
1257// places the return value in 'env' if this library can handle the GetEnv request. Otherwise
1258// returns false and does not modify the 'env' pointer.
1259static jint GetEnvHandler(art::JavaVMExt* vm, /*out*/void** env, jint version) {
1260 if (IsJvmtiVersion(version)) {
1261 CreateArtJvmTiEnv(vm, env);
1262 return JNI_OK;
1263 } else {
1264 printf("version 0x%x is not valid!", version);
1265 return JNI_EVERSION;
1266 }
1267}
1268
1269// The plugin initialization function. This adds the jvmti environment.
1270extern "C" bool ArtPlugin_Initialize() {
Andreas Gampee08a2be2016-10-06 13:13:30 -07001271 art::Runtime* runtime = art::Runtime::Current();
1272 runtime->GetJavaVM()->AddEnvironmentHook(GetEnvHandler);
1273 runtime->AddSystemWeakHolder(&gObjectTagTable);
Alex Light49948e92016-08-11 15:35:28 -07001274 return true;
1275}
1276
1277// The actual struct holding all of the entrypoints into the jvmti interface.
1278const jvmtiInterface_1 gJvmtiInterface = {
Alex Light9c20a142016-08-23 15:05:12 -07001279 // SPECIAL FUNCTION: RetransformClassWithHook Is normally reserved1
1280 // TODO Remove once we have events working.
1281 reinterpret_cast<void*>(JvmtiFunctions::RetransformClassWithHook),
1282 // nullptr, // reserved1
Alex Light49948e92016-08-11 15:35:28 -07001283 JvmtiFunctions::SetEventNotificationMode,
Alex Light1e07ca62016-12-02 11:40:56 -08001284 // SPECIAL FUNCTION: RedefineClassDirect Is normally reserved3
1285 // TODO Remove once we have events working.
1286 reinterpret_cast<void*>(JvmtiFunctions::RedefineClassDirect),
1287 // nullptr, // reserved3
Alex Light49948e92016-08-11 15:35:28 -07001288 JvmtiFunctions::GetAllThreads,
1289 JvmtiFunctions::SuspendThread,
1290 JvmtiFunctions::ResumeThread,
1291 JvmtiFunctions::StopThread,
1292 JvmtiFunctions::InterruptThread,
1293 JvmtiFunctions::GetThreadInfo,
1294 JvmtiFunctions::GetOwnedMonitorInfo, // 10
1295 JvmtiFunctions::GetCurrentContendedMonitor,
1296 JvmtiFunctions::RunAgentThread,
1297 JvmtiFunctions::GetTopThreadGroups,
1298 JvmtiFunctions::GetThreadGroupInfo,
1299 JvmtiFunctions::GetThreadGroupChildren,
1300 JvmtiFunctions::GetFrameCount,
1301 JvmtiFunctions::GetThreadState,
1302 JvmtiFunctions::GetCurrentThread,
1303 JvmtiFunctions::GetFrameLocation,
1304 JvmtiFunctions::NotifyFramePop, // 20
1305 JvmtiFunctions::GetLocalObject,
1306 JvmtiFunctions::GetLocalInt,
1307 JvmtiFunctions::GetLocalLong,
1308 JvmtiFunctions::GetLocalFloat,
1309 JvmtiFunctions::GetLocalDouble,
1310 JvmtiFunctions::SetLocalObject,
1311 JvmtiFunctions::SetLocalInt,
1312 JvmtiFunctions::SetLocalLong,
1313 JvmtiFunctions::SetLocalFloat,
1314 JvmtiFunctions::SetLocalDouble, // 30
1315 JvmtiFunctions::CreateRawMonitor,
1316 JvmtiFunctions::DestroyRawMonitor,
1317 JvmtiFunctions::RawMonitorEnter,
1318 JvmtiFunctions::RawMonitorExit,
1319 JvmtiFunctions::RawMonitorWait,
1320 JvmtiFunctions::RawMonitorNotify,
1321 JvmtiFunctions::RawMonitorNotifyAll,
1322 JvmtiFunctions::SetBreakpoint,
1323 JvmtiFunctions::ClearBreakpoint,
1324 nullptr, // reserved40
1325 JvmtiFunctions::SetFieldAccessWatch,
1326 JvmtiFunctions::ClearFieldAccessWatch,
1327 JvmtiFunctions::SetFieldModificationWatch,
1328 JvmtiFunctions::ClearFieldModificationWatch,
1329 JvmtiFunctions::IsModifiableClass,
1330 JvmtiFunctions::Allocate,
1331 JvmtiFunctions::Deallocate,
1332 JvmtiFunctions::GetClassSignature,
1333 JvmtiFunctions::GetClassStatus,
1334 JvmtiFunctions::GetSourceFileName, // 50
1335 JvmtiFunctions::GetClassModifiers,
1336 JvmtiFunctions::GetClassMethods,
1337 JvmtiFunctions::GetClassFields,
1338 JvmtiFunctions::GetImplementedInterfaces,
1339 JvmtiFunctions::IsInterface,
1340 JvmtiFunctions::IsArrayClass,
1341 JvmtiFunctions::GetClassLoader,
1342 JvmtiFunctions::GetObjectHashCode,
1343 JvmtiFunctions::GetObjectMonitorUsage,
1344 JvmtiFunctions::GetFieldName, // 60
1345 JvmtiFunctions::GetFieldDeclaringClass,
1346 JvmtiFunctions::GetFieldModifiers,
1347 JvmtiFunctions::IsFieldSynthetic,
1348 JvmtiFunctions::GetMethodName,
1349 JvmtiFunctions::GetMethodDeclaringClass,
1350 JvmtiFunctions::GetMethodModifiers,
1351 nullptr, // reserved67
1352 JvmtiFunctions::GetMaxLocals,
1353 JvmtiFunctions::GetArgumentsSize,
1354 JvmtiFunctions::GetLineNumberTable, // 70
1355 JvmtiFunctions::GetMethodLocation,
1356 JvmtiFunctions::GetLocalVariableTable,
1357 JvmtiFunctions::SetNativeMethodPrefix,
1358 JvmtiFunctions::SetNativeMethodPrefixes,
1359 JvmtiFunctions::GetBytecodes,
1360 JvmtiFunctions::IsMethodNative,
1361 JvmtiFunctions::IsMethodSynthetic,
1362 JvmtiFunctions::GetLoadedClasses,
1363 JvmtiFunctions::GetClassLoaderClasses,
1364 JvmtiFunctions::PopFrame, // 80
1365 JvmtiFunctions::ForceEarlyReturnObject,
1366 JvmtiFunctions::ForceEarlyReturnInt,
1367 JvmtiFunctions::ForceEarlyReturnLong,
1368 JvmtiFunctions::ForceEarlyReturnFloat,
1369 JvmtiFunctions::ForceEarlyReturnDouble,
1370 JvmtiFunctions::ForceEarlyReturnVoid,
1371 JvmtiFunctions::RedefineClasses,
1372 JvmtiFunctions::GetVersionNumber,
1373 JvmtiFunctions::GetCapabilities,
1374 JvmtiFunctions::GetSourceDebugExtension, // 90
1375 JvmtiFunctions::IsMethodObsolete,
1376 JvmtiFunctions::SuspendThreadList,
1377 JvmtiFunctions::ResumeThreadList,
1378 nullptr, // reserved94
1379 nullptr, // reserved95
1380 nullptr, // reserved96
1381 nullptr, // reserved97
1382 nullptr, // reserved98
1383 nullptr, // reserved99
1384 JvmtiFunctions::GetAllStackTraces, // 100
1385 JvmtiFunctions::GetThreadListStackTraces,
1386 JvmtiFunctions::GetThreadLocalStorage,
1387 JvmtiFunctions::SetThreadLocalStorage,
1388 JvmtiFunctions::GetStackTrace,
1389 nullptr, // reserved105
1390 JvmtiFunctions::GetTag,
1391 JvmtiFunctions::SetTag,
1392 JvmtiFunctions::ForceGarbageCollection,
1393 JvmtiFunctions::IterateOverObjectsReachableFromObject,
1394 JvmtiFunctions::IterateOverReachableObjects, // 110
1395 JvmtiFunctions::IterateOverHeap,
1396 JvmtiFunctions::IterateOverInstancesOfClass,
1397 nullptr, // reserved113
1398 JvmtiFunctions::GetObjectsWithTags,
1399 JvmtiFunctions::FollowReferences,
1400 JvmtiFunctions::IterateThroughHeap,
1401 nullptr, // reserved117
1402 nullptr, // reserved118
1403 nullptr, // reserved119
1404 JvmtiFunctions::SetJNIFunctionTable, // 120
1405 JvmtiFunctions::GetJNIFunctionTable,
1406 JvmtiFunctions::SetEventCallbacks,
1407 JvmtiFunctions::GenerateEvents,
1408 JvmtiFunctions::GetExtensionFunctions,
1409 JvmtiFunctions::GetExtensionEvents,
1410 JvmtiFunctions::SetExtensionEventCallback,
1411 JvmtiFunctions::DisposeEnvironment,
1412 JvmtiFunctions::GetErrorName,
1413 JvmtiFunctions::GetJLocationFormat,
1414 JvmtiFunctions::GetSystemProperties, // 130
1415 JvmtiFunctions::GetSystemProperty,
1416 JvmtiFunctions::SetSystemProperty,
1417 JvmtiFunctions::GetPhase,
1418 JvmtiFunctions::GetCurrentThreadCpuTimerInfo,
1419 JvmtiFunctions::GetCurrentThreadCpuTime,
1420 JvmtiFunctions::GetThreadCpuTimerInfo,
1421 JvmtiFunctions::GetThreadCpuTime,
1422 JvmtiFunctions::GetTimerInfo,
1423 JvmtiFunctions::GetTime,
1424 JvmtiFunctions::GetPotentialCapabilities, // 140
1425 nullptr, // reserved141
1426 JvmtiFunctions::AddCapabilities,
1427 JvmtiFunctions::RelinquishCapabilities,
1428 JvmtiFunctions::GetAvailableProcessors,
1429 JvmtiFunctions::GetClassVersionNumbers,
1430 JvmtiFunctions::GetConstantPool,
1431 JvmtiFunctions::GetEnvironmentLocalStorage,
1432 JvmtiFunctions::SetEnvironmentLocalStorage,
1433 JvmtiFunctions::AddToBootstrapClassLoaderSearch,
1434 JvmtiFunctions::SetVerboseFlag, // 150
1435 JvmtiFunctions::AddToSystemClassLoaderSearch,
1436 JvmtiFunctions::RetransformClasses,
1437 JvmtiFunctions::GetOwnedMonitorStackDepthInfo,
1438 JvmtiFunctions::GetObjectSize,
1439 JvmtiFunctions::GetLocalInstance,
1440};
1441
1442}; // namespace openjdkjvmti