< prev index next >

src/hotspot/share/prims/jvmtiExport.cpp

Print this page
rev 50392 : JEP 331


1011   // tries to access nonexistant services.
1012   if (on) {
1013     vm_exit_during_initialization("Java Kernel does not support JVMTI.");
1014   }
1015 }
1016 
1017 
1018 static inline Klass* oop_to_klass(oop obj) {
1019   Klass* k = obj->klass();
1020 
1021   // if the object is a java.lang.Class then return the java mirror
1022   if (k == SystemDictionary::Class_klass()) {
1023     if (!java_lang_Class::is_primitive(obj)) {
1024       k = java_lang_Class::as_Klass(obj);
1025       assert(k != NULL, "class for non-primitive mirror must exist");
1026     }
1027   }
1028   return k;
1029 }
1030 
1031 class JvmtiVMObjectAllocEventMark : public JvmtiClassEventMark  {
1032  private:
1033    jobject _jobj;
1034    jlong    _size;
1035  public:
1036    JvmtiVMObjectAllocEventMark(JavaThread *thread, oop obj) : JvmtiClassEventMark(thread, oop_to_klass(obj)) {
1037      _jobj = (jobject)to_jobject(obj);
1038      _size = obj->size() * wordSize;
1039    };
1040    jobject jni_jobject() { return _jobj; }
1041    jlong size() { return _size; }
1042 };
1043 
1044 class JvmtiCompiledMethodLoadEventMark : public JvmtiMethodEventMark {
1045  private:
1046   jint _code_size;
1047   const void *_code_data;
1048   jint _map_length;
1049   jvmtiAddrLocationMap *_map;
1050   const void *_compile_info;
1051  public:
1052   JvmtiCompiledMethodLoadEventMark(JavaThread *thread, nmethod *nm, void* compile_info_ptr = NULL)
1053           : JvmtiMethodEventMark(thread,methodHandle(thread, nm->method())) {
1054     _code_data = nm->insts_begin();
1055     _code_size = nm->insts_size();
1056     _compile_info = compile_info_ptr; // Set void pointer of compiledMethodLoad Event. Default value is NULL.


1181 bool              JvmtiExport::_should_post_field_modification            = false;
1182 bool              JvmtiExport::_should_post_class_load                    = false;
1183 bool              JvmtiExport::_should_post_class_prepare                 = false;
1184 bool              JvmtiExport::_should_post_class_unload                  = false;
1185 bool              JvmtiExport::_should_post_thread_life                   = false;
1186 bool              JvmtiExport::_should_clean_up_heap_objects              = false;
1187 bool              JvmtiExport::_should_post_native_method_bind            = false;
1188 bool              JvmtiExport::_should_post_dynamic_code_generated        = false;
1189 bool              JvmtiExport::_should_post_data_dump                     = false;
1190 bool              JvmtiExport::_should_post_compiled_method_load          = false;
1191 bool              JvmtiExport::_should_post_compiled_method_unload        = false;
1192 bool              JvmtiExport::_should_post_monitor_contended_enter       = false;
1193 bool              JvmtiExport::_should_post_monitor_contended_entered     = false;
1194 bool              JvmtiExport::_should_post_monitor_wait                  = false;
1195 bool              JvmtiExport::_should_post_monitor_waited                = false;
1196 bool              JvmtiExport::_should_post_garbage_collection_start      = false;
1197 bool              JvmtiExport::_should_post_garbage_collection_finish     = false;
1198 bool              JvmtiExport::_should_post_object_free                   = false;
1199 bool              JvmtiExport::_should_post_resource_exhausted            = false;
1200 bool              JvmtiExport::_should_post_vm_object_alloc               = false;

1201 bool              JvmtiExport::_should_post_on_exceptions                 = false;
1202 
1203 ////////////////////////////////////////////////////////////////////////////////////////////////
1204 
1205 
1206 //
1207 // JVMTI single step management
1208 //
1209 void JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, address location) {
1210   assert(JvmtiExport::should_post_single_step(), "must be single stepping");
1211 
1212   HandleMark hm(thread);
1213   methodHandle mh(thread, method);
1214 
1215   // update information about current location and post a step event
1216   JvmtiThreadState *state = thread->jvmti_thread_state();
1217   if (state == NULL) {
1218     return;
1219   }
1220   EVT_TRIG_TRACE(JVMTI_EVENT_SINGLE_STEP, ("[%s] Trg Single Step triggered",


2263 {
2264   // register the stub with the current dynamic code event collector
2265   JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
2266   // state can only be NULL if the current thread is exiting which
2267   // should not happen since we're trying to post an event
2268   guarantee(state != NULL, "attempt to register stub via an exiting thread");
2269   JvmtiDynamicCodeEventCollector* collector = state->get_dynamic_code_event_collector();
2270   guarantee(collector != NULL, "attempt to register stub without event collector");
2271   collector->register_stub(name, code_begin, code_end);
2272 }
2273 
2274 // Collect all the vm internally allocated objects which are visible to java world
2275 void JvmtiExport::record_vm_internal_object_allocation(oop obj) {
2276   Thread* thread = Thread::current_or_null();
2277   if (thread != NULL && thread->is_Java_thread())  {
2278     // Can not take safepoint here.
2279     NoSafepointVerifier no_sfpt;
2280     // Can not take safepoint here so can not use state_for to get
2281     // jvmti thread state.
2282     JvmtiThreadState *state = ((JavaThread*)thread)->jvmti_thread_state();
2283     if (state != NULL ) {
2284       // state is non NULL when VMObjectAllocEventCollector is enabled.
2285       JvmtiVMObjectAllocEventCollector *collector;
2286       collector = state->get_vm_object_alloc_event_collector();
2287       if (collector != NULL && collector->is_enabled()) {
2288         // Don't record classes as these will be notified via the ClassLoad
2289         // event.
2290         if (obj->klass() != SystemDictionary::Class_klass()) {
2291           collector->record_allocation(obj);
2292         }
2293       }
2294     }
2295   }
2296 }
2297 





















2298 void JvmtiExport::post_garbage_collection_finish() {
2299   Thread *thread = Thread::current(); // this event is posted from VM-Thread.
2300   EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2301                  ("[%s] garbage collection finish event triggered",
2302                   JvmtiTrace::safe_get_thread_name(thread)));
2303   JvmtiEnvIterator it;
2304   for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2305     if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH)) {
2306       EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2307                 ("[%s] garbage collection finish event sent",
2308                  JvmtiTrace::safe_get_thread_name(thread)));
2309       JvmtiThreadEventTransition jet(thread);
2310       // JNIEnv is NULL here because this event is posted from VM Thread
2311       jvmtiEventGarbageCollectionFinish callback = env->callbacks()->GarbageCollectionFinish;
2312       if (callback != NULL) {
2313         (*callback)(env->jvmti_external());
2314       }
2315     }
2316   }
2317 }


