5

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.

czs
  • 185
  • 2
  • 12
  • ... is there a question hidden somewhere or did you simply want to share your experiences?? – specializt Sep 22 '15 at 13:35
  • You have to look at the title. But for sure I added questions into the article. – czs Sep 22 '15 at 14:03
  • Thats not a question, its a statement. Plus : completely excluding your actual questions(s) inside of your content is guaranteed to get you exactly zero answers. This is not a blog or forum of some kind, people WANT to help but also refuse to do so if you cant even assemble a simple question .... most of the time – specializt Sep 22 '15 at 14:05

2 Answers2

1

One solution, as you pointed out, would be to use GetFieldName. That requires you to lookup the jfieldid, which can be really annoying.

The way i've seen others do it, is to simply assign their own ID's and stash it in the thread local storage. See JavaThreadLayer.cpp from UofO's TAU project, specifically the JavaThreadLayer::GetThreadId() function.

lscoughlin
  • 2,327
  • 16
  • 23
  • That seems like a solution. And what about to use `pthread_self()` since the callback method is (probably) called in the same thread? My early tests shows that it works as expected. – czs Sep 25 '15 at 13:26
  • I can't see why that wouldn't be a perfectly reasonable solution as well -- coming out of JVMTI i _think_ it will _always_ be the same thread -- sort of a requirement to support debugger architectures since it's more then a little hard to asynchronous stack introspection ^^ – lscoughlin Sep 28 '15 at 11:51
1

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.

czs
  • 185
  • 2
  • 12