任何人都可以指导如何使用GDBdebugging器在Linux上debuggingJNI代码(如果可能的话,请build议其他选项)。
-My JNI project when running on Linux is leading to a JVM crash. -The CPP code has been compiled into .so files. -I run the project like this : *java xyz.jar -commandline_args_to_project*.
我已经安装了Gdb,但没有得到我们如何使用它来debugging项目。 我也一定需要用-g选项编译.cpp文件tdebugging.so文件?
这篇博文解释了整个事情。
我发现以下方式真的很有趣。 通过将下面的文件链接到要调试的jni库,当库被动态链接器加载时,它会自动为当前jvm启动一个gdbserver,这要归功于gcc构造函数属性。
从命令行或eclipse中简单地使用远程gdb可以很容易地进行调试。 我只设置,如果我建立在调试模式,我暂时还没有实现检测,如果jvm开始调试,只允许在这一刻,但可能很容易。
我简单地从这篇文章中调整了这个概念: http : //www.codeproject.com/Articles/33249/Debugging-C-Code-from-Java-Application
#ifndef NDEBUG // If we are debugging #include <stdlib.h> #include <iostream> #include <sstream> namespace debugger { static int gdb_process_pid = 0; /** * \brief We create a gdb server on library load by dynamic linker, to be able to debug the library when java begins accessing it. * Breakpoint have naturally to be set. */ __attribute__((constructor)) static void exec_gdb() { // Create child process for running GDB debugger int pid = fork(); if (pid < 0) { abort(); } else if (pid) { // Application process gdb_process_pid = pid; // save debugger pid sleep(10); /* Give GDB time to attach */ // Continue the application execution controlled by GDB } else /* child */ { // GDBserver Process // Pass parent process id to the debugger std::stringstream pidStr; pidStr << getppid(); // Invoke GDB debugger execl("/usr/bin/gdbserver", "gdbserver", "127.0.0.1:11337", "--attach", pidStr.str().c_str(), (char *) 0); // Get here only in case of GDB invocation failure std::cerr << "\nFailed to exec GDB\n" << std::endl; } } } #endif
此外,它还允许在安装了gdbserver的嵌入式设备上进行调试,并在开发PC上调试gdb-multiarch。
在eclipse中进行调试时,它会在C / C ++调试器和Java调试器之间自动跳转。 您只需启动两个调试会话:java one和运行在127.0.0.1:11337上的远程C / C ++。
通过tm.sauron的链接是正确的但是当我们有很多参数传递给java命令时会不太方便,就像我的项目中有几行参数要传递一样。 那么在这种情况下,我们可以使用IDE来启动应用程序,并在我们想要在本机库中进行调试时将其打破。 Ofcourse本地库需要在调试模式下创建。