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