blob: 28cfd64a77af7e683f676c97c85330dc40ff1103 [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) {
Nicolas Geoffrayc66c0772017-01-10 08:59:31 +0000602 return ERR(NOT_IMPLEMENTED);
Alex Light49948e92016-08-11 15:35:28 -0700603 }
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) {
Andreas Gampee4c33842017-01-09 10:50:17 -0800843 // We do not have any extension functions.
844 *extension_count_ptr = 0;
845 *extensions = nullptr;
846
847 return ERR(NONE);
Alex Light49948e92016-08-11 15:35:28 -0700848 }
849
850 static jvmtiError GetExtensionEvents(jvmtiEnv* env,
851 jint* extension_count_ptr,
852 jvmtiExtensionEventInfo** extensions) {
Andreas Gampee4c33842017-01-09 10:50:17 -0800853 // We do not have any extension events.
854 *extension_count_ptr = 0;
855 *extensions = nullptr;
856
857 return ERR(NONE);
Alex Light49948e92016-08-11 15:35:28 -0700858 }
859
860 static jvmtiError SetExtensionEventCallback(jvmtiEnv* env,
861 jint extension_event_index,
862 jvmtiExtensionEvent callback) {
Andreas Gampee4c33842017-01-09 10:50:17 -0800863 // We do not have any extension events, so any call is illegal.
864 return ERR(ILLEGAL_ARGUMENT);
Alex Light49948e92016-08-11 15:35:28 -0700865 }
866
867 static jvmtiError GetPotentialCapabilities(jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700868 ENSURE_VALID_ENV(env);
869 ENSURE_NON_NULL(capabilities_ptr);
870 *capabilities_ptr = kPotentialCapabilities;
871 return OK;
Alex Light49948e92016-08-11 15:35:28 -0700872 }
873
874 static jvmtiError AddCapabilities(jvmtiEnv* env, const jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700875 ENSURE_VALID_ENV(env);
876 ENSURE_NON_NULL(capabilities_ptr);
877 ArtJvmTiEnv* art_env = static_cast<ArtJvmTiEnv*>(env);
878 jvmtiError ret = OK;
879#define ADD_CAPABILITY(e) \
880 do { \
881 if (capabilities_ptr->e == 1) { \
882 if (kPotentialCapabilities.e == 1) { \
883 art_env->capabilities.e = 1;\
884 } else { \
885 ret = ERR(NOT_AVAILABLE); \
886 } \
887 } \
888 } while (false)
889
890 ADD_CAPABILITY(can_tag_objects);
891 ADD_CAPABILITY(can_generate_field_modification_events);
892 ADD_CAPABILITY(can_generate_field_access_events);
893 ADD_CAPABILITY(can_get_bytecodes);
894 ADD_CAPABILITY(can_get_synthetic_attribute);
895 ADD_CAPABILITY(can_get_owned_monitor_info);
896 ADD_CAPABILITY(can_get_current_contended_monitor);
897 ADD_CAPABILITY(can_get_monitor_info);
898 ADD_CAPABILITY(can_pop_frame);
899 ADD_CAPABILITY(can_redefine_classes);
900 ADD_CAPABILITY(can_signal_thread);
901 ADD_CAPABILITY(can_get_source_file_name);
902 ADD_CAPABILITY(can_get_line_numbers);
903 ADD_CAPABILITY(can_get_source_debug_extension);
904 ADD_CAPABILITY(can_access_local_variables);
905 ADD_CAPABILITY(can_maintain_original_method_order);
906 ADD_CAPABILITY(can_generate_single_step_events);
907 ADD_CAPABILITY(can_generate_exception_events);
908 ADD_CAPABILITY(can_generate_frame_pop_events);
909 ADD_CAPABILITY(can_generate_breakpoint_events);
910 ADD_CAPABILITY(can_suspend);
911 ADD_CAPABILITY(can_redefine_any_class);
912 ADD_CAPABILITY(can_get_current_thread_cpu_time);
913 ADD_CAPABILITY(can_get_thread_cpu_time);
914 ADD_CAPABILITY(can_generate_method_entry_events);
915 ADD_CAPABILITY(can_generate_method_exit_events);
916 ADD_CAPABILITY(can_generate_all_class_hook_events);
917 ADD_CAPABILITY(can_generate_compiled_method_load_events);
918 ADD_CAPABILITY(can_generate_monitor_events);
919 ADD_CAPABILITY(can_generate_vm_object_alloc_events);
920 ADD_CAPABILITY(can_generate_native_method_bind_events);
921 ADD_CAPABILITY(can_generate_garbage_collection_events);
922 ADD_CAPABILITY(can_generate_object_free_events);
923 ADD_CAPABILITY(can_force_early_return);
924 ADD_CAPABILITY(can_get_owned_monitor_stack_depth_info);
925 ADD_CAPABILITY(can_get_constant_pool);
926 ADD_CAPABILITY(can_set_native_method_prefix);
927 ADD_CAPABILITY(can_retransform_classes);
928 ADD_CAPABILITY(can_retransform_any_class);
929 ADD_CAPABILITY(can_generate_resource_exhaustion_heap_events);
930 ADD_CAPABILITY(can_generate_resource_exhaustion_threads_events);
931#undef ADD_CAPABILITY
932 return ret;
Alex Light49948e92016-08-11 15:35:28 -0700933 }
934
935 static jvmtiError RelinquishCapabilities(jvmtiEnv* env,
936 const jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700937 ENSURE_VALID_ENV(env);
938 ENSURE_NON_NULL(capabilities_ptr);
939 ArtJvmTiEnv* art_env = reinterpret_cast<ArtJvmTiEnv*>(env);
940#define DEL_CAPABILITY(e) \
941 do { \
942 if (capabilities_ptr->e == 1) { \
943 art_env->capabilities.e = 0;\
944 } \
945 } while (false)
946
947 DEL_CAPABILITY(can_tag_objects);
948 DEL_CAPABILITY(can_generate_field_modification_events);
949 DEL_CAPABILITY(can_generate_field_access_events);
950 DEL_CAPABILITY(can_get_bytecodes);
951 DEL_CAPABILITY(can_get_synthetic_attribute);
952 DEL_CAPABILITY(can_get_owned_monitor_info);
953 DEL_CAPABILITY(can_get_current_contended_monitor);
954 DEL_CAPABILITY(can_get_monitor_info);
955 DEL_CAPABILITY(can_pop_frame);
956 DEL_CAPABILITY(can_redefine_classes);
957 DEL_CAPABILITY(can_signal_thread);
958 DEL_CAPABILITY(can_get_source_file_name);
959 DEL_CAPABILITY(can_get_line_numbers);
960 DEL_CAPABILITY(can_get_source_debug_extension);
961 DEL_CAPABILITY(can_access_local_variables);
962 DEL_CAPABILITY(can_maintain_original_method_order);
963 DEL_CAPABILITY(can_generate_single_step_events);
964 DEL_CAPABILITY(can_generate_exception_events);
965 DEL_CAPABILITY(can_generate_frame_pop_events);
966 DEL_CAPABILITY(can_generate_breakpoint_events);
967 DEL_CAPABILITY(can_suspend);
968 DEL_CAPABILITY(can_redefine_any_class);
969 DEL_CAPABILITY(can_get_current_thread_cpu_time);
970 DEL_CAPABILITY(can_get_thread_cpu_time);
971 DEL_CAPABILITY(can_generate_method_entry_events);
972 DEL_CAPABILITY(can_generate_method_exit_events);
973 DEL_CAPABILITY(can_generate_all_class_hook_events);
974 DEL_CAPABILITY(can_generate_compiled_method_load_events);
975 DEL_CAPABILITY(can_generate_monitor_events);
976 DEL_CAPABILITY(can_generate_vm_object_alloc_events);
977 DEL_CAPABILITY(can_generate_native_method_bind_events);
978 DEL_CAPABILITY(can_generate_garbage_collection_events);
979 DEL_CAPABILITY(can_generate_object_free_events);
980 DEL_CAPABILITY(can_force_early_return);
981 DEL_CAPABILITY(can_get_owned_monitor_stack_depth_info);
982 DEL_CAPABILITY(can_get_constant_pool);
983 DEL_CAPABILITY(can_set_native_method_prefix);
984 DEL_CAPABILITY(can_retransform_classes);
985 DEL_CAPABILITY(can_retransform_any_class);
986 DEL_CAPABILITY(can_generate_resource_exhaustion_heap_events);
987 DEL_CAPABILITY(can_generate_resource_exhaustion_threads_events);
988#undef DEL_CAPABILITY
989 return OK;
Alex Light49948e92016-08-11 15:35:28 -0700990 }
991
992 static jvmtiError GetCapabilities(jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -0700993 ENSURE_VALID_ENV(env);
994 ENSURE_NON_NULL(capabilities_ptr);
995 ArtJvmTiEnv* artenv = reinterpret_cast<ArtJvmTiEnv*>(env);
996 *capabilities_ptr = artenv->capabilities;
997 return OK;
Alex Light49948e92016-08-11 15:35:28 -0700998 }
999
1000 static jvmtiError GetCurrentThreadCpuTimerInfo(jvmtiEnv* env, jvmtiTimerInfo* info_ptr) {
1001 return ERR(NOT_IMPLEMENTED);
1002 }
1003
1004 static jvmtiError GetCurrentThreadCpuTime(jvmtiEnv* env, jlong* nanos_ptr) {
1005 return ERR(NOT_IMPLEMENTED);
1006 }
1007
1008 static jvmtiError GetThreadCpuTimerInfo(jvmtiEnv* env, jvmtiTimerInfo* info_ptr) {
1009 return ERR(NOT_IMPLEMENTED);
1010 }
1011
1012 static jvmtiError GetThreadCpuTime(jvmtiEnv* env, jthread thread, jlong* nanos_ptr) {
1013 return ERR(NOT_IMPLEMENTED);
1014 }
1015
1016 static jvmtiError GetTimerInfo(jvmtiEnv* env, jvmtiTimerInfo* info_ptr) {
1017 return ERR(NOT_IMPLEMENTED);
1018 }
1019
1020 static jvmtiError GetTime(jvmtiEnv* env, jlong* nanos_ptr) {
1021 return ERR(NOT_IMPLEMENTED);
1022 }
1023
1024 static jvmtiError GetAvailableProcessors(jvmtiEnv* env, jint* processor_count_ptr) {
1025 return ERR(NOT_IMPLEMENTED);
1026 }
1027
1028 static jvmtiError AddToBootstrapClassLoaderSearch(jvmtiEnv* env, const char* segment) {
1029 return ERR(NOT_IMPLEMENTED);
1030 }
1031
1032 static jvmtiError AddToSystemClassLoaderSearch(jvmtiEnv* env, const char* segment) {
1033 return ERR(NOT_IMPLEMENTED);
1034 }
1035
1036 static jvmtiError GetSystemProperties(jvmtiEnv* env, jint* count_ptr, char*** property_ptr) {
1037 return ERR(NOT_IMPLEMENTED);
1038 }
1039
1040 static jvmtiError GetSystemProperty(jvmtiEnv* env, const char* property, char** value_ptr) {
1041 return ERR(NOT_IMPLEMENTED);
1042 }
1043
1044 static jvmtiError SetSystemProperty(jvmtiEnv* env, const char* property, const char* value) {
1045 return ERR(NOT_IMPLEMENTED);
1046 }
1047
1048 static jvmtiError GetPhase(jvmtiEnv* env, jvmtiPhase* phase_ptr) {
1049 return ERR(NOT_IMPLEMENTED);
1050 }
1051
1052 static jvmtiError DisposeEnvironment(jvmtiEnv* env) {
Alex Lighte6574242016-08-17 09:56:24 -07001053 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001054 delete env;
1055 return OK;
1056 }
1057
1058 static jvmtiError SetEnvironmentLocalStorage(jvmtiEnv* env, const void* data) {
Alex Lighte6574242016-08-17 09:56:24 -07001059 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001060 reinterpret_cast<ArtJvmTiEnv*>(env)->local_data = const_cast<void*>(data);
1061 return OK;
1062 }
1063
1064 static jvmtiError GetEnvironmentLocalStorage(jvmtiEnv* env, void** data_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001065 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001066 *data_ptr = reinterpret_cast<ArtJvmTiEnv*>(env)->local_data;
1067 return OK;
1068 }
1069
1070 static jvmtiError GetVersionNumber(jvmtiEnv* env, jint* version_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001071 ENSURE_VALID_ENV(env);
Alex Light49948e92016-08-11 15:35:28 -07001072 *version_ptr = JVMTI_VERSION;
1073 return OK;
1074 }
1075
1076 static jvmtiError GetErrorName(jvmtiEnv* env, jvmtiError error, char** name_ptr) {
Alex Lighte6574242016-08-17 09:56:24 -07001077 ENSURE_NON_NULL(name_ptr);
Alex Light49948e92016-08-11 15:35:28 -07001078 switch (error) {
1079#define ERROR_CASE(e) case (JVMTI_ERROR_ ## e) : do { \
Alex Light41960712017-01-06 14:44:23 -08001080 jvmtiError res = CopyString(env, \
1081 "JVMTI_ERROR_"#e, \
1082 reinterpret_cast<unsigned char**>(name_ptr)); \
1083 if (res != OK) { \
1084 *name_ptr = nullptr; \
1085 return res; \
1086 } else { \
1087 return OK; \
1088 } \
Alex Light49948e92016-08-11 15:35:28 -07001089 } while (false)
1090 ERROR_CASE(NONE);
1091 ERROR_CASE(INVALID_THREAD);
1092 ERROR_CASE(INVALID_THREAD_GROUP);
1093 ERROR_CASE(INVALID_PRIORITY);
1094 ERROR_CASE(THREAD_NOT_SUSPENDED);
1095 ERROR_CASE(THREAD_NOT_ALIVE);
1096 ERROR_CASE(INVALID_OBJECT);
1097 ERROR_CASE(INVALID_CLASS);
1098 ERROR_CASE(CLASS_NOT_PREPARED);
1099 ERROR_CASE(INVALID_METHODID);
1100 ERROR_CASE(INVALID_LOCATION);
1101 ERROR_CASE(INVALID_FIELDID);
1102 ERROR_CASE(NO_MORE_FRAMES);
1103 ERROR_CASE(OPAQUE_FRAME);
1104 ERROR_CASE(TYPE_MISMATCH);
1105 ERROR_CASE(INVALID_SLOT);
1106 ERROR_CASE(DUPLICATE);
1107 ERROR_CASE(NOT_FOUND);
1108 ERROR_CASE(INVALID_MONITOR);
1109 ERROR_CASE(NOT_MONITOR_OWNER);
1110 ERROR_CASE(INTERRUPT);
1111 ERROR_CASE(INVALID_CLASS_FORMAT);
1112 ERROR_CASE(CIRCULAR_CLASS_DEFINITION);
1113 ERROR_CASE(FAILS_VERIFICATION);
1114 ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_ADDED);
1115 ERROR_CASE(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED);
1116 ERROR_CASE(INVALID_TYPESTATE);
1117 ERROR_CASE(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED);
1118 ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_DELETED);
1119 ERROR_CASE(UNSUPPORTED_VERSION);
1120 ERROR_CASE(NAMES_DONT_MATCH);
1121 ERROR_CASE(UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED);
1122 ERROR_CASE(UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED);
1123 ERROR_CASE(UNMODIFIABLE_CLASS);
1124 ERROR_CASE(NOT_AVAILABLE);
1125 ERROR_CASE(MUST_POSSESS_CAPABILITY);
1126 ERROR_CASE(NULL_POINTER);
1127 ERROR_CASE(ABSENT_INFORMATION);
1128 ERROR_CASE(INVALID_EVENT_TYPE);
1129 ERROR_CASE(ILLEGAL_ARGUMENT);
1130 ERROR_CASE(NATIVE_METHOD);
1131 ERROR_CASE(CLASS_LOADER_UNSUPPORTED);
1132 ERROR_CASE(OUT_OF_MEMORY);
1133 ERROR_CASE(ACCESS_DENIED);
1134 ERROR_CASE(WRONG_PHASE);
1135 ERROR_CASE(INTERNAL);
1136 ERROR_CASE(UNATTACHED_THREAD);
1137 ERROR_CASE(INVALID_ENVIRONMENT);
1138#undef ERROR_CASE
1139 default: {
Alex Light41960712017-01-06 14:44:23 -08001140 jvmtiError res = CopyString(env,
1141 "JVMTI_ERROR_UNKNOWN",
1142 reinterpret_cast<unsigned char**>(name_ptr));
1143 if (res != OK) {
1144 *name_ptr = nullptr;
1145 return res;
1146 } else {
1147 return ERR(ILLEGAL_ARGUMENT);
1148 }
Alex Light49948e92016-08-11 15:35:28 -07001149 }
1150 }
1151 }
1152
1153 static jvmtiError SetVerboseFlag(jvmtiEnv* env, jvmtiVerboseFlag flag, jboolean value) {
1154 return ERR(NOT_IMPLEMENTED);
1155 }
1156
1157 static jvmtiError GetJLocationFormat(jvmtiEnv* env, jvmtiJlocationFormat* format_ptr) {
1158 return ERR(NOT_IMPLEMENTED);
1159 }
Alex Light9c20a142016-08-23 15:05:12 -07001160
1161 // TODO Remove this once events are working.
1162 static jvmtiError RetransformClassWithHook(jvmtiEnv* env,
1163 jclass klass,
1164 jvmtiEventClassFileLoadHook hook) {
1165 std::vector<jclass> classes;
1166 classes.push_back(klass);
1167 return RetransformClassesWithHook(reinterpret_cast<ArtJvmTiEnv*>(env), classes, hook);
1168 }
1169
Alex Light1e07ca62016-12-02 11:40:56 -08001170 static jvmtiError RedefineClassDirect(ArtJvmTiEnv* env,
1171 jclass klass,
1172 jint dex_size,
1173 unsigned char* dex_file) {
1174 if (!IsValidEnv(env)) {
1175 return ERR(INVALID_ENVIRONMENT);
1176 }
1177 jvmtiError ret = OK;
1178 std::string location;
1179 if ((ret = GetClassLocation(env, klass, &location)) != OK) {
1180 // TODO Do something more here? Maybe give log statements?
1181 return ret;
1182 }
1183 std::string error;
1184 ret = Redefiner::RedefineClass(env,
1185 art::Runtime::Current(),
1186 art::Thread::Current(),
1187 klass,
1188 location,
1189 dex_size,
1190 reinterpret_cast<uint8_t*>(dex_file),
1191 &error);
1192 if (ret != OK) {
Alex Light10f02fb2017-01-06 16:21:48 -08001193 LOG(WARNING) << "FAILURE TO REDEFINE " << error;
Alex Light1e07ca62016-12-02 11:40:56 -08001194 }
1195 return ret;
1196 }
1197
Alex Light9c20a142016-08-23 15:05:12 -07001198 // TODO This will be called by the event handler for the art::ti Event Load Event
1199 static jvmtiError RetransformClassesWithHook(ArtJvmTiEnv* env,
1200 const std::vector<jclass>& classes,
1201 jvmtiEventClassFileLoadHook hook) {
1202 if (!IsValidEnv(env)) {
1203 return ERR(INVALID_ENVIRONMENT);
1204 }
Alex Lighta01de592016-11-15 10:43:06 -08001205 jvmtiError res = OK;
1206 std::string error;
Alex Light9c20a142016-08-23 15:05:12 -07001207 for (jclass klass : classes) {
1208 JNIEnv* jni_env = nullptr;
1209 jobject loader = nullptr;
1210 std::string name;
1211 jobject protection_domain = nullptr;
1212 jint data_len = 0;
1213 unsigned char* dex_data = nullptr;
1214 jvmtiError ret = OK;
1215 std::string location;
1216 if ((ret = GetTransformationData(env,
1217 klass,
1218 /*out*/&location,
1219 /*out*/&jni_env,
1220 /*out*/&loader,
1221 /*out*/&name,
1222 /*out*/&protection_domain,
1223 /*out*/&data_len,
1224 /*out*/&dex_data)) != OK) {
1225 // TODO Do something more here? Maybe give log statements?
1226 return ret;
1227 }
1228 jint new_data_len = 0;
1229 unsigned char* new_dex_data = nullptr;
1230 hook(env,
1231 jni_env,
1232 klass,
1233 loader,
1234 name.c_str(),
1235 protection_domain,
1236 data_len,
1237 dex_data,
1238 /*out*/&new_data_len,
1239 /*out*/&new_dex_data);
1240 // Check if anything actually changed.
1241 if ((new_data_len != 0 || new_dex_data != nullptr) && new_dex_data != dex_data) {
Alex Lighta01de592016-11-15 10:43:06 -08001242 res = Redefiner::RedefineClass(env,
1243 art::Runtime::Current(),
1244 art::Thread::Current(),
1245 klass,
1246 location,
1247 new_data_len,
1248 new_dex_data,
1249 &error);
Alex Light9c20a142016-08-23 15:05:12 -07001250 env->Deallocate(new_dex_data);
1251 }
1252 // Deallocate the old dex data.
1253 env->Deallocate(dex_data);
Alex Lighta01de592016-11-15 10:43:06 -08001254 if (res != OK) {
1255 LOG(ERROR) << "FAILURE TO REDEFINE " << error;
1256 return res;
1257 }
Alex Light9c20a142016-08-23 15:05:12 -07001258 }
1259 return OK;
1260 }
Alex Light49948e92016-08-11 15:35:28 -07001261};
1262
1263static bool IsJvmtiVersion(jint version) {
1264 return version == JVMTI_VERSION_1 ||
1265 version == JVMTI_VERSION_1_0 ||
1266 version == JVMTI_VERSION_1_1 ||
1267 version == JVMTI_VERSION_1_2 ||
1268 version == JVMTI_VERSION;
1269}
1270
1271// Creates a jvmtiEnv and returns it with the art::ti::Env that is associated with it. new_art_ti
1272// is a pointer to the uninitialized memory for an art::ti::Env.
1273static void CreateArtJvmTiEnv(art::JavaVMExt* vm, /*out*/void** new_jvmtiEnv) {
1274 struct ArtJvmTiEnv* env = new ArtJvmTiEnv(vm);
1275 *new_jvmtiEnv = env;
Andreas Gampe77708d92016-10-07 11:48:21 -07001276
1277 gEventHandler.RegisterArtJvmTiEnv(env);
Alex Light49948e92016-08-11 15:35:28 -07001278}
1279
1280// A hook that the runtime uses to allow plugins to handle GetEnv calls. It returns true and
1281// places the return value in 'env' if this library can handle the GetEnv request. Otherwise
1282// returns false and does not modify the 'env' pointer.
1283static jint GetEnvHandler(art::JavaVMExt* vm, /*out*/void** env, jint version) {
1284 if (IsJvmtiVersion(version)) {
1285 CreateArtJvmTiEnv(vm, env);
1286 return JNI_OK;
1287 } else {
1288 printf("version 0x%x is not valid!", version);
1289 return JNI_EVERSION;
1290 }
1291}
1292
1293// The plugin initialization function. This adds the jvmti environment.
1294extern "C" bool ArtPlugin_Initialize() {
Andreas Gampee08a2be2016-10-06 13:13:30 -07001295 art::Runtime* runtime = art::Runtime::Current();
1296 runtime->GetJavaVM()->AddEnvironmentHook(GetEnvHandler);
1297 runtime->AddSystemWeakHolder(&gObjectTagTable);
Alex Light49948e92016-08-11 15:35:28 -07001298 return true;
1299}
1300
1301// The actual struct holding all of the entrypoints into the jvmti interface.
1302const jvmtiInterface_1 gJvmtiInterface = {
Alex Light9c20a142016-08-23 15:05:12 -07001303 // SPECIAL FUNCTION: RetransformClassWithHook Is normally reserved1
1304 // TODO Remove once we have events working.
1305 reinterpret_cast<void*>(JvmtiFunctions::RetransformClassWithHook),
1306 // nullptr, // reserved1
Alex Light49948e92016-08-11 15:35:28 -07001307 JvmtiFunctions::SetEventNotificationMode,
Alex Light1e07ca62016-12-02 11:40:56 -08001308 // SPECIAL FUNCTION: RedefineClassDirect Is normally reserved3
1309 // TODO Remove once we have events working.
1310 reinterpret_cast<void*>(JvmtiFunctions::RedefineClassDirect),
1311 // nullptr, // reserved3
Alex Light49948e92016-08-11 15:35:28 -07001312 JvmtiFunctions::GetAllThreads,
1313 JvmtiFunctions::SuspendThread,
1314 JvmtiFunctions::ResumeThread,
1315 JvmtiFunctions::StopThread,
1316 JvmtiFunctions::InterruptThread,
1317 JvmtiFunctions::GetThreadInfo,
1318 JvmtiFunctions::GetOwnedMonitorInfo, // 10
1319 JvmtiFunctions::GetCurrentContendedMonitor,
1320 JvmtiFunctions::RunAgentThread,
1321 JvmtiFunctions::GetTopThreadGroups,
1322 JvmtiFunctions::GetThreadGroupInfo,
1323 JvmtiFunctions::GetThreadGroupChildren,
1324 JvmtiFunctions::GetFrameCount,
1325 JvmtiFunctions::GetThreadState,
1326 JvmtiFunctions::GetCurrentThread,
1327 JvmtiFunctions::GetFrameLocation,
1328 JvmtiFunctions::NotifyFramePop, // 20
1329 JvmtiFunctions::GetLocalObject,
1330 JvmtiFunctions::GetLocalInt,
1331 JvmtiFunctions::GetLocalLong,
1332 JvmtiFunctions::GetLocalFloat,
1333 JvmtiFunctions::GetLocalDouble,
1334 JvmtiFunctions::SetLocalObject,
1335 JvmtiFunctions::SetLocalInt,
1336 JvmtiFunctions::SetLocalLong,
1337 JvmtiFunctions::SetLocalFloat,
1338 JvmtiFunctions::SetLocalDouble, // 30
1339 JvmtiFunctions::CreateRawMonitor,
1340 JvmtiFunctions::DestroyRawMonitor,
1341 JvmtiFunctions::RawMonitorEnter,
1342 JvmtiFunctions::RawMonitorExit,
1343 JvmtiFunctions::RawMonitorWait,
1344 JvmtiFunctions::RawMonitorNotify,
1345 JvmtiFunctions::RawMonitorNotifyAll,
1346 JvmtiFunctions::SetBreakpoint,
1347 JvmtiFunctions::ClearBreakpoint,
1348 nullptr, // reserved40
1349 JvmtiFunctions::SetFieldAccessWatch,
1350 JvmtiFunctions::ClearFieldAccessWatch,
1351 JvmtiFunctions::SetFieldModificationWatch,
1352 JvmtiFunctions::ClearFieldModificationWatch,
1353 JvmtiFunctions::IsModifiableClass,
1354 JvmtiFunctions::Allocate,
1355 JvmtiFunctions::Deallocate,
1356 JvmtiFunctions::GetClassSignature,
1357 JvmtiFunctions::GetClassStatus,
1358 JvmtiFunctions::GetSourceFileName, // 50
1359 JvmtiFunctions::GetClassModifiers,
1360 JvmtiFunctions::GetClassMethods,
1361 JvmtiFunctions::GetClassFields,
1362 JvmtiFunctions::GetImplementedInterfaces,
1363 JvmtiFunctions::IsInterface,
1364 JvmtiFunctions::IsArrayClass,
1365 JvmtiFunctions::GetClassLoader,
1366 JvmtiFunctions::GetObjectHashCode,
1367 JvmtiFunctions::GetObjectMonitorUsage,
1368 JvmtiFunctions::GetFieldName, // 60
1369 JvmtiFunctions::GetFieldDeclaringClass,
1370 JvmtiFunctions::GetFieldModifiers,
1371 JvmtiFunctions::IsFieldSynthetic,
1372 JvmtiFunctions::GetMethodName,
1373 JvmtiFunctions::GetMethodDeclaringClass,
1374 JvmtiFunctions::GetMethodModifiers,
1375 nullptr, // reserved67
1376 JvmtiFunctions::GetMaxLocals,
1377 JvmtiFunctions::GetArgumentsSize,
1378 JvmtiFunctions::GetLineNumberTable, // 70
1379 JvmtiFunctions::GetMethodLocation,
1380 JvmtiFunctions::GetLocalVariableTable,
1381 JvmtiFunctions::SetNativeMethodPrefix,
1382 JvmtiFunctions::SetNativeMethodPrefixes,
1383 JvmtiFunctions::GetBytecodes,
1384 JvmtiFunctions::IsMethodNative,
1385 JvmtiFunctions::IsMethodSynthetic,
1386 JvmtiFunctions::GetLoadedClasses,
1387 JvmtiFunctions::GetClassLoaderClasses,
1388 JvmtiFunctions::PopFrame, // 80
1389 JvmtiFunctions::ForceEarlyReturnObject,
1390 JvmtiFunctions::ForceEarlyReturnInt,
1391 JvmtiFunctions::ForceEarlyReturnLong,
1392 JvmtiFunctions::ForceEarlyReturnFloat,
1393 JvmtiFunctions::ForceEarlyReturnDouble,
1394 JvmtiFunctions::ForceEarlyReturnVoid,
1395 JvmtiFunctions::RedefineClasses,
1396 JvmtiFunctions::GetVersionNumber,
1397 JvmtiFunctions::GetCapabilities,
1398 JvmtiFunctions::GetSourceDebugExtension, // 90
1399 JvmtiFunctions::IsMethodObsolete,
1400 JvmtiFunctions::SuspendThreadList,
1401 JvmtiFunctions::ResumeThreadList,
1402 nullptr, // reserved94
1403 nullptr, // reserved95
1404 nullptr, // reserved96
1405 nullptr, // reserved97
1406 nullptr, // reserved98
1407 nullptr, // reserved99
1408 JvmtiFunctions::GetAllStackTraces, // 100
1409 JvmtiFunctions::GetThreadListStackTraces,
1410 JvmtiFunctions::GetThreadLocalStorage,
1411 JvmtiFunctions::SetThreadLocalStorage,
1412 JvmtiFunctions::GetStackTrace,
1413 nullptr, // reserved105
1414 JvmtiFunctions::GetTag,
1415 JvmtiFunctions::SetTag,
1416 JvmtiFunctions::ForceGarbageCollection,
1417 JvmtiFunctions::IterateOverObjectsReachableFromObject,
1418 JvmtiFunctions::IterateOverReachableObjects, // 110
1419 JvmtiFunctions::IterateOverHeap,
1420 JvmtiFunctions::IterateOverInstancesOfClass,
1421 nullptr, // reserved113
1422 JvmtiFunctions::GetObjectsWithTags,
1423 JvmtiFunctions::FollowReferences,
1424 JvmtiFunctions::IterateThroughHeap,
1425 nullptr, // reserved117
1426 nullptr, // reserved118
1427 nullptr, // reserved119
1428 JvmtiFunctions::SetJNIFunctionTable, // 120
1429 JvmtiFunctions::GetJNIFunctionTable,
1430 JvmtiFunctions::SetEventCallbacks,
1431 JvmtiFunctions::GenerateEvents,
1432 JvmtiFunctions::GetExtensionFunctions,
1433 JvmtiFunctions::GetExtensionEvents,
1434 JvmtiFunctions::SetExtensionEventCallback,
1435 JvmtiFunctions::DisposeEnvironment,
1436 JvmtiFunctions::GetErrorName,
1437 JvmtiFunctions::GetJLocationFormat,
1438 JvmtiFunctions::GetSystemProperties, // 130
1439 JvmtiFunctions::GetSystemProperty,
1440 JvmtiFunctions::SetSystemProperty,
1441 JvmtiFunctions::GetPhase,
1442 JvmtiFunctions::GetCurrentThreadCpuTimerInfo,
1443 JvmtiFunctions::GetCurrentThreadCpuTime,
1444 JvmtiFunctions::GetThreadCpuTimerInfo,
1445 JvmtiFunctions::GetThreadCpuTime,
1446 JvmtiFunctions::GetTimerInfo,
1447 JvmtiFunctions::GetTime,
1448 JvmtiFunctions::GetPotentialCapabilities, // 140
1449 nullptr, // reserved141
1450 JvmtiFunctions::AddCapabilities,
1451 JvmtiFunctions::RelinquishCapabilities,
1452 JvmtiFunctions::GetAvailableProcessors,
1453 JvmtiFunctions::GetClassVersionNumbers,
1454 JvmtiFunctions::GetConstantPool,
1455 JvmtiFunctions::GetEnvironmentLocalStorage,
1456 JvmtiFunctions::SetEnvironmentLocalStorage,
1457 JvmtiFunctions::AddToBootstrapClassLoaderSearch,
1458 JvmtiFunctions::SetVerboseFlag, // 150
1459 JvmtiFunctions::AddToSystemClassLoaderSearch,
1460 JvmtiFunctions::RetransformClasses,
1461 JvmtiFunctions::GetOwnedMonitorStackDepthInfo,
1462 JvmtiFunctions::GetObjectSize,
1463 JvmtiFunctions::GetLocalInstance,
1464};
1465
1466}; // namespace openjdkjvmti