[英]How to Debug Java -JNI using GDB on linux ?
任何人都可以指導如何使用GDB調試器在Linux上調試JNI代碼(如果可能請建議其他選項)。
-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,但我沒有得到如何使用它來調試項目。 我是否還需要使用-g選項t debug .so文件編譯.cpp文件?
此博客帖子解釋了整個事情。
我發現以下方式非常有趣。 通過將下面的文件鏈接到您要調試的jni庫,當動態鏈接器加載庫時,由於gcc構造函數屬性,它會自動啟動當前jvm的gdbserver。
只需從命令行或從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和遠程C / C ++,它運行在127.0.0.1:11337上。
鏈接由tm.sauron是正確的但是當我們有許多參數傳遞給java命令時會不太方便,就像我的項目有幾行param傳遞一樣。 因此,在這種情況下,我們可以使用IDE啟動應用程序,並在我們想要在本機庫中進行調試時將其分解。 需要在調試模式下創建Ofcourse本機庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.