使用make编译openjdk
- 从github上搜索openjdk,官网上下载非常慢而且经常中断
- 解压到openjdk-jdk-jdk10目录
./configure --with-debug-level=slowdebug --disable-warnings-as-errors --with-freetype=/usr/local/Cellar/freetype/2.9.1/
-
make LOG=trace all
:LOG=trace
灰常重要 - 如果把xcode升级到了10,编译时会找不到
#include
头文件,链接时会找不到libstdc++,因为xcode 10以后删除了libstdc++,只需要把xcode9中/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++
和/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/libstdc++.*
复制到xcode10相同的目录即可
openjdk9之后在osx上build一般还是很顺利的,可能会提示openssl,freetype之类的依赖缺失,使用brew install
即可解决,20分支之后openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/
目录就是编译好的openjdk,打开个命令行窗口,cd到上述目录,执行jdk/bin/java -version
是不是出现了熟悉的画面!!!
xcode debug openjdk
肯定不能只满足与此,下面演示在xcode里debug
- xcode新建command line工程,删除自动生成的hello world代码
- 在工程上右键选择
Add Files to ""
,然后选择openjdk-jdk-jdk10文件夹 - 左上角悬着target,然后选择
Edit Schame
,删除Build下的target,在Run下Executable选择openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/jdk/bin/java
,然后加断点就可以debug了。
划重点!!!:debug时可能会遇到各种signal SIGSEGV
或signal SIGBUS
导致debug中断,在stackoverflow看到一个神技:
在java的入口函数出加断点,位置为src/java.base/share/native/launcher/main.c
,
如果这就满足了,那就太没有追求了,这种方法虽然能debug,但是不能进行符号导航。
根据build.log构造xcode工程
下面开始本文的重点,在xcode中编译出一个完美的可debug可符号导航的openjdk。还记得之前make时添加的LOG=trace
参数吧,这个参数可以设置make时把详细的编译指令记录到log里。我用xcode10编译openjdk10之后,build/macosx-x86_64-normal-server-slowdebug/build.log
的大小是68.6M!!!
- 先看看log中libjvm.dylib是怎么链接的
grep 'clang.*/libjvm.dylib' build.log | less
/usr/bin/clang++
-m64
-mno-omit-leaf-frame-pointer
-mstack-alignment=16
-stdlib=libstdc++
-fPIC
-Wl,-rpath,@loader_path/.
-Wl,-rpath,@loader_path/..
-dynamiclib
-compatibility_version 1.0.0
-current_version 1.0.0
-Wl,-exported_symbols_list,/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/hotspot/variant-server/libjvm/mapfile
-Wl,-install_name,@rpath/libjvm.dylib
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk
-iframework /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks
-F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/JavaVM.framework/Frameworks
-o /Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/support/modules_libs/java.base/server/libjvm.dylib
@/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/hotspot/variant-server/libjvm/objs/_BUILD_LIBJVM_objectfilenames.txt
- 从上一步能找到libjvm.dylib链接的所有obj文件的列表:
build/macosx-x86_64-normal-server-slowdebug/hotspot/variant-server/libjvm/objs/_BUILD_LIBJVM_objectfilenames.txt
此文件是编译libjvm.dylib需要的所有obj文件,按图索骥把文件找全了,基本就是src/hotspot/cpu/x86
,src/hotspot/os/bsd
,src/hotspot/os/posix
,src/hotspot/os_cpu/bsd_x86
,src/hotspot/share
,build/macosx-x86_64-normal-server-slowdebug/hotspot/variant-server/gensrc
,build/macosx-x86_64-normal-server-slowdebug/support/headers
下的所有源文件和头文件,遇到一个文件的x86_32和x86_64两个版本同时存在时删除x86_32即可。新建一个dylib工程,然后加入上述文件。
- 添加完源文件之后,接下来配置target jvm
第一项:配置dylib生成目录
第二项:配置链接时的目录
第三项:配置头文件搜索路径
第四项:配置dylib文件搜索路径(此处没显示,编译其他dylib时需要)
第一项:配置c++编译参数
第二项:配置警告参数
第三项:配置预处理宏定义
这三项参数来自哪里?
grep 'clang.*jvm\.cpp' build.log | less
/usr/bin/clang++
-include-pch /Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/hotspot/variant-server/libjvm/objs/precompiled/precompiled.hpp.pch
-m64
-fPIC
-D_GNU_SOURCE
-flimit-debug-info
-D__STDC_FORMAT_MACROS
-D__STDC_LIMIT_MACROS
-D__STDC_CONSTANT_MACROS
-DSUPPORTS_CLOCK_MONOTONIC
-D_ALLBSD_SOURCE
-D_DARWIN_C_SOURCE
-D_XOPEN_SOURCE
-fno-rtti
-fno-exceptions
-fvisibility=hidden
-mno-omit-leaf-frame-pointer
-mstack-alignment=16
-pipe
-fno-strict-aliasing
-fno-omit-frame-pointer
-DVM_LITTLE_ENDIAN
-D_LP64=1
-Wno-deprecated
-Wpointer-arith
-Wsign-compare
-Wundef
-Wunused-function
-Wformat=2
-DMAC_OS_X_VERSION_MIN_REQUIRED=1070
-mmacosx-version-min=10.7.0
-DASSERT
-D_NMT_NOINLINE_
-DTARGET_ARCH_x86
-DINCLUDE_SUFFIX_OS=_bsd
-DINCLUDE_SUFFIX_CPU=_x86
-DINCLUDE_SUFFIX_COMPILER=_gcc
-DTARGET_COMPILER_gcc
-DAMD64 '-DHOTSPOT_LIB_ARCH="amd64"'
-DCOMPILER1
-DCOMPILER2
-DDTRACE_ENABLED
-DINCLUDE_AOT
-I/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/src/hotspot/share
-I/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/src/hotspot/os/bsd
-I/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/src/hotspot/os/posix
-I/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/src/hotspot/cpu/x86
-I/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/src/hotspot/os_cpu/bsd_x86
-I/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/hotspot/variant-server/gensrc
-I/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/src/hotspot/share/precompiled
-I/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/src/hotspot/share/include
-I/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/src/hotspot/os/posix/include
-I/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/src/java.base/share/native/include
-I/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/src/java.base/unix/native/include
-I/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/src/java.base/share/native/libjimage
-g
-Wno-tautological-compare
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk
-iframework /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks
-F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/JavaVM.framework/Frameworks '-DTHIS_FILE="jvm.cpp"'
-c
-MMD
-MF /Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/hotspot/variant-server/libjvm/objs/jvm.d
-o /Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/hotspot/variant-server/libjvm/objs/jvm.o /Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/src/hotspot/share/prims/jvm.cpp
- -m/-f 对应c++编译参数
- -D对应预处理宏定义,配置到xcode时去掉开头的-D,如果有包含双引号的字符串配置到xcode时双引号外面再加单引号,例如:
'-DHOTSPOT_LIB_ARCH="amd64"'
配置到xcode时应该是HOTSPOT_LIB_ARCH='"amd64"'
- -W对应警告参数
- -I对应头文件搜索路径
- -o后面是obj或dylib文件,再之后是对应的源文件
把商户参数配置到对应的地方基本够用了,可能还差几个,直接点编译,差什么直接在build.log中通过grep搜索即可。
编译libjava.dylib等其他的动态库时会有依赖,需要配置dylib搜索路径和要链接的dylib
我怎么知道要链接哪些库?eazy,查build.log
grep 'clang.*/libjava.dylib' build.log | less
/usr/bin/clang
-mmacosx-version-min=10.7.0
-dynamiclib
-compatibility_version 1.0.0
-current_version 1.0.0
-L/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/support/modules_libs/java.base
-L/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/support/modules_libs/java.base/server
-Wl,-rpath,@loader_path/.
-L/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/support/native/java.base/
-Wl,-install_name,@rpath/libjava.dylib
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk
-iframework /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks
-F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/JavaVM.framework/Frameworks
-o /Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/support/modules_libs/java.base/libjava.dylib @/Users/apple/Documents/code/opensource/openjdk-jdk-jdk10/build/macosx-x86_64-normal-server-slowdebug/support/native/java.base/libjava/_BUILD_LIBJAVA_objectfilenames.txt
-ljvm
-lverify
-lfdlibm
-framework CoreFoundation
-framework Foundation
-framework Security
-framework SystemConfiguration
-o下面的行就是要链接的dylib和系统库
重复此步骤编译出其他的dylib直到可以编译出可执行指令java,一共需要一下target
最终把这些都顺利搞定了之后,想来个java -version
试试,发现并不行,还差最后一步:
本地按照oracle jdk10
- 复制oracle jdk下的conf和modules文件夹
- 复制oracle jdk下的lib文件夹下的jvm.cfg,tzdb.dat以及security文件夹
- build时配置java生成目录为bin,libjvm.dylib生成目录为lib/server,libjli.dylib的生成目录为lib/jli
然后再试,是不是就行啦,好好钻研吧! 骚年!!!
开源项目十有八九都是通过./configure然后make编译的,貌似这是一种通用方法,可以很方便的把基于makefile的项目导入ide,方便调试分析!!!
后记:
曾经多次想debug openjdk的源码,加深自己对java的理解,到今天大概尝试过10来次,随着自己能力的提升每次前进一点点,到今天终于编译出了一个完美的可debug的openjdk。下一步深入钻研一下。