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