2467                       JvmtiTrace::safe_get_thread_name(thread)));
2468 
2469   JvmtiEnvThreadStateIterator it(state);
2470   for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2471     if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAITED)) {
2472       EVT_TRACE(JVMTI_EVENT_MONITOR_WAITED,
2473                    ("[%s] monitor waited event sent",
2474                     JvmtiTrace::safe_get_thread_name(thread)));
2475       JvmtiMonitorEventMark  jem(thread, h());
2476       JvmtiEnv *env = ets->get_env();
2477       JvmtiThreadEventTransition jet(thread);
2478       jvmtiEventMonitorWaited callback = env->callbacks()->MonitorWaited;
2479       if (callback != NULL) {
2480         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2481                     jem.jni_object(), timed_out);
2482       }
2483     }
2484   }
2485 }
2486 
2487 
2488 void JvmtiExport::post_vm_object_alloc(JavaThread *thread,  oop object) {
2489   EVT_TRIG_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Trg vm object alloc triggered",
2490                       JvmtiTrace::safe_get_thread_name(thread)));
2491   if (object == NULL) {
2492     return;
2493   }
2494   HandleMark hm(thread);
2495   Handle h(thread, object);
2496   JvmtiEnvIterator it;
2497   for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2498     if (env->is_enabled(JVMTI_EVENT_VM_OBJECT_ALLOC)) {
2499       EVT_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Evt vmobject alloc sent %s",
2500                                          JvmtiTrace::safe_get_thread_name(thread),
2501                                          object==NULL? "NULL" : object->klass()->external_name()));
2502 
2503       JvmtiVMObjectAllocEventMark jem(thread, h());
2504       JvmtiJavaThreadEventTransition jet(thread);
2505       jvmtiEventVMObjectAlloc callback = env->callbacks()->VMObjectAlloc;
2506       if (callback != NULL) {
2507         (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2508                     jem.jni_jobject(), jem.jni_class(), jem.size());
2509       }
2510     }
2511   }
2512 }
2513 




























