Skip to main content

Debugging JNI exception failures

12 replies [Last post]
cowwoc
Offline
Joined: 2003-08-24

Hi,

I keep on getting the much hated:

FATAL ERROR in native method: JNI call made with exception pending

error... Is there a way to get the JVM to output even more debugging information such that every single time a JNI exception is thrown, it outputs a message to the screen (kinda like -verbose)? I'd expect some sort of flag to enable this at JVM startup.

It really is extremely difficult to debug these errors otherwise because the JRE is full of JNI calls that don't check for success or failure. Maybe this was done intentionally for performance reasons but it makes it very difficult to work with.

Gili

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ndcosta
Offline
Joined: 2004-05-21

Hi,

there have been some bugs filed at sun related to JNI bugs in WToolkit - Check out bug
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5076963
(also listed in the release notes -
http://java.sun.com/j2se/1.5.0/ReleaseNotes.html)

Please check up against mustang - if it still shows up, go ahead and file a New bug report with the bug 5076963 as reference

cheers..

cowwoc
Offline
Joined: 2003-08-24

ndcosta,

That's ok. As I explained before, I know what is causing the JNI errors. I am experimenting with stripping away class files because we're having a discussion in another forum thread about the benefits/downsides of allowing rt.jar to be stripped down.

Hence, I'm not concerned with these JNI errors getting triggered because that's my own fault. I am more concerned with being able to debug what I did wrong which caused them to be thrown; that is, which class, method, field was being referenced and was missing which caused an exception to be thrown.

Gili

cowwoc
Offline
Joined: 2003-08-24

Guys, any ideas?

I'm getting totally useless output now:

FATAL ERROR in native method: JNI call made with exception pending
at sun.awt.windows.WToolkit.eventLoop(Native Method)
at sun.awt.windows.WToolkit.run(WToolkit.java:269)
at java.lang.Thread.run(Thread.java:595)

It could be *anything* that failed to run here! It is impossible to debug. I need to be able to know whenever an exception is thrown. Please help!

Gili

bino_george
Offline
Joined: 2003-06-16

Hi Gili,
It looks like you have some exception pending.
You will need to clear it before proceeding. If you want
to print out the message you can use the ExceptionDescribe
method.

Checkout http://java.sun.com/docs/books/tutorial/native1.1/implementing/error.html

for more information on debugging JNI exceptions.

Thanks,
Bino.

cowwoc
Offline
Joined: 2003-08-24

Bino,

You misunderstand, I *know* what the exception being thrown is and how to clear it, but I am trying to find out who threw it in the first place (location in the code). I am looking for a seamless logging mechanism which would allow me to debug who threw the last exception without modifying the source-code at all.

Right now I am experimenting with the JRE code and it is throwing exceptions (FieldNotFoundError to be precise) and not catching it. I am experimenting removing unused fields from classes. It really isn't an option for me to start injecting ExceptionDescribe() calls into the code because I don't understand it nor do I have the capability to rebuild it.

In a nutshell: I should be able to get a verbose error description without having to modify the source-code. It would be rather trivial to add more output into the -verbose:jni code, would it not?

Gili

kellyohair
Offline
Joined: 2004-09-03

There is a JVMTI event for an exception throw, so modifying one of the jvmti demos in the installed demo/jvmti area you could write an agent that would save the stack trace for the last N exceptions thrown. Or something like that. I'll file an RFE for mustang to create such a demo, seems like something that would track exceptions could be a handy general tool/demo. The heapTracker demo tracks allocations and saved stack traces, might be easy to modify that one.

cowwoc
Offline
Joined: 2003-08-24

Instead of all these complicated solutions, why not modify the code that outputs "FATAL ERROR in native method: JNI call made with exception pending" and have it invoke ExceptionDescribe() which will give us the stack-trace of the exception that was never caught? That should help us track down the problem and it seems like a far simpler solution, no?

Gili

kellyohair
Offline
Joined: 2004-09-03

It isn't clear to me that when this error is detected that the current stack trace is what you want. I assume you want the stack trace when the exception was thrown, not when this error condition was detected. I could be wrong. I'll try and check with one of the VM engineers on what can be done.

cowwoc
Offline
Joined: 2003-08-24

> It isn't clear to me that when this error is detected
> that the current stack trace is what you want. I
> assume you want the stack trace when the exception
> was thrown, not when this error condition was
> detected. I could be wrong. I'll try and check with
> one of the VM engineers on what can be done.

That is correct. I want the stack-trace when the exception was thrown.

Gili

timbell
Offline
Joined: 2003-06-10

Are you running with -Xcheck:jni on your command line? That is always the first thing I suggest for people working with JNI.

kellyohair
Offline
Joined: 2004-09-03

There is a -verbose:jni but I'm not sure how worthwhile it is.

The JVM TI interface allows you to do full JNI interception, and I've wanted to try that, but have not been able to find the time. It might make a good JVM TI demo, and also provide some help with JNI issues.
The native tools (debuggers or truss or strace) could then be used to watch/trace your JNI interceptions.

Let me know if you'd like more details.

cowwoc
Offline
Joined: 2003-08-24

I am running with -Xcheck:jni and -verbose. Neither is useful in tracking down these bugs. The JRE should really have better debugging capability for these errors. They are terribly difficult to track down.

Gili