blob: 81e21d1d81a0f4f289945faf1059f961d4c27936 [file] [log] [blame]
Alex Light49948e92016-08-11 15:35:28 -07001/* Copyright (C) 2016 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
Alex Light9c20a142016-08-23 15:05:12 -070032#include <string>
33#include <vector>
34
Alex Light49948e92016-08-11 15:35:28 -070035#include <jni.h>
Alex Light9c20a142016-08-23 15:05:12 -070036
Alex Light49948e92016-08-11 15:35:28 -070037#include "openjdkjvmti/jvmti.h"
38
Andreas Gampedb6dcb62016-09-13 09:05:59 -070039#include "art_jvmti.h"
Andreas Gampe77708d92016-10-07 11:48:21 -070040#include "base/mutex.h"
41#include "events-inl.h"
Alex Light49948e92016-08-11 15:35:28 -070042#include "jni_env_ext-inl.h"
Andreas Gampe6dee92e2016-09-12 19:58:13 -070043#include "obj_ptr-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080044#include "object_tagging.h"
Alex Light9c20a142016-08-23 15:05:12 -070045#include "runtime.h"
Andreas Gampe6dee92e2016-09-12 19:58:13 -070046#include "scoped_thread_state_change-inl.h"
Andreas Gampe77708d92016-10-07 11:48:21 -070047#include "thread-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080048#include "thread_list.h"
Andreas Gampee492ae32016-10-28 19:34:57 -070049#include "ti_class.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080050#include "ti_field.h"
Andreas Gampeba8df692016-11-01 10:30:44 -070051#include "ti_heap.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070052#include "ti_method.h"
Andreas Gampe50a4e492017-01-06 18:00:20 -080053#include "ti_object.h"
Alex Lighta01de592016-11-15 10:43:06 -080054#include "ti_redefine.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070055#include "ti_stack.h"
Alex Light9c20a142016-08-23 15:05:12 -070056#include "transform.h"
Alex Light49948e92016-08-11 15:35:28 -070057
58// TODO Remove this at some point by annotating all the methods. It was put in to make the skeleton
59// easier to create.
60#pragma GCC diagnostic ignored "-Wunused-parameter"
61
62namespace openjdkjvmti {
63
Andreas Gampe77708d92016-10-07 11:48:21 -070064EventHandler gEventHandler;
Andreas Gampecc13b222016-10-10 19:09:09 -070065ObjectTagTable gObjectTagTable(&gEventHandler);
Andreas Gampe6dee92e2016-09-12 19:58:13 -070066
Alex Lighte6574242016-08-17 09:56:24 -070067#define ENSURE_NON_NULL(n) \
68 do { \
69 if ((n) == nullptr) { \
70 return ERR(NULL_POINTER); \
71 } \
72 } while (false)
73
Alex Light49948e92016-08-11 15:35:28 -070074class JvmtiFunctions {
75 private:
76 static bool IsValidEnv(jvmtiEnv* env) {
77 return env != nullptr;
78 }
79
Alex Lighte6574242016-08-17 09:56:24 -070080#define ENSURE_VALID_ENV(env) \
81 do { \
82 if (!IsValidEnv(env)) { \
83 return ERR(INVALID_ENVIRONMENT); \
84 } \
85 } while (false)
86
87#define ENSURE_HAS_CAP(env, cap) \
88 do { \
89 ENSURE_VALID_ENV(env); \
90 if (ArtJvmTiEnv::AsArtJvmTiEnv(env)->capabilities.cap != 1) { \
91 return ERR(MUST_POSSESS_CAPABILITY); \
92 } \
93 } while (false)
94
Alex Light49948e92016-08-11 15:35:28 -070095 public:
96 static jvmtiError Allocate(jvmtiEnv* env, jlong size, unsigned char** mem_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -070097 ENSURE_VALID_ENV(env);
98 ENSURE_NON_NULL(mem_ptr);
Alex Light49948e92016-08-11 15:35:28 -070099 if (size < 0) {
100 return ERR(ILLEGAL_ARGUMENT);
101 } else if (size == 0) {
102 *mem_ptr = nullptr;
103 return OK;
104 }
105 *mem_ptr = static_cast<unsigned char*>(malloc(size));
106 return (*mem_ptr != nullptr) ? OK : ERR(OUT_OF_MEMORY);
107 }
108
109 static jvmtiError Deallocate(jvmtiEnv* env, unsigned char* mem) {
Alex Lighte6574242016-08-17 09:56:24 -0700110 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -0700111 if (mem != nullptr) {
112 free(mem);
113 }
114 return OK;
115 }
116
117 static jvmtiError GetThreadState(jvmtiEnv* env, jthread thread, jint* thread_state_ptr) {
118 return ERR(NOT_IMPLEMENTED);
119 }
120
121 static jvmtiError GetCurrentThread(jvmtiEnv* env, jthread* thread_ptr) {
122 return ERR(NOT_IMPLEMENTED);
123 }
124
125 static jvmtiError GetAllThreads(jvmtiEnv* env, jint* threads_count_ptr, jthread** threads_ptr) {
126 return ERR(NOT_IMPLEMENTED);
127 }
128
129 static jvmtiError SuspendThread(jvmtiEnv* env, jthread thread) {
130 return ERR(NOT_IMPLEMENTED);
131 }
132
133 static jvmtiError SuspendThreadList(jvmtiEnv* env,
134 jint request_count,
135 const jthread* request_list,
136 jvmtiError* results) {
137 return ERR(NOT_IMPLEMENTED);
138 }
139
140 static jvmtiError ResumeThread(jvmtiEnv* env, jthread thread) {
141 return ERR(NOT_IMPLEMENTED);
142 }
143
144 static jvmtiError ResumeThreadList(jvmtiEnv* env,
145 jint request_count,
146 const jthread* request_list,
147 jvmtiError* results) {
148 return ERR(NOT_IMPLEMENTED);
149 }
150
151 static jvmtiError StopThread(jvmtiEnv* env, jthread thread, jobject exception) {
152 return ERR(NOT_IMPLEMENTED);
153 }
154
155 static jvmtiError InterruptThread(jvmtiEnv* env, jthread thread) {
156 return ERR(NOT_IMPLEMENTED);
157 }
158
159 static jvmtiError GetThreadInfo(jvmtiEnv* env, jthread thread, jvmtiThreadInfo* info_ptr) {
160 return ERR(NOT_IMPLEMENTED);
161 }
162
163 static jvmtiError GetOwnedMonitorInfo(jvmtiEnv* env,
164 jthread thread,
165 jint* owned_monitor_count_ptr,
166 jobject** owned_monitors_ptr) {
167 return ERR(NOT_IMPLEMENTED);
168 }
169
170 static jvmtiError GetOwnedMonitorStackDepthInfo(jvmtiEnv* env,
171 jthread thread,
172 jint* monitor_info_count_ptr,
173 jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
174 return ERR(NOT_IMPLEMENTED);
175 }
176
177 static jvmtiError GetCurrentContendedMonitor(jvmtiEnv* env,
178 jthread thread,
179 jobject* monitor_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700180 return ERR(NOT_IMPLEMENTED);
Alex Light49948e92016-08-11 15:35:28 -0700181 }
182
183 static jvmtiError RunAgentThread(jvmtiEnv* env,
184 jthread thread,
185 jvmtiStartFunction proc,
186 const void* arg,
187 jint priority) {
188 return ERR(NOT_IMPLEMENTED);
189 }
190
191 static jvmtiError SetThreadLocalStorage(jvmtiEnv* env, jthread thread, const void* data) {
192 return ERR(NOT_IMPLEMENTED);
193 }
194
195 static jvmtiError GetThreadLocalStorage(jvmtiEnv* env, jthread thread, void** data_ptr) {
196 return ERR(NOT_IMPLEMENTED);
197 }
198
199 static jvmtiError GetTopThreadGroups(jvmtiEnv* env,
200 jint* group_count_ptr,
201 jthreadGroup** groups_ptr) {
202 return ERR(NOT_IMPLEMENTED);
203 }
204
205 static jvmtiError GetThreadGroupInfo(jvmtiEnv* env,
206 jthreadGroup group,
207 jvmtiThreadGroupInfo* info_ptr) {
208 return ERR(NOT_IMPLEMENTED);
209 }
210
211 static jvmtiError GetThreadGroupChildren(jvmtiEnv* env,
212 jthreadGroup group,
213 jint* thread_count_ptr,
214 jthread** threads_ptr,
215 jint* group_count_ptr,
216 jthreadGroup** groups_ptr) {
217 return ERR(NOT_IMPLEMENTED);
218 }
219
220 static jvmtiError GetStackTrace(jvmtiEnv* env,
221 jthread thread,
222 jint start_depth,
223 jint max_frame_count,
224 jvmtiFrameInfo* frame_buffer,
225 jint* count_ptr) {
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700226 return StackUtil::GetStackTrace(env,
227 thread,
228 start_depth,
229 max_frame_count,
230 frame_buffer,
231 count_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700232 }
233
234 static jvmtiError GetAllStackTraces(jvmtiEnv* env,
235 jint max_frame_count,
236 jvmtiStackInfo** stack_info_ptr,
237 jint* thread_count_ptr) {
238 return ERR(NOT_IMPLEMENTED);
239 }
240
241 static jvmtiError GetThreadListStackTraces(jvmtiEnv* env,
242 jint thread_count,
243 const jthread* thread_list,
244 jint max_frame_count,
245 jvmtiStackInfo** stack_info_ptr) {
246 return ERR(NOT_IMPLEMENTED);
247 }
248
249 static jvmtiError GetFrameCount(jvmtiEnv* env, jthread thread, jint* count_ptr) {
250 return ERR(NOT_IMPLEMENTED);
251 }
252
253 static jvmtiError PopFrame(jvmtiEnv* env, jthread thread) {
254 return ERR(NOT_IMPLEMENTED);
255 }
256
257 static jvmtiError GetFrameLocation(jvmtiEnv* env,
258 jthread thread,
259 jint depth,
260 jmethodID* method_ptr,
261 jlocation* location_ptr) {
262 return ERR(NOT_IMPLEMENTED);
263 }
264
265 static jvmtiError NotifyFramePop(jvmtiEnv* env, jthread thread, jint depth) {
266 return ERR(NOT_IMPLEMENTED);
267 }
268
269 static jvmtiError ForceEarlyReturnObject(jvmtiEnv* env, jthread thread, jobject value) {
270 return ERR(NOT_IMPLEMENTED);
271 }
272
273 static jvmtiError ForceEarlyReturnInt(jvmtiEnv* env, jthread thread, jint value) {
274 return ERR(NOT_IMPLEMENTED);
275 }
276
277 static jvmtiError ForceEarlyReturnLong(jvmtiEnv* env, jthread thread, jlong value) {
278 return ERR(NOT_IMPLEMENTED);
279 }
280
281 static jvmtiError ForceEarlyReturnFloat(jvmtiEnv* env, jthread thread, jfloat value) {
282 return ERR(NOT_IMPLEMENTED);
283 }
284
285 static jvmtiError ForceEarlyReturnDouble(jvmtiEnv* env, jthread thread, jdouble value) {
286 return ERR(NOT_IMPLEMENTED);
287 }
288
289 static jvmtiError ForceEarlyReturnVoid(jvmtiEnv* env, jthread thread) {
290 return ERR(NOT_IMPLEMENTED);
291 }
292
293 static jvmtiError FollowReferences(jvmtiEnv* env,
294 jint heap_filter,
295 jclass klass,
296 jobject initial_object,
297 const jvmtiHeapCallbacks* callbacks,
298 const void* user_data) {
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700299 HeapUtil heap_util(&gObjectTagTable);
300 return heap_util.FollowReferences(env,
301 heap_filter,
302 klass,
303 initial_object,
304 callbacks,
305 user_data);
Alex Light49948e92016-08-11 15:35:28 -0700306 }
307
308 static jvmtiError IterateThroughHeap(jvmtiEnv* env,
309 jint heap_filter,
310 jclass klass,
311 const jvmtiHeapCallbacks* callbacks,
312 const void* user_data) {
Alex Lighte6574242016-08-17 09:56:24 -0700313 ENSURE_HAS_CAP(env, can_tag_objects);
Andreas Gampee54d9922016-10-11 19:55:37 -0700314 HeapUtil heap_util(&gObjectTagTable);
315 return heap_util.IterateThroughHeap(env, heap_filter, klass, callbacks, user_data);
Alex Light49948e92016-08-11 15:35:28 -0700316 }
317
318 static jvmtiError GetTag(jvmtiEnv* env, jobject object, jlong* tag_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700319 ENSURE_HAS_CAP(env, can_tag_objects);
Andreas Gampe6dee92e2016-09-12 19:58:13 -0700320
321 JNIEnv* jni_env = GetJniEnv(env);
322 if (jni_env == nullptr) {
323 return ERR(INTERNAL);
324 }
325
326 art::ScopedObjectAccess soa(jni_env);
327 art::ObjPtr<art::mirror::Object> obj = soa.Decode<art::mirror::Object>(object);
328 if (!gObjectTagTable.GetTag(obj.Ptr(), tag_ptr)) {
329 *tag_ptr = 0;
330 }
331
332 return ERR(NONE);
Alex Light49948e92016-08-11 15:35:28 -0700333 }
334
335 static jvmtiError SetTag(jvmtiEnv* env, jobject object, jlong tag) {
Alex Lighte6574242016-08-17 09:56:24 -0700336 ENSURE_HAS_CAP(env, can_tag_objects);
337
Andreas Gampe6dee92e2016-09-12 19:58:13 -0700338 if (object == nullptr) {
339 return ERR(NULL_POINTER);
340 }
341
342 JNIEnv* jni_env = GetJniEnv(env);
343 if (jni_env == nullptr) {
344 return ERR(INTERNAL);
345 }
346
347 art::ScopedObjectAccess soa(jni_env);
348 art::ObjPtr<art::mirror::Object> obj = soa.Decode<art::mirror::Object>(object);
Andreas Gampee54eee12016-10-20 19:03:58 -0700349 gObjectTagTable.Set(obj.Ptr(), tag);
Andreas Gampe6dee92e2016-09-12 19:58:13 -0700350
351 return ERR(NONE);
Alex Light49948e92016-08-11 15:35:28 -0700352 }
353
354 static jvmtiError GetObjectsWithTags(jvmtiEnv* env,
355 jint tag_count,
356 const jlong* tags,
357 jint* count_ptr,
358 jobject** object_result_ptr,
359 jlong** tag_result_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700360 ENSURE_HAS_CAP(env, can_tag_objects);
361
Andreas Gampe5e6046b2016-10-25 12:05:53 -0700362 JNIEnv* jni_env = GetJniEnv(env);
363 if (jni_env == nullptr) {
364 return ERR(INTERNAL);
365 }
366
367 art::ScopedObjectAccess soa(jni_env);
368 return gObjectTagTable.GetTaggedObjects(env,
369 tag_count,
370 tags,
371 count_ptr,
372 object_result_ptr,
373 tag_result_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700374 }
375
376 static jvmtiError ForceGarbageCollection(jvmtiEnv* env) {
Andreas Gampe8da6d032016-10-31 19:31:03 -0700377 return HeapUtil::ForceGarbageCollection(env);
Alex Light49948e92016-08-11 15:35:28 -0700378 }
379
380 static jvmtiError IterateOverObjectsReachableFromObject(
381 jvmtiEnv* env,
382 jobject object,
383 jvmtiObjectReferenceCallback object_reference_callback,
384 const void* user_data) {
385 return ERR(NOT_IMPLEMENTED);
386 }
387
388 static jvmtiError IterateOverReachableObjects(jvmtiEnv* env,
389 jvmtiHeapRootCallback heap_root_callback,
390 jvmtiStackReferenceCallback stack_ref_callback,
391 jvmtiObjectReferenceCallback object_ref_callback,
392 const void* user_data) {
393 return ERR(NOT_IMPLEMENTED);
394 }
395
396 static jvmtiError IterateOverHeap(jvmtiEnv* env,
397 jvmtiHeapObjectFilter object_filter,
398 jvmtiHeapObjectCallback heap_object_callback,
399 const void* user_data) {
400 return ERR(NOT_IMPLEMENTED);
401 }
402
403 static jvmtiError IterateOverInstancesOfClass(jvmtiEnv* env,
404 jclass klass,
405 jvmtiHeapObjectFilter object_filter,
406 jvmtiHeapObjectCallback heap_object_callback,
407 const void* user_data) {
408 return ERR(NOT_IMPLEMENTED);
409 }
410
411 static jvmtiError GetLocalObject(jvmtiEnv* env,
412 jthread thread,
413 jint depth,
414 jint slot,
415 jobject* value_ptr) {
416 return ERR(NOT_IMPLEMENTED);
417 }
418
419 static jvmtiError GetLocalInstance(jvmtiEnv* env,
420 jthread thread,
421 jint depth,
422 jobject* value_ptr) {
423 return ERR(NOT_IMPLEMENTED);
424 }
425
426 static jvmtiError GetLocalInt(jvmtiEnv* env,
427 jthread thread,
428 jint depth,
429 jint slot,
430 jint* value_ptr) {
431 return ERR(NOT_IMPLEMENTED);
432 }
433
434 static jvmtiError GetLocalLong(jvmtiEnv* env,
435 jthread thread,
436 jint depth,
437 jint slot,
438 jlong* value_ptr) {
439 return ERR(NOT_IMPLEMENTED);
440 }
441
442 static jvmtiError GetLocalFloat(jvmtiEnv* env,
443 jthread thread,
444 jint depth,
445 jint slot,
446 jfloat* value_ptr) {
447 return ERR(NOT_IMPLEMENTED);
448 }
449
450 static jvmtiError GetLocalDouble(jvmtiEnv* env,
451 jthread thread,
452 jint depth,
453 jint slot,
454 jdouble* value_ptr) {
455 return ERR(NOT_IMPLEMENTED);
456 }
457
458 static jvmtiError SetLocalObject(jvmtiEnv* env,
459 jthread thread,
460 jint depth,
461 jint slot,
462 jobject value) {
463 return ERR(NOT_IMPLEMENTED);
464 }
465
466 static jvmtiError SetLocalInt(jvmtiEnv* env,
467 jthread thread,
468 jint depth,
469 jint slot,
470 jint value) {
471 return ERR(NOT_IMPLEMENTED);
472 }
473
474 static jvmtiError SetLocalLong(jvmtiEnv* env,
475 jthread thread,
476 jint depth,
477 jint slot,
478 jlong value) {
479 return ERR(NOT_IMPLEMENTED);
480 }
481
482 static jvmtiError SetLocalFloat(jvmtiEnv* env,
483 jthread thread,
484 jint depth,
485 jint slot,
486 jfloat value) {
487 return ERR(NOT_IMPLEMENTED);
488 }
489
490 static jvmtiError SetLocalDouble(jvmtiEnv* env,
491 jthread thread,
492 jint depth,
493 jint slot,
494 jdouble value) {
495 return ERR(NOT_IMPLEMENTED);
496 }
497
498 static jvmtiError SetBreakpoint(jvmtiEnv* env, jmethodID method, jlocation location) {
499 return ERR(NOT_IMPLEMENTED);
500 }
501
502 static jvmtiError ClearBreakpoint(jvmtiEnv* env, jmethodID method, jlocation location) {
503 return ERR(NOT_IMPLEMENTED);
504 }
505
506 static jvmtiError SetFieldAccessWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
507 return ERR(NOT_IMPLEMENTED);
508 }
509
510 static jvmtiError ClearFieldAccessWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
511 return ERR(NOT_IMPLEMENTED);
512 }
513
514 static jvmtiError SetFieldModificationWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
515 return ERR(NOT_IMPLEMENTED);
516 }
517
518 static jvmtiError ClearFieldModificationWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
519 return ERR(NOT_IMPLEMENTED);
520 }
521
522 static jvmtiError GetLoadedClasses(jvmtiEnv* env, jint* class_count_ptr, jclass** classes_ptr) {
Andreas Gampeaa8b60c2016-10-12 12:51:25 -0700523 HeapUtil heap_util(&gObjectTagTable);
524 return heap_util.GetLoadedClasses(env, class_count_ptr, classes_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700525 }
526
527 static jvmtiError GetClassLoaderClasses(jvmtiEnv* env,
528 jobject initiating_loader,
529 jint* class_count_ptr,
530 jclass** classes_ptr) {
531 return ERR(NOT_IMPLEMENTED);
532 }
533
534 static jvmtiError GetClassSignature(jvmtiEnv* env,
535 jclass klass,
536 char** signature_ptr,
537 char** generic_ptr) {
Andreas Gampee492ae32016-10-28 19:34:57 -0700538 return ClassUtil::GetClassSignature(env, klass, signature_ptr, generic_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700539 }
540
541 static jvmtiError GetClassStatus(jvmtiEnv* env, jclass klass, jint* status_ptr) {
Andreas Gampeff9d2092017-01-06 09:12:49 -0800542 return ClassUtil::GetClassStatus(env, klass, status_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700543 }
544
545 static jvmtiError GetSourceFileName(jvmtiEnv* env, jclass klass, char** source_name_ptr) {
546 return ERR(NOT_IMPLEMENTED);
547 }
548
549 static jvmtiError GetClassModifiers(jvmtiEnv* env, jclass klass, jint* modifiers_ptr) {
Andreas Gampe64013e52017-01-06 13:07:19 -0800550 return ClassUtil::GetClassModifiers(env, klass, modifiers_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700551 }
552
553 static jvmtiError GetClassMethods(jvmtiEnv* env,
554 jclass klass,
555 jint* method_count_ptr,
556 jmethodID** methods_ptr) {
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800557 return ClassUtil::GetClassMethods(env, klass, method_count_ptr, methods_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700558 }
559
560 static jvmtiError GetClassFields(jvmtiEnv* env,
561 jclass klass,
562 jint* field_count_ptr,
563 jfieldID** fields_ptr) {
Andreas Gampeac587272017-01-05 15:21:34 -0800564 return ClassUtil::GetClassFields(env, klass, field_count_ptr, fields_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700565 }
566
567 static jvmtiError GetImplementedInterfaces(jvmtiEnv* env,
568 jclass klass,
569 jint* interface_count_ptr,
570 jclass** interfaces_ptr) {
Andreas Gampe8b07e472017-01-06 14:20:39 -0800571 return ClassUtil::GetImplementedInterfaces(env, klass, interface_count_ptr, interfaces_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700572 }
573
574 static jvmtiError GetClassVersionNumbers(jvmtiEnv* env,
575 jclass klass,
576 jint* minor_version_ptr,
577 jint* major_version_ptr) {
578 return ERR(NOT_IMPLEMENTED);
579 }
580
581 static jvmtiError GetConstantPool(jvmtiEnv* env,
582 jclass klass,
583 jint* constant_pool_count_ptr,
584 jint* constant_pool_byte_count_ptr,
585 unsigned char** constant_pool_bytes_ptr) {
586 return ERR(NOT_IMPLEMENTED);
587 }
588
589 static jvmtiError IsInterface(jvmtiEnv* env, jclass klass, jboolean* is_interface_ptr) {
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800590 return ClassUtil::IsInterface(env, klass, is_interface_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700591 }
592
593 static jvmtiError IsArrayClass(jvmtiEnv* env,
594 jclass klass,
595 jboolean* is_array_class_ptr) {
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800596 return ClassUtil::IsArrayClass(env, klass, is_array_class_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700597 }
598
599 static jvmtiError IsModifiableClass(jvmtiEnv* env,
600 jclass klass,
601 jboolean* is_modifiable_class_ptr) {
602 return ERR(NOT_IMPLEMENTED);
603 }
604
605 static jvmtiError GetClassLoader(jvmtiEnv* env, jclass klass, jobject* classloader_ptr) {
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800606 return ClassUtil::GetClassLoader(env, klass, classloader_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700607 }
608
609 static jvmtiError GetSourceDebugExtension(jvmtiEnv* env,
610 jclass klass,
611 char** source_debug_extension_ptr) {
612 return ERR(NOT_IMPLEMENTED);
613 }
614
615 static jvmtiError RetransformClasses(jvmtiEnv* env, jint class_count, const jclass* classes) {
616 return ERR(NOT_IMPLEMENTED);
617 }
618
619 static jvmtiError RedefineClasses(jvmtiEnv* env,
620 jint class_count,
621 const jvmtiClassDefinition* class_definitions) {
622 return ERR(NOT_IMPLEMENTED);
623 }
624
625 static jvmtiError GetObjectSize(jvmtiEnv* env, jobject object, jlong* size_ptr) {
Andreas Gampe50a4e492017-01-06 18:00:20 -0800626 return ObjectUtil::GetObjectSize(env, object, size_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700627 }
628
629 static jvmtiError GetObjectHashCode(jvmtiEnv* env, jobject object, jint* hash_code_ptr) {
Andreas Gampe50a4e492017-01-06 18:00:20 -0800630 return ObjectUtil::GetObjectHashCode(env, object, hash_code_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700631 }
632
633 static jvmtiError GetObjectMonitorUsage(jvmtiEnv* env,
634 jobject object,
635 jvmtiMonitorUsage* info_ptr) {
636 return ERR(NOT_IMPLEMENTED);
637 }
638
639 static jvmtiError GetFieldName(jvmtiEnv* env,
640 jclass klass,
641 jfieldID field,
642 char** name_ptr,
643 char** signature_ptr,
644 char** generic_ptr) {
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800645 return FieldUtil::GetFieldName(env, klass, field, name_ptr, signature_ptr, generic_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700646 }
647
648 static jvmtiError GetFieldDeclaringClass(jvmtiEnv* env,
649 jclass klass,
650 jfieldID field,
651 jclass* declaring_class_ptr) {
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800652 return FieldUtil::GetFieldDeclaringClass(env, klass, field, declaring_class_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700653 }
654
655 static jvmtiError GetFieldModifiers(jvmtiEnv* env,
656 jclass klass,
657 jfieldID field,
658 jint* modifiers_ptr) {
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800659 return FieldUtil::GetFieldModifiers(env, klass, field, modifiers_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700660 }
661
662 static jvmtiError IsFieldSynthetic(jvmtiEnv* env,
663 jclass klass,
664 jfieldID field,
665 jboolean* is_synthetic_ptr) {
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800666 return FieldUtil::IsFieldSynthetic(env, klass, field, is_synthetic_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700667 }
668
669 static jvmtiError GetMethodName(jvmtiEnv* env,
670 jmethodID method,
671 char** name_ptr,
672 char** signature_ptr,
673 char** generic_ptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700674 return MethodUtil::GetMethodName(env, method, name_ptr, signature_ptr, generic_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700675 }
676
677 static jvmtiError GetMethodDeclaringClass(jvmtiEnv* env,
678 jmethodID method,
679 jclass* declaring_class_ptr) {
Andreas Gampe368a2082016-10-28 17:33:13 -0700680 return MethodUtil::GetMethodDeclaringClass(env, method, declaring_class_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700681 }
682
683 static jvmtiError GetMethodModifiers(jvmtiEnv* env,
684 jmethodID method,
685 jint* modifiers_ptr) {
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700686 return MethodUtil::GetMethodModifiers(env, method, modifiers_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700687 }
688
689 static jvmtiError GetMaxLocals(jvmtiEnv* env,
690 jmethodID method,
691 jint* max_ptr) {
692 return ERR(NOT_IMPLEMENTED);
693 }
694
695 static jvmtiError GetArgumentsSize(jvmtiEnv* env,
696 jmethodID method,
697 jint* size_ptr) {
698 return ERR(NOT_IMPLEMENTED);
699 }
700
701 static jvmtiError GetLineNumberTable(jvmtiEnv* env,
702 jmethodID method,
703 jint* entry_count_ptr,
704 jvmtiLineNumberEntry** table_ptr) {
Andreas Gampeda3e5612016-12-13 19:00:53 -0800705 return MethodUtil::GetLineNumberTable(env, method, entry_count_ptr, table_ptr);
Alex Light49948e92016-08-11 15:35:28 -0700706 }
707
708 static jvmtiError GetMethodLocation(jvmtiEnv* env,
709 jmethodID method,
710 jlocation* start_location_ptr,
711 jlocation* end_location_ptr) {
712 return ERR(NOT_IMPLEMENTED);
713 }
714
715 static jvmtiError GetLocalVariableTable(jvmtiEnv* env,
716 jmethodID method,
717 jint* entry_count_ptr,
718 jvmtiLocalVariableEntry** table_ptr) {
719 return ERR(NOT_IMPLEMENTED);
720 }
721
722 static jvmtiError GetBytecodes(jvmtiEnv* env,
723 jmethodID method,
724 jint* bytecode_count_ptr,
725 unsigned char** bytecodes_ptr) {
726 return ERR(NOT_IMPLEMENTED);
727 }
728
729 static jvmtiError IsMethodNative(jvmtiEnv* env, jmethodID method, jboolean* is_native_ptr) {
730 return ERR(NOT_IMPLEMENTED);
731 }
732
733 static jvmtiError IsMethodSynthetic(jvmtiEnv* env, jmethodID method, jboolean* is_synthetic_ptr) {
734 return ERR(NOT_IMPLEMENTED);
735 }
736
737 static jvmtiError IsMethodObsolete(jvmtiEnv* env, jmethodID method, jboolean* is_obsolete_ptr) {
738 return ERR(NOT_IMPLEMENTED);
739 }
740
741 static jvmtiError SetNativeMethodPrefix(jvmtiEnv* env, const char* prefix) {
742 return ERR(NOT_IMPLEMENTED);
743 }
744
745 static jvmtiError SetNativeMethodPrefixes(jvmtiEnv* env, jint prefix_count, char** prefixes) {
746 return ERR(NOT_IMPLEMENTED);
747 }
748
749 static jvmtiError CreateRawMonitor(jvmtiEnv* env, const char* name, jrawMonitorID* monitor_ptr) {
750 return ERR(NOT_IMPLEMENTED);
751 }
752
753 static jvmtiError DestroyRawMonitor(jvmtiEnv* env, jrawMonitorID monitor) {
754 return ERR(NOT_IMPLEMENTED);
755 }
756
757 static jvmtiError RawMonitorEnter(jvmtiEnv* env, jrawMonitorID monitor) {
758 return ERR(NOT_IMPLEMENTED);
759 }
760
761 static jvmtiError RawMonitorExit(jvmtiEnv* env, jrawMonitorID monitor) {
762 return ERR(NOT_IMPLEMENTED);
763 }
764
765 static jvmtiError RawMonitorWait(jvmtiEnv* env, jrawMonitorID monitor, jlong millis) {
766 return ERR(NOT_IMPLEMENTED);
767 }
768
769 static jvmtiError RawMonitorNotify(jvmtiEnv* env, jrawMonitorID monitor) {
770 return ERR(NOT_IMPLEMENTED);
771 }
772
773 static jvmtiError RawMonitorNotifyAll(jvmtiEnv* env, jrawMonitorID monitor) {
774 return ERR(NOT_IMPLEMENTED);
775 }
776
777 static jvmtiError SetJNIFunctionTable(jvmtiEnv* env, const jniNativeInterface* function_table) {
778 return ERR(NOT_IMPLEMENTED);
779 }
780
781 static jvmtiError GetJNIFunctionTable(jvmtiEnv* env, jniNativeInterface** function_table) {
782 return ERR(NOT_IMPLEMENTED);
783 }
784
Andreas Gampe77708d92016-10-07 11:48:21 -0700785 // TODO: This will require locking, so that an agent can't remove callbacks when we're dispatching
786 // an event.
Alex Light49948e92016-08-11 15:35:28 -0700787 static jvmtiError SetEventCallbacks(jvmtiEnv* env,
788 const jvmtiEventCallbacks* callbacks,
789 jint size_of_callbacks) {
Alex Lighte6574242016-08-17 09:56:24 -0700790 ENSURE_VALID_ENV(env);
Andreas Gampe77708d92016-10-07 11:48:21 -0700791 if (size_of_callbacks < 0) {
792 return ERR(ILLEGAL_ARGUMENT);
793 }
794
795 if (callbacks == nullptr) {
796 ArtJvmTiEnv::AsArtJvmTiEnv(env)->event_callbacks.reset();
797 return ERR(NONE);
798 }
799
800 std::unique_ptr<jvmtiEventCallbacks> tmp(new jvmtiEventCallbacks());
801 memset(tmp.get(), 0, sizeof(jvmtiEventCallbacks));
802 size_t copy_size = std::min(sizeof(jvmtiEventCallbacks),
803 static_cast<size_t>(size_of_callbacks));
804 copy_size = art::RoundDown(copy_size, sizeof(void*));
805 memcpy(tmp.get(), callbacks, copy_size);
806
807 ArtJvmTiEnv::AsArtJvmTiEnv(env)->event_callbacks = std::move(tmp);
808
809 return ERR(NONE);
Alex Light49948e92016-08-11 15:35:28 -0700810 }
811
812 static jvmtiError SetEventNotificationMode(jvmtiEnv* env,
813 jvmtiEventMode mode,
814 jvmtiEvent event_type,
815 jthread event_thread,
816 ...) {
Alex Lighte6574242016-08-17 09:56:24 -0700817 ENSURE_VALID_ENV(env);
818 // TODO: Check for capabilities.
Andreas Gampe77708d92016-10-07 11:48:21 -0700819 art::Thread* art_thread = nullptr;
820 if (event_thread != nullptr) {
821 // TODO: Need non-aborting call here, to return JVMTI_ERROR_INVALID_THREAD.
822 art::ScopedObjectAccess soa(art::Thread::Current());
823 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
824 art_thread = art::Thread::FromManagedThread(soa, event_thread);
825
826 if (art_thread == nullptr || // The thread hasn't been started or is already dead.
827 art_thread->IsStillStarting()) {
828 // TODO: We may want to let the EventHandler know, so it could clean up masks, potentially.
829 return ERR(THREAD_NOT_ALIVE);
830 }
831 }
832
833 return gEventHandler.SetEvent(ArtJvmTiEnv::AsArtJvmTiEnv(env), art_thread, event_type, mode);
Alex Light49948e92016-08-11 15:35:28 -0700834 }
835
836 static jvmtiError GenerateEvents(jvmtiEnv* env, jvmtiEvent event_type) {
837 return ERR(NOT_IMPLEMENTED);
838 }
839
840 static jvmtiError GetExtensionFunctions(jvmtiEnv* env,
841 jint* extension_count_ptr,
842 jvmtiExtensionFunctionInfo** extensions) {
843 return ERR(NOT_IMPLEMENTED);
844 }
845
846 static jvmtiError GetExtensionEvents(jvmtiEnv* env,
847 jint* extension_count_ptr,
848 jvmtiExtensionEventInfo** extensions) {
849 return ERR(NOT_IMPLEMENTED);
850 }
851
852 static jvmtiError SetExtensionEventCallback(jvmtiEnv* env,
853 jint extension_event_index,
854 jvmtiExtensionEvent callback) {
855 return ERR(NOT_IMPLEMENTED);
856 }
857
858 static jvmtiError GetPotentialCapabilities(jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700859 ENSURE_VALID_ENV(env);
860 ENSURE_NON_NULL(capabilities_ptr);
861 *capabilities_ptr = kPotentialCapabilities;
862 return OK;
Alex Light49948e92016-08-11 15:35:28 -0700863 }
864
865 static jvmtiError AddCapabilities(jvmtiEnv* env, const jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700866 ENSURE_VALID_ENV(env);
867 ENSURE_NON_NULL(capabilities_ptr);
868 ArtJvmTiEnv* art_env = static_cast<ArtJvmTiEnv*>(env);
869 jvmtiError ret = OK;
870#define ADD_CAPABILITY(e) \
871 do { \
872 if (capabilities_ptr->e == 1) { \
873 if (kPotentialCapabilities.e == 1) { \
874 art_env->capabilities.e = 1;\
875 } else { \
876 ret = ERR(NOT_AVAILABLE); \
877 } \
878 } \
879 } while (false)
880
881 ADD_CAPABILITY(can_tag_objects);
882 ADD_CAPABILITY(can_generate_field_modification_events);
883 ADD_CAPABILITY(can_generate_field_access_events);
884 ADD_CAPABILITY(can_get_bytecodes);
885 ADD_CAPABILITY(can_get_synthetic_attribute);
886 ADD_CAPABILITY(can_get_owned_monitor_info);
887 ADD_CAPABILITY(can_get_current_contended_monitor);
888 ADD_CAPABILITY(can_get_monitor_info);
889 ADD_CAPABILITY(can_pop_frame);
890 ADD_CAPABILITY(can_redefine_classes);
891 ADD_CAPABILITY(can_signal_thread);
892 ADD_CAPABILITY(can_get_source_file_name);
893 ADD_CAPABILITY(can_get_line_numbers);
894 ADD_CAPABILITY(can_get_source_debug_extension);
895 ADD_CAPABILITY(can_access_local_variables);
896 ADD_CAPABILITY(can_maintain_original_method_order);
897 ADD_CAPABILITY(can_generate_single_step_events);
898 ADD_CAPABILITY(can_generate_exception_events);
899 ADD_CAPABILITY(can_generate_frame_pop_events);
900 ADD_CAPABILITY(can_generate_breakpoint_events);
901 ADD_CAPABILITY(can_suspend);
902 ADD_CAPABILITY(can_redefine_any_class);
903 ADD_CAPABILITY(can_get_current_thread_cpu_time);
904 ADD_CAPABILITY(can_get_thread_cpu_time);
905 ADD_CAPABILITY(can_generate_method_entry_events);
906 ADD_CAPABILITY(can_generate_method_exit_events);
907 ADD_CAPABILITY(can_generate_all_class_hook_events);
908 ADD_CAPABILITY(can_generate_compiled_method_load_events);
909 ADD_CAPABILITY(can_generate_monitor_events);
910 ADD_CAPABILITY(can_generate_vm_object_alloc_events);
911 ADD_CAPABILITY(can_generate_native_method_bind_events);
912 ADD_CAPABILITY(can_generate_garbage_collection_events);
913 ADD_CAPABILITY(can_generate_object_free_events);
914 ADD_CAPABILITY(can_force_early_return);
915 ADD_CAPABILITY(can_get_owned_monitor_stack_depth_info);
916 ADD_CAPABILITY(can_get_constant_pool);
917 ADD_CAPABILITY(can_set_native_method_prefix);
918 ADD_CAPABILITY(can_retransform_classes);
919 ADD_CAPABILITY(can_retransform_any_class);
920 ADD_CAPABILITY(can_generate_resource_exhaustion_heap_events);
921 ADD_CAPABILITY(can_generate_resource_exhaustion_threads_events);
922#undef ADD_CAPABILITY
923 return ret;
Alex Light49948e92016-08-11 15:35:28 -0700924 }
925
926 static jvmtiError RelinquishCapabilities(jvmtiEnv* env,
927 const jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700928 ENSURE_VALID_ENV(env);
929 ENSURE_NON_NULL(capabilities_ptr);
930 ArtJvmTiEnv* art_env = reinterpret_cast<ArtJvmTiEnv*>(env);
931#define DEL_CAPABILITY(e) \
932 do { \
933 if (capabilities_ptr->e == 1) { \
934 art_env->capabilities.e = 0;\
935 } \
936 } while (false)
937
938 DEL_CAPABILITY(can_tag_objects);
939 DEL_CAPABILITY(can_generate_field_modification_events);
940 DEL_CAPABILITY(can_generate_field_access_events);
941 DEL_CAPABILITY(can_get_bytecodes);
942 DEL_CAPABILITY(can_get_synthetic_attribute);
943 DEL_CAPABILITY(can_get_owned_monitor_info);
944 DEL_CAPABILITY(can_get_current_contended_monitor);
945 DEL_CAPABILITY(can_get_monitor_info);
946 DEL_CAPABILITY(can_pop_frame);
947 DEL_CAPABILITY(can_redefine_classes);
948 DEL_CAPABILITY(can_signal_thread);
949 DEL_CAPABILITY(can_get_source_file_name);
950 DEL_CAPABILITY(can_get_line_numbers);
951 DEL_CAPABILITY(can_get_source_debug_extension);
952 DEL_CAPABILITY(can_access_local_variables);
953 DEL_CAPABILITY(can_maintain_original_method_order);
954 DEL_CAPABILITY(can_generate_single_step_events);
955 DEL_CAPABILITY(can_generate_exception_events);
956 DEL_CAPABILITY(can_generate_frame_pop_events);
957 DEL_CAPABILITY(can_generate_breakpoint_events);
958 DEL_CAPABILITY(can_suspend);
959 DEL_CAPABILITY(can_redefine_any_class);
960 DEL_CAPABILITY(can_get_current_thread_cpu_time);
961 DEL_CAPABILITY(can_get_thread_cpu_time);
962 DEL_CAPABILITY(can_generate_method_entry_events);
963 DEL_CAPABILITY(can_generate_method_exit_events);
964 DEL_CAPABILITY(can_generate_all_class_hook_events);
965 DEL_CAPABILITY(can_generate_compiled_method_load_events);
966 DEL_CAPABILITY(can_generate_monitor_events);
967 DEL_CAPABILITY(can_generate_vm_object_alloc_events);
968 DEL_CAPABILITY(can_generate_native_method_bind_events);
969 DEL_CAPABILITY(can_generate_garbage_collection_events);
970 DEL_CAPABILITY(can_generate_object_free_events);
971 DEL_CAPABILITY(can_force_early_return);
972 DEL_CAPABILITY(can_get_owned_monitor_stack_depth_info);
973 DEL_CAPABILITY(can_get_constant_pool);
974 DEL_CAPABILITY(can_set_native_method_prefix);
975 DEL_CAPABILITY(can_retransform_classes);
976 DEL_CAPABILITY(can_retransform_any_class);
977 DEL_CAPABILITY(can_generate_resource_exhaustion_heap_events);
978 DEL_CAPABILITY(can_generate_resource_exhaustion_threads_events);
979#undef DEL_CAPABILITY
980 return OK;
Alex Light49948e92016-08-11 15:35:28 -0700981 }
982
983 static jvmtiError GetCapabilities(jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700984 ENSURE_VALID_ENV(env);
985 ENSURE_NON_NULL(capabilities_ptr);
986 ArtJvmTiEnv* artenv = reinterpret_cast<ArtJvmTiEnv*>(env);
987 *capabilities_ptr = artenv->capabilities;
988 return OK;
Alex Light49948e92016-08-11 15:35:28 -0700989 }
990
991 static jvmtiError GetCurrentThreadCpuTimerInfo(jvmtiEnv* env, jvmtiTimerInfo* info_ptr) {
992 return ERR(NOT_IMPLEMENTED);
993 }
994
995 static jvmtiError GetCurrentThreadCpuTime(jvmtiEnv* env, jlong* nanos_ptr) {
996 return ERR(NOT_IMPLEMENTED);
997 }
998
999 static jvmtiError GetThreadCpuTimerInfo(jvmtiEnv* env, jvmtiTimerInfo* info_ptr) {
1000 return ERR(NOT_IMPLEMENTED);
1001 }
1002
1003 static jvmtiError GetThreadCpuTime(jvmtiEnv* env, jthread thread, jlong* nanos_ptr) {
1004 return ERR(NOT_IMPLEMENTED);
1005 }
1006
1007 static jvmtiError GetTimerInfo(jvmtiEnv* env, jvmtiTimerInfo* info_ptr) {
1008 return ERR(NOT_IMPLEMENTED);
1009 }
1010
1011 static jvmtiError GetTime(jvmtiEnv* env, jlong* nanos_ptr) {
1012 return ERR(NOT_IMPLEMENTED);
1013 }
1014
1015 static jvmtiError GetAvailableProcessors(jvmtiEnv* env, jint* processor_count_ptr) {
1016 return ERR(NOT_IMPLEMENTED);
1017 }
1018
1019 static jvmtiError AddToBootstrapClassLoaderSearch(jvmtiEnv* env, const char* segment) {
1020 return ERR(NOT_IMPLEMENTED);
1021 }
1022
1023 static jvmtiError AddToSystemClassLoaderSearch(jvmtiEnv* env, const char* segment) {
1024 return ERR(NOT_IMPLEMENTED);
1025 }
1026
1027 static jvmtiError GetSystemProperties(jvmtiEnv* env, jint* count_ptr, char*** property_ptr) {
1028 return ERR(NOT_IMPLEMENTED);
1029 }
1030
1031 static jvmtiError GetSystemProperty(jvmtiEnv* env, const char* property, char** value_ptr) {
1032 return ERR(NOT_IMPLEMENTED);
1033 }
1034
1035 static jvmtiError SetSystemProperty(jvmtiEnv* env, const char* property, const char* value) {
1036 return ERR(NOT_IMPLEMENTED);
1037 }
1038
1039 static jvmtiError GetPhase(jvmtiEnv* env, jvmtiPhase* phase_ptr) {
1040 return ERR(NOT_IMPLEMENTED);
1041 }
1042
1043 static jvmtiError DisposeEnvironment(jvmtiEnv* env) {
Alex Lighte6574242016-08-17 09:56:24 -07001044 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001045 delete env;
1046 return OK;
1047 }
1048
1049 static jvmtiError SetEnvironmentLocalStorage(jvmtiEnv* env, const void* data) {
Alex Lighte6574242016-08-17 09:56:24 -07001050 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001051 reinterpret_cast<ArtJvmTiEnv*>(env)->local_data = const_cast<void*>(data);
1052 return OK;
1053 }
1054
1055 static jvmtiError GetEnvironmentLocalStorage(jvmtiEnv* env, void** data_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001056 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001057 *data_ptr = reinterpret_cast<ArtJvmTiEnv*>(env)->local_data;
1058 return OK;
1059 }
1060
1061 static jvmtiError GetVersionNumber(jvmtiEnv* env, jint* version_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001062 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001063 *version_ptr = JVMTI_VERSION;
1064 return OK;
1065 }
1066
1067 static jvmtiError GetErrorName(jvmtiEnv* env, jvmtiError error, char** name_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001068 ENSURE_NON_NULL(name_ptr);
Alex Light49948e92016-08-11 15:35:28 -07001069 switch (error) {
1070#define ERROR_CASE(e) case (JVMTI_ERROR_ ## e) : do { \
Alex Light41960712017-01-06 14:44:23 -08001071 jvmtiError res = CopyString(env, \
1072 "JVMTI_ERROR_"#e, \
1073 reinterpret_cast<unsigned char**>(name_ptr)); \
1074 if (res != OK) { \
1075 *name_ptr = nullptr; \
1076 return res; \
1077 } else { \
1078 return OK; \
1079 } \
Alex Light49948e92016-08-11 15:35:28 -07001080 } while (false)
1081 ERROR_CASE(NONE);
1082 ERROR_CASE(INVALID_THREAD);
1083 ERROR_CASE(INVALID_THREAD_GROUP);
1084 ERROR_CASE(INVALID_PRIORITY);
1085 ERROR_CASE(THREAD_NOT_SUSPENDED);
1086 ERROR_CASE(THREAD_NOT_ALIVE);
1087 ERROR_CASE(INVALID_OBJECT);
1088 ERROR_CASE(INVALID_CLASS);
1089 ERROR_CASE(CLASS_NOT_PREPARED);
1090 ERROR_CASE(INVALID_METHODID);
1091 ERROR_CASE(INVALID_LOCATION);
1092 ERROR_CASE(INVALID_FIELDID);
1093 ERROR_CASE(NO_MORE_FRAMES);
1094 ERROR_CASE(OPAQUE_FRAME);
1095 ERROR_CASE(TYPE_MISMATCH);
1096 ERROR_CASE(INVALID_SLOT);
1097 ERROR_CASE(DUPLICATE);
1098 ERROR_CASE(NOT_FOUND);
1099 ERROR_CASE(INVALID_MONITOR);
1100 ERROR_CASE(NOT_MONITOR_OWNER);
1101 ERROR_CASE(INTERRUPT);
1102 ERROR_CASE(INVALID_CLASS_FORMAT);
1103 ERROR_CASE(CIRCULAR_CLASS_DEFINITION);
1104 ERROR_CASE(FAILS_VERIFICATION);
1105 ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_ADDED);
1106 ERROR_CASE(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED);
1107 ERROR_CASE(INVALID_TYPESTATE);
1108 ERROR_CASE(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED);
1109 ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_DELETED);
1110 ERROR_CASE(UNSUPPORTED_VERSION);
1111 ERROR_CASE(NAMES_DONT_MATCH);
1112 ERROR_CASE(UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED);
1113 ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED);
1114 ERROR_CASE(UNMODIFIABLE_CLASS);
1115 ERROR_CASE(NOT_AVAILABLE);
1116 ERROR_CASE(MUST_POSSESS_CAPABILITY);
1117 ERROR_CASE(NULL_POINTER);
1118 ERROR_CASE(ABSENT_INFORMATION);
1119 ERROR_CASE(INVALID_EVENT_TYPE);
1120 ERROR_CASE(ILLEGAL_ARGUMENT);
1121 ERROR_CASE(NATIVE_METHOD);
1122 ERROR_CASE(CLASS_LOADER_UNSUPPORTED);
1123 ERROR_CASE(OUT_OF_MEMORY);
1124 ERROR_CASE(ACCESS_DENIED);
1125 ERROR_CASE(WRONG_PHASE);
1126 ERROR_CASE(INTERNAL);
1127 ERROR_CASE(UNATTACHED_THREAD);
1128 ERROR_CASE(INVALID_ENVIRONMENT);
1129#undef ERROR_CASE
1130 default: {
Alex Light41960712017-01-06 14:44:23 -08001131 jvmtiError res = CopyString(env,
1132 "JVMTI_ERROR_UNKNOWN",
1133 reinterpret_cast<unsigned char**>(name_ptr));
1134 if (res != OK) {
1135 *name_ptr = nullptr;
1136 return res;
1137 } else {
1138 return ERR(ILLEGAL_ARGUMENT);
1139 }
Alex Light49948e92016-08-11 15:35:28 -07001140 }
1141 }
1142 }
1143
1144 static jvmtiError SetVerboseFlag(jvmtiEnv* env, jvmtiVerboseFlag flag, jboolean value) {
1145 return ERR(NOT_IMPLEMENTED);
1146 }
1147
1148 static jvmtiError GetJLocationFormat(jvmtiEnv* env, jvmtiJlocationFormat* format_ptr) {
1149 return ERR(NOT_IMPLEMENTED);
1150 }
Alex Light9c20a142016-08-23 15:05:12 -07001151
1152 // TODO Remove this once events are working.
1153 static jvmtiError RetransformClassWithHook(jvmtiEnv* env,
1154 jclass klass,
1155 jvmtiEventClassFileLoadHook hook) {
1156 std::vector<jclass> classes;
1157 classes.push_back(klass);
1158 return RetransformClassesWithHook(reinterpret_cast<ArtJvmTiEnv*>(env), classes, hook);
1159 }
1160
Alex Light1e07ca62016-12-02 11:40:56 -08001161 static jvmtiError RedefineClassDirect(ArtJvmTiEnv* env,
1162 jclass klass,
1163 jint dex_size,
1164 unsigned char* dex_file) {
1165 if (!IsValidEnv(env)) {
1166 return ERR(INVALID_ENVIRONMENT);
1167 }
1168 jvmtiError ret = OK;
1169 std::string location;
1170 if ((ret = GetClassLocation(env, klass, &location)) != OK) {
1171 // TODO Do something more here? Maybe give log statements?
1172 return ret;
1173 }
1174 std::string error;
1175 ret = Redefiner::RedefineClass(env,
1176 art::Runtime::Current(),
1177 art::Thread::Current(),
1178 klass,
1179 location,
1180 dex_size,
1181 reinterpret_cast<uint8_t*>(dex_file),
1182 &error);
1183 if (ret != OK) {
1184 LOG(ERROR) << "FAILURE TO REDEFINE " << error;
1185 }
1186 return ret;
1187 }
1188
Alex Light9c20a142016-08-23 15:05:12 -07001189 // TODO This will be called by the event handler for the art::ti Event Load Event
1190 static jvmtiError RetransformClassesWithHook(ArtJvmTiEnv* env,
1191 const std::vector<jclass>& classes,
1192 jvmtiEventClassFileLoadHook hook) {
1193 if (!IsValidEnv(env)) {
1194 return ERR(INVALID_ENVIRONMENT);
1195 }
Alex Lighta01de592016-11-15 10:43:06 -08001196 jvmtiError res = OK;
1197 std::string error;
Alex Light9c20a142016-08-23 15:05:12 -07001198 for (jclass klass : classes) {
1199 JNIEnv* jni_env = nullptr;
1200 jobject loader = nullptr;
1201 std::string name;
1202 jobject protection_domain = nullptr;
1203 jint data_len = 0;
1204 unsigned char* dex_data = nullptr;
1205 jvmtiError ret = OK;
1206 std::string location;
1207 if ((ret = GetTransformationData(env,
1208 klass,
1209 /*out*/&location,
1210 /*out*/&jni_env,
1211 /*out*/&loader,
1212 /*out*/&name,
1213 /*out*/&protection_domain,
1214 /*out*/&data_len,
1215 /*out*/&dex_data)) != OK) {
1216 // TODO Do something more here? Maybe give log statements?
1217 return ret;
1218 }
1219 jint new_data_len = 0;
1220 unsigned char* new_dex_data = nullptr;
1221 hook(env,
1222 jni_env,
1223 klass,
1224 loader,
1225 name.c_str(),
1226 protection_domain,
1227 data_len,
1228 dex_data,
1229 /*out*/&new_data_len,
1230 /*out*/&new_dex_data);
1231 // Check if anything actually changed.
1232 if ((new_data_len != 0 || new_dex_data != nullptr) && new_dex_data != dex_data) {
Alex Lighta01de592016-11-15 10:43:06 -08001233 res = Redefiner::RedefineClass(env,
1234 art::Runtime::Current(),
1235 art::Thread::Current(),
1236 klass,
1237 location,
1238 new_data_len,
1239 new_dex_data,
1240 &error);
Alex Light9c20a142016-08-23 15:05:12 -07001241 env->Deallocate(new_dex_data);
1242 }
1243 // Deallocate the old dex data.
1244 env->Deallocate(dex_data);
Alex Lighta01de592016-11-15 10:43:06 -08001245 if (res != OK) {
1246 LOG(ERROR) << "FAILURE TO REDEFINE " << error;
1247 return res;
1248 }
Alex Light9c20a142016-08-23 15:05:12 -07001249 }
1250 return OK;
1251 }
Alex Light49948e92016-08-11 15:35:28 -07001252};
1253
1254static bool IsJvmtiVersion(jint version) {
1255 return version == JVMTI_VERSION_1 ||
1256 version == JVMTI_VERSION_1_0 ||
1257 version == JVMTI_VERSION_1_1 ||
1258 version == JVMTI_VERSION_1_2 ||
1259 version == JVMTI_VERSION;
1260}
1261
1262// Creates a jvmtiEnv and returns it with the art::ti::Env that is associated with it. new_art_ti
1263// is a pointer to the uninitialized memory for an art::ti::Env.
1264static void CreateArtJvmTiEnv(art::JavaVMExt* vm, /*out*/void** new_jvmtiEnv) {
1265 struct ArtJvmTiEnv* env = new ArtJvmTiEnv(vm);
1266 *new_jvmtiEnv = env;
Andreas Gampe77708d92016-10-07 11:48:21 -07001267
1268 gEventHandler.RegisterArtJvmTiEnv(env);
Alex Light49948e92016-08-11 15:35:28 -07001269}
1270
1271// A hook that the runtime uses to allow plugins to handle GetEnv calls. It returns true and
1272// places the return value in 'env' if this library can handle the GetEnv request. Otherwise
1273// returns false and does not modify the 'env' pointer.
1274static jint GetEnvHandler(art::JavaVMExt* vm, /*out*/void** env, jint version) {
1275 if (IsJvmtiVersion(version)) {
1276 CreateArtJvmTiEnv(vm, env);
1277 return JNI_OK;
1278 } else {
1279 printf("version 0x%x is not valid!", version);
1280 return JNI_EVERSION;
1281 }
1282}
1283
1284// The plugin initialization function. This adds the jvmti environment.
1285extern "C" bool ArtPlugin_Initialize() {
Andreas Gampee08a2be2016-10-06 13:13:30 -07001286 art::Runtime* runtime = art::Runtime::Current();
1287 runtime->GetJavaVM()->AddEnvironmentHook(GetEnvHandler);
1288 runtime->AddSystemWeakHolder(&gObjectTagTable);
Alex Light49948e92016-08-11 15:35:28 -07001289 return true;
1290}
1291
1292// The actual struct holding all of the entrypoints into the jvmti interface.
1293const jvmtiInterface_1 gJvmtiInterface = {
Alex Light9c20a142016-08-23 15:05:12 -07001294 // SPECIAL FUNCTION: RetransformClassWithHook Is normally reserved1
1295 // TODO Remove once we have events working.
1296 reinterpret_cast<void*>(JvmtiFunctions::RetransformClassWithHook),
1297 // nullptr, // reserved1
Alex Light49948e92016-08-11 15:35:28 -07001298 JvmtiFunctions::SetEventNotificationMode,
Alex Light1e07ca62016-12-02 11:40:56 -08001299 // SPECIAL FUNCTION: RedefineClassDirect Is normally reserved3
1300 // TODO Remove once we have events working.
1301 reinterpret_cast<void*>(JvmtiFunctions::RedefineClassDirect),
1302 // nullptr, // reserved3
Alex Light49948e92016-08-11 15:35:28 -07001303 JvmtiFunctions::GetAllThreads,
1304 JvmtiFunctions::SuspendThread,
1305 JvmtiFunctions::ResumeThread,
1306 JvmtiFunctions::StopThread,
1307 JvmtiFunctions::InterruptThread,
1308 JvmtiFunctions::GetThreadInfo,
1309 JvmtiFunctions::GetOwnedMonitorInfo, // 10
1310 JvmtiFunctions::GetCurrentContendedMonitor,
1311 JvmtiFunctions::RunAgentThread,
1312 JvmtiFunctions::GetTopThreadGroups,
1313 JvmtiFunctions::GetThreadGroupInfo,
1314 JvmtiFunctions::GetThreadGroupChildren,
1315 JvmtiFunctions::GetFrameCount,
1316 JvmtiFunctions::GetThreadState,
1317 JvmtiFunctions::GetCurrentThread,
1318 JvmtiFunctions::GetFrameLocation,
1319 JvmtiFunctions::NotifyFramePop, // 20
1320 JvmtiFunctions::GetLocalObject,
1321 JvmtiFunctions::GetLocalInt,
1322 JvmtiFunctions::GetLocalLong,
1323 JvmtiFunctions::GetLocalFloat,
1324 JvmtiFunctions::GetLocalDouble,
1325 JvmtiFunctions::SetLocalObject,
1326 JvmtiFunctions::SetLocalInt,
1327 JvmtiFunctions::SetLocalLong,
1328 JvmtiFunctions::SetLocalFloat,
1329 JvmtiFunctions::SetLocalDouble, // 30
1330 JvmtiFunctions::CreateRawMonitor,
1331 JvmtiFunctions::DestroyRawMonitor,
1332 JvmtiFunctions::RawMonitorEnter,
1333 JvmtiFunctions::RawMonitorExit,
1334 JvmtiFunctions::RawMonitorWait,
1335 JvmtiFunctions::RawMonitorNotify,
1336 JvmtiFunctions::RawMonitorNotifyAll,
1337 JvmtiFunctions::SetBreakpoint,
1338 JvmtiFunctions::ClearBreakpoint,
1339 nullptr, // reserved40
1340 JvmtiFunctions::SetFieldAccessWatch,
1341 JvmtiFunctions::ClearFieldAccessWatch,
1342 JvmtiFunctions::SetFieldModificationWatch,
1343 JvmtiFunctions::ClearFieldModificationWatch,
1344 JvmtiFunctions::IsModifiableClass,
1345 JvmtiFunctions::Allocate,
1346 JvmtiFunctions::Deallocate,
1347 JvmtiFunctions::GetClassSignature,
1348 JvmtiFunctions::GetClassStatus,
1349 JvmtiFunctions::GetSourceFileName, // 50
1350 JvmtiFunctions::GetClassModifiers,
1351 JvmtiFunctions::GetClassMethods,
1352 JvmtiFunctions::GetClassFields,
1353 JvmtiFunctions::GetImplementedInterfaces,
1354 JvmtiFunctions::IsInterface,
1355 JvmtiFunctions::IsArrayClass,
1356 JvmtiFunctions::GetClassLoader,
1357 JvmtiFunctions::GetObjectHashCode,
1358 JvmtiFunctions::GetObjectMonitorUsage,
1359 JvmtiFunctions::GetFieldName, // 60
1360 JvmtiFunctions::GetFieldDeclaringClass,
1361 JvmtiFunctions::GetFieldModifiers,
1362 JvmtiFunctions::IsFieldSynthetic,
1363 JvmtiFunctions::GetMethodName,
1364 JvmtiFunctions::GetMethodDeclaringClass,
1365 JvmtiFunctions::GetMethodModifiers,
1366 nullptr, // reserved67
1367 JvmtiFunctions::GetMaxLocals,
1368 JvmtiFunctions::GetArgumentsSize,
1369 JvmtiFunctions::GetLineNumberTable, // 70
1370 JvmtiFunctions::GetMethodLocation,
1371 JvmtiFunctions::GetLocalVariableTable,
1372 JvmtiFunctions::SetNativeMethodPrefix,
1373 JvmtiFunctions::SetNativeMethodPrefixes,
1374 JvmtiFunctions::GetBytecodes,
1375 JvmtiFunctions::IsMethodNative,
1376 JvmtiFunctions::IsMethodSynthetic,
1377 JvmtiFunctions::GetLoadedClasses,
1378 JvmtiFunctions::GetClassLoaderClasses,
1379 JvmtiFunctions::PopFrame, // 80
1380 JvmtiFunctions::ForceEarlyReturnObject,
1381 JvmtiFunctions::ForceEarlyReturnInt,
1382 JvmtiFunctions::ForceEarlyReturnLong,
1383 JvmtiFunctions::ForceEarlyReturnFloat,
1384 JvmtiFunctions::ForceEarlyReturnDouble,
1385 JvmtiFunctions::ForceEarlyReturnVoid,
1386 JvmtiFunctions::RedefineClasses,
1387 JvmtiFunctions::GetVersionNumber,
1388 JvmtiFunctions::GetCapabilities,
1389 JvmtiFunctions::GetSourceDebugExtension, // 90
1390 JvmtiFunctions::IsMethodObsolete,
1391 JvmtiFunctions::SuspendThreadList,
1392 JvmtiFunctions::ResumeThreadList,
1393 nullptr, // reserved94
1394 nullptr, // reserved95
1395 nullptr, // reserved96
1396 nullptr, // reserved97
1397 nullptr, // reserved98
1398 nullptr, // reserved99
1399 JvmtiFunctions::GetAllStackTraces, // 100
1400 JvmtiFunctions::GetThreadListStackTraces,
1401 JvmtiFunctions::GetThreadLocalStorage,
1402 JvmtiFunctions::SetThreadLocalStorage,
1403 JvmtiFunctions::GetStackTrace,
1404 nullptr, // reserved105
1405 JvmtiFunctions::GetTag,
1406 JvmtiFunctions::SetTag,
1407 JvmtiFunctions::ForceGarbageCollection,
1408 JvmtiFunctions::IterateOverObjectsReachableFromObject,
1409 JvmtiFunctions::IterateOverReachableObjects, // 110
1410 JvmtiFunctions::IterateOverHeap,
1411 JvmtiFunctions::IterateOverInstancesOfClass,
1412 nullptr, // reserved113
1413 JvmtiFunctions::GetObjectsWithTags,
1414 JvmtiFunctions::FollowReferences,
1415 JvmtiFunctions::IterateThroughHeap,
1416 nullptr, // reserved117
1417 nullptr, // reserved118
1418 nullptr, // reserved119
1419 JvmtiFunctions::SetJNIFunctionTable, // 120
1420 JvmtiFunctions::GetJNIFunctionTable,
1421 JvmtiFunctions::SetEventCallbacks,
1422 JvmtiFunctions::GenerateEvents,
1423 JvmtiFunctions::GetExtensionFunctions,
1424 JvmtiFunctions::GetExtensionEvents,
1425 JvmtiFunctions::SetExtensionEventCallback,
1426 JvmtiFunctions::DisposeEnvironment,
1427 JvmtiFunctions::GetErrorName,
1428 JvmtiFunctions::GetJLocationFormat,
1429 JvmtiFunctions::GetSystemProperties, // 130
1430 JvmtiFunctions::GetSystemProperty,
1431 JvmtiFunctions::SetSystemProperty,
1432 JvmtiFunctions::GetPhase,
1433 JvmtiFunctions::GetCurrentThreadCpuTimerInfo,
1434 JvmtiFunctions::GetCurrentThreadCpuTime,
1435 JvmtiFunctions::GetThreadCpuTimerInfo,
1436 JvmtiFunctions::GetThreadCpuTime,
1437 JvmtiFunctions::GetTimerInfo,
1438 JvmtiFunctions::GetTime,
1439 JvmtiFunctions::GetPotentialCapabilities, // 140
1440 nullptr, // reserved141
1441 JvmtiFunctions::AddCapabilities,
1442 JvmtiFunctions::RelinquishCapabilities,
1443 JvmtiFunctions::GetAvailableProcessors,
1444 JvmtiFunctions::GetClassVersionNumbers,
1445 JvmtiFunctions::GetConstantPool,
1446 JvmtiFunctions::GetEnvironmentLocalStorage,
1447 JvmtiFunctions::SetEnvironmentLocalStorage,
1448 JvmtiFunctions::AddToBootstrapClassLoaderSearch,
1449 JvmtiFunctions::SetVerboseFlag, // 150
1450 JvmtiFunctions::AddToSystemClassLoaderSearch,
1451 JvmtiFunctions::RetransformClasses,
1452 JvmtiFunctions::GetOwnedMonitorStackDepthInfo,
1453 JvmtiFunctions::GetObjectSize,
1454 JvmtiFunctions::GetLocalInstance,
1455};
1456
1457}; // namespace openjdkjvmti