You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Daniel Diaz <Da...@inria.fr> on 2001/02/06 21:05:38 UTC

signals with JNI

Hi,

I have a problem under Linux loading the JVM since before I set a Handler to 
catch SIGSEGV signals. It seems that this causes some troubles when the JVM 
is loaded. This seems due to threads in Java. I saw several post from 
yourself about this topic.

I include my C test file (called x.c) and the command-line 
to compile it is:

gcc -o x /usr/java/jdk1.3/jre/lib/i386/client/libjvm.so x.c 
-I/usr/java/jdk1.3/include -I/usr/java/jdk1.3/include/linux -g -Wall

(assuming the JDK is installed in /usr/java/jdk1.3)

Here is the C file

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <jni.h>

JNIEnv *env;
JavaVM *vm;
jclass cls;

void
Load_JVM(void)
{
  JavaVMOption options[2];
  JavaVMInitArgs vm_args;

  options[0].optionString = "-Djava.class.path=."; /* user classes */

  vm_args.version = JNI_VERSION_1_2;
  vm_args.options = options;
  vm_args.nOptions = 1;
  vm_args.ignoreUnrecognized = 1;
    
  printf("READY TO LOAD THE JVM\n");
  if (JNI_CreateJavaVM(&vm, (void **)&env, (void *)&vm_args) < 0)
    {
      fprintf(stderr, "cannot load the JVM\n");
      exit(1);
    }

  printf("JVM LOADED\n");
}


void
SIGSEGV_Handler(void)

{
 fprintf(stderr, "Received SIGSEGV\n");
 exit(2);
}


int
main(int argc, char *argv[])
{
  signal(SIGSEGV, (void (*)()) SIGSEGV_Handler);
  Load_JVM();
  return 0;
}

It simply installs a handler for SIGSEGV and then loads the JVM. If you 
comment the line signal(SIGSEGV, (void (*)()) SIGSEGV_Handler) in the main(), 
this works properly but with the handler this crashes with:

READY TO LOAD THE JVM
#
# HotSpot Virtual Machine Error, Internal Error
# Please report this error at
# http://java.sun.com/cgi-bin/bugreport.cgi
#
# Error ID: 4F533F4C494E55580E4350500570
#
# Problematic Thread: Received SIGSEGV

Another strange problem occurs if you replace this line by
  system("uname -a");

after executing system() the JVM loading is blocked (in a sigsuspend I think).

Could you help me on this please ?

-- 
===============================================
                 Daniel Diaz
University of Paris 1      INRIA Rocquencourt
75013 Paris FRANCE      78153 Le Chesnay FRANCE
        web: http://pauillac.inria.fr/~diaz
        email: Daniel.Diaz@inria.fr
===============================================



Re: signals with JNI

Posted by Pier Fumagalli <pi...@betaversion.org>.
Pier Fumagalli <pi...@betaversion.org> wrote:
> 
> So far the same code you wrote works with SIGHUP, SIGKILL and SIGUSR?...

Uh uh uh... So damn stupid Pier :) Now I get it, it's not because of
SIGSEGV, but it's because a SIGSEGV is raised when the VM is created... :)

Now I remember when I saw that message in the first place...

Basically, what happens is that the "main" thread, to be able to receive
signals, must be "detached" from the VM... By "detached" I don't mean
calling the JNI_Detach_Thread() function, but it _MUST_ return before any
signal can be caught (and this is a bug in the invocation stuff!!!!)...

What you can do is:
1) create the VM
2) set the SIGSEGV handler
3) create a new Thread() that is the one which will call your main() method

In this order it "should" work... (In theory, as always!)

    Pier

-- 
Pier Fumagalli                                 <ma...@betaversion.org>



Re: signals with JNI

Posted by Pier Fumagalli <pi...@betaversion.org>.
Daniel Diaz <Da...@inria.fr> wrote:
> 
> Another strange problem occurs if you replace this line by
> system("uname -a");
> 
> after executing system() the JVM loading is blocked (in a sigsuspend I think).
> 
> Could you help me on this please ?

Hehehe :) You're going down hard-core on the VM :)

Well, I don't think you'll be actually able to TRAP a SEGV signal, I mean,
that signal means "segment violation" and it's the worst thing you can
possibly try to debug...

What I think is happening is that the VM itself (for debugging purposes, I
believe with the Debugging Native Interface), tries to set a handler to
SIGSEGV itself, and if it has already been set, it will just panic...

Aka, all it means is "don't mess around too much with that" :)

I don't know, though! I should have to go and look into the Hotspot
sources...

So far the same code you wrote works with SIGHUP, SIGKILL and SIGUSR?...

    Pier

-- 
Pier Fumagalli                                 <ma...@betaversion.org>



Re: signals with JNI

Posted by cm...@yahoo.com.
> Hi,
> 
> I have a problem under Linux loading the JVM since before I set a Handler to 
> catch SIGSEGV signals. It seems that this causes some troubles when the JVM 
> is loaded. This seems due to threads in Java. I saw several post from 
> yourself about this topic.

Long ago... Did you tried with a different VM ? 


> It simply installs a handler for SIGSEGV and then loads the JVM. If you 
> comment the line signal(SIGSEGV, (void (*)()) SIGSEGV_Handler) in the main(), 
> this works properly but with the handler this crashes with:
> 
> READY TO LOAD THE JVM
> #
> # HotSpot Virtual Machine Error, Internal Error
> # Please report this error at
> # http://java.sun.com/cgi-bin/bugreport.cgi
> #
> # Error ID: 4F533F4C494E55580E4350500570
> #
> # Problematic Thread: Received SIGSEGV

I guess you should do exactly that ( after you try with 
a different VM and works as you expect :-)

Costin