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 }
2470 JvmtiTrace::safe_get_thread_name(thread)));
2471
2472 JvmtiEnvThreadStateIterator it(state);
2473 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2474 if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAITED)) {
2475 EVT_TRACE(JVMTI_EVENT_MONITOR_WAITED,
2476 ("[%s] monitor waited event sent",
2477 JvmtiTrace::safe_get_thread_name(thread)));
2478 JvmtiMonitorEventMark jem(thread, h());
2479 JvmtiEnv *env = ets->get_env();
2480 JvmtiThreadEventTransition jet(thread);
2481 jvmtiEventMonitorWaited callback = env->callbacks()->MonitorWaited;
2482 if (callback != NULL) {
2483 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2484 jem.jni_object(), timed_out);
2485 }
2486 }
2487 }
2488 }
2489
2490
2491 void JvmtiExport::post_vm_object_alloc(JavaThread *thread, oop object) {
2492 EVT_TRIG_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Trg vm object alloc triggered",
2493 JvmtiTrace::safe_get_thread_name(thread)));
2494 if (object == NULL) {
2495 return;
2496 }
2497 HandleMark hm(thread);
2498 Handle h(thread, object);
2499 JvmtiEnvIterator it;
2500 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2501 if (env->is_enabled(JVMTI_EVENT_VM_OBJECT_ALLOC)) {
2502 EVT_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Evt vmobject alloc sent %s",
2503 JvmtiTrace::safe_get_thread_name(thread),
2504 object==NULL? "NULL" : object->klass()->external_name()));
2505
2506 JvmtiVMObjectAllocEventMark jem(thread, h());
2507 JvmtiJavaThreadEventTransition jet(thread);
2508 jvmtiEventVMObjectAlloc callback = env->callbacks()->VMObjectAlloc;
2509 if (callback != NULL) {
2510 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2511 jem.jni_jobject(), jem.jni_class(), jem.size());
2512 }
2513 }
2514 }
2515 }
2516
2517 ////////////////////////////////////////////////////////////////////////////////////////////////
2518
2519 void JvmtiExport::cleanup_thread(JavaThread* thread) {
2520 assert(JavaThread::current() == thread, "thread is not current");
2521 MutexLocker mu(JvmtiThreadState_lock);
2522
2523 if (thread->jvmti_thread_state() != NULL) {
2524 // This has to happen after the thread state is removed, which is
2525 // why it is not in post_thread_end_event like its complement
2526 // Maybe both these functions should be rolled into the posts?
2527 JvmtiEventController::thread_ended(thread);
2528 }
2529 }
2530
2531 void JvmtiExport::clear_detected_exception(JavaThread* thread) {
2532 assert(JavaThread::current() == thread, "thread is not current");
2533
2534 JvmtiThreadState* state = thread->jvmti_thread_state();
2535 if (state != NULL) {
2536 state->clear_exception_state();
2537 }
2538 }
2539
2540 void JvmtiExport::oops_do(OopClosure* f) {
2541 JvmtiCurrentBreakpoints::oops_do(f);
2542 JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(f);
2543 }
2544
2545 void JvmtiExport::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
2546 JvmtiTagMap::weak_oops_do(is_alive, f);
2547 }
2548
2549 void JvmtiExport::gc_epilogue() {
2550 JvmtiCurrentBreakpoints::gc_epilogue();
2551 }
2552
2553 // Onload raw monitor transition.
2554 void JvmtiExport::transition_pending_onload_raw_monitors() {
2555 JvmtiPendingMonitors::transition_raw_monitors();
2556 }
2557
2558 ////////////////////////////////////////////////////////////////////////////////////////////////
2559 #if INCLUDE_SERVICES
2560 // Attach is disabled if SERVICES is not included
2561
2562 // type for the Agent_OnAttach entry point
2655 }
2656 return result;
2657 }
2658
2659 #endif // INCLUDE_SERVICES
2660 ////////////////////////////////////////////////////////////////////////////////////////////////
2661
2662 // Setup current current thread for event collection.
2663 void JvmtiEventCollector::setup_jvmti_thread_state() {
2664 // set this event collector to be the current one.
2665 JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
2666 // state can only be NULL if the current thread is exiting which
2667 // should not happen since we're trying to configure for event collection
2668 guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state");
2669 if (is_vm_object_alloc_event()) {
2670 _prev = state->get_vm_object_alloc_event_collector();
2671 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this);
2672 } else if (is_dynamic_code_event()) {
2673 _prev = state->get_dynamic_code_event_collector();
2674 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this);
2675 }
2676 }
2677
2678 // Unset current event collection in this thread and reset it with previous
2679 // collector.
2680 void JvmtiEventCollector::unset_jvmti_thread_state() {
2681 JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state();
2682 if (state != NULL) {
2683 // restore the previous event collector (if any)
2684 if (is_vm_object_alloc_event()) {
2685 if (state->get_vm_object_alloc_event_collector() == this) {
2686 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev);
2687 } else {
2688 // this thread's jvmti state was created during the scope of
2689 // the event collector.
2690 }
2691 } else {
2692 if (is_dynamic_code_event()) {
2693 if (state->get_dynamic_code_event_collector() == this) {
2694 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev);
2695 } else {
2696 // this thread's jvmti state was created during the scope of
2697 // the event collector.
2698 }
2699 }
2700 }
2701 }
2702 }
2703
2704 // create the dynamic code event collector
2705 JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(NULL) {
2706 if (JvmtiExport::should_post_dynamic_code_generated()) {
2707 setup_jvmti_thread_state();
2708 }
2709 }
2710
2711 // iterate over any code blob descriptors collected and post a
2712 // DYNAMIC_CODE_GENERATED event to the profiler.
2713 JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() {
2714 assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events");
2715 // iterate over any code blob descriptors that we collected
2716 if (_code_blobs != NULL) {
2717 for (int i=0; i<_code_blobs->length(); i++) {
2718 JvmtiCodeBlobDesc* blob = _code_blobs->at(i);
2719 JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end());
2720 FreeHeap(blob);
2721 }
2722 delete _code_blobs;
2723 }
2724 unset_jvmti_thread_state();
2725 }
2726
2727 // register a stub
2728 void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) {
2729 if (_code_blobs == NULL) {
2730 _code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(1,true);
2731 }
2732 _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end));
2733 }
2734
2735 // Setup current thread to record vm allocated objects.
2736 JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() : _allocated(NULL) {
2737 if (JvmtiExport::should_post_vm_object_alloc()) {
2738 _enable = true;
2739 setup_jvmti_thread_state();
2740 } else {
2741 _enable = false;
2742 }
2743 }
2744
2745 // Post vm_object_alloc event for vm allocated objects visible to java
2746 // world.
2747 JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() {
2748 if (_allocated != NULL) {
2749 set_enabled(false);
2750 for (int i = 0; i < _allocated->length(); i++) {
2751 oop obj = _allocated->at(i);
2752 JvmtiExport::post_vm_object_alloc(JavaThread::current(), obj);
2753 }
2754 delete _allocated;
2755 }
2756 unset_jvmti_thread_state();
2757 }
2758
2759 void JvmtiVMObjectAllocEventCollector::record_allocation(oop obj) {
2760 assert(is_enabled(), "VM object alloc event collector is not enabled");
2761 if (_allocated == NULL) {
2762 _allocated = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(1, true);
2763 }
2764 _allocated->push(obj);
2765 }
2766
2767 // GC support.
2768 void JvmtiVMObjectAllocEventCollector::oops_do(OopClosure* f) {
2769 if (_allocated != NULL) {
2770 for(int i=_allocated->length() - 1; i >= 0; i--) {
2771 if (_allocated->at(i) != NULL) {
2772 f->do_oop(_allocated->adr_at(i));
2773 }
2774 }
2775 }
2776 }
2777
2778 void JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(OopClosure* f) {
2779 // no-op if jvmti not enabled
2780 if (!JvmtiEnv::environments_might_exist()) {
2781 return;
2782 }
2783
2784 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jthr = jtiwh.next(); ) {
2785 JvmtiThreadState *state = jthr->jvmti_thread_state();
2786 if (state != NULL) {
2787 JvmtiVMObjectAllocEventCollector *collector;
2788 collector = state->get_vm_object_alloc_event_collector();
2789 while (collector != NULL) {
2790 collector->oops_do(f);
2791 collector = (JvmtiVMObjectAllocEventCollector *)collector->get_prev();
2792 }
2793 }
2794 }
2795 }
2796
2797
2798 // Disable collection of VMObjectAlloc events
2799 NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) {
2800 // a no-op if VMObjectAlloc event is not enabled
2801 if (!JvmtiExport::should_post_vm_object_alloc()) {
2802 return;
2803 }
2804 Thread* thread = Thread::current_or_null();
2805 if (thread != NULL && thread->is_Java_thread()) {
2806 JavaThread* current_thread = (JavaThread*)thread;
2807 JvmtiThreadState *state = current_thread->jvmti_thread_state();
2808 if (state != NULL) {
2809 JvmtiVMObjectAllocEventCollector *collector;
2810 collector = state->get_vm_object_alloc_event_collector();
2811 if (collector != NULL && collector->is_enabled()) {
2812 _collector = collector;
2813 _collector->set_enabled(false);
2814 }
2815 }
2816 }
2817 }
2818
2819 // Re-Enable collection of VMObjectAlloc events (if previously enabled)
2820 NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() {
2821 if (was_enabled()) {
2822 _collector->set_enabled(true);
2823 }
2824 };
2825
2826 JvmtiGCMarker::JvmtiGCMarker() {
2827 // if there aren't any JVMTI environments then nothing to do
2828 if (!JvmtiEnv::environments_might_exist()) {
2829 return;
2830 }
2831
2832 if (JvmtiExport::should_post_garbage_collection_start()) {
2833 JvmtiExport::post_garbage_collection_start();
2834 }
2835
2836 if (SafepointSynchronize::is_at_safepoint()) {
2837 // Do clean up tasks that need to be done at a safepoint
2838 JvmtiEnvBase::check_for_periodic_clean_up();
2839 }
2840 }
2841
2842 JvmtiGCMarker::~JvmtiGCMarker() {
2843 // if there aren't any JVMTI environments then nothing to do
2844 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 }
2491 JvmtiTrace::safe_get_thread_name(thread)));
2492
2493 JvmtiEnvThreadStateIterator it(state);
2494 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2495 if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAITED)) {
2496 EVT_TRACE(JVMTI_EVENT_MONITOR_WAITED,
2497 ("[%s] monitor waited event sent",
2498 JvmtiTrace::safe_get_thread_name(thread)));
2499 JvmtiMonitorEventMark jem(thread, h());
2500 JvmtiEnv *env = ets->get_env();
2501 JvmtiThreadEventTransition jet(thread);
2502 jvmtiEventMonitorWaited callback = env->callbacks()->MonitorWaited;
2503 if (callback != NULL) {
2504 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2505 jem.jni_object(), timed_out);
2506 }
2507 }
2508 }
2509 }
2510
2511 void JvmtiExport::post_vm_object_alloc(JavaThread *thread, oop object) {
2512 EVT_TRIG_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Trg vm object alloc triggered",
2513 JvmtiTrace::safe_get_thread_name(thread)));
2514 if (object == NULL) {
2515 return;
2516 }
2517 HandleMark hm(thread);
2518 Handle h(thread, object);
2519 JvmtiEnvIterator it;
2520 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2521 if (env->is_enabled(JVMTI_EVENT_VM_OBJECT_ALLOC)) {
2522 EVT_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Evt vmobject alloc sent %s",
2523 JvmtiTrace::safe_get_thread_name(thread),
2524 object==NULL? "NULL" : object->klass()->external_name()));
2525
2526 JvmtiObjectAllocEventMark jem(thread, h());
2527 JvmtiJavaThreadEventTransition jet(thread);
2528 jvmtiEventVMObjectAlloc callback = env->callbacks()->VMObjectAlloc;
2529 if (callback != NULL) {
2530 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2531 jem.jni_jobject(), jem.jni_class(), jem.size());
2532 }
2533 }
2534 }
2535 }
2536
2537 void JvmtiExport::post_sampled_object_alloc(JavaThread *thread, oop object) {
2538 EVT_TRIG_TRACE(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC,
2539 ("[%s] Trg sampled object alloc triggered",
2540 JvmtiTrace::safe_get_thread_name(thread)));
2541
2542 if (object == NULL) {
2543 return;
2544 }
2545
2546 HandleMark hm(thread);
2547 Handle h(thread, object);
2548
2549 JvmtiEnvIterator it;
2550 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2551 if (env->is_enabled(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC)) {
2552 EVT_TRACE(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC,
2553 ("[%s] Evt sampled object alloc sent %s",
2554 JvmtiTrace::safe_get_thread_name(thread),
2555 object == NULL ? "NULL" : object->klass()->external_name()));
2556
2557 JvmtiObjectAllocEventMark jem(thread, h());
2558 JvmtiJavaThreadEventTransition jet(thread);
2559 jvmtiEventSampledObjectAlloc callback = env->callbacks()->SampledObjectAlloc;
2560 if (callback != NULL) {
2561 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2562 jem.jni_jobject(), jem.jni_class(), jem.size());
2563 }
2564 }
2565 }
2566 }
2567
2568 ////////////////////////////////////////////////////////////////////////////////////////////////
2569
2570 void JvmtiExport::cleanup_thread(JavaThread* thread) {
2571 assert(JavaThread::current() == thread, "thread is not current");
2572 MutexLocker mu(JvmtiThreadState_lock);
2573
2574 if (thread->jvmti_thread_state() != NULL) {
2575 // This has to happen after the thread state is removed, which is
2576 // why it is not in post_thread_end_event like its complement
2577 // Maybe both these functions should be rolled into the posts?
2578 JvmtiEventController::thread_ended(thread);
2579 }
2580 }
2581
2582 void JvmtiExport::clear_detected_exception(JavaThread* thread) {
2583 assert(JavaThread::current() == thread, "thread is not current");
2584
2585 JvmtiThreadState* state = thread->jvmti_thread_state();
2586 if (state != NULL) {
2587 state->clear_exception_state();
2588 }
2589 }
2590
2591 void JvmtiExport::oops_do(OopClosure* f) {
2592 JvmtiCurrentBreakpoints::oops_do(f);
2593 JvmtiObjectAllocEventCollector::oops_do_for_all_threads(f);
2594 }
2595
2596 void JvmtiExport::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
2597 JvmtiTagMap::weak_oops_do(is_alive, f);
2598 }
2599
2600 void JvmtiExport::gc_epilogue() {
2601 JvmtiCurrentBreakpoints::gc_epilogue();
2602 }
2603
2604 // Onload raw monitor transition.
2605 void JvmtiExport::transition_pending_onload_raw_monitors() {
2606 JvmtiPendingMonitors::transition_raw_monitors();
2607 }
2608
2609 ////////////////////////////////////////////////////////////////////////////////////////////////
2610 #if INCLUDE_SERVICES
2611 // Attach is disabled if SERVICES is not included
2612
2613 // type for the Agent_OnAttach entry point
2706 }
2707 return result;
2708 }
2709
2710 #endif // INCLUDE_SERVICES
2711 ////////////////////////////////////////////////////////////////////////////////////////////////
2712
2713 // Setup current current thread for event collection.
2714 void JvmtiEventCollector::setup_jvmti_thread_state() {
2715 // set this event collector to be the current one.
2716 JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
2717 // state can only be NULL if the current thread is exiting which
2718 // should not happen since we're trying to configure for event collection
2719 guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state");
2720 if (is_vm_object_alloc_event()) {
2721 _prev = state->get_vm_object_alloc_event_collector();
2722 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this);
2723 } else if (is_dynamic_code_event()) {
2724 _prev = state->get_dynamic_code_event_collector();
2725 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this);
2726 } else if (is_sampled_object_alloc_event()) {
2727 _prev = state->get_sampled_object_alloc_event_collector();
2728 state->set_sampled_object_alloc_event_collector((JvmtiSampledObjectAllocEventCollector*)this);
2729 }
2730 }
2731
2732 // Unset current event collection in this thread and reset it with previous
2733 // collector.
2734 void JvmtiEventCollector::unset_jvmti_thread_state() {
2735 JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state();
2736 if (state != NULL) {
2737 // restore the previous event collector (if any)
2738 if (is_vm_object_alloc_event()) {
2739 if (state->get_vm_object_alloc_event_collector() == this) {
2740 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev);
2741 } else {
2742 // this thread's jvmti state was created during the scope of
2743 // the event collector.
2744 }
2745 } else if (is_dynamic_code_event()) {
2746 if (state->get_dynamic_code_event_collector() == this) {
2747 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev);
2748 } else {
2749 // this thread's jvmti state was created during the scope of
2750 // the event collector.
2751 }
2752 } else if (is_sampled_object_alloc_event()) {
2753 if (state->get_sampled_object_alloc_event_collector() == this) {
2754 state->set_sampled_object_alloc_event_collector((JvmtiSampledObjectAllocEventCollector*)_prev);
2755 } else {
2756 // this thread's jvmti state was created during the scope of
2757 // the event collector.
2758 }
2759 }
2760 }
2761 }
2762
2763 // create the dynamic code event collector
2764 JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(NULL) {
2765 if (JvmtiExport::should_post_dynamic_code_generated()) {
2766 setup_jvmti_thread_state();
2767 }
2768 }
2769
2770 // iterate over any code blob descriptors collected and post a
2771 // DYNAMIC_CODE_GENERATED event to the profiler.
2772 JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() {
2773 assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events");
2774 // iterate over any code blob descriptors that we collected
2775 if (_code_blobs != NULL) {
2776 for (int i=0; i<_code_blobs->length(); i++) {
2777 JvmtiCodeBlobDesc* blob = _code_blobs->at(i);
2778 JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end());
2779 FreeHeap(blob);
2780 }
2781 delete _code_blobs;
2782 }
2783 unset_jvmti_thread_state();
2784 }
2785
2786 // register a stub
2787 void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) {
2788 if (_code_blobs == NULL) {
2789 _code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(1,true);
2790 }
2791 _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end));
2792 }
2793
2794 // Setup current thread to record vm allocated objects.
2795 JvmtiObjectAllocEventCollector::JvmtiObjectAllocEventCollector() :
2796 _allocated(NULL), _enable(false), _post_callback(NULL) {
2797 }
2798
2799 // Post vm_object_alloc event for vm allocated objects visible to java
2800 // world.
2801 void JvmtiObjectAllocEventCollector::generate_call_for_allocated() {
2802 if (_allocated != NULL) {
2803 set_enabled(false);
2804 for (int i = 0; i < _allocated->length(); i++) {
2805 oop obj = _allocated->at(i);
2806 _post_callback(JavaThread::current(), obj);
2807 }
2808 delete _allocated;
2809 }
2810 }
2811
2812 void JvmtiObjectAllocEventCollector::record_allocation(oop obj) {
2813 assert(is_enabled(), "Object alloc event collector is not enabled");
2814 if (_allocated == NULL) {
2815 _allocated = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(1, true);
2816 }
2817 _allocated->push(obj);
2818 }
2819
2820 // GC support.
2821 void JvmtiObjectAllocEventCollector::oops_do(OopClosure* f) {
2822 if (_allocated != NULL) {
2823 for(int i=_allocated->length() - 1; i >= 0; i--) {
2824 if (_allocated->at(i) != NULL) {
2825 f->do_oop(_allocated->adr_at(i));
2826 }
2827 }
2828 }
2829 }
2830
2831 void JvmtiObjectAllocEventCollector::oops_do_for_all_threads(OopClosure* f) {
2832 // no-op if jvmti not enabled
2833 if (!JvmtiEnv::environments_might_exist()) {
2834 return;
2835 }
2836
2837 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jthr = jtiwh.next(); ) {
2838 JvmtiThreadState *state = jthr->jvmti_thread_state();
2839 if (state != NULL) {
2840 JvmtiObjectAllocEventCollector *collector;
2841 collector = state->get_vm_object_alloc_event_collector();
2842 while (collector != NULL) {
2843 collector->oops_do(f);
2844 collector = (JvmtiObjectAllocEventCollector*) collector->get_prev();
2845 }
2846
2847 collector = state->get_sampled_object_alloc_event_collector();
2848 while (collector != NULL) {
2849 collector->oops_do(f);
2850 collector = (JvmtiObjectAllocEventCollector*) collector->get_prev();
2851 }
2852 }
2853 }
2854 }
2855
2856
2857 // Disable collection of VMObjectAlloc events
2858 NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) {
2859 // a no-op if VMObjectAlloc event is not enabled
2860 if (!JvmtiExport::should_post_vm_object_alloc()) {
2861 return;
2862 }
2863 Thread* thread = Thread::current_or_null();
2864 if (thread != NULL && thread->is_Java_thread()) {
2865 JavaThread* current_thread = (JavaThread*)thread;
2866 JvmtiThreadState *state = current_thread->jvmti_thread_state();
2867 if (state != NULL) {
2868 JvmtiVMObjectAllocEventCollector *collector;
2869 collector = state->get_vm_object_alloc_event_collector();
2870 if (collector != NULL && collector->is_enabled()) {
2871 _collector = collector;
2872 _collector->set_enabled(false);
2873 }
2874 }
2875 }
2876 }
2877
2878 // Re-Enable collection of VMObjectAlloc events (if previously enabled)
2879 NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() {
2880 if (was_enabled()) {
2881 _collector->set_enabled(true);
2882 }
2883 };
2884
2885 // Setup current thread to record vm allocated objects.
2886 JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() {
2887 if (JvmtiExport::should_post_vm_object_alloc()) {
2888 _enable = true;
2889 _post_callback = JvmtiExport::post_vm_object_alloc;
2890 setup_jvmti_thread_state();
2891 }
2892 }
2893
2894 JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() {
2895 generate_call_for_allocated();
2896 unset_jvmti_thread_state();
2897 }
2898
2899 // Setup current thread to record sampled allocated objects.
2900 JvmtiSampledObjectAllocEventCollector::JvmtiSampledObjectAllocEventCollector() {
2901 if (JvmtiExport::should_post_sampled_object_alloc()) {
2902 _enable = true;
2903 _post_callback = JvmtiExport::post_sampled_object_alloc;
2904 setup_jvmti_thread_state();
2905 }
2906 }
2907
2908 JvmtiSampledObjectAllocEventCollector::~JvmtiSampledObjectAllocEventCollector() {
2909 generate_call_for_allocated();
2910 unset_jvmti_thread_state();
2911 }
2912
2913 JvmtiGCMarker::JvmtiGCMarker() {
2914 // if there aren't any JVMTI environments then nothing to do
2915 if (!JvmtiEnv::environments_might_exist()) {
2916 return;
2917 }
2918
2919 if (JvmtiExport::should_post_garbage_collection_start()) {
2920 JvmtiExport::post_garbage_collection_start();
2921 }
2922
2923 if (SafepointSynchronize::is_at_safepoint()) {
2924 // Do clean up tasks that need to be done at a safepoint
2925 JvmtiEnvBase::check_for_periodic_clean_up();
2926 }
2927 }
2928
2929 JvmtiGCMarker::~JvmtiGCMarker() {
2930 // if there aren't any JVMTI environments then nothing to do
2931 if (!JvmtiEnv::environments_might_exist()) {
|