Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 40 additions & 33 deletions src/hotspot/share/prims/jvmtiExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ JvmtiExport::get_jvmti_interface(JavaVM *jvm, void **penv, jint version) {
JvmtiThreadState*
JvmtiExport::get_jvmti_thread_state(JavaThread *thread, bool allow_suspend) {
assert(thread == JavaThread::current(), "must be current thread");
assert(thread->thread_state() == _thread_in_vm, "thread should be in vm");
if (thread->is_vthread_mounted() && thread->jvmti_thread_state() == nullptr) {
JvmtiEventController::thread_started(thread);
if (allow_suspend && thread->is_suspended()) {
Expand Down Expand Up @@ -1826,58 +1827,64 @@ void JvmtiExport::post_method_entry(JavaThread *thread, Method* method, frame cu
}

void JvmtiExport::post_method_exit(JavaThread* thread, Method* method, frame current_frame) {
// At this point we only have the address of a "raw result" and
// we just call into the interpreter to convert this into a jvalue.
// The post_method_exit_transition always makes transition to vm and back
// where GC can happen. So it is needed to preserve result and then restore it
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// where GC can happen. So it is needed to preserve result and then restore it
// where GC can happen. So it is needed to preserve result and then restore it

// even if events are not actually posted.
// Saving oop_result into value.j is deferred until jvmti state is ready.
HandleMark hm(thread);
methodHandle mh(thread, method);

JvmtiThreadState *state = get_jvmti_thread_state(thread);

if (state == nullptr || !state->is_interp_only_mode()) {
// for any thread that actually wants method exit, interp_only_mode is set
return;
}

Handle result;
oop oop_result;
jvalue value;
value.j = 0L;

if (state->is_enabled(JVMTI_EVENT_METHOD_EXIT)) {
// At this point we only have the address of a "raw result" and
// we just call into the interpreter to convert this into a jvalue.
oop oop_result;
BasicType type = current_frame.interpreter_frame_result(&oop_result, &value);
assert(type == T_VOID || current_frame.interpreter_frame_expression_stack_size() > 0,
"Stack shouldn't be empty");
if (is_reference_type(type)) {
result = Handle(thread, oop_result);
value.l = JNIHandles::make_local(thread, result());
}
BasicType type = current_frame.interpreter_frame_result(&oop_result, &value);
assert(type == T_VOID || current_frame.interpreter_frame_expression_stack_size() > 0,
"Stack shouldn't be empty");
if (is_reference_type(type)) {
result = Handle(thread, oop_result);
}
post_method_exit_transition(thread, mh, type, result, value);
if (result.not_null() && !mh->is_native()) {
*(oop*)current_frame.interpreter_frame_tos_address() = result();
}
}

// Do not allow NotifyFramePop to add new FramePop event request at
// depth 0 as it is already late in the method exiting dance.
state->set_top_frame_is_exiting();

// Deferred transition to VM, so we can stash away the return oop before GC.
void JvmtiExport::post_method_exit_transition(JavaThread* thread, methodHandle mh,
BasicType type, Handle result, jvalue value) {
JvmtiThreadState* state; // should be initialized in vm state only
JavaThread* current = thread; // for JRT_BLOCK
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If thread is always the current thread then I suggest just calling it current in the first place.

JRT_BLOCK
post_method_exit_inner(thread, mh, state, false /* not exception exit */, current_frame, value);
state = get_jvmti_thread_state(thread);
if (state == nullptr || !state->is_interp_only_mode()) {
// The transition from vm to java
return;
}

if (state->is_enabled(JVMTI_EVENT_METHOD_EXIT)) {
// Deferred saving Object result into value.
if (is_reference_type(type)) {
value.l = JNIHandles::make_local(thread, result());
}
}

// Do not allow NotifyFramePop to add new FramePop event request at
// depth 0 as it is already late in the method exiting dance.
state->set_top_frame_is_exiting();

post_method_exit_inner(thread, mh, state, false /* not exception exit */, value);
JRT_BLOCK_END

// The JRT_BLOCK_END can safepoint in ThreadInVMfromJava desctructor. Now it is safe to allow
// adding FramePop event requests as no safepoint can happen before removing activation.
state->clr_top_frame_is_exiting();

if (result.not_null() && !mh->is_native()) {
// We have to restore the oop on the stack for interpreter frames
*(oop*)current_frame.interpreter_frame_tos_address() = result();
}
}

void JvmtiExport::post_method_exit_inner(JavaThread* thread,
methodHandle& mh,
JvmtiThreadState *state,
bool exception_exit,
frame current_frame,
jvalue& value) {
if (mh->jvmti_mount_transition() || thread->should_hide_jvmti_events()) {
return;
Expand Down Expand Up @@ -2107,7 +2114,7 @@ void JvmtiExport::notice_unwind_due_to_exception(JavaThread *thread, Method* met
// When these events are enabled code should be in running in interp mode.
jvalue no_value;
no_value.j = 0L;
JvmtiExport::post_method_exit_inner(thread, mh, state, true, thread->last_frame(), no_value);
JvmtiExport::post_method_exit_inner(thread, mh, state, true, no_value);
// The cached cur_stack_depth might have changed from the
// operations of frame pop or method exit. We are not 100% sure
// the cached cur_stack_depth is still valid depth so invalidate
Expand Down
7 changes: 6 additions & 1 deletion src/hotspot/share/prims/jvmtiExport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,16 @@ class JvmtiExport : public AllStatic {
// dependency information is complete or not.
static bool _all_dependencies_are_recorded;

static void post_method_exit_transition(JavaThread* thread,
methodHandle mh,
BasicType type,
Handle result,
jvalue value);

static void post_method_exit_inner(JavaThread* thread,
methodHandle& mh,
JvmtiThreadState *state,
bool exception_exit,
frame current_frame,
jvalue& value);

public:
Expand Down