1

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

  • access to thread local variable
  • the thread name and id

from within the JVMTI agent?

AlBlue
  • 23,254
  • 14
  • 71
  • 91
jmj
  • 237,923
  • 42
  • 401
  • 438

2 Answers2

3

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.

apangin
  • 92,924
  • 10
  • 193
  • 247
  • [sorry for long pause], I tried getting threadId (with same code) from Java's Object's constructor (from native method) and I ran into [this](https://paste.ubuntu.com/16069205/), class `java/lang/Thread` has been loaded already ofcourse – jmj Apr 26 '16 at 18:48
  • @JigarJoshi ¯\\_(ツ)_/¯ – apangin Apr 26 '16 at 20:40
  • I can hardly tell what you've made wrong without any details. – apangin Apr 26 '16 at 20:40
  • sorry about that, [This is the code](https://paste.ubuntu.com/16071541/) which gets followed through during constructor of Object, after once VM has started, all User object will follow through here, agent is attached, all is good if I remove method call, I am happy to provide more detail, please let me know, Thanks! – jmj Apr 26 '16 at 21:57
  • @JigarJoshi `Thread.toString` apparently creates new objects => infinite recursion. – apangin Apr 26 '16 at 23:11
1

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.

Debugger
  • 21
  • 4