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.

[3207 byte] By [dash_huana] at [2007-11-27 8:59:28]
# 1

I assume you know that JVMPI has been slated for removal for several years. Its replacement is the JVM Tool Interface. JVMPI was deprecated in J2SE 5.0 and was disabled in Java SE 6.0. The code has since been completely removed. If you are working on a new tool then perhaps it would be better to start with JVM TI now rather than having to create two versions.

alan.batemana at 2007-7-12 21:26:54 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

alan.bateman

Thank you for your advice, but I'm working on an old profiler which used JVMPI, rather than writting a profiler of my own, the above codes are a simplified example to make the problem clearer and easier to understand. We are considering to change to other ways, if it couldn't be settled. But I still wish to solve this problem or at least get to know why is it so.

dash_huana at 2007-7-12 21:26:54 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

I should strongly recommend moving from JVMPI to JVM TI if you can. The reason is that JVMPI has been deprecated since 5.0 and can not be used with Java SE 6 and newer.

Anyway, the issue you have may be due to the signal you are using. I'm not sure which operating system this is but on Linux the USR2 signal is used for the suspend/resume implementation. Have you looked at the Troubleshooting Guide or the Signal Chaining page for details on how signals are used and how to chain handlers? Another idea is to remove the signal handling code implementation and to use the data dump event. This is supported by both JVMPI and JVM TI so that you get an event (via the QUIT signal).

alan.batemana at 2007-7-12 21:26:54 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...