编译与调试 OpenJDK

编译 OpenJDK

关于编译 OpenJDK 官方文档有很好的介绍,http://openjdk.java.net/groups/build/,汇总了 JDK 6, JDK 7, JDK 8, JDK 9 的 build OpenJDK 的 README 文件。编译 JDK 8 需要 Xcode 4,现在 Xcode 版本已远高于 4 了,前人尝试编译发现有很多坑 [ doc, blog ],所以本文直接开始尝试编译 OpenJDK 9。OpenJDK 9 源码根目录下的 README 文件有如下提示信息(github, hg):

1
2
3
4
5
For information about building OpenJDK, including how to fully retrieve all
source code, please see either of these:
* common/doc/building.html (html version)
* common/doc/building.md (markdown version)

common/doc/building.html 和 common/doc/building.md 就是本文的主要参考来源。

长话短说 TL;DR

building.md 首先给如下的 TL;DR

1
2
3
4
5
$ hg clone http://hg.openjdk.java.net/jdk9/jdk9 jdk9
$ cd jdk9
$ bash get_source.sh # 下载全部源代码
$ bash configure # configure 编译环境,若编译报错,需要添加 `--disable-warnings-as-errors`
$ make images # 编译 OpenJDK

现在就按照,这个几个命令尝试。

注意,如果运行 bash get_source.sh 时出现类似以下错误,是网络问题造成,多运行几次 bash get_source.sh,直到不出现错误为止。

1
2
WARNING: langtools exited abnormally (255)
WARNING: nashorn exited abnormally (255)

若不想使用 hg cloneget_source.sh 下载全部源代码,可以从 OpenJDK 的 github 镜像(非官方)下载:

1
$ git clone -b jdk9/jdk9 https://github.com/dmlloyd/openjdk.git

下载好全部源代码后,接下来需要运行 bash configure,控制台最后输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
··· 省略
A new configuration has been successfully created in
/Users/yulewei/jdk9/build/macosx-x86_64-normal-server-release
using default settings.
Configuration summary:
* Debug level: release
* HS debug level: product
* JDK variant: normal
* JVM variants: server
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64
* Version string: 9-internal+0-adhoc.yulewei.jdk9-hg (9-internal)
Tools summary:
* Boot JDK: java version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-b01) Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode) (at /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home)
* Toolchain: clang (clang/LLVM)
* C Compiler: Version 9.0.0 (at /usr/bin/clang)
* C++ Compiler: Version 9.0.0 (at /usr/bin/clang++)
Build performance summary:
* Cores to use: 8
* Memory limit: 16384 MB

可以看到 Debug level: release,除了默认的 release,还有 fastdebug, slowdebugoptimized 这三个调试级别 [ doc ]。现在试试 slowdebug 这个调试级别:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ bash ./configure --with-debug-level=slowdebug
... 省略
A new configuration has been successfully created in
/Users/yulewei/CODING/openjdk/openjdk-git/build/macosx-x86_64-normal-server-slowdebug
using configure arguments '--with-debug-level=slowdebug'.
Configuration summary:
* Debug level: slowdebug
* HS debug level: debug
* JDK variant: normal
* JVM variants: server
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64
* Version string: 9-internal+0-adhoc.yulewei.openjdk-git (9-internal)
... 省略

现在开始编译这个 OpenJDK,运行 make images。但是会报如下的编译错误:

1
2
3
/Users/yulewei/jdk9/hotspot/src/share/vm/memory/binaryTreeDictionary.hpp:167:12: error: instantiation of variable 'TreeChunk<FreeChunk, AdaptiveFreeList<FreeChunk> >::_min_tree_chunk_size' required here, but no definition is available [-Werror,-Wundefined-var-template]
return _min_tree_chunk_size;
^

对个这个编译错误,configure 命令后需要添加的 --disable-warnings-as-errors [ doc, mail.openjdk ],即将原先的命令修改为:

1
2
$ bash configure --disable-warnings-as-errors --with-debug-level=slowdebug
$ make images

但依然报错,如下:

