0%

centos7源码编译openjdk11,并使用Clion调试openjdk11源码

centos7源码编译openjdk11,并使用Clion调试openjdk11源码

源码下载

1
git clone https://github.com/openjdk/jdk.git

切换到openjdk11分支

1
git checkout jdk-11+24

执行验证

1
git status

image-20241216105615647

编译工具安装

1
sudo yum groupinstall "Development Tools"

准备openjdk10作为N-1版本启动引导

https://jdk.java.net/archive/

image-20241216104133390

解压

image-20241216105101198

执行安装脚本

1
bash configure --with-boot-jdk=/home/hou/jdk-10.0.1

提示报错1

image-20241216103154765

checking for X11/extensions/shape.h… configure: error: Could not find all X11 headers (shape.h Xrender.h XTest.h Intrinsic.h). You might be able to fix this by running ‘sudo yum install libXtst-devel libXt-devel libXrender-devel libXi-devel’.

执行安装

1
sudo yum install libXtst-devel libXt-devel libXrender-devel libXi-devel

再次执行安装脚本

1
bash configure --with-boot-jdk=/home/hou/jdk-10.0.1

提示报错2

image-20241216103340413

checking cups/ppd.h presence… configure: error: Could not find cups! You might be able to fix this by running ‘sudo yum install cups-devel’.

执行安装

1
sudo yum install cups-devel

再次执行安装脚本

1
bash configure --with-boot-jdk=/home/hou/jdk-10.0.1

提示报错3

image-20241216103434766

configure: error: Could not find fontconfig! You might be able to fix this by running ‘sudo yum install fontconfig-devel’.

执行安装

1
sudo yum install fontconfig-devel

再次执行安装脚本

1
bash configure --with-boot-jdk=/home/hou/jdk-10.0.1

提示报错4

image-20241216103527628

configure: error: Could not find alsa! You might be able to fix this by running ‘sudo yum install alsa-lib-devel’.

执行安装

1
sudo yum install alsa-lib-devel

再次执行安装脚本

1
bash configure --with-boot-jdk=/home/hou/jdk-10.0.1

提示构建成功

image-20241216103621422

执行make,生成镜像

1
make images

image-20241216105004336

创建成功

image-20241216105537054

验证安装

1
./build/*/images/jdk/bin/java -version

image-20241216105149079

执行成功

启用调试模式

1
bash configure --with-boot-jdk=/home/hou/jdk-10.0.1 --disable-warnings-as-errors --with-debug-level=slowdebug --with-jvm-variants=server
1
2
3
# disable-warnings-as-errors选项是禁止把warning 当成error
# --with-debug-level=slowdebug。用来设置编译的级别,可选值为release、fastdebug、slowde-bug,越往后进行的优化措施就越少,带的调试信息就越多。默认值为release。slowdebug 含有最丰富的调试信息,没有这些信息,很多执行可能被优化掉,我们单步执行时,可能看不到一些变量的值。所以最好指定slowdebug 为编译级别。
# with-jvm-variants 编译特定模式的HotSpot虚拟机,可选值:server、client、minimal、core、zero、custom
1
make images

编译成功

image-20241216141151988

执行测试

1
./build/*/images/jdk/bin/java -version

image-20241216141240447

下载Clion导入源码项目

设置自定义编译目标

image-20241216160339380

配置编译命令

image-20241216160411712

配置clean命令

image-20241216160438438

分别配置对应的选项

image-20241216160533177

编辑配置

image-20241216160557945

选中编译的java命令,并执行version

image-20241216160614096

启动运行

image-20241216160704850

启动调试

打断点在java.c上

image-20241216160738145

执行调试

image-20241216160802545

点击继续运行

image-20241216160827103

可以看到运行成功

image-20241216160908592

可以看到报错1

image-20241216161447221

由于HotSpot JVM内部使用了SEGV等信号来实现一些功能(如NullPointerExceptionsafepoints等),所以调试过程中,GDB可能会误报Signal: SIGSEGV (Segmentation fault). 解决办法是,在用户目录下创建.gdbinit,让GDB捕获SEGV等信号:

1
vim ~/.gdbinit

将以下内容追加到文件中并保存:

1
2
handle SIGSEGV pass noprint nostop 
handle SIGBUS pass noprint nostop

重新运行就没有相关报错了

image-20241216161715500