0%

centos7源码编译openjdk11,并使用GDB或者clion+GDB或者vscode+GDB调试openjdk11源码

centos7源码编译openjdk11,并使用GDB或者clion+GDB或者vscode+GDB调试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

GDB调试

​ 没有任何工具的情况下使用这种方式最方便,也是最直观的。

进入到执行目录下

1
cd /home/hou/jdk/build/linux-x86_64-normal-server-slowdebug/jdk/bin

image-20250122153304353

执行gdb命令

1
gdb ./java

image-20250122153322009

设置断点

1
b JLI_Launch

执行命令

1
run -version 

image-20250122153740161

单步调试

1
n

image-20250122153839764

退出调试

1
quit

image-20250122153942029

CLION+GDB调试

使用强大的ide将会使调试代码更加方便,clion的缺点是不是开源免费的,下面演示一下

下载并安装clion

https://www.jetbrains.com/zh-cn/clion/download/#section=windows

下载linux版本

image-20250122154611874

解压进入到目录中

1
cd /home/hou/clion-2024.3.1/bin

执行运行脚本

1
./clion.sh

image-20250122155039012

打开项目

image-20250122165235575

image-20250122165253249

配置debug

image-20250122165354707

配置地址

image-20250122165456812

打印断点

image-20250122165527259

安装gdbserver

1
sudo yum install gdb-gdbserver

进入目录下

1
cd /home/hou/jdk/build/linux-x86_64-normal-server-slowdebug/jdk/bin

执行gdb服务命令

1
gdbserver :1234 ./java -version

可以看到已经启动

image-20250122164134096

启动调试

可以看到已经在断点处了

image-20250122165700040

vscode+GDB调试

GDB直接调试不那么直观,看着很难受,使用开源vscode工具,再也不用担心clion的license到期了,非常方便直观可以进行断点调试。

vscode下载

https://code.visualstudio.com/updates/v1_85

安装好后启动

输入code就可以启动

1
code

打开源码目录

image-20250122163635032

image-20250122163703586

安装插件

image-20250122163741370

image-20250122163753256

添加配置

image-20250122163824937

选择这个

image-20250122163902048

配置launch.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "by-gdb",
"request": "launch",
"name": "Launch(remote)",
"program": "/home/hou/jdk/build/linux-x86_64-normal-server-slowdebug/jdk/bin/java",
"cwd": "${workspaceRoot}",
"remote": {
"enabled": true,
"address": ":1234",
"mode": "remote",
"execfile": "/home/hou/jdk/build/linux-x86_64-normal-server-slowdebug/jdk/bin/java"
}
}
]
}

安装gdbserver

1
sudo yum install gdb-gdbserver

进入目录下

1
cd /home/hou/jdk/build/linux-x86_64-normal-server-slowdebug/jdk/bin

执行gdb服务命令

1
gdbserver :1234 ./java -version

可以看到已经启动

image-20250122164134096

设置断点

image-20250122164239623

启动调试

image-20250122164302191

可以看到已经捕获到断点,尽情调试吧

image-20250122164340572

image-20250122164350252