27 #include "jvmtifiles/jvmtiEnv.hpp"
28 #include "memory/resourceArea.hpp"
29 #include "oops/objArrayKlass.hpp"
30 #include "oops/objArrayOop.hpp"
31 #include "oops/oop.inline.hpp"
32 #include "prims/jvmtiEnvBase.hpp"
33 #include "prims/jvmtiEventController.inline.hpp"
34 #include "prims/jvmtiExtensions.hpp"
35 #include "prims/jvmtiImpl.hpp"
36 #include "prims/jvmtiManageCapabilities.hpp"
37 #include "prims/jvmtiTagMap.hpp"
38 #include "prims/jvmtiThreadState.inline.hpp"
39 #include "runtime/biasedLocking.hpp"
40 #include "runtime/deoptimization.hpp"
41 #include "runtime/interfaceSupport.hpp"
42 #include "runtime/jfieldIDWorkaround.hpp"
43 #include "runtime/objectMonitor.hpp"
44 #include "runtime/objectMonitor.inline.hpp"
45 #include "runtime/signature.hpp"
46 #include "runtime/thread.inline.hpp"
47 #include "runtime/vframe.hpp"
48 #include "runtime/vframe_hp.hpp"
49 #include "runtime/vmThread.hpp"
50 #include "runtime/vm_operations.hpp"
51
52 ///////////////////////////////////////////////////////////////
53 //
54 // JvmtiEnvBase
55 //
56
57 JvmtiEnvBase* JvmtiEnvBase::_head_environment = NULL;
58
59 bool JvmtiEnvBase::_globally_initialized = false;
60 volatile bool JvmtiEnvBase::_needs_clean_up = false;
61
62 jvmtiPhase JvmtiEnvBase::_phase = JVMTI_PHASE_PRIMORDIAL;
63
64 volatile int JvmtiEnvBase::_dying_thread_env_iteration_count = 0;
65
66 extern jvmtiInterface_1_ jvmti_Interface;
470 JvmtiEnvBase::set_event_callbacks(const jvmtiEventCallbacks* callbacks,
471 jint size_of_callbacks) {
472 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
473
474 size_t byte_cnt = sizeof(jvmtiEventCallbacks);
475
476 // clear in either case to be sure we got any gap between sizes
477 memset(&_event_callbacks, 0, byte_cnt);
478
479 // Now that JvmtiThreadState_lock is held, prevent a possible race condition where events
480 // are re-enabled by a call to set event callbacks where the DisposeEnvironment
481 // occurs after the boiler-plate environment check and before the lock is acquired.
482 if (callbacks != NULL && is_valid()) {
483 if (size_of_callbacks < (jint)byte_cnt) {
484 byte_cnt = size_of_callbacks;
485 }
486 memcpy(&_event_callbacks, callbacks, byte_cnt);
487 }
488 }
489
490 // Called from JVMTI entry points which perform stack walking. If the
491 // associated JavaThread is the current thread, then wait_for_suspend
492 // is not used. Otherwise, it determines if we should wait for the
493 // "other" thread to complete external suspension. (NOTE: in future
494 // releases the suspension mechanism should be reimplemented so this
495 // is not necessary.)
496 //
497 bool
498 JvmtiEnvBase::is_thread_fully_suspended(JavaThread* thr, bool wait_for_suspend, uint32_t *bits) {
499 // "other" threads require special handling
500 if (thr != JavaThread::current()) {
501 if (wait_for_suspend) {
502 // We are allowed to wait for the external suspend to complete
503 // so give the other thread a chance to get suspended.
504 if (!thr->wait_for_ext_suspend_completion(SuspendRetryCount,
505 SuspendRetryDelay, bits)) {
506 // didn't make it so let the caller know
507 return false;
508 }
509 }
510 // We aren't allowed to wait for the external suspend to complete
511 // so if the other thread isn't externally suspended we need to
512 // let the caller know.
513 else if (!thr->is_ext_suspend_completed_with_lock(bits)) {
514 return false;
515 }
516 }
517
518 return true;
519 }
520
521
522 // In the fullness of time, all users of the method should instead
523 // directly use allocate, besides being cleaner and faster, this will
524 // mean much better out of memory handling
525 unsigned char *
526 JvmtiEnvBase::jvmtiMalloc(jlong size) {
527 unsigned char* mem = NULL;
528 jvmtiError result = allocate(size, &mem);
529 assert(result == JVMTI_ERROR_NONE, "Allocate failed");
530 return mem;
531 }
532
533
534 //
535 // Threads
536 //
537
538 jobject *
539 JvmtiEnvBase::new_jobjectArray(int length, Handle *handles) {
540 if (length == 0) {
541 return NULL;
543
544 jobject *objArray = (jobject *) jvmtiMalloc(sizeof(jobject) * length);
545 NULL_CHECK(objArray, NULL);
546
547 for (int i=0; i<length; i++) {
548 objArray[i] = jni_reference(handles[i]);
549 }
550 return objArray;
551 }
552
553 jthread *
554 JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) {
555 return (jthread *) new_jobjectArray(length,handles);
556 }
557
558 jthreadGroup *
559 JvmtiEnvBase::new_jthreadGroupArray(int length, Handle *handles) {
560 return (jthreadGroup *) new_jobjectArray(length,handles);
561 }
562
563
564 JavaThread *
565 JvmtiEnvBase::get_JavaThread(jthread jni_thread) {
566 oop t = JNIHandles::resolve_external_guard(jni_thread);
567 if (t == NULL || !t->is_a(SystemDictionary::Thread_klass())) {
568 return NULL;
569 }
570 // The following returns NULL if the thread has not yet run or is in
571 // process of exiting
572 return java_lang_Thread::thread(t);
573 }
574
575
576 // return the vframe on the specified thread and depth, NULL if no such frame
577 vframe*
578 JvmtiEnvBase::vframeFor(JavaThread* java_thread, jint depth) {
579 if (!java_thread->has_last_Java_frame()) {
580 return NULL;
581 }
582 RegisterMap reg_map(java_thread);
583 vframe *vf = java_thread->last_java_vframe(®_map);
584 int d = 0;
585 while ((vf != NULL) && (d < depth)) {
586 vf = vf->java_sender();
587 d++;
588 }
589 return vf;
590 }
591
592
593 //
594 // utilities: JNI objects
595 //
653 if (mi->owner_is_scalar_replaced()) continue;
654
655 // see if owner of the monitor is our object
656 if (mi->owner() != NULL && mi->owner() == hobj()) {
657 ret++;
658 }
659 }
660 }
661 }
662 return ret;
663 }
664
665
666
667 jvmtiError
668 JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread, jobject *monitor_ptr) {
669 #ifdef ASSERT
670 uint32_t debug_bits = 0;
671 #endif
672 assert((SafepointSynchronize::is_at_safepoint() ||
673 is_thread_fully_suspended(java_thread, false, &debug_bits)),
674 "at safepoint or target thread is suspended");
675 oop obj = NULL;
676 ObjectMonitor *mon = java_thread->current_waiting_monitor();
677 if (mon == NULL) {
678 // thread is not doing an Object.wait() call
679 mon = java_thread->current_pending_monitor();
680 if (mon != NULL) {
681 // The thread is trying to enter() or raw_enter() an ObjectMonitor.
682 obj = (oop)mon->object();
683 // If obj == NULL, then ObjectMonitor is raw which doesn't count
684 // as contended for this API
685 }
686 // implied else: no contended ObjectMonitor
687 } else {
688 // thread is doing an Object.wait() call
689 obj = (oop)mon->object();
690 assert(obj != NULL, "Object.wait() should have an object");
691 }
692
693 if (obj == NULL) {
694 *monitor_ptr = NULL;
695 } else {
696 HandleMark hm;
697 Handle hobj(Thread::current(), obj);
698 *monitor_ptr = jni_reference(calling_thread, hobj);
699 }
700 return JVMTI_ERROR_NONE;
701 }
702
703
704 jvmtiError
705 JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
706 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
707 jvmtiError err = JVMTI_ERROR_NONE;
708 #ifdef ASSERT
709 uint32_t debug_bits = 0;
710 #endif
711 assert((SafepointSynchronize::is_at_safepoint() ||
712 is_thread_fully_suspended(java_thread, false, &debug_bits)),
713 "at safepoint or target thread is suspended");
714
715 if (java_thread->has_last_Java_frame()) {
716 ResourceMark rm;
717 HandleMark hm;
718 RegisterMap reg_map(java_thread);
719
720 int depth = 0;
721 for (javaVFrame *jvf = java_thread->last_java_vframe(®_map); jvf != NULL;
722 jvf = jvf->java_sender()) {
723 if (depth++ < MaxJavaStackTraceDepth) { // check for stack too deep
724 // add locked objects for this frame into list
725 err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
726 if (err != JVMTI_ERROR_NONE) {
727 return err;
728 }
729 }
730 }
731 }
732
814 if (err != JVMTI_ERROR_NONE) {
815 return err;
816 }
817 Handle hobj(Thread::current(), obj);
818 jmsdi->monitor = jni_reference(calling_thread, hobj);
819 jmsdi->stack_depth = stack_depth;
820 owned_monitors_list->append(jmsdi);
821 }
822
823 return err;
824 }
825
826 jvmtiError
827 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
828 jint start_depth, jint max_count,
829 jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
830 #ifdef ASSERT
831 uint32_t debug_bits = 0;
832 #endif
833 assert((SafepointSynchronize::is_at_safepoint() ||
834 is_thread_fully_suspended(java_thread, false, &debug_bits)),
835 "at safepoint or target thread is suspended");
836 int count = 0;
837 if (java_thread->has_last_Java_frame()) {
838 RegisterMap reg_map(java_thread);
839 Thread* current_thread = Thread::current();
840 ResourceMark rm(current_thread);
841 javaVFrame *jvf = java_thread->last_java_vframe(®_map);
842 HandleMark hm(current_thread);
843 if (start_depth != 0) {
844 if (start_depth > 0) {
845 for (int j = 0; j < start_depth && jvf != NULL; j++) {
846 jvf = jvf->java_sender();
847 }
848 if (jvf == NULL) {
849 // start_depth is deeper than the stack depth
850 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
851 }
852 } else { // start_depth < 0
853 // we are referencing the starting depth based on the oldest
854 // part of the stack.
897 }
898 *count_ptr = count;
899 return JVMTI_ERROR_NONE;
900 }
901
902 jvmtiError
903 JvmtiEnvBase::get_frame_count(JvmtiThreadState *state, jint *count_ptr) {
904 assert((state != NULL),
905 "JavaThread should create JvmtiThreadState before calling this method");
906 *count_ptr = state->count_frames();
907 return JVMTI_ERROR_NONE;
908 }
909
910 jvmtiError
911 JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth,
912 jmethodID* method_ptr, jlocation* location_ptr) {
913 #ifdef ASSERT
914 uint32_t debug_bits = 0;
915 #endif
916 assert((SafepointSynchronize::is_at_safepoint() ||
917 is_thread_fully_suspended(java_thread, false, &debug_bits)),
918 "at safepoint or target thread is suspended");
919 Thread* current_thread = Thread::current();
920 ResourceMark rm(current_thread);
921
922 vframe *vf = vframeFor(java_thread, depth);
923 if (vf == NULL) {
924 return JVMTI_ERROR_NO_MORE_FRAMES;
925 }
926
927 // vframeFor should return a java frame. If it doesn't
928 // it means we've got an internal error and we return the
929 // error in product mode. In debug mode we will instead
930 // attempt to cast the vframe to a javaVFrame and will
931 // cause an assertion/crash to allow further diagnosis.
932 #ifdef PRODUCT
933 if (!vf->is_java_frame()) {
934 return JVMTI_ERROR_INTERNAL;
935 }
936 #endif
937
959
960 // Check arguments
961 {
962 oop mirror = JNIHandles::resolve_external_guard(object);
963 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
964 NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
965
966 hobj = Handle(current_thread, mirror);
967 }
968
969 JavaThread *owning_thread = NULL;
970 ObjectMonitor *mon = NULL;
971 jvmtiMonitorUsage ret = {
972 NULL, 0, 0, NULL, 0, NULL
973 };
974
975 uint32_t debug_bits = 0;
976 // first derive the object's owner and entry_count (if any)
977 {
978 // Revoke any biases before querying the mark word
979 if (SafepointSynchronize::is_at_safepoint()) {
980 BiasedLocking::revoke_at_safepoint(hobj);
981 } else {
982 BiasedLocking::revoke_and_rebias(hobj, false, calling_thread);
983 }
984
985 address owner = NULL;
986 {
987 markOop mark = hobj()->mark();
988
989 if (!mark->has_monitor()) {
990 // this object has a lightweight monitor
991
992 if (mark->has_locker()) {
993 owner = (address)mark->locker(); // save the address of the Lock word
994 }
995 // implied else: no owner
996 } else {
997 // this object has a heavyweight monitor
998 mon = mark->monitor();
999
1000 // The owner field of a heavyweight monitor may be NULL for no
1001 // owner, a JavaThread * or it may still be the address of the
1002 // Lock word in a JavaThread's stack. A monitor can be inflated
1003 // by a non-owning JavaThread, but only the owning JavaThread
1004 // can change the owner field from the Lock word to the
1005 // JavaThread * and it may not have done that yet.
1006 owner = (address)mon->owner();
1007 }
1008 }
1009
1010 if (owner != NULL) {
1011 // This monitor is owned so we have to find the owning JavaThread.
1012 // Since owning_thread_from_monitor_owner() grabs a lock, GC can
1013 // move our object at this point. However, our owner value is safe
1014 // since it is either the Lock word on a stack or a JavaThread *.
1015 owning_thread = Threads::owning_thread_from_monitor_owner(owner, !at_safepoint);
1016 // Cannot assume (owning_thread != NULL) here because this function
1017 // may not have been called at a safepoint and the owning_thread
1018 // might not be suspended.
1019 if (owning_thread != NULL) {
1020 // The monitor's owner either has to be the current thread, at safepoint
1021 // or it has to be suspended. Any of these conditions will prevent both
1022 // contending and waiting threads from modifying the state of
1023 // the monitor.
1024 if (!at_safepoint && !JvmtiEnv::is_thread_fully_suspended(owning_thread, true, &debug_bits)) {
1025 // Don't worry! This return of JVMTI_ERROR_THREAD_NOT_SUSPENDED
1026 // will not make it back to the JVM/TI agent. The error code will
1027 // get intercepted in JvmtiEnv::GetObjectMonitorUsage() which
1028 // will retry the call via a VM_GetObjectMonitorUsage VM op.
1029 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1030 }
1031 HandleMark hm;
1032 Handle th(current_thread, owning_thread->threadObj());
1033 ret.owner = (jthread)jni_reference(calling_thread, th);
1034 }
1035 // implied else: no owner
1036 }
1037
1038 if (owning_thread != NULL) { // monitor is owned
1039 // The recursions field of a monitor does not reflect recursions
1040 // as lightweight locks before inflating the monitor are not included.
1041 // We have to count the number of recursive monitor entries the hard way.
1042 // We pass a handle to survive any GCs along the way.
1043 ResourceMark rm;
1044 ret.entry_count = count_locked_objects(owning_thread, hobj);
1045 }
1046 // implied else: entry_count == 0
1047 }
1048
1049 jint nWant = 0, nWait = 0;
1050 if (mon != NULL) {
1051 // this object has a heavyweight monitor
1052 nWant = mon->contentions(); // # of threads contending for monitor
1053 nWait = mon->waiters(); // # of threads in Object.wait()
1054 ret.waiter_count = nWant + nWait;
1055 ret.notify_waiter_count = nWait;
1056 } else {
1067 }
1068 err = allocate(ret.notify_waiter_count * sizeof(jthread *),
1069 (unsigned char**)&ret.notify_waiters);
1070 if (err != JVMTI_ERROR_NONE) {
1071 deallocate((unsigned char*)ret.waiters);
1072 return err;
1073 }
1074
1075 // now derive the rest of the fields
1076 if (mon != NULL) {
1077 // this object has a heavyweight monitor
1078
1079 // Number of waiters may actually be less than the waiter count.
1080 // So NULL out memory so that unused memory will be NULL.
1081 memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *));
1082 memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *));
1083
1084 if (ret.waiter_count > 0) {
1085 // we have contending and/or waiting threads
1086 HandleMark hm;
1087 if (nWant > 0) {
1088 // we have contending threads
1089 ResourceMark rm;
1090 // get_pending_threads returns only java thread so we do not need to
1091 // check for non java threads.
1092 GrowableArray<JavaThread*>* wantList = Threads::get_pending_threads(
1093 nWant, (address)mon, !at_safepoint);
1094 if (wantList->length() < nWant) {
1095 // robustness: the pending list has gotten smaller
1096 nWant = wantList->length();
1097 }
1098 for (int i = 0; i < nWant; i++) {
1099 JavaThread *pending_thread = wantList->at(i);
1100 // If the monitor has no owner, then a non-suspended contending
1101 // thread could potentially change the state of the monitor by
1102 // entering it. The JVM/TI spec doesn't allow this.
1103 if (owning_thread == NULL && !at_safepoint &
1104 !JvmtiEnv::is_thread_fully_suspended(pending_thread, true, &debug_bits)) {
1105 if (ret.owner != NULL) {
1106 destroy_jni_reference(calling_thread, ret.owner);
1107 }
1108 for (int j = 0; j < i; j++) {
1109 destroy_jni_reference(calling_thread, ret.waiters[j]);
1110 }
1111 deallocate((unsigned char*)ret.waiters);
1112 deallocate((unsigned char*)ret.notify_waiters);
1113 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1114 }
1115 Handle th(current_thread, pending_thread->threadObj());
1116 ret.waiters[i] = (jthread)jni_reference(calling_thread, th);
1117 }
1118 }
1119 if (nWait > 0) {
1120 // we have threads in Object.wait()
1121 int offset = nWant; // add after any contending threads
1122 ObjectWaiter *waiter = mon->first_waiter();
1123 for (int i = 0, j = 0; i < nWait; i++) {
1124 if (waiter == NULL) {
1125 // robustness: the waiting list has gotten smaller
1126 nWait = j;
1127 break;
1128 }
1129 Thread *t = mon->thread_of_waiter(waiter);
1130 if (t != NULL && t->is_Java_thread()) {
1131 JavaThread *wjava_thread = (JavaThread *)t;
1132 // If the thread was found on the ObjectWaiter list, then
1133 // it has not been notified. This thread can't change the
1134 // state of the monitor so it doesn't need to be suspended.
1135 Handle th(current_thread, wjava_thread->threadObj());
1136 ret.waiters[offset + j] = (jthread)jni_reference(calling_thread, th);
1137 ret.notify_waiters[j++] = (jthread)jni_reference(calling_thread, th);
1138 }
1139 waiter = mon->next_waiter(waiter);
1140 }
1141 }
1142 }
1143
1144 // Adjust count. nWant and nWait count values may be less than original.
1145 ret.waiter_count = nWant + nWait;
1146 ret.notify_waiter_count = nWait;
1147 } else {
1148 // this object has a lightweight monitor and we have nothing more
1149 // to do here because the defaults are just fine.
1150 }
1151
1152 // we don't update return parameter unless everything worked
1153 *info_ptr = ret;
1154
1155 return JVMTI_ERROR_NONE;
1156 }
1157
1158 ResourceTracker::ResourceTracker(JvmtiEnv* env) {
1159 _env = env;
1160 _allocations = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<unsigned char*>(20, true);
1161 _failed = false;
1162 }
1274 memcpy(si, &(sin->info), sizeof(jvmtiStackInfo));
1275 if (frames_size == 0) {
1276 si->frame_buffer = NULL;
1277 } else {
1278 memcpy(fi, sin->info.frame_buffer, frames_size);
1279 si->frame_buffer = fi; // point to the new allocated copy of the frames
1280 fi += frame_count;
1281 }
1282 }
1283 assert(si == _stack_info, "the last copied stack info must be the first record");
1284 assert((unsigned char *)fi == ((unsigned char *)_stack_info) + alloc_size,
1285 "the last copied frame info must be the last record");
1286 }
1287
1288
1289 void
1290 VM_GetThreadListStackTraces::doit() {
1291 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1292
1293 ResourceMark rm;
1294 for (int i = 0; i < _thread_count; ++i) {
1295 jthread jt = _thread_list[i];
1296 oop thread_oop = JNIHandles::resolve_external_guard(jt);
1297 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
1298 set_result(JVMTI_ERROR_INVALID_THREAD);
1299 return;
1300 }
1301 fill_frames(jt, java_lang_Thread::thread(thread_oop), thread_oop);
1302 }
1303 allocate_and_fill_stacks(_thread_count);
1304 }
1305
1306 void
1307 VM_GetAllStackTraces::doit() {
1308 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1309
1310 ResourceMark rm;
1311 _final_thread_count = 0;
1312 for (JavaThread *jt = Threads::first(); jt != NULL; jt = jt->next()) {
1313 oop thread_oop = jt->threadObj();
1314 if (thread_oop != NULL &&
1315 !jt->is_exiting() &&
1316 java_lang_Thread::is_alive(thread_oop) &&
1317 !jt->is_hidden_from_external_view()) {
1318 ++_final_thread_count;
1319 // Handle block of the calling thread is used to create local refs.
1320 fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
1321 jt, thread_oop);
1322 }
1323 }
1324 allocate_and_fill_stacks(_final_thread_count);
1325 }
1326
1327 // Verifies that the top frame is a java frame in an expected state.
1328 // Deoptimizes frame if needed.
1329 // Checks that the frame method signature matches the return type (tos).
1330 // HandleMark must be defined in the caller only.
1331 // It is to keep a ret_ob_h handle alive after return to the caller.
1332 jvmtiError
1387 // from the same method call instruction.
1388 // The ForceEarlyReturn forces return from method so the execution
1389 // continues at the bytecode following the method call.
1390
1391 // Threads_lock NOT held, java_thread not protected by lock
1392 // java_thread - pre-checked
1393
1394 jvmtiError
1395 JvmtiEnvBase::force_early_return(JavaThread* java_thread, jvalue value, TosState tos) {
1396 JavaThread* current_thread = JavaThread::current();
1397 HandleMark hm(current_thread);
1398 uint32_t debug_bits = 0;
1399
1400 // retrieve or create the state
1401 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1402 if (state == NULL) {
1403 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1404 }
1405
1406 // Check if java_thread is fully suspended
1407 if (!is_thread_fully_suspended(java_thread,
1408 true /* wait for suspend completion */,
1409 &debug_bits)) {
1410 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1411 }
1412
1413 // Check to see if a ForceEarlyReturn was already in progress
1414 if (state->is_earlyret_pending()) {
1415 // Probably possible for JVMTI clients to trigger this, but the
1416 // JPDA backend shouldn't allow this to happen
1417 return JVMTI_ERROR_INTERNAL;
1418 }
1419 {
1420 // The same as for PopFrame. Workaround bug:
1421 // 4812902: popFrame hangs if the method is waiting at a synchronize
1422 // Catch this condition and return an error to avoid hanging.
1423 // Now JVMTI spec allows an implementation to bail out with an opaque
1424 // frame error.
1425 OSThread* osThread = java_thread->osthread();
1426 if (osThread->get_state() == MONITOR_WAIT) {
1427 return JVMTI_ERROR_OPAQUE_FRAME;
1428 }
1429 }
1504
1505 // Iterate over all the modules loaded to the system.
1506 ClassLoaderDataGraph::modules_do(&do_module);
1507
1508 jint len = _tbl->length();
1509 guarantee(len > 0, "at least one module must be present");
1510
1511 jobject* array = (jobject*)env->jvmtiMalloc((jlong)(len * sizeof(jobject)));
1512 if (array == NULL) {
1513 return JVMTI_ERROR_OUT_OF_MEMORY;
1514 }
1515 for (jint idx = 0; idx < len; idx++) {
1516 array[idx] = JNIHandles::make_local(Thread::current(), _tbl->at(idx).resolve());
1517 }
1518 _tbl = NULL;
1519 *modules_ptr = array;
1520 *module_count_ptr = len;
1521 return JVMTI_ERROR_NONE;
1522 }
1523
|
27 #include "jvmtifiles/jvmtiEnv.hpp"
28 #include "memory/resourceArea.hpp"
29 #include "oops/objArrayKlass.hpp"
30 #include "oops/objArrayOop.hpp"
31 #include "oops/oop.inline.hpp"
32 #include "prims/jvmtiEnvBase.hpp"
33 #include "prims/jvmtiEventController.inline.hpp"
34 #include "prims/jvmtiExtensions.hpp"
35 #include "prims/jvmtiImpl.hpp"
36 #include "prims/jvmtiManageCapabilities.hpp"
37 #include "prims/jvmtiTagMap.hpp"
38 #include "prims/jvmtiThreadState.inline.hpp"
39 #include "runtime/biasedLocking.hpp"
40 #include "runtime/deoptimization.hpp"
41 #include "runtime/interfaceSupport.hpp"
42 #include "runtime/jfieldIDWorkaround.hpp"
43 #include "runtime/objectMonitor.hpp"
44 #include "runtime/objectMonitor.inline.hpp"
45 #include "runtime/signature.hpp"
46 #include "runtime/thread.inline.hpp"
47 #include "runtime/threadSMR.hpp"
48 #include "runtime/vframe.hpp"
49 #include "runtime/vframe_hp.hpp"
50 #include "runtime/vmThread.hpp"
51 #include "runtime/vm_operations.hpp"
52
53 ///////////////////////////////////////////////////////////////
54 //
55 // JvmtiEnvBase
56 //
57
58 JvmtiEnvBase* JvmtiEnvBase::_head_environment = NULL;
59
60 bool JvmtiEnvBase::_globally_initialized = false;
61 volatile bool JvmtiEnvBase::_needs_clean_up = false;
62
63 jvmtiPhase JvmtiEnvBase::_phase = JVMTI_PHASE_PRIMORDIAL;
64
65 volatile int JvmtiEnvBase::_dying_thread_env_iteration_count = 0;
66
67 extern jvmtiInterface_1_ jvmti_Interface;
471 JvmtiEnvBase::set_event_callbacks(const jvmtiEventCallbacks* callbacks,
472 jint size_of_callbacks) {
473 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
474
475 size_t byte_cnt = sizeof(jvmtiEventCallbacks);
476
477 // clear in either case to be sure we got any gap between sizes
478 memset(&_event_callbacks, 0, byte_cnt);
479
480 // Now that JvmtiThreadState_lock is held, prevent a possible race condition where events
481 // are re-enabled by a call to set event callbacks where the DisposeEnvironment
482 // occurs after the boiler-plate environment check and before the lock is acquired.
483 if (callbacks != NULL && is_valid()) {
484 if (size_of_callbacks < (jint)byte_cnt) {
485 byte_cnt = size_of_callbacks;
486 }
487 memcpy(&_event_callbacks, callbacks, byte_cnt);
488 }
489 }
490
491
492 // In the fullness of time, all users of the method should instead
493 // directly use allocate, besides being cleaner and faster, this will
494 // mean much better out of memory handling
495 unsigned char *
496 JvmtiEnvBase::jvmtiMalloc(jlong size) {
497 unsigned char* mem = NULL;
498 jvmtiError result = allocate(size, &mem);
499 assert(result == JVMTI_ERROR_NONE, "Allocate failed");
500 return mem;
501 }
502
503
504 //
505 // Threads
506 //
507
508 jobject *
509 JvmtiEnvBase::new_jobjectArray(int length, Handle *handles) {
510 if (length == 0) {
511 return NULL;
513
514 jobject *objArray = (jobject *) jvmtiMalloc(sizeof(jobject) * length);
515 NULL_CHECK(objArray, NULL);
516
517 for (int i=0; i<length; i++) {
518 objArray[i] = jni_reference(handles[i]);
519 }
520 return objArray;
521 }
522
523 jthread *
524 JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) {
525 return (jthread *) new_jobjectArray(length,handles);
526 }
527
528 jthreadGroup *
529 JvmtiEnvBase::new_jthreadGroupArray(int length, Handle *handles) {
530 return (jthreadGroup *) new_jobjectArray(length,handles);
531 }
532
533 // return the vframe on the specified thread and depth, NULL if no such frame
534 vframe*
535 JvmtiEnvBase::vframeFor(JavaThread* java_thread, jint depth) {
536 if (!java_thread->has_last_Java_frame()) {
537 return NULL;
538 }
539 RegisterMap reg_map(java_thread);
540 vframe *vf = java_thread->last_java_vframe(®_map);
541 int d = 0;
542 while ((vf != NULL) && (d < depth)) {
543 vf = vf->java_sender();
544 d++;
545 }
546 return vf;
547 }
548
549
550 //
551 // utilities: JNI objects
552 //
610 if (mi->owner_is_scalar_replaced()) continue;
611
612 // see if owner of the monitor is our object
613 if (mi->owner() != NULL && mi->owner() == hobj()) {
614 ret++;
615 }
616 }
617 }
618 }
619 return ret;
620 }
621
622
623
624 jvmtiError
625 JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread, jobject *monitor_ptr) {
626 #ifdef ASSERT
627 uint32_t debug_bits = 0;
628 #endif
629 assert((SafepointSynchronize::is_at_safepoint() ||
630 java_thread->is_thread_fully_suspended(false, &debug_bits)),
631 "at safepoint or target thread is suspended");
632 oop obj = NULL;
633 ObjectMonitor *mon = java_thread->current_waiting_monitor();
634 if (mon == NULL) {
635 // thread is not doing an Object.wait() call
636 mon = java_thread->current_pending_monitor();
637 if (mon != NULL) {
638 // The thread is trying to enter() or raw_enter() an ObjectMonitor.
639 obj = (oop)mon->object();
640 // If obj == NULL, then ObjectMonitor is raw which doesn't count
641 // as contended for this API
642 }
643 // implied else: no contended ObjectMonitor
644 } else {
645 // thread is doing an Object.wait() call
646 obj = (oop)mon->object();
647 assert(obj != NULL, "Object.wait() should have an object");
648 }
649
650 if (obj == NULL) {
651 *monitor_ptr = NULL;
652 } else {
653 HandleMark hm;
654 Handle hobj(Thread::current(), obj);
655 *monitor_ptr = jni_reference(calling_thread, hobj);
656 }
657 return JVMTI_ERROR_NONE;
658 }
659
660
661 jvmtiError
662 JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
663 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
664 jvmtiError err = JVMTI_ERROR_NONE;
665 #ifdef ASSERT
666 uint32_t debug_bits = 0;
667 #endif
668 assert((SafepointSynchronize::is_at_safepoint() ||
669 java_thread->is_thread_fully_suspended(false, &debug_bits)),
670 "at safepoint or target thread is suspended");
671
672 if (java_thread->has_last_Java_frame()) {
673 ResourceMark rm;
674 HandleMark hm;
675 RegisterMap reg_map(java_thread);
676
677 int depth = 0;
678 for (javaVFrame *jvf = java_thread->last_java_vframe(®_map); jvf != NULL;
679 jvf = jvf->java_sender()) {
680 if (depth++ < MaxJavaStackTraceDepth) { // check for stack too deep
681 // add locked objects for this frame into list
682 err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
683 if (err != JVMTI_ERROR_NONE) {
684 return err;
685 }
686 }
687 }
688 }
689
771 if (err != JVMTI_ERROR_NONE) {
772 return err;
773 }
774 Handle hobj(Thread::current(), obj);
775 jmsdi->monitor = jni_reference(calling_thread, hobj);
776 jmsdi->stack_depth = stack_depth;
777 owned_monitors_list->append(jmsdi);
778 }
779
780 return err;
781 }
782
783 jvmtiError
784 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
785 jint start_depth, jint max_count,
786 jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
787 #ifdef ASSERT
788 uint32_t debug_bits = 0;
789 #endif
790 assert((SafepointSynchronize::is_at_safepoint() ||
791 java_thread->is_thread_fully_suspended(false, &debug_bits)),
792 "at safepoint or target thread is suspended");
793 int count = 0;
794 if (java_thread->has_last_Java_frame()) {
795 RegisterMap reg_map(java_thread);
796 Thread* current_thread = Thread::current();
797 ResourceMark rm(current_thread);
798 javaVFrame *jvf = java_thread->last_java_vframe(®_map);
799 HandleMark hm(current_thread);
800 if (start_depth != 0) {
801 if (start_depth > 0) {
802 for (int j = 0; j < start_depth && jvf != NULL; j++) {
803 jvf = jvf->java_sender();
804 }
805 if (jvf == NULL) {
806 // start_depth is deeper than the stack depth
807 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
808 }
809 } else { // start_depth < 0
810 // we are referencing the starting depth based on the oldest
811 // part of the stack.
854 }
855 *count_ptr = count;
856 return JVMTI_ERROR_NONE;
857 }
858
859 jvmtiError
860 JvmtiEnvBase::get_frame_count(JvmtiThreadState *state, jint *count_ptr) {
861 assert((state != NULL),
862 "JavaThread should create JvmtiThreadState before calling this method");
863 *count_ptr = state->count_frames();
864 return JVMTI_ERROR_NONE;
865 }
866
867 jvmtiError
868 JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth,
869 jmethodID* method_ptr, jlocation* location_ptr) {
870 #ifdef ASSERT
871 uint32_t debug_bits = 0;
872 #endif
873 assert((SafepointSynchronize::is_at_safepoint() ||
874 java_thread->is_thread_fully_suspended(false, &debug_bits)),
875 "at safepoint or target thread is suspended");
876 Thread* current_thread = Thread::current();
877 ResourceMark rm(current_thread);
878
879 vframe *vf = vframeFor(java_thread, depth);
880 if (vf == NULL) {
881 return JVMTI_ERROR_NO_MORE_FRAMES;
882 }
883
884 // vframeFor should return a java frame. If it doesn't
885 // it means we've got an internal error and we return the
886 // error in product mode. In debug mode we will instead
887 // attempt to cast the vframe to a javaVFrame and will
888 // cause an assertion/crash to allow further diagnosis.
889 #ifdef PRODUCT
890 if (!vf->is_java_frame()) {
891 return JVMTI_ERROR_INTERNAL;
892 }
893 #endif
894
916
917 // Check arguments
918 {
919 oop mirror = JNIHandles::resolve_external_guard(object);
920 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
921 NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
922
923 hobj = Handle(current_thread, mirror);
924 }
925
926 JavaThread *owning_thread = NULL;
927 ObjectMonitor *mon = NULL;
928 jvmtiMonitorUsage ret = {
929 NULL, 0, 0, NULL, 0, NULL
930 };
931
932 uint32_t debug_bits = 0;
933 // first derive the object's owner and entry_count (if any)
934 {
935 // Revoke any biases before querying the mark word
936 if (at_safepoint) {
937 BiasedLocking::revoke_at_safepoint(hobj);
938 } else {
939 BiasedLocking::revoke_and_rebias(hobj, false, calling_thread);
940 }
941
942 address owner = NULL;
943 {
944 markOop mark = hobj()->mark();
945
946 if (!mark->has_monitor()) {
947 // this object has a lightweight monitor
948
949 if (mark->has_locker()) {
950 owner = (address)mark->locker(); // save the address of the Lock word
951 }
952 // implied else: no owner
953 } else {
954 // this object has a heavyweight monitor
955 mon = mark->monitor();
956
957 // The owner field of a heavyweight monitor may be NULL for no
958 // owner, a JavaThread * or it may still be the address of the
959 // Lock word in a JavaThread's stack. A monitor can be inflated
960 // by a non-owning JavaThread, but only the owning JavaThread
961 // can change the owner field from the Lock word to the
962 // JavaThread * and it may not have done that yet.
963 owner = (address)mon->owner();
964 }
965 }
966
967 if (owner != NULL) {
968 // Use current thread since function can be called from a
969 // JavaThread or the VMThread.
970 ThreadsListHandle tlh;
971 // This monitor is owned so we have to find the owning JavaThread.
972 owning_thread = Threads::owning_thread_from_monitor_owner(tlh.list(), owner);
973 // Cannot assume (owning_thread != NULL) here because this function
974 // may not have been called at a safepoint and the owning_thread
975 // might not be suspended.
976 if (owning_thread != NULL) {
977 // The monitor's owner either has to be the current thread, at safepoint
978 // or it has to be suspended. Any of these conditions will prevent both
979 // contending and waiting threads from modifying the state of
980 // the monitor.
981 if (!at_safepoint && !owning_thread->is_thread_fully_suspended(true, &debug_bits)) {
982 // Don't worry! This return of JVMTI_ERROR_THREAD_NOT_SUSPENDED
983 // will not make it back to the JVM/TI agent. The error code will
984 // get intercepted in JvmtiEnv::GetObjectMonitorUsage() which
985 // will retry the call via a VM_GetObjectMonitorUsage VM op.
986 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
987 }
988 HandleMark hm;
989 Handle th(current_thread, owning_thread->threadObj());
990 ret.owner = (jthread)jni_reference(calling_thread, th);
991 }
992 // implied else: no owner
993 } // ThreadsListHandle is destroyed here.
994
995 if (owning_thread != NULL) { // monitor is owned
996 // The recursions field of a monitor does not reflect recursions
997 // as lightweight locks before inflating the monitor are not included.
998 // We have to count the number of recursive monitor entries the hard way.
999 // We pass a handle to survive any GCs along the way.
1000 ResourceMark rm;
1001 ret.entry_count = count_locked_objects(owning_thread, hobj);
1002 }
1003 // implied else: entry_count == 0
1004 }
1005
1006 jint nWant = 0, nWait = 0;
1007 if (mon != NULL) {
1008 // this object has a heavyweight monitor
1009 nWant = mon->contentions(); // # of threads contending for monitor
1010 nWait = mon->waiters(); // # of threads in Object.wait()
1011 ret.waiter_count = nWant + nWait;
1012 ret.notify_waiter_count = nWait;
1013 } else {
1024 }
1025 err = allocate(ret.notify_waiter_count * sizeof(jthread *),
1026 (unsigned char**)&ret.notify_waiters);
1027 if (err != JVMTI_ERROR_NONE) {
1028 deallocate((unsigned char*)ret.waiters);
1029 return err;
1030 }
1031
1032 // now derive the rest of the fields
1033 if (mon != NULL) {
1034 // this object has a heavyweight monitor
1035
1036 // Number of waiters may actually be less than the waiter count.
1037 // So NULL out memory so that unused memory will be NULL.
1038 memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *));
1039 memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *));
1040
1041 if (ret.waiter_count > 0) {
1042 // we have contending and/or waiting threads
1043 HandleMark hm;
1044 // Use current thread since function can be called from a
1045 // JavaThread or the VMThread.
1046 ThreadsListHandle tlh;
1047 if (nWant > 0) {
1048 // we have contending threads
1049 ResourceMark rm;
1050 // get_pending_threads returns only java thread so we do not need to
1051 // check for non java threads.
1052 GrowableArray<JavaThread*>* wantList = Threads::get_pending_threads(tlh.list(), nWant, (address)mon);
1053 if (wantList->length() < nWant) {
1054 // robustness: the pending list has gotten smaller
1055 nWant = wantList->length();
1056 }
1057 for (int i = 0; i < nWant; i++) {
1058 JavaThread *pending_thread = wantList->at(i);
1059 // If the monitor has no owner, then a non-suspended contending
1060 // thread could potentially change the state of the monitor by
1061 // entering it. The JVM/TI spec doesn't allow this.
1062 if (owning_thread == NULL && !at_safepoint &
1063 !pending_thread->is_thread_fully_suspended(true, &debug_bits)) {
1064 if (ret.owner != NULL) {
1065 destroy_jni_reference(calling_thread, ret.owner);
1066 }
1067 for (int j = 0; j < i; j++) {
1068 destroy_jni_reference(calling_thread, ret.waiters[j]);
1069 }
1070 deallocate((unsigned char*)ret.waiters);
1071 deallocate((unsigned char*)ret.notify_waiters);
1072 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1073 }
1074 Handle th(current_thread, pending_thread->threadObj());
1075 ret.waiters[i] = (jthread)jni_reference(calling_thread, th);
1076 }
1077 }
1078 if (nWait > 0) {
1079 // we have threads in Object.wait()
1080 int offset = nWant; // add after any contending threads
1081 ObjectWaiter *waiter = mon->first_waiter();
1082 for (int i = 0, j = 0; i < nWait; i++) {
1083 if (waiter == NULL) {
1084 // robustness: the waiting list has gotten smaller
1085 nWait = j;
1086 break;
1087 }
1088 Thread *t = mon->thread_of_waiter(waiter);
1089 if (t != NULL && t->is_Java_thread()) {
1090 JavaThread *wjava_thread = (JavaThread *)t;
1091 // If the thread was found on the ObjectWaiter list, then
1092 // it has not been notified. This thread can't change the
1093 // state of the monitor so it doesn't need to be suspended.
1094 Handle th(current_thread, wjava_thread->threadObj());
1095 ret.waiters[offset + j] = (jthread)jni_reference(calling_thread, th);
1096 ret.notify_waiters[j++] = (jthread)jni_reference(calling_thread, th);
1097 }
1098 waiter = mon->next_waiter(waiter);
1099 }
1100 }
1101 } // ThreadsListHandle is destroyed here.
1102
1103 // Adjust count. nWant and nWait count values may be less than original.
1104 ret.waiter_count = nWant + nWait;
1105 ret.notify_waiter_count = nWait;
1106 } else {
1107 // this object has a lightweight monitor and we have nothing more
1108 // to do here because the defaults are just fine.
1109 }
1110
1111 // we don't update return parameter unless everything worked
1112 *info_ptr = ret;
1113
1114 return JVMTI_ERROR_NONE;
1115 }
1116
1117 ResourceTracker::ResourceTracker(JvmtiEnv* env) {
1118 _env = env;
1119 _allocations = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<unsigned char*>(20, true);
1120 _failed = false;
1121 }
1233 memcpy(si, &(sin->info), sizeof(jvmtiStackInfo));
1234 if (frames_size == 0) {
1235 si->frame_buffer = NULL;
1236 } else {
1237 memcpy(fi, sin->info.frame_buffer, frames_size);
1238 si->frame_buffer = fi; // point to the new allocated copy of the frames
1239 fi += frame_count;
1240 }
1241 }
1242 assert(si == _stack_info, "the last copied stack info must be the first record");
1243 assert((unsigned char *)fi == ((unsigned char *)_stack_info) + alloc_size,
1244 "the last copied frame info must be the last record");
1245 }
1246
1247
1248 void
1249 VM_GetThreadListStackTraces::doit() {
1250 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1251
1252 ResourceMark rm;
1253 ThreadsListHandle tlh;
1254 for (int i = 0; i < _thread_count; ++i) {
1255 jthread jt = _thread_list[i];
1256 JavaThread* java_thread = NULL;
1257 oop thread_oop = NULL;
1258 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), jt, &java_thread, &thread_oop);
1259 if (err != JVMTI_ERROR_NONE) {
1260 // We got an error code so we don't have a JavaThread *, but
1261 // only return an error from here if we didn't get a valid
1262 // thread_oop.
1263 if (thread_oop == NULL) {
1264 set_result(err);
1265 return;
1266 }
1267 // We have a valid thread_oop.
1268 }
1269 fill_frames(jt, java_thread, thread_oop);
1270 }
1271 allocate_and_fill_stacks(_thread_count);
1272 }
1273
1274 void
1275 VM_GetAllStackTraces::doit() {
1276 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1277
1278 ResourceMark rm;
1279 _final_thread_count = 0;
1280 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1281 oop thread_oop = jt->threadObj();
1282 if (thread_oop != NULL &&
1283 !jt->is_exiting() &&
1284 java_lang_Thread::is_alive(thread_oop) &&
1285 !jt->is_hidden_from_external_view()) {
1286 ++_final_thread_count;
1287 // Handle block of the calling thread is used to create local refs.
1288 fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
1289 jt, thread_oop);
1290 }
1291 }
1292 allocate_and_fill_stacks(_final_thread_count);
1293 }
1294
1295 // Verifies that the top frame is a java frame in an expected state.
1296 // Deoptimizes frame if needed.
1297 // Checks that the frame method signature matches the return type (tos).
1298 // HandleMark must be defined in the caller only.
1299 // It is to keep a ret_ob_h handle alive after return to the caller.
1300 jvmtiError
1355 // from the same method call instruction.
1356 // The ForceEarlyReturn forces return from method so the execution
1357 // continues at the bytecode following the method call.
1358
1359 // Threads_lock NOT held, java_thread not protected by lock
1360 // java_thread - pre-checked
1361
1362 jvmtiError
1363 JvmtiEnvBase::force_early_return(JavaThread* java_thread, jvalue value, TosState tos) {
1364 JavaThread* current_thread = JavaThread::current();
1365 HandleMark hm(current_thread);
1366 uint32_t debug_bits = 0;
1367
1368 // retrieve or create the state
1369 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1370 if (state == NULL) {
1371 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1372 }
1373
1374 // Check if java_thread is fully suspended
1375 if (!java_thread->is_thread_fully_suspended(true /* wait for suspend completion */, &debug_bits)) {
1376 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1377 }
1378
1379 // Check to see if a ForceEarlyReturn was already in progress
1380 if (state->is_earlyret_pending()) {
1381 // Probably possible for JVMTI clients to trigger this, but the
1382 // JPDA backend shouldn't allow this to happen
1383 return JVMTI_ERROR_INTERNAL;
1384 }
1385 {
1386 // The same as for PopFrame. Workaround bug:
1387 // 4812902: popFrame hangs if the method is waiting at a synchronize
1388 // Catch this condition and return an error to avoid hanging.
1389 // Now JVMTI spec allows an implementation to bail out with an opaque
1390 // frame error.
1391 OSThread* osThread = java_thread->osthread();
1392 if (osThread->get_state() == MONITOR_WAIT) {
1393 return JVMTI_ERROR_OPAQUE_FRAME;
1394 }
1395 }
1470
1471 // Iterate over all the modules loaded to the system.
1472 ClassLoaderDataGraph::modules_do(&do_module);
1473
1474 jint len = _tbl->length();
1475 guarantee(len > 0, "at least one module must be present");
1476
1477 jobject* array = (jobject*)env->jvmtiMalloc((jlong)(len * sizeof(jobject)));
1478 if (array == NULL) {
1479 return JVMTI_ERROR_OUT_OF_MEMORY;
1480 }
1481 for (jint idx = 0; idx < len; idx++) {
1482 array[idx] = JNIHandles::make_local(Thread::current(), _tbl->at(idx).resolve());
1483 }
1484 _tbl = NULL;
1485 *modules_ptr = array;
1486 *module_count_ptr = len;
1487 return JVMTI_ERROR_NONE;
1488 }
1489
1490 void
1491 VM_UpdateForPopTopFrame::doit() {
1492 JavaThread* jt = _state->get_thread();
1493 ThreadsListHandle tlh;
1494 if (jt != NULL && tlh.includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
1495 _state->update_for_pop_top_frame();
1496 } else {
1497 _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1498 }
1499 }
1500
1501 void
1502 VM_SetFramePop::doit() {
1503 JavaThread* jt = _state->get_thread();
1504 ThreadsListHandle tlh;
1505 if (jt != NULL && tlh.includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
1506 int frame_number = _state->count_frames() - _depth;
1507 _state->env_thread_state((JvmtiEnvBase*)_env)->set_frame_pop(frame_number);
1508 } else {
1509 _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1510 }
1511 }
1512
1513 void
1514 VM_GetOwnedMonitorInfo::doit() {
1515 _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1516 ThreadsListHandle tlh;
1517 if (_java_thread != NULL && tlh.includes(_java_thread)
1518 && !_java_thread->is_exiting() && _java_thread->threadObj() != NULL) {
1519 _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread, _java_thread,
1520 _owned_monitors_list);
1521 }
1522 }
1523
1524 void
1525 VM_GetCurrentContendedMonitor::doit() {
1526 _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1527 ThreadsListHandle tlh;
1528 if (_java_thread != NULL && tlh.includes(_java_thread)
1529 && !_java_thread->is_exiting() && _java_thread->threadObj() != NULL) {
1530 _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,_java_thread,_owned_monitor_ptr);
1531 }
1532 }
1533
1534 void
1535 VM_GetStackTrace::doit() {
1536 _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1537 ThreadsListHandle tlh;
1538 if (_java_thread != NULL && tlh.includes(_java_thread)
1539 && !_java_thread->is_exiting() && _java_thread->threadObj() != NULL) {
1540 _result = ((JvmtiEnvBase *)_env)->get_stack_trace(_java_thread,
1541 _start_depth, _max_count,
1542 _frame_buffer, _count_ptr);
1543 }
1544 }
1545
1546 void
1547 VM_GetFrameCount::doit() {
1548 _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1549 JavaThread* jt = _state->get_thread();
1550 ThreadsListHandle tlh;
1551 if (jt != NULL && tlh.includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
1552 _result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr);
1553 }
1554 }
1555
1556 void
1557 VM_GetFrameLocation::doit() {
1558 _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
1559 ThreadsListHandle tlh;
1560 if (_java_thread != NULL && tlh.includes(_java_thread)
1561 && !_java_thread->is_exiting() && _java_thread->threadObj() != NULL) {
1562 _result = ((JvmtiEnvBase*)_env)->get_frame_location(_java_thread, _depth,
1563 _method_ptr, _location_ptr);
1564 }
1565 }
|