9 Replies Latest reply: Sep 5, 2008 11:54 AM by 843810 RSS

    why JVMTI agent library doesn't load properly using System.loadLibrary()?

    843810
      Hi all,
      I'm working on the development of a profiler tool using java agent (to access premain()), asm (to instrument bytecode)and JVMTI agent (to access functions which JDK doesn't provide). I'm trying to load JVMTI library using System.loadLibrary() but it throws exception "EXCEPTION_ACCESS_VIOLATION (0xc0000005)" while loading the same using command line VM arguments works fine. I'm trying to load through code as after loading javaagent through command line I need to decide in code whether JVMTI agent is to be used and load the same.
      Can anyone please tell me the reason for this unusual behavior and alternative to solve the problem?
        • 1. Re: why JVMTI agent library doesn't load properly using System.loadLibrary()?
          843810
          you can not load jvmti agent from System.loadLibrary(). It does not call back Agent_OnLoad() so you won't be able to initialize the jvmti agent.

          Take a look at this document.

          http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/invocation.html


          If you want to load your agent from your program than it may be possible to use the
          attach on demand API. Take a look at this.

          http://java.sun.com/javase/6/docs/jdk/api/attach/spec/index.html
          • 2. Re: why JVMTI agent library doesn't load properly using System.loadLibrary()?
            843810
            it is possible to also initialise the JVMTI agent by using the -agentlib option and getting the jvmtiEnv in the Agent_OnLoad() call and then load the same library via System.loadLibrary(). The jvmtiEnv can then be used in calls to the loaded library via JNI.
            • 3. Re: why JVMTI agent library doesn't load properly using System.loadLibrary()?
              843810
              Loading library using System.loadLibrary() after it is loaded from -agentlib is no op.
              • 4. Re: why JVMTI agent library doesn't load properly using System.loadLibrary()?
                843810
                try looking at this....
                http://www.javaworld.com/javaworld/javatips/jw-javatip92.html
                • 5. Re: why JVMTI agent library doesn't load properly using System.loadLibrary()?
                  843810
                  Libraries are loaded only once. OS will not load more than one copy of same shared library. If it is already loaded then it will load and create another
                  copy of it. Well why do you want to load the same library two times?
                  Link you sent is for the jvmpi and it has been removed in the latest version java and it was an experimental profiling api. You do not have to load
                  library using System.loadLibrary() for jvmti agent.
                  • 6. Re: why JVMTI agent library doesn't load properly using System.loadLibrary()?
                    843810
                    all i can say is that this works (for pi and ti) - true, if you call System.loadLibrary twice the second call will say the lib is already loaded, but as this is an OS limit it applies equally to jvmpi and jvmti.
                    • 7. Re: why JVMTI agent library doesn't load properly using System.loadLibrary()?
                      843810
                      If you want to do it your way. No problem. There is no harm but it is unnecessary. I would not recommend it.
                      • 8. Re: why JVMTI agent library doesn't load properly using System.loadLibrary()?
                        843810
                        Ok, I understand that we cant load JVMTI agent library using System.loadLibrary(). But I think there is some misunderstanding here. First I'm not loading the same library twice. I'm using two agents: javaagent( for accessing premain()) and JVMTI native agent(dll) with java wrapper (to use JVMTI functions for profiling).
                        I'm loading javaagent jar using command line option (java javaagent:PATH_TO_jar ...). For JVMTI agent I wanted to load library(dll) in code because I want to decide in premain() whether to use JVMTI functions( eg. getCPUTime()) or JDK methods(eg. nanoTime()) for insertion in byte code (for insertion I'm using ASM).
                        Is there any alternative for this (apart from using java6)?

                        One thing I tried was to load both javaagent jar and JVMTI dll library using same command line option (java javaagent:PATH_TO_jar agentlib:JVMTIAgentLibName ...) but it gives some other errors. Can we load both this way?
                        If this is right, then I can open another thread for this different error.
                        • 9. Re: why JVMTI agent library doesn't load properly using System.loadLibrary()?
                          843810
                          vishal.aim wrote:
                          Ok, I understand that we cant load JVMTI agent library using System.loadLibrary(). But I think there is some misunderstanding here. First I'm not loading the same library twice. I'm using two agents: javaagent( for accessing premain()) and JVMTI native agent(dll) with java wrapper (to use JVMTI functions for profiling).
                          I know you are not loading it twice.
                          I'm loading javaagent jar using command line option (java javaagent:PATH_TO_jar ...). For JVMTI agent I wanted to load library(dll) in code because I want to decide in premain() whether to use JVMTI functions( eg. getCPUTime()) or JDK methods(eg. nanoTime()) for insertion in byte code (for insertion I'm using ASM).
                          Is there any alternative for this (apart from using java6)?
                          java.lang.intrumentation was introduced in 1.5 version of java. If you do not want to use java 6 you can try 1.5.

                          >
                          One thing I tried was to load both javaagent jar and JVMTI dll library using same command line option (java javaagent:PATH_TO_jar agentlib:JVMTIAgentLibName ...) but it gives some other errors. Can we load both this way?
                          If this is right, then I can open another thread for this different error.
                          You should be able to load both the agent. Send the error details we will take a look at it. Open a new thread for this.


                          It looks like it is possible to load an jvmti agent from System.loadLibrary() without loading it from an command line option.
                          If you replace the Agent_OnLoad with JNI_OnLoad in jvmti agent library and System.loadLibrary() will make JNI_OnLoad() callback and you can get jvmtiEnv in this callback using GetEnv(JVMTI_...). Once you have jvmtiEnv then you should be able to make jvmti calls. This is not documented and I am not sure if this is supported but it works now.