简体   繁体   中英

Using Eclipse to debug a JNI / C++ library and processes

I'm have trouble understanding how to debug both my C++ code and an associated forked Java process. I've got C++ code that forks off a .jar SWING file. This opens a Java Window and waits for user input...

I've created a C++ eclipse project that successfully compiles both the C++ and Java sources. Both compilations use the -g compiler flag. But when I run in debug mode I can only step through the initial C++ code. After the fork statement (that launches the JAVA SWING code) I cannot step through the Java code. The next step goes directly to the statement of the fork. I'm using JNI calls to call a C++ shared object lib that is created at compile time.

I've read other posts about folks starting their Java application that then calls C++ through JNI calls. But I'm doing the opposite. I start with my C++ code that then forks a java process.

Either way it seems I somehow need to attach my forked Java process to the current C++ eclipse debug session. How do I go about doing that? A simple example would be greatly appreciated.

You have to create the Java VM in your C++ with the command line options for Java debugging. To run Java with a listener for debugging you pass the following options (if port 8000 is not free, you can choose another address)

-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

Then in the Eclipse debugging dialog you create a "Remote Java Application". Use the address in your options in the "Port" field in "Connection Properties" (8000 is the default). Set some breakpoints in the Java code and press "Debug". If you need to debug code that happens at Java startup you can use suspend=y in the options, then the VM will wait for a connection from a debugger before starting up.

If you cannot change the options for the Java process you're out of luck.

Edit: the documentation http://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html tells me that -Xrun is now -agentlib . Didn't know that because I've been using the above options for years and they haven't stopped working yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM