How can i use JVMPI to trace all the classes loaded by a Java program?

Davanum Srinivas

Here's the code:

#include <jvmpi.h>

// global jvmpi interface pointer
static JVMPI_Interface *jvmpi_interface;

// function for handling event notification
void notifyEvent(JVMPI_Event *event) {
  switch(event->event_type) {
  case JVMPI_EVENT_CLASS_LOAD: 
    fprintf(stderr, "myprofiler> Class Load : %s
", event->u.class_load.class_name);
    break;
  }                       
}

// profiler agent entry point
extern "C" { 
  JNIEXPORT jint JNICALL JVM_OnLoad(JavaVM *jvm, char *options, void *reserved) {
    fprintf(stderr, "myprofiler> initializing ..... 
");
    
    // get jvmpi interface pointer
    if ((jvm->GetEnv((void **)&jvmpi_interface, JVMPI_VERSION_1)) < 0) {
      fprintf(stderr, "myprofiler> error in obtaining jvmpi interface pointer
");
      return JNI_ERR;
    } 
    
    // initialize jvmpi interface
    jvmpi_interface->NotifyEvent = notifyEvent;
    
    // enabling class load event notification
    jvmpi_interface->EnableEvent(JVMPI_EVENT_CLASS_LOAD, NULL);

    fprintf(stderr, "myprofiler> .... ok 

");
    return JNI_OK;
  }
}
More information can be found at:
http://java.sun.com/j2se/1.3/docs/guide/jvmpi/jvmpi.html
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact