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