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