0 Replies Latest reply: Dec 5, 2013 5:43 AM by user12956197 RSS

    Failed to load agent library

    user12956197

      I wrote a simple agent in windows with mingw(by netbeans) but failed when using it in java test program

      1. create a c++ dynamic library in netbeans

          my build tool is mingw. I add c:/Program Files/Java/jdk1.7.0_10/include  and c:/Program Files/Java/jdk1.7.0_10/include/win32 to c++ compiler Including Directories

         set Build->Linker->Additional Options to -mwindows

       

      2. add Agent.cpp

      #include <jvmti.h>

      #include <string>

      #include <cstring>

      #include <iostream>

      #include <list>

      #include <map>

      #include <set>

      #include <stdlib.h>

      #include <jni_md.h>

       

      JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options,

        void *reserved) {

        jvmtiEnv *jvmti;

        jint result = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);

        if (result != JNI_OK) {

        printf("ERROR: Unable to access JVMTI!\n");

        }

        jvmtiError err = (jvmtiError) 0;

        jclass *classes;

        jint count;

       

        err = jvmti->GetLoadedClasses(&count, &classes);

        if (err) {

        printf("ERROR: JVMTI GetLoadedClasses failed!\n");

        }

        for (int i = 0; i < count; i++) {

        char *sig;

        jvmti->GetClassSignature(classes[i], &sig, NULL);

        printf("cls sig=%s\n", sig);

        }

        return err;

      }

       

      JNIEXPORT void JNICALL Agent_OnUnload(JavaVM *vm) {

        // nothing to do

      }

       

      3. build it successfully

      the build scripts:

       

      "/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf

      make[1]: Entering directory `/c/Users/lili14/Documents/NetBeansProjects/JVMTIAgent'

      "/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/MinGW_1-Windows/libJVMTIAgent.dll

      make[2]: Entering directory `/c/Users/lili14/Documents/NetBeansProjects/JVMTIAgent'

      mkdir -p build/Debug/MinGW_1-Windows

      rm -f build/Debug/MinGW_1-Windows/Agent.o.d

      g++.exe    -c -g -I../../../../../Program\ Files/Java/jdk1.7.0_10/include -I../../../../../Program\ Files/Java/jdk1.7.0_10/include/win32  -MMD -MP -MF build/Debug/MinGW_1-Windows/Agent.o.d -o build/Debug/MinGW_1-Windows/Agent.o Agent.cpp

      mkdir -p dist/Debug/MinGW_1-Windows

      g++.exe     -mwindows -shared -o dist/Debug/MinGW_1-Windows/libJVMTIAgent.dll build/Debug/MinGW_1-Windows/Agent.o 

      make[2]: Leaving directory `/c/Users/lili14/Documents/NetBeansProjects/JVMTIAgent'

      make[1]: Leaving directory `/c/Users/lili14/Documents/NetBeansProjects/JVMTIAgent'

       

      4. create a test java program

       

      import com.sun.tools.attach.VirtualMachine;

       

      public class TestAgentVMAttacher {

       

       

        public static void main(String[] args) throws Exception {

              String pid = "36132";

              String agentPath = "C:\\Users\\lili14\\Documents\\NetBeansProjects\\JVMTIAgent\\dist\\Debug\\MinGW_1-Windows\\libJVMTIAgent.dll";

              VirtualMachine virtualMachine = com.sun.tools.attach.VirtualMachine.attach(pid);

              virtualMachine.loadAgentPath(agentPath, null);

              virtualMachine.detach();

          

        }

       

       

      }

       

      pid is set correctly to a running java program

      but it throws exception:

      Exception in thread "main" com.sun.tools.attach.AgentLoadException: Failed to lo

      ad agent library

        at sun.tools.attach.WindowsVirtualMachine.execute(WindowsVirtualMachine.java:11

      0)

        at sun.tools.attach.HotSpotVirtualMachine.loadAgentLibrary(HotSpotVirtualMachin

      e.java:58)

        at sun.tools.attach.HotSpotVirtualMachine.loadAgentPath(HotSpotVirtualMachine.j

      ava:88)

        at test.TestAgentVMAttacher.main(TestAgentVMAttacher.java:11)

       

      my environment:

      win7(32bit) + oracle jdk 1.7.0 update 10