2514 ////////////////////////////////////////////////////////////////////////////////////////////////
2515 
2516 void JvmtiExport::cleanup_thread(JavaThread* thread) {
2517   assert(JavaThread::current() == thread, "thread is not current");
2518   MutexLocker mu(JvmtiThreadState_lock);
2519 
2520   if (thread->jvmti_thread_state() != NULL) {
2521     // This has to happen after the thread state is removed, which is
2522     // why it is not in post_thread_end_event like its complement
2523     // Maybe both these functions should be rolled into the posts?
2524     JvmtiEventController::thread_ended(thread);
2525   }
2526 }
2527 
2528 void JvmtiExport::clear_detected_exception(JavaThread* thread) {
2529   assert(JavaThread::current() == thread, "thread is not current");
2530 
2531   JvmtiThreadState* state = thread->jvmti_thread_state();
2532   if (state != NULL) {
2533     state->clear_exception_state();
2534   }
2535 }
2536 
2537 void JvmtiExport::oops_do(OopClosure* f) {
2538   JvmtiCurrentBreakpoints::oops_do(f);
2539   JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(f);
2540 }
2541 
2542 void JvmtiExport::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
2543   JvmtiTagMap::weak_oops_do(is_alive, f);
2544 }
2545 
2546 void JvmtiExport::gc_epilogue() {
2547   JvmtiCurrentBreakpoints::gc_epilogue();
2548 }
2549 
2550 // Onload raw monitor transition.
2551 void JvmtiExport::transition_pending_onload_raw_monitors() {
2552   JvmtiPendingMonitors::transition_raw_monitors();
2553 }
2554 
2555 ////////////////////////////////////////////////////////////////////////////////////////////////
2556 #if INCLUDE_SERVICES
2557 // Attach is disabled if SERVICES is not included
2558 
2559 // type for the Agent_OnAttach entry point


2652   }
2653   return result;
2654 }
2655 
2656 #endif // INCLUDE_SERVICES
2657 ////////////////////////////////////////////////////////////////////////////////////////////////
2658 
2659 // Setup current current thread for event collection.
2660 void JvmtiEventCollector::setup_jvmti_thread_state() {
2661   // set this event collector to be the current one.
2662   JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
2663   // state can only be NULL if the current thread is exiting which
2664   // should not happen since we're trying to configure for event collection
2665   guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state");
2666   if (is_vm_object_alloc_event()) {
2667     _prev = state->get_vm_object_alloc_event_collector();
2668     state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this);
2669   } else if (is_dynamic_code_event()) {
2670     _prev = state->get_dynamic_code_event_collector();
2671     state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this);








2672   }