1
2
3
4
5
6
7
8
9
10
11
/Users/yulewei/jdk9/hotspot/src/share/vm/memory/virtualspace.cpp:585:14: error: ordered comparison between pointer and zero ('char *' and 'int')
if (base() > 0) {
~~~~~~ ^ ~
...
/Users/yulewei/jdk9/hotspot/src/share/vm/opto/lcm.cpp:42:35: error: ordered comparison between pointer and zero ('address' (aka 'unsigned char *') and 'int')
if (Universe::narrow_oop_base() > 0) { // Implies UseCompressedOops.
~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~
...
/Users/yulewei/jdk9/hotspot/src/share/vm/opto/loopPredicate.cpp:915:73: error: ordered comparison between pointer and zero ('const TypeInt *' and 'int')
assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int() >= 0, "must be");
~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~

使用 Google 搜索这个编译错误,发现 OpenJDK 官方早就有人提了相关 bug,JDK-8174050JDK-8187787。至于如何修改,可以参考,hg 316854ef2fa2 或这 git 镜像 a05122a

这个错误是 XCode 版本不一致造成的,博主机器运行的版本是 9.0,而文档上说 Mac 下 JDK 9 的代码是用 XCode 8.3.2 和 --disable-warnings-as-errors 编译成功的 [ doc ]。

修改这 3 个 cpp 文件代码后,重新运行 configuremake,不出意外的话,就可以编译成功。等待十几分钟后,最后一行输出:

1
2
... 省略
Finished building target 'images' in configuration 'macosx-x86_64-normal-server-release'

全部编译结果都在 build/macosx-x86_64-normal-server-slowdebug 目录下。来验证下:

1
2
3
4
$ build/macosx-x86_64-normal-server-slowdebug/jdk/bin/java -version
openjdk version "9-internal"
OpenJDK Runtime Environment (build 9-internal+0-adhoc.yulewei.jdk9-hg)
OpenJDK 64-Bit Server VM (build 9-internal+0-adhoc.yulewei.jdk9-hg, mixed mode)

命令小结

编译 debug 版 OpenJDK 需要的命令:

1
2
3
4
5
$ hg clone http://hg.openjdk.java.net/jdk9/jdk9 jdk9
$ cd jdk9
$ bash get_source.sh # 下载全部源代码
$ bash configure --disable-warnings-as-errors --with-debug-level=slowdebug
$ make images # 编译 OpenJDK

阅读和调试 HotSpot 代码

我们使用 slowdebug 编译了 jdk 源代码,build 目录下会生成 java.dSYMjavac.dSYMlibjava.dylib.dSYMlibjvm.dylib.dSYM 等调试信息文件。有这些调试信息文件,就可以用 gdb 或者 lldb 调试 HotSpot 了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
lldb java
(lldb) b main
Breakpoint 1: 19 locations.
(lldb) run
Process 6276 launched: '/Users/yulewei/jdk9/build/macosx-x86_64-normal-server-slowdebug/jdk/bin/java' (x86_64)
Process 6276 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000100001813 java`main(argc=1, argv=0x00007fff5fbfef98) at main.c:98
95 {
96 int margc;
97 char** margv;
-> 98 const jboolean const_javaw = JNI_FALSE;
99 #endif /* JAVAW */
100
101 JLI_InitArgProcessing(!HAS_JAVA_ARGS, const_disable_argfile);
Target 0: (java) stopped.
(lldb) source info
Lines found in module `java
[0x0000000100001813-0x0000000100001817): /Users/yulewei/CODING/jdk9/jdk/src/java.base/share/native/launcher/main.c:98:20

使用 CLion

使用命令行工具太原始了,CLion 下阅读和调试更加方便。但是 CLion 目前只支持 cmake,不支持 make 项目。Google 下,发现 JDK 10 下有个官方分支 JDK-8177329-cmake-branch,能生成 CMakeLists.txt [ mail, README-cmake.md ],但博主尝试生成 CMakeLists.txt,失败了。只好退而求其次,使用简单的 CMakeLists.txt 文件,好让 CLion 能语法索引整个 HotSpot 项目,方便在 CLion 下阅读代码。CMakeLists.txt 文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
cmake_minimum_required(VERSION 3.7)
project(hotspot)
include_directories(
src/share/vm
src/os/linux/vm
src/cpu/x86/vm
src/os_cpu/linux_x86/vm
src/share/vm/precompiled)
file(GLOB_RECURSE SOURCE_FILES "*.cpp" "*.hpp" "*.c" "*.h")
add_executable(hotspot ${SOURCE_FILES})

参考资料