src/share/vm/prims/jvmtiExport.cpp

Wed, 31 Jan 2018 19:24:57 -0500

author
dbuck
date
Wed, 31 Jan 2018 19:24:57 -0500
changeset 9289
427b2fb1944f
parent 8415
d109bda16490
child 8604
04d83ba48607
permissions
-rw-r--r--

8189170: Add option to disable stack overflow checking in primordial thread for use with JNI_CreateJavaJVM
Reviewed-by: dcubed

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

mercurial