调试时出现 Signal received: SIGSEGV (Segmentation fault) 比较烦。在 jni.cpp 的 JNI_CreateJavaVM 设置断点调试。不同 jdk 版本路径不同。(编译调试 openjdk 的惨痛经历 - 简书)
这是因为:
测试本来就是要触发SIGSEGV的。而GDB在调试的时候会在程序注册的signal handler之前先获取到signal,要continue才会跑到应用注册的signal handler去。
解决方法
在openjdk的根目录上创建一个.gdbinit,这个文件的作用是为gdb在运行前提供配置信息
在Netbeans里面设置,右键项目->属性->调试,在“Gdb初始化文件”里面设置刚创建的文件
handle SIGSEGV pass noprint nostop
handle SIGUSR1 pass noprint nostop
handle SIGUSR2 pass noprint nostop
set logging on
set breakpoint pending on
为了方便其他国家的程序员,我翻译英文如下:
It is annoying to encounter "Signal received: SIGSEGV (Segmentation fault)" when you make a breakpoint at JNI_CreateJavaVM in jni.cpp.
Reason:
Because the purpose of debuging is to trigger SIGSEGV, and gbd must catch the signal before the program registers signal handler.
Solution:
Create a .gdbinit in the root of openjdk, this is for providing configuring infomation for gdb running.
In Netbeans, right click the project that you made, select "properties", select "debug", fill in the .gdbinit file path and the name in the "Gdb initial file"
handle SIGSEGV pass noprint nostop
handle SIGUSR1 pass noprint nostop
handle SIGUSR2 pass noprint nostop
set logging on