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