java program stopped running when using JVMPI
Hi!
I'm writting a very simple profiler using JVMPI. I intended to pause/continue it by sending signal, like kill -SIGUSR2 PID, to it, but whenever I did this, the java program that ran with the profiler stopped running, however the profiler kept running properly.
Here's the code of the simple profiler and the testing java program.
//myprofiler.cc
#include <jvmpi.h>
#include <pthread.h>
#include <stdio.h>
#include <signal.h>
void sig_handler_pause(int);
static JVMPI_Interface *jvmpi_interface;
void notifyEvent(JVMPI_Event *event) {
switch(event->event_type) {
case JVMPI_EVENT_CLASS_LOAD:
fprintf(stderr, "myprofiler> Class Load : %s\n", event->u.class_load.class_name);
break;
case JVMPI_EVENT_OBJECT_ALLOC:
fprintf(stderr, "myprofiler>object alloc \n");
break;
}
}
extern "C" {
JNIEXPORT jint JNICALL JVM_OnLoad(JavaVM *jvm, char *options, void *reserved) {
fprintf(stderr, "myprofiler> initializing ..... \n");
// get jvmpi interface pointer
if ((jvm->GetEnv((void **)&jvmpi_interface, JVMPI_VERSION_1)) < 0) {
fprintf(stderr, "myprofiler> error in obtaining jvmpi interface pointer\n");
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 \n\n");
signal(SIGUSR2,sig_handler_pause);
return JNI_OK;
}
}
void start(){
fprintf(stderr,"start...\n");
jvmpi_interface->EnableEvent(JVMPI_EVENT_OBJECT_ALLOC, NULL);
jvmpi_interface->EnableEvent(JVMPI_EVENT_CLASS_LOAD,NULL);
}
void pause(){
fprintf(stderr,"pause...\n");
jvmpi_interface->DisableEvent(JVMPI_EVENT_OBJECT_ALLOC, NULL);
jvmpi_interface->DisableEvent(JVMPI_EVENT_CLASS_LOAD,NULL);
}
int status=0;
void sig_handler_pause(int sig){
if(status == 0){
start();
status = 1;
}
else if(status == 1){
pause();
status = 0;
}
}
//Test.java
public class Test {
public static void main(String[] args) {
try {
int i = 0;
while(true)
{
Thread.sleep(1000);
System.out.println(i++);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Here're some notes:
1)If I didn't call
jvmpi_interface->EnableEvent(JVMPI_EVENT_OBJECT_ALLOC, NULL);
in "start()" function, the java program Test ran properly after I sent signal to the process by using kill command in an command line;
or
2) If I delete "Thread.sleep(1000)" in Test.java, then whether I call
jvmpi_interface->EnableEvent(JVMPI_EVENT_OBJECT_ALLOC, NULL);
in "start()" or not, the Test program ran properly after I sent signal to the process.
It seems to be a thread dead lock, but I'm not sure. Can any body help?Thanks very much.

