7 Replies Latest reply: Nov 14, 2007 10:24 AM by 800369 RSS

    Access an object instance

    843810
      Hi All,

      Using JVMTI, is there any way to get access to an object instance, see it's properties, call it's methods, etc. It looks like all the iterate callback functions just give information like, tags, object size, etc., but there seems to be no way to gain access to the object instance itself. Am I missing something?

      many thanks!

      -B
        • 1. Re: Access an object instance
          800369
          JVM TI is designed to be used in conjunction with JNI so you can invoke methods, etc. The Iterate* function you are looking at there to support agents doing heap profiling.
          • 2. Re: Access an object instance
            843810
            Thanks for your reply!

            Yeah I know about JNI. I can instantiate new objects, call their methods, etc. But I don't understand how I can get access to an existing object already in memory using JVMTI. Surely it's possible. Any insight into this is greatly appreciated.

            thanks,

            B
            • 3. Re: Access an object instance
              843810
              Well... let me show some code I have tried and describe the results I get.

              jmethodID this_id = env->GetMethodID(win_clazz,"<init>","()V");

              jint count;
              jvmtiLocalVariableEntry* var_entry;
              jvmtiError err = jvmti->GetLocalVariableTable(this_id,&count,&var_entry);

              if(err != JVMTI_ERROR_NONE)
              {
              char buf[20];
              itoas(err,buf,20,10);
              OutputDebugString("ERROR in GetLocalVariableTable");
              OutputDebugString(buf);
              }

              jobject obj;
              err = jvmti->GetLocalObject(NULL,var_entry->length,var_entry->slot,&obj);

              if(err != JVMTI_ERROR_NONE)
              {
              char buf[20];
              itoas(err,buf,20,10);
              OutputDebugString("ERROR in GetLocalObject");
              OutputDebugString(buf);
              }

              I believe that something like this would work to achieve an object reference, however I am getting a jvmtiError = 99 for both of these functions. Error code 99 is JVMTI_ERROR_MUST_POSSESS_CAPABILITY, however I do have this code before these 2 function calls:

              jvmtiCapabilities capabilities;

              capabilities.can_tag_objects = 1;
              capabilities.can_access_local_variables = 1;
              jvmti->AddCapabilities(&capabilities);

              which should be all that is required according to the JVMTI spec.

              Is there a restriction when connecting the agent to a VM that is already running?

              thanks,

              B
              • 4. Re: Access an object instance
                843810
                Once again, I answer my own question. :) Maybe it will help someone else.

                What I found out is that you have to have certain "capabilities" set ( or permissions ) and those cannot be set in the live phase. They must be set in the load phase. So basically you have to start your agent with the target VM, you can't just connect to it get all of the JVMTI features. It's a security feature.
                • 5. Re: Access an object instance
                  800369
                  It's not a security feature but rather than some capabilities require to be set before the VM is initialized. For example, in the HotSpot VM, if you enable some debugging related capabilities in the onload phase then it generates a different interpreter at startup. In jdk6 the implementation was improved to allow for more capabilities to be enabled in the live phase, and in the future it will be improved further. As regards your original question - JVM TI callbacks/events passed jobject and other JNI references to the agent so that allows you to call JNI functions.
                  • 6. Re: Access an object instance
                    843810
                    Maybe it's not an explicit security feature, but imagine if I could connect to your VM and call methods, see values, etc. That seems like a big security hole to me. However if I am in control of launching the application with my agent, then it's reasonable to be able to do those things.

                    So you say there will be more capabilities for the live phase in the future? Do you know which ones it will be?

                    thanks,

                    B
                    • 7. Re: Access an object instance
                      800369
                      Whether you can connect and invoke methods isn't really anything to do with JVM TI but since you mention it, an important requirement for some tools is the ability to attach to a running application in a secure manner and load an agent into the VM. The NetBeans Profiler is one example that supports this feature. jconsole kinda does this too except it starts a management/JMX agent rather than a JVM TI agent. In jdk6 the implementation was updated to allow capabilities such as can_redefine_classes and can_tag_objects be turned on in the live phase. In the future I would expect this will be possible for many of the debugger related capabilities.