2673 }
2674 
2675 // Unset current event collection in this thread and reset it with previous
2676 // collector.
2677 void JvmtiEventCollector::unset_jvmti_thread_state() {




2678   JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state();
2679   if (state != NULL) {
2680     // restore the previous event collector (if any)
2681     if (is_vm_object_alloc_event()) {
2682       if (state->get_vm_object_alloc_event_collector() == this) {
2683         state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev);
2684       } else {
2685         // this thread's jvmti state was created during the scope of
2686         // the event collector.
2687       }
2688     } else {
2689       if (is_dynamic_code_event()) {
2690         if (state->get_dynamic_code_event_collector() == this) {
2691           state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev);
2692         } else {
2693           // this thread's jvmti state was created during the scope of
2694           // the event collector.
2695         }






2696       }
2697     }
2698   }
2699 }
2700 
2701 // create the dynamic code event collector
2702 JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(NULL) {
2703   if (JvmtiExport::should_post_dynamic_code_generated()) {
2704     setup_jvmti_thread_state();
2705   }
2706 }
2707 
2708 // iterate over any code blob descriptors collected and post a
2709 // DYNAMIC_CODE_GENERATED event to the profiler.
2710 JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() {
2711   assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events");
2712  // iterate over any code blob descriptors that we collected
2713  if (_code_blobs != NULL) {
2714    for (int i=0; i<_code_blobs->length(); i++) {
2715      JvmtiCodeBlobDesc* blob = _code_blobs->at(i);
2716      JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end());
2717      FreeHeap(blob);
2718    }
2719    delete _code_blobs;
2720  }
2721  unset_jvmti_thread_state();
2722 }
2723 
2724 // register a stub
2725 void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) {
2726  if (_code_blobs == NULL) {
2727    _code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(1,true);
2728  }
2729  _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end));
2730 }
2731 
2732 // Setup current thread to record vm allocated objects.
2733 JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() : _allocated(NULL) {
2734   if (JvmtiExport::should_post_vm_object_alloc()) {
2735     _enable = true;
2736     setup_jvmti_thread_state();
2737   } else {
2738     _enable = false;
2739   }
2740 }
2741 
2742 // Post vm_object_alloc event for vm allocated objects visible to java
2743 // world.
2744 JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() {
2745   if (_allocated != NULL) {
2746     set_enabled(false);
2747     for (int i = 0; i < _allocated->length(); i++) {
2748       oop obj = _allocated->at(i);
2749       JvmtiExport::post_vm_object_alloc(JavaThread::current(), obj);
2750     }
2751     delete _allocated;
2752   }
2753   unset_jvmti_thread_state();
2754 }
2755 
2756 void JvmtiVMObjectAllocEventCollector::record_allocation(oop obj) {
2757   assert(is_enabled(), "VM object alloc event collector is not enabled");
2758   if (_allocated == NULL) {
2759     _allocated = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(1, true);
2760   }
2761   _allocated->push(obj);
2762 }
2763 
2764 // GC support.
2765 void JvmtiVMObjectAllocEventCollector::oops_do(OopClosure* f) {
2766   if (_allocated != NULL) {
2767     for(int i=_allocated->length() - 1; i >= 0; i--) {
2768       if (_allocated->at(i) != NULL) {
2769         f->do_oop(_allocated->adr_at(i));
2770       }
2771     }
2772   }
2773 }
2774 
2775 void JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(OopClosure* f) {
2776   // no-op if jvmti not enabled
2777   if (!JvmtiEnv::environments_might_exist()) {
2778     return;
2779   }
2780 
2781   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jthr = jtiwh.next(); ) {
2782     JvmtiThreadState *state = jthr->jvmti_thread_state();
2783     if (state != NULL) {
2784       JvmtiVMObjectAllocEventCollector *collector;
2785       collector = state->get_vm_object_alloc_event_collector();
2786       while (collector != NULL) {
2787         collector->oops_do(f);
2788         collector = (JvmtiVMObjectAllocEventCollector *)collector->get_prev();






2789       }
2790     }
2791   }
2792 }
2793 
2794 
2795 // Disable collection of VMObjectAlloc events
2796 NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) {
2797   // a no-op if VMObjectAlloc event is not enabled
2798   if (!JvmtiExport::should_post_vm_object_alloc()) {
2799     return;
2800   }
2801   Thread* thread = Thread::current_or_null();
2802   if (thread != NULL && thread->is_Java_thread())  {
2803     JavaThread* current_thread = (JavaThread*)thread;
2804     JvmtiThreadState *state = current_thread->jvmti_thread_state();
2805     if (state != NULL) {
2806       JvmtiVMObjectAllocEventCollector *collector;
2807       collector = state->get_vm_object_alloc_event_collector();
2808       if (collector != NULL && collector->is_enabled()) {
2809         _collector = collector;
2810         _collector->set_enabled(false);
2811       }
2812     }
2813   }
2814 }
2815 
2816 // Re-Enable collection of VMObjectAlloc events (if previously enabled)
2817 NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() {
2818   if (was_enabled()) {
2819     _collector->set_enabled(true);
2820   }
2821 };

























































