< prev index next >

src/hotspot/share/prims/jvmtiExport.cpp

Print this page
rev 49521 : [mq]: heap8
rev 49522 : [mq]: event_rebased
rev 49523 : [mq]: heap9
rev 49524 : [mq]: heap11


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",


2266 {
2267   // register the stub with the current dynamic code event collector
2268   JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
2269   // state can only be NULL if the current thread is exiting which
2270   // should not happen since we're trying to post an event
2271   guarantee(state != NULL, "attempt to register stub via an exiting thread");
2272   JvmtiDynamicCodeEventCollector* collector = state->get_dynamic_code_event_collector();
2273   guarantee(collector != NULL, "attempt to register stub without event collector");
2274   collector->register_stub(name, code_begin, code_end);
2275 }
2276 
2277 // Collect all the vm internally allocated objects which are visible to java world
2278 void JvmtiExport::record_vm_internal_object_allocation(oop obj) {
2279   Thread* thread = Thread::current_or_null();
2280   if (thread != NULL && thread->is_Java_thread())  {
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",


2267 {
2268   // register the stub with the current dynamic code event collector
2269   JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
2270   // state can only be NULL if the current thread is exiting which
2271   // should not happen since we're trying to post an event
2272   guarantee(state != NULL, "attempt to register stub via an exiting thread");
2273   JvmtiDynamicCodeEventCollector* collector = state->get_dynamic_code_event_collector();
2274   guarantee(collector != NULL, "attempt to register stub without event collector");
2275   collector->register_stub(name, code_begin, code_end);
2276 }
2277 
2278 // Collect all the vm internally allocated objects which are visible to java world
2279 void JvmtiExport::record_vm_internal_object_allocation(oop obj) {
2280   Thread* thread = Thread::current_or_null();
2281   if (thread != NULL && thread->is_Java_thread())  {
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 
2316       if (collector != NULL && collector->is_enabled()) {
2317         collector->record_allocation(obj);
2318       }
2319     }
2320   }
2321 }
2322 
2323 void JvmtiExport::post_garbage_collection_finish() {
2324   Thread *thread = Thread::current(); // this event is posted from VM-Thread.
2325   EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2326                  ("[%s] garbage collection finish event triggered",
2327                   JvmtiTrace::safe_get_thread_name(thread)));
2328   JvmtiEnvIterator it;
2329   for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2330     if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH)) {
2331       EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2332                 ("[%s] garbage collection finish event sent",
2333                  JvmtiTrace::safe_get_thread_name(thread)));
2334       JvmtiThreadEventTransition jet(thread);
2335       // JNIEnv is NULL here because this event is posted from VM Thread
2336       jvmtiEventGarbageCollectionFinish callback = env->callbacks()->GarbageCollectionFinish;
2337       if (callback != NULL) {
2338         (*callback)(env->jvmti_external());
2339       }
2340     }
2341   }
2342 }


2492                       JvmtiTrace::safe_get_thread_name(thread)));
2493 
2494   JvmtiEnvThreadStateIterator it(state);
2495   for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2496     if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAITED)) {
2497       EVT_TRACE(JVMTI_EVENT_MONITOR_WAITED,
2498                    ("[%s] monitor waited event sent",
2499                     JvmtiTrace::safe_get_thread_name(thread)));
2500       JvmtiMonitorEventMark  jem(thread, h());
2501       JvmtiEnv *env = ets->get_env();
2502       JvmtiThreadEventTransition jet(thread);
2503       jvmtiEventMonitorWaited callback = env->callbacks()->MonitorWaited;
2504       if (callback != NULL) {
2505         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2506                     jem.jni_object(), timed_out);
2507       }
2508     }
2509   }
2510 }
2511 

