HotSpot源码阅读和解析
从零开始阅读HotSpot源码
一、获取并构建HotSpot 源码
HotSpot 源码下载地址:
https://github.com/openjdk/jdk/tree/jdk8-b120
一、准备环境
1.1、服务器资源
操作系统:Centos 8.5 x64
操作软件:CLion(JetBrains的产品)
# cat /proc/version
Linux version 3.10.0-1160.59.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Wed Feb 23 16:47:03 UTC 2022
# uname -a
Linux localhost.localdomain 3.10.0-1160.59.1.el7.x86_64 #1 SMP Wed Feb 23 16:47:03 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
二、下载源码(OpenJDK8)
1.安装hg 服务
yum install mercurial -y
2.使用hg 下载openjkd到 opt 目录
cd /opt
3.更新代码,操作和Git类似
cd /opt/jdk8u
hg pull
4.获取完整源码(下载时间较长)
sh get_source.sh
三、编译源码
-
构建JDK8需要使用JDK7 update 7或者更高的版本作为引导(Bootstrap)JDK,但不应该使用JDK8作为引导JDK;
-
JDK7下载: https://www.oracle.com/java/technologies/javase/javase7-archive-downloads.html
-
这里下载:jdk-7u80-linux-x64.tar.gz
1.将jdk-7u80-linux-x64.tar.gz文件上传到linux
使用Xftp 7 或者 rz 命令上传文件工具即可:
上传到linux下的:/opt/tools/目录下
2.解压jdk7到/opt 目录下
cd /opt/tools
tar -zxf jdk-7u80-linux-x64.tar.gz -C /opt
3.配置
cd /opt/jdk8u
chmod -x configure
4.安装编译所需要的工具包
yum install -y unzip zip libXtst-devel libXt-devel libXrender-devel cups-devel freetype-devel alsa-lib-devel fontconfig-devel
yum group info 'Development Tools'
yum groupinstall -y “Development Tools”
5.配置编译环境
./configure --with-target-bits=64 --with-boot-jdk=/opt/jdk1.7.0_80/ --with-debug-level=slowdebug --with-native-debug-symbols=internal
配置说明:
–with-target-bits=64 :指定⽣成64位jdk;
–with-boot-jdk=/opt/jdk1.7.0_80/:jdk7的路径;
–with-debug-level=slowdebug:编译时debug的级别,有release, fastdebug, slowdebug 三种级别;
6.编译(编译时间较长、耐心等待)
make all
7.测试
./build/linux-x86_64-normal-server-slowdebug/jdk/bin/java -version
openjdk version "1.8.0-internal-debug"
OpenJDK Runtime Environment (build 1.8.0-internal-debug-root_2022_06_14_21_00-b00)
OpenJDK 64-Bit Server VM (build 25.71-b00-debug, mixed mode)
8.复制拷贝一份
zip jdk8u.zip jdk8u -r
四、Windows 远程调试
1.linux环境准备
yum install cmake -y
yum install gcc-c++ -y
yum install gdb -y
yum install gdb-gdbserver -y
2.Windows环境准备
1.首先用Clion新建一个空项目

2.设置远程调试



3.同步代码(windows上的代码需要和CentOS上的代码一样)连接信息

4.对应目录,本地目录为新建的OpenJDK8项目目录

5.同步时要排除本地目录

6.下载远程代码(下载之后linux上的代码,可能会丢失某些文件,所以先把linxu的源码备份一次)



7.设置远程调试,地址为CentOs地址,端口随意


8.Linux环境准备
将jdk8u还原
unzip jdk8u.zip
linux启动GDB,端口与之前的对应(1234)
gdbserver :1234 /opt/jdk8u/build/linux-x86_64-normal-server-slowdebug/jdk/bin/java -version

linux启动1234端口(防火墙)
vi /etc/sysconfig/iptables
略
-A INPUT -m state --state NEW -m tcp -p tcp --dport 1234 -j ACCEPT
重启防⽕墙
service iptables restart
4.打上断点,启动的Debug(比较慢,载入libjvm.debuginfo的时候)


结束
网友评论