blob: 3a6fcef784092341d3f9d2abbee2c30e067ff09d [file] [log] [blame]
Alex Light49948e92016-08-11 15:35:28 -07001/* Copyright (C) 2016 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
Yi Kongc57c6802018-10-29 14:28:56 -070032#include <memory>
Alex Light9c20a142016-08-23 15:05:12 -070033#include <string>
Andreas Gampef37e3022017-01-13 17:54:46 -080034#include <type_traits>
Alex Light9c20a142016-08-23 15:05:12 -070035#include <vector>
36
Andreas Gampe57943812017-12-06 21:39:13 -080037#include <android-base/logging.h>
38
Alex Light49948e92016-08-11 15:35:28 -070039#include <jni.h>
Alex Light9c20a142016-08-23 15:05:12 -070040
Andreas Gampe5e03a302017-03-13 13:10:00 -070041#include "jvmti.h"
Alex Light49948e92016-08-11 15:35:28 -070042
Andreas Gampedb6dcb62016-09-13 09:05:59 -070043#include "art_jvmti.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080044#include "base/logging.h" // For gLogVerbosity.
Andreas Gampe77708d92016-10-07 11:48:21 -070045#include "base/mutex.h"
46#include "events-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010047#include "jni/jni_env_ext-inl.h"
Andreas Gampe6dee92e2016-09-12 19:58:13 -070048#include "obj_ptr-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080049#include "object_tagging.h"
Alex Light9c20a142016-08-23 15:05:12 -070050#include "runtime.h"
Andreas Gampe6dee92e2016-09-12 19:58:13 -070051#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070052#include "thread-current-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080053#include "thread_list.h"
Alex Lightbf64a572017-07-05 10:34:49 -070054#include "ti_allocator.h"
Alex Lighta26e3492017-06-27 17:55:37 -070055#include "ti_breakpoint.h"
Andreas Gampee492ae32016-10-28 19:34:57 -070056#include "ti_class.h"
Andreas Gampeeb0cea12017-01-23 08:50:04 -080057#include "ti_dump.h"
Alex Light3f33c0a2017-11-08 10:17:37 -080058#include "ti_extension.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080059#include "ti_field.h"
Andreas Gampeba8df692016-11-01 10:30:44 -070060#include "ti_heap.h"
Andreas Gampe6f8e4f02017-01-16 18:18:14 -080061#include "ti_jni.h"
Alex Lightae45cbb2018-10-18 15:49:56 -070062#include "ti_logging.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070063#include "ti_method.h"
Andreas Gampe319dbe82017-01-09 16:42:21 -080064#include "ti_monitor.h"
Andreas Gampe50a4e492017-01-06 18:00:20 -080065#include "ti_object.h"
Andreas Gampe96eca782017-01-19 19:45:30 -080066#include "ti_phase.h"
Andreas Gampe1bdaf732017-01-09 19:21:06 -080067#include "ti_properties.h"
Alex Lighta01de592016-11-15 10:43:06 -080068#include "ti_redefine.h"
Andreas Gampece7732b2017-01-17 15:50:26 -080069#include "ti_search.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070070#include "ti_stack.h"
Andreas Gampeaf13ab92017-01-11 20:57:40 -080071#include "ti_thread.h"
Andreas Gamped18d9e22017-01-16 16:08:45 +000072#include "ti_threadgroup.h"
Andreas Gampe35bcf812017-01-13 16:24:17 -080073#include "ti_timers.h"
Alex Light9c20a142016-08-23 15:05:12 -070074#include "transform.h"
Alex Light49948e92016-08-11 15:35:28 -070075
Alex Light49948e92016-08-11 15:35:28 -070076namespace openjdkjvmti {
77
Alex Light0e841182018-02-12 17:42:50 +000078// NB These are heap allocated to avoid the static destructors being run if an agent calls exit(3).
79// These should never be null.
80EventHandler* gEventHandler;
81DeoptManager* gDeoptManager;
Andreas Gampe6dee92e2016-09-12 19:58:13 -070082
Alex Lighte6574242016-08-17 09:56:24 -070083#define ENSURE_NON_NULL(n) \
84 do { \
85 if ((n) == nullptr) { \
86 return ERR(NULL_POINTER); \
87 } \
88 } while (false)
89
Alex Light2ce6fc82017-12-18 16:42:36 -080090// Returns whether we are able to use all jvmti features.
91static bool IsFullJvmtiAvailable() {
92 art::Runtime* runtime = art::Runtime::Current();
93 return runtime->GetInstrumentation()->IsForcedInterpretOnly() || runtime->IsJavaDebuggable();
94}
95
Alex Light49948e92016-08-11 15:35:28 -070096class JvmtiFunctions {
97 private:
Alex Light1edc8cf2017-03-24 14:22:56 -070098 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 }
Alex Light49948e92016-08-11 15:35:28 -0700106 }
107
Alex Light1edc8cf2017-03-24 14:22:56 -0700108#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 } \
Alex Lighte6574242016-08-17 09:56:24 -0700114 } while (false)
115
116#define ENSURE_HAS_CAP(env, cap) \
117 do { \
Alex Lighte6574242016-08-17 09:56:24 -0700118 if (ArtJvmTiEnv::AsArtJvmTiEnv(env)->capabilities.cap != 1) { \
119 return ERR(MUST_POSSESS_CAPABILITY); \
120 } \
121 } while (false)
122
Alex Light49948e92016-08-11 15:35:28 -0700123 public:
124 static jvmtiError Allocate(jvmtiEnv* env, jlong size, unsigned char** mem_ptr) {
Alex Light6a399f32018-01-10 16:31:55 -0800125 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 }
Alex Lighte6574242016-08-17 09:56:24 -0700130 ENSURE_NON_NULL(mem_ptr);
Alex Lightbf64a572017-07-05 10:34:49 -0700131 return AllocUtil::Allocate(env, size, mem_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700132 }
133
134 static jvmtiError Deallocate(jvmtiEnv* env, unsigned char* mem) {
Alex Light6a399f32018-01-10 16:31:55 -0800135 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 }
Alex Lightbf64a572017-07-05 10:34:49 -0700140 return AllocUtil::Deallocate(env, mem);
Alex Light49948e92016-08-11 15:35:28 -0700141 }
142
143 static jvmtiError GetThreadState(jvmtiEnv* env, jthread thread, jint* thread_state_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700144 ENSURE_VALID_ENV(env);
Andreas Gampe72c19832017-01-12 13:22:16 -0800145 return ThreadUtil::GetThreadState(env, thread, thread_state_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700146 }
147
148 static jvmtiError GetCurrentThread(jvmtiEnv* env, jthread* thread_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700149 ENSURE_VALID_ENV(env);
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800150 return ThreadUtil::GetCurrentThread(env, thread_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700151 }
152
153 static jvmtiError GetAllThreads(jvmtiEnv* env, jint* threads_count_ptr, jthread** threads_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700154 ENSURE_VALID_ENV(env);
Andreas Gampe85807442017-01-13 14:40:58 -0800155 return ThreadUtil::GetAllThreads(env, threads_count_ptr, threads_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700156 }
157
Alex Light88fd7202017-06-30 08:31:59 -0700158 static jvmtiError SuspendThread(jvmtiEnv* env, jthread thread) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700159 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800160 ENSURE_HAS_CAP(env, can_suspend);
Alex Light88fd7202017-06-30 08:31:59 -0700161 return ThreadUtil::SuspendThread(env, thread);
Alex Light49948e92016-08-11 15:35:28 -0700162 }
163
164 static jvmtiError SuspendThreadList(jvmtiEnv* env,
Alex Light88fd7202017-06-30 08:31:59 -0700165 jint request_count,
166 const jthread* request_list,
167 jvmtiError* results) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700168 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800169 ENSURE_HAS_CAP(env, can_suspend);
Alex Light88fd7202017-06-30 08:31:59 -0700170 return ThreadUtil::SuspendThreadList(env, request_count, request_list, results);
Alex Light49948e92016-08-11 15:35:28 -0700171 }
172
Alex Light88fd7202017-06-30 08:31:59 -0700173 static jvmtiError ResumeThread(jvmtiEnv* env, jthread thread) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700174 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800175 ENSURE_HAS_CAP(env, can_suspend);
Alex Light88fd7202017-06-30 08:31:59 -0700176 return ThreadUtil::ResumeThread(env, thread);
Alex Light49948e92016-08-11 15:35:28 -0700177 }
178
179 static jvmtiError ResumeThreadList(jvmtiEnv* env,
Alex Light88fd7202017-06-30 08:31:59 -0700180 jint request_count,
181 const jthread* request_list,
182 jvmtiError* results) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700183 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800184 ENSURE_HAS_CAP(env, can_suspend);
Alex Light88fd7202017-06-30 08:31:59 -0700185 return ThreadUtil::ResumeThreadList(env, request_count, request_list, results);
Alex Light49948e92016-08-11 15:35:28 -0700186 }
187
Alex Light54d39dc2017-09-25 17:00:16 -0700188 static jvmtiError StopThread(jvmtiEnv* env, jthread thread, jobject exception) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700189 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800190 ENSURE_HAS_CAP(env, can_signal_thread);
Alex Light54d39dc2017-09-25 17:00:16 -0700191 return ThreadUtil::StopThread(env, thread, exception);
Alex Light49948e92016-08-11 15:35:28 -0700192 }
193
Alex Light54d39dc2017-09-25 17:00:16 -0700194 static jvmtiError InterruptThread(jvmtiEnv* env, jthread thread) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700195 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800196 ENSURE_HAS_CAP(env, can_signal_thread);
Alex Light54d39dc2017-09-25 17:00:16 -0700197 return ThreadUtil::InterruptThread(env, thread);
Alex Light49948e92016-08-11 15:35:28 -0700198 }
199
200 static jvmtiError GetThreadInfo(jvmtiEnv* env, jthread thread, jvmtiThreadInfo* info_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700201 ENSURE_VALID_ENV(env);
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800202 return ThreadUtil::GetThreadInfo(env, thread, info_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700203 }
204
205 static jvmtiError GetOwnedMonitorInfo(jvmtiEnv* env,
Alex Light88e1ddd2017-08-21 13:09:55 -0700206 jthread thread,
207 jint* owned_monitor_count_ptr,
208 jobject** owned_monitors_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700209 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800210 ENSURE_HAS_CAP(env, can_get_owned_monitor_info);
Alex Light88e1ddd2017-08-21 13:09:55 -0700211 return StackUtil::GetOwnedMonitorInfo(env,
212 thread,
213 owned_monitor_count_ptr,
214 owned_monitors_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700215 }
216
Alex Light88e1ddd2017-08-21 13:09:55 -0700217 static jvmtiError GetOwnedMonitorStackDepthInfo(jvmtiEnv* env,
218 jthread thread,
219 jint* monitor_info_count_ptr,
220 jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700221 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800222 ENSURE_HAS_CAP(env, can_get_owned_monitor_stack_depth_info);
Alex Light88e1ddd2017-08-21 13:09:55 -0700223 return StackUtil::GetOwnedMonitorStackDepthInfo(env,
224 thread,
225 monitor_info_count_ptr,
226 monitor_info_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700227 }
228
229 static jvmtiError GetCurrentContendedMonitor(jvmtiEnv* env,
Alex Light41006c62017-09-14 09:51:14 -0700230 jthread thread,
231 jobject* monitor_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700232 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800233 ENSURE_HAS_CAP(env, can_get_current_contended_monitor);
Alex Light41006c62017-09-14 09:51:14 -0700234 return MonitorUtil::GetCurrentContendedMonitor(env, thread, monitor_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700235 }
236
237 static jvmtiError RunAgentThread(jvmtiEnv* env,
238 jthread thread,
239 jvmtiStartFunction proc,
240 const void* arg,
241 jint priority) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700242 ENSURE_VALID_ENV(env);
Andreas Gampe732b0ac2017-01-18 15:23:39 -0800243 return ThreadUtil::RunAgentThread(env, thread, proc, arg, priority);
Alex Light49948e92016-08-11 15:35:28 -0700244 }
245
246 static jvmtiError SetThreadLocalStorage(jvmtiEnv* env, jthread thread, const void* data) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700247 ENSURE_VALID_ENV(env);
Andreas Gampe7b3b3262017-01-19 20:40:42 -0800248 return ThreadUtil::SetThreadLocalStorage(env, thread, data);
Alex Light49948e92016-08-11 15:35:28 -0700249 }
250
251 static jvmtiError GetThreadLocalStorage(jvmtiEnv* env, jthread thread, void** data_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700252 ENSURE_VALID_ENV(env);
Andreas Gampe7b3b3262017-01-19 20:40:42 -0800253 return ThreadUtil::GetThreadLocalStorage(env, thread, data_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700254 }
255
256 static jvmtiError GetTopThreadGroups(jvmtiEnv* env,
257 jint* group_count_ptr,
258 jthreadGroup** groups_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700259 ENSURE_VALID_ENV(env);
Andreas Gamped18d9e22017-01-16 16:08:45 +0000260 return ThreadGroupUtil::GetTopThreadGroups(env, group_count_ptr, groups_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700261 }
262
263 static jvmtiError GetThreadGroupInfo(jvmtiEnv* env,
264 jthreadGroup group,
265 jvmtiThreadGroupInfo* info_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700266 ENSURE_VALID_ENV(env);
Andreas Gamped18d9e22017-01-16 16:08:45 +0000267 return ThreadGroupUtil::GetThreadGroupInfo(env, group, info_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700268 }
269
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) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700276 ENSURE_VALID_ENV(env);
Andreas Gamped18d9e22017-01-16 16:08:45 +0000277 return ThreadGroupUtil::GetThreadGroupChildren(env,
278 group,
279 thread_count_ptr,
280 threads_ptr,
281 group_count_ptr,
282 groups_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700283 }
284
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) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700291 ENSURE_VALID_ENV(env);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700292 return StackUtil::GetStackTrace(env,
293 thread,
294 start_depth,
295 max_frame_count,
296 frame_buffer,
297 count_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700298 }
299
300 static jvmtiError GetAllStackTraces(jvmtiEnv* env,
301 jint max_frame_count,
302 jvmtiStackInfo** stack_info_ptr,
303 jint* thread_count_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700304 ENSURE_VALID_ENV(env);
Andreas Gampea1a27c62017-01-11 16:37:16 -0800305 return StackUtil::GetAllStackTraces(env, max_frame_count, stack_info_ptr, thread_count_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700306 }
307
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) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700313 ENSURE_VALID_ENV(env);
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800314 return StackUtil::GetThreadListStackTraces(env,
315 thread_count,
316 thread_list,
317 max_frame_count,
318 stack_info_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700319 }
320
321 static jvmtiError GetFrameCount(jvmtiEnv* env, jthread thread, jint* count_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700322 ENSURE_VALID_ENV(env);
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800323 return StackUtil::GetFrameCount(env, thread, count_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700324 }
325
Alex Light0aa7a5a2018-10-10 15:58:14 +0000326 static jvmtiError PopFrame(jvmtiEnv* env, jthread thread) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700327 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800328 ENSURE_HAS_CAP(env, can_pop_frame);
Alex Light0aa7a5a2018-10-10 15:58:14 +0000329 return StackUtil::PopFrame(env, thread);
Alex Light49948e92016-08-11 15:35:28 -0700330 }
331
332 static jvmtiError GetFrameLocation(jvmtiEnv* env,
333 jthread thread,
334 jint depth,
335 jmethodID* method_ptr,
336 jlocation* location_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700337 ENSURE_VALID_ENV(env);
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800338 return StackUtil::GetFrameLocation(env, thread, depth, method_ptr, location_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700339 }
340
Alex Lighte814f9d2017-07-31 16:14:39 -0700341 static jvmtiError NotifyFramePop(jvmtiEnv* env, jthread thread, jint depth) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700342 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800343 ENSURE_HAS_CAP(env, can_generate_frame_pop_events);
Alex Lighte814f9d2017-07-31 16:14:39 -0700344 return StackUtil::NotifyFramePop(env, thread, depth);
Alex Light49948e92016-08-11 15:35:28 -0700345 }
346
Alex Lightb7c640d2019-03-20 15:52:13 -0700347 static jvmtiError ForceEarlyReturnObject(jvmtiEnv* env, jthread thread, jobject value) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700348 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800349 ENSURE_HAS_CAP(env, can_force_early_return);
Alex Lightb7c640d2019-03-20 15:52:13 -0700350 return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
Alex Light49948e92016-08-11 15:35:28 -0700351 }
352
Alex Lightb7c640d2019-03-20 15:52:13 -0700353 static jvmtiError ForceEarlyReturnInt(jvmtiEnv* env, jthread thread, jint value) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700354 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800355 ENSURE_HAS_CAP(env, can_force_early_return);
Alex Lightb7c640d2019-03-20 15:52:13 -0700356 return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
Alex Light49948e92016-08-11 15:35:28 -0700357 }
358
Alex Lightb7c640d2019-03-20 15:52:13 -0700359 static jvmtiError ForceEarlyReturnLong(jvmtiEnv* env, jthread thread, jlong value) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700360 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800361 ENSURE_HAS_CAP(env, can_force_early_return);
Alex Lightb7c640d2019-03-20 15:52:13 -0700362 return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
Alex Light49948e92016-08-11 15:35:28 -0700363 }
364
Alex Lightb7c640d2019-03-20 15:52:13 -0700365 static jvmtiError ForceEarlyReturnFloat(jvmtiEnv* env, jthread thread, jfloat value) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700366 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800367 ENSURE_HAS_CAP(env, can_force_early_return);
Alex Lightb7c640d2019-03-20 15:52:13 -0700368 return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
Alex Light49948e92016-08-11 15:35:28 -0700369 }
370
Alex Lightb7c640d2019-03-20 15:52:13 -0700371 static jvmtiError ForceEarlyReturnDouble(jvmtiEnv* env, jthread thread, jdouble value) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700372 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800373 ENSURE_HAS_CAP(env, can_force_early_return);
Alex Lightb7c640d2019-03-20 15:52:13 -0700374 return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
Alex Light49948e92016-08-11 15:35:28 -0700375 }
376
Alex Lightb7c640d2019-03-20 15:52:13 -0700377 static jvmtiError ForceEarlyReturnVoid(jvmtiEnv* env, jthread thread) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700378 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800379 ENSURE_HAS_CAP(env, can_force_early_return);
Alex Lightb7c640d2019-03-20 15:52:13 -0700380 return StackUtil::ForceEarlyReturn<nullptr_t>(env, gEventHandler, thread, nullptr);
Alex Light49948e92016-08-11 15:35:28 -0700381 }
382
383 static jvmtiError FollowReferences(jvmtiEnv* env,
384 jint heap_filter,
385 jclass klass,
386 jobject initial_object,
387 const jvmtiHeapCallbacks* callbacks,
388 const void* user_data) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700389 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800390 ENSURE_HAS_CAP(env, can_tag_objects);
Andreas Gampede19eb92017-02-24 16:21:18 -0800391 HeapUtil heap_util(ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700392 return heap_util.FollowReferences(env,
393 heap_filter,
394 klass,
395 initial_object,
396 callbacks,
397 user_data);
Alex Light49948e92016-08-11 15:35:28 -0700398 }
399
400 static jvmtiError IterateThroughHeap(jvmtiEnv* env,
401 jint heap_filter,
402 jclass klass,
403 const jvmtiHeapCallbacks* callbacks,
404 const void* user_data) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700405 ENSURE_VALID_ENV(env);
Alex Lighte6574242016-08-17 09:56:24 -0700406 ENSURE_HAS_CAP(env, can_tag_objects);
Andreas Gampede19eb92017-02-24 16:21:18 -0800407 HeapUtil heap_util(ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
Andreas Gampee54d9922016-10-11 19:55:37 -0700408 return heap_util.IterateThroughHeap(env, heap_filter, klass, callbacks, user_data);
Alex Light49948e92016-08-11 15:35:28 -0700409 }
410
411 static jvmtiError GetTag(jvmtiEnv* env, jobject object, jlong* tag_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700412 ENSURE_VALID_ENV(env);
Alex Lighte6574242016-08-17 09:56:24 -0700413 ENSURE_HAS_CAP(env, can_tag_objects);
Andreas Gampe6dee92e2016-09-12 19:58:13 -0700414
415 JNIEnv* jni_env = GetJniEnv(env);
416 if (jni_env == nullptr) {
417 return ERR(INTERNAL);
418 }
419
420 art::ScopedObjectAccess soa(jni_env);
421 art::ObjPtr<art::mirror::Object> obj = soa.Decode<art::mirror::Object>(object);
Andreas Gampede19eb92017-02-24 16:21:18 -0800422 if (!ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table->GetTag(obj.Ptr(), tag_ptr)) {
Andreas Gampe6dee92e2016-09-12 19:58:13 -0700423 *tag_ptr = 0;
424 }
425
426 return ERR(NONE);
Alex Light49948e92016-08-11 15:35:28 -0700427 }
428
429 static jvmtiError SetTag(jvmtiEnv* env, jobject object, jlong tag) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700430 ENSURE_VALID_ENV(env);
Alex Lighte6574242016-08-17 09:56:24 -0700431 ENSURE_HAS_CAP(env, can_tag_objects);
432
Andreas Gampe6dee92e2016-09-12 19:58:13 -0700433 if (object == nullptr) {
434 return ERR(NULL_POINTER);
435 }
436
437 JNIEnv* jni_env = GetJniEnv(env);
438 if (jni_env == nullptr) {
439 return ERR(INTERNAL);
440 }
441
442 art::ScopedObjectAccess soa(jni_env);
443 art::ObjPtr<art::mirror::Object> obj = soa.Decode<art::mirror::Object>(object);
Andreas Gampede19eb92017-02-24 16:21:18 -0800444 ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table->Set(obj.Ptr(), tag);
Andreas Gampe6dee92e2016-09-12 19:58:13 -0700445
446 return ERR(NONE);
Alex Light49948e92016-08-11 15:35:28 -0700447 }
448
449 static jvmtiError GetObjectsWithTags(jvmtiEnv* env,
450 jint tag_count,
451 const jlong* tags,
452 jint* count_ptr,
453 jobject** object_result_ptr,
454 jlong** tag_result_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700455 ENSURE_VALID_ENV(env);
Alex Lighte6574242016-08-17 09:56:24 -0700456 ENSURE_HAS_CAP(env, can_tag_objects);
457
Andreas Gampe5e6046b2016-10-25 12:05:53 -0700458 JNIEnv* jni_env = GetJniEnv(env);
459 if (jni_env == nullptr) {
460 return ERR(INTERNAL);
461 }
462
463 art::ScopedObjectAccess soa(jni_env);
Andreas Gampede19eb92017-02-24 16:21:18 -0800464 return ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table->GetTaggedObjects(env,
465 tag_count,
466 tags,
467 count_ptr,
468 object_result_ptr,
469 tag_result_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700470 }
471
472 static jvmtiError ForceGarbageCollection(jvmtiEnv* env) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700473 ENSURE_VALID_ENV(env);
Andreas Gampe8da6d032016-10-31 19:31:03 -0700474 return HeapUtil::ForceGarbageCollection(env);
Alex Light49948e92016-08-11 15:35:28 -0700475 }
476
477 static jvmtiError IterateOverObjectsReachableFromObject(
478 jvmtiEnv* env,
Alex Light6a3fd512017-02-27 14:34:32 -0800479 jobject object ATTRIBUTE_UNUSED,
480 jvmtiObjectReferenceCallback object_reference_callback ATTRIBUTE_UNUSED,
481 const void* user_data ATTRIBUTE_UNUSED) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700482 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800483 ENSURE_HAS_CAP(env, can_tag_objects);
Alex Light49948e92016-08-11 15:35:28 -0700484 return ERR(NOT_IMPLEMENTED);
485 }
486
Alex Light6a3fd512017-02-27 14:34:32 -0800487 static jvmtiError IterateOverReachableObjects(
488 jvmtiEnv* env,
489 jvmtiHeapRootCallback heap_root_callback ATTRIBUTE_UNUSED,
490 jvmtiStackReferenceCallback stack_ref_callback ATTRIBUTE_UNUSED,
491 jvmtiObjectReferenceCallback object_ref_callback ATTRIBUTE_UNUSED,
492 const void* user_data ATTRIBUTE_UNUSED) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700493 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800494 ENSURE_HAS_CAP(env, can_tag_objects);
Alex Light49948e92016-08-11 15:35:28 -0700495 return ERR(NOT_IMPLEMENTED);
496 }
497
498 static jvmtiError IterateOverHeap(jvmtiEnv* env,
Alex Light6a3fd512017-02-27 14:34:32 -0800499 jvmtiHeapObjectFilter object_filter ATTRIBUTE_UNUSED,
500 jvmtiHeapObjectCallback heap_object_callback ATTRIBUTE_UNUSED,
501 const void* user_data ATTRIBUTE_UNUSED) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700502 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800503 ENSURE_HAS_CAP(env, can_tag_objects);
Alex Light49948e92016-08-11 15:35:28 -0700504 return ERR(NOT_IMPLEMENTED);
505 }
506
Alex Light6a3fd512017-02-27 14:34:32 -0800507 static jvmtiError IterateOverInstancesOfClass(
508 jvmtiEnv* env,
Alex Lightbbbcb532018-08-30 12:50:27 -0700509 jclass klass,
510 jvmtiHeapObjectFilter object_filter,
511 jvmtiHeapObjectCallback heap_object_callback,
512 const void* user_data) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700513 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800514 ENSURE_HAS_CAP(env, can_tag_objects);
Alex Lightbbbcb532018-08-30 12:50:27 -0700515 HeapUtil heap_util(ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
516 return heap_util.IterateOverInstancesOfClass(
517 env, klass, object_filter, heap_object_callback, user_data);
Alex Light49948e92016-08-11 15:35:28 -0700518 }
519
520 static jvmtiError GetLocalObject(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700521 jthread thread,
522 jint depth,
523 jint slot,
524 jobject* value_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700525 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800526 ENSURE_HAS_CAP(env, can_access_local_variables);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700527 return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700528 }
529
530 static jvmtiError GetLocalInstance(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700531 jthread thread,
532 jint depth,
533 jobject* value_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700534 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800535 ENSURE_HAS_CAP(env, can_access_local_variables);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700536 return MethodUtil::GetLocalInstance(env, thread, depth, value_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700537 }
538
539 static jvmtiError GetLocalInt(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700540 jthread thread,
541 jint depth,
542 jint slot,
543 jint* value_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700544 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800545 ENSURE_HAS_CAP(env, can_access_local_variables);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700546 return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700547 }
548
549 static jvmtiError GetLocalLong(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700550 jthread thread,
551 jint depth,
552 jint slot,
553 jlong* value_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700554 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800555 ENSURE_HAS_CAP(env, can_access_local_variables);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700556 return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700557 }
558
559 static jvmtiError GetLocalFloat(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700560 jthread thread,
561 jint depth,
562 jint slot,
563 jfloat* value_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700564 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800565 ENSURE_HAS_CAP(env, can_access_local_variables);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700566 return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700567 }
568
569 static jvmtiError GetLocalDouble(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700570 jthread thread,
571 jint depth,
572 jint slot,
573 jdouble* value_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700574 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800575 ENSURE_HAS_CAP(env, can_access_local_variables);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700576 return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700577 }
578
579 static jvmtiError SetLocalObject(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700580 jthread thread,
581 jint depth,
582 jint slot,
583 jobject value) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700584 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800585 ENSURE_HAS_CAP(env, can_access_local_variables);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700586 return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
Alex Light49948e92016-08-11 15:35:28 -0700587 }
588
589 static jvmtiError SetLocalInt(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700590 jthread thread,
591 jint depth,
592 jint slot,
593 jint value) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700594 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800595 ENSURE_HAS_CAP(env, can_access_local_variables);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700596 return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
Alex Light49948e92016-08-11 15:35:28 -0700597 }
598
599 static jvmtiError SetLocalLong(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700600 jthread thread,
601 jint depth,
602 jint slot,
603 jlong value) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700604 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800605 ENSURE_HAS_CAP(env, can_access_local_variables);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700606 return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
Alex Light49948e92016-08-11 15:35:28 -0700607 }
608
609 static jvmtiError SetLocalFloat(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700610 jthread thread,
611 jint depth,
612 jint slot,
613 jfloat value) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700614 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800615 ENSURE_HAS_CAP(env, can_access_local_variables);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700616 return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
Alex Light49948e92016-08-11 15:35:28 -0700617 }
618
619 static jvmtiError SetLocalDouble(jvmtiEnv* env,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700620 jthread thread,
621 jint depth,
622 jint slot,
623 jdouble value) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700624 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800625 ENSURE_HAS_CAP(env, can_access_local_variables);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700626 return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
Alex Light49948e92016-08-11 15:35:28 -0700627 }
628
Alex Lighta26e3492017-06-27 17:55:37 -0700629
630 static jvmtiError SetBreakpoint(jvmtiEnv* env, jmethodID method, jlocation location) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700631 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800632 ENSURE_HAS_CAP(env, can_generate_breakpoint_events);
Alex Lighta26e3492017-06-27 17:55:37 -0700633 return BreakpointUtil::SetBreakpoint(env, method, location);
Alex Light49948e92016-08-11 15:35:28 -0700634 }
635
Alex Lighta26e3492017-06-27 17:55:37 -0700636 static jvmtiError ClearBreakpoint(jvmtiEnv* env, jmethodID method, jlocation location) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700637 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800638 ENSURE_HAS_CAP(env, can_generate_breakpoint_events);
Alex Lighta26e3492017-06-27 17:55:37 -0700639 return BreakpointUtil::ClearBreakpoint(env, method, location);
Alex Light49948e92016-08-11 15:35:28 -0700640 }
641
Alex Light084fa372017-06-16 08:58:34 -0700642 static jvmtiError SetFieldAccessWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700643 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800644 ENSURE_HAS_CAP(env, can_generate_field_access_events);
Alex Light084fa372017-06-16 08:58:34 -0700645 return FieldUtil::SetFieldAccessWatch(env, klass, field);
Alex Light49948e92016-08-11 15:35:28 -0700646 }
647
Alex Light084fa372017-06-16 08:58:34 -0700648 static jvmtiError ClearFieldAccessWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700649 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800650 ENSURE_HAS_CAP(env, can_generate_field_access_events);
Alex Light084fa372017-06-16 08:58:34 -0700651 return FieldUtil::ClearFieldAccessWatch(env, klass, field);
Alex Light49948e92016-08-11 15:35:28 -0700652 }
653
Alex Light084fa372017-06-16 08:58:34 -0700654 static jvmtiError SetFieldModificationWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700655 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800656 ENSURE_HAS_CAP(env, can_generate_field_modification_events);
Alex Light084fa372017-06-16 08:58:34 -0700657 return FieldUtil::SetFieldModificationWatch(env, klass, field);
Alex Light49948e92016-08-11 15:35:28 -0700658 }
659
Alex Light084fa372017-06-16 08:58:34 -0700660 static jvmtiError ClearFieldModificationWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700661 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800662 ENSURE_HAS_CAP(env, can_generate_field_modification_events);
Alex Light084fa372017-06-16 08:58:34 -0700663 return FieldUtil::ClearFieldModificationWatch(env, klass, field);
Alex Light49948e92016-08-11 15:35:28 -0700664 }
665
666 static jvmtiError GetLoadedClasses(jvmtiEnv* env, jint* class_count_ptr, jclass** classes_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700667 ENSURE_VALID_ENV(env);
Andreas Gampede19eb92017-02-24 16:21:18 -0800668 HeapUtil heap_util(ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
Andreas Gampeaa8b60c2016-10-12 12:51:25 -0700669 return heap_util.GetLoadedClasses(env, class_count_ptr, classes_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700670 }
671
672 static jvmtiError GetClassLoaderClasses(jvmtiEnv* env,
673 jobject initiating_loader,
674 jint* class_count_ptr,
675 jclass** classes_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700676 ENSURE_VALID_ENV(env);
Andreas Gampe70f16392017-01-16 14:20:10 -0800677 return ClassUtil::GetClassLoaderClasses(env, initiating_loader, class_count_ptr, classes_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700678 }
679
680 static jvmtiError GetClassSignature(jvmtiEnv* env,
681 jclass klass,
682 char** signature_ptr,
683 char** generic_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700684 ENSURE_VALID_ENV(env);
Andreas Gampee492ae32016-10-28 19:34:57 -0700685 return ClassUtil::GetClassSignature(env, klass, signature_ptr, generic_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700686 }
687
688 static jvmtiError GetClassStatus(jvmtiEnv* env, jclass klass, jint* status_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700689 ENSURE_VALID_ENV(env);
Andreas Gampeff9d2092017-01-06 09:12:49 -0800690 return ClassUtil::GetClassStatus(env, klass, status_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700691 }
692
Alex Light6fa7b812017-06-16 09:04:29 -0700693 static jvmtiError GetSourceFileName(jvmtiEnv* env, jclass klass, char** source_name_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700694 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800695 ENSURE_HAS_CAP(env, can_get_source_file_name);
Alex Light6fa7b812017-06-16 09:04:29 -0700696 return ClassUtil::GetSourceFileName(env, klass, source_name_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700697 }
698
699 static jvmtiError GetClassModifiers(jvmtiEnv* env, jclass klass, jint* modifiers_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700700 ENSURE_VALID_ENV(env);
Andreas Gampe64013e52017-01-06 13:07:19 -0800701 return ClassUtil::GetClassModifiers(env, klass, modifiers_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700702 }
703
704 static jvmtiError GetClassMethods(jvmtiEnv* env,
705 jclass klass,
706 jint* method_count_ptr,
707 jmethodID** methods_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700708 ENSURE_VALID_ENV(env);
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800709 return ClassUtil::GetClassMethods(env, klass, method_count_ptr, methods_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700710 }
711
712 static jvmtiError GetClassFields(jvmtiEnv* env,
713 jclass klass,
714 jint* field_count_ptr,
715 jfieldID** fields_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700716 ENSURE_VALID_ENV(env);
Andreas Gampeac587272017-01-05 15:21:34 -0800717 return ClassUtil::GetClassFields(env, klass, field_count_ptr, fields_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700718 }
719
720 static jvmtiError GetImplementedInterfaces(jvmtiEnv* env,
721 jclass klass,
722 jint* interface_count_ptr,
723 jclass** interfaces_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700724 ENSURE_VALID_ENV(env);
Andreas Gampe8b07e472017-01-06 14:20:39 -0800725 return ClassUtil::GetImplementedInterfaces(env, klass, interface_count_ptr, interfaces_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700726 }
727
728 static jvmtiError GetClassVersionNumbers(jvmtiEnv* env,
729 jclass klass,
730 jint* minor_version_ptr,
731 jint* major_version_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700732 ENSURE_VALID_ENV(env);
Andreas Gampe812a2442017-01-19 22:04:46 -0800733 return ClassUtil::GetClassVersionNumbers(env, klass, minor_version_ptr, major_version_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700734 }
735
736 static jvmtiError GetConstantPool(jvmtiEnv* env,
Alex Light6a3fd512017-02-27 14:34:32 -0800737 jclass klass ATTRIBUTE_UNUSED,
738 jint* constant_pool_count_ptr ATTRIBUTE_UNUSED,
739 jint* constant_pool_byte_count_ptr ATTRIBUTE_UNUSED,
740 unsigned char** constant_pool_bytes_ptr ATTRIBUTE_UNUSED) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700741 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800742 ENSURE_HAS_CAP(env, can_get_constant_pool);
Alex Light49948e92016-08-11 15:35:28 -0700743 return ERR(NOT_IMPLEMENTED);
744 }
745
746 static jvmtiError IsInterface(jvmtiEnv* env, jclass klass, jboolean* is_interface_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700747 ENSURE_VALID_ENV(env);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800748 return ClassUtil::IsInterface(env, klass, is_interface_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700749 }
750
751 static jvmtiError IsArrayClass(jvmtiEnv* env,
752 jclass klass,
753 jboolean* is_array_class_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700754 ENSURE_VALID_ENV(env);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800755 return ClassUtil::IsArrayClass(env, klass, is_array_class_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700756 }
757
758 static jvmtiError IsModifiableClass(jvmtiEnv* env,
759 jclass klass,
760 jboolean* is_modifiable_class_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700761 ENSURE_VALID_ENV(env);
Alex Lighte4a88632017-01-10 07:41:24 -0800762 return Redefiner::IsModifiableClass(env, klass, is_modifiable_class_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700763 }
764
765 static jvmtiError GetClassLoader(jvmtiEnv* env, jclass klass, jobject* classloader_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700766 ENSURE_VALID_ENV(env);
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800767 return ClassUtil::GetClassLoader(env, klass, classloader_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700768 }
769
770 static jvmtiError GetSourceDebugExtension(jvmtiEnv* env,
Alex Light6fa7b812017-06-16 09:04:29 -0700771 jclass klass,
772 char** source_debug_extension_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700773 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800774 ENSURE_HAS_CAP(env, can_get_source_debug_extension);
Alex Light6fa7b812017-06-16 09:04:29 -0700775 return ClassUtil::GetSourceDebugExtension(env, klass, source_debug_extension_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700776 }
777
778 static jvmtiError RetransformClasses(jvmtiEnv* env, jint class_count, const jclass* classes) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700779 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800780 ENSURE_HAS_CAP(env, can_retransform_classes);
Alex Light6ac57502017-01-19 15:05:06 -0800781 std::string error_msg;
782 jvmtiError res = Transformer::RetransformClasses(ArtJvmTiEnv::AsArtJvmTiEnv(env),
Alex Light0e841182018-02-12 17:42:50 +0000783 gEventHandler,
Alex Light6ac57502017-01-19 15:05:06 -0800784 art::Runtime::Current(),
785 art::Thread::Current(),
786 class_count,
787 classes,
788 &error_msg);
789 if (res != OK) {
Alex Lightae45cbb2018-10-18 15:49:56 -0700790 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANFORM " << error_msg;
Alex Light6ac57502017-01-19 15:05:06 -0800791 }
792 return res;
Alex Light49948e92016-08-11 15:35:28 -0700793 }
794
795 static jvmtiError RedefineClasses(jvmtiEnv* env,
796 jint class_count,
797 const jvmtiClassDefinition* class_definitions) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700798 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800799 ENSURE_HAS_CAP(env, can_redefine_classes);
Alex Light0e692732017-01-10 15:00:05 -0800800 std::string error_msg;
801 jvmtiError res = Redefiner::RedefineClasses(ArtJvmTiEnv::AsArtJvmTiEnv(env),
Alex Light0e841182018-02-12 17:42:50 +0000802 gEventHandler,
Alex Light0e692732017-01-10 15:00:05 -0800803 art::Runtime::Current(),
804 art::Thread::Current(),
805 class_count,
806 class_definitions,
807 &error_msg);
808 if (res != OK) {
Alex Lightae45cbb2018-10-18 15:49:56 -0700809 JVMTI_LOG(WARNING, env) << "FAILURE TO REDEFINE " << error_msg;
Alex Light0e692732017-01-10 15:00:05 -0800810 }
811 return res;
Alex Light49948e92016-08-11 15:35:28 -0700812 }
813
814 static jvmtiError GetObjectSize(jvmtiEnv* env, jobject object, jlong* size_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700815 ENSURE_VALID_ENV(env);
Andreas Gampe50a4e492017-01-06 18:00:20 -0800816 return ObjectUtil::GetObjectSize(env, object, size_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700817 }
818
819 static jvmtiError GetObjectHashCode(jvmtiEnv* env, jobject object, jint* hash_code_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700820 ENSURE_VALID_ENV(env);
Andreas Gampe50a4e492017-01-06 18:00:20 -0800821 return ObjectUtil::GetObjectHashCode(env, object, hash_code_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700822 }
823
824 static jvmtiError GetObjectMonitorUsage(jvmtiEnv* env,
Alex Lightce568642017-09-05 16:54:25 -0700825 jobject object,
826 jvmtiMonitorUsage* info_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700827 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800828 ENSURE_HAS_CAP(env, can_get_monitor_info);
Alex Lightce568642017-09-05 16:54:25 -0700829 return ObjectUtil::GetObjectMonitorUsage(env, object, info_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700830 }
831
832 static jvmtiError GetFieldName(jvmtiEnv* env,
833 jclass klass,
834 jfieldID field,
835 char** name_ptr,
836 char** signature_ptr,
837 char** generic_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700838 ENSURE_VALID_ENV(env);
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800839 return FieldUtil::GetFieldName(env, klass, field, name_ptr, signature_ptr, generic_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700840 }
841
842 static jvmtiError GetFieldDeclaringClass(jvmtiEnv* env,
843 jclass klass,
844 jfieldID field,
845 jclass* declaring_class_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700846 ENSURE_VALID_ENV(env);
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800847 return FieldUtil::GetFieldDeclaringClass(env, klass, field, declaring_class_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700848 }
849
850 static jvmtiError GetFieldModifiers(jvmtiEnv* env,
851 jclass klass,
852 jfieldID field,
853 jint* modifiers_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700854 ENSURE_VALID_ENV(env);
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800855 return FieldUtil::GetFieldModifiers(env, klass, field, modifiers_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700856 }
857
858 static jvmtiError IsFieldSynthetic(jvmtiEnv* env,
859 jclass klass,
860 jfieldID field,
861 jboolean* is_synthetic_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700862 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800863 ENSURE_HAS_CAP(env, can_get_synthetic_attribute);
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800864 return FieldUtil::IsFieldSynthetic(env, klass, field, is_synthetic_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700865 }
866
867 static jvmtiError GetMethodName(jvmtiEnv* env,
868 jmethodID method,
869 char** name_ptr,
870 char** signature_ptr,
871 char** generic_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700872 ENSURE_VALID_ENV(env);
Andreas Gampe3c252f02016-10-27 18:25:17 -0700873 return MethodUtil::GetMethodName(env, method, name_ptr, signature_ptr, generic_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700874 }
875
876 static jvmtiError GetMethodDeclaringClass(jvmtiEnv* env,
877 jmethodID method,
878 jclass* declaring_class_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700879 ENSURE_VALID_ENV(env);
Andreas Gampe368a2082016-10-28 17:33:13 -0700880 return MethodUtil::GetMethodDeclaringClass(env, method, declaring_class_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700881 }
882
883 static jvmtiError GetMethodModifiers(jvmtiEnv* env,
884 jmethodID method,
885 jint* modifiers_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700886 ENSURE_VALID_ENV(env);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700887 return MethodUtil::GetMethodModifiers(env, method, modifiers_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700888 }
889
890 static jvmtiError GetMaxLocals(jvmtiEnv* env,
891 jmethodID method,
892 jint* max_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700893 ENSURE_VALID_ENV(env);
Andreas Gampef71832e2017-01-09 11:38:04 -0800894 return MethodUtil::GetMaxLocals(env, method, max_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700895 }
896
897 static jvmtiError GetArgumentsSize(jvmtiEnv* env,
898 jmethodID method,
899 jint* size_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700900 ENSURE_VALID_ENV(env);
Andreas Gampef71832e2017-01-09 11:38:04 -0800901 return MethodUtil::GetArgumentsSize(env, method, size_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700902 }
903
904 static jvmtiError GetLineNumberTable(jvmtiEnv* env,
905 jmethodID method,
906 jint* entry_count_ptr,
907 jvmtiLineNumberEntry** table_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700908 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800909 ENSURE_HAS_CAP(env, can_get_line_numbers);
Andreas Gampeda3e5612016-12-13 19:00:53 -0800910 return MethodUtil::GetLineNumberTable(env, method, entry_count_ptr, table_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700911 }
912
913 static jvmtiError GetMethodLocation(jvmtiEnv* env,
914 jmethodID method,
915 jlocation* start_location_ptr,
916 jlocation* end_location_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700917 ENSURE_VALID_ENV(env);
Andreas Gampef71832e2017-01-09 11:38:04 -0800918 return MethodUtil::GetMethodLocation(env, method, start_location_ptr, end_location_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700919 }
920
921 static jvmtiError GetLocalVariableTable(jvmtiEnv* env,
Alex Lightce68cc62017-07-26 10:30:38 -0700922 jmethodID method,
923 jint* entry_count_ptr,
924 jvmtiLocalVariableEntry** table_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700925 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800926 ENSURE_HAS_CAP(env, can_access_local_variables);
Alex Lightce68cc62017-07-26 10:30:38 -0700927 return MethodUtil::GetLocalVariableTable(env, method, entry_count_ptr, table_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700928 }
929
930 static jvmtiError GetBytecodes(jvmtiEnv* env,
Alex Light4c174282017-07-05 10:18:18 -0700931 jmethodID method,
932 jint* bytecode_count_ptr,
933 unsigned char** bytecodes_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700934 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800935 ENSURE_HAS_CAP(env, can_get_bytecodes);
Alex Light4c174282017-07-05 10:18:18 -0700936 return MethodUtil::GetBytecodes(env, method, bytecode_count_ptr, bytecodes_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700937 }
938
939 static jvmtiError IsMethodNative(jvmtiEnv* env, jmethodID method, jboolean* is_native_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700940 ENSURE_VALID_ENV(env);
Andreas Gampefdeef522017-01-09 14:40:25 -0800941 return MethodUtil::IsMethodNative(env, method, is_native_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700942 }
943
944 static jvmtiError IsMethodSynthetic(jvmtiEnv* env, jmethodID method, jboolean* is_synthetic_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700945 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800946 ENSURE_HAS_CAP(env, can_get_synthetic_attribute);
Andreas Gampefdeef522017-01-09 14:40:25 -0800947 return MethodUtil::IsMethodSynthetic(env, method, is_synthetic_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700948 }
949
950 static jvmtiError IsMethodObsolete(jvmtiEnv* env, jmethodID method, jboolean* is_obsolete_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700951 ENSURE_VALID_ENV(env);
Andreas Gampefdeef522017-01-09 14:40:25 -0800952 return MethodUtil::IsMethodObsolete(env, method, is_obsolete_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700953 }
954
Alex Light6a3fd512017-02-27 14:34:32 -0800955 static jvmtiError SetNativeMethodPrefix(jvmtiEnv* env, const char* prefix ATTRIBUTE_UNUSED) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700956 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800957 ENSURE_HAS_CAP(env, can_set_native_method_prefix);
Alex Light49948e92016-08-11 15:35:28 -0700958 return ERR(NOT_IMPLEMENTED);
959 }
960
Alex Light6a3fd512017-02-27 14:34:32 -0800961 static jvmtiError SetNativeMethodPrefixes(jvmtiEnv* env,
962 jint prefix_count ATTRIBUTE_UNUSED,
963 char** prefixes ATTRIBUTE_UNUSED) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700964 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -0800965 ENSURE_HAS_CAP(env, can_set_native_method_prefix);
Alex Light49948e92016-08-11 15:35:28 -0700966 return ERR(NOT_IMPLEMENTED);
967 }
968
969 static jvmtiError CreateRawMonitor(jvmtiEnv* env, const char* name, jrawMonitorID* monitor_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700970 ENSURE_VALID_ENV(env);
Andreas Gampe319dbe82017-01-09 16:42:21 -0800971 return MonitorUtil::CreateRawMonitor(env, name, monitor_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700972 }
973
974 static jvmtiError DestroyRawMonitor(jvmtiEnv* env, jrawMonitorID monitor) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700975 ENSURE_VALID_ENV(env);
Andreas Gampe319dbe82017-01-09 16:42:21 -0800976 return MonitorUtil::DestroyRawMonitor(env, monitor);
Alex Light49948e92016-08-11 15:35:28 -0700977 }
978
979 static jvmtiError RawMonitorEnter(jvmtiEnv* env, jrawMonitorID monitor) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700980 ENSURE_VALID_ENV(env);
Andreas Gampe319dbe82017-01-09 16:42:21 -0800981 return MonitorUtil::RawMonitorEnter(env, monitor);
Alex Light49948e92016-08-11 15:35:28 -0700982 }
983
984 static jvmtiError RawMonitorExit(jvmtiEnv* env, jrawMonitorID monitor) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700985 ENSURE_VALID_ENV(env);
Andreas Gampe319dbe82017-01-09 16:42:21 -0800986 return MonitorUtil::RawMonitorExit(env, monitor);
Alex Light49948e92016-08-11 15:35:28 -0700987 }
988
989 static jvmtiError RawMonitorWait(jvmtiEnv* env, jrawMonitorID monitor, jlong millis) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700990 ENSURE_VALID_ENV(env);
Andreas Gampe319dbe82017-01-09 16:42:21 -0800991 return MonitorUtil::RawMonitorWait(env, monitor, millis);
Alex Light49948e92016-08-11 15:35:28 -0700992 }
993
994 static jvmtiError RawMonitorNotify(jvmtiEnv* env, jrawMonitorID monitor) {
Alex Light1edc8cf2017-03-24 14:22:56 -0700995 ENSURE_VALID_ENV(env);
Andreas Gampe319dbe82017-01-09 16:42:21 -0800996 return MonitorUtil::RawMonitorNotify(env, monitor);
Alex Light49948e92016-08-11 15:35:28 -0700997 }
998
999 static jvmtiError RawMonitorNotifyAll(jvmtiEnv* env, jrawMonitorID monitor) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001000 ENSURE_VALID_ENV(env);
Andreas Gampe319dbe82017-01-09 16:42:21 -08001001 return MonitorUtil::RawMonitorNotifyAll(env, monitor);
Alex Light49948e92016-08-11 15:35:28 -07001002 }
1003
1004 static jvmtiError SetJNIFunctionTable(jvmtiEnv* env, const jniNativeInterface* function_table) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001005 ENSURE_VALID_ENV(env);
Andreas Gampe6f8e4f02017-01-16 18:18:14 -08001006 return JNIUtil::SetJNIFunctionTable(env, function_table);
Alex Light49948e92016-08-11 15:35:28 -07001007 }
1008
1009 static jvmtiError GetJNIFunctionTable(jvmtiEnv* env, jniNativeInterface** function_table) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001010 ENSURE_VALID_ENV(env);
Andreas Gampe6f8e4f02017-01-16 18:18:14 -08001011 return JNIUtil::GetJNIFunctionTable(env, function_table);
Alex Light49948e92016-08-11 15:35:28 -07001012 }
1013
Andreas Gampe77708d92016-10-07 11:48:21 -07001014 // TODO: This will require locking, so that an agent can't remove callbacks when we're dispatching
1015 // an event.
Alex Light49948e92016-08-11 15:35:28 -07001016 static jvmtiError SetEventCallbacks(jvmtiEnv* env,
1017 const jvmtiEventCallbacks* callbacks,
1018 jint size_of_callbacks) {
Alex Lighte6574242016-08-17 09:56:24 -07001019 ENSURE_VALID_ENV(env);
Andreas Gampe77708d92016-10-07 11:48:21 -07001020 if (size_of_callbacks < 0) {
1021 return ERR(ILLEGAL_ARGUMENT);
1022 }
1023
1024 if (callbacks == nullptr) {
1025 ArtJvmTiEnv::AsArtJvmTiEnv(env)->event_callbacks.reset();
1026 return ERR(NONE);
1027 }
1028
Alex Light8c2b9292017-11-09 13:21:01 -08001029 // Lock the event_info_mutex_ while we replace the callbacks.
1030 ArtJvmTiEnv* art_env = ArtJvmTiEnv::AsArtJvmTiEnv(env);
1031 art::WriterMutexLock lk(art::Thread::Current(), art_env->event_info_mutex_);
1032 std::unique_ptr<ArtJvmtiEventCallbacks> tmp(new ArtJvmtiEventCallbacks());
1033 // Copy over the extension events.
1034 tmp->CopyExtensionsFrom(art_env->event_callbacks.get());
1035 // Never overwrite the extension events.
Andreas Gampe77708d92016-10-07 11:48:21 -07001036 size_t copy_size = std::min(sizeof(jvmtiEventCallbacks),
1037 static_cast<size_t>(size_of_callbacks));
1038 copy_size = art::RoundDown(copy_size, sizeof(void*));
Alex Light8c2b9292017-11-09 13:21:01 -08001039 // Copy non-extension events.
Andreas Gampe77708d92016-10-07 11:48:21 -07001040 memcpy(tmp.get(), callbacks, copy_size);
1041
Alex Light8c2b9292017-11-09 13:21:01 -08001042 // replace the event table.
1043 art_env->event_callbacks = std::move(tmp);
Andreas Gampe77708d92016-10-07 11:48:21 -07001044
1045 return ERR(NONE);
Alex Light49948e92016-08-11 15:35:28 -07001046 }
1047
1048 static jvmtiError SetEventNotificationMode(jvmtiEnv* env,
1049 jvmtiEventMode mode,
1050 jvmtiEvent event_type,
1051 jthread event_thread,
1052 ...) {
Alex Lighte6574242016-08-17 09:56:24 -07001053 ENSURE_VALID_ENV(env);
Alex Light40d87f42017-01-18 10:27:06 -08001054 ArtJvmTiEnv* art_env = ArtJvmTiEnv::AsArtJvmTiEnv(env);
Alex Light0e841182018-02-12 17:42:50 +00001055 return gEventHandler->SetEvent(art_env,
Alex Light3dacdd62019-03-12 15:45:47 +00001056 event_thread,
Alex Light0e841182018-02-12 17:42:50 +00001057 GetArtJvmtiEvent(art_env, event_type),
1058 mode);
Alex Light49948e92016-08-11 15:35:28 -07001059 }
1060
Alex Light1edc8cf2017-03-24 14:22:56 -07001061 static jvmtiError GenerateEvents(jvmtiEnv* env,
Alex Light6a3fd512017-02-27 14:34:32 -08001062 jvmtiEvent event_type ATTRIBUTE_UNUSED) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001063 ENSURE_VALID_ENV(env);
Alex Light6a3fd512017-02-27 14:34:32 -08001064 return OK;
Alex Light49948e92016-08-11 15:35:28 -07001065 }
1066
Alex Light1edc8cf2017-03-24 14:22:56 -07001067 static jvmtiError GetExtensionFunctions(jvmtiEnv* env,
Alex Light49948e92016-08-11 15:35:28 -07001068 jint* extension_count_ptr,
1069 jvmtiExtensionFunctionInfo** extensions) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001070 ENSURE_VALID_ENV(env);
Andreas Gamped73aba42017-05-03 21:40:26 -07001071 ENSURE_NON_NULL(extension_count_ptr);
1072 ENSURE_NON_NULL(extensions);
Alex Light3f33c0a2017-11-08 10:17:37 -08001073 return ExtensionUtil::GetExtensionFunctions(env, extension_count_ptr, extensions);
Alex Light49948e92016-08-11 15:35:28 -07001074 }
1075
Alex Light1edc8cf2017-03-24 14:22:56 -07001076 static jvmtiError GetExtensionEvents(jvmtiEnv* env,
Alex Light49948e92016-08-11 15:35:28 -07001077 jint* extension_count_ptr,
1078 jvmtiExtensionEventInfo** extensions) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001079 ENSURE_VALID_ENV(env);
Alex Light3f33c0a2017-11-08 10:17:37 -08001080 ENSURE_NON_NULL(extension_count_ptr);
1081 ENSURE_NON_NULL(extensions);
1082 return ExtensionUtil::GetExtensionEvents(env, extension_count_ptr, extensions);
Alex Light49948e92016-08-11 15:35:28 -07001083 }
1084
Alex Light1edc8cf2017-03-24 14:22:56 -07001085 static jvmtiError SetExtensionEventCallback(jvmtiEnv* env,
Alex Light3f33c0a2017-11-08 10:17:37 -08001086 jint extension_event_index,
1087 jvmtiExtensionEvent callback) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001088 ENSURE_VALID_ENV(env);
Alex Light8c2b9292017-11-09 13:21:01 -08001089 return ExtensionUtil::SetExtensionEventCallback(env,
1090 extension_event_index,
1091 callback,
Alex Light0e841182018-02-12 17:42:50 +00001092 gEventHandler);
Alex Light49948e92016-08-11 15:35:28 -07001093 }
1094
Alex Light2ce6fc82017-12-18 16:42:36 -08001095#define FOR_ALL_CAPABILITIES(FUN) \
1096 FUN(can_tag_objects) \
1097 FUN(can_generate_field_modification_events) \
1098 FUN(can_generate_field_access_events) \
1099 FUN(can_get_bytecodes) \
1100 FUN(can_get_synthetic_attribute) \
1101 FUN(can_get_owned_monitor_info) \
1102 FUN(can_get_current_contended_monitor) \
1103 FUN(can_get_monitor_info) \
1104 FUN(can_pop_frame) \
1105 FUN(can_redefine_classes) \
1106 FUN(can_signal_thread) \
1107 FUN(can_get_source_file_name) \
1108 FUN(can_get_line_numbers) \
1109 FUN(can_get_source_debug_extension) \
1110 FUN(can_access_local_variables) \
1111 FUN(can_maintain_original_method_order) \
1112 FUN(can_generate_single_step_events) \
1113 FUN(can_generate_exception_events) \
1114 FUN(can_generate_frame_pop_events) \
1115 FUN(can_generate_breakpoint_events) \
1116 FUN(can_suspend) \
1117 FUN(can_redefine_any_class) \
1118 FUN(can_get_current_thread_cpu_time) \
1119 FUN(can_get_thread_cpu_time) \
1120 FUN(can_generate_method_entry_events) \
1121 FUN(can_generate_method_exit_events) \
1122 FUN(can_generate_all_class_hook_events) \
1123 FUN(can_generate_compiled_method_load_events) \
1124 FUN(can_generate_monitor_events) \
1125 FUN(can_generate_vm_object_alloc_events) \
1126 FUN(can_generate_native_method_bind_events) \
1127 FUN(can_generate_garbage_collection_events) \
1128 FUN(can_generate_object_free_events) \
1129 FUN(can_force_early_return) \
1130 FUN(can_get_owned_monitor_stack_depth_info) \
1131 FUN(can_get_constant_pool) \
1132 FUN(can_set_native_method_prefix) \
1133 FUN(can_retransform_classes) \
1134 FUN(can_retransform_any_class) \
1135 FUN(can_generate_resource_exhaustion_heap_events) \
1136 FUN(can_generate_resource_exhaustion_threads_events)
1137
Alex Light49948e92016-08-11 15:35:28 -07001138 static jvmtiError GetPotentialCapabilities(jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001139 ENSURE_VALID_ENV(env);
1140 ENSURE_NON_NULL(capabilities_ptr);
1141 *capabilities_ptr = kPotentialCapabilities;
Alex Light2ce6fc82017-12-18 16:42:36 -08001142 if (UNLIKELY(!IsFullJvmtiAvailable())) {
1143#define REMOVE_NONDEBUGGABLE_UNSUPPORTED(e) \
1144 do { \
1145 if (kNonDebuggableUnsupportedCapabilities.e == 1) { \
1146 capabilities_ptr->e = 0; \
1147 } \
1148 } while (false);
1149
1150 FOR_ALL_CAPABILITIES(REMOVE_NONDEBUGGABLE_UNSUPPORTED);
1151#undef REMOVE_NONDEBUGGABLE_UNSUPPORTED
1152 }
Alex Lighte6574242016-08-17 09:56:24 -07001153 return OK;
Alex Light49948e92016-08-11 15:35:28 -07001154 }
1155
1156 static jvmtiError AddCapabilities(jvmtiEnv* env, const jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001157 ENSURE_VALID_ENV(env);
1158 ENSURE_NON_NULL(capabilities_ptr);
1159 ArtJvmTiEnv* art_env = static_cast<ArtJvmTiEnv*>(env);
1160 jvmtiError ret = OK;
Alex Light34d8e082017-03-27 09:50:36 -07001161 jvmtiCapabilities changed = {};
1162 jvmtiCapabilities potential_capabilities = {};
Alex Light1d224962017-02-27 10:26:35 -08001163 ret = env->GetPotentialCapabilities(&potential_capabilities);
1164 if (ret != OK) {
1165 return ret;
1166 }
Alex Lighte6574242016-08-17 09:56:24 -07001167#define ADD_CAPABILITY(e) \
1168 do { \
1169 if (capabilities_ptr->e == 1) { \
Alex Light1d224962017-02-27 10:26:35 -08001170 if (potential_capabilities.e == 1) { \
Alex Light73afd322017-01-18 11:17:47 -08001171 if (art_env->capabilities.e != 1) { \
1172 art_env->capabilities.e = 1; \
1173 changed.e = 1; \
1174 }\
Alex Lighte6574242016-08-17 09:56:24 -07001175 } else { \
1176 ret = ERR(NOT_AVAILABLE); \
1177 } \
1178 } \
Alex Light2ce6fc82017-12-18 16:42:36 -08001179 } while (false);
Alex Lighte6574242016-08-17 09:56:24 -07001180
Alex Light2ce6fc82017-12-18 16:42:36 -08001181 FOR_ALL_CAPABILITIES(ADD_CAPABILITY);
Alex Lighte6574242016-08-17 09:56:24 -07001182#undef ADD_CAPABILITY
Alex Light0e841182018-02-12 17:42:50 +00001183 gEventHandler->HandleChangedCapabilities(ArtJvmTiEnv::AsArtJvmTiEnv(env),
1184 changed,
Andreas Gampe6e897762018-10-16 13:09:32 -07001185 /*added=*/true);
Alex Lighte6574242016-08-17 09:56:24 -07001186 return ret;
Alex Light49948e92016-08-11 15:35:28 -07001187 }
1188
1189 static jvmtiError RelinquishCapabilities(jvmtiEnv* env,
1190 const jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001191 ENSURE_VALID_ENV(env);
1192 ENSURE_NON_NULL(capabilities_ptr);
1193 ArtJvmTiEnv* art_env = reinterpret_cast<ArtJvmTiEnv*>(env);
Alex Light34d8e082017-03-27 09:50:36 -07001194 jvmtiCapabilities changed = {};
Alex Lighte6574242016-08-17 09:56:24 -07001195#define DEL_CAPABILITY(e) \
1196 do { \
1197 if (capabilities_ptr->e == 1) { \
Alex Light73afd322017-01-18 11:17:47 -08001198 if (art_env->capabilities.e == 1) { \
1199 art_env->capabilities.e = 0;\
1200 changed.e = 1; \
1201 } \
Alex Lighte6574242016-08-17 09:56:24 -07001202 } \
Alex Light2ce6fc82017-12-18 16:42:36 -08001203 } while (false);
Alex Lighte6574242016-08-17 09:56:24 -07001204
Alex Light2ce6fc82017-12-18 16:42:36 -08001205 FOR_ALL_CAPABILITIES(DEL_CAPABILITY);
Alex Lighte6574242016-08-17 09:56:24 -07001206#undef DEL_CAPABILITY
Alex Light0e841182018-02-12 17:42:50 +00001207 gEventHandler->HandleChangedCapabilities(ArtJvmTiEnv::AsArtJvmTiEnv(env),
1208 changed,
Andreas Gampe6e897762018-10-16 13:09:32 -07001209 /*added=*/false);
Alex Lighte6574242016-08-17 09:56:24 -07001210 return OK;
Alex Light49948e92016-08-11 15:35:28 -07001211 }
1212
Alex Light2ce6fc82017-12-18 16:42:36 -08001213#undef FOR_ALL_CAPABILITIES
1214
Alex Light49948e92016-08-11 15:35:28 -07001215 static jvmtiError GetCapabilities(jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001216 ENSURE_VALID_ENV(env);
1217 ENSURE_NON_NULL(capabilities_ptr);
1218 ArtJvmTiEnv* artenv = reinterpret_cast<ArtJvmTiEnv*>(env);
1219 *capabilities_ptr = artenv->capabilities;
1220 return OK;
Alex Light49948e92016-08-11 15:35:28 -07001221 }
1222
Alex Light6a3fd512017-02-27 14:34:32 -08001223 static jvmtiError GetCurrentThreadCpuTimerInfo(jvmtiEnv* env,
1224 jvmtiTimerInfo* info_ptr ATTRIBUTE_UNUSED) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001225 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -08001226 ENSURE_HAS_CAP(env, can_get_current_thread_cpu_time);
Alex Light49948e92016-08-11 15:35:28 -07001227 return ERR(NOT_IMPLEMENTED);
1228 }
1229
Alex Light6a3fd512017-02-27 14:34:32 -08001230 static jvmtiError GetCurrentThreadCpuTime(jvmtiEnv* env, jlong* nanos_ptr ATTRIBUTE_UNUSED) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001231 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -08001232 ENSURE_HAS_CAP(env, can_get_current_thread_cpu_time);
Alex Light49948e92016-08-11 15:35:28 -07001233 return ERR(NOT_IMPLEMENTED);
1234 }
1235
Alex Light6a3fd512017-02-27 14:34:32 -08001236 static jvmtiError GetThreadCpuTimerInfo(jvmtiEnv* env,
1237 jvmtiTimerInfo* info_ptr ATTRIBUTE_UNUSED) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001238 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -08001239 ENSURE_HAS_CAP(env, can_get_thread_cpu_time);
Alex Light49948e92016-08-11 15:35:28 -07001240 return ERR(NOT_IMPLEMENTED);
1241 }
1242
Alex Light6a3fd512017-02-27 14:34:32 -08001243 static jvmtiError GetThreadCpuTime(jvmtiEnv* env,
1244 jthread thread ATTRIBUTE_UNUSED,
1245 jlong* nanos_ptr ATTRIBUTE_UNUSED) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001246 ENSURE_VALID_ENV(env);
Alex Light9db679d2017-01-25 15:28:04 -08001247 ENSURE_HAS_CAP(env, can_get_thread_cpu_time);
Alex Light49948e92016-08-11 15:35:28 -07001248 return ERR(NOT_IMPLEMENTED);
1249 }
1250
1251 static jvmtiError GetTimerInfo(jvmtiEnv* env, jvmtiTimerInfo* info_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001252 ENSURE_VALID_ENV(env);
Andreas Gampe35bcf812017-01-13 16:24:17 -08001253 return TimerUtil::GetTimerInfo(env, info_ptr);
Alex Light49948e92016-08-11 15:35:28 -07001254 }
1255
1256 static jvmtiError GetTime(jvmtiEnv* env, jlong* nanos_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001257 ENSURE_VALID_ENV(env);
Andreas Gampe35bcf812017-01-13 16:24:17 -08001258 return TimerUtil::GetTime(env, nanos_ptr);
Alex Light49948e92016-08-11 15:35:28 -07001259 }
1260
1261 static jvmtiError GetAvailableProcessors(jvmtiEnv* env, jint* processor_count_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001262 ENSURE_VALID_ENV(env);
Andreas Gampe35bcf812017-01-13 16:24:17 -08001263 return TimerUtil::GetAvailableProcessors(env, processor_count_ptr);
Alex Light49948e92016-08-11 15:35:28 -07001264 }
1265
1266 static jvmtiError AddToBootstrapClassLoaderSearch(jvmtiEnv* env, const char* segment) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001267 ENSURE_VALID_ENV(env);
Andreas Gampece7732b2017-01-17 15:50:26 -08001268 return SearchUtil::AddToBootstrapClassLoaderSearch(env, segment);
Alex Light49948e92016-08-11 15:35:28 -07001269 }
1270
1271 static jvmtiError AddToSystemClassLoaderSearch(jvmtiEnv* env, const char* segment) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001272 ENSURE_VALID_ENV(env);
Andreas Gampece7732b2017-01-17 15:50:26 -08001273 return SearchUtil::AddToSystemClassLoaderSearch(env, segment);
Alex Light49948e92016-08-11 15:35:28 -07001274 }
1275
1276 static jvmtiError GetSystemProperties(jvmtiEnv* env, jint* count_ptr, char*** property_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001277 ENSURE_VALID_ENV(env);
Andreas Gampe1bdaf732017-01-09 19:21:06 -08001278 return PropertiesUtil::GetSystemProperties(env, count_ptr, property_ptr);
Alex Light49948e92016-08-11 15:35:28 -07001279 }
1280
1281 static jvmtiError GetSystemProperty(jvmtiEnv* env, const char* property, char** value_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001282 ENSURE_VALID_ENV(env);
Andreas Gampe1bdaf732017-01-09 19:21:06 -08001283 return PropertiesUtil::GetSystemProperty(env, property, value_ptr);
Alex Light49948e92016-08-11 15:35:28 -07001284 }
1285
1286 static jvmtiError SetSystemProperty(jvmtiEnv* env, const char* property, const char* value) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001287 ENSURE_VALID_ENV(env);
Andreas Gampe1bdaf732017-01-09 19:21:06 -08001288 return PropertiesUtil::SetSystemProperty(env, property, value);
Alex Light49948e92016-08-11 15:35:28 -07001289 }
1290
1291 static jvmtiError GetPhase(jvmtiEnv* env, jvmtiPhase* phase_ptr) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001292 ENSURE_VALID_ENV(env);
Andreas Gampe96eca782017-01-19 19:45:30 -08001293 return PhaseUtil::GetPhase(env, phase_ptr);
Alex Light49948e92016-08-11 15:35:28 -07001294 }
1295
1296 static jvmtiError DisposeEnvironment(jvmtiEnv* env) {
Alex Lighte6574242016-08-17 09:56:24 -07001297 ENSURE_VALID_ENV(env);
Alex Light092a4042017-07-12 08:46:44 -07001298 ArtJvmTiEnv* tienv = ArtJvmTiEnv::AsArtJvmTiEnv(env);
Alex Light0e841182018-02-12 17:42:50 +00001299 gEventHandler->RemoveArtJvmTiEnv(tienv);
Alex Light092a4042017-07-12 08:46:44 -07001300 art::Runtime::Current()->RemoveSystemWeakHolder(tienv->object_tag_table.get());
1301 ThreadUtil::RemoveEnvironment(tienv);
1302 delete tienv;
Alex Light49948e92016-08-11 15:35:28 -07001303 return OK;
1304 }
1305
1306 static jvmtiError SetEnvironmentLocalStorage(jvmtiEnv* env, const void* data) {
Alex Lighte6574242016-08-17 09:56:24 -07001307 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001308 reinterpret_cast<ArtJvmTiEnv*>(env)->local_data = const_cast<void*>(data);
1309 return OK;
1310 }
1311
1312 static jvmtiError GetEnvironmentLocalStorage(jvmtiEnv* env, void** data_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001313 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001314 *data_ptr = reinterpret_cast<ArtJvmTiEnv*>(env)->local_data;
1315 return OK;
1316 }
1317
1318 static jvmtiError GetVersionNumber(jvmtiEnv* env, jint* version_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001319 ENSURE_VALID_ENV(env);
Alex Light2ce6fc82017-12-18 16:42:36 -08001320 *version_ptr = ArtJvmTiEnv::AsArtJvmTiEnv(env)->ti_version;
Alex Light49948e92016-08-11 15:35:28 -07001321 return OK;
1322 }
1323
1324 static jvmtiError GetErrorName(jvmtiEnv* env, jvmtiError error, char** name_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001325 ENSURE_NON_NULL(name_ptr);
Andreas Gampe95c466d2017-05-08 14:50:47 -07001326 auto copy_fn = [&](const char* name_cstr) {
1327 jvmtiError res;
1328 JvmtiUniquePtr<char[]> copy = CopyString(env, name_cstr, &res);
1329 if (copy == nullptr) {
1330 *name_ptr = nullptr;
1331 return res;
1332 } else {
1333 *name_ptr = copy.release();
1334 return OK;
1335 }
1336 };
Alex Light49948e92016-08-11 15:35:28 -07001337 switch (error) {
Andreas Gampe95c466d2017-05-08 14:50:47 -07001338#define ERROR_CASE(e) case (JVMTI_ERROR_ ## e) : \
1339 return copy_fn("JVMTI_ERROR_"#e);
Alex Light49948e92016-08-11 15:35:28 -07001340 ERROR_CASE(NONE);
1341 ERROR_CASE(INVALID_THREAD);
1342 ERROR_CASE(INVALID_THREAD_GROUP);
1343 ERROR_CASE(INVALID_PRIORITY);
1344 ERROR_CASE(THREAD_NOT_SUSPENDED);
Andreas Gampe95c466d2017-05-08 14:50:47 -07001345 ERROR_CASE(THREAD_SUSPENDED);
Alex Light49948e92016-08-11 15:35:28 -07001346 ERROR_CASE(THREAD_NOT_ALIVE);
1347 ERROR_CASE(INVALID_OBJECT);
1348 ERROR_CASE(INVALID_CLASS);
1349 ERROR_CASE(CLASS_NOT_PREPARED);
1350 ERROR_CASE(INVALID_METHODID);
1351 ERROR_CASE(INVALID_LOCATION);
1352 ERROR_CASE(INVALID_FIELDID);
1353 ERROR_CASE(NO_MORE_FRAMES);
1354 ERROR_CASE(OPAQUE_FRAME);
1355 ERROR_CASE(TYPE_MISMATCH);
1356 ERROR_CASE(INVALID_SLOT);
1357 ERROR_CASE(DUPLICATE);
1358 ERROR_CASE(NOT_FOUND);
1359 ERROR_CASE(INVALID_MONITOR);
1360 ERROR_CASE(NOT_MONITOR_OWNER);
1361 ERROR_CASE(INTERRUPT);
1362 ERROR_CASE(INVALID_CLASS_FORMAT);
1363 ERROR_CASE(CIRCULAR_CLASS_DEFINITION);
1364 ERROR_CASE(FAILS_VERIFICATION);
1365 ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_ADDED);
1366 ERROR_CASE(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED);
1367 ERROR_CASE(INVALID_TYPESTATE);
1368 ERROR_CASE(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED);
1369 ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_DELETED);
1370 ERROR_CASE(UNSUPPORTED_VERSION);
1371 ERROR_CASE(NAMES_DONT_MATCH);
1372 ERROR_CASE(UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED);
1373 ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED);
1374 ERROR_CASE(UNMODIFIABLE_CLASS);
1375 ERROR_CASE(NOT_AVAILABLE);
1376 ERROR_CASE(MUST_POSSESS_CAPABILITY);
1377 ERROR_CASE(NULL_POINTER);
1378 ERROR_CASE(ABSENT_INFORMATION);
1379 ERROR_CASE(INVALID_EVENT_TYPE);
1380 ERROR_CASE(ILLEGAL_ARGUMENT);
1381 ERROR_CASE(NATIVE_METHOD);
1382 ERROR_CASE(CLASS_LOADER_UNSUPPORTED);
1383 ERROR_CASE(OUT_OF_MEMORY);
1384 ERROR_CASE(ACCESS_DENIED);
1385 ERROR_CASE(WRONG_PHASE);
1386 ERROR_CASE(INTERNAL);
1387 ERROR_CASE(UNATTACHED_THREAD);
1388 ERROR_CASE(INVALID_ENVIRONMENT);
1389#undef ERROR_CASE
Alex Light49948e92016-08-11 15:35:28 -07001390 }
Andreas Gampe95c466d2017-05-08 14:50:47 -07001391
1392 return ERR(ILLEGAL_ARGUMENT);
Alex Light49948e92016-08-11 15:35:28 -07001393 }
1394
Alex Light1edc8cf2017-03-24 14:22:56 -07001395 static jvmtiError SetVerboseFlag(jvmtiEnv* env,
Alex Light6a3fd512017-02-27 14:34:32 -08001396 jvmtiVerboseFlag flag,
1397 jboolean value) {
Alex Light1edc8cf2017-03-24 14:22:56 -07001398 ENSURE_VALID_ENV(env);
Andreas Gampef37e3022017-01-13 17:54:46 -08001399 if (flag == jvmtiVerboseFlag::JVMTI_VERBOSE_OTHER) {
1400 // OTHER is special, as it's 0, so can't do a bit check.
1401 bool val = (value == JNI_TRUE) ? true : false;
1402
1403 art::gLogVerbosity.collector = val;
1404 art::gLogVerbosity.compiler = val;
1405 art::gLogVerbosity.deopt = val;
1406 art::gLogVerbosity.heap = val;
Mathieu Chartier765b2a02019-05-02 11:04:13 -07001407 art::gLogVerbosity.interpreter = val;
Andreas Gampef37e3022017-01-13 17:54:46 -08001408 art::gLogVerbosity.jdwp = val;
1409 art::gLogVerbosity.jit = val;
1410 art::gLogVerbosity.monitor = val;
1411 art::gLogVerbosity.oat = val;
1412 art::gLogVerbosity.profiler = val;
1413 art::gLogVerbosity.signals = val;
1414 art::gLogVerbosity.simulator = val;
1415 art::gLogVerbosity.startup = val;
1416 art::gLogVerbosity.third_party_jni = val;
1417 art::gLogVerbosity.threads = val;
1418 art::gLogVerbosity.verifier = val;
Andreas Gampe92d77202017-12-06 20:49:00 -08001419 // Do not set verifier-debug.
Andreas Gampef37e3022017-01-13 17:54:46 -08001420 art::gLogVerbosity.image = val;
1421
1422 // Note: can't switch systrace_lock_logging. That requires changing entrypoints.
1423
1424 art::gLogVerbosity.agents = val;
1425 } else {
1426 // Spec isn't clear whether "flag" is a mask or supposed to be single. We implement the mask
1427 // semantics.
1428 constexpr std::underlying_type<jvmtiVerboseFlag>::type kMask =
1429 jvmtiVerboseFlag::JVMTI_VERBOSE_GC |
1430 jvmtiVerboseFlag::JVMTI_VERBOSE_CLASS |
1431 jvmtiVerboseFlag::JVMTI_VERBOSE_JNI;
1432 if ((flag & ~kMask) != 0) {
1433 return ERR(ILLEGAL_ARGUMENT);
1434 }
1435
1436 bool val = (value == JNI_TRUE) ? true : false;
1437
1438 if ((flag & jvmtiVerboseFlag::JVMTI_VERBOSE_GC) != 0) {
1439 art::gLogVerbosity.gc = val;
1440 }
1441
1442 if ((flag & jvmtiVerboseFlag::JVMTI_VERBOSE_CLASS) != 0) {
1443 art::gLogVerbosity.class_linker = val;
1444 }
1445
1446 if ((flag & jvmtiVerboseFlag::JVMTI_VERBOSE_JNI) != 0) {
1447 art::gLogVerbosity.jni = val;
1448 }
1449 }
1450
1451 return ERR(NONE);
Alex Light49948e92016-08-11 15:35:28 -07001452 }
1453
Alex Light1edc8cf2017-03-24 14:22:56 -07001454 static jvmtiError GetJLocationFormat(jvmtiEnv* env, jvmtiJlocationFormat* format_ptr) {
1455 ENSURE_VALID_ENV(env);
Andreas Gampeacfc9572017-01-17 18:36:56 -08001456 // Report BCI as jlocation format. We report dex bytecode indices.
1457 if (format_ptr == nullptr) {
1458 return ERR(NULL_POINTER);
1459 }
1460 *format_ptr = jvmtiJlocationFormat::JVMTI_JLOCATION_JVMBCI;
1461 return ERR(NONE);
Alex Light49948e92016-08-11 15:35:28 -07001462 }
1463};
1464
1465static bool IsJvmtiVersion(jint version) {
1466 return version == JVMTI_VERSION_1 ||
1467 version == JVMTI_VERSION_1_0 ||
1468 version == JVMTI_VERSION_1_1 ||
1469 version == JVMTI_VERSION_1_2 ||
1470 version == JVMTI_VERSION;
1471}
1472
Andreas Gampede19eb92017-02-24 16:21:18 -08001473extern const jvmtiInterface_1 gJvmtiInterface;
Alex Light092a4042017-07-12 08:46:44 -07001474
Alex Light2ce6fc82017-12-18 16:42:36 -08001475ArtJvmTiEnv::ArtJvmTiEnv(art::JavaVMExt* runtime, EventHandler* event_handler, jint version)
Andreas Gampede19eb92017-02-24 16:21:18 -08001476 : art_vm(runtime),
1477 local_data(nullptr),
Alex Light2ce6fc82017-12-18 16:42:36 -08001478 ti_version(version),
Alex Lightb6106d52017-10-18 15:02:15 -07001479 capabilities(),
Alex Lightae45cbb2018-10-18 15:49:56 -07001480 event_info_mutex_("jvmtiEnv_EventInfoMutex"),
1481 last_error_mutex_("jvmtiEnv_LastErrorMutex", art::LockLevel::kGenericBottomLock) {
Yi Kongc57c6802018-10-29 14:28:56 -07001482 object_tag_table = std::make_unique<ObjectTagTable>(event_handler, this);
Andreas Gampede19eb92017-02-24 16:21:18 -08001483 functions = &gJvmtiInterface;
1484}
1485
Alex Light49948e92016-08-11 15:35:28 -07001486// Creates a jvmtiEnv and returns it with the art::ti::Env that is associated with it. new_art_ti
1487// is a pointer to the uninitialized memory for an art::ti::Env.
Alex Light2ce6fc82017-12-18 16:42:36 -08001488static void CreateArtJvmTiEnv(art::JavaVMExt* vm, jint version, /*out*/void** new_jvmtiEnv) {
Alex Light0e841182018-02-12 17:42:50 +00001489 struct ArtJvmTiEnv* env = new ArtJvmTiEnv(vm, gEventHandler, version);
Alex Light49948e92016-08-11 15:35:28 -07001490 *new_jvmtiEnv = env;
Andreas Gampe77708d92016-10-07 11:48:21 -07001491
Alex Light0e841182018-02-12 17:42:50 +00001492 gEventHandler->RegisterArtJvmTiEnv(env);
Andreas Gampede19eb92017-02-24 16:21:18 -08001493
1494 art::Runtime::Current()->AddSystemWeakHolder(
1495 ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
Alex Light49948e92016-08-11 15:35:28 -07001496}
1497
1498// A hook that the runtime uses to allow plugins to handle GetEnv calls. It returns true and
1499// places the return value in 'env' if this library can handle the GetEnv request. Otherwise
1500// returns false and does not modify the 'env' pointer.
1501static jint GetEnvHandler(art::JavaVMExt* vm, /*out*/void** env, jint version) {
Alex Light2ce6fc82017-12-18 16:42:36 -08001502 // JavaDebuggable will either be set by the runtime as it is starting up or the plugin if it's
1503 // loaded early enough. If this is false we cannot guarantee conformance to all JVMTI behaviors
1504 // due to optimizations. We will only allow agents to get ArtTiEnvs using the kArtTiVersion.
1505 if (IsFullJvmtiAvailable() && IsJvmtiVersion(version)) {
1506 CreateArtJvmTiEnv(vm, JVMTI_VERSION, env);
1507 return JNI_OK;
1508 } else if (version == kArtTiVersion) {
1509 CreateArtJvmTiEnv(vm, kArtTiVersion, env);
Alex Light49948e92016-08-11 15:35:28 -07001510 return JNI_OK;
1511 } else {
1512 printf("version 0x%x is not valid!", version);
1513 return JNI_EVERSION;
1514 }
1515}
1516
1517// The plugin initialization function. This adds the jvmti environment.
1518extern "C" bool ArtPlugin_Initialize() {
Andreas Gampee08a2be2016-10-06 13:13:30 -07001519 art::Runtime* runtime = art::Runtime::Current();
Andreas Gampe96eca782017-01-19 19:45:30 -08001520
Alex Light0e841182018-02-12 17:42:50 +00001521 gDeoptManager = new DeoptManager;
1522 gEventHandler = new EventHandler;
1523
1524 gDeoptManager->Setup();
Andreas Gampe96eca782017-01-19 19:45:30 -08001525 if (runtime->IsStarted()) {
1526 PhaseUtil::SetToLive();
1527 } else {
1528 PhaseUtil::SetToOnLoad();
1529 }
Alex Light0e841182018-02-12 17:42:50 +00001530 PhaseUtil::Register(gEventHandler);
1531 ThreadUtil::Register(gEventHandler);
1532 ClassUtil::Register(gEventHandler);
1533 DumpUtil::Register(gEventHandler);
1534 MethodUtil::Register(gEventHandler);
Alex Light72d7e942019-07-23 13:10:20 -07001535 HeapExtensions::Register(gEventHandler);
Andreas Gampecefaa142017-01-23 15:04:59 -08001536 SearchUtil::Register();
Andreas Gampe9e38a502017-03-06 08:19:26 -08001537 HeapUtil::Register();
Alex Lightca97ada2018-02-02 09:25:31 -08001538 Transformer::Setup();
Andreas Gampe96eca782017-01-19 19:45:30 -08001539
Alex Light2ce6fc82017-12-18 16:42:36 -08001540 {
1541 // Make sure we can deopt anything we need to.
Nicolas Geoffray226805d2018-12-14 10:59:02 +00001542 art::ScopedSuspendAll ssa(__FUNCTION__);
Alex Light0e841182018-02-12 17:42:50 +00001543 gDeoptManager->FinishSetup();
Alex Light2ce6fc82017-12-18 16:42:36 -08001544 }
1545
Andreas Gampee08a2be2016-10-06 13:13:30 -07001546 runtime->GetJavaVM()->AddEnvironmentHook(GetEnvHandler);
Andreas Gampe96eca782017-01-19 19:45:30 -08001547
Alex Light49948e92016-08-11 15:35:28 -07001548 return true;
1549}
1550
Andreas Gampeeafaf572017-01-20 12:34:15 -08001551extern "C" bool ArtPlugin_Deinitialize() {
Alex Light0e841182018-02-12 17:42:50 +00001552 gEventHandler->Shutdown();
1553 gDeoptManager->Shutdown();
Andreas Gampeeafaf572017-01-20 12:34:15 -08001554 PhaseUtil::Unregister();
1555 ThreadUtil::Unregister();
Andreas Gampee6377462017-01-20 17:37:50 -08001556 ClassUtil::Unregister();
Andreas Gampeeb0cea12017-01-23 08:50:04 -08001557 DumpUtil::Unregister();
Alex Lightd78ddec2017-04-18 15:20:38 -07001558 MethodUtil::Unregister();
Andreas Gampecefaa142017-01-23 15:04:59 -08001559 SearchUtil::Unregister();
Andreas Gampe9e38a502017-03-06 08:19:26 -08001560 HeapUtil::Unregister();
Andreas Gampeeafaf572017-01-20 12:34:15 -08001561
Alex Light0e841182018-02-12 17:42:50 +00001562 // TODO It would be good to delete the gEventHandler and gDeoptManager here but we cannot since
1563 // daemon threads might be suspended and we want to make sure that even if they wake up briefly
1564 // they won't hit deallocated memory. By this point none of the functions will do anything since
1565 // they have already shutdown.
1566
Andreas Gampeeafaf572017-01-20 12:34:15 -08001567 return true;
1568}
1569
Alex Light49948e92016-08-11 15:35:28 -07001570// The actual struct holding all of the entrypoints into the jvmti interface.
1571const jvmtiInterface_1 gJvmtiInterface = {
Alex Light6ac57502017-01-19 15:05:06 -08001572 nullptr, // reserved1
Alex Light49948e92016-08-11 15:35:28 -07001573 JvmtiFunctions::SetEventNotificationMode,
Alex Light0e692732017-01-10 15:00:05 -08001574 nullptr, // reserved3
Alex Light49948e92016-08-11 15:35:28 -07001575 JvmtiFunctions::GetAllThreads,
1576 JvmtiFunctions::SuspendThread,
1577 JvmtiFunctions::ResumeThread,
1578 JvmtiFunctions::StopThread,
1579 JvmtiFunctions::InterruptThread,
1580 JvmtiFunctions::GetThreadInfo,
1581 JvmtiFunctions::GetOwnedMonitorInfo, // 10
1582 JvmtiFunctions::GetCurrentContendedMonitor,
1583 JvmtiFunctions::RunAgentThread,
1584 JvmtiFunctions::GetTopThreadGroups,
1585 JvmtiFunctions::GetThreadGroupInfo,
1586 JvmtiFunctions::GetThreadGroupChildren,
1587 JvmtiFunctions::GetFrameCount,
1588 JvmtiFunctions::GetThreadState,
1589 JvmtiFunctions::GetCurrentThread,
1590 JvmtiFunctions::GetFrameLocation,
1591 JvmtiFunctions::NotifyFramePop, // 20
1592 JvmtiFunctions::GetLocalObject,
1593 JvmtiFunctions::GetLocalInt,
1594 JvmtiFunctions::GetLocalLong,
1595 JvmtiFunctions::GetLocalFloat,
1596 JvmtiFunctions::GetLocalDouble,
1597 JvmtiFunctions::SetLocalObject,
1598 JvmtiFunctions::SetLocalInt,
1599 JvmtiFunctions::SetLocalLong,
1600 JvmtiFunctions::SetLocalFloat,
1601 JvmtiFunctions::SetLocalDouble, // 30
1602 JvmtiFunctions::CreateRawMonitor,
1603 JvmtiFunctions::DestroyRawMonitor,
1604 JvmtiFunctions::RawMonitorEnter,
1605 JvmtiFunctions::RawMonitorExit,
1606 JvmtiFunctions::RawMonitorWait,
1607 JvmtiFunctions::RawMonitorNotify,
1608 JvmtiFunctions::RawMonitorNotifyAll,
1609 JvmtiFunctions::SetBreakpoint,
1610 JvmtiFunctions::ClearBreakpoint,
1611 nullptr, // reserved40
1612 JvmtiFunctions::SetFieldAccessWatch,
1613 JvmtiFunctions::ClearFieldAccessWatch,
1614 JvmtiFunctions::SetFieldModificationWatch,
1615 JvmtiFunctions::ClearFieldModificationWatch,
1616 JvmtiFunctions::IsModifiableClass,
1617 JvmtiFunctions::Allocate,
1618 JvmtiFunctions::Deallocate,
1619 JvmtiFunctions::GetClassSignature,
1620 JvmtiFunctions::GetClassStatus,
1621 JvmtiFunctions::GetSourceFileName, // 50
1622 JvmtiFunctions::GetClassModifiers,
1623 JvmtiFunctions::GetClassMethods,
1624 JvmtiFunctions::GetClassFields,
1625 JvmtiFunctions::GetImplementedInterfaces,
1626 JvmtiFunctions::IsInterface,
1627 JvmtiFunctions::IsArrayClass,
1628 JvmtiFunctions::GetClassLoader,
1629 JvmtiFunctions::GetObjectHashCode,
1630 JvmtiFunctions::GetObjectMonitorUsage,
1631 JvmtiFunctions::GetFieldName, // 60
1632 JvmtiFunctions::GetFieldDeclaringClass,
1633 JvmtiFunctions::GetFieldModifiers,
1634 JvmtiFunctions::IsFieldSynthetic,
1635 JvmtiFunctions::GetMethodName,
1636 JvmtiFunctions::GetMethodDeclaringClass,
1637 JvmtiFunctions::GetMethodModifiers,
1638 nullptr, // reserved67
1639 JvmtiFunctions::GetMaxLocals,
1640 JvmtiFunctions::GetArgumentsSize,
1641 JvmtiFunctions::GetLineNumberTable, // 70
1642 JvmtiFunctions::GetMethodLocation,
1643 JvmtiFunctions::GetLocalVariableTable,
1644 JvmtiFunctions::SetNativeMethodPrefix,
1645 JvmtiFunctions::SetNativeMethodPrefixes,
1646 JvmtiFunctions::GetBytecodes,
1647 JvmtiFunctions::IsMethodNative,
1648 JvmtiFunctions::IsMethodSynthetic,
1649 JvmtiFunctions::GetLoadedClasses,
1650 JvmtiFunctions::GetClassLoaderClasses,
1651 JvmtiFunctions::PopFrame, // 80
1652 JvmtiFunctions::ForceEarlyReturnObject,
1653 JvmtiFunctions::ForceEarlyReturnInt,
1654 JvmtiFunctions::ForceEarlyReturnLong,
1655 JvmtiFunctions::ForceEarlyReturnFloat,
1656 JvmtiFunctions::ForceEarlyReturnDouble,
1657 JvmtiFunctions::ForceEarlyReturnVoid,
1658 JvmtiFunctions::RedefineClasses,
1659 JvmtiFunctions::GetVersionNumber,
1660 JvmtiFunctions::GetCapabilities,
1661 JvmtiFunctions::GetSourceDebugExtension, // 90
1662 JvmtiFunctions::IsMethodObsolete,
1663 JvmtiFunctions::SuspendThreadList,
1664 JvmtiFunctions::ResumeThreadList,
1665 nullptr, // reserved94
1666 nullptr, // reserved95
1667 nullptr, // reserved96
1668 nullptr, // reserved97
1669 nullptr, // reserved98
1670 nullptr, // reserved99
1671 JvmtiFunctions::GetAllStackTraces, // 100
1672 JvmtiFunctions::GetThreadListStackTraces,
1673 JvmtiFunctions::GetThreadLocalStorage,
1674 JvmtiFunctions::SetThreadLocalStorage,
1675 JvmtiFunctions::GetStackTrace,
1676 nullptr, // reserved105
1677 JvmtiFunctions::GetTag,
1678 JvmtiFunctions::SetTag,
1679 JvmtiFunctions::ForceGarbageCollection,
1680 JvmtiFunctions::IterateOverObjectsReachableFromObject,
1681 JvmtiFunctions::IterateOverReachableObjects, // 110
1682 JvmtiFunctions::IterateOverHeap,
1683 JvmtiFunctions::IterateOverInstancesOfClass,
1684 nullptr, // reserved113
1685 JvmtiFunctions::GetObjectsWithTags,
1686 JvmtiFunctions::FollowReferences,
1687 JvmtiFunctions::IterateThroughHeap,
1688 nullptr, // reserved117
1689 nullptr, // reserved118
1690 nullptr, // reserved119
1691 JvmtiFunctions::SetJNIFunctionTable, // 120
1692 JvmtiFunctions::GetJNIFunctionTable,
1693 JvmtiFunctions::SetEventCallbacks,
1694 JvmtiFunctions::GenerateEvents,
1695 JvmtiFunctions::GetExtensionFunctions,
1696 JvmtiFunctions::GetExtensionEvents,
1697 JvmtiFunctions::SetExtensionEventCallback,
1698 JvmtiFunctions::DisposeEnvironment,
1699 JvmtiFunctions::GetErrorName,
1700 JvmtiFunctions::GetJLocationFormat,
1701 JvmtiFunctions::GetSystemProperties, // 130
1702 JvmtiFunctions::GetSystemProperty,
1703 JvmtiFunctions::SetSystemProperty,
1704 JvmtiFunctions::GetPhase,
1705 JvmtiFunctions::GetCurrentThreadCpuTimerInfo,
1706 JvmtiFunctions::GetCurrentThreadCpuTime,
1707 JvmtiFunctions::GetThreadCpuTimerInfo,
1708 JvmtiFunctions::GetThreadCpuTime,
1709 JvmtiFunctions::GetTimerInfo,
1710 JvmtiFunctions::GetTime,
1711 JvmtiFunctions::GetPotentialCapabilities, // 140
1712 nullptr, // reserved141
1713 JvmtiFunctions::AddCapabilities,
1714 JvmtiFunctions::RelinquishCapabilities,
1715 JvmtiFunctions::GetAvailableProcessors,
1716 JvmtiFunctions::GetClassVersionNumbers,
1717 JvmtiFunctions::GetConstantPool,
1718 JvmtiFunctions::GetEnvironmentLocalStorage,
1719 JvmtiFunctions::SetEnvironmentLocalStorage,
1720 JvmtiFunctions::AddToBootstrapClassLoaderSearch,
1721 JvmtiFunctions::SetVerboseFlag, // 150
1722 JvmtiFunctions::AddToSystemClassLoaderSearch,
1723 JvmtiFunctions::RetransformClasses,
1724 JvmtiFunctions::GetOwnedMonitorStackDepthInfo,
1725 JvmtiFunctions::GetObjectSize,
1726 JvmtiFunctions::GetLocalInstance,
1727};
1728
1729}; // namespace openjdkjvmti