src/share/vm/prims/jvmtiExport.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

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

mercurial