Robin's Blog

记录 积累 学习 成长

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

How to stop at a C/C++ code block while debugging a Java JNI?

I use the procedure described below to break the code execution into a C/C++ code while debugging a JNI code block. I use Microsoft Visual Studio 2008 and Netbeans IDE for my project. I am sure you can adapt it for other IDEs as well.

 

Configure Visual Studio Project

To be able to break inside a native code block from within Java IDE, you must first configure Visual Studio to attach to the Java executable that will run your Java code. Here is how:

  • Open the Visual Studio solution/project containing the native library code you want to debug.
  • Rick click on the project node and select the option "Properties".
  • On the "Property Pages" dialog, expand the node labeled "Configuration Properties" and select the sub-node "Debugging"
  • On the right side, please select the option "Local Windows Debugger" from the drop down labeled "Debugger to Launch"
  • On the grid below, select the option "Browse..." on the drop down labeled "Command".  Navigate to the Java executable that will run your java code. This is typically the java executable in the bin folder inside the java SDK folder. For ex: if your Java project is configured to run using"C:\j2sdk1.4.2_19", then the command path will be "C:\j2sdk1.4.2_19\bin\java.exe".
  • 同时还要勾选 Attach 为YES

  将DLL文件拷贝到系统Path变量指定的目标或JDK/bin 中时需要连同 bin/debug 目录中其它文件一起。

Stepping into native code

Now that you have configured your Visual Studio project, you can break into the native code, while running the Java code. Here is how:

  • Set breakpoints inside the native code using Visual Studio as you would normally do. These break points may appear as empty circles instead of the normal red bullet. This is normal.
  • Now setup a break point in the Java code at a point where the code is about to invoke the JNI method of interest. The  best place is before theSystem.loadLibrary() call that will load the native library you are debugging. But you can also set the breakpoint at a different place later in the life cycle. Setting the breaking point before the load lirbary call will allow you to make code changes to the native code and compile it while the Java code execution waiting.
  • The run the Java code in debug mode. 
  • Once the Java IDE to breaks at the Java break point you have set, switch to the Visual Studio and launch the native library project in debug mode. 
  • After launching the Visual Studio project in debug mode, switch to the Java IDE and let the execution continue.
  • When the code execution reaches the native breakpoints you have set, control will be transferred to the Visual Studio IDE and the code execution with stop. You can now step through the native code as you would normally do in Visual Studio.

 编程注意的问题:

double* a = env->GetDoubleArrayElements(startpoint, NULL); 

env->ReleaseDoubleArrayElements(startpoint, a, 0);//write ref Obj value to jdoublearry 

必须配合使用,不能同时运行多次ReleaseDoubleArrayElements

posted on 2011-08-02 11:40  Robin99  阅读(218)  评论(0编辑  收藏  举报