1 /* 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 
32 #include <memory>
33 #include <string>
34 #include <type_traits>
35 #include <vector>
36 
37 #include <android-base/logging.h>
38 
39 #include <jni.h>
40 
41 #include "jvmti.h"
42 
43 #include "alloc_manager.h"
44 #include "art_jvmti.h"
45 #include "base/logging.h"  // For gLogVerbosity.
46 #include "base/mutex.h"
47 #include "events-inl.h"
48 #include "jni/jni_env_ext-inl.h"
49 #include "obj_ptr-inl.h"
50 #include "object_tagging.h"
51 #include "runtime.h"
52 #include "scoped_thread_state_change-inl.h"
53 #include "thread-current-inl.h"
54 #include "thread_list.h"
55 #include "ti_allocator.h"
56 #include "ti_breakpoint.h"
57 #include "ti_class.h"
58 #include "ti_dump.h"
59 #include "ti_extension.h"
60 #include "ti_field.h"
61 #include "ti_heap.h"
62 #include "ti_jni.h"
63 #include "ti_logging.h"
64 #include "ti_method.h"
65 #include "ti_monitor.h"
66 #include "ti_object.h"
67 #include "ti_phase.h"
68 #include "ti_properties.h"
69 #include "ti_redefine.h"
70 #include "ti_search.h"
71 #include "ti_stack.h"
72 #include "ti_thread.h"
73 #include "ti_threadgroup.h"
74 #include "ti_timers.h"
75 #include "transform.h"
76 
77 namespace openjdkjvmti {
78 
79 // NB These are heap allocated to avoid the static destructors being run if an agent calls exit(3).
80 // These should never be null.
81 EventHandler* gEventHandler;
82 DeoptManager* gDeoptManager;
83 AllocationManager* gAllocManager;
84 
85 #define ENSURE_NON_NULL(n)      \
86   do {                          \
87     if ((n) == nullptr) {       \
88       return ERR(NULL_POINTER); \
89     }                           \
90   } while (false)
91 
92 // Returns whether we are able to use all jvmti features.
IsFullJvmtiAvailable()93 static bool IsFullJvmtiAvailable() {
94   art::Runtime* runtime = art::Runtime::Current();
95   return runtime->GetInstrumentation()->IsForcedInterpretOnly() || runtime->IsJavaDebuggable();
96 }
97 
98 class JvmtiFunctions {
99  private:
getEnvironmentError(jvmtiEnv * env)100   static jvmtiError getEnvironmentError(jvmtiEnv* env) {
101     if (env == nullptr) {
102       return ERR(INVALID_ENVIRONMENT);
103     } else if (art::Thread::Current() == nullptr) {
104       return ERR(UNATTACHED_THREAD);
105     } else {
106       return OK;
107     }
108   }
109 
110 #define ENSURE_VALID_ENV(env)                                            \
111   do {                                                                   \
112     jvmtiError ensure_valid_env_ ## __LINE__ = getEnvironmentError(env); \
113     if (ensure_valid_env_ ## __LINE__ != OK) {                           \
114       return ensure_valid_env_ ## __LINE__ ;                             \
115     }                                                                    \
116   } while (false)
117 
118 #define ENSURE_HAS_CAP(env, cap) \
119   do { \
120     if (ArtJvmTiEnv::AsArtJvmTiEnv(env)->capabilities.cap != 1) { \
121       return ERR(MUST_POSSESS_CAPABILITY); \
122     } \
123   } while (false)
124 
125  public:
Allocate(jvmtiEnv * env,jlong size,unsigned char ** mem_ptr)126   static jvmtiError Allocate(jvmtiEnv* env, jlong size, unsigned char** mem_ptr) {
127     jvmtiError err = getEnvironmentError(env);
128     // Allow UNATTACHED_THREAD since we don't really care about that for this function.
129     if (err != OK && err != ERR(UNATTACHED_THREAD)) {
130       return err;
131     }
132     ENSURE_NON_NULL(mem_ptr);
133     return AllocUtil::Allocate(env, size, mem_ptr);
134   }
135 
Deallocate(jvmtiEnv * env,unsigned char * mem)136   static jvmtiError Deallocate(jvmtiEnv* env, unsigned char* mem) {
137     jvmtiError err = getEnvironmentError(env);
138     // Allow UNATTACHED_THREAD since we don't really care about that for this function.
139     if (err != OK && err != ERR(UNATTACHED_THREAD)) {
140       return err;
141     }
142     return AllocUtil::Deallocate(env, mem);
143   }
144 
GetThreadState(jvmtiEnv * env,jthread thread,jint * thread_state_ptr)145   static jvmtiError GetThreadState(jvmtiEnv* env, jthread thread, jint* thread_state_ptr) {
146     ENSURE_VALID_ENV(env);
147     return ThreadUtil::GetThreadState(env, thread, thread_state_ptr);
148   }
149 
GetCurrentThread(jvmtiEnv * env,jthread * thread_ptr)150   static jvmtiError GetCurrentThread(jvmtiEnv* env, jthread* thread_ptr) {
151     ENSURE_VALID_ENV(env);
152     return ThreadUtil::GetCurrentThread(env, thread_ptr);
153   }
154 
GetAllThreads(jvmtiEnv * env,jint * threads_count_ptr,jthread ** threads_ptr)155   static jvmtiError GetAllThreads(jvmtiEnv* env, jint* threads_count_ptr, jthread** threads_ptr) {
156     ENSURE_VALID_ENV(env);
157     return ThreadUtil::GetAllThreads(env, threads_count_ptr, threads_ptr);
158   }
159 
SuspendThread(jvmtiEnv * env,jthread thread)160   static jvmtiError SuspendThread(jvmtiEnv* env, jthread thread) {
161     ENSURE_VALID_ENV(env);
162     ENSURE_HAS_CAP(env, can_suspend);
163     return ThreadUtil::SuspendThread(env, thread);
164   }
165 
SuspendThreadList(jvmtiEnv * env,jint request_count,const jthread * request_list,jvmtiError * results)166   static jvmtiError SuspendThreadList(jvmtiEnv* env,
167                                       jint request_count,
168                                       const jthread* request_list,
169                                       jvmtiError* results) {
170     ENSURE_VALID_ENV(env);
171     ENSURE_HAS_CAP(env, can_suspend);
172     return ThreadUtil::SuspendThreadList(env, request_count, request_list, results);
173   }
174 
ResumeThread(jvmtiEnv * env,jthread thread)175   static jvmtiError ResumeThread(jvmtiEnv* env, jthread thread) {
176     ENSURE_VALID_ENV(env);
177     ENSURE_HAS_CAP(env, can_suspend);
178     return ThreadUtil::ResumeThread(env, thread);
179   }
180 
ResumeThreadList(jvmtiEnv * env,jint request_count,const jthread * request_list,jvmtiError * results)181   static jvmtiError ResumeThreadList(jvmtiEnv* env,
182                                      jint request_count,
183                                      const jthread* request_list,
184                                      jvmtiError* results) {
185     ENSURE_VALID_ENV(env);
186     ENSURE_HAS_CAP(env, can_suspend);
187     return ThreadUtil::ResumeThreadList(env, request_count, request_list, results);
188   }
189 
StopThread(jvmtiEnv * env,jthread thread,jobject exception)190   static jvmtiError StopThread(jvmtiEnv* env, jthread thread, jobject exception) {
191     ENSURE_VALID_ENV(env);
192     ENSURE_HAS_CAP(env, can_signal_thread);
193     return ThreadUtil::StopThread(env, thread, exception);
194   }
195 
InterruptThread(jvmtiEnv * env,jthread thread)196   static jvmtiError InterruptThread(jvmtiEnv* env, jthread thread) {
197     ENSURE_VALID_ENV(env);
198     ENSURE_HAS_CAP(env, can_signal_thread);
199     return ThreadUtil::InterruptThread(env, thread);
200   }
201 
GetThreadInfo(jvmtiEnv * env,jthread thread,jvmtiThreadInfo * info_ptr)202   static jvmtiError GetThreadInfo(jvmtiEnv* env, jthread thread, jvmtiThreadInfo* info_ptr) {
203     ENSURE_VALID_ENV(env);
204     return ThreadUtil::GetThreadInfo(env, thread, info_ptr);
205   }
206 
GetOwnedMonitorInfo(jvmtiEnv * env,jthread thread,jint * owned_monitor_count_ptr,jobject ** owned_monitors_ptr)207   static jvmtiError GetOwnedMonitorInfo(jvmtiEnv* env,
208                                         jthread thread,
209                                         jint* owned_monitor_count_ptr,
210                                         jobject** owned_monitors_ptr) {
211     ENSURE_VALID_ENV(env);
212     ENSURE_HAS_CAP(env, can_get_owned_monitor_info);
213     return StackUtil::GetOwnedMonitorInfo(env,
214                                           thread,
215                                           owned_monitor_count_ptr,
216                                           owned_monitors_ptr);
217   }
218 
GetOwnedMonitorStackDepthInfo(jvmtiEnv * env,jthread thread,jint * monitor_info_count_ptr,jvmtiMonitorStackDepthInfo ** monitor_info_ptr)219   static jvmtiError GetOwnedMonitorStackDepthInfo(jvmtiEnv* env,
220                                                   jthread thread,
221                                                   jint* monitor_info_count_ptr,
222                                                   jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
223     ENSURE_VALID_ENV(env);
224     ENSURE_HAS_CAP(env, can_get_owned_monitor_stack_depth_info);
225     return StackUtil::GetOwnedMonitorStackDepthInfo(env,
226                                                     thread,
227                                                     monitor_info_count_ptr,
228                                                     monitor_info_ptr);
229   }
230 
GetCurrentContendedMonitor(jvmtiEnv * env,jthread thread,jobject * monitor_ptr)231   static jvmtiError GetCurrentContendedMonitor(jvmtiEnv* env,
232                                                jthread thread,
233                                                jobject* monitor_ptr) {
234     ENSURE_VALID_ENV(env);
235     ENSURE_HAS_CAP(env, can_get_current_contended_monitor);
236     return MonitorUtil::GetCurrentContendedMonitor(env, thread, monitor_ptr);
237   }
238 
RunAgentThread(jvmtiEnv * env,jthread thread,jvmtiStartFunction proc,const void * arg,jint priority)239   static jvmtiError RunAgentThread(jvmtiEnv* env,
240                                    jthread thread,
241                                    jvmtiStartFunction proc,
242                                    const void* arg,
243                                    jint priority) {
244     ENSURE_VALID_ENV(env);
245     return ThreadUtil::RunAgentThread(env, thread, proc, arg, priority);
246   }
247 
SetThreadLocalStorage(jvmtiEnv * env,jthread thread,const void * data)248   static jvmtiError SetThreadLocalStorage(jvmtiEnv* env, jthread thread, const void* data) {
249     ENSURE_VALID_ENV(env);
250     return ThreadUtil::SetThreadLocalStorage(env, thread, data);
251   }
252 
GetThreadLocalStorage(jvmtiEnv * env,jthread thread,void ** data_ptr)253   static jvmtiError GetThreadLocalStorage(jvmtiEnv* env, jthread thread, void** data_ptr) {
254     ENSURE_VALID_ENV(env);
255     return ThreadUtil::GetThreadLocalStorage(env, thread, data_ptr);
256   }
257 
GetTopThreadGroups(jvmtiEnv * env,jint * group_count_ptr,jthreadGroup ** groups_ptr)258   static jvmtiError GetTopThreadGroups(jvmtiEnv* env,
259                                        jint* group_count_ptr,
260                                        jthreadGroup** groups_ptr) {
261     ENSURE_VALID_ENV(env);
262     return ThreadGroupUtil::GetTopThreadGroups(env, group_count_ptr, groups_ptr);
263   }
264 
GetThreadGroupInfo(jvmtiEnv * env,jthreadGroup group,jvmtiThreadGroupInfo * info_ptr)265   static jvmtiError GetThreadGroupInfo(jvmtiEnv* env,
266                                        jthreadGroup group,
267                                        jvmtiThreadGroupInfo* info_ptr) {
268     ENSURE_VALID_ENV(env);
269     return ThreadGroupUtil::GetThreadGroupInfo(env, group, info_ptr);
270   }
271 
GetThreadGroupChildren(jvmtiEnv * env,jthreadGroup group,jint * thread_count_ptr,jthread ** threads_ptr,jint * group_count_ptr,jthreadGroup ** groups_ptr)272   static jvmtiError GetThreadGroupChildren(jvmtiEnv* env,
273                                            jthreadGroup group,
274                                            jint* thread_count_ptr,
275                                            jthread** threads_ptr,
276                                            jint* group_count_ptr,
277                                            jthreadGroup** groups_ptr) {
278     ENSURE_VALID_ENV(env);
279     return ThreadGroupUtil::GetThreadGroupChildren(env,
280                                                    group,
281                                                    thread_count_ptr,
282                                                    threads_ptr,
283                                                    group_count_ptr,
284                                                    groups_ptr);
285   }
286 
GetStackTrace(jvmtiEnv * env,jthread thread,jint start_depth,jint max_frame_count,jvmtiFrameInfo * frame_buffer,jint * count_ptr)287   static jvmtiError GetStackTrace(jvmtiEnv* env,
288                                   jthread thread,
289                                   jint start_depth,
290                                   jint max_frame_count,
291                                   jvmtiFrameInfo* frame_buffer,
292                                   jint* count_ptr) {
293     ENSURE_VALID_ENV(env);
294     return StackUtil::GetStackTrace(env,
295                                     thread,
296                                     start_depth,
297                                     max_frame_count,
298                                     frame_buffer,
299                                     count_ptr);
300   }
301 
GetAllStackTraces(jvmtiEnv * env,jint max_frame_count,jvmtiStackInfo ** stack_info_ptr,jint * thread_count_ptr)302   static jvmtiError GetAllStackTraces(jvmtiEnv* env,
303                                       jint max_frame_count,
304                                       jvmtiStackInfo** stack_info_ptr,
305                                       jint* thread_count_ptr) {
306     ENSURE_VALID_ENV(env);
307     return StackUtil::GetAllStackTraces(env, max_frame_count, stack_info_ptr, thread_count_ptr);
308   }
309 
GetThreadListStackTraces(jvmtiEnv * env,jint thread_count,const jthread * thread_list,jint max_frame_count,jvmtiStackInfo ** stack_info_ptr)310   static jvmtiError GetThreadListStackTraces(jvmtiEnv* env,
311                                              jint thread_count,
312                                              const jthread* thread_list,
313                                              jint max_frame_count,
314                                              jvmtiStackInfo** stack_info_ptr) {
315     ENSURE_VALID_ENV(env);
316     return StackUtil::GetThreadListStackTraces(env,
317                                                thread_count,
318                                                thread_list,
319                                                max_frame_count,
320                                                stack_info_ptr);
321   }
322 
GetFrameCount(jvmtiEnv * env,jthread thread,jint * count_ptr)323   static jvmtiError GetFrameCount(jvmtiEnv* env, jthread thread, jint* count_ptr) {
324     ENSURE_VALID_ENV(env);
325     return StackUtil::GetFrameCount(env, thread, count_ptr);
326   }
327 
PopFrame(jvmtiEnv * env,jthread thread)328   static jvmtiError PopFrame(jvmtiEnv* env, jthread thread) {
329     ENSURE_VALID_ENV(env);
330     ENSURE_HAS_CAP(env, can_pop_frame);
331     return StackUtil::PopFrame(env, thread);
332   }
333 
GetFrameLocation(jvmtiEnv * env,jthread thread,jint depth,jmethodID * method_ptr,jlocation * location_ptr)334   static jvmtiError GetFrameLocation(jvmtiEnv* env,
335                                      jthread thread,
336                                      jint depth,
337                                      jmethodID* method_ptr,
338                                      jlocation* location_ptr) {
339     ENSURE_VALID_ENV(env);
340     return StackUtil::GetFrameLocation(env, thread, depth, method_ptr, location_ptr);
341   }
342 
NotifyFramePop(jvmtiEnv * env,jthread thread,jint depth)343   static jvmtiError NotifyFramePop(jvmtiEnv* env, jthread thread, jint depth) {
344     ENSURE_VALID_ENV(env);
345     ENSURE_HAS_CAP(env, can_generate_frame_pop_events);
346     return StackUtil::NotifyFramePop(env, thread, depth);
347   }
348 
ForceEarlyReturnObject(jvmtiEnv * env,jthread thread,jobject value)349   static jvmtiError ForceEarlyReturnObject(jvmtiEnv* env, jthread thread, jobject value) {
350     ENSURE_VALID_ENV(env);
351     ENSURE_HAS_CAP(env, can_force_early_return);
352     return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
353   }
354 
ForceEarlyReturnInt(jvmtiEnv * env,jthread thread,jint value)355   static jvmtiError ForceEarlyReturnInt(jvmtiEnv* env, jthread thread, jint value) {
356     ENSURE_VALID_ENV(env);
357     ENSURE_HAS_CAP(env, can_force_early_return);
358     return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
359   }
360 
ForceEarlyReturnLong(jvmtiEnv * env,jthread thread,jlong value)361   static jvmtiError ForceEarlyReturnLong(jvmtiEnv* env, jthread thread, jlong value) {
362     ENSURE_VALID_ENV(env);
363     ENSURE_HAS_CAP(env, can_force_early_return);
364     return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
365   }
366 
ForceEarlyReturnFloat(jvmtiEnv * env,jthread thread,jfloat value)367   static jvmtiError ForceEarlyReturnFloat(jvmtiEnv* env, jthread thread, jfloat value) {
368     ENSURE_VALID_ENV(env);
369     ENSURE_HAS_CAP(env, can_force_early_return);
370     return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
371   }
372 
ForceEarlyReturnDouble(jvmtiEnv * env,jthread thread,jdouble value)373   static jvmtiError ForceEarlyReturnDouble(jvmtiEnv* env, jthread thread, jdouble value) {
374     ENSURE_VALID_ENV(env);
375     ENSURE_HAS_CAP(env, can_force_early_return);
376     return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
377   }
378 
ForceEarlyReturnVoid(jvmtiEnv * env,jthread thread)379   static jvmtiError ForceEarlyReturnVoid(jvmtiEnv* env, jthread thread) {
380     ENSURE_VALID_ENV(env);
381     ENSURE_HAS_CAP(env, can_force_early_return);
382     return StackUtil::ForceEarlyReturn<nullptr_t>(env, gEventHandler, thread, nullptr);
383   }
384 
FollowReferences(jvmtiEnv * env,jint heap_filter,jclass klass,jobject initial_object,const jvmtiHeapCallbacks * callbacks,const void * user_data)385   static jvmtiError FollowReferences(jvmtiEnv* env,
386                                      jint heap_filter,
387                                      jclass klass,
388                                      jobject initial_object,
389                                      const jvmtiHeapCallbacks* callbacks,
390                                      const void* user_data) {
391     ENSURE_VALID_ENV(env);
392     ENSURE_HAS_CAP(env, can_tag_objects);
393     HeapUtil heap_util(ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
394     return heap_util.FollowReferences(env,
395                                       heap_filter,
396                                       klass,
397                                       initial_object,
398                                       callbacks,
399                                       user_data);
400   }
401 
IterateThroughHeap(jvmtiEnv * env,jint heap_filter,jclass klass,const jvmtiHeapCallbacks * callbacks,const void * user_data)402   static jvmtiError IterateThroughHeap(jvmtiEnv* env,
403                                        jint heap_filter,
404                                        jclass klass,
405                                        const jvmtiHeapCallbacks* callbacks,
406                                        const void* user_data) {
407     ENSURE_VALID_ENV(env);
408     ENSURE_HAS_CAP(env, can_tag_objects);
409     HeapUtil heap_util(ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
410     return heap_util.IterateThroughHeap(env, heap_filter, klass, callbacks, user_data);
411   }
412 
GetTag(jvmtiEnv * env,jobject object,jlong * tag_ptr)413   static jvmtiError GetTag(jvmtiEnv* env, jobject object, jlong* tag_ptr) {
414     ENSURE_VALID_ENV(env);
415     ENSURE_HAS_CAP(env, can_tag_objects);
416 
417     JNIEnv* jni_env = GetJniEnv(env);
418     if (jni_env == nullptr) {
419       return ERR(INTERNAL);
420     }
421 
422     art::ScopedObjectAccess soa(jni_env);
423     art::ObjPtr<art::mirror::Object> obj = soa.Decode<art::mirror::Object>(object);
424     if (!ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table->GetTag(obj.Ptr(), tag_ptr)) {
425       *tag_ptr = 0;
426     }
427 
428     return ERR(NONE);
429   }
430 
SetTag(jvmtiEnv * env,jobject object,jlong tag)431   static jvmtiError SetTag(jvmtiEnv* env, jobject object, jlong tag) {
432     ENSURE_VALID_ENV(env);
433     ENSURE_HAS_CAP(env, can_tag_objects);
434 
435     if (object == nullptr) {
436       return ERR(NULL_POINTER);
437     }
438 
439     JNIEnv* jni_env = GetJniEnv(env);
440     if (jni_env == nullptr) {
441       return ERR(INTERNAL);
442     }
443 
444     art::ScopedObjectAccess soa(jni_env);
445     art::ObjPtr<art::mirror::Object> obj = soa.Decode<art::mirror::Object>(object);
446     ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table->Set(obj.Ptr(), tag);
447 
448     return ERR(NONE);
449   }
450 
GetObjectsWithTags(jvmtiEnv * env,jint tag_count,const jlong * tags,jint * count_ptr,jobject ** object_result_ptr,jlong ** tag_result_ptr)451   static jvmtiError GetObjectsWithTags(jvmtiEnv* env,
452                                        jint tag_count,
453                                        const jlong* tags,
454                                        jint* count_ptr,
455                                        jobject** object_result_ptr,
456                                        jlong** tag_result_ptr) {
457     ENSURE_VALID_ENV(env);
458     ENSURE_HAS_CAP(env, can_tag_objects);
459 
460     JNIEnv* jni_env = GetJniEnv(env);
461     if (jni_env == nullptr) {
462       return ERR(INTERNAL);
463     }
464 
465     art::ScopedObjectAccess soa(jni_env);
466     return ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table->GetTaggedObjects(env,
467                                                                                tag_count,
468                                                                                tags,
469                                                                                count_ptr,
470                                                                                object_result_ptr,
471                                                                                tag_result_ptr);
472   }
473 
ForceGarbageCollection(jvmtiEnv * env)474   static jvmtiError ForceGarbageCollection(jvmtiEnv* env) {
475     ENSURE_VALID_ENV(env);
476     return HeapUtil::ForceGarbageCollection(env);
477   }
478 
IterateOverObjectsReachableFromObject(jvmtiEnv * env,jobject object ATTRIBUTE_UNUSED,jvmtiObjectReferenceCallback object_reference_callback ATTRIBUTE_UNUSED,const void * user_data ATTRIBUTE_UNUSED)479   static jvmtiError IterateOverObjectsReachableFromObject(
480       jvmtiEnv* env,
481       jobject object ATTRIBUTE_UNUSED,
482       jvmtiObjectReferenceCallback object_reference_callback ATTRIBUTE_UNUSED,
483       const void* user_data ATTRIBUTE_UNUSED) {
484     ENSURE_VALID_ENV(env);
485     ENSURE_HAS_CAP(env, can_tag_objects);
486     return ERR(NOT_IMPLEMENTED);
487   }
488 
IterateOverReachableObjects(jvmtiEnv * env,jvmtiHeapRootCallback heap_root_callback ATTRIBUTE_UNUSED,jvmtiStackReferenceCallback stack_ref_callback ATTRIBUTE_UNUSED,jvmtiObjectReferenceCallback object_ref_callback ATTRIBUTE_UNUSED,const void * user_data ATTRIBUTE_UNUSED)489   static jvmtiError IterateOverReachableObjects(
490       jvmtiEnv* env,
491       jvmtiHeapRootCallback heap_root_callback ATTRIBUTE_UNUSED,
492       jvmtiStackReferenceCallback stack_ref_callback ATTRIBUTE_UNUSED,
493       jvmtiObjectReferenceCallback object_ref_callback ATTRIBUTE_UNUSED,
494       const void* user_data ATTRIBUTE_UNUSED) {
495     ENSURE_VALID_ENV(env);
496     ENSURE_HAS_CAP(env, can_tag_objects);
497     return ERR(NOT_IMPLEMENTED);
498   }
499 
IterateOverHeap(jvmtiEnv * env,jvmtiHeapObjectFilter object_filter ATTRIBUTE_UNUSED,jvmtiHeapObjectCallback heap_object_callback ATTRIBUTE_UNUSED,const void * user_data ATTRIBUTE_UNUSED)500   static jvmtiError IterateOverHeap(jvmtiEnv* env,
501                                     jvmtiHeapObjectFilter object_filter ATTRIBUTE_UNUSED,
502                                     jvmtiHeapObjectCallback heap_object_callback ATTRIBUTE_UNUSED,
503                                     const void* user_data ATTRIBUTE_UNUSED) {
504     ENSURE_VALID_ENV(env);
505     ENSURE_HAS_CAP(env, can_tag_objects);
506     return ERR(NOT_IMPLEMENTED);
507   }
508 
IterateOverInstancesOfClass(jvmtiEnv * env,jclass klass,jvmtiHeapObjectFilter object_filter,jvmtiHeapObjectCallback heap_object_callback,const void * user_data)509   static jvmtiError IterateOverInstancesOfClass(
510       jvmtiEnv* env,
511       jclass klass,
512       jvmtiHeapObjectFilter object_filter,
513       jvmtiHeapObjectCallback heap_object_callback,
514       const void* user_data) {
515     ENSURE_VALID_ENV(env);
516     ENSURE_HAS_CAP(env, can_tag_objects);
517     HeapUtil heap_util(ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
518     return heap_util.IterateOverInstancesOfClass(
519         env, klass, object_filter, heap_object_callback, user_data);
520   }
521 
GetLocalObject(jvmtiEnv * env,jthread thread,jint depth,jint slot,jobject * value_ptr)522   static jvmtiError GetLocalObject(jvmtiEnv* env,
523                                    jthread thread,
524                                    jint depth,
525                                    jint slot,
526                                    jobject* value_ptr) {
527     ENSURE_VALID_ENV(env);
528     ENSURE_HAS_CAP(env, can_access_local_variables);
529     return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
530   }
531 
GetLocalInstance(jvmtiEnv * env,jthread thread,jint depth,jobject * value_ptr)532   static jvmtiError GetLocalInstance(jvmtiEnv* env,
533                                      jthread thread,
534                                      jint depth,
535                                      jobject* value_ptr) {
536     ENSURE_VALID_ENV(env);
537     ENSURE_HAS_CAP(env, can_access_local_variables);
538     return MethodUtil::GetLocalInstance(env, thread, depth, value_ptr);
539   }
540 
GetLocalInt(jvmtiEnv * env,jthread thread,jint depth,jint slot,jint * value_ptr)541   static jvmtiError GetLocalInt(jvmtiEnv* env,
542                                 jthread thread,
543                                 jint depth,
544                                 jint slot,
545                                 jint* value_ptr) {
546     ENSURE_VALID_ENV(env);
547     ENSURE_HAS_CAP(env, can_access_local_variables);
548     return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
549   }
550 
GetLocalLong(jvmtiEnv * env,jthread thread,jint depth,jint slot,jlong * value_ptr)551   static jvmtiError GetLocalLong(jvmtiEnv* env,
552                                  jthread thread,
553                                  jint depth,
554                                  jint slot,
555                                  jlong* value_ptr) {
556     ENSURE_VALID_ENV(env);
557     ENSURE_HAS_CAP(env, can_access_local_variables);
558     return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
559   }
560 
GetLocalFloat(jvmtiEnv * env,jthread thread,jint depth,jint slot,jfloat * value_ptr)561   static jvmtiError GetLocalFloat(jvmtiEnv* env,
562                                   jthread thread,
563                                   jint depth,
564                                   jint slot,
565                                   jfloat* value_ptr) {
566     ENSURE_VALID_ENV(env);
567     ENSURE_HAS_CAP(env, can_access_local_variables);
568     return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
569   }
570 
GetLocalDouble(jvmtiEnv * env,jthread thread,jint depth,jint slot,jdouble * value_ptr)571   static jvmtiError GetLocalDouble(jvmtiEnv* env,
572                                    jthread thread,
573                                    jint depth,
574                                    jint slot,
575                                    jdouble* value_ptr) {
576     ENSURE_VALID_ENV(env);
577     ENSURE_HAS_CAP(env, can_access_local_variables);
578     return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
579   }
580 
SetLocalObject(jvmtiEnv * env,jthread thread,jint depth,jint slot,jobject value)581   static jvmtiError SetLocalObject(jvmtiEnv* env,
582                                    jthread thread,
583                                    jint depth,
584                                    jint slot,
585                                    jobject value) {
586     ENSURE_VALID_ENV(env);
587     ENSURE_HAS_CAP(env, can_access_local_variables);
588     return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
589   }
590 
SetLocalInt(jvmtiEnv * env,jthread thread,jint depth,jint slot,jint value)591   static jvmtiError SetLocalInt(jvmtiEnv* env,
592                                 jthread thread,
593                                 jint depth,
594                                 jint slot,
595                                 jint value) {
596     ENSURE_VALID_ENV(env);
597     ENSURE_HAS_CAP(env, can_access_local_variables);
598     return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
599   }
600 
SetLocalLong(jvmtiEnv * env,jthread thread,jint depth,jint slot,jlong value)601   static jvmtiError SetLocalLong(jvmtiEnv* env,
602                                  jthread thread,
603                                  jint depth,
604                                  jint slot,
605                                  jlong value) {
606     ENSURE_VALID_ENV(env);
607     ENSURE_HAS_CAP(env, can_access_local_variables);
608     return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
609   }
610 
SetLocalFloat(jvmtiEnv * env,jthread thread,jint depth,jint slot,jfloat value)611   static jvmtiError SetLocalFloat(jvmtiEnv* env,
612                                   jthread thread,
613                                   jint depth,
614                                   jint slot,
615                                   jfloat value) {
616     ENSURE_VALID_ENV(env);
617     ENSURE_HAS_CAP(env, can_access_local_variables);
618     return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
619   }
620 
SetLocalDouble(jvmtiEnv * env,jthread thread,jint depth,jint slot,jdouble value)621   static jvmtiError SetLocalDouble(jvmtiEnv* env,
622                                    jthread thread,
623                                    jint depth,
624                                    jint slot,
625                                    jdouble value) {
626     ENSURE_VALID_ENV(env);
627     ENSURE_HAS_CAP(env, can_access_local_variables);
628     return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
629   }
630 
631 
SetBreakpoint(jvmtiEnv * env,jmethodID method,jlocation location)632   static jvmtiError SetBreakpoint(jvmtiEnv* env, jmethodID method, jlocation location) {
633     ENSURE_VALID_ENV(env);
634     ENSURE_HAS_CAP(env, can_generate_breakpoint_events);
635     return BreakpointUtil::SetBreakpoint(env, method, location);
636   }
637 
ClearBreakpoint(jvmtiEnv * env,jmethodID method,jlocation location)638   static jvmtiError ClearBreakpoint(jvmtiEnv* env, jmethodID method, jlocation location) {
639     ENSURE_VALID_ENV(env);
640     ENSURE_HAS_CAP(env, can_generate_breakpoint_events);
641     return BreakpointUtil::ClearBreakpoint(env, method, location);
642   }
643 
SetFieldAccessWatch(jvmtiEnv * env,jclass klass,jfieldID field)644   static jvmtiError SetFieldAccessWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
645     ENSURE_VALID_ENV(env);
646     ENSURE_HAS_CAP(env, can_generate_field_access_events);
647     return FieldUtil::SetFieldAccessWatch(env, klass, field);
648   }
649 
ClearFieldAccessWatch(jvmtiEnv * env,jclass klass,jfieldID field)650   static jvmtiError ClearFieldAccessWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
651     ENSURE_VALID_ENV(env);
652     ENSURE_HAS_CAP(env, can_generate_field_access_events);
653     return FieldUtil::ClearFieldAccessWatch(env, klass, field);
654   }
655 
SetFieldModificationWatch(jvmtiEnv * env,jclass klass,jfieldID field)656   static jvmtiError SetFieldModificationWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
657     ENSURE_VALID_ENV(env);
658     ENSURE_HAS_CAP(env, can_generate_field_modification_events);
659     return FieldUtil::SetFieldModificationWatch(env, klass, field);
660   }
661 
ClearFieldModificationWatch(jvmtiEnv * env,jclass klass,jfieldID field)662   static jvmtiError ClearFieldModificationWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
663     ENSURE_VALID_ENV(env);
664     ENSURE_HAS_CAP(env, can_generate_field_modification_events);
665     return FieldUtil::ClearFieldModificationWatch(env, klass, field);
666   }
667 
GetLoadedClasses(jvmtiEnv * env,jint * class_count_ptr,jclass ** classes_ptr)668   static jvmtiError GetLoadedClasses(jvmtiEnv* env, jint* class_count_ptr, jclass** classes_ptr) {
669     ENSURE_VALID_ENV(env);
670     HeapUtil heap_util(ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
671     return heap_util.GetLoadedClasses(env, class_count_ptr, classes_ptr);
672   }
673 
GetClassLoaderClasses(jvmtiEnv * env,jobject initiating_loader,jint * class_count_ptr,jclass ** classes_ptr)674   static jvmtiError GetClassLoaderClasses(jvmtiEnv* env,
675                                           jobject initiating_loader,
676                                           jint* class_count_ptr,
677                                           jclass** classes_ptr) {
678     ENSURE_VALID_ENV(env);
679     return ClassUtil::GetClassLoaderClasses(env, initiating_loader, class_count_ptr, classes_ptr);
680   }
681 
GetClassSignature(jvmtiEnv * env,jclass klass,char ** signature_ptr,char ** generic_ptr)682   static jvmtiError GetClassSignature(jvmtiEnv* env,
683                                       jclass klass,
684                                       char** signature_ptr,
685                                       char** generic_ptr) {
686     ENSURE_VALID_ENV(env);
687     return ClassUtil::GetClassSignature(env, klass, signature_ptr, generic_ptr);
688   }
689 
GetClassStatus(jvmtiEnv * env,jclass klass,jint * status_ptr)690   static jvmtiError GetClassStatus(jvmtiEnv* env, jclass klass, jint* status_ptr) {
691     ENSURE_VALID_ENV(env);
692     return ClassUtil::GetClassStatus(env, klass, status_ptr);
693   }
694 
GetSourceFileName(jvmtiEnv * env,jclass klass,char ** source_name_ptr)695   static jvmtiError GetSourceFileName(jvmtiEnv* env, jclass klass, char** source_name_ptr) {
696     ENSURE_VALID_ENV(env);
697     ENSURE_HAS_CAP(env, can_get_source_file_name);
698     return ClassUtil::GetSourceFileName(env, klass, source_name_ptr);
699   }
700 
GetClassModifiers(jvmtiEnv * env,jclass klass,jint * modifiers_ptr)701   static jvmtiError GetClassModifiers(jvmtiEnv* env, jclass klass, jint* modifiers_ptr) {
702     ENSURE_VALID_ENV(env);
703     return ClassUtil::GetClassModifiers(env, klass, modifiers_ptr);
704   }
705 
GetClassMethods(jvmtiEnv * env,jclass klass,jint * method_count_ptr,jmethodID ** methods_ptr)706   static jvmtiError GetClassMethods(jvmtiEnv* env,
707                                     jclass klass,
708                                     jint* method_count_ptr,
709                                     jmethodID** methods_ptr) {
710     ENSURE_VALID_ENV(env);
711     return ClassUtil::GetClassMethods(env, klass, method_count_ptr, methods_ptr);
712   }
713 
GetClassFields(jvmtiEnv * env,jclass klass,jint * field_count_ptr,jfieldID ** fields_ptr)714   static jvmtiError GetClassFields(jvmtiEnv* env,
715                                    jclass klass,
716                                    jint* field_count_ptr,
717                                    jfieldID** fields_ptr) {
718     ENSURE_VALID_ENV(env);
719     return ClassUtil::GetClassFields(env, klass, field_count_ptr, fields_ptr);
720   }
721 
GetImplementedInterfaces(jvmtiEnv * env,jclass klass,jint * interface_count_ptr,jclass ** interfaces_ptr)722   static jvmtiError GetImplementedInterfaces(jvmtiEnv* env,
723                                              jclass klass,
724                                              jint* interface_count_ptr,
725                                              jclass** interfaces_ptr) {
726     ENSURE_VALID_ENV(env);
727     return ClassUtil::GetImplementedInterfaces(env, klass, interface_count_ptr, interfaces_ptr);
728   }
729 
GetClassVersionNumbers(jvmtiEnv * env,jclass klass,jint * minor_version_ptr,jint * major_version_ptr)730   static jvmtiError GetClassVersionNumbers(jvmtiEnv* env,
731                                            jclass klass,
732                                            jint* minor_version_ptr,
733                                            jint* major_version_ptr) {
734     ENSURE_VALID_ENV(env);
735     return ClassUtil::GetClassVersionNumbers(env, klass, minor_version_ptr, major_version_ptr);
736   }
737 
GetConstantPool(jvmtiEnv * env,jclass klass ATTRIBUTE_UNUSED,jint * constant_pool_count_ptr ATTRIBUTE_UNUSED,jint * constant_pool_byte_count_ptr ATTRIBUTE_UNUSED,unsigned char ** constant_pool_bytes_ptr ATTRIBUTE_UNUSED)738   static jvmtiError GetConstantPool(jvmtiEnv* env,
739                                     jclass klass ATTRIBUTE_UNUSED,
740                                     jint* constant_pool_count_ptr ATTRIBUTE_UNUSED,
741                                     jint* constant_pool_byte_count_ptr ATTRIBUTE_UNUSED,
742                                     unsigned char** constant_pool_bytes_ptr ATTRIBUTE_UNUSED) {
743     ENSURE_VALID_ENV(env);
744     ENSURE_HAS_CAP(env, can_get_constant_pool);
745     return ERR(NOT_IMPLEMENTED);
746   }
747 
IsInterface(jvmtiEnv * env,jclass klass,jboolean * is_interface_ptr)748   static jvmtiError IsInterface(jvmtiEnv* env, jclass klass, jboolean* is_interface_ptr) {
749     ENSURE_VALID_ENV(env);
750     return ClassUtil::IsInterface(env, klass, is_interface_ptr);
751   }
752 
IsArrayClass(jvmtiEnv * env,jclass klass,jboolean * is_array_class_ptr)753   static jvmtiError IsArrayClass(jvmtiEnv* env,
754                                  jclass klass,
755                                  jboolean* is_array_class_ptr) {
756     ENSURE_VALID_ENV(env);
757     return ClassUtil::IsArrayClass(env, klass, is_array_class_ptr);
758   }
759 
IsModifiableClass(jvmtiEnv * env,jclass klass,jboolean * is_modifiable_class_ptr)760   static jvmtiError IsModifiableClass(jvmtiEnv* env,
761                                       jclass klass,
762                                       jboolean* is_modifiable_class_ptr) {
763     ENSURE_VALID_ENV(env);
764     return Redefiner::IsModifiableClass(env, klass, is_modifiable_class_ptr);
765   }
766 
GetClassLoader(jvmtiEnv * env,jclass klass,jobject * classloader_ptr)767   static jvmtiError GetClassLoader(jvmtiEnv* env, jclass klass, jobject* classloader_ptr) {
768     ENSURE_VALID_ENV(env);
769     return ClassUtil::GetClassLoader(env, klass, classloader_ptr);
770   }
771 
GetSourceDebugExtension(jvmtiEnv * env,jclass klass,char ** source_debug_extension_ptr)772   static jvmtiError GetSourceDebugExtension(jvmtiEnv* env,
773                                             jclass klass,
774                                             char** source_debug_extension_ptr) {
775     ENSURE_VALID_ENV(env);
776     ENSURE_HAS_CAP(env, can_get_source_debug_extension);
777     return ClassUtil::GetSourceDebugExtension(env, klass, source_debug_extension_ptr);
778   }
779 
RetransformClasses(jvmtiEnv * env,jint class_count,const jclass * classes)780   static jvmtiError RetransformClasses(jvmtiEnv* env, jint class_count, const jclass* classes) {
781     ENSURE_VALID_ENV(env);
782     ENSURE_HAS_CAP(env, can_retransform_classes);
783     return Transformer::RetransformClasses(env, class_count, classes);
784   }
785 
RedefineClasses(jvmtiEnv * env,jint class_count,const jvmtiClassDefinition * class_definitions)786   static jvmtiError RedefineClasses(jvmtiEnv* env,
787                                     jint class_count,
788                                     const jvmtiClassDefinition* class_definitions) {
789     ENSURE_VALID_ENV(env);
790     ENSURE_HAS_CAP(env, can_redefine_classes);
791     return Redefiner::RedefineClasses(env, class_count, class_definitions);
792   }
793 
GetObjectSize(jvmtiEnv * env,jobject object,jlong * size_ptr)794   static jvmtiError GetObjectSize(jvmtiEnv* env, jobject object, jlong* size_ptr) {
795     ENSURE_VALID_ENV(env);
796     return ObjectUtil::GetObjectSize(env, object, size_ptr);
797   }
798 
GetObjectHashCode(jvmtiEnv * env,jobject object,jint * hash_code_ptr)799   static jvmtiError GetObjectHashCode(jvmtiEnv* env, jobject object, jint* hash_code_ptr) {
800     ENSURE_VALID_ENV(env);
801     return ObjectUtil::GetObjectHashCode(env, object, hash_code_ptr);
802   }
803 
GetObjectMonitorUsage(jvmtiEnv * env,jobject object,jvmtiMonitorUsage * info_ptr)804   static jvmtiError GetObjectMonitorUsage(jvmtiEnv* env,
805                                           jobject object,
806                                           jvmtiMonitorUsage* info_ptr) {
807     ENSURE_VALID_ENV(env);
808     ENSURE_HAS_CAP(env, can_get_monitor_info);
809     return ObjectUtil::GetObjectMonitorUsage(env, object, info_ptr);
810   }
811 
GetFieldName(jvmtiEnv * env,jclass klass,jfieldID field,char ** name_ptr,char ** signature_ptr,char ** generic_ptr)812   static jvmtiError GetFieldName(jvmtiEnv* env,
813                                  jclass klass,
814                                  jfieldID field,
815                                  char** name_ptr,
816                                  char** signature_ptr,
817                                  char** generic_ptr) {
818     ENSURE_VALID_ENV(env);
819     return FieldUtil::GetFieldName(env, klass, field, name_ptr, signature_ptr, generic_ptr);
820   }
821 
GetFieldDeclaringClass(jvmtiEnv * env,jclass klass,jfieldID field,jclass * declaring_class_ptr)822   static jvmtiError GetFieldDeclaringClass(jvmtiEnv* env,
823                                            jclass klass,
824                                            jfieldID field,
825                                            jclass* declaring_class_ptr) {
826     ENSURE_VALID_ENV(env);
827     return FieldUtil::GetFieldDeclaringClass(env, klass, field, declaring_class_ptr);
828   }
829 
GetFieldModifiers(jvmtiEnv * env,jclass klass,jfieldID field,jint * modifiers_ptr)830   static jvmtiError GetFieldModifiers(jvmtiEnv* env,
831                                       jclass klass,
832                                       jfieldID field,
833                                       jint* modifiers_ptr) {
834     ENSURE_VALID_ENV(env);
835     return FieldUtil::GetFieldModifiers(env, klass, field, modifiers_ptr);
836   }
837 
IsFieldSynthetic(jvmtiEnv * env,jclass klass,jfieldID field,jboolean * is_synthetic_ptr)838   static jvmtiError IsFieldSynthetic(jvmtiEnv* env,
839                                      jclass klass,
840                                      jfieldID field,
841                                      jboolean* is_synthetic_ptr) {
842     ENSURE_VALID_ENV(env);
843     ENSURE_HAS_CAP(env, can_get_synthetic_attribute);
844     return FieldUtil::IsFieldSynthetic(env, klass, field, is_synthetic_ptr);
845   }
846 
GetMethodName(jvmtiEnv * env,jmethodID method,char ** name_ptr,char ** signature_ptr,char ** generic_ptr)847   static jvmtiError GetMethodName(jvmtiEnv* env,
848                                   jmethodID method,
849                                   char** name_ptr,
850                                   char** signature_ptr,
851                                   char** generic_ptr) {
852     ENSURE_VALID_ENV(env);
853     return MethodUtil::GetMethodName(env, method, name_ptr, signature_ptr, generic_ptr);
854   }
855 
GetMethodDeclaringClass(jvmtiEnv * env,jmethodID method,jclass * declaring_class_ptr)856   static jvmtiError GetMethodDeclaringClass(jvmtiEnv* env,
857                                             jmethodID method,
858                                             jclass* declaring_class_ptr) {
859     ENSURE_VALID_ENV(env);
860     return MethodUtil::GetMethodDeclaringClass(env, method, declaring_class_ptr);
861   }
862 
GetMethodModifiers(jvmtiEnv * env,jmethodID method,jint * modifiers_ptr)863   static jvmtiError GetMethodModifiers(jvmtiEnv* env,
864                                        jmethodID method,
865                                        jint* modifiers_ptr) {
866     ENSURE_VALID_ENV(env);
867     return MethodUtil::GetMethodModifiers(env, method, modifiers_ptr);
868   }
869 
GetMaxLocals(jvmtiEnv * env,jmethodID method,jint * max_ptr)870   static jvmtiError GetMaxLocals(jvmtiEnv* env,
871                                  jmethodID method,
872                                  jint* max_ptr) {
873     ENSURE_VALID_ENV(env);
874     return MethodUtil::GetMaxLocals(env, method, max_ptr);
875   }
876 
GetArgumentsSize(jvmtiEnv * env,jmethodID method,jint * size_ptr)877   static jvmtiError GetArgumentsSize(jvmtiEnv* env,
878                                      jmethodID method,
879                                      jint* size_ptr) {
880     ENSURE_VALID_ENV(env);
881     return MethodUtil::GetArgumentsSize(env, method, size_ptr);
882   }
883 
GetLineNumberTable(jvmtiEnv * env,jmethodID method,jint * entry_count_ptr,jvmtiLineNumberEntry ** table_ptr)884   static jvmtiError GetLineNumberTable(jvmtiEnv* env,
885                                        jmethodID method,
886                                        jint* entry_count_ptr,
887                                        jvmtiLineNumberEntry** table_ptr) {
888     ENSURE_VALID_ENV(env);
889     ENSURE_HAS_CAP(env, can_get_line_numbers);
890     return MethodUtil::GetLineNumberTable(env, method, entry_count_ptr, table_ptr);
891   }
892 
GetMethodLocation(jvmtiEnv * env,jmethodID method,jlocation * start_location_ptr,jlocation * end_location_ptr)893   static jvmtiError GetMethodLocation(jvmtiEnv* env,
894                                       jmethodID method,
895                                       jlocation* start_location_ptr,
896                                       jlocation* end_location_ptr) {
897     ENSURE_VALID_ENV(env);
898     return MethodUtil::GetMethodLocation(env, method, start_location_ptr, end_location_ptr);
899   }
900 
GetLocalVariableTable(jvmtiEnv * env,jmethodID method,jint * entry_count_ptr,jvmtiLocalVariableEntry ** table_ptr)901   static jvmtiError GetLocalVariableTable(jvmtiEnv* env,
902                                           jmethodID method,
903                                           jint* entry_count_ptr,
904                                           jvmtiLocalVariableEntry** table_ptr) {
905     ENSURE_VALID_ENV(env);
906     ENSURE_HAS_CAP(env, can_access_local_variables);
907     return MethodUtil::GetLocalVariableTable(env, method, entry_count_ptr, table_ptr);
908   }
909 
GetBytecodes(jvmtiEnv * env,jmethodID method,jint * bytecode_count_ptr,unsigned char ** bytecodes_ptr)910   static jvmtiError GetBytecodes(jvmtiEnv* env,
911                                  jmethodID method,
912                                  jint* bytecode_count_ptr,
913                                  unsigned char** bytecodes_ptr) {
914     ENSURE_VALID_ENV(env);
915     ENSURE_HAS_CAP(env, can_get_bytecodes);
916     return MethodUtil::GetBytecodes(env, method, bytecode_count_ptr, bytecodes_ptr);
917   }
918 
IsMethodNative(jvmtiEnv * env,jmethodID method,jboolean * is_native_ptr)919   static jvmtiError IsMethodNative(jvmtiEnv* env, jmethodID method, jboolean* is_native_ptr) {
920     ENSURE_VALID_ENV(env);
921     return MethodUtil::IsMethodNative(env, method, is_native_ptr);
922   }
923 
IsMethodSynthetic(jvmtiEnv * env,jmethodID method,jboolean * is_synthetic_ptr)924   static jvmtiError IsMethodSynthetic(jvmtiEnv* env, jmethodID method, jboolean* is_synthetic_ptr) {
925     ENSURE_VALID_ENV(env);
926     ENSURE_HAS_CAP(env, can_get_synthetic_attribute);
927     return MethodUtil::IsMethodSynthetic(env, method, is_synthetic_ptr);
928   }
929 
IsMethodObsolete(jvmtiEnv * env,jmethodID method,jboolean * is_obsolete_ptr)930   static jvmtiError IsMethodObsolete(jvmtiEnv* env, jmethodID method, jboolean* is_obsolete_ptr) {
931     ENSURE_VALID_ENV(env);
932     return MethodUtil::IsMethodObsolete(env, method, is_obsolete_ptr);
933   }
934 
SetNativeMethodPrefix(jvmtiEnv * env,const char * prefix ATTRIBUTE_UNUSED)935   static jvmtiError SetNativeMethodPrefix(jvmtiEnv* env, const char* prefix ATTRIBUTE_UNUSED) {
936     ENSURE_VALID_ENV(env);
937     ENSURE_HAS_CAP(env, can_set_native_method_prefix);
938     return ERR(NOT_IMPLEMENTED);
939   }
940 
SetNativeMethodPrefixes(jvmtiEnv * env,jint prefix_count ATTRIBUTE_UNUSED,char ** prefixes ATTRIBUTE_UNUSED)941   static jvmtiError SetNativeMethodPrefixes(jvmtiEnv* env,
942                                             jint prefix_count ATTRIBUTE_UNUSED,
943                                             char** prefixes ATTRIBUTE_UNUSED) {
944     ENSURE_VALID_ENV(env);
945     ENSURE_HAS_CAP(env, can_set_native_method_prefix);
946     return ERR(NOT_IMPLEMENTED);
947   }
948 
CreateRawMonitor(jvmtiEnv * env,const char * name,jrawMonitorID * monitor_ptr)949   static jvmtiError CreateRawMonitor(jvmtiEnv* env, const char* name, jrawMonitorID* monitor_ptr) {
950     ENSURE_VALID_ENV(env);
951     return MonitorUtil::CreateRawMonitor(env, name, monitor_ptr);
952   }
953 
DestroyRawMonitor(jvmtiEnv * env,jrawMonitorID monitor)954   static jvmtiError DestroyRawMonitor(jvmtiEnv* env, jrawMonitorID monitor) {
955     ENSURE_VALID_ENV(env);
956     return MonitorUtil::DestroyRawMonitor(env, monitor);
957   }
958 
RawMonitorEnter(jvmtiEnv * env,jrawMonitorID monitor)959   static jvmtiError RawMonitorEnter(jvmtiEnv* env, jrawMonitorID monitor) {
960     ENSURE_VALID_ENV(env);
961     return MonitorUtil::RawMonitorEnter(env, monitor);
962   }
963 
RawMonitorExit(jvmtiEnv * env,jrawMonitorID monitor)964   static jvmtiError RawMonitorExit(jvmtiEnv* env, jrawMonitorID monitor) {
965     ENSURE_VALID_ENV(env);
966     return MonitorUtil::RawMonitorExit(env, monitor);
967   }
968 
RawMonitorWait(jvmtiEnv * env,jrawMonitorID monitor,jlong millis)969   static jvmtiError RawMonitorWait(jvmtiEnv* env, jrawMonitorID monitor, jlong millis) {
970     ENSURE_VALID_ENV(env);
971     return MonitorUtil::RawMonitorWait(env, monitor, millis);
972   }
973 
RawMonitorNotify(jvmtiEnv * env,jrawMonitorID monitor)974   static jvmtiError RawMonitorNotify(jvmtiEnv* env, jrawMonitorID monitor) {
975     ENSURE_VALID_ENV(env);
976     return MonitorUtil::RawMonitorNotify(env, monitor);
977   }
978 
RawMonitorNotifyAll(jvmtiEnv * env,jrawMonitorID monitor)979   static jvmtiError RawMonitorNotifyAll(jvmtiEnv* env, jrawMonitorID monitor) {
980     ENSURE_VALID_ENV(env);
981     return MonitorUtil::RawMonitorNotifyAll(env, monitor);
982   }
983 
SetJNIFunctionTable(jvmtiEnv * env,const jniNativeInterface * function_table)984   static jvmtiError SetJNIFunctionTable(jvmtiEnv* env, const jniNativeInterface* function_table) {
985     ENSURE_VALID_ENV(env);
986     return JNIUtil::SetJNIFunctionTable(env, function_table);
987   }
988 
GetJNIFunctionTable(jvmtiEnv * env,jniNativeInterface ** function_table)989   static jvmtiError GetJNIFunctionTable(jvmtiEnv* env, jniNativeInterface** function_table) {
990     ENSURE_VALID_ENV(env);
991     return JNIUtil::GetJNIFunctionTable(env, function_table);
992   }
993 
994   // TODO: This will require locking, so that an agent can't remove callbacks when we're dispatching
995   //       an event.
SetEventCallbacks(jvmtiEnv * env,const jvmtiEventCallbacks * callbacks,jint size_of_callbacks)996   static jvmtiError SetEventCallbacks(jvmtiEnv* env,
997                                       const jvmtiEventCallbacks* callbacks,
998                                       jint size_of_callbacks) {
999     ENSURE_VALID_ENV(env);
1000     if (size_of_callbacks < 0) {
1001       return ERR(ILLEGAL_ARGUMENT);
1002     }
1003 
1004     if (callbacks == nullptr) {
1005       ArtJvmTiEnv::AsArtJvmTiEnv(env)->event_callbacks.reset();
1006       return ERR(NONE);
1007     }
1008 
1009     // Lock the event_info_mutex_ while we replace the callbacks.
1010     ArtJvmTiEnv* art_env = ArtJvmTiEnv::AsArtJvmTiEnv(env);
1011     art::WriterMutexLock lk(art::Thread::Current(), art_env->event_info_mutex_);
1012     std::unique_ptr<ArtJvmtiEventCallbacks> tmp(new ArtJvmtiEventCallbacks());
1013     // Copy over the extension events.
1014     tmp->CopyExtensionsFrom(art_env->event_callbacks.get());
1015     // Never overwrite the extension events.
1016     size_t copy_size = std::min(sizeof(jvmtiEventCallbacks),
1017                                 static_cast<size_t>(size_of_callbacks));
1018     copy_size = art::RoundDown(copy_size, sizeof(void*));
1019     // Copy non-extension events.
1020     memcpy(tmp.get(), callbacks, copy_size);
1021 
1022     // replace the event table.
1023     art_env->event_callbacks = std::move(tmp);
1024 
1025     return ERR(NONE);
1026   }
1027 
SetEventNotificationMode(jvmtiEnv * env,jvmtiEventMode mode,jvmtiEvent event_type,jthread event_thread,...)1028   static jvmtiError SetEventNotificationMode(jvmtiEnv* env,
1029                                              jvmtiEventMode mode,
1030                                              jvmtiEvent event_type,
1031                                              jthread event_thread,
1032                                              ...) {
1033     ENSURE_VALID_ENV(env);
1034     ArtJvmTiEnv* art_env = ArtJvmTiEnv::AsArtJvmTiEnv(env);
1035     return gEventHandler->SetEvent(art_env,
1036                                    event_thread,
1037                                    GetArtJvmtiEvent(art_env, event_type),
1038                                    mode);
1039   }
1040 
GenerateEvents(jvmtiEnv * env,jvmtiEvent event_type ATTRIBUTE_UNUSED)1041   static jvmtiError GenerateEvents(jvmtiEnv* env,
1042                                    jvmtiEvent event_type ATTRIBUTE_UNUSED) {
1043     ENSURE_VALID_ENV(env);
1044     return OK;
1045   }
1046 
GetExtensionFunctions(jvmtiEnv * env,jint * extension_count_ptr,jvmtiExtensionFunctionInfo ** extensions)1047   static jvmtiError GetExtensionFunctions(jvmtiEnv* env,
1048                                           jint* extension_count_ptr,
1049                                           jvmtiExtensionFunctionInfo** extensions) {
1050     ENSURE_VALID_ENV(env);
1051     ENSURE_NON_NULL(extension_count_ptr);
1052     ENSURE_NON_NULL(extensions);
1053     return ExtensionUtil::GetExtensionFunctions(env, extension_count_ptr, extensions);
1054   }
1055 
GetExtensionEvents(jvmtiEnv * env,jint * extension_count_ptr,jvmtiExtensionEventInfo ** extensions)1056   static jvmtiError GetExtensionEvents(jvmtiEnv* env,
1057                                        jint* extension_count_ptr,
1058                                        jvmtiExtensionEventInfo** extensions) {
1059     ENSURE_VALID_ENV(env);
1060     ENSURE_NON_NULL(extension_count_ptr);
1061     ENSURE_NON_NULL(extensions);
1062     return ExtensionUtil::GetExtensionEvents(env, extension_count_ptr, extensions);
1063   }
1064 
SetExtensionEventCallback(jvmtiEnv * env,jint extension_event_index,jvmtiExtensionEvent callback)1065   static jvmtiError SetExtensionEventCallback(jvmtiEnv* env,
1066                                               jint extension_event_index,
1067                                               jvmtiExtensionEvent callback) {
1068     ENSURE_VALID_ENV(env);
1069     return ExtensionUtil::SetExtensionEventCallback(env,
1070                                                     extension_event_index,
1071                                                     callback,
1072                                                     gEventHandler);
1073   }
1074 
1075 #define FOR_ALL_CAPABILITIES(FUN)                        \
1076     FUN(can_tag_objects)                                 \
1077     FUN(can_generate_field_modification_events)          \
1078     FUN(can_generate_field_access_events)                \
1079     FUN(can_get_bytecodes)                               \
1080     FUN(can_get_synthetic_attribute)                     \
1081     FUN(can_get_owned_monitor_info)                      \
1082     FUN(can_get_current_contended_monitor)               \
1083     FUN(can_get_monitor_info)                            \
1084     FUN(can_pop_frame)                                   \
1085     FUN(can_redefine_classes)                            \
1086     FUN(can_signal_thread)                               \
1087     FUN(can_get_source_file_name)                        \
1088     FUN(can_get_line_numbers)                            \
1089     FUN(can_get_source_debug_extension)                  \
1090     FUN(can_access_local_variables)                      \
1091     FUN(can_maintain_original_method_order)              \
1092     FUN(can_generate_single_step_events)                 \
1093     FUN(can_generate_exception_events)                   \
1094     FUN(can_generate_frame_pop_events)                   \
1095     FUN(can_generate_breakpoint_events)                  \
1096     FUN(can_suspend)                                     \
1097     FUN(can_redefine_any_class)                          \
1098     FUN(can_get_current_thread_cpu_time)                 \
1099     FUN(can_get_thread_cpu_time)                         \
1100     FUN(can_generate_method_entry_events)                \
1101     FUN(can_generate_method_exit_events)                 \
1102     FUN(can_generate_all_class_hook_events)              \
1103     FUN(can_generate_compiled_method_load_events)        \
1104     FUN(can_generate_monitor_events)                     \
1105     FUN(can_generate_vm_object_alloc_events)             \
1106     FUN(can_generate_native_method_bind_events)          \
1107     FUN(can_generate_garbage_collection_events)          \
1108     FUN(can_generate_object_free_events)                 \
1109     FUN(can_force_early_return)                          \
1110     FUN(can_get_owned_monitor_stack_depth_info)          \
1111     FUN(can_get_constant_pool)                           \
1112     FUN(can_set_native_method_prefix)                    \
1113     FUN(can_retransform_classes)                         \
1114     FUN(can_retransform_any_class)                       \
1115     FUN(can_generate_resource_exhaustion_heap_events)    \
1116     FUN(can_generate_resource_exhaustion_threads_events)
1117 
GetPotentialCapabilities(jvmtiEnv * env,jvmtiCapabilities * capabilities_ptr)1118   static jvmtiError GetPotentialCapabilities(jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr) {
1119     ENSURE_VALID_ENV(env);
1120     ENSURE_NON_NULL(capabilities_ptr);
1121     *capabilities_ptr = kPotentialCapabilities;
1122     if (UNLIKELY(!IsFullJvmtiAvailable())) {
1123 #define REMOVE_NONDEBUGGABLE_UNSUPPORTED(e)                 \
1124       do {                                                  \
1125         if (kNonDebuggableUnsupportedCapabilities.e == 1) { \
1126           capabilities_ptr->e = 0;                          \
1127         }                                                   \
1128       } while (false);
1129 
1130       FOR_ALL_CAPABILITIES(REMOVE_NONDEBUGGABLE_UNSUPPORTED);
1131 #undef REMOVE_NONDEBUGGABLE_UNSUPPORTED
1132     }
1133     return OK;
1134   }
1135 
AddCapabilities(jvmtiEnv * env,const jvmtiCapabilities * capabilities_ptr)1136   static jvmtiError AddCapabilities(jvmtiEnv* env, const jvmtiCapabilities* capabilities_ptr) {
1137     ENSURE_VALID_ENV(env);
1138     ENSURE_NON_NULL(capabilities_ptr);
1139     ArtJvmTiEnv* art_env = static_cast<ArtJvmTiEnv*>(env);
1140     jvmtiError ret = OK;
1141     jvmtiCapabilities changed = {};
1142     jvmtiCapabilities potential_capabilities = {};
1143     ret = env->GetPotentialCapabilities(&potential_capabilities);
1144     if (ret != OK) {
1145       return ret;
1146     }
1147 #define ADD_CAPABILITY(e) \
1148     do { \
1149       if (capabilities_ptr->e == 1) { \
1150         if (potential_capabilities.e == 1) { \
1151           if (art_env->capabilities.e != 1) { \
1152             art_env->capabilities.e = 1; \
1153             changed.e = 1; \
1154           }\
1155         } else { \
1156           ret = ERR(NOT_AVAILABLE); \
1157         } \
1158       } \
1159     } while (false);
1160 
1161     FOR_ALL_CAPABILITIES(ADD_CAPABILITY);
1162 #undef ADD_CAPABILITY
1163     gEventHandler->HandleChangedCapabilities(ArtJvmTiEnv::AsArtJvmTiEnv(env),
1164                                              changed,
1165                                              /*added=*/true);
1166     return ret;
1167   }
1168 
RelinquishCapabilities(jvmtiEnv * env,const jvmtiCapabilities * capabilities_ptr)1169   static jvmtiError RelinquishCapabilities(jvmtiEnv* env,
1170                                            const jvmtiCapabilities* capabilities_ptr) {
1171     ENSURE_VALID_ENV(env);
1172     ENSURE_NON_NULL(capabilities_ptr);
1173     ArtJvmTiEnv* art_env = reinterpret_cast<ArtJvmTiEnv*>(env);
1174     jvmtiCapabilities changed = {};
1175 #define DEL_CAPABILITY(e) \
1176     do { \
1177       if (capabilities_ptr->e == 1) { \
1178         if (art_env->capabilities.e == 1) { \
1179           art_env->capabilities.e = 0;\
1180           changed.e = 1; \
1181         } \
1182       } \
1183     } while (false);
1184 
1185     FOR_ALL_CAPABILITIES(DEL_CAPABILITY);
1186 #undef DEL_CAPABILITY
1187     gEventHandler->HandleChangedCapabilities(ArtJvmTiEnv::AsArtJvmTiEnv(env),
1188                                              changed,
1189                                              /*added=*/false);
1190     return OK;
1191   }
1192 
1193 #undef FOR_ALL_CAPABILITIES
1194 
GetCapabilities(jvmtiEnv * env,jvmtiCapabilities * capabilities_ptr)1195   static jvmtiError GetCapabilities(jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr) {
1196     ENSURE_VALID_ENV(env);
1197     ENSURE_NON_NULL(capabilities_ptr);
1198     ArtJvmTiEnv* artenv = reinterpret_cast<ArtJvmTiEnv*>(env);
1199     *capabilities_ptr = artenv->capabilities;
1200     return OK;
1201   }
1202 
GetCurrentThreadCpuTimerInfo(jvmtiEnv * env,jvmtiTimerInfo * info_ptr ATTRIBUTE_UNUSED)1203   static jvmtiError GetCurrentThreadCpuTimerInfo(jvmtiEnv* env,
1204                                                  jvmtiTimerInfo* info_ptr ATTRIBUTE_UNUSED) {
1205     ENSURE_VALID_ENV(env);
1206     ENSURE_HAS_CAP(env, can_get_current_thread_cpu_time);
1207     return ERR(NOT_IMPLEMENTED);
1208   }
1209 
GetCurrentThreadCpuTime(jvmtiEnv * env,jlong * nanos_ptr ATTRIBUTE_UNUSED)1210   static jvmtiError GetCurrentThreadCpuTime(jvmtiEnv* env, jlong* nanos_ptr ATTRIBUTE_UNUSED) {
1211     ENSURE_VALID_ENV(env);
1212     ENSURE_HAS_CAP(env, can_get_current_thread_cpu_time);
1213     return ERR(NOT_IMPLEMENTED);
1214   }
1215 
GetThreadCpuTimerInfo(jvmtiEnv * env,jvmtiTimerInfo * info_ptr ATTRIBUTE_UNUSED)1216   static jvmtiError GetThreadCpuTimerInfo(jvmtiEnv* env,
1217                                           jvmtiTimerInfo* info_ptr ATTRIBUTE_UNUSED) {
1218     ENSURE_VALID_ENV(env);
1219     ENSURE_HAS_CAP(env, can_get_thread_cpu_time);
1220     return ERR(NOT_IMPLEMENTED);
1221   }
1222 
GetThreadCpuTime(jvmtiEnv * env,jthread thread ATTRIBUTE_UNUSED,jlong * nanos_ptr ATTRIBUTE_UNUSED)1223   static jvmtiError GetThreadCpuTime(jvmtiEnv* env,
1224                                      jthread thread ATTRIBUTE_UNUSED,
1225                                      jlong* nanos_ptr ATTRIBUTE_UNUSED) {
1226     ENSURE_VALID_ENV(env);
1227     ENSURE_HAS_CAP(env, can_get_thread_cpu_time);
1228     return ERR(NOT_IMPLEMENTED);
1229   }
1230 
GetTimerInfo(jvmtiEnv * env,jvmtiTimerInfo * info_ptr)1231   static jvmtiError GetTimerInfo(jvmtiEnv* env, jvmtiTimerInfo* info_ptr) {
1232     ENSURE_VALID_ENV(env);
1233     return TimerUtil::GetTimerInfo(env, info_ptr);
1234   }
1235 
GetTime(jvmtiEnv * env,jlong * nanos_ptr)1236   static jvmtiError GetTime(jvmtiEnv* env, jlong* nanos_ptr) {
1237     ENSURE_VALID_ENV(env);
1238     return TimerUtil::GetTime(env, nanos_ptr);
1239   }
1240 
GetAvailableProcessors(jvmtiEnv * env,jint * processor_count_ptr)1241   static jvmtiError GetAvailableProcessors(jvmtiEnv* env, jint* processor_count_ptr) {
1242     ENSURE_VALID_ENV(env);
1243     return TimerUtil::GetAvailableProcessors(env, processor_count_ptr);
1244   }
1245 
AddToBootstrapClassLoaderSearch(jvmtiEnv * env,const char * segment)1246   static jvmtiError AddToBootstrapClassLoaderSearch(jvmtiEnv* env, const char* segment) {
1247     ENSURE_VALID_ENV(env);
1248     return SearchUtil::AddToBootstrapClassLoaderSearch(env, segment);
1249   }
1250 
AddToSystemClassLoaderSearch(jvmtiEnv * env,const char * segment)1251   static jvmtiError AddToSystemClassLoaderSearch(jvmtiEnv* env, const char* segment) {
1252     ENSURE_VALID_ENV(env);
1253     return SearchUtil::AddToSystemClassLoaderSearch(env, segment);
1254   }
1255 
GetSystemProperties(jvmtiEnv * env,jint * count_ptr,char *** property_ptr)1256   static jvmtiError GetSystemProperties(jvmtiEnv* env, jint* count_ptr, char*** property_ptr) {
1257     ENSURE_VALID_ENV(env);
1258     return PropertiesUtil::GetSystemProperties(env, count_ptr, property_ptr);
1259   }
1260 
GetSystemProperty(jvmtiEnv * env,const char * property,char ** value_ptr)1261   static jvmtiError GetSystemProperty(jvmtiEnv* env, const char* property, char** value_ptr) {
1262     ENSURE_VALID_ENV(env);
1263     return PropertiesUtil::GetSystemProperty(env, property, value_ptr);
1264   }
1265 
SetSystemProperty(jvmtiEnv * env,const char * property,const char * value)1266   static jvmtiError SetSystemProperty(jvmtiEnv* env, const char* property, const char* value) {
1267     ENSURE_VALID_ENV(env);
1268     return PropertiesUtil::SetSystemProperty(env, property, value);
1269   }
1270 
GetPhase(jvmtiEnv * env,jvmtiPhase * phase_ptr)1271   static jvmtiError GetPhase(jvmtiEnv* env, jvmtiPhase* phase_ptr) {
1272     ENSURE_VALID_ENV(env);
1273     return PhaseUtil::GetPhase(env, phase_ptr);
1274   }
1275 
DisposeEnvironment(jvmtiEnv * env)1276   static jvmtiError DisposeEnvironment(jvmtiEnv* env) {
1277     ENSURE_VALID_ENV(env);
1278     ArtJvmTiEnv* tienv = ArtJvmTiEnv::AsArtJvmTiEnv(env);
1279     gEventHandler->RemoveArtJvmTiEnv(tienv);
1280     art::Runtime::Current()->RemoveSystemWeakHolder(tienv->object_tag_table.get());
1281     ThreadUtil::RemoveEnvironment(tienv);
1282     delete tienv;
1283     return OK;
1284   }
1285 
SetEnvironmentLocalStorage(jvmtiEnv * env,const void * data)1286   static jvmtiError SetEnvironmentLocalStorage(jvmtiEnv* env, const void* data) {
1287     ENSURE_VALID_ENV(env);
1288     reinterpret_cast<ArtJvmTiEnv*>(env)->local_data = const_cast<void*>(data);
1289     return OK;
1290   }
1291 
GetEnvironmentLocalStorage(jvmtiEnv * env,void ** data_ptr)1292   static jvmtiError GetEnvironmentLocalStorage(jvmtiEnv* env, void** data_ptr) {
1293     ENSURE_VALID_ENV(env);
1294     *data_ptr = reinterpret_cast<ArtJvmTiEnv*>(env)->local_data;
1295     return OK;
1296   }
1297 
GetVersionNumber(jvmtiEnv * env,jint * version_ptr)1298   static jvmtiError GetVersionNumber(jvmtiEnv* env, jint* version_ptr) {
1299     ENSURE_VALID_ENV(env);
1300     *version_ptr = ArtJvmTiEnv::AsArtJvmTiEnv(env)->ti_version;
1301     return OK;
1302   }
1303 
GetErrorName(jvmtiEnv * env,jvmtiError error,char ** name_ptr)1304   static jvmtiError GetErrorName(jvmtiEnv* env, jvmtiError error,  char** name_ptr) {
1305     ENSURE_NON_NULL(name_ptr);
1306     auto copy_fn = [&](const char* name_cstr) {
1307       jvmtiError res;
1308       JvmtiUniquePtr<char[]> copy = CopyString(env, name_cstr, &res);
1309       if (copy == nullptr) {
1310         *name_ptr = nullptr;
1311         return res;
1312       } else {
1313         *name_ptr = copy.release();
1314         return OK;
1315       }
1316     };
1317     switch (error) {
1318 #define ERROR_CASE(e) case (JVMTI_ERROR_ ## e) : \
1319         return copy_fn("JVMTI_ERROR_"#e);
1320       ERROR_CASE(NONE);
1321       ERROR_CASE(INVALID_THREAD);
1322       ERROR_CASE(INVALID_THREAD_GROUP);
1323       ERROR_CASE(INVALID_PRIORITY);
1324       ERROR_CASE(THREAD_NOT_SUSPENDED);
1325       ERROR_CASE(THREAD_SUSPENDED);
1326       ERROR_CASE(THREAD_NOT_ALIVE);
1327       ERROR_CASE(INVALID_OBJECT);
1328       ERROR_CASE(INVALID_CLASS);
1329       ERROR_CASE(CLASS_NOT_PREPARED);
1330       ERROR_CASE(INVALID_METHODID);
1331       ERROR_CASE(INVALID_LOCATION);
1332       ERROR_CASE(INVALID_FIELDID);
1333       ERROR_CASE(NO_MORE_FRAMES);
1334       ERROR_CASE(OPAQUE_FRAME);
1335       ERROR_CASE(TYPE_MISMATCH);
1336       ERROR_CASE(INVALID_SLOT);
1337       ERROR_CASE(DUPLICATE);
1338       ERROR_CASE(NOT_FOUND);
1339       ERROR_CASE(INVALID_MONITOR);
1340       ERROR_CASE(NOT_MONITOR_OWNER);
1341       ERROR_CASE(INTERRUPT);
1342       ERROR_CASE(INVALID_CLASS_FORMAT);
1343       ERROR_CASE(CIRCULAR_CLASS_DEFINITION);
1344       ERROR_CASE(FAILS_VERIFICATION);
1345       ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_ADDED);
1346       ERROR_CASE(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED);
1347       ERROR_CASE(INVALID_TYPESTATE);
1348       ERROR_CASE(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED);
1349       ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_DELETED);
1350       ERROR_CASE(UNSUPPORTED_VERSION);
1351       ERROR_CASE(NAMES_DONT_MATCH);
1352       ERROR_CASE(UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED);
1353       ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED);
1354       ERROR_CASE(UNMODIFIABLE_CLASS);
1355       ERROR_CASE(NOT_AVAILABLE);
1356       ERROR_CASE(MUST_POSSESS_CAPABILITY);
1357       ERROR_CASE(NULL_POINTER);
1358       ERROR_CASE(ABSENT_INFORMATION);
1359       ERROR_CASE(INVALID_EVENT_TYPE);
1360       ERROR_CASE(ILLEGAL_ARGUMENT);
1361       ERROR_CASE(NATIVE_METHOD);
1362       ERROR_CASE(CLASS_LOADER_UNSUPPORTED);
1363       ERROR_CASE(OUT_OF_MEMORY);
1364       ERROR_CASE(ACCESS_DENIED);
1365       ERROR_CASE(WRONG_PHASE);
1366       ERROR_CASE(INTERNAL);
1367       ERROR_CASE(UNATTACHED_THREAD);
1368       ERROR_CASE(INVALID_ENVIRONMENT);
1369 #undef ERROR_CASE
1370     }
1371 
1372     return ERR(ILLEGAL_ARGUMENT);
1373   }
1374 
SetVerboseFlag(jvmtiEnv * env,jvmtiVerboseFlag flag,jboolean value)1375   static jvmtiError SetVerboseFlag(jvmtiEnv* env,
1376                                    jvmtiVerboseFlag flag,
1377                                    jboolean value) {
1378     ENSURE_VALID_ENV(env);
1379     return LogUtil::SetVerboseFlag(env, flag, value);
1380   }
1381 
GetJLocationFormat(jvmtiEnv * env,jvmtiJlocationFormat * format_ptr)1382   static jvmtiError GetJLocationFormat(jvmtiEnv* env, jvmtiJlocationFormat* format_ptr) {
1383     ENSURE_VALID_ENV(env);
1384     // Report BCI as jlocation format. We report dex bytecode indices.
1385     if (format_ptr == nullptr) {
1386       return ERR(NULL_POINTER);
1387     }
1388     *format_ptr = jvmtiJlocationFormat::JVMTI_JLOCATION_JVMBCI;
1389     return ERR(NONE);
1390   }
1391 };
1392 
IsJvmtiVersion(jint version)1393 static bool IsJvmtiVersion(jint version) {
1394   return version ==  JVMTI_VERSION_1 ||
1395          version == JVMTI_VERSION_1_0 ||
1396          version == JVMTI_VERSION_1_1 ||
1397          version == JVMTI_VERSION_1_2 ||
1398          version == JVMTI_VERSION;
1399 }
1400 
1401 extern const jvmtiInterface_1 gJvmtiInterface;
1402 
ArtJvmTiEnv(art::JavaVMExt * runtime,EventHandler * event_handler,jint version)1403 ArtJvmTiEnv::ArtJvmTiEnv(art::JavaVMExt* runtime, EventHandler* event_handler, jint version)
1404     : art_vm(runtime),
1405       local_data(nullptr),
1406       ti_version(version),
1407       capabilities(),
1408       event_info_mutex_("jvmtiEnv_EventInfoMutex"),
1409       last_error_mutex_("jvmtiEnv_LastErrorMutex", art::LockLevel::kGenericBottomLock) {
1410   object_tag_table = std::make_unique<ObjectTagTable>(event_handler, this);
1411   functions = &gJvmtiInterface;
1412 }
1413 
1414 // Creates a jvmtiEnv and returns it with the art::ti::Env that is associated with it. new_art_ti
1415 // is a pointer to the uninitialized memory for an art::ti::Env.
CreateArtJvmTiEnv(art::JavaVMExt * vm,jint version,void ** new_jvmtiEnv)1416 static void CreateArtJvmTiEnv(art::JavaVMExt* vm, jint version, /*out*/void** new_jvmtiEnv) {
1417   struct ArtJvmTiEnv* env = new ArtJvmTiEnv(vm, gEventHandler, version);
1418   *new_jvmtiEnv = env;
1419 
1420   gEventHandler->RegisterArtJvmTiEnv(env);
1421 
1422   art::Runtime::Current()->AddSystemWeakHolder(
1423       ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
1424 }
1425 
1426 // A hook that the runtime uses to allow plugins to handle GetEnv calls. It returns true and
1427 // places the return value in 'env' if this library can handle the GetEnv request. Otherwise
1428 // returns false and does not modify the 'env' pointer.
GetEnvHandler(art::JavaVMExt * vm,void ** env,jint version)1429 static jint GetEnvHandler(art::JavaVMExt* vm, /*out*/void** env, jint version) {
1430   // JavaDebuggable will either be set by the runtime as it is starting up or the plugin if it's
1431   // loaded early enough. If this is false we cannot guarantee conformance to all JVMTI behaviors
1432   // due to optimizations. We will only allow agents to get ArtTiEnvs using the kArtTiVersion.
1433   if (IsFullJvmtiAvailable() && IsJvmtiVersion(version)) {
1434     CreateArtJvmTiEnv(vm, JVMTI_VERSION, env);
1435     return JNI_OK;
1436   } else if (version == kArtTiVersion) {
1437     CreateArtJvmTiEnv(vm, kArtTiVersion, env);
1438     return JNI_OK;
1439   } else {
1440     printf("version 0x%x is not valid!", version);
1441     return JNI_EVERSION;
1442   }
1443 }
1444 
1445 // The plugin initialization function. This adds the jvmti environment.
ArtPlugin_Initialize()1446 extern "C" bool ArtPlugin_Initialize() {
1447   art::Runtime* runtime = art::Runtime::Current();
1448 
1449   gAllocManager = new AllocationManager;
1450   gDeoptManager = new DeoptManager;
1451   gEventHandler = new EventHandler;
1452 
1453   gDeoptManager->Setup();
1454   if (runtime->IsStarted()) {
1455     PhaseUtil::SetToLive();
1456   } else {
1457     PhaseUtil::SetToOnLoad();
1458   }
1459   PhaseUtil::Register(gEventHandler);
1460   ThreadUtil::Register(gEventHandler);
1461   ClassUtil::Register(gEventHandler);
1462   DumpUtil::Register(gEventHandler);
1463   MethodUtil::Register(gEventHandler);
1464   HeapExtensions::Register(gEventHandler);
1465   SearchUtil::Register();
1466   HeapUtil::Register();
1467   FieldUtil::Register(gEventHandler);
1468   BreakpointUtil::Register(gEventHandler);
1469   Transformer::Register(gEventHandler);
1470 
1471   {
1472     // Make sure we can deopt anything we need to.
1473     art::ScopedSuspendAll ssa(__FUNCTION__);
1474     gDeoptManager->FinishSetup();
1475   }
1476 
1477   runtime->GetJavaVM()->AddEnvironmentHook(GetEnvHandler);
1478 
1479   return true;
1480 }
1481 
ArtPlugin_Deinitialize()1482 extern "C" bool ArtPlugin_Deinitialize() {
1483   gEventHandler->Shutdown();
1484   gDeoptManager->Shutdown();
1485   PhaseUtil::Unregister();
1486   ThreadUtil::Unregister();
1487   ClassUtil::Unregister();
1488   DumpUtil::Unregister();
1489   MethodUtil::Unregister();
1490   SearchUtil::Unregister();
1491   HeapUtil::Unregister();
1492   FieldUtil::Unregister();
1493   BreakpointUtil::Unregister();
1494 
1495   // TODO It would be good to delete the gEventHandler and gDeoptManager here but we cannot since
1496   // daemon threads might be suspended and we want to make sure that even if they wake up briefly
1497   // they won't hit deallocated memory. By this point none of the functions will do anything since
1498   // they have already shutdown.
1499 
1500   return true;
1501 }
1502 
1503 // The actual struct holding all of the entrypoints into the jvmti interface.
1504 const jvmtiInterface_1 gJvmtiInterface = {
1505   nullptr,  // reserved1
1506   JvmtiFunctions::SetEventNotificationMode,
1507   nullptr,  // reserved3
1508   JvmtiFunctions::GetAllThreads,
1509   JvmtiFunctions::SuspendThread,
1510   JvmtiFunctions::ResumeThread,
1511   JvmtiFunctions::StopThread,
1512   JvmtiFunctions::InterruptThread,
1513   JvmtiFunctions::GetThreadInfo,
1514   JvmtiFunctions::GetOwnedMonitorInfo,  // 10
1515   JvmtiFunctions::GetCurrentContendedMonitor,
1516   JvmtiFunctions::RunAgentThread,
1517   JvmtiFunctions::GetTopThreadGroups,
1518   JvmtiFunctions::GetThreadGroupInfo,
1519   JvmtiFunctions::GetThreadGroupChildren,
1520   JvmtiFunctions::GetFrameCount,
1521   JvmtiFunctions::GetThreadState,
1522   JvmtiFunctions::GetCurrentThread,
1523   JvmtiFunctions::GetFrameLocation,
1524   JvmtiFunctions::NotifyFramePop,  // 20
1525   JvmtiFunctions::GetLocalObject,
1526   JvmtiFunctions::GetLocalInt,
1527   JvmtiFunctions::GetLocalLong,
1528   JvmtiFunctions::GetLocalFloat,
1529   JvmtiFunctions::GetLocalDouble,
1530   JvmtiFunctions::SetLocalObject,
1531   JvmtiFunctions::SetLocalInt,
1532   JvmtiFunctions::SetLocalLong,
1533   JvmtiFunctions::SetLocalFloat,
1534   JvmtiFunctions::SetLocalDouble,  // 30
1535   JvmtiFunctions::CreateRawMonitor,
1536   JvmtiFunctions::DestroyRawMonitor,
1537   JvmtiFunctions::RawMonitorEnter,
1538   JvmtiFunctions::RawMonitorExit,
1539   JvmtiFunctions::RawMonitorWait,
1540   JvmtiFunctions::RawMonitorNotify,
1541   JvmtiFunctions::RawMonitorNotifyAll,
1542   JvmtiFunctions::SetBreakpoint,
1543   JvmtiFunctions::ClearBreakpoint,
1544   nullptr,  // reserved40
1545   JvmtiFunctions::SetFieldAccessWatch,
1546   JvmtiFunctions::ClearFieldAccessWatch,
1547   JvmtiFunctions::SetFieldModificationWatch,
1548   JvmtiFunctions::ClearFieldModificationWatch,
1549   JvmtiFunctions::IsModifiableClass,
1550   JvmtiFunctions::Allocate,
1551   JvmtiFunctions::Deallocate,
1552   JvmtiFunctions::GetClassSignature,
1553   JvmtiFunctions::GetClassStatus,
1554   JvmtiFunctions::GetSourceFileName,  // 50
1555   JvmtiFunctions::GetClassModifiers,
1556   JvmtiFunctions::GetClassMethods,
1557   JvmtiFunctions::GetClassFields,
1558   JvmtiFunctions::GetImplementedInterfaces,
1559   JvmtiFunctions::IsInterface,
1560   JvmtiFunctions::IsArrayClass,
1561   JvmtiFunctions::GetClassLoader,
1562   JvmtiFunctions::GetObjectHashCode,
1563   JvmtiFunctions::GetObjectMonitorUsage,
1564   JvmtiFunctions::GetFieldName,  // 60
1565   JvmtiFunctions::GetFieldDeclaringClass,
1566   JvmtiFunctions::GetFieldModifiers,
1567   JvmtiFunctions::IsFieldSynthetic,
1568   JvmtiFunctions::GetMethodName,
1569   JvmtiFunctions::GetMethodDeclaringClass,
1570   JvmtiFunctions::GetMethodModifiers,
1571   nullptr,  // reserved67
1572   JvmtiFunctions::GetMaxLocals,
1573   JvmtiFunctions::GetArgumentsSize,
1574   JvmtiFunctions::GetLineNumberTable,  // 70
1575   JvmtiFunctions::GetMethodLocation,
1576   JvmtiFunctions::GetLocalVariableTable,
1577   JvmtiFunctions::SetNativeMethodPrefix,
1578   JvmtiFunctions::SetNativeMethodPrefixes,
1579   JvmtiFunctions::GetBytecodes,
1580   JvmtiFunctions::IsMethodNative,
1581   JvmtiFunctions::IsMethodSynthetic,
1582   JvmtiFunctions::GetLoadedClasses,
1583   JvmtiFunctions::GetClassLoaderClasses,
1584   JvmtiFunctions::PopFrame,  // 80
1585   JvmtiFunctions::ForceEarlyReturnObject,
1586   JvmtiFunctions::ForceEarlyReturnInt,
1587   JvmtiFunctions::ForceEarlyReturnLong,
1588   JvmtiFunctions::ForceEarlyReturnFloat,
1589   JvmtiFunctions::ForceEarlyReturnDouble,
1590   JvmtiFunctions::ForceEarlyReturnVoid,
1591   JvmtiFunctions::RedefineClasses,
1592   JvmtiFunctions::GetVersionNumber,
1593   JvmtiFunctions::GetCapabilities,
1594   JvmtiFunctions::GetSourceDebugExtension,  // 90
1595   JvmtiFunctions::IsMethodObsolete,
1596   JvmtiFunctions::SuspendThreadList,
1597   JvmtiFunctions::ResumeThreadList,
1598   nullptr,  // reserved94
1599   nullptr,  // reserved95
1600   nullptr,  // reserved96
1601   nullptr,  // reserved97
1602   nullptr,  // reserved98
1603   nullptr,  // reserved99
1604   JvmtiFunctions::GetAllStackTraces,  // 100
1605   JvmtiFunctions::GetThreadListStackTraces,
1606   JvmtiFunctions::GetThreadLocalStorage,
1607   JvmtiFunctions::SetThreadLocalStorage,
1608   JvmtiFunctions::GetStackTrace,
1609   nullptr,  // reserved105
1610   JvmtiFunctions::GetTag,
1611   JvmtiFunctions::SetTag,
1612   JvmtiFunctions::ForceGarbageCollection,
1613   JvmtiFunctions::IterateOverObjectsReachableFromObject,
1614   JvmtiFunctions::IterateOverReachableObjects,  // 110
1615   JvmtiFunctions::IterateOverHeap,
1616   JvmtiFunctions::IterateOverInstancesOfClass,
1617   nullptr,  // reserved113
1618   JvmtiFunctions::GetObjectsWithTags,
1619   JvmtiFunctions::FollowReferences,
1620   JvmtiFunctions::IterateThroughHeap,
1621   nullptr,  // reserved117
1622   nullptr,  // reserved118
1623   nullptr,  // reserved119
1624   JvmtiFunctions::SetJNIFunctionTable,  // 120
1625   JvmtiFunctions::GetJNIFunctionTable,
1626   JvmtiFunctions::SetEventCallbacks,
1627   JvmtiFunctions::GenerateEvents,
1628   JvmtiFunctions::GetExtensionFunctions,
1629   JvmtiFunctions::GetExtensionEvents,
1630   JvmtiFunctions::SetExtensionEventCallback,
1631   JvmtiFunctions::DisposeEnvironment,
1632   JvmtiFunctions::GetErrorName,
1633   JvmtiFunctions::GetJLocationFormat,
1634   JvmtiFunctions::GetSystemProperties,  // 130
1635   JvmtiFunctions::GetSystemProperty,
1636   JvmtiFunctions::SetSystemProperty,
1637   JvmtiFunctions::GetPhase,
1638   JvmtiFunctions::GetCurrentThreadCpuTimerInfo,
1639   JvmtiFunctions::GetCurrentThreadCpuTime,
1640   JvmtiFunctions::GetThreadCpuTimerInfo,
1641   JvmtiFunctions::GetThreadCpuTime,
1642   JvmtiFunctions::GetTimerInfo,
1643   JvmtiFunctions::GetTime,
1644   JvmtiFunctions::GetPotentialCapabilities,  // 140
1645   nullptr,  // reserved141
1646   JvmtiFunctions::AddCapabilities,
1647   JvmtiFunctions::RelinquishCapabilities,
1648   JvmtiFunctions::GetAvailableProcessors,
1649   JvmtiFunctions::GetClassVersionNumbers,
1650   JvmtiFunctions::GetConstantPool,
1651   JvmtiFunctions::GetEnvironmentLocalStorage,
1652   JvmtiFunctions::SetEnvironmentLocalStorage,
1653   JvmtiFunctions::AddToBootstrapClassLoaderSearch,
1654   JvmtiFunctions::SetVerboseFlag,  // 150
1655   JvmtiFunctions::AddToSystemClassLoaderSearch,
1656   JvmtiFunctions::RetransformClasses,
1657   JvmtiFunctions::GetOwnedMonitorStackDepthInfo,
1658   JvmtiFunctions::GetObjectSize,
1659   JvmtiFunctions::GetLocalInstance,
1660 };
1661 
1662 };  // namespace openjdkjvmti
1663