How to uniquely identify a stream in jvmti

I am working on a JVMTI agent, and I want to identify the same thread when the method enters and exits. I can get the name of the stream, but this is not enough.

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()); } } 

Thus, the input of this method will have one name and the output from this stream has a different name.

When using JVMTI, for example:

 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 a different stream name, and I want to have the same identifier for them.

How can I get the same id for the stream?

One solution would be to get the field value for the Thread ( tid ) link. How to do it? I can iterate through the heap, but I cannot get the field name.

+5
source share
2 answers

One solution, as you pointed out, would be to use GetFieldName. This requires you to look for jfieldid, which can be very annoying.

The way I saw others is to simply assign their own identifier and put it in a local thread store. See JavaThreadLayer.cpp from the UofO TAU project, in particular the JavaThreadLayer::GetThreadId() function.

+1
source

Finally I found another simplicity:

Since I / O callbacks are performed on the same thread, you can use pthread_self() and drop it, for example. until unsigned int . This is not the same as tid , as you can find in java, but you will get a unique number for the stream, although the name changes.

+1
source

Source: https://habr.com/ru/post/1232028/


All Articles