Jigar Joshi
Jigar Joshi

Reputation: 240928

Access thread local variable in JVMTI Agent

I have got the hold of a jthread in particular event in JVMTI agent. How do I get:

from within the JVMTI agent?

Upvotes: 1

Views: 933

Answers (2)

Debugger
Debugger

Reputation: 21

For accessing local variables you may call GetLocalVariabletable() to retrieve a table (array) of variable entries and using slot number for a variable obtained from variable entry you may call getlocalXXX series of functions to obtain the value of the variable depending on the variables signature (also obtained from variable table entry )and set it using setlocalXXX() functions.To further read object subclasses i.e class objects you may use jni series of functions upon the jobject retrieved using getlocalobject.

Upvotes: 1

apangin
apangin

Reputation: 98380

jthread is a regular JNI reference to java.lang.Thread object. You can use it to access fields and invoke methods on Thread instance, e.g.

    jclass threadClass = jniEnv->FindClass("java/lang/Thread");
    jmethodID methodID = jniEnv->GetMethodID(threadClass, "getId", "()J");
    jlong id = jniEnv->CallLongMethod(thread, methodID);

Alternatively you may use JVMTI GetThreadInfo function to get thread name as char*.

Thread locals of a thread can be accesses through package-private threadLocals field.

Upvotes: 3

Related Questions