1014 // tries to access nonexistant services.
1015 if (on) {
1016 vm_exit_during_initialization("Java Kernel does not support JVMTI.");
1017 }
1018 }
1019
1020
1021 static inline Klass* oop_to_klass(oop obj) {
1022 Klass* k = obj->klass();
1023
1024 // if the object is a java.lang.Class then return the java mirror
1025 if (k == SystemDictionary::Class_klass()) {
1026 if (!java_lang_Class::is_primitive(obj)) {
1027 k = java_lang_Class::as_Klass(obj);
1028 assert(k != NULL, "class for non-primitive mirror must exist");
1029 }
1030 }
1031 return k;
1032 }
1033
1034 class JvmtiVMObjectAllocEventMark : public JvmtiClassEventMark {
1035 private:
1036 jobject _jobj;
1037 jlong _size;
1038 public:
1039 JvmtiVMObjectAllocEventMark(JavaThread *thread, oop obj) : JvmtiClassEventMark(thread, oop_to_klass(obj)) {
1040 _jobj = (jobject)to_jobject(obj);
1041 _size = obj->size() * wordSize;
1042 };
1043 jobject jni_jobject() { return _jobj; }
1044 jlong size() { return _size; }
1045 };
1046
1047 class JvmtiCompiledMethodLoadEventMark : public JvmtiMethodEventMark {
1048 private:
1049 jint _code_size;
1050 const void *_code_data;
1051 jint _map_length;
1052 jvmtiAddrLocationMap *_map;
1053 const void *_compile_info;
1054 public:
1055 JvmtiCompiledMethodLoadEventMark(JavaThread *thread, nmethod *nm, void* compile_info_ptr = NULL)
1056 : JvmtiMethodEventMark(thread,methodHandle(thread, nm->method())) {
1057 _code_data = nm->insts_begin();
1058 _code_size = nm->insts_size();
1059 _compile_info = compile_info_ptr; // Set void pointer of compiledMethodLoad Event. Default value is NULL.
1184 bool JvmtiExport::_should_post_field_modification = false;
1185 bool JvmtiExport::_should_post_class_load = false;
1186 bool JvmtiExport::_should_post_class_prepare = false;
1187 bool JvmtiExport::_should_post_class_unload = false;
1188 bool JvmtiExport::_should_post_thread_life = false;
1189 bool JvmtiExport::_should_clean_up_heap_objects = false;
1190 bool JvmtiExport::_should_post_native_method_bind = false;
1191 bool JvmtiExport::_should_post_dynamic_code_generated = false;
1192 bool JvmtiExport::_should_post_data_dump = false;
1193 bool JvmtiExport::_should_post_compiled_method_load = false;
1194 bool JvmtiExport::_should_post_compiled_method_unload = false;
1195 bool JvmtiExport::_should_post_monitor_contended_enter = false;
1196 bool JvmtiExport::_should_post_monitor_contended_entered = false;
1197 bool JvmtiExport::_should_post_monitor_wait = false;
1198 bool JvmtiExport::_should_post_monitor_waited = false;
1199 bool JvmtiExport::_should_post_garbage_collection_start = false;
1200 bool JvmtiExport::_should_post_garbage_collection_finish = false;
1201 bool JvmtiExport::_should_post_object_free = false;
1202 bool JvmtiExport::_should_post_resource_exhausted = false;
1203 bool JvmtiExport::_should_post_vm_object_alloc = false;
1204 bool JvmtiExport::_should_post_on_exceptions = false;
1205
1206 ////////////////////////////////////////////////////////////////////////////////////////////////
1207
1208
1209 //
1210 // JVMTI single step management
1211 //
1212 void JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, address location) {
1213 assert(JvmtiExport::should_post_single_step(), "must be single stepping");
1214
1215 HandleMark hm(thread);
1216 methodHandle mh(thread, method);
1217
1218 // update information about current location and post a step event
1219 JvmtiThreadState *state = thread->jvmti_thread_state();
1220 if (state == NULL) {
1221 return;
1222 }
1223 EVT_TRIG_TRACE(JVMTI_EVENT_SINGLE_STEP, ("[%s] Trg Single Step triggered",
2281 // Can not take safepoint here.
2282 NoSafepointVerifier no_sfpt;
2283 // Can not take safepoint here so can not use state_for to get
2284 // jvmti thread state.
2285 JvmtiThreadState *state = ((JavaThread*)thread)->jvmti_thread_state();
2286 if (state != NULL ) {
2287 // state is non NULL when VMObjectAllocEventCollector is enabled.
2288 JvmtiVMObjectAllocEventCollector *collector;
2289 collector = state->get_vm_object_alloc_event_collector();
2290 if (collector != NULL && collector->is_enabled()) {
2291 // Don't record classes as these will be notified via the ClassLoad
2292 // event.
2293 if (obj->klass() != SystemDictionary::Class_klass()) {
2294 collector->record_allocation(obj);
2295 }
2296 }
2297 }
2298 }
2299 }
2300
2301 void JvmtiExport::post_garbage_collection_finish() {
2302 Thread *thread = Thread::current(); // this event is posted from VM-Thread.
2303 EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2304 ("[%s] garbage collection finish event triggered",
2305 JvmtiTrace::safe_get_thread_name(thread)));
2306 JvmtiEnvIterator it;
2307 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2308 if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH)) {
2309 EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2310 ("[%s] garbage collection finish event sent",
2311 JvmtiTrace::safe_get_thread_name(thread)));
2312 JvmtiThreadEventTransition jet(thread);
2313 // JNIEnv is NULL here because this event is posted from VM Thread
2314 jvmtiEventGarbageCollectionFinish callback = env->callbacks()->GarbageCollectionFinish;
2315 if (callback != NULL) {
2316 (*callback)(env->jvmti_external());
2317 }
2318 }
2319 }
2320 }
2482 JvmtiTrace::safe_get_thread_name(thread)));
2483
2484 JvmtiEnvThreadStateIterator it(state);
2485 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2486 if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAITED)) {
2487 EVT_TRACE(JVMTI_EVENT_MONITOR_WAITED,
2488 ("[%s] monitor waited event sent",
2489 JvmtiTrace::safe_get_thread_name(thread)));
2490 JvmtiMonitorEventMark jem(thread, h());
2491 JvmtiEnv *env = ets->get_env();
2492 JvmtiThreadEventTransition jet(thread);
2493 jvmtiEventMonitorWaited callback = env->callbacks()->MonitorWaited;
2494 if (callback != NULL) {
2495 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2496 jem.jni_object(), timed_out);
2497 }
2498 }
2499 }
2500 }
2501
2502
2503 void JvmtiExport::post_vm_object_alloc(JavaThread *thread, oop object) {
2504 EVT_TRIG_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Trg vm object alloc triggered",
2505 JvmtiTrace::safe_get_thread_name(thread)));
2506 if (object == NULL) {
2507 return;
2508 }
2509 HandleMark hm(thread);
2510 Handle h(thread, object);
2511 JvmtiEnvIterator it;
2512 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2513 if (env->is_enabled(JVMTI_EVENT_VM_OBJECT_ALLOC)) {
2514 EVT_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Evt vmobject alloc sent %s",
2515 JvmtiTrace::safe_get_thread_name(thread),
2516 object==NULL? "NULL" : object->klass()->external_name()));
2517
2518 JvmtiVMObjectAllocEventMark jem(thread, h());
2519 JvmtiJavaThreadEventTransition jet(thread);
2520 jvmtiEventVMObjectAlloc callback = env->callbacks()->VMObjectAlloc;
2521 if (callback != NULL) {
2522 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2523 jem.jni_jobject(), jem.jni_class(), jem.size());
2524 }
2525 }
2526 }
2527 }
2528
2529 ////////////////////////////////////////////////////////////////////////////////////////////////
2530
2531 void JvmtiExport::cleanup_thread(JavaThread* thread) {
2532 assert(JavaThread::current() == thread, "thread is not current");
2533 MutexLocker mu(JvmtiThreadState_lock);
2534
2535 if (thread->jvmti_thread_state() != NULL) {
2536 // This has to happen after the thread state is removed, which is
2537 // why it is not in post_thread_end_event like its complement
2538 // Maybe both these functions should be rolled into the posts?
2539 JvmtiEventController::thread_ended(thread);
2540 }
2541 }
2542
2543 void JvmtiExport::clear_detected_exception(JavaThread* thread) {
2544 assert(JavaThread::current() == thread, "thread is not current");
2545
2546 JvmtiThreadState* state = thread->jvmti_thread_state();
2547 if (state != NULL) {
2548 state->clear_exception_state();
2549 }
2550 }
2551
2552 void JvmtiExport::oops_do(OopClosure* f) {
2553 JvmtiCurrentBreakpoints::oops_do(f);
2554 JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(f);
2555 }
2556
2557 void JvmtiExport::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
2558 JvmtiTagMap::weak_oops_do(is_alive, f);
2559 }
2560
2561 void JvmtiExport::gc_epilogue() {
2562 JvmtiCurrentBreakpoints::gc_epilogue();
2563 }
2564
2565 // Onload raw monitor transition.
2566 void JvmtiExport::transition_pending_onload_raw_monitors() {
2567 JvmtiPendingMonitors::transition_raw_monitors();
2568 }
2569
2570 ////////////////////////////////////////////////////////////////////////////////////////////////
2571 #if INCLUDE_SERVICES
2572 // Attach is disabled if SERVICES is not included
2573
2574 // type for the Agent_OnAttach entry point
2667 }
2668 return result;
2669 }
2670
2671 #endif // INCLUDE_SERVICES
2672 ////////////////////////////////////////////////////////////////////////////////////////////////
2673
2674 // Setup current current thread for event collection.
2675 void JvmtiEventCollector::setup_jvmti_thread_state() {
2676 // set this event collector to be the current one.
2677 JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
2678 // state can only be NULL if the current thread is exiting which
2679 // should not happen since we're trying to configure for event collection
2680 guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state");
2681 if (is_vm_object_alloc_event()) {
2682 _prev = state->get_vm_object_alloc_event_collector();
2683 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this);
2684 } else if (is_dynamic_code_event()) {
2685 _prev = state->get_dynamic_code_event_collector();
2686 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this);
2687 }
2688 }
2689
2690 // Unset current event collection in this thread and reset it with previous
2691 // collector.
2692 void JvmtiEventCollector::unset_jvmti_thread_state() {
2693 JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state();
2694 if (state != NULL) {
2695 // restore the previous event collector (if any)
2696 if (is_vm_object_alloc_event()) {
2697 if (state->get_vm_object_alloc_event_collector() == this) {
2698 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev);
2699 } else {
2700 // this thread's jvmti state was created during the scope of
2701 // the event collector.
2702 }
2703 } else {
2704 if (is_dynamic_code_event()) {
2705 if (state->get_dynamic_code_event_collector() == this) {
2706 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev);
2707 } else {
2708 // this thread's jvmti state was created during the scope of
2709 // the event collector.
2710 }
2711 }
2712 }
2713 }
2714 }
2715
2716 // create the dynamic code event collector
2717 JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(NULL) {
2718 if (JvmtiExport::should_post_dynamic_code_generated()) {
2719 setup_jvmti_thread_state();
2720 }
2721 }
2722
2723 // iterate over any code blob descriptors collected and post a
2724 // DYNAMIC_CODE_GENERATED event to the profiler.
2725 JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() {
2726 assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events");
2727 // iterate over any code blob descriptors that we collected
2728 if (_code_blobs != NULL) {
2729 for (int i=0; i<_code_blobs->length(); i++) {
2730 JvmtiCodeBlobDesc* blob = _code_blobs->at(i);
2731 JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end());
2732 FreeHeap(blob);
2733 }
2734 delete _code_blobs;
2735 }
2736 unset_jvmti_thread_state();
2737 }
2738
2739 // register a stub
2740 void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) {
2741 if (_code_blobs == NULL) {
2742 _code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(1,true);
2743 }
2744 _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end));
2745 }
2746
2747 // Setup current thread to record vm allocated objects.
2748 JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() : _allocated(NULL) {
2749 if (JvmtiExport::should_post_vm_object_alloc()) {
2750 _enable = true;
2751 setup_jvmti_thread_state();
2752 } else {
2753 _enable = false;
2754 }
2755 }
2756
2757 // Post vm_object_alloc event for vm allocated objects visible to java
2758 // world.
2759 JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() {
2760 if (_allocated != NULL) {
2761 set_enabled(false);
2762 for (int i = 0; i < _allocated->length(); i++) {
2763 oop obj = _allocated->at(i);
2764 if (ServiceUtil::visible_oop(obj)) {
2765 JvmtiExport::post_vm_object_alloc(JavaThread::current(), obj);
2766 }
2767 }
2768 delete _allocated;
2769 }
2770 unset_jvmti_thread_state();
2771 }
2772
2773 void JvmtiVMObjectAllocEventCollector::record_allocation(oop obj) {
2774 assert(is_enabled(), "VM object alloc event collector is not enabled");
2775 if (_allocated == NULL) {
2776 _allocated = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(1, true);
2777 }
2778 _allocated->push(obj);
2779 }
2780
2781 // GC support.
2782 void JvmtiVMObjectAllocEventCollector::oops_do(OopClosure* f) {
2783 if (_allocated != NULL) {
2784 for(int i=_allocated->length() - 1; i >= 0; i--) {
2785 if (_allocated->at(i) != NULL) {
2786 f->do_oop(_allocated->adr_at(i));
2787 }
2788 }
2789 }
2790 }
2791
2792 void JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(OopClosure* f) {
2793 // no-op if jvmti not enabled
2794 if (!JvmtiEnv::environments_might_exist()) {
2795 return;
2796 }
2797
2798 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jthr = jtiwh.next(); ) {
2799 JvmtiThreadState *state = jthr->jvmti_thread_state();
2800 if (state != NULL) {
2801 JvmtiVMObjectAllocEventCollector *collector;
2802 collector = state->get_vm_object_alloc_event_collector();
2803 while (collector != NULL) {
2804 collector->oops_do(f);
2805 collector = (JvmtiVMObjectAllocEventCollector *)collector->get_prev();
2806 }
2807 }
2808 }
2809 }
2810
2811
2812 // Disable collection of VMObjectAlloc events
2813 NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) {
2814 // a no-op if VMObjectAlloc event is not enabled
2815 if (!JvmtiExport::should_post_vm_object_alloc()) {
2816 return;
2817 }
2818 Thread* thread = Thread::current_or_null();
2819 if (thread != NULL && thread->is_Java_thread()) {
2820 JavaThread* current_thread = (JavaThread*)thread;
2821 JvmtiThreadState *state = current_thread->jvmti_thread_state();
2822 if (state != NULL) {
2823 JvmtiVMObjectAllocEventCollector *collector;
2824 collector = state->get_vm_object_alloc_event_collector();
2825 if (collector != NULL && collector->is_enabled()) {
2826 _collector = collector;
2827 _collector->set_enabled(false);
2828 }
2829 }
2830 }
2831 }
2832
2833 // Re-Enable collection of VMObjectAlloc events (if previously enabled)
2834 NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() {
2835 if (was_enabled()) {
2836 _collector->set_enabled(true);
2837 }
2838 };
2839
2840 JvmtiGCMarker::JvmtiGCMarker() {
2841 // if there aren't any JVMTI environments then nothing to do
2842 if (!JvmtiEnv::environments_might_exist()) {
2843 return;
2844 }
2845
2846 if (JvmtiExport::should_post_garbage_collection_start()) {
2847 JvmtiExport::post_garbage_collection_start();
2848 }
2849
2850 if (SafepointSynchronize::is_at_safepoint()) {
2851 // Do clean up tasks that need to be done at a safepoint
2852 JvmtiEnvBase::check_for_periodic_clean_up();
2853 }
2854 }
2855
2856 JvmtiGCMarker::~JvmtiGCMarker() {
2857 // if there aren't any JVMTI environments then nothing to do
2858 if (!JvmtiEnv::environments_might_exist()) {
|
1014 // tries to access nonexistant services.
1015 if (on) {
1016 vm_exit_during_initialization("Java Kernel does not support JVMTI.");
1017 }
1018 }
1019
1020
1021 static inline Klass* oop_to_klass(oop obj) {
1022 Klass* k = obj->klass();
1023
1024 // if the object is a java.lang.Class then return the java mirror
1025 if (k == SystemDictionary::Class_klass()) {
1026 if (!java_lang_Class::is_primitive(obj)) {
1027 k = java_lang_Class::as_Klass(obj);
1028 assert(k != NULL, "class for non-primitive mirror must exist");
1029 }
1030 }
1031 return k;
1032 }
1033
1034 class JvmtiObjectAllocEventMark : public JvmtiClassEventMark {
1035 private:
1036 jobject _jobj;
1037 jlong _size;
1038 public:
1039 JvmtiObjectAllocEventMark(JavaThread *thread, oop obj) : JvmtiClassEventMark(thread, oop_to_klass(obj)) {
1040 _jobj = (jobject)to_jobject(obj);
1041 _size = obj->size() * wordSize;
1042 };
1043 jobject jni_jobject() { return _jobj; }
1044 jlong size() { return _size; }
1045 };
1046
1047 class JvmtiCompiledMethodLoadEventMark : public JvmtiMethodEventMark {
1048 private:
1049 jint _code_size;
1050 const void *_code_data;
1051 jint _map_length;
1052 jvmtiAddrLocationMap *_map;
1053 const void *_compile_info;
1054 public:
1055 JvmtiCompiledMethodLoadEventMark(JavaThread *thread, nmethod *nm, void* compile_info_ptr = NULL)
1056 : JvmtiMethodEventMark(thread,methodHandle(thread, nm->method())) {
1057 _code_data = nm->insts_begin();
1058 _code_size = nm->insts_size();
1059 _compile_info = compile_info_ptr; // Set void pointer of compiledMethodLoad Event. Default value is NULL.
1184 bool JvmtiExport::_should_post_field_modification = false;
1185 bool JvmtiExport::_should_post_class_load = false;
1186 bool JvmtiExport::_should_post_class_prepare = false;
1187 bool JvmtiExport::_should_post_class_unload = false;
1188 bool JvmtiExport::_should_post_thread_life = false;
1189 bool JvmtiExport::_should_clean_up_heap_objects = false;
1190 bool JvmtiExport::_should_post_native_method_bind = false;
1191 bool JvmtiExport::_should_post_dynamic_code_generated = false;
1192 bool JvmtiExport::_should_post_data_dump = false;
1193 bool JvmtiExport::_should_post_compiled_method_load = false;
1194 bool JvmtiExport::_should_post_compiled_method_unload = false;
1195 bool JvmtiExport::_should_post_monitor_contended_enter = false;
1196 bool JvmtiExport::_should_post_monitor_contended_entered = false;
1197 bool JvmtiExport::_should_post_monitor_wait = false;
1198 bool JvmtiExport::_should_post_monitor_waited = false;
1199 bool JvmtiExport::_should_post_garbage_collection_start = false;
1200 bool JvmtiExport::_should_post_garbage_collection_finish = false;
1201 bool JvmtiExport::_should_post_object_free = false;
1202 bool JvmtiExport::_should_post_resource_exhausted = false;
1203 bool JvmtiExport::_should_post_vm_object_alloc = false;
1204 bool JvmtiExport::_should_post_sampled_object_alloc = false;
1205 bool JvmtiExport::_should_post_on_exceptions = false;
1206
1207 ////////////////////////////////////////////////////////////////////////////////////////////////
1208
1209
1210 //
1211 // JVMTI single step management
1212 //
1213 void JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, address location) {
1214 assert(JvmtiExport::should_post_single_step(), "must be single stepping");
1215
1216 HandleMark hm(thread);
1217 methodHandle mh(thread, method);
1218
1219 // update information about current location and post a step event
1220 JvmtiThreadState *state = thread->jvmti_thread_state();
1221 if (state == NULL) {
1222 return;
1223 }
1224 EVT_TRIG_TRACE(JVMTI_EVENT_SINGLE_STEP, ("[%s] Trg Single Step triggered",
2282 // Can not take safepoint here.
2283 NoSafepointVerifier no_sfpt;
2284 // Can not take safepoint here so can not use state_for to get
2285 // jvmti thread state.
2286 JvmtiThreadState *state = ((JavaThread*)thread)->jvmti_thread_state();
2287 if (state != NULL ) {
2288 // state is non NULL when VMObjectAllocEventCollector is enabled.
2289 JvmtiVMObjectAllocEventCollector *collector;
2290 collector = state->get_vm_object_alloc_event_collector();
2291 if (collector != NULL && collector->is_enabled()) {
2292 // Don't record classes as these will be notified via the ClassLoad
2293 // event.
2294 if (obj->klass() != SystemDictionary::Class_klass()) {
2295 collector->record_allocation(obj);
2296 }
2297 }
2298 }
2299 }
2300 }
2301
2302 // Collect all the vm internally allocated objects which are visible to java world
2303 void JvmtiExport::record_sampled_internal_object_allocation(oop obj) {
2304 Thread* thread = Thread::current_or_null();
2305 if (thread != NULL && thread->is_Java_thread()) {
2306 // Can not take safepoint here.
2307 NoSafepointVerifier no_sfpt;
2308 // Can not take safepoint here so can not use state_for to get
2309 // jvmti thread state.
2310 JvmtiThreadState *state = ((JavaThread*)thread)->jvmti_thread_state();
2311 if (state != NULL ) {
2312 // state is non NULL when SampledObjectAllocEventCollector is enabled.
2313 JvmtiSampledObjectAllocEventCollector *collector;
2314 collector = state->get_sampled_object_alloc_event_collector();
2315 if (collector != NULL && collector->is_enabled()) {
2316 collector->record_allocation(obj);
2317 }
2318 }
2319 }
2320 }
2321
2322 void JvmtiExport::post_garbage_collection_finish() {
2323 Thread *thread = Thread::current(); // this event is posted from VM-Thread.
2324 EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2325 ("[%s] garbage collection finish event triggered",
2326 JvmtiTrace::safe_get_thread_name(thread)));
2327 JvmtiEnvIterator it;
2328 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2329 if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH)) {
2330 EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2331 ("[%s] garbage collection finish event sent",
2332 JvmtiTrace::safe_get_thread_name(thread)));
2333 JvmtiThreadEventTransition jet(thread);
2334 // JNIEnv is NULL here because this event is posted from VM Thread
2335 jvmtiEventGarbageCollectionFinish callback = env->callbacks()->GarbageCollectionFinish;
2336 if (callback != NULL) {
2337 (*callback)(env->jvmti_external());
2338 }
2339 }
2340 }
2341 }
2503 JvmtiTrace::safe_get_thread_name(thread)));
2504
2505 JvmtiEnvThreadStateIterator it(state);
2506 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2507 if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAITED)) {
2508 EVT_TRACE(JVMTI_EVENT_MONITOR_WAITED,
2509 ("[%s] monitor waited event sent",
2510 JvmtiTrace::safe_get_thread_name(thread)));
2511 JvmtiMonitorEventMark jem(thread, h());
2512 JvmtiEnv *env = ets->get_env();
2513 JvmtiThreadEventTransition jet(thread);
2514 jvmtiEventMonitorWaited callback = env->callbacks()->MonitorWaited;
2515 if (callback != NULL) {
2516 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2517 jem.jni_object(), timed_out);
2518 }
2519 }
2520 }
2521 }
2522
2523 void JvmtiExport::post_vm_object_alloc(JavaThread *thread, oop object) {
2524 EVT_TRIG_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Trg vm object alloc triggered",
2525 JvmtiTrace::safe_get_thread_name(thread)));
2526 if (object == NULL) {
2527 return;
2528 }
2529 HandleMark hm(thread);
2530 Handle h(thread, object);
2531 JvmtiEnvIterator it;
2532 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2533 if (env->is_enabled(JVMTI_EVENT_VM_OBJECT_ALLOC)) {
2534 EVT_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Evt vmobject alloc sent %s",
2535 JvmtiTrace::safe_get_thread_name(thread),
2536 object==NULL? "NULL" : object->klass()->external_name()));
2537
2538 JvmtiObjectAllocEventMark jem(thread, h());
2539 JvmtiJavaThreadEventTransition jet(thread);
2540 jvmtiEventVMObjectAlloc callback = env->callbacks()->VMObjectAlloc;
2541 if (callback != NULL) {
2542 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2543 jem.jni_jobject(), jem.jni_class(), jem.size());
2544 }
2545 }
2546 }
2547 }
2548
2549 void JvmtiExport::post_sampled_object_alloc(JavaThread *thread, oop object) {
2550 EVT_TRIG_TRACE(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC,
2551 ("[%s] Trg sampled object alloc triggered",
2552 JvmtiTrace::safe_get_thread_name(thread)));
2553
2554 if (object == NULL) {
2555 return;
2556 }
2557
2558 HandleMark hm(thread);
2559 Handle h(thread, object);
2560
2561 JvmtiEnvIterator it;
2562 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2563 if (env->is_enabled(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC)) {
2564 EVT_TRACE(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC,
2565 ("[%s] Evt sampled object alloc sent %s",
2566 JvmtiTrace::safe_get_thread_name(thread),
2567 object == NULL ? "NULL" : object->klass()->external_name()));
2568
2569 JvmtiObjectAllocEventMark jem(thread, h());
2570 JvmtiJavaThreadEventTransition jet(thread);
2571 jvmtiEventSampledObjectAlloc callback = env->callbacks()->SampledObjectAlloc;
2572 if (callback != NULL) {
2573 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2574 jem.jni_jobject(), jem.jni_class(), jem.size());
2575 }
2576 }
2577 }
2578 }
2579
2580 ////////////////////////////////////////////////////////////////////////////////////////////////
2581
2582 void JvmtiExport::cleanup_thread(JavaThread* thread) {
2583 assert(JavaThread::current() == thread, "thread is not current");
2584 MutexLocker mu(JvmtiThreadState_lock);
2585
2586 if (thread->jvmti_thread_state() != NULL) {
2587 // This has to happen after the thread state is removed, which is
2588 // why it is not in post_thread_end_event like its complement
2589 // Maybe both these functions should be rolled into the posts?
2590 JvmtiEventController::thread_ended(thread);
2591 }
2592 }
2593
2594 void JvmtiExport::clear_detected_exception(JavaThread* thread) {
2595 assert(JavaThread::current() == thread, "thread is not current");
2596
2597 JvmtiThreadState* state = thread->jvmti_thread_state();
2598 if (state != NULL) {
2599 state->clear_exception_state();
2600 }
2601 }
2602
2603 void JvmtiExport::oops_do(OopClosure* f) {
2604 JvmtiCurrentBreakpoints::oops_do(f);
2605 JvmtiObjectAllocEventCollector::oops_do_for_all_threads(f);
2606 }
2607
2608 void JvmtiExport::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
2609 JvmtiTagMap::weak_oops_do(is_alive, f);
2610 }
2611
2612 void JvmtiExport::gc_epilogue() {
2613 JvmtiCurrentBreakpoints::gc_epilogue();
2614 }
2615
2616 // Onload raw monitor transition.
2617 void JvmtiExport::transition_pending_onload_raw_monitors() {
2618 JvmtiPendingMonitors::transition_raw_monitors();
2619 }
2620
2621 ////////////////////////////////////////////////////////////////////////////////////////////////
2622 #if INCLUDE_SERVICES
2623 // Attach is disabled if SERVICES is not included
2624
2625 // type for the Agent_OnAttach entry point
2718 }
2719 return result;
2720 }
2721
2722 #endif // INCLUDE_SERVICES
2723 ////////////////////////////////////////////////////////////////////////////////////////////////
2724
2725 // Setup current current thread for event collection.
2726 void JvmtiEventCollector::setup_jvmti_thread_state() {
2727 // set this event collector to be the current one.
2728 JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
2729 // state can only be NULL if the current thread is exiting which
2730 // should not happen since we're trying to configure for event collection
2731 guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state");
2732 if (is_vm_object_alloc_event()) {
2733 _prev = state->get_vm_object_alloc_event_collector();
2734 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this);
2735 } else if (is_dynamic_code_event()) {
2736 _prev = state->get_dynamic_code_event_collector();
2737 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this);
2738 } else if (is_sampled_object_alloc_event()) {
2739 _prev = state->get_sampled_object_alloc_event_collector();
2740 state->set_sampled_object_alloc_event_collector((JvmtiSampledObjectAllocEventCollector*)this);
2741 }
2742 }
2743
2744 // Unset current event collection in this thread and reset it with previous
2745 // collector.
2746 void JvmtiEventCollector::unset_jvmti_thread_state() {
2747 JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state();
2748 if (state != NULL) {
2749 // restore the previous event collector (if any)
2750 if (is_vm_object_alloc_event()) {
2751 if (state->get_vm_object_alloc_event_collector() == this) {
2752 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev);
2753 } else {
2754 // this thread's jvmti state was created during the scope of
2755 // the event collector.
2756 }
2757 } else if (is_dynamic_code_event()) {
2758 if (state->get_dynamic_code_event_collector() == this) {
2759 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev);
2760 } else {
2761 // this thread's jvmti state was created during the scope of
2762 // the event collector.
2763 }
2764 } else if (is_sampled_object_alloc_event()) {
2765 if (state->get_sampled_object_alloc_event_collector() == this) {
2766 state->set_sampled_object_alloc_event_collector((JvmtiSampledObjectAllocEventCollector*)_prev);
2767 } else {
2768 // this thread's jvmti state was created during the scope of
2769 // the event collector.
2770 }
2771 }
2772 }
2773 }
2774
2775 // create the dynamic code event collector
2776 JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(NULL) {
2777 if (JvmtiExport::should_post_dynamic_code_generated()) {
2778 setup_jvmti_thread_state();
2779 }
2780 }
2781
2782 // iterate over any code blob descriptors collected and post a
2783 // DYNAMIC_CODE_GENERATED event to the profiler.
2784 JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() {
2785 assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events");
2786 // iterate over any code blob descriptors that we collected
2787 if (_code_blobs != NULL) {
2788 for (int i=0; i<_code_blobs->length(); i++) {
2789 JvmtiCodeBlobDesc* blob = _code_blobs->at(i);
2790 JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end());
2791 FreeHeap(blob);
2792 }
2793 delete _code_blobs;
2794 }
2795 unset_jvmti_thread_state();
2796 }
2797
2798 // register a stub
2799 void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) {
2800 if (_code_blobs == NULL) {
2801 _code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(1,true);
2802 }
2803 _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end));
2804 }
2805
2806 JvmtiObjectAllocEventCollector::JvmtiObjectAllocEventCollector() :
2807 _allocated(NULL), _enable(false), _post_callback(NULL),
2808 _callback_for_all_oops(false) {
2809 }
2810
2811 void JvmtiObjectAllocEventCollector::generate_call_for_allocated() {
2812 if (_allocated != NULL) {
2813 set_enabled(false);
2814 for (int i = 0; i < _allocated->length(); i++) {
2815 oop obj = _allocated->at(i);
2816 if (_callback_for_all_oops || ServiceUtil::visible_oop(obj)) {
2817 _post_callback(JavaThread::current(), obj);
2818 }
2819 }
2820 delete _allocated;
2821 }
2822 }
2823
2824 void JvmtiObjectAllocEventCollector::record_allocation(oop obj) {
2825 assert(is_enabled(), "Object alloc event collector is not enabled");
2826 if (_allocated == NULL) {
2827 _allocated = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(1, true);
2828 }
2829 _allocated->push(obj);
2830 }
2831
2832 // GC support.
2833 void JvmtiObjectAllocEventCollector::oops_do(OopClosure* f) {
2834 if (_allocated != NULL) {
2835 for(int i=_allocated->length() - 1; i >= 0; i--) {
2836 if (_allocated->at(i) != NULL) {
2837 f->do_oop(_allocated->adr_at(i));
2838 }
2839 }
2840 }
2841 }
2842
2843 void JvmtiObjectAllocEventCollector::oops_do_for_all_threads(OopClosure* f) {
2844 // no-op if jvmti not enabled
2845 if (!JvmtiEnv::environments_might_exist()) {
2846 return;
2847 }
2848
2849 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jthr = jtiwh.next(); ) {
2850 JvmtiThreadState *state = jthr->jvmti_thread_state();
2851 if (state != NULL) {
2852 JvmtiObjectAllocEventCollector *collector;
2853 collector = state->get_vm_object_alloc_event_collector();
2854 while (collector != NULL) {
2855 collector->oops_do(f);
2856 collector = (JvmtiObjectAllocEventCollector*) collector->get_prev();
2857 }
2858
2859 collector = state->get_sampled_object_alloc_event_collector();
2860 while (collector != NULL) {
2861 collector->oops_do(f);
2862 collector = (JvmtiObjectAllocEventCollector*) collector->get_prev();
2863 }
2864 }
2865 }
2866 }
2867
2868
2869 // Disable collection of VMObjectAlloc events
2870 NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) {
2871 // a no-op if VMObjectAlloc event is not enabled
2872 if (!JvmtiExport::should_post_vm_object_alloc()) {
2873 return;
2874 }
2875 Thread* thread = Thread::current_or_null();
2876 if (thread != NULL && thread->is_Java_thread()) {
2877 JavaThread* current_thread = (JavaThread*)thread;
2878 JvmtiThreadState *state = current_thread->jvmti_thread_state();
2879 if (state != NULL) {
2880 JvmtiVMObjectAllocEventCollector *collector;
2881 collector = state->get_vm_object_alloc_event_collector();
2882 if (collector != NULL && collector->is_enabled()) {
2883 _collector = collector;
2884 _collector->set_enabled(false);
2885 }
2886 }
2887 }
2888 }
2889
2890 // Re-Enable collection of VMObjectAlloc events (if previously enabled)
2891 NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() {
2892 if (was_enabled()) {
2893 _collector->set_enabled(true);
2894 }
2895 };
2896
2897 // Setup current thread to record vm allocated objects.
2898 JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() {
2899 if (JvmtiExport::should_post_vm_object_alloc()) {
2900 _enable = true;
2901 _post_callback = JvmtiExport::post_vm_object_alloc;
2902 setup_jvmti_thread_state();
2903 }
2904 }
2905
2906 JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() {
2907 generate_call_for_allocated();
2908 unset_jvmti_thread_state();
2909 }
2910
2911 // Setup current thread to record sampled allocated objects.
2912 JvmtiSampledObjectAllocEventCollector::JvmtiSampledObjectAllocEventCollector() {
2913 if (JvmtiExport::should_post_sampled_object_alloc()) {
2914 _enable = true;
2915 // TODO: switch it back..
2916 _callback_for_all_oops = false;
2917 _post_callback = JvmtiExport::post_sampled_object_alloc;
2918 setup_jvmti_thread_state();
2919 }
2920 }
2921
2922 JvmtiSampledObjectAllocEventCollector::~JvmtiSampledObjectAllocEventCollector() {
2923 generate_call_for_allocated();
2924 unset_jvmti_thread_state();
2925 }
2926
2927 JvmtiGCMarker::JvmtiGCMarker() {
2928 // if there aren't any JVMTI environments then nothing to do
2929 if (!JvmtiEnv::environments_might_exist()) {
2930 return;
2931 }
2932
2933 if (JvmtiExport::should_post_garbage_collection_start()) {
2934 JvmtiExport::post_garbage_collection_start();
2935 }
2936
2937 if (SafepointSynchronize::is_at_safepoint()) {
2938 // Do clean up tasks that need to be done at a safepoint
2939 JvmtiEnvBase::check_for_periodic_clean_up();
2940 }
2941 }
2942
2943 JvmtiGCMarker::~JvmtiGCMarker() {
2944 // if there aren't any JVMTI environments then nothing to do
2945 if (!JvmtiEnv::environments_might_exist()) {
|