• Post Reply Bookmark Topic Watch Topic
  • New Topic

debugging JNI application  RSS feed

 
George Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,


I am wondering if I want to debug JNI application (upper layer Java and lower layer C), are there any tools to debug through Java code to C code and return back from C code to Java code.

I am using Eclipse. But I do not find such functions in Eclipse (maybe I miss some plug-ins?). Currently, I can only debug Java code.


thanks in advance,
George
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly an answer to your question, but when I used JNI what we tried to do was have no code in the C layer (we were calling an API) and have all the logic in the Java code.

I think this approach is quite well suited to using API/library, as your JNI layer just makes a call out to the library method you want to use, any errors in the C layer are done to the library or incorrectly passing of parameters. Any logic errors were in the Java layer.

G
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think JNI is a bit advanced for the beginners forum. Let's promote this to the intermediate forum.
 
George Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for moving, marc.


Originally posted by marc weber:
I think JNI is a bit advanced for the beginners forum. Let's promote this to the intermediate forum.


Looking forward to reply. :-)


regards,
George
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(bartender: I thought JNI was supposed to go in Other APIs. It says Other APIs including JNI)

Anyway...

I don't know of any single tool that can seamlessly debugs Java and native C/C++ code. That doesn't mean that it doesn't exist. In my organisation, we mostly use built-in or free command-line tools - not big on IDEs (unless emacs counts) or buying expensive tools - so there may be some premium product out there that I don't know about.

You certainly can use an ordinary C debugger to debug the C/C++ code that forms your JNI library.

First, make sure it's built with the necessary debug information included (compiler/linker switches).

Then run your Java launcher (e.g. "java[.exe]") in a C/C++ debugger. You can use gdb on Unix or Mac OS X. Or you can use Visual Studio (spit) or WinDBG (yea) on Windows.

You won't get many debugging facilities on the code of the JVM itself, as it has no debug symbols. But once your native library has loaded, you can set breakpoints on your JNI functions, see variable values, examine call stack, catch C++ exceptions etc.

This said, I agree with the previous responder who recommended keeping the C/C++ code as simple as possible and putting the complicated stuff in Java. The C/C++ should be just sufficient to interface with the native facilities you're using. That is assuming you're using JNI for accessing native facilities, rather than for performance; the latter is much less necessary these days, because the latest JVMs are so fast (faster than C, in some cases).

If you need to debug Java and C/C++ in the same session, you can run "jdb[.exe]" (rather than "java[.exe]") in the C/C++ debugger. Works fine ... honest.

Final point: if it's difficult to adjust your Java start-up to run via C/C++ debugger, you can use your ordinary start-up procedure, then attach the C/C++ debugger to the Java process.
[ August 13, 2007: Message edited by: Peter Chase ]
 
George Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter,


Originally posted by Peter Chase:
(bartender: I thought JNI was supposed to go in Other APIs. It says Other APIs including JNI)

Anyway...

I don't know of any single tool that can seamlessly debugs Java and native C/C++ code. That doesn't mean that it doesn't exist. In my organisation, we mostly use built-in or free command-line tools - not big on IDEs (unless emacs counts) or buying expensive tools - so there may be some premium product out there that I don't know about.

You certainly can use an ordinary C debugger to debug the C/C++ code that forms your JNI library.

First, make sure it's built with the necessary debug information included (compiler/linker switches).

Then run your Java launcher (e.g. "java[.exe]") in a C/C++ debugger. You can use gdb on Unix or Mac OS X. Or you can use Visual Studio (spit) or WinDBG (yea) on Windows.

You won't get many debugging facilities on the code of the JVM itself, as it has no debug symbols. But once your native library has loaded, you can set breakpoints on your JNI functions, see variable values, examine call stack, catch C++ exceptions etc.

This said, I agree with the previous responder who recommended keeping the C/C++ code as simple as possible and putting the complicated stuff in Java. The C/C++ should be just sufficient to interface with the native facilities you're using. That is assuming you're using JNI for accessing native facilities, rather than for performance; the latter is much less necessary these days, because the latest JVMs are so fast (faster than C, in some cases).

If you need to debug Java and C/C++ in the same session, you can run "jdb[.exe]" (rather than "java[.exe]") in the C/C++ debugger. Works fine ... honest.

Final point: if it's difficult to adjust your Java start-up to run via C/C++ debugger, you can use your ordinary start-up procedure, then attach the C/C++ debugger to the Java process.

[ August 13, 2007: Message edited by: Peter Chase ]


I have tried to set Visual Studio parameters,

in the native DLL Project Properties dialog, I assign Command to java, then set Command argument to,

-classpath "C:\Program Files\Java\jdk1.5.0_06" HelloWorld,

when when debugging from Visual Studio, Visual Studio will crash. Do you know anything wrong with the settings?

I have also tried to run manually from command line,

java -classpath "C:\Program Files\Java\jdk1.5.0_06" HelloWorld

and the result is correct.


regards,
George
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't use Visual Studio for developing my native libraries, just occasionally for debugging them.

When you say you set the Command to "java", did you include the full path to the Java executable? If not, I think you should, as there could be more than one Java on your machine.

As for Visual Studio crashing, contact your debugger vendor

How about trying attaching Visual Studio to your running Java process, rather than launching Java from Visual Studio? Does that work?
 
George Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter,


How to attaching Visual Studio to your running Java process?


regards,
George

Originally posted by Peter Chase:
I don't use Visual Studio for developing my native libraries, just occasionally for debugging them.

When you say you set the Command to "java", did you include the full path to the Java executable? If not, I think you should, as there could be more than one Java on your machine.

As for Visual Studio crashing, contact your debugger vendor

How about trying attaching Visual Studio to your running Java process, rather than launching Java from Visual Studio? Does that work?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Visual Studio 2005, use the Tools menu, Attach To Process. Look for the java.exe process.

It's in the Tools menu of VS 2003, as well, as Debug Processes.
 
George Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter,


Originally posted by Peter Chase:
In Visual Studio 2005, use the Tools menu, Attach To Process. Look for the java.exe process.

It's in the Tools menu of VS 2003, as well, as Debug Processes.


I have tried that in Visual Studio 2003, I used Debug --> Process and attach to javaw.exe process.

I have also set some break points in VS 2003, but they never hit when I run Java application from Eclipse 3.2.

Any ideas of what is the issue?


regards,
George
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eclipse is written in Java, isn't it? Are you sure you didn't attach to Eclipse itself?
 
George Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter,


Originally posted by Peter Chase:
Eclipse is written in Java, isn't it? Are you sure you didn't attach to Eclipse itself?


I think I should attach to javaw.exe, right?


regards,
George
 
Timothy Wall
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by George Lin:

I think I should attach to javaw.exe, right?



One instance of javaw.exe will be the eclipse process. Depending on your launch settings, your program might be run as javaw.exe OR java.exe. If you look in windows task manager, the process with the smaller memory footprint is likely yours (eclipse runs around 200Mb).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!