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