2822 
2823 JvmtiGCMarker::JvmtiGCMarker() {
2824   // if there aren't any JVMTI environments then nothing to do
2825   if (!JvmtiEnv::environments_might_exist()) {
2826     return;
2827   }
2828 
2829   if (JvmtiExport::should_post_garbage_collection_start()) {
2830     JvmtiExport::post_garbage_collection_start();
2831   }
2832 
2833   if (SafepointSynchronize::is_at_safepoint()) {
2834     // Do clean up tasks that need to be done at a safepoint
2835     JvmtiEnvBase::check_for_periodic_clean_up();
2836   }
2837 }
2838 
2839 JvmtiGCMarker::~JvmtiGCMarker() {
2840   // if there aren't any JVMTI environments then nothing to do
2841   if (!JvmtiEnv::environments_might_exist()) {


1011   // tries to access nonexistant services.
1012   if (on) {
1013     vm_exit_during_initialization("Java Kernel does not support JVMTI.");
1014   }
1015 }
1016 
1017 
1018 static inline Klass* oop_to_klass(oop obj) {
1019   Klass* k = obj->klass();
1020 
1021   // if the object is a java.lang.Class then return the java mirror
1022   if (k == SystemDictionary::Class_klass()) {
1023     if (!java_lang_Class::is_primitive(obj)) {
1024       k = java_lang_Class::as_Klass(obj);
1025       assert(k != NULL, "class for non-primitive mirror must exist");
1026     }
1027   }
1028   return k;
1029 }
1030 
1031 class JvmtiObjectAllocEventMark : public JvmtiClassEventMark  {
1032  private:
1033    jobject _jobj;
1034    jlong    _size;
1035  public:
1036    JvmtiObjectAllocEventMark(JavaThread *thread, oop obj) : JvmtiClassEventMark(thread, oop_to_klass(obj)) {
1037      _jobj = (jobject)to_jobject(obj);
1038      _size = obj->size() * wordSize;
1039    };
1040    jobject jni_jobject() { return _jobj; }
1041    jlong size() { return _size; }
1042 };
1043 
1044 class JvmtiCompiledMethodLoadEventMark : public JvmtiMethodEventMark {
1045  private:
1046   jint _code_size;
1047   const void *_code_data;
1048   jint _map_length;
1049   jvmtiAddrLocationMap *_map;
1050   const void *_compile_info;
1051  public:
1052   JvmtiCompiledMethodLoadEventMark(JavaThread *thread, nmethod *nm, void* compile_info_ptr = NULL)
1053           : JvmtiMethodEventMark(thread,methodHandle(thread, nm->method())) {
1054     _code_data = nm->insts_begin();
1055     _code_size = nm->insts_size();
1056     _compile_info = compile_info_ptr; // Set void pointer of compiledMethodLoad Event. Default value is NULL.


1181 bool              JvmtiExport::_should_post_field_modification            = false;
1182 bool              JvmtiExport::_should_post_class_load                    = false;
1183 bool              JvmtiExport::_should_post_class_prepare                 = false;
1184 bool              JvmtiExport::_should_post_class_unload                  = false;
1185 bool              JvmtiExport::_should_post_thread_life                   = false;
1186 bool              JvmtiExport::_should_clean_up_heap_objects              = false;
1187 bool              JvmtiExport::_should_post_native_method_bind            = false;
1188 bool              JvmtiExport::_should_post_dynamic_code_generated        = false;
1189 bool              JvmtiExport::_should_post_data_dump                     = false;
1190 bool              JvmtiExport::_should_post_compiled_method_load          = false;
1191 bool              JvmtiExport::_should_post_compiled_method_unload        = false;
1192 bool              JvmtiExport::_should_post_monitor_contended_enter       = false;
1193 bool              JvmtiExport::_should_post_monitor_contended_entered     = false;
1194 bool              JvmtiExport::_should_post_monitor_wait                  = false;
1195 bool              JvmtiExport::_should_post_monitor_waited                = false;
1196 bool              JvmtiExport::_should_post_garbage_collection_start      = false;
1197 bool              JvmtiExport::_should_post_garbage_collection_finish     = false;
1198 bool              JvmtiExport::_should_post_object_free                   = false;
1199 bool              JvmtiExport::_should_post_resource_exhausted            = false;
1200 bool              JvmtiExport::_should_post_vm_object_alloc               = false;
1201 bool              JvmtiExport::_should_post_sampled_object_alloc          = false;
1202 bool              JvmtiExport::_should_post_on_exceptions                 = false;
1203 
1204 ////////////////////////////////////////////////////////////////////////////////////////////////
1205 
1206 
1207 //
1208 // JVMTI single step management
1209 //
1210 void JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, address location) {
1211   assert(JvmtiExport::should_post_single_step(), "must be single stepping");
1212 
1213   HandleMark hm(thread);
1214   methodHandle mh(thread, method);
1215 
1216   // update information about current location and post a step event
1217   JvmtiThreadState *state = thread->jvmti_thread_state();
1218   if (state == NULL) {
1219     return;
1220   }
1221   EVT_TRIG_TRACE(JVMTI_EVENT_SINGLE_STEP, ("[%s] Trg Single Step triggered",


2264 {
2265   // register the stub with the current dynamic code event collector
2266   JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
2267   // state can only be NULL if the current thread is exiting which
2268   // should not happen since we're trying to post an event
2269   guarantee(state != NULL, "attempt to register stub via an exiting thread");
2270   JvmtiDynamicCodeEventCollector* collector = state->get_dynamic_code_event_collector();
2271   guarantee(collector != NULL, "attempt to register stub without event collector");
2272   collector->register_stub(name, code_begin, code_end);
2273 }
2274 
2275 // Collect all the vm internally allocated objects which are visible to java world
2276 void JvmtiExport::record_vm_internal_object_allocation(oop obj) {
2277   Thread* thread = Thread::current_or_null();
2278   if (thread != NULL && thread->is_Java_thread())  {
2279     // Can not take safepoint here.
2280     NoSafepointVerifier no_sfpt;
2281     // Can not take safepoint here so can not use state_for to get
2282     // jvmti thread state.
2283     JvmtiThreadState *state = ((JavaThread*)thread)->jvmti_thread_state();
2284     if (state != NULL) {
2285       // state is non NULL when VMObjectAllocEventCollector is enabled.
2286       JvmtiVMObjectAllocEventCollector *collector;
2287       collector = state->get_vm_object_alloc_event_collector();
2288       if (collector != NULL && collector->is_enabled()) {
2289         // Don't record classes as these will be notified via the ClassLoad
2290         // event.
2291         if (obj->klass() != SystemDictionary::Class_klass()) {
2292           collector->record_allocation(obj);
2293         }
2294       }
2295     }
2296   }
2297 }
2298 
2299 // Collect all the sampled allocated objects.
2300 void JvmtiExport::record_sampled_internal_object_allocation(oop obj) {
2301   Thread* thread = Thread::current_or_null();
2302   if (thread != NULL && thread->is_Java_thread())  {
2303     // Can not take safepoint here.
2304     NoSafepointVerifier no_sfpt;
2305     // Can not take safepoint here so can not use state_for to get
2306     // jvmti thread state.
2307     JvmtiThreadState *state = ((JavaThread*)thread)->jvmti_thread_state();
2308     if (state != NULL) {
2309       // state is non NULL when SampledObjectAllocEventCollector is enabled.
2310       JvmtiSampledObjectAllocEventCollector *collector;
2311       collector = state->get_sampled_object_alloc_event_collector();
2312 
2313       if (collector != NULL && collector->is_enabled()) {
2314         collector->record_allocation(obj);
2315       }
2316     }
2317   }
2318 }
2319 
2320 void JvmtiExport::post_garbage_collection_finish() {
2321   Thread *thread = Thread::current(); // this event is posted from VM-Thread.
2322   EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2323                  ("[%s] garbage collection finish event triggered",
2324                   JvmtiTrace::safe_get_thread_name(thread)));
2325   JvmtiEnvIterator it;
2326   for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2327     if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH)) {
2328       EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2329                 ("[%s] garbage collection finish event sent",
2330                  JvmtiTrace::safe_get_thread_name(thread)));
2331       JvmtiThreadEventTransition jet(thread);
2332       // JNIEnv is NULL here because this event is posted from VM Thread
2333       jvmtiEventGarbageCollectionFinish callback = env->callbacks()->GarbageCollectionFinish;
2334       if (callback != NULL) {
2335         (*callback)(env->jvmti_external());
2336       }
2337     }
2338   }
2339 }


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

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


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

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