2512 void JvmtiExport::post_vm_object_alloc(JavaThread *thread, oop object) {
2513   EVT_TRIG_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Trg vm object alloc triggered",
2514                       JvmtiTrace::safe_get_thread_name(thread)));
2515   if (object == NULL) {
2516     return;
2517   }
2518   HandleMark hm(thread);
2519   Handle h(thread, object);
2520   JvmtiEnvIterator it;
2521   for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2522     if (env->is_enabled(JVMTI_EVENT_VM_OBJECT_ALLOC)) {
2523       EVT_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Evt vmobject alloc sent %s",
2524                                          JvmtiTrace::safe_get_thread_name(thread),
2525                                          object==NULL? "NULL" : object->klass()->external_name()));
2526 
2527       JvmtiObjectAllocEventMark jem(thread, h());
2528       JvmtiJavaThreadEventTransition jet(thread);
2529       jvmtiEventVMObjectAlloc callback = env->callbacks()->VMObjectAlloc;
2530       if (callback != NULL) {
2531         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2532                     jem.jni_jobject(), jem.jni_class(), jem.size());
2533       }
2534     }
2535   }
2536 }
2537 
2538 void JvmtiExport::post_sampled_object_alloc(JavaThread *thread, oop object) {
2539   EVT_TRIG_TRACE(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC,
2540                  ("[%s] Trg sampled object alloc triggered",
2541                   JvmtiTrace::safe_get_thread_name(thread)));
2542   if (object == NULL) {
2543     return;
2544   }
2545 
2546   HandleMark hm(thread);
2547   Handle h(thread, object);
2548   JvmtiEnvIterator it;
2549   for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2550     if (env->is_enabled(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC)) {
2551       EVT_TRACE(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC,
2552                 ("[%s] Evt sampled object alloc sent %s",
2553                  JvmtiTrace::safe_get_thread_name(thread),
2554                  object == NULL ? "NULL" : object->klass()->external_name()));
2555 
2556       JvmtiObjectAllocEventMark jem(thread, h());
2557       JvmtiJavaThreadEventTransition jet(thread);
2558       jvmtiEventSampledObjectAlloc callback = env->callbacks()->SampledObjectAlloc;
2559       if (callback != NULL) {
2560         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2561                     jem.jni_jobject(), jem.jni_class(), jem.size());
2562       }
2563     }
2564   }
2565 }
2566 
2567 ////////////////////////////////////////////////////////////////////////////////////////////////
2568 
2569 void JvmtiExport::cleanup_thread(JavaThread* thread) {
2570   assert(JavaThread::current() == thread, "thread is not current");
2571   MutexLocker mu(JvmtiThreadState_lock);
2572 
2573   if (thread->jvmti_thread_state() != NULL) {
2574     // This has to happen after the thread state is removed, which is
2575     // why it is not in post_thread_end_event like its complement
2576     // Maybe both these functions should be rolled into the posts?
2577     JvmtiEventController::thread_ended(thread);
2578   }
2579 }
2580 
2581 void JvmtiExport::clear_detected_exception(JavaThread* thread) {
2582   assert(JavaThread::current() == thread, "thread is not current");
2583 
2584   JvmtiThreadState* state = thread->jvmti_thread_state();
2585   if (state != NULL) {
2586     state->clear_exception_state();
2587   }
2588 }
2589 
2590 void JvmtiExport::oops_do(OopClosure* f) {
2591   JvmtiCurrentBreakpoints::oops_do(f);
2592   JvmtiObjectAllocEventCollector::oops_do_for_all_threads(f);
2593 }
2594 
2595 void JvmtiExport::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
2596   JvmtiTagMap::weak_oops_do(is_alive, f);
2597 }
2598 
2599 void JvmtiExport::gc_epilogue() {
2600   JvmtiCurrentBreakpoints::gc_epilogue();
2601 }
2602 
2603 // Onload raw monitor transition.
2604 void JvmtiExport::transition_pending_onload_raw_monitors() {
2605   JvmtiPendingMonitors::transition_raw_monitors();
2606 }
2607 
2608 ////////////////////////////////////////////////////////////////////////////////////////////////
2609 #if INCLUDE_SERVICES
2610 // Attach is disabled if SERVICES is not included
2611 
2612 // type for the Agent_OnAttach entry point


