Fri, 25 Jan 2013 10:04:08 -0500
8000692: Remove old KERNEL code
Summary: Removed depreciated kernel VM source code from hotspot VM
Reviewed-by: dholmes, acorn
1 /*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "code/nmethod.hpp"
28 #include "code/pcDesc.hpp"
29 #include "code/scopeDesc.hpp"
30 #include "interpreter/interpreter.hpp"
31 #include "jvmtifiles/jvmtiEnv.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "oops/objArrayKlass.hpp"
34 #include "oops/objArrayOop.hpp"
35 #include "prims/jvmtiCodeBlobEvents.hpp"
36 #include "prims/jvmtiEventController.hpp"
37 #include "prims/jvmtiEventController.inline.hpp"
38 #include "prims/jvmtiExport.hpp"
39 #include "prims/jvmtiImpl.hpp"
40 #include "prims/jvmtiManageCapabilities.hpp"
41 #include "prims/jvmtiRawMonitor.hpp"
42 #include "prims/jvmtiTagMap.hpp"
43 #include "prims/jvmtiThreadState.inline.hpp"
44 #include "runtime/arguments.hpp"
45 #include "runtime/handles.hpp"
46 #include "runtime/interfaceSupport.hpp"
47 #include "runtime/objectMonitor.hpp"
48 #include "runtime/objectMonitor.inline.hpp"
49 #include "runtime/thread.hpp"
50 #include "runtime/vframe.hpp"
51 #include "services/attachListener.hpp"
52 #include "services/serviceUtil.hpp"
53 #ifndef SERIALGC
54 #include "gc_implementation/parallelScavenge/psMarkSweep.hpp"
55 #endif
57 #ifdef JVMTI_TRACE
58 #define EVT_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_SENT) != 0) { SafeResourceMark rm; tty->print_cr out; }
59 #define EVT_TRIG_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_TRIGGER) != 0) { SafeResourceMark rm; tty->print_cr out; }
60 #else
61 #define EVT_TRIG_TRACE(evt,out)
62 #define EVT_TRACE(evt,out)
63 #endif
65 ///////////////////////////////////////////////////////////////
66 //
67 // JvmtiEventTransition
68 //
69 // TO DO --
70 // more handle purging
72 // Use this for JavaThreads and state is _thread_in_vm.
73 class JvmtiJavaThreadEventTransition : StackObj {
74 private:
75 ResourceMark _rm;
76 ThreadToNativeFromVM _transition;
77 HandleMark _hm;
79 public:
80 JvmtiJavaThreadEventTransition(JavaThread *thread) :
81 _rm(),
82 _transition(thread),
83 _hm(thread) {};
84 };
86 // For JavaThreads which are not in _thread_in_vm state
87 // and other system threads use this.
88 class JvmtiThreadEventTransition : StackObj {
89 private:
90 ResourceMark _rm;
91 HandleMark _hm;
92 JavaThreadState _saved_state;
93 JavaThread *_jthread;
95 public:
96 JvmtiThreadEventTransition(Thread *thread) : _rm(), _hm() {
97 if (thread->is_Java_thread()) {
98 _jthread = (JavaThread *)thread;
99 _saved_state = _jthread->thread_state();
100 if (_saved_state == _thread_in_Java) {
101 ThreadStateTransition::transition_from_java(_jthread, _thread_in_native);
102 } else {
103 ThreadStateTransition::transition(_jthread, _saved_state, _thread_in_native);
104 }
105 } else {
106 _jthread = NULL;
107 }
108 }
110 ~JvmtiThreadEventTransition() {
111 if (_jthread != NULL)
112 ThreadStateTransition::transition_from_native(_jthread, _saved_state);
113 }
114 };
117 ///////////////////////////////////////////////////////////////
118 //
119 // JvmtiEventMark
120 //
122 class JvmtiEventMark : public StackObj {
123 private:
124 JavaThread *_thread;
125 JNIEnv* _jni_env;
126 bool _exception_detected;
127 bool _exception_caught;
128 #if 0
129 JNIHandleBlock* _hblock;
130 #endif
132 public:
133 JvmtiEventMark(JavaThread *thread) : _thread(thread),
134 _jni_env(thread->jni_environment()) {
135 #if 0
136 _hblock = thread->active_handles();
137 _hblock->clear_thoroughly(); // so we can be safe
138 #else
139 // we want to use the code above - but that needs the JNIHandle changes - later...
140 // for now, steal JNI push local frame code
141 JvmtiThreadState *state = thread->jvmti_thread_state();
142 // we are before an event.
143 // Save current jvmti thread exception state.
144 if (state != NULL) {
145 _exception_detected = state->is_exception_detected();
146 _exception_caught = state->is_exception_caught();
147 } else {
148 _exception_detected = false;
149 _exception_caught = false;
150 }
152 JNIHandleBlock* old_handles = thread->active_handles();
153 JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread);
154 assert(new_handles != NULL, "should not be NULL");
155 new_handles->set_pop_frame_link(old_handles);
156 thread->set_active_handles(new_handles);
157 #endif
158 assert(thread == JavaThread::current(), "thread must be current!");
159 thread->frame_anchor()->make_walkable(thread);
160 };
162 ~JvmtiEventMark() {
163 #if 0
164 _hblock->clear(); // for consistency with future correct behavior
165 #else
166 // we want to use the code above - but that needs the JNIHandle changes - later...
167 // for now, steal JNI pop local frame code
168 JNIHandleBlock* old_handles = _thread->active_handles();
169 JNIHandleBlock* new_handles = old_handles->pop_frame_link();
170 assert(new_handles != NULL, "should not be NULL");
171 _thread->set_active_handles(new_handles);
172 // Note that we set the pop_frame_link to NULL explicitly, otherwise
173 // the release_block call will release the blocks.
174 old_handles->set_pop_frame_link(NULL);
175 JNIHandleBlock::release_block(old_handles, _thread); // may block
176 #endif
178 JvmtiThreadState* state = _thread->jvmti_thread_state();
179 // we are continuing after an event.
180 if (state != NULL) {
181 // Restore the jvmti thread exception state.
182 if (_exception_detected) {
183 state->set_exception_detected();
184 }
185 if (_exception_caught) {
186 state->set_exception_caught();
187 }
188 }
189 }
191 #if 0
192 jobject to_jobject(oop obj) { return obj == NULL? NULL : _hblock->allocate_handle_fast(obj); }
193 #else
194 // we want to use the code above - but that needs the JNIHandle changes - later...
195 // for now, use regular make_local
196 jobject to_jobject(oop obj) { return JNIHandles::make_local(_thread,obj); }
197 #endif
199 jclass to_jclass(Klass* klass) { return (klass == NULL ? NULL : (jclass)to_jobject(klass->java_mirror())); }
201 jmethodID to_jmethodID(methodHandle method) { return method->jmethod_id(); }
203 JNIEnv* jni_env() { return _jni_env; }
204 };
206 class JvmtiThreadEventMark : public JvmtiEventMark {
207 private:
208 jthread _jt;
210 public:
211 JvmtiThreadEventMark(JavaThread *thread) :
212 JvmtiEventMark(thread) {
213 _jt = (jthread)(to_jobject(thread->threadObj()));
214 };
215 jthread jni_thread() { return _jt; }
216 };
218 class JvmtiClassEventMark : public JvmtiThreadEventMark {
219 private:
220 jclass _jc;
222 public:
223 JvmtiClassEventMark(JavaThread *thread, Klass* klass) :
224 JvmtiThreadEventMark(thread) {
225 _jc = to_jclass(klass);
226 };
227 jclass jni_class() { return _jc; }
228 };
230 class JvmtiMethodEventMark : public JvmtiThreadEventMark {
231 private:
232 jmethodID _mid;
234 public:
235 JvmtiMethodEventMark(JavaThread *thread, methodHandle method) :
236 JvmtiThreadEventMark(thread),
237 _mid(to_jmethodID(method)) {};
238 jmethodID jni_methodID() { return _mid; }
239 };
241 class JvmtiLocationEventMark : public JvmtiMethodEventMark {
242 private:
243 jlocation _loc;
245 public:
246 JvmtiLocationEventMark(JavaThread *thread, methodHandle method, address location) :
247 JvmtiMethodEventMark(thread, method),
248 _loc(location - method->code_base()) {};
249 jlocation location() { return _loc; }
250 };
252 class JvmtiExceptionEventMark : public JvmtiLocationEventMark {
253 private:
254 jobject _exc;
256 public:
257 JvmtiExceptionEventMark(JavaThread *thread, methodHandle method, address location, Handle exception) :
258 JvmtiLocationEventMark(thread, method, location),
259 _exc(to_jobject(exception())) {};
260 jobject exception() { return _exc; }
261 };
263 class JvmtiClassFileLoadEventMark : public JvmtiThreadEventMark {
264 private:
265 const char *_class_name;
266 jobject _jloader;
267 jobject _protection_domain;
268 jclass _class_being_redefined;
270 public:
271 JvmtiClassFileLoadEventMark(JavaThread *thread, Symbol* name,
272 Handle class_loader, Handle prot_domain, KlassHandle *class_being_redefined) : JvmtiThreadEventMark(thread) {
273 _class_name = name != NULL? name->as_utf8() : NULL;
274 _jloader = (jobject)to_jobject(class_loader());
275 _protection_domain = (jobject)to_jobject(prot_domain());
276 if (class_being_redefined == NULL) {
277 _class_being_redefined = NULL;
278 } else {
279 _class_being_redefined = (jclass)to_jclass((*class_being_redefined)());
280 }
281 };
282 const char *class_name() {
283 return _class_name;
284 }
285 jobject jloader() {
286 return _jloader;
287 }
288 jobject protection_domain() {
289 return _protection_domain;
290 }
291 jclass class_being_redefined() {
292 return _class_being_redefined;
293 }
294 };
296 //////////////////////////////////////////////////////////////////////////////
298 int JvmtiExport::_field_access_count = 0;
299 int JvmtiExport::_field_modification_count = 0;
301 bool JvmtiExport::_can_access_local_variables = false;
302 bool JvmtiExport::_can_hotswap_or_post_breakpoint = false;
303 bool JvmtiExport::_can_modify_any_class = false;
304 bool JvmtiExport::_can_walk_any_space = false;
306 bool JvmtiExport::_has_redefined_a_class = false;
307 bool JvmtiExport::_all_dependencies_are_recorded = false;
309 //
310 // field access management
311 //
313 // interpreter generator needs the address of the counter
314 address JvmtiExport::get_field_access_count_addr() {
315 // We don't grab a lock because we don't want to
316 // serialize field access between all threads. This means that a
317 // thread on another processor can see the wrong count value and
318 // may either miss making a needed call into post_field_access()
319 // or will make an unneeded call into post_field_access(). We pay
320 // this price to avoid slowing down the VM when we aren't watching
321 // field accesses.
322 // Other access/mutation safe by virtue of being in VM state.
323 return (address)(&_field_access_count);
324 }
326 //
327 // field modification management
328 //
330 // interpreter generator needs the address of the counter
331 address JvmtiExport::get_field_modification_count_addr() {
332 // We don't grab a lock because we don't
333 // want to serialize field modification between all threads. This
334 // means that a thread on another processor can see the wrong
335 // count value and may either miss making a needed call into
336 // post_field_modification() or will make an unneeded call into
337 // post_field_modification(). We pay this price to avoid slowing
338 // down the VM when we aren't watching field modifications.
339 // Other access/mutation safe by virtue of being in VM state.
340 return (address)(&_field_modification_count);
341 }
344 ///////////////////////////////////////////////////////////////
345 // Functions needed by java.lang.instrument for starting up javaagent.
346 ///////////////////////////////////////////////////////////////
348 jint
349 JvmtiExport::get_jvmti_interface(JavaVM *jvm, void **penv, jint version) {
350 // The JVMTI_VERSION_INTERFACE_JVMTI part of the version number
351 // has already been validated in JNI GetEnv().
352 int major, minor, micro;
354 // micro version doesn't matter here (yet?)
355 decode_version_values(version, &major, &minor, µ);
356 switch (major) {
357 case 1:
358 switch (minor) {
359 case 0: // version 1.0.<micro> is recognized
360 case 1: // version 1.1.<micro> is recognized
361 case 2: // version 1.2.<micro> is recognized
362 break;
364 default:
365 return JNI_EVERSION; // unsupported minor version number
366 }
367 break;
368 default:
369 return JNI_EVERSION; // unsupported major version number
370 }
372 if (JvmtiEnv::get_phase() == JVMTI_PHASE_LIVE) {
373 JavaThread* current_thread = (JavaThread*) ThreadLocalStorage::thread();
374 // transition code: native to VM
375 ThreadInVMfromNative __tiv(current_thread);
376 VM_ENTRY_BASE(jvmtiEnv*, JvmtiExport::get_jvmti_interface, current_thread)
377 debug_only(VMNativeEntryWrapper __vew;)
379 JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version);
380 *penv = jvmti_env->jvmti_external(); // actual type is jvmtiEnv* -- not to be confused with JvmtiEnv*
381 return JNI_OK;
383 } else if (JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
384 // not live, no thread to transition
385 JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version);
386 *penv = jvmti_env->jvmti_external(); // actual type is jvmtiEnv* -- not to be confused with JvmtiEnv*
387 return JNI_OK;
389 } else {
390 // Called at the wrong time
391 *penv = NULL;
392 return JNI_EDETACHED;
393 }
394 }
397 void
398 JvmtiExport::decode_version_values(jint version, int * major, int * minor,
399 int * micro) {
400 *major = (version & JVMTI_VERSION_MASK_MAJOR) >> JVMTI_VERSION_SHIFT_MAJOR;
401 *minor = (version & JVMTI_VERSION_MASK_MINOR) >> JVMTI_VERSION_SHIFT_MINOR;
402 *micro = (version & JVMTI_VERSION_MASK_MICRO) >> JVMTI_VERSION_SHIFT_MICRO;
403 }
405 void JvmtiExport::enter_primordial_phase() {
406 JvmtiEnvBase::set_phase(JVMTI_PHASE_PRIMORDIAL);
407 }
409 void JvmtiExport::enter_start_phase() {
410 JvmtiManageCapabilities::recompute_always_capabilities();
411 JvmtiEnvBase::set_phase(JVMTI_PHASE_START);
412 }
414 void JvmtiExport::enter_onload_phase() {
415 JvmtiEnvBase::set_phase(JVMTI_PHASE_ONLOAD);
416 }
418 void JvmtiExport::enter_live_phase() {
419 JvmtiEnvBase::set_phase(JVMTI_PHASE_LIVE);
420 }
422 //
423 // JVMTI events that the VM posts to the debugger and also startup agent
424 // and call the agent's premain() for java.lang.instrument.
425 //
427 void JvmtiExport::post_vm_start() {
428 EVT_TRIG_TRACE(JVMTI_EVENT_VM_START, ("JVMTI Trg VM start event triggered" ));
430 // can now enable some events
431 JvmtiEventController::vm_start();
433 JvmtiEnvIterator it;
434 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
435 if (env->is_enabled(JVMTI_EVENT_VM_START)) {
436 EVT_TRACE(JVMTI_EVENT_VM_START, ("JVMTI Evt VM start event sent" ));
438 JavaThread *thread = JavaThread::current();
439 JvmtiThreadEventMark jem(thread);
440 JvmtiJavaThreadEventTransition jet(thread);
441 jvmtiEventVMStart callback = env->callbacks()->VMStart;
442 if (callback != NULL) {
443 (*callback)(env->jvmti_external(), jem.jni_env());
444 }
445 }
446 }
447 }
450 void JvmtiExport::post_vm_initialized() {
451 EVT_TRIG_TRACE(JVMTI_EVENT_VM_INIT, ("JVMTI Trg VM init event triggered" ));
453 // can now enable events
454 JvmtiEventController::vm_init();
456 JvmtiEnvIterator it;
457 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
458 if (env->is_enabled(JVMTI_EVENT_VM_INIT)) {
459 EVT_TRACE(JVMTI_EVENT_VM_INIT, ("JVMTI Evt VM init event sent" ));
461 JavaThread *thread = JavaThread::current();
462 JvmtiThreadEventMark jem(thread);
463 JvmtiJavaThreadEventTransition jet(thread);
464 jvmtiEventVMInit callback = env->callbacks()->VMInit;
465 if (callback != NULL) {
466 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
467 }
468 }
469 }
470 }
473 void JvmtiExport::post_vm_death() {
474 EVT_TRIG_TRACE(JVMTI_EVENT_VM_DEATH, ("JVMTI Trg VM death event triggered" ));
476 JvmtiEnvIterator it;
477 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
478 if (env->is_enabled(JVMTI_EVENT_VM_DEATH)) {
479 EVT_TRACE(JVMTI_EVENT_VM_DEATH, ("JVMTI Evt VM death event sent" ));
481 JavaThread *thread = JavaThread::current();
482 JvmtiEventMark jem(thread);
483 JvmtiJavaThreadEventTransition jet(thread);
484 jvmtiEventVMDeath callback = env->callbacks()->VMDeath;
485 if (callback != NULL) {
486 (*callback)(env->jvmti_external(), jem.jni_env());
487 }
488 }
489 }
491 JvmtiEnvBase::set_phase(JVMTI_PHASE_DEAD);
492 JvmtiEventController::vm_death();
493 }
495 char**
496 JvmtiExport::get_all_native_method_prefixes(int* count_ptr) {
497 // Have to grab JVMTI thread state lock to be sure environment doesn't
498 // go away while we iterate them. No locks during VM bring-up.
499 if (Threads::number_of_threads() == 0 || SafepointSynchronize::is_at_safepoint()) {
500 return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr);
501 } else {
502 MutexLocker mu(JvmtiThreadState_lock);
503 return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr);
504 }
505 }
507 class JvmtiClassFileLoadHookPoster : public StackObj {
508 private:
509 Symbol* _h_name;
510 Handle _class_loader;
511 Handle _h_protection_domain;
512 unsigned char ** _data_ptr;
513 unsigned char ** _end_ptr;
514 JavaThread * _thread;
515 jint _curr_len;
516 unsigned char * _curr_data;
517 JvmtiEnv * _curr_env;
518 jint * _cached_length_ptr;
519 unsigned char ** _cached_data_ptr;
520 JvmtiThreadState * _state;
521 KlassHandle * _h_class_being_redefined;
522 JvmtiClassLoadKind _load_kind;
524 public:
525 inline JvmtiClassFileLoadHookPoster(Symbol* h_name, Handle class_loader,
526 Handle h_protection_domain,
527 unsigned char **data_ptr, unsigned char **end_ptr,
528 unsigned char **cached_data_ptr,
529 jint *cached_length_ptr) {
530 _h_name = h_name;
531 _class_loader = class_loader;
532 _h_protection_domain = h_protection_domain;
533 _data_ptr = data_ptr;
534 _end_ptr = end_ptr;
535 _thread = JavaThread::current();
536 _curr_len = *end_ptr - *data_ptr;
537 _curr_data = *data_ptr;
538 _curr_env = NULL;
539 _cached_length_ptr = cached_length_ptr;
540 _cached_data_ptr = cached_data_ptr;
542 _state = _thread->jvmti_thread_state();
543 if (_state != NULL) {
544 _h_class_being_redefined = _state->get_class_being_redefined();
545 _load_kind = _state->get_class_load_kind();
546 // Clear class_being_redefined flag here. The action
547 // from agent handler could generate a new class file load
548 // hook event and if it is not cleared the new event generated
549 // from regular class file load could have this stale redefined
550 // class handle info.
551 _state->clear_class_being_redefined();
552 } else {
553 // redefine and retransform will always set the thread state
554 _h_class_being_redefined = (KlassHandle *) NULL;
555 _load_kind = jvmti_class_load_kind_load;
556 }
557 }
559 void post() {
560 // EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
561 // ("JVMTI [%s] class file load hook event triggered",
562 // JvmtiTrace::safe_get_thread_name(_thread)));
563 post_all_envs();
564 copy_modified_data();
565 }
567 private:
568 void post_all_envs() {
569 if (_load_kind != jvmti_class_load_kind_retransform) {
570 // for class load and redefine,
571 // call the non-retransformable agents
572 JvmtiEnvIterator it;
573 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
574 if (!env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) {
575 // non-retransformable agents cannot retransform back,
576 // so no need to cache the original class file bytes
577 post_to_env(env, false);
578 }
579 }
580 }
581 JvmtiEnvIterator it;
582 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
583 // retransformable agents get all events
584 if (env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) {
585 // retransformable agents need to cache the original class file
586 // bytes if changes are made via the ClassFileLoadHook
587 post_to_env(env, true);
588 }
589 }
590 }
592 void post_to_env(JvmtiEnv* env, bool caching_needed) {
593 unsigned char *new_data = NULL;
594 jint new_len = 0;
595 // EVT_TRACE(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
596 // ("JVMTI [%s] class file load hook event sent %s data_ptr = %d, data_len = %d",
597 // JvmtiTrace::safe_get_thread_name(_thread),
598 // _h_name == NULL ? "NULL" : _h_name->as_utf8(),
599 // _curr_data, _curr_len ));
600 JvmtiClassFileLoadEventMark jem(_thread, _h_name, _class_loader,
601 _h_protection_domain,
602 _h_class_being_redefined);
603 JvmtiJavaThreadEventTransition jet(_thread);
604 JNIEnv* jni_env = (JvmtiEnv::get_phase() == JVMTI_PHASE_PRIMORDIAL)?
605 NULL : jem.jni_env();
606 jvmtiEventClassFileLoadHook callback = env->callbacks()->ClassFileLoadHook;
607 if (callback != NULL) {
608 (*callback)(env->jvmti_external(), jni_env,
609 jem.class_being_redefined(),
610 jem.jloader(), jem.class_name(),
611 jem.protection_domain(),
612 _curr_len, _curr_data,
613 &new_len, &new_data);
614 }
615 if (new_data != NULL) {
616 // this agent has modified class data.
617 if (caching_needed && *_cached_data_ptr == NULL) {
618 // data has been changed by the new retransformable agent
619 // and it hasn't already been cached, cache it
620 *_cached_data_ptr = (unsigned char *)os::malloc(_curr_len, mtInternal);
621 memcpy(*_cached_data_ptr, _curr_data, _curr_len);
622 *_cached_length_ptr = _curr_len;
623 }
625 if (_curr_data != *_data_ptr) {
626 // curr_data is previous agent modified class data.
627 // And this has been changed by the new agent so
628 // we can delete it now.
629 _curr_env->Deallocate(_curr_data);
630 }
632 // Class file data has changed by the current agent.
633 _curr_data = new_data;
634 _curr_len = new_len;
635 // Save the current agent env we need this to deallocate the
636 // memory allocated by this agent.
637 _curr_env = env;
638 }
639 }
641 void copy_modified_data() {
642 // if one of the agent has modified class file data.
643 // Copy modified class data to new resources array.
644 if (_curr_data != *_data_ptr) {
645 *_data_ptr = NEW_RESOURCE_ARRAY(u1, _curr_len);
646 memcpy(*_data_ptr, _curr_data, _curr_len);
647 *_end_ptr = *_data_ptr + _curr_len;
648 _curr_env->Deallocate(_curr_data);
649 }
650 }
651 };
653 bool JvmtiExport::_should_post_class_file_load_hook = false;
655 // this entry is for class file load hook on class load, redefine and retransform
656 void JvmtiExport::post_class_file_load_hook(Symbol* h_name,
657 Handle class_loader,
658 Handle h_protection_domain,
659 unsigned char **data_ptr,
660 unsigned char **end_ptr,
661 unsigned char **cached_data_ptr,
662 jint *cached_length_ptr) {
663 JvmtiClassFileLoadHookPoster poster(h_name, class_loader,
664 h_protection_domain,
665 data_ptr, end_ptr,
666 cached_data_ptr,
667 cached_length_ptr);
668 poster.post();
669 }
671 void JvmtiExport::report_unsupported(bool on) {
672 // If any JVMTI service is turned on, we need to exit before native code
673 // tries to access nonexistant services.
674 if (on) {
675 vm_exit_during_initialization("Java Kernel does not support JVMTI.");
676 }
677 }
680 static inline Klass* oop_to_klass(oop obj) {
681 Klass* k = obj->klass();
683 // if the object is a java.lang.Class then return the java mirror
684 if (k == SystemDictionary::Class_klass()) {
685 if (!java_lang_Class::is_primitive(obj)) {
686 k = java_lang_Class::as_Klass(obj);
687 assert(k != NULL, "class for non-primitive mirror must exist");
688 }
689 }
690 return k;
691 }
693 class JvmtiVMObjectAllocEventMark : public JvmtiClassEventMark {
694 private:
695 jobject _jobj;
696 jlong _size;
697 public:
698 JvmtiVMObjectAllocEventMark(JavaThread *thread, oop obj) : JvmtiClassEventMark(thread, oop_to_klass(obj)) {
699 _jobj = (jobject)to_jobject(obj);
700 _size = obj->size() * wordSize;
701 };
702 jobject jni_jobject() { return _jobj; }
703 jlong size() { return _size; }
704 };
706 class JvmtiCompiledMethodLoadEventMark : public JvmtiMethodEventMark {
707 private:
708 jint _code_size;
709 const void *_code_data;
710 jint _map_length;
711 jvmtiAddrLocationMap *_map;
712 const void *_compile_info;
713 public:
714 JvmtiCompiledMethodLoadEventMark(JavaThread *thread, nmethod *nm, void* compile_info_ptr = NULL)
715 : JvmtiMethodEventMark(thread,methodHandle(thread, nm->method())) {
716 _code_data = nm->insts_begin();
717 _code_size = nm->insts_size();
718 _compile_info = compile_info_ptr; // Set void pointer of compiledMethodLoad Event. Default value is NULL.
719 JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nm, &_map, &_map_length);
720 }
721 ~JvmtiCompiledMethodLoadEventMark() {
722 FREE_C_HEAP_ARRAY(jvmtiAddrLocationMap, _map, mtInternal);
723 }
725 jint code_size() { return _code_size; }
726 const void *code_data() { return _code_data; }
727 jint map_length() { return _map_length; }
728 const jvmtiAddrLocationMap* map() { return _map; }
729 const void *compile_info() { return _compile_info; }
730 };
734 class JvmtiMonitorEventMark : public JvmtiThreadEventMark {
735 private:
736 jobject _jobj;
737 public:
738 JvmtiMonitorEventMark(JavaThread *thread, oop object)
739 : JvmtiThreadEventMark(thread){
740 _jobj = to_jobject(object);
741 }
742 jobject jni_object() { return _jobj; }
743 };
745 ///////////////////////////////////////////////////////////////
746 //
747 // pending CompiledMethodUnload support
748 //
750 void JvmtiExport::post_compiled_method_unload(
751 jmethodID method, const void *code_begin) {
752 JavaThread* thread = JavaThread::current();
753 EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
754 ("JVMTI [%s] method compile unload event triggered",
755 JvmtiTrace::safe_get_thread_name(thread)));
757 // post the event for each environment that has this event enabled.
758 JvmtiEnvIterator it;
759 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
760 if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_UNLOAD)) {
762 EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
763 ("JVMTI [%s] class compile method unload event sent jmethodID " PTR_FORMAT,
764 JvmtiTrace::safe_get_thread_name(thread), method));
766 ResourceMark rm(thread);
768 JvmtiEventMark jem(thread);
769 JvmtiJavaThreadEventTransition jet(thread);
770 jvmtiEventCompiledMethodUnload callback = env->callbacks()->CompiledMethodUnload;
771 if (callback != NULL) {
772 (*callback)(env->jvmti_external(), method, code_begin);
773 }
774 }
775 }
776 }
778 ///////////////////////////////////////////////////////////////
779 //
780 // JvmtiExport
781 //
783 void JvmtiExport::post_raw_breakpoint(JavaThread *thread, Method* method, address location) {
784 HandleMark hm(thread);
785 methodHandle mh(thread, method);
787 JvmtiThreadState *state = thread->jvmti_thread_state();
788 if (state == NULL) {
789 return;
790 }
791 EVT_TRIG_TRACE(JVMTI_EVENT_BREAKPOINT, ("JVMTI [%s] Trg Breakpoint triggered",
792 JvmtiTrace::safe_get_thread_name(thread)));
793 JvmtiEnvThreadStateIterator it(state);
794 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
795 ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_BREAKPOINT);
796 if (!ets->breakpoint_posted() && ets->is_enabled(JVMTI_EVENT_BREAKPOINT)) {
797 ThreadState old_os_state = thread->osthread()->get_state();
798 thread->osthread()->set_state(BREAKPOINTED);
799 EVT_TRACE(JVMTI_EVENT_BREAKPOINT, ("JVMTI [%s] Evt Breakpoint sent %s.%s @ %d",
800 JvmtiTrace::safe_get_thread_name(thread),
801 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
802 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
803 location - mh()->code_base() ));
805 JvmtiEnv *env = ets->get_env();
806 JvmtiLocationEventMark jem(thread, mh, location);
807 JvmtiJavaThreadEventTransition jet(thread);
808 jvmtiEventBreakpoint callback = env->callbacks()->Breakpoint;
809 if (callback != NULL) {
810 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
811 jem.jni_methodID(), jem.location());
812 }
814 ets->set_breakpoint_posted();
815 thread->osthread()->set_state(old_os_state);
816 }
817 }
818 }
820 //////////////////////////////////////////////////////////////////////////////
822 bool JvmtiExport::_can_get_source_debug_extension = false;
823 bool JvmtiExport::_can_maintain_original_method_order = false;
824 bool JvmtiExport::_can_post_interpreter_events = false;
825 bool JvmtiExport::_can_post_on_exceptions = false;
826 bool JvmtiExport::_can_post_breakpoint = false;
827 bool JvmtiExport::_can_post_field_access = false;
828 bool JvmtiExport::_can_post_field_modification = false;
829 bool JvmtiExport::_can_post_method_entry = false;
830 bool JvmtiExport::_can_post_method_exit = false;
831 bool JvmtiExport::_can_pop_frame = false;
832 bool JvmtiExport::_can_force_early_return = false;
834 bool JvmtiExport::_should_post_single_step = false;
835 bool JvmtiExport::_should_post_field_access = false;
836 bool JvmtiExport::_should_post_field_modification = false;
837 bool JvmtiExport::_should_post_class_load = false;
838 bool JvmtiExport::_should_post_class_prepare = false;
839 bool JvmtiExport::_should_post_class_unload = false;
840 bool JvmtiExport::_should_post_thread_life = false;
841 bool JvmtiExport::_should_clean_up_heap_objects = false;
842 bool JvmtiExport::_should_post_native_method_bind = false;
843 bool JvmtiExport::_should_post_dynamic_code_generated = false;
844 bool JvmtiExport::_should_post_data_dump = false;
845 bool JvmtiExport::_should_post_compiled_method_load = false;
846 bool JvmtiExport::_should_post_compiled_method_unload = false;
847 bool JvmtiExport::_should_post_monitor_contended_enter = false;
848 bool JvmtiExport::_should_post_monitor_contended_entered = false;
849 bool JvmtiExport::_should_post_monitor_wait = false;
850 bool JvmtiExport::_should_post_monitor_waited = false;
851 bool JvmtiExport::_should_post_garbage_collection_start = false;
852 bool JvmtiExport::_should_post_garbage_collection_finish = false;
853 bool JvmtiExport::_should_post_object_free = false;
854 bool JvmtiExport::_should_post_resource_exhausted = false;
855 bool JvmtiExport::_should_post_vm_object_alloc = false;
856 bool JvmtiExport::_should_post_on_exceptions = false;
858 ////////////////////////////////////////////////////////////////////////////////////////////////
861 //
862 // JVMTI single step management
863 //
864 void JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, address location) {
865 assert(JvmtiExport::should_post_single_step(), "must be single stepping");
867 HandleMark hm(thread);
868 methodHandle mh(thread, method);
870 // update information about current location and post a step event
871 JvmtiThreadState *state = thread->jvmti_thread_state();
872 if (state == NULL) {
873 return;
874 }
875 EVT_TRIG_TRACE(JVMTI_EVENT_SINGLE_STEP, ("JVMTI [%s] Trg Single Step triggered",
876 JvmtiTrace::safe_get_thread_name(thread)));
877 if (!state->hide_single_stepping()) {
878 if (state->is_pending_step_for_popframe()) {
879 state->process_pending_step_for_popframe();
880 }
881 if (state->is_pending_step_for_earlyret()) {
882 state->process_pending_step_for_earlyret();
883 }
884 JvmtiExport::post_single_step(thread, mh(), location);
885 }
886 }
889 void JvmtiExport::expose_single_stepping(JavaThread *thread) {
890 JvmtiThreadState *state = thread->jvmti_thread_state();
891 if (state != NULL) {
892 state->clear_hide_single_stepping();
893 }
894 }
897 bool JvmtiExport::hide_single_stepping(JavaThread *thread) {
898 JvmtiThreadState *state = thread->jvmti_thread_state();
899 if (state != NULL && state->is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
900 state->set_hide_single_stepping();
901 return true;
902 } else {
903 return false;
904 }
905 }
907 void JvmtiExport::post_class_load(JavaThread *thread, Klass* klass) {
908 HandleMark hm(thread);
909 KlassHandle kh(thread, klass);
911 EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_LOAD, ("JVMTI [%s] Trg Class Load triggered",
912 JvmtiTrace::safe_get_thread_name(thread)));
913 JvmtiThreadState* state = thread->jvmti_thread_state();
914 if (state == NULL) {
915 return;
916 }
917 JvmtiEnvThreadStateIterator it(state);
918 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
919 if (ets->is_enabled(JVMTI_EVENT_CLASS_LOAD)) {
920 EVT_TRACE(JVMTI_EVENT_CLASS_LOAD, ("JVMTI [%s] Evt Class Load sent %s",
921 JvmtiTrace::safe_get_thread_name(thread),
922 kh()==NULL? "NULL" : kh()->external_name() ));
924 JvmtiEnv *env = ets->get_env();
925 JvmtiClassEventMark jem(thread, kh());
926 JvmtiJavaThreadEventTransition jet(thread);
927 jvmtiEventClassLoad callback = env->callbacks()->ClassLoad;
928 if (callback != NULL) {
929 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class());
930 }
931 }
932 }
933 }
936 void JvmtiExport::post_class_prepare(JavaThread *thread, Klass* klass) {
937 HandleMark hm(thread);
938 KlassHandle kh(thread, klass);
940 EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("JVMTI [%s] Trg Class Prepare triggered",
941 JvmtiTrace::safe_get_thread_name(thread)));
942 JvmtiThreadState* state = thread->jvmti_thread_state();
943 if (state == NULL) {
944 return;
945 }
946 JvmtiEnvThreadStateIterator it(state);
947 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
948 if (ets->is_enabled(JVMTI_EVENT_CLASS_PREPARE)) {
949 EVT_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("JVMTI [%s] Evt Class Prepare sent %s",
950 JvmtiTrace::safe_get_thread_name(thread),
951 kh()==NULL? "NULL" : kh()->external_name() ));
953 JvmtiEnv *env = ets->get_env();
954 JvmtiClassEventMark jem(thread, kh());
955 JvmtiJavaThreadEventTransition jet(thread);
956 jvmtiEventClassPrepare callback = env->callbacks()->ClassPrepare;
957 if (callback != NULL) {
958 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class());
959 }
960 }
961 }
962 }
964 void JvmtiExport::post_class_unload(Klass* klass) {
965 Thread *thread = Thread::current();
966 HandleMark hm(thread);
967 KlassHandle kh(thread, klass);
969 EVT_TRIG_TRACE(EXT_EVENT_CLASS_UNLOAD, ("JVMTI [?] Trg Class Unload triggered" ));
970 if (JvmtiEventController::is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) {
971 assert(thread->is_VM_thread(), "wrong thread");
973 // get JavaThread for whom we are proxy
974 JavaThread *real_thread =
975 (JavaThread *)((VMThread *)thread)->vm_operation()->calling_thread();
977 JvmtiEnvIterator it;
978 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
979 if (env->is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) {
980 EVT_TRACE(EXT_EVENT_CLASS_UNLOAD, ("JVMTI [?] Evt Class Unload sent %s",
981 kh()==NULL? "NULL" : kh()->external_name() ));
983 // do everything manually, since this is a proxy - needs special care
984 JNIEnv* jni_env = real_thread->jni_environment();
985 jthread jt = (jthread)JNIHandles::make_local(real_thread, real_thread->threadObj());
986 jclass jk = (jclass)JNIHandles::make_local(real_thread, kh()->java_mirror());
988 // Before we call the JVMTI agent, we have to set the state in the
989 // thread for which we are proxying.
990 JavaThreadState prev_state = real_thread->thread_state();
991 assert(prev_state == _thread_blocked, "JavaThread should be at safepoint");
992 real_thread->set_thread_state(_thread_in_native);
994 jvmtiExtensionEvent callback = env->ext_callbacks()->ClassUnload;
995 if (callback != NULL) {
996 (*callback)(env->jvmti_external(), jni_env, jt, jk);
997 }
999 assert(real_thread->thread_state() == _thread_in_native,
1000 "JavaThread should be in native");
1001 real_thread->set_thread_state(prev_state);
1003 JNIHandles::destroy_local(jk);
1004 JNIHandles::destroy_local(jt);
1005 }
1006 }
1007 }
1008 }
1011 void JvmtiExport::post_thread_start(JavaThread *thread) {
1012 assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
1014 EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_START, ("JVMTI [%s] Trg Thread Start event triggered",
1015 JvmtiTrace::safe_get_thread_name(thread)));
1017 // do JVMTI thread initialization (if needed)
1018 JvmtiEventController::thread_started(thread);
1020 // Do not post thread start event for hidden java thread.
1021 if (JvmtiEventController::is_enabled(JVMTI_EVENT_THREAD_START) &&
1022 !thread->is_hidden_from_external_view()) {
1023 JvmtiEnvIterator it;
1024 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1025 if (env->is_enabled(JVMTI_EVENT_THREAD_START)) {
1026 EVT_TRACE(JVMTI_EVENT_THREAD_START, ("JVMTI [%s] Evt Thread Start event sent",
1027 JvmtiTrace::safe_get_thread_name(thread) ));
1029 JvmtiThreadEventMark jem(thread);
1030 JvmtiJavaThreadEventTransition jet(thread);
1031 jvmtiEventThreadStart callback = env->callbacks()->ThreadStart;
1032 if (callback != NULL) {
1033 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
1034 }
1035 }
1036 }
1037 }
1038 }
1041 void JvmtiExport::post_thread_end(JavaThread *thread) {
1042 EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_END, ("JVMTI [%s] Trg Thread End event triggered",
1043 JvmtiTrace::safe_get_thread_name(thread)));
1045 JvmtiThreadState *state = thread->jvmti_thread_state();
1046 if (state == NULL) {
1047 return;
1048 }
1050 // Do not post thread end event for hidden java thread.
1051 if (state->is_enabled(JVMTI_EVENT_THREAD_END) &&
1052 !thread->is_hidden_from_external_view()) {
1054 JvmtiEnvThreadStateIterator it(state);
1055 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1056 if (ets->is_enabled(JVMTI_EVENT_THREAD_END)) {
1057 EVT_TRACE(JVMTI_EVENT_THREAD_END, ("JVMTI [%s] Evt Thread End event sent",
1058 JvmtiTrace::safe_get_thread_name(thread) ));
1060 JvmtiEnv *env = ets->get_env();
1061 JvmtiThreadEventMark jem(thread);
1062 JvmtiJavaThreadEventTransition jet(thread);
1063 jvmtiEventThreadEnd callback = env->callbacks()->ThreadEnd;
1064 if (callback != NULL) {
1065 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
1066 }
1067 }
1068 }
1069 }
1070 }
1072 void JvmtiExport::post_object_free(JvmtiEnv* env, jlong tag) {
1073 assert(SafepointSynchronize::is_at_safepoint(), "must be executed at safepoint");
1074 assert(env->is_enabled(JVMTI_EVENT_OBJECT_FREE), "checking");
1076 EVT_TRIG_TRACE(JVMTI_EVENT_OBJECT_FREE, ("JVMTI [?] Trg Object Free triggered" ));
1077 EVT_TRACE(JVMTI_EVENT_OBJECT_FREE, ("JVMTI [?] Evt Object Free sent"));
1079 jvmtiEventObjectFree callback = env->callbacks()->ObjectFree;
1080 if (callback != NULL) {
1081 (*callback)(env->jvmti_external(), tag);
1082 }
1083 }
1085 void JvmtiExport::post_resource_exhausted(jint resource_exhausted_flags, const char* description) {
1086 EVT_TRIG_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("JVMTI Trg resource exhausted event triggered" ));
1088 JvmtiEnvIterator it;
1089 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1090 if (env->is_enabled(JVMTI_EVENT_RESOURCE_EXHAUSTED)) {
1091 EVT_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("JVMTI Evt resource exhausted event sent" ));
1093 JavaThread *thread = JavaThread::current();
1094 JvmtiThreadEventMark jem(thread);
1095 JvmtiJavaThreadEventTransition jet(thread);
1096 jvmtiEventResourceExhausted callback = env->callbacks()->ResourceExhausted;
1097 if (callback != NULL) {
1098 (*callback)(env->jvmti_external(), jem.jni_env(),
1099 resource_exhausted_flags, NULL, description);
1100 }
1101 }
1102 }
1103 }
1105 void JvmtiExport::post_method_entry(JavaThread *thread, Method* method, frame current_frame) {
1106 HandleMark hm(thread);
1107 methodHandle mh(thread, method);
1109 EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("JVMTI [%s] Trg Method Entry triggered %s.%s",
1110 JvmtiTrace::safe_get_thread_name(thread),
1111 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1112 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
1114 JvmtiThreadState* state = thread->jvmti_thread_state();
1115 if (state == NULL || !state->is_interp_only_mode()) {
1116 // for any thread that actually wants method entry, interp_only_mode is set
1117 return;
1118 }
1120 state->incr_cur_stack_depth();
1122 if (state->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) {
1123 JvmtiEnvThreadStateIterator it(state);
1124 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1125 if (ets->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) {
1126 EVT_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("JVMTI [%s] Evt Method Entry sent %s.%s",
1127 JvmtiTrace::safe_get_thread_name(thread),
1128 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1129 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
1131 JvmtiEnv *env = ets->get_env();
1132 JvmtiMethodEventMark jem(thread, mh);
1133 JvmtiJavaThreadEventTransition jet(thread);
1134 jvmtiEventMethodEntry callback = env->callbacks()->MethodEntry;
1135 if (callback != NULL) {
1136 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_methodID());
1137 }
1138 }
1139 }
1140 }
1141 }
1143 void JvmtiExport::post_method_exit(JavaThread *thread, Method* method, frame current_frame) {
1144 HandleMark hm(thread);
1145 methodHandle mh(thread, method);
1147 EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_EXIT, ("JVMTI [%s] Trg Method Exit triggered %s.%s",
1148 JvmtiTrace::safe_get_thread_name(thread),
1149 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1150 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
1152 JvmtiThreadState *state = thread->jvmti_thread_state();
1153 if (state == NULL || !state->is_interp_only_mode()) {
1154 // for any thread that actually wants method exit, interp_only_mode is set
1155 return;
1156 }
1158 // return a flag when a method terminates by throwing an exception
1159 // i.e. if an exception is thrown and it's not caught by the current method
1160 bool exception_exit = state->is_exception_detected() && !state->is_exception_caught();
1163 if (state->is_enabled(JVMTI_EVENT_METHOD_EXIT)) {
1164 Handle result;
1165 jvalue value;
1166 value.j = 0L;
1168 // if the method hasn't been popped because of an exception then we populate
1169 // the return_value parameter for the callback. At this point we only have
1170 // the address of a "raw result" and we just call into the interpreter to
1171 // convert this into a jvalue.
1172 if (!exception_exit) {
1173 oop oop_result;
1174 BasicType type = current_frame.interpreter_frame_result(&oop_result, &value);
1175 if (type == T_OBJECT || type == T_ARRAY) {
1176 result = Handle(thread, oop_result);
1177 }
1178 }
1180 JvmtiEnvThreadStateIterator it(state);
1181 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1182 if (ets->is_enabled(JVMTI_EVENT_METHOD_EXIT)) {
1183 EVT_TRACE(JVMTI_EVENT_METHOD_EXIT, ("JVMTI [%s] Evt Method Exit sent %s.%s",
1184 JvmtiTrace::safe_get_thread_name(thread),
1185 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1186 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
1188 JvmtiEnv *env = ets->get_env();
1189 JvmtiMethodEventMark jem(thread, mh);
1190 if (result.not_null()) {
1191 value.l = JNIHandles::make_local(thread, result());
1192 }
1193 JvmtiJavaThreadEventTransition jet(thread);
1194 jvmtiEventMethodExit callback = env->callbacks()->MethodExit;
1195 if (callback != NULL) {
1196 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1197 jem.jni_methodID(), exception_exit, value);
1198 }
1199 }
1200 }
1201 }
1203 if (state->is_enabled(JVMTI_EVENT_FRAME_POP)) {
1204 JvmtiEnvThreadStateIterator it(state);
1205 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1206 int cur_frame_number = state->cur_stack_depth();
1208 if (ets->is_frame_pop(cur_frame_number)) {
1209 // we have a NotifyFramePop entry for this frame.
1210 // now check that this env/thread wants this event
1211 if (ets->is_enabled(JVMTI_EVENT_FRAME_POP)) {
1212 EVT_TRACE(JVMTI_EVENT_FRAME_POP, ("JVMTI [%s] Evt Frame Pop sent %s.%s",
1213 JvmtiTrace::safe_get_thread_name(thread),
1214 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1215 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
1217 // we also need to issue a frame pop event for this frame
1218 JvmtiEnv *env = ets->get_env();
1219 JvmtiMethodEventMark jem(thread, mh);
1220 JvmtiJavaThreadEventTransition jet(thread);
1221 jvmtiEventFramePop callback = env->callbacks()->FramePop;
1222 if (callback != NULL) {
1223 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1224 jem.jni_methodID(), exception_exit);
1225 }
1226 }
1227 // remove the frame's entry
1228 ets->clear_frame_pop(cur_frame_number);
1229 }
1230 }
1231 }
1233 state->decr_cur_stack_depth();
1234 }
1237 // Todo: inline this for optimization
1238 void JvmtiExport::post_single_step(JavaThread *thread, Method* method, address location) {
1239 HandleMark hm(thread);
1240 methodHandle mh(thread, method);
1242 JvmtiThreadState *state = thread->jvmti_thread_state();
1243 if (state == NULL) {
1244 return;
1245 }
1246 JvmtiEnvThreadStateIterator it(state);
1247 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1248 ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_SINGLE_STEP);
1249 if (!ets->single_stepping_posted() && ets->is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
1250 EVT_TRACE(JVMTI_EVENT_SINGLE_STEP, ("JVMTI [%s] Evt Single Step sent %s.%s @ %d",
1251 JvmtiTrace::safe_get_thread_name(thread),
1252 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1253 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
1254 location - mh()->code_base() ));
1256 JvmtiEnv *env = ets->get_env();
1257 JvmtiLocationEventMark jem(thread, mh, location);
1258 JvmtiJavaThreadEventTransition jet(thread);
1259 jvmtiEventSingleStep callback = env->callbacks()->SingleStep;
1260 if (callback != NULL) {
1261 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1262 jem.jni_methodID(), jem.location());
1263 }
1265 ets->set_single_stepping_posted();
1266 }
1267 }
1268 }
1271 void JvmtiExport::post_exception_throw(JavaThread *thread, Method* method, address location, oop exception) {
1272 HandleMark hm(thread);
1273 methodHandle mh(thread, method);
1274 Handle exception_handle(thread, exception);
1276 JvmtiThreadState *state = thread->jvmti_thread_state();
1277 if (state == NULL) {
1278 return;
1279 }
1281 EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION, ("JVMTI [%s] Trg Exception thrown triggered",
1282 JvmtiTrace::safe_get_thread_name(thread)));
1283 if (!state->is_exception_detected()) {
1284 state->set_exception_detected();
1285 JvmtiEnvThreadStateIterator it(state);
1286 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1287 if (ets->is_enabled(JVMTI_EVENT_EXCEPTION) && (exception != NULL)) {
1289 EVT_TRACE(JVMTI_EVENT_EXCEPTION,
1290 ("JVMTI [%s] Evt Exception thrown sent %s.%s @ %d",
1291 JvmtiTrace::safe_get_thread_name(thread),
1292 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1293 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
1294 location - mh()->code_base() ));
1296 JvmtiEnv *env = ets->get_env();
1297 JvmtiExceptionEventMark jem(thread, mh, location, exception_handle);
1299 // It's okay to clear these exceptions here because we duplicate
1300 // this lookup in InterpreterRuntime::exception_handler_for_exception.
1301 EXCEPTION_MARK;
1303 bool should_repeat;
1304 vframeStream st(thread);
1305 assert(!st.at_end(), "cannot be at end");
1306 Method* current_method = NULL;
1307 // A GC may occur during the Method::fast_exception_handler_bci_for()
1308 // call below if it needs to load the constraint class. Using a
1309 // methodHandle to keep the 'current_method' from being deallocated
1310 // if GC happens.
1311 methodHandle current_mh = methodHandle(thread, current_method);
1312 int current_bci = -1;
1313 do {
1314 current_method = st.method();
1315 current_mh = methodHandle(thread, current_method);
1316 current_bci = st.bci();
1317 do {
1318 should_repeat = false;
1319 KlassHandle eh_klass(thread, exception_handle()->klass());
1320 current_bci = Method::fast_exception_handler_bci_for(
1321 current_mh, eh_klass, current_bci, THREAD);
1322 if (HAS_PENDING_EXCEPTION) {
1323 exception_handle = Handle(thread, PENDING_EXCEPTION);
1324 CLEAR_PENDING_EXCEPTION;
1325 should_repeat = true;
1326 }
1327 } while (should_repeat && (current_bci != -1));
1328 st.next();
1329 } while ((current_bci < 0) && (!st.at_end()));
1331 jmethodID catch_jmethodID;
1332 if (current_bci < 0) {
1333 catch_jmethodID = 0;
1334 current_bci = 0;
1335 } else {
1336 catch_jmethodID = jem.to_jmethodID(current_mh);
1337 }
1339 JvmtiJavaThreadEventTransition jet(thread);
1340 jvmtiEventException callback = env->callbacks()->Exception;
1341 if (callback != NULL) {
1342 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1343 jem.jni_methodID(), jem.location(),
1344 jem.exception(),
1345 catch_jmethodID, current_bci);
1346 }
1347 }
1348 }
1349 }
1351 // frames may get popped because of this throw, be safe - invalidate cached depth
1352 state->invalidate_cur_stack_depth();
1353 }
1356 void JvmtiExport::notice_unwind_due_to_exception(JavaThread *thread, Method* method, address location, oop exception, bool in_handler_frame) {
1357 HandleMark hm(thread);
1358 methodHandle mh(thread, method);
1359 Handle exception_handle(thread, exception);
1361 JvmtiThreadState *state = thread->jvmti_thread_state();
1362 if (state == NULL) {
1363 return;
1364 }
1365 EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION_CATCH,
1366 ("JVMTI [%s] Trg unwind_due_to_exception triggered %s.%s @ %s%d - %s",
1367 JvmtiTrace::safe_get_thread_name(thread),
1368 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1369 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
1370 location==0? "no location:" : "",
1371 location==0? 0 : location - mh()->code_base(),
1372 in_handler_frame? "in handler frame" : "not handler frame" ));
1374 if (state->is_exception_detected()) {
1376 state->invalidate_cur_stack_depth();
1377 if (!in_handler_frame) {
1378 // Not in exception handler.
1379 if(state->is_interp_only_mode()) {
1380 // method exit and frame pop events are posted only in interp mode.
1381 // When these events are enabled code should be in running in interp mode.
1382 JvmtiExport::post_method_exit(thread, method, thread->last_frame());
1383 // The cached cur_stack_depth might have changed from the
1384 // operations of frame pop or method exit. We are not 100% sure
1385 // the cached cur_stack_depth is still valid depth so invalidate
1386 // it.
1387 state->invalidate_cur_stack_depth();
1388 }
1389 } else {
1390 // In exception handler frame. Report exception catch.
1391 assert(location != NULL, "must be a known location");
1392 // Update cur_stack_depth - the frames above the current frame
1393 // have been unwound due to this exception:
1394 assert(!state->is_exception_caught(), "exception must not be caught yet.");
1395 state->set_exception_caught();
1397 JvmtiEnvThreadStateIterator it(state);
1398 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1399 if (ets->is_enabled(JVMTI_EVENT_EXCEPTION_CATCH) && (exception_handle() != NULL)) {
1400 EVT_TRACE(JVMTI_EVENT_EXCEPTION_CATCH,
1401 ("JVMTI [%s] Evt ExceptionCatch sent %s.%s @ %d",
1402 JvmtiTrace::safe_get_thread_name(thread),
1403 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1404 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
1405 location - mh()->code_base() ));
1407 JvmtiEnv *env = ets->get_env();
1408 JvmtiExceptionEventMark jem(thread, mh, location, exception_handle);
1409 JvmtiJavaThreadEventTransition jet(thread);
1410 jvmtiEventExceptionCatch callback = env->callbacks()->ExceptionCatch;
1411 if (callback != NULL) {
1412 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1413 jem.jni_methodID(), jem.location(),
1414 jem.exception());
1415 }
1416 }
1417 }
1418 }
1419 }
1420 }
1422 oop JvmtiExport::jni_GetField_probe(JavaThread *thread, jobject jobj, oop obj,
1423 Klass* klass, jfieldID fieldID, bool is_static) {
1424 if (*((int *)get_field_access_count_addr()) > 0 && thread->has_last_Java_frame()) {
1425 // At least one field access watch is set so we have more work
1426 // to do. This wrapper is used by entry points that allow us
1427 // to create handles in post_field_access_by_jni().
1428 post_field_access_by_jni(thread, obj, klass, fieldID, is_static);
1429 // event posting can block so refetch oop if we were passed a jobj
1430 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj);
1431 }
1432 return obj;
1433 }
1435 oop JvmtiExport::jni_GetField_probe_nh(JavaThread *thread, jobject jobj, oop obj,
1436 Klass* klass, jfieldID fieldID, bool is_static) {
1437 if (*((int *)get_field_access_count_addr()) > 0 && thread->has_last_Java_frame()) {
1438 // At least one field access watch is set so we have more work
1439 // to do. This wrapper is used by "quick" entry points that don't
1440 // allow us to create handles in post_field_access_by_jni(). We
1441 // override that with a ResetNoHandleMark.
1442 ResetNoHandleMark rnhm;
1443 post_field_access_by_jni(thread, obj, klass, fieldID, is_static);
1444 // event posting can block so refetch oop if we were passed a jobj
1445 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj);
1446 }
1447 return obj;
1448 }
1450 void JvmtiExport::post_field_access_by_jni(JavaThread *thread, oop obj,
1451 Klass* klass, jfieldID fieldID, bool is_static) {
1452 // We must be called with a Java context in order to provide reasonable
1453 // values for the klazz, method, and location fields. The callers of this
1454 // function don't make the call unless there is a Java context.
1455 assert(thread->has_last_Java_frame(), "must be called with a Java context");
1457 ResourceMark rm;
1458 fieldDescriptor fd;
1459 // if get_field_descriptor finds fieldID to be invalid, then we just bail
1460 bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd);
1461 assert(valid_fieldID == true,"post_field_access_by_jni called with invalid fieldID");
1462 if (!valid_fieldID) return;
1463 // field accesses are not watched so bail
1464 if (!fd.is_field_access_watched()) return;
1466 HandleMark hm(thread);
1467 KlassHandle h_klass(thread, klass);
1468 Handle h_obj;
1469 if (!is_static) {
1470 // non-static field accessors have an object, but we need a handle
1471 assert(obj != NULL, "non-static needs an object");
1472 h_obj = Handle(thread, obj);
1473 }
1474 post_field_access(thread,
1475 thread->last_frame().interpreter_frame_method(),
1476 thread->last_frame().interpreter_frame_bcp(),
1477 h_klass, h_obj, fieldID);
1478 }
1480 void JvmtiExport::post_field_access(JavaThread *thread, Method* method,
1481 address location, KlassHandle field_klass, Handle object, jfieldID field) {
1483 HandleMark hm(thread);
1484 methodHandle mh(thread, method);
1486 JvmtiThreadState *state = thread->jvmti_thread_state();
1487 if (state == NULL) {
1488 return;
1489 }
1490 EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("JVMTI [%s] Trg Field Access event triggered",
1491 JvmtiTrace::safe_get_thread_name(thread)));
1492 JvmtiEnvThreadStateIterator it(state);
1493 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1494 if (ets->is_enabled(JVMTI_EVENT_FIELD_ACCESS)) {
1495 EVT_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("JVMTI [%s] Evt Field Access event sent %s.%s @ %d",
1496 JvmtiTrace::safe_get_thread_name(thread),
1497 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1498 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
1499 location - mh()->code_base() ));
1501 JvmtiEnv *env = ets->get_env();
1502 JvmtiLocationEventMark jem(thread, mh, location);
1503 jclass field_jclass = jem.to_jclass(field_klass());
1504 jobject field_jobject = jem.to_jobject(object());
1505 JvmtiJavaThreadEventTransition jet(thread);
1506 jvmtiEventFieldAccess callback = env->callbacks()->FieldAccess;
1507 if (callback != NULL) {
1508 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1509 jem.jni_methodID(), jem.location(),
1510 field_jclass, field_jobject, field);
1511 }
1512 }
1513 }
1514 }
1516 oop JvmtiExport::jni_SetField_probe(JavaThread *thread, jobject jobj, oop obj,
1517 Klass* klass, jfieldID fieldID, bool is_static,
1518 char sig_type, jvalue *value) {
1519 if (*((int *)get_field_modification_count_addr()) > 0 && thread->has_last_Java_frame()) {
1520 // At least one field modification watch is set so we have more work
1521 // to do. This wrapper is used by entry points that allow us
1522 // to create handles in post_field_modification_by_jni().
1523 post_field_modification_by_jni(thread, obj, klass, fieldID, is_static, sig_type, value);
1524 // event posting can block so refetch oop if we were passed a jobj
1525 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj);
1526 }
1527 return obj;
1528 }
1530 oop JvmtiExport::jni_SetField_probe_nh(JavaThread *thread, jobject jobj, oop obj,
1531 Klass* klass, jfieldID fieldID, bool is_static,
1532 char sig_type, jvalue *value) {
1533 if (*((int *)get_field_modification_count_addr()) > 0 && thread->has_last_Java_frame()) {
1534 // At least one field modification watch is set so we have more work
1535 // to do. This wrapper is used by "quick" entry points that don't
1536 // allow us to create handles in post_field_modification_by_jni(). We
1537 // override that with a ResetNoHandleMark.
1538 ResetNoHandleMark rnhm;
1539 post_field_modification_by_jni(thread, obj, klass, fieldID, is_static, sig_type, value);
1540 // event posting can block so refetch oop if we were passed a jobj
1541 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj);
1542 }
1543 return obj;
1544 }
1546 void JvmtiExport::post_field_modification_by_jni(JavaThread *thread, oop obj,
1547 Klass* klass, jfieldID fieldID, bool is_static,
1548 char sig_type, jvalue *value) {
1549 // We must be called with a Java context in order to provide reasonable
1550 // values for the klazz, method, and location fields. The callers of this
1551 // function don't make the call unless there is a Java context.
1552 assert(thread->has_last_Java_frame(), "must be called with Java context");
1554 ResourceMark rm;
1555 fieldDescriptor fd;
1556 // if get_field_descriptor finds fieldID to be invalid, then we just bail
1557 bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd);
1558 assert(valid_fieldID == true,"post_field_modification_by_jni called with invalid fieldID");
1559 if (!valid_fieldID) return;
1560 // field modifications are not watched so bail
1561 if (!fd.is_field_modification_watched()) return;
1563 HandleMark hm(thread);
1565 Handle h_obj;
1566 if (!is_static) {
1567 // non-static field accessors have an object, but we need a handle
1568 assert(obj != NULL, "non-static needs an object");
1569 h_obj = Handle(thread, obj);
1570 }
1571 KlassHandle h_klass(thread, klass);
1572 post_field_modification(thread,
1573 thread->last_frame().interpreter_frame_method(),
1574 thread->last_frame().interpreter_frame_bcp(),
1575 h_klass, h_obj, fieldID, sig_type, value);
1576 }
1578 void JvmtiExport::post_raw_field_modification(JavaThread *thread, Method* method,
1579 address location, KlassHandle field_klass, Handle object, jfieldID field,
1580 char sig_type, jvalue *value) {
1582 if (sig_type == 'I' || sig_type == 'Z' || sig_type == 'C' || sig_type == 'S') {
1583 // 'I' instructions are used for byte, char, short and int.
1584 // determine which it really is, and convert
1585 fieldDescriptor fd;
1586 bool found = JvmtiEnv::get_field_descriptor(field_klass(), field, &fd);
1587 // should be found (if not, leave as is)
1588 if (found) {
1589 jint ival = value->i;
1590 // convert value from int to appropriate type
1591 switch (fd.field_type()) {
1592 case T_BOOLEAN:
1593 sig_type = 'Z';
1594 value->i = 0; // clear it
1595 value->z = (jboolean)ival;
1596 break;
1597 case T_BYTE:
1598 sig_type = 'B';
1599 value->i = 0; // clear it
1600 value->b = (jbyte)ival;
1601 break;
1602 case T_CHAR:
1603 sig_type = 'C';
1604 value->i = 0; // clear it
1605 value->c = (jchar)ival;
1606 break;
1607 case T_SHORT:
1608 sig_type = 'S';
1609 value->i = 0; // clear it
1610 value->s = (jshort)ival;
1611 break;
1612 case T_INT:
1613 // nothing to do
1614 break;
1615 default:
1616 // this is an integer instruction, should be one of above
1617 ShouldNotReachHere();
1618 break;
1619 }
1620 }
1621 }
1623 // convert oop to JNI handle.
1624 if (sig_type == 'L' || sig_type == '[') {
1625 value->l = (jobject)JNIHandles::make_local(thread, (oop)value->l);
1626 }
1628 post_field_modification(thread, method, location, field_klass, object, field, sig_type, value);
1630 // Destroy the JNI handle allocated above.
1631 if (sig_type == 'L') {
1632 JNIHandles::destroy_local(value->l);
1633 }
1634 }
1636 void JvmtiExport::post_field_modification(JavaThread *thread, Method* method,
1637 address location, KlassHandle field_klass, Handle object, jfieldID field,
1638 char sig_type, jvalue *value_ptr) {
1640 HandleMark hm(thread);
1641 methodHandle mh(thread, method);
1643 JvmtiThreadState *state = thread->jvmti_thread_state();
1644 if (state == NULL) {
1645 return;
1646 }
1647 EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_MODIFICATION,
1648 ("JVMTI [%s] Trg Field Modification event triggered",
1649 JvmtiTrace::safe_get_thread_name(thread)));
1651 JvmtiEnvThreadStateIterator it(state);
1652 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1653 if (ets->is_enabled(JVMTI_EVENT_FIELD_MODIFICATION)) {
1654 EVT_TRACE(JVMTI_EVENT_FIELD_MODIFICATION,
1655 ("JVMTI [%s] Evt Field Modification event sent %s.%s @ %d",
1656 JvmtiTrace::safe_get_thread_name(thread),
1657 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1658 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
1659 location - mh()->code_base() ));
1661 JvmtiEnv *env = ets->get_env();
1662 JvmtiLocationEventMark jem(thread, mh, location);
1663 jclass field_jclass = jem.to_jclass(field_klass());
1664 jobject field_jobject = jem.to_jobject(object());
1665 JvmtiJavaThreadEventTransition jet(thread);
1666 jvmtiEventFieldModification callback = env->callbacks()->FieldModification;
1667 if (callback != NULL) {
1668 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1669 jem.jni_methodID(), jem.location(),
1670 field_jclass, field_jobject, field, sig_type, *value_ptr);
1671 }
1672 }
1673 }
1674 }
1676 void JvmtiExport::post_native_method_bind(Method* method, address* function_ptr) {
1677 JavaThread* thread = JavaThread::current();
1678 assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
1680 HandleMark hm(thread);
1681 methodHandle mh(thread, method);
1683 EVT_TRIG_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("JVMTI [%s] Trg Native Method Bind event triggered",
1684 JvmtiTrace::safe_get_thread_name(thread)));
1686 if (JvmtiEventController::is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) {
1687 JvmtiEnvIterator it;
1688 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1689 if (env->is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) {
1690 EVT_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("JVMTI [%s] Evt Native Method Bind event sent",
1691 JvmtiTrace::safe_get_thread_name(thread) ));
1693 JvmtiMethodEventMark jem(thread, mh);
1694 JvmtiJavaThreadEventTransition jet(thread);
1695 JNIEnv* jni_env = JvmtiEnv::get_phase() == JVMTI_PHASE_PRIMORDIAL? NULL : jem.jni_env();
1696 jvmtiEventNativeMethodBind callback = env->callbacks()->NativeMethodBind;
1697 if (callback != NULL) {
1698 (*callback)(env->jvmti_external(), jni_env, jem.jni_thread(),
1699 jem.jni_methodID(), (void*)(*function_ptr), (void**)function_ptr);
1700 }
1701 }
1702 }
1703 }
1704 }
1706 // Returns a record containing inlining information for the given nmethod
1707 jvmtiCompiledMethodLoadInlineRecord* create_inline_record(nmethod* nm) {
1708 jint numstackframes = 0;
1709 jvmtiCompiledMethodLoadInlineRecord* record = (jvmtiCompiledMethodLoadInlineRecord*)NEW_RESOURCE_OBJ(jvmtiCompiledMethodLoadInlineRecord);
1710 record->header.kind = JVMTI_CMLR_INLINE_INFO;
1711 record->header.next = NULL;
1712 record->header.majorinfoversion = JVMTI_CMLR_MAJOR_VERSION_1;
1713 record->header.minorinfoversion = JVMTI_CMLR_MINOR_VERSION_0;
1714 record->numpcs = 0;
1715 for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) {
1716 if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue;
1717 record->numpcs++;
1718 }
1719 record->pcinfo = (PCStackInfo*)(NEW_RESOURCE_ARRAY(PCStackInfo, record->numpcs));
1720 int scope = 0;
1721 for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) {
1722 if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue;
1723 void* pc_address = (void*)p->real_pc(nm);
1724 assert(pc_address != NULL, "pc_address must be non-null");
1725 record->pcinfo[scope].pc = pc_address;
1726 numstackframes=0;
1727 for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != NULL;sd = sd->sender()) {
1728 numstackframes++;
1729 }
1730 assert(numstackframes != 0, "numstackframes must be nonzero.");
1731 record->pcinfo[scope].methods = (jmethodID *)NEW_RESOURCE_ARRAY(jmethodID, numstackframes);
1732 record->pcinfo[scope].bcis = (jint *)NEW_RESOURCE_ARRAY(jint, numstackframes);
1733 record->pcinfo[scope].numstackframes = numstackframes;
1734 int stackframe = 0;
1735 for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != NULL;sd = sd->sender()) {
1736 // sd->method() can be NULL for stubs but not for nmethods. To be completely robust, include an assert that we should never see a null sd->method()
1737 assert(sd->method() != NULL, "sd->method() cannot be null.");
1738 record->pcinfo[scope].methods[stackframe] = sd->method()->jmethod_id();
1739 record->pcinfo[scope].bcis[stackframe] = sd->bci();
1740 stackframe++;
1741 }
1742 scope++;
1743 }
1744 return record;
1745 }
1747 void JvmtiExport::post_compiled_method_load(nmethod *nm) {
1748 JavaThread* thread = JavaThread::current();
1750 EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD,
1751 ("JVMTI [%s] method compile load event triggered",
1752 JvmtiTrace::safe_get_thread_name(thread)));
1754 JvmtiEnvIterator it;
1755 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1756 if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_LOAD)) {
1758 EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD,
1759 ("JVMTI [%s] class compile method load event sent %s.%s ",
1760 JvmtiTrace::safe_get_thread_name(thread),
1761 (nm->method() == NULL) ? "NULL" : nm->method()->klass_name()->as_C_string(),
1762 (nm->method() == NULL) ? "NULL" : nm->method()->name()->as_C_string()));
1763 ResourceMark rm(thread);
1764 HandleMark hm(thread);
1766 // Add inlining information
1767 jvmtiCompiledMethodLoadInlineRecord* inlinerecord = create_inline_record(nm);
1768 // Pass inlining information through the void pointer
1769 JvmtiCompiledMethodLoadEventMark jem(thread, nm, inlinerecord);
1770 JvmtiJavaThreadEventTransition jet(thread);
1771 jvmtiEventCompiledMethodLoad callback = env->callbacks()->CompiledMethodLoad;
1772 if (callback != NULL) {
1773 (*callback)(env->jvmti_external(), jem.jni_methodID(),
1774 jem.code_size(), jem.code_data(), jem.map_length(),
1775 jem.map(), jem.compile_info());
1776 }
1777 }
1778 }
1779 }
1782 // post a COMPILED_METHOD_LOAD event for a given environment
1783 void JvmtiExport::post_compiled_method_load(JvmtiEnv* env, const jmethodID method, const jint length,
1784 const void *code_begin, const jint map_length,
1785 const jvmtiAddrLocationMap* map)
1786 {
1787 JavaThread* thread = JavaThread::current();
1788 EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD,
1789 ("JVMTI [%s] method compile load event triggered (by GenerateEvents)",
1790 JvmtiTrace::safe_get_thread_name(thread)));
1791 if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_LOAD)) {
1793 EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD,
1794 ("JVMTI [%s] class compile method load event sent (by GenerateEvents), jmethodID=" PTR_FORMAT,
1795 JvmtiTrace::safe_get_thread_name(thread), method));
1797 JvmtiEventMark jem(thread);
1798 JvmtiJavaThreadEventTransition jet(thread);
1799 jvmtiEventCompiledMethodLoad callback = env->callbacks()->CompiledMethodLoad;
1800 if (callback != NULL) {
1801 (*callback)(env->jvmti_external(), method,
1802 length, code_begin, map_length,
1803 map, NULL);
1804 }
1805 }
1806 }
1808 void JvmtiExport::post_dynamic_code_generated_internal(const char *name, const void *code_begin, const void *code_end) {
1809 assert(name != NULL && name[0] != '\0', "sanity check");
1811 JavaThread* thread = JavaThread::current();
1812 // In theory everyone coming thru here is in_vm but we need to be certain
1813 // because a callee will do a vm->native transition
1814 ThreadInVMfromUnknown __tiv;
1816 EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
1817 ("JVMTI [%s] method dynamic code generated event triggered",
1818 JvmtiTrace::safe_get_thread_name(thread)));
1819 JvmtiEnvIterator it;
1820 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1821 if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) {
1822 EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
1823 ("JVMTI [%s] dynamic code generated event sent for %s",
1824 JvmtiTrace::safe_get_thread_name(thread), name));
1825 JvmtiEventMark jem(thread);
1826 JvmtiJavaThreadEventTransition jet(thread);
1827 jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char));
1828 jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated;
1829 if (callback != NULL) {
1830 (*callback)(env->jvmti_external(), name, (void*)code_begin, length);
1831 }
1832 }
1833 }
1834 }
1836 void JvmtiExport::post_dynamic_code_generated(const char *name, const void *code_begin, const void *code_end) {
1837 jvmtiPhase phase = JvmtiEnv::get_phase();
1838 if (phase == JVMTI_PHASE_PRIMORDIAL || phase == JVMTI_PHASE_START) {
1839 post_dynamic_code_generated_internal(name, code_begin, code_end);
1840 } else {
1841 // It may not be safe to post the event from this thread. Defer all
1842 // postings to the service thread so that it can perform them in a safe
1843 // context and in-order.
1844 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
1845 JvmtiDeferredEvent event = JvmtiDeferredEvent::dynamic_code_generated_event(
1846 name, code_begin, code_end);
1847 JvmtiDeferredEventQueue::enqueue(event);
1848 }
1849 }
1852 // post a DYNAMIC_CODE_GENERATED event for a given environment
1853 // used by GenerateEvents
1854 void JvmtiExport::post_dynamic_code_generated(JvmtiEnv* env, const char *name,
1855 const void *code_begin, const void *code_end)
1856 {
1857 JavaThread* thread = JavaThread::current();
1858 EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
1859 ("JVMTI [%s] dynamic code generated event triggered (by GenerateEvents)",
1860 JvmtiTrace::safe_get_thread_name(thread)));
1861 if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) {
1862 EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
1863 ("JVMTI [%s] dynamic code generated event sent for %s",
1864 JvmtiTrace::safe_get_thread_name(thread), name));
1865 JvmtiEventMark jem(thread);
1866 JvmtiJavaThreadEventTransition jet(thread);
1867 jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char));
1868 jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated;
1869 if (callback != NULL) {
1870 (*callback)(env->jvmti_external(), name, (void*)code_begin, length);
1871 }
1872 }
1873 }
1875 // post a DynamicCodeGenerated event while holding locks in the VM.
1876 void JvmtiExport::post_dynamic_code_generated_while_holding_locks(const char* name,
1877 address code_begin, address code_end)
1878 {
1879 // register the stub with the current dynamic code event collector
1880 JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
1881 // state can only be NULL if the current thread is exiting which
1882 // should not happen since we're trying to post an event
1883 guarantee(state != NULL, "attempt to register stub via an exiting thread");
1884 JvmtiDynamicCodeEventCollector* collector = state->get_dynamic_code_event_collector();
1885 guarantee(collector != NULL, "attempt to register stub without event collector");
1886 collector->register_stub(name, code_begin, code_end);
1887 }
1889 // Collect all the vm internally allocated objects which are visible to java world
1890 void JvmtiExport::record_vm_internal_object_allocation(oop obj) {
1891 Thread* thread = ThreadLocalStorage::thread();
1892 if (thread != NULL && thread->is_Java_thread()) {
1893 // Can not take safepoint here.
1894 No_Safepoint_Verifier no_sfpt;
1895 // Can not take safepoint here so can not use state_for to get
1896 // jvmti thread state.
1897 JvmtiThreadState *state = ((JavaThread*)thread)->jvmti_thread_state();
1898 if (state != NULL ) {
1899 // state is non NULL when VMObjectAllocEventCollector is enabled.
1900 JvmtiVMObjectAllocEventCollector *collector;
1901 collector = state->get_vm_object_alloc_event_collector();
1902 if (collector != NULL && collector->is_enabled()) {
1903 // Don't record classes as these will be notified via the ClassLoad
1904 // event.
1905 if (obj->klass() != SystemDictionary::Class_klass()) {
1906 collector->record_allocation(obj);
1907 }
1908 }
1909 }
1910 }
1911 }
1913 void JvmtiExport::post_garbage_collection_finish() {
1914 Thread *thread = Thread::current(); // this event is posted from VM-Thread.
1915 EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
1916 ("JVMTI [%s] garbage collection finish event triggered",
1917 JvmtiTrace::safe_get_thread_name(thread)));
1918 JvmtiEnvIterator it;
1919 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1920 if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH)) {
1921 EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
1922 ("JVMTI [%s] garbage collection finish event sent ",
1923 JvmtiTrace::safe_get_thread_name(thread)));
1924 JvmtiThreadEventTransition jet(thread);
1925 // JNIEnv is NULL here because this event is posted from VM Thread
1926 jvmtiEventGarbageCollectionFinish callback = env->callbacks()->GarbageCollectionFinish;
1927 if (callback != NULL) {
1928 (*callback)(env->jvmti_external());
1929 }
1930 }
1931 }
1932 }
1934 void JvmtiExport::post_garbage_collection_start() {
1935 Thread* thread = Thread::current(); // this event is posted from vm-thread.
1936 EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START,
1937 ("JVMTI [%s] garbage collection start event triggered",
1938 JvmtiTrace::safe_get_thread_name(thread)));
1939 JvmtiEnvIterator it;
1940 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1941 if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_START)) {
1942 EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START,
1943 ("JVMTI [%s] garbage collection start event sent ",
1944 JvmtiTrace::safe_get_thread_name(thread)));
1945 JvmtiThreadEventTransition jet(thread);
1946 // JNIEnv is NULL here because this event is posted from VM Thread
1947 jvmtiEventGarbageCollectionStart callback = env->callbacks()->GarbageCollectionStart;
1948 if (callback != NULL) {
1949 (*callback)(env->jvmti_external());
1950 }
1951 }
1952 }
1953 }
1955 void JvmtiExport::post_data_dump() {
1956 Thread *thread = Thread::current();
1957 EVT_TRIG_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST,
1958 ("JVMTI [%s] data dump request event triggered",
1959 JvmtiTrace::safe_get_thread_name(thread)));
1960 JvmtiEnvIterator it;
1961 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1962 if (env->is_enabled(JVMTI_EVENT_DATA_DUMP_REQUEST)) {
1963 EVT_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST,
1964 ("JVMTI [%s] data dump request event sent ",
1965 JvmtiTrace::safe_get_thread_name(thread)));
1966 JvmtiThreadEventTransition jet(thread);
1967 // JNIEnv is NULL here because this event is posted from VM Thread
1968 jvmtiEventDataDumpRequest callback = env->callbacks()->DataDumpRequest;
1969 if (callback != NULL) {
1970 (*callback)(env->jvmti_external());
1971 }
1972 }
1973 }
1974 }
1976 void JvmtiExport::post_monitor_contended_enter(JavaThread *thread, ObjectMonitor *obj_mntr) {
1977 oop object = (oop)obj_mntr->object();
1978 if (!ServiceUtil::visible_oop(object)) {
1979 // Ignore monitor contended enter for vm internal object.
1980 return;
1981 }
1982 JvmtiThreadState *state = thread->jvmti_thread_state();
1983 if (state == NULL) {
1984 return;
1985 }
1987 HandleMark hm(thread);
1988 Handle h(thread, object);
1990 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
1991 ("JVMTI [%s] montior contended enter event triggered",
1992 JvmtiTrace::safe_get_thread_name(thread)));
1994 JvmtiEnvThreadStateIterator it(state);
1995 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1996 if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTER)) {
1997 EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
1998 ("JVMTI [%s] monitor contended enter event sent",
1999 JvmtiTrace::safe_get_thread_name(thread)));
2000 JvmtiMonitorEventMark jem(thread, h());
2001 JvmtiEnv *env = ets->get_env();
2002 JvmtiThreadEventTransition jet(thread);
2003 jvmtiEventMonitorContendedEnter callback = env->callbacks()->MonitorContendedEnter;
2004 if (callback != NULL) {
2005 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object());
2006 }
2007 }
2008 }
2009 }
2011 void JvmtiExport::post_monitor_contended_entered(JavaThread *thread, ObjectMonitor *obj_mntr) {
2012 oop object = (oop)obj_mntr->object();
2013 if (!ServiceUtil::visible_oop(object)) {
2014 // Ignore monitor contended entered for vm internal object.
2015 return;
2016 }
2017 JvmtiThreadState *state = thread->jvmti_thread_state();
2018 if (state == NULL) {
2019 return;
2020 }
2022 HandleMark hm(thread);
2023 Handle h(thread, object);
2025 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
2026 ("JVMTI [%s] montior contended entered event triggered",
2027 JvmtiTrace::safe_get_thread_name(thread)));
2029 JvmtiEnvThreadStateIterator it(state);
2030 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2031 if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED)) {
2032 EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
2033 ("JVMTI [%s] monitor contended enter event sent",
2034 JvmtiTrace::safe_get_thread_name(thread)));
2035 JvmtiMonitorEventMark jem(thread, h());
2036 JvmtiEnv *env = ets->get_env();
2037 JvmtiThreadEventTransition jet(thread);
2038 jvmtiEventMonitorContendedEntered callback = env->callbacks()->MonitorContendedEntered;
2039 if (callback != NULL) {
2040 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object());
2041 }
2042 }
2043 }
2044 }
2046 void JvmtiExport::post_monitor_wait(JavaThread *thread, oop object,
2047 jlong timeout) {
2048 JvmtiThreadState *state = thread->jvmti_thread_state();
2049 if (state == NULL) {
2050 return;
2051 }
2053 HandleMark hm(thread);
2054 Handle h(thread, object);
2056 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAIT,
2057 ("JVMTI [%s] montior wait event triggered",
2058 JvmtiTrace::safe_get_thread_name(thread)));
2060 JvmtiEnvThreadStateIterator it(state);
2061 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2062 if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAIT)) {
2063 EVT_TRACE(JVMTI_EVENT_MONITOR_WAIT,
2064 ("JVMTI [%s] monitor wait event sent ",
2065 JvmtiTrace::safe_get_thread_name(thread)));
2066 JvmtiMonitorEventMark jem(thread, h());
2067 JvmtiEnv *env = ets->get_env();
2068 JvmtiThreadEventTransition jet(thread);
2069 jvmtiEventMonitorWait callback = env->callbacks()->MonitorWait;
2070 if (callback != NULL) {
2071 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2072 jem.jni_object(), timeout);
2073 }
2074 }
2075 }
2076 }
2078 void JvmtiExport::post_monitor_waited(JavaThread *thread, ObjectMonitor *obj_mntr, jboolean timed_out) {
2079 oop object = (oop)obj_mntr->object();
2080 if (!ServiceUtil::visible_oop(object)) {
2081 // Ignore monitor waited for vm internal object.
2082 return;
2083 }
2084 JvmtiThreadState *state = thread->jvmti_thread_state();
2085 if (state == NULL) {
2086 return;
2087 }
2089 HandleMark hm(thread);
2090 Handle h(thread, object);
2092 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAITED,
2093 ("JVMTI [%s] montior waited event triggered",
2094 JvmtiTrace::safe_get_thread_name(thread)));
2096 JvmtiEnvThreadStateIterator it(state);
2097 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2098 if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAITED)) {
2099 EVT_TRACE(JVMTI_EVENT_MONITOR_WAITED,
2100 ("JVMTI [%s] monitor waited event sent ",
2101 JvmtiTrace::safe_get_thread_name(thread)));
2102 JvmtiMonitorEventMark jem(thread, h());
2103 JvmtiEnv *env = ets->get_env();
2104 JvmtiThreadEventTransition jet(thread);
2105 jvmtiEventMonitorWaited callback = env->callbacks()->MonitorWaited;
2106 if (callback != NULL) {
2107 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2108 jem.jni_object(), timed_out);
2109 }
2110 }
2111 }
2112 }
2115 void JvmtiExport::post_vm_object_alloc(JavaThread *thread, oop object) {
2116 EVT_TRIG_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("JVMTI [%s] Trg vm object alloc triggered",
2117 JvmtiTrace::safe_get_thread_name(thread)));
2118 if (object == NULL) {
2119 return;
2120 }
2121 HandleMark hm(thread);
2122 Handle h(thread, object);
2123 JvmtiEnvIterator it;
2124 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2125 if (env->is_enabled(JVMTI_EVENT_VM_OBJECT_ALLOC)) {
2126 EVT_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("JVMTI [%s] Evt vmobject alloc sent %s",
2127 JvmtiTrace::safe_get_thread_name(thread),
2128 object==NULL? "NULL" : java_lang_Class::as_Klass(object)->external_name()));
2130 JvmtiVMObjectAllocEventMark jem(thread, h());
2131 JvmtiJavaThreadEventTransition jet(thread);
2132 jvmtiEventVMObjectAlloc callback = env->callbacks()->VMObjectAlloc;
2133 if (callback != NULL) {
2134 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2135 jem.jni_jobject(), jem.jni_class(), jem.size());
2136 }
2137 }
2138 }
2139 }
2141 ////////////////////////////////////////////////////////////////////////////////////////////////
2143 void JvmtiExport::cleanup_thread(JavaThread* thread) {
2144 assert(JavaThread::current() == thread, "thread is not current");
2145 MutexLocker mu(JvmtiThreadState_lock);
2147 if (thread->jvmti_thread_state() != NULL) {
2148 // This has to happen after the thread state is removed, which is
2149 // why it is not in post_thread_end_event like its complement
2150 // Maybe both these functions should be rolled into the posts?
2151 JvmtiEventController::thread_ended(thread);
2152 }
2153 }
2155 void JvmtiExport::oops_do(OopClosure* f) {
2156 JvmtiCurrentBreakpoints::oops_do(f);
2157 JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(f);
2158 }
2160 void JvmtiExport::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
2161 JvmtiTagMap::weak_oops_do(is_alive, f);
2162 }
2164 void JvmtiExport::gc_epilogue() {
2165 JvmtiCurrentBreakpoints::gc_epilogue();
2166 }
2168 // Onload raw monitor transition.
2169 void JvmtiExport::transition_pending_onload_raw_monitors() {
2170 JvmtiPendingMonitors::transition_raw_monitors();
2171 }
2173 ////////////////////////////////////////////////////////////////////////////////////////////////
2175 // type for the Agent_OnAttach entry point
2176 extern "C" {
2177 typedef jint (JNICALL *OnAttachEntry_t)(JavaVM*, char *, void *);
2178 }
2180 jint JvmtiExport::load_agent_library(AttachOperation* op, outputStream* st) {
2181 char ebuf[1024];
2182 char buffer[JVM_MAXPATHLEN];
2183 void* library = NULL;
2184 jint result = JNI_ERR;
2186 // get agent name and options
2187 const char* agent = op->arg(0);
2188 const char* absParam = op->arg(1);
2189 const char* options = op->arg(2);
2191 // The abs paramter should be "true" or "false"
2192 bool is_absolute_path = (absParam != NULL) && (strcmp(absParam,"true")==0);
2195 // If the path is absolute we attempt to load the library. Otherwise we try to
2196 // load it from the standard dll directory.
2198 if (is_absolute_path) {
2199 library = os::dll_load(agent, ebuf, sizeof ebuf);
2200 } else {
2201 // Try to load the agent from the standard dll directory
2202 if (os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(),
2203 agent)) {
2204 library = os::dll_load(buffer, ebuf, sizeof ebuf);
2205 }
2206 if (library == NULL) {
2207 // not found - try local path
2208 char ns[1] = {0};
2209 if (os::dll_build_name(buffer, sizeof(buffer), ns, agent)) {
2210 library = os::dll_load(buffer, ebuf, sizeof ebuf);
2211 }
2212 }
2213 }
2215 // If the library was loaded then we attempt to invoke the Agent_OnAttach
2216 // function
2217 if (library != NULL) {
2219 // Lookup the Agent_OnAttach function
2220 OnAttachEntry_t on_attach_entry = NULL;
2221 const char *on_attach_symbols[] = AGENT_ONATTACH_SYMBOLS;
2222 for (uint symbol_index = 0; symbol_index < ARRAY_SIZE(on_attach_symbols); symbol_index++) {
2223 on_attach_entry =
2224 CAST_TO_FN_PTR(OnAttachEntry_t, os::dll_lookup(library, on_attach_symbols[symbol_index]));
2225 if (on_attach_entry != NULL) break;
2226 }
2228 if (on_attach_entry == NULL) {
2229 // Agent_OnAttach missing - unload library
2230 os::dll_unload(library);
2231 } else {
2232 // Invoke the Agent_OnAttach function
2233 JavaThread* THREAD = JavaThread::current();
2234 {
2235 extern struct JavaVM_ main_vm;
2236 JvmtiThreadEventMark jem(THREAD);
2237 JvmtiJavaThreadEventTransition jet(THREAD);
2239 result = (*on_attach_entry)(&main_vm, (char*)options, NULL);
2240 }
2242 // Agent_OnAttach may have used JNI
2243 if (HAS_PENDING_EXCEPTION) {
2244 CLEAR_PENDING_EXCEPTION;
2245 }
2247 // If OnAttach returns JNI_OK then we add it to the list of
2248 // agent libraries so that we can call Agent_OnUnload later.
2249 if (result == JNI_OK) {
2250 Arguments::add_loaded_agent(agent, (char*)options, is_absolute_path, library);
2251 }
2253 // Agent_OnAttach executed so completion status is JNI_OK
2254 st->print_cr("%d", result);
2255 result = JNI_OK;
2256 }
2257 }
2258 return result;
2259 }
2261 ////////////////////////////////////////////////////////////////////////////////////////////////
2263 // Setup current current thread for event collection.
2264 void JvmtiEventCollector::setup_jvmti_thread_state() {
2265 // set this event collector to be the current one.
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 configure for event collection
2269 guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state");
2270 if (is_vm_object_alloc_event()) {
2271 _prev = state->get_vm_object_alloc_event_collector();
2272 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this);
2273 } else if (is_dynamic_code_event()) {
2274 _prev = state->get_dynamic_code_event_collector();
2275 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this);
2276 }
2277 }
2279 // Unset current event collection in this thread and reset it with previous
2280 // collector.
2281 void JvmtiEventCollector::unset_jvmti_thread_state() {
2282 JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state();
2283 if (state != NULL) {
2284 // restore the previous event collector (if any)
2285 if (is_vm_object_alloc_event()) {
2286 if (state->get_vm_object_alloc_event_collector() == this) {
2287 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev);
2288 } else {
2289 // this thread's jvmti state was created during the scope of
2290 // the event collector.
2291 }
2292 } else {
2293 if (is_dynamic_code_event()) {
2294 if (state->get_dynamic_code_event_collector() == this) {
2295 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev);
2296 } else {
2297 // this thread's jvmti state was created during the scope of
2298 // the event collector.
2299 }
2300 }
2301 }
2302 }
2303 }
2305 // create the dynamic code event collector
2306 JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(NULL) {
2307 if (JvmtiExport::should_post_dynamic_code_generated()) {
2308 setup_jvmti_thread_state();
2309 }
2310 }
2312 // iterate over any code blob descriptors collected and post a
2313 // DYNAMIC_CODE_GENERATED event to the profiler.
2314 JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() {
2315 assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events");
2316 // iterate over any code blob descriptors that we collected
2317 if (_code_blobs != NULL) {
2318 for (int i=0; i<_code_blobs->length(); i++) {
2319 JvmtiCodeBlobDesc* blob = _code_blobs->at(i);
2320 JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end());
2321 FreeHeap(blob);
2322 }
2323 delete _code_blobs;
2324 }
2325 unset_jvmti_thread_state();
2326 }
2328 // register a stub
2329 void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) {
2330 if (_code_blobs == NULL) {
2331 _code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(1,true);
2332 }
2333 _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end));
2334 }
2336 // Setup current thread to record vm allocated objects.
2337 JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() : _allocated(NULL) {
2338 if (JvmtiExport::should_post_vm_object_alloc()) {
2339 _enable = true;
2340 setup_jvmti_thread_state();
2341 } else {
2342 _enable = false;
2343 }
2344 }
2346 // Post vm_object_alloc event for vm allocated objects visible to java
2347 // world.
2348 JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() {
2349 if (_allocated != NULL) {
2350 set_enabled(false);
2351 for (int i = 0; i < _allocated->length(); i++) {
2352 oop obj = _allocated->at(i);
2353 if (ServiceUtil::visible_oop(obj)) {
2354 JvmtiExport::post_vm_object_alloc(JavaThread::current(), obj);
2355 }
2356 }
2357 delete _allocated;
2358 }
2359 unset_jvmti_thread_state();
2360 }
2362 void JvmtiVMObjectAllocEventCollector::record_allocation(oop obj) {
2363 assert(is_enabled(), "VM object alloc event collector is not enabled");
2364 if (_allocated == NULL) {
2365 _allocated = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(1, true);
2366 }
2367 _allocated->push(obj);
2368 }
2370 // GC support.
2371 void JvmtiVMObjectAllocEventCollector::oops_do(OopClosure* f) {
2372 if (_allocated != NULL) {
2373 for(int i=_allocated->length() - 1; i >= 0; i--) {
2374 if (_allocated->at(i) != NULL) {
2375 f->do_oop(_allocated->adr_at(i));
2376 }
2377 }
2378 }
2379 }
2381 void JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(OopClosure* f) {
2382 // no-op if jvmti not enabled
2383 if (!JvmtiEnv::environments_might_exist()) {
2384 return;
2385 }
2387 // Runs at safepoint. So no need to acquire Threads_lock.
2388 for (JavaThread *jthr = Threads::first(); jthr != NULL; jthr = jthr->next()) {
2389 JvmtiThreadState *state = jthr->jvmti_thread_state();
2390 if (state != NULL) {
2391 JvmtiVMObjectAllocEventCollector *collector;
2392 collector = state->get_vm_object_alloc_event_collector();
2393 while (collector != NULL) {
2394 collector->oops_do(f);
2395 collector = (JvmtiVMObjectAllocEventCollector *)collector->get_prev();
2396 }
2397 }
2398 }
2399 }
2402 // Disable collection of VMObjectAlloc events
2403 NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) {
2404 // a no-op if VMObjectAlloc event is not enabled
2405 if (!JvmtiExport::should_post_vm_object_alloc()) {
2406 return;
2407 }
2408 Thread* thread = ThreadLocalStorage::thread();
2409 if (thread != NULL && thread->is_Java_thread()) {
2410 JavaThread* current_thread = (JavaThread*)thread;
2411 JvmtiThreadState *state = current_thread->jvmti_thread_state();
2412 if (state != NULL) {
2413 JvmtiVMObjectAllocEventCollector *collector;
2414 collector = state->get_vm_object_alloc_event_collector();
2415 if (collector != NULL && collector->is_enabled()) {
2416 _collector = collector;
2417 _collector->set_enabled(false);
2418 }
2419 }
2420 }
2421 }
2423 // Re-Enable collection of VMObjectAlloc events (if previously enabled)
2424 NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() {
2425 if (was_enabled()) {
2426 _collector->set_enabled(true);
2427 }
2428 };
2430 JvmtiGCMarker::JvmtiGCMarker() {
2431 // if there aren't any JVMTI environments then nothing to do
2432 if (!JvmtiEnv::environments_might_exist()) {
2433 return;
2434 }
2436 if (JvmtiExport::should_post_garbage_collection_start()) {
2437 JvmtiExport::post_garbage_collection_start();
2438 }
2440 if (SafepointSynchronize::is_at_safepoint()) {
2441 // Do clean up tasks that need to be done at a safepoint
2442 JvmtiEnvBase::check_for_periodic_clean_up();
2443 }
2444 }
2446 JvmtiGCMarker::~JvmtiGCMarker() {
2447 // if there aren't any JVMTI environments then nothing to do
2448 if (!JvmtiEnv::environments_might_exist()) {
2449 return;
2450 }
2452 // JVMTI notify gc finish
2453 if (JvmtiExport::should_post_garbage_collection_finish()) {
2454 JvmtiExport::post_garbage_collection_finish();
2455 }
2456 }