2805 }
2806 
2807 // Post vm_object_alloc event for vm allocated objects visible to java
2808 // world.
2809 void JvmtiObjectAllocEventCollector::generate_call_for_allocated() {
2810   if (_allocated) {
2811     set_enabled(false);
2812     for (int i = 0; i < _allocated->length(); i++) {
2813       oop obj = _allocated->at(i);
2814       _post_callback(JavaThread::current(), obj);
2815     }
2816     delete _allocated, _allocated = NULL;
2817   }

2818 }
2819 
2820 void JvmtiObjectAllocEventCollector::record_allocation(oop obj) {
2821   assert(is_enabled(), "Object alloc event collector is not enabled");
2822   if (_allocated == NULL) {
2823     _allocated = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(1, true);
2824   }
2825   _allocated->push(obj);
2826 }
2827 
2828 // GC support.
2829 void JvmtiObjectAllocEventCollector::oops_do(OopClosure* f) {
2830   if (_allocated) {
2831     for(int i = _allocated->length() - 1; i >= 0; i--) {
2832       if (_allocated->at(i) != NULL) {
2833         f->do_oop(_allocated->adr_at(i));
2834       }
2835     }
2836   }
2837 }
2838 
2839 void JvmtiObjectAllocEventCollector::oops_do_for_all_threads(OopClosure* f) {
2840   // no-op if jvmti not enabled
2841   if (!JvmtiEnv::environments_might_exist()) {
2842     return;
2843   }
2844 
2845   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jthr = jtiwh.next(); ) {
2846     JvmtiThreadState *state = jthr->jvmti_thread_state();
2847     if (state != NULL) {
2848       JvmtiObjectAllocEventCollector *collector;
2849       collector = state->get_vm_object_alloc_event_collector();
2850       while (collector != NULL) {
2851         collector->oops_do(f);
2852         collector = (JvmtiObjectAllocEventCollector*) collector->get_prev();
2853       }
2854 
2855       collector = state->get_sampled_object_alloc_event_collector();
2856       while (collector != NULL) {
2857         collector->oops_do(f);
2858         collector = (JvmtiObjectAllocEventCollector*) collector->get_prev();
2859       }
2860     }
2861   }
2862 }
2863 
2864 
2865 // Disable collection of VMObjectAlloc events
2866 NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) {
2867   // a no-op if VMObjectAlloc event is not enabled
2868   if (!JvmtiExport::should_post_vm_object_alloc()) {
2869     return;
2870   }
2871   Thread* thread = Thread::current_or_null();
2872   if (thread != NULL && thread->is_Java_thread())  {
2873     JavaThread* current_thread = (JavaThread*)thread;
2874     JvmtiThreadState *state = current_thread->jvmti_thread_state();
2875     if (state != NULL) {
2876       JvmtiVMObjectAllocEventCollector *collector;
2877       collector = state->get_vm_object_alloc_event_collector();
2878       if (collector != NULL && collector->is_enabled()) {
2879         _collector = collector;
2880         _collector->set_enabled(false);
2881       }
2882     }
2883   }
2884 }
2885 
2886 // Re-Enable collection of VMObjectAlloc events (if previously enabled)
2887 NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() {
2888   if (was_enabled()) {
2889     _collector->set_enabled(true);
2890   }
2891 };
2892 
2893 // Setup current thread to record vm allocated objects.
2894 JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() {
2895   if (JvmtiExport::should_post_vm_object_alloc()) {
2896     _enable = true;
2897     setup_jvmti_thread_state();
2898     _post_callback = JvmtiExport::post_vm_object_alloc;
2899   }
2900 }
2901 
2902 JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() {
2903   if (_enable) {
2904     generate_call_for_allocated();
2905   }
2906   unset_jvmti_thread_state();
2907 }
2908 
2909 bool JvmtiSampledObjectAllocEventCollector::object_alloc_is_safe_to_sample() {
2910   Thread* thread = Thread::current();
2911   // Really only sample allocations if this is a JavaThread and not the compiler
2912   // thread.
2913   if (!thread->is_Java_thread() || thread->is_Compiler_thread()) {
2914     return false;
2915   }
2916 
2917   if (Compile_lock->owner() == thread ||
2918       MultiArray_lock->owner() == thread) {
2919     return false;
2920   }
2921   return true;
2922 }
2923 
2924 // Setup current thread to record sampled allocated objects.
2925 JvmtiSampledObjectAllocEventCollector::JvmtiSampledObjectAllocEventCollector() {
2926   if (JvmtiExport::should_post_sampled_object_alloc()) {
2927     if (!object_alloc_is_safe_to_sample()) {
2928       return;
2929     }
2930 
2931     _enable = true;
2932     setup_jvmti_thread_state();
2933     _post_callback = JvmtiExport::post_sampled_object_alloc;
2934   }
2935 }
2936 
2937 JvmtiSampledObjectAllocEventCollector::~JvmtiSampledObjectAllocEventCollector() {
2938   if (!_enable) {
2939     return;
2940   }
2941 
2942   generate_call_for_allocated();
2943   unset_jvmti_thread_state();
2944 
2945   // Unset the sampling collector as present in assertion mode only.
2946   assert(Thread::current()->is_Java_thread(),
2947          "Should always be in a Java thread");
2948 }
2949 
2950 JvmtiGCMarker::JvmtiGCMarker() {
2951   // if there aren't any JVMTI environments then nothing to do
2952   if (!JvmtiEnv::environments_might_exist()) {
2953     return;
2954   }
2955 
2956   if (JvmtiExport::should_post_garbage_collection_start()) {
2957     JvmtiExport::post_garbage_collection_start();
2958   }
2959 
2960   if (SafepointSynchronize::is_at_safepoint()) {
2961     // Do clean up tasks that need to be done at a safepoint
2962     JvmtiEnvBase::check_for_periodic_clean_up();
2963   }
2964 }
2965 
2966 JvmtiGCMarker::~JvmtiGCMarker() {
2967   // if there aren't any JVMTI environments then nothing to do
2968   if (!JvmtiEnv::environments_might_exist()) {
< prev index next >