i'm working on JVMTI agent and I want to identify same thread on method enter and exit. I'm able to obtain thread name, but that's not sufficient.
Imagine you have a method like this:
public class Main {
public static void myMethod() {
System.out.println("doing something as " + Thread.currentThread().getName());
Thread.currentThread().setName("SomethingDifferent");
System.out.println("doing something as same thread " + Thread.currentThread().getName());
}
}
So entering this method will have one name and exiting this thread have different name.
When using JVMTI like this:
static void JNICALL callback_on_method_entry(jvmtiEnv *jvmti, JNIEnv* env,
jthread thread, jmethodID method)
{
...
(*jvmti)->GetThreadInfo(jvmti, thread, &info);
...
}
static void JNICALL callback_on_method_exit(jvmtiEnv *jvmti, JNIEnv *env, jthread thread, jmethodID method, jboolean was_popped_by_exception, jvalue return_value)
{
...
(*jvmti)->GetThreadInfo(jvmti, thread, &info);
...
}
Each info
will report different thread name and I want to have the same identifier for them.
How can I get the same identifier for the thread ?
One solution can be to get field value of referenced Thread
(tid
).
How to do that ? I can iterat through the heap but I cannot get the field name.
I finally found another simple soulution:
Because the entry/exit callbacks are running in the same thread, it's possible to use pthread_self()
and cast it e.g. to unsigned int
. It's not the same tid
as you can find in java, but you will get the unique number for thread although the name changes.