如何调试Linux中多java线程调用的JNI

时间:2016-10-31 08:13:21

标签: java java-native-interface gdb

我创建了一个包含多线程的Java类,每个线程调用一个不同的JNI接口。我尝试使用gdb -p(pid是java进程id)来调试JNI。但是,我找不到具体的JNI接口对应哪个线程。在这种情况下,如何在gdb中调试JNI代码?

1 个答案:

答案 0 :(得分:0)

您只需将gdb连接到JVM进程即可。

启动示例代码

jps

使用jps列出Java进程

gdb /proc/PID/exe PID

将gdb连接到进程

jps
9340 Jps
9278 HelloWorld
...
gdb /proc/9278/exe 9278
...
(gdb) break mode_1
Breakpoint 1 at 0x2b8126cd97b6: file c/recipeNo024_HelloWorld.c, line 6.
(gdb) break mode_2
Breakpoint 2 at 0x2b8126cd97c8: file c/recipeNo024_HelloWorld.c, line 10.
(gdb) break mode_3
Breakpoint 3 at 0x2b8126cd97da: file c/recipeNo024_HelloWorld.c, line 14.
(gdb) cont
...
...
[Switching to Thread 0x2b81270dc700 (LWP 9337)]

Breakpoint 2, mode_2 () at c/recipeNo024_HelloWorld.c:10
10              printf("Mode 2\n");
(gdb) cont
Continuing.
[Switching to Thread 0x2b8126fdb700 (LWP 9335)]

Breakpoint 1, mode_1 () at c/recipeNo024_HelloWorld.c:6
6               printf("Mode 1\n");
(gdb) cont
Continuing.
[Switching to Thread 0x2b81271dd700 (LWP 9338)]

Breakpoint 3, mode_3 () at c/recipeNo024_HelloWorld.c:14
14              printf("Mode 3\n");
(gdb) cont

调试代码

df.plot(df.sttm, legend=False)

您可以在此处找到此示例的示例代码:

https://github.com/mkowsiak/jnicookbook/tree/master/recipeNo024

您还可以在此处找到有关使用CLion进行调试的演示视频:

https://youtu.be/8Cjeq4l5COU