2705   }
2706   return result;
2707 }
2708 
2709 #endif // INCLUDE_SERVICES
2710 ////////////////////////////////////////////////////////////////////////////////////////////////
2711 
2712 // Setup current current thread for event collection.
2713 void JvmtiEventCollector::setup_jvmti_thread_state() {
2714   // set this event collector to be the current one.
2715   JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
2716   // state can only be NULL if the current thread is exiting which
2717   // should not happen since we're trying to configure for event collection
2718   guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state");
2719   if (is_vm_object_alloc_event()) {
2720     _prev = state->get_vm_object_alloc_event_collector();
2721     state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this);
2722   } else if (is_dynamic_code_event()) {
2723     _prev = state->get_dynamic_code_event_collector();
2724     state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this);
2725   } else if (is_sampled_object_alloc_event()) {
2726     _prev = state->get_sampled_object_alloc_event_collector();
2727     state->set_sampled_object_alloc_event_collector((JvmtiSampledObjectAllocEventCollector*)this);
2728   }
2729 }
2730 
2731 // Unset current event collection in this thread and reset it with previous
2732 // collector.
2733 void JvmtiEventCollector::unset_jvmti_thread_state() {
2734   JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state();
2735   if (state != NULL) {
2736     // restore the previous event collector (if any)
2737     if (is_vm_object_alloc_event()) {
2738       if (state->get_vm_object_alloc_event_collector() == this) {
2739         state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev);
2740       } else {
2741         // this thread's jvmti state was created during the scope of
2742         // the event collector.
2743       }
2744     } else if (is_dynamic_code_event()) {

2745       if (state->get_dynamic_code_event_collector() == this) {
2746         state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev);
2747       } else {
2748         // this thread's jvmti state was created during the scope of
2749         // the event collector.
2750       }
2751     } else if (is_sampled_object_alloc_event()) {
2752       if (state->get_sampled_object_alloc_event_collector() == this) {
2753         state->set_sampled_object_alloc_event_collector((JvmtiSampledObjectAllocEventCollector*)_prev);
2754       } else {
2755         // this thread's jvmti state was created during the scope of
2756         // the event collector.
2757       }
2758     }
2759   }
2760 }
2761 
2762 // create the dynamic code event collector
2763 JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(NULL) {
2764   if (JvmtiExport::should_post_dynamic_code_generated()) {
2765     setup_jvmti_thread_state();
2766   }
2767 }
2768 
2769 // iterate over any code blob descriptors collected and post a
2770 // DYNAMIC_CODE_GENERATED event to the profiler.
2771 JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() {
2772   assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events");
2773  // iterate over any code blob descriptors that we collected
2774  if (_code_blobs != NULL) {
2775    for (int i=0; i<_code_blobs->length(); i++) {
2776      JvmtiCodeBlobDesc* blob = _code_blobs->at(i);
2777      JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end());
2778      FreeHeap(blob);
2779    }
2780    delete _code_blobs;
2781  }
2782  unset_jvmti_thread_state();
2783 }
2784 
2785 // register a stub
2786 void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) {
2787  if (_code_blobs == NULL) {
2788    _code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(1,true);
2789  }
2790  _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end));
2791 }
2792 
2793 // Setup current thread to record vm allocated objects.
2794 JvmtiObjectAllocEventCollector::JvmtiObjectAllocEventCollector() :
2795     _allocated(NULL), _enable(false), _post_callback(NULL) {





2796 }
2797 
2798 // Post vm_object_alloc event for vm allocated objects visible to java
2799 // world.
2800 void JvmtiObjectAllocEventCollector::generate_call_for_allocated() {
2801   if (_allocated != NULL) {
2802     set_enabled(false);
2803     for (int i = 0; i < _allocated->length(); i++) {
2804       oop obj = _allocated->at(i);
2805       _post_callback(JavaThread::current(), obj);
2806     }
2807     delete _allocated, _allocated = NULL;
2808   }

2809 }
2810 
2811 void JvmtiObjectAllocEventCollector::record_allocation(oop obj) {
2812   assert(is_enabled(), "Object alloc event collector is not enabled");
2813   if (_allocated == NULL) {
2814     _allocated = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(1, true);
2815   }
2816   _allocated->push(obj);
2817 }
2818 
2819 // GC support.
2820 void JvmtiObjectAllocEventCollector::oops_do(OopClosure* f) {
2821   if (_allocated != NULL) {
2822     for(int i = _allocated->length() - 1; i >= 0; i--) {
2823       if (_allocated->at(i) != NULL) {
2824         f->do_oop(_allocated->adr_at(i));
2825       }
2826     }
2827   }
2828 }
2829 
2830 void JvmtiObjectAllocEventCollector::oops_do_for_all_threads(OopClosure* f) {
2831   // no-op if jvmti not enabled
2832   if (!JvmtiEnv::environments_might_exist()) {
2833     return;
2834   }
2835 
2836   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jthr = jtiwh.next(); ) {
2837     JvmtiThreadState *state = jthr->jvmti_thread_state();
2838     if (state != NULL) {
2839       JvmtiObjectAllocEventCollector *collector;
2840       collector = state->get_vm_object_alloc_event_collector();
2841       while (collector != NULL) {
2842         collector->oops_do(f);
2843         collector = (JvmtiObjectAllocEventCollector*) collector->get_prev();
2844       }
2845 
2846       collector = state->get_sampled_object_alloc_event_collector();
2847       while (collector != NULL) {
2848         collector->oops_do(f);
2849         collector = (JvmtiObjectAllocEventCollector*) collector->get_prev();
2850       }
2851     }
2852   }
2853 }
2854 
2855 
2856 // Disable collection of VMObjectAlloc events
2857 NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) {
2858   // a no-op if VMObjectAlloc event is not enabled
2859   if (!JvmtiExport::should_post_vm_object_alloc()) {
2860     return;
2861   }
2862   Thread* thread = Thread::current_or_null();
2863   if (thread != NULL && thread->is_Java_thread())  {
2864     JavaThread* current_thread = (JavaThread*)thread;
2865     JvmtiThreadState *state = current_thread->jvmti_thread_state();
2866     if (state != NULL) {
2867       JvmtiVMObjectAllocEventCollector *collector;
2868       collector = state->get_vm_object_alloc_event_collector();
2869       if (collector != NULL && collector->is_enabled()) {
2870         _collector = collector;
2871         _collector->set_enabled(false);
2872       }
2873     }
2874   }
2875 }
2876 
2877 // Re-Enable collection of VMObjectAlloc events (if previously enabled)
2878 NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() {
2879   if (was_enabled()) {
2880     _collector->set_enabled(true);
2881   }
2882 };
2883 
2884 // Setup current thread to record vm allocated objects.
2885 JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() {
2886   if (JvmtiExport::should_post_vm_object_alloc()) {
2887     _enable = true;
2888     _post_callback = JvmtiExport::post_vm_object_alloc;
2889     setup_jvmti_thread_state();
2890   }
2891 }
2892 
2893 JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() {
2894   generate_call_for_allocated();
2895   unset_jvmti_thread_state();
2896 }
2897 
2898 // Setup current thread to record sampled allocated objects.
2899 JvmtiSampledObjectAllocEventCollector::JvmtiSampledObjectAllocEventCollector() {
2900   if (JvmtiExport::should_post_sampled_object_alloc()) {
2901     _enable = true;
2902     _post_callback = JvmtiExport::post_sampled_object_alloc;
2903     setup_jvmti_thread_state();
2904   }
2905   // Set the sampling collector as present in assertion mode only.
2906   assert(JavaThread::current()->heap_sampler().add_sampling_collector(),
2907          "Should never return false.");
2908 }
2909 
2910 JvmtiSampledObjectAllocEventCollector::~JvmtiSampledObjectAllocEventCollector() {
2911   if (JvmtiExport::should_post_sampled_object_alloc()) {
2912     generate_call_for_allocated();
2913   }
2914   // Note: we set ourselves in the thread state only if we should post but
2915   // always unset in case the user has disabled the posting but we were set.
2916   unset_jvmti_thread_state();
2917   // Set the sampling collector as present in assertion mode only.
2918   assert(JavaThread::current()->heap_sampler().remove_sampling_collector(),
2919          "Should never return false.");
2920 }
2921 
2922 JvmtiGCMarker::JvmtiGCMarker() {
2923   // if there aren't any JVMTI environments then nothing to do
2924   if (!JvmtiEnv::environments_might_exist()) {
2925     return;
2926   }
2927 
2928   if (JvmtiExport::should_post_garbage_collection_start()) {
2929     JvmtiExport::post_garbage_collection_start();
2930   }
2931 
2932   if (SafepointSynchronize::is_at_safepoint()) {
2933     // Do clean up tasks that need to be done at a safepoint
2934     JvmtiEnvBase::check_for_periodic_clean_up();
2935   }
2936 }
2937 
2938 JvmtiGCMarker::~JvmtiGCMarker() {
2939   // if there aren't any JVMTI environments then nothing to do
2940   if (!JvmtiEnv::environments_might_exist()) {
< prev index next >