前言
這是一段相當(dāng)痛苦的過程。逐次記錄如下。
因?yàn)橛袝r(shí)候排查問題如果更深層次, 不可避免的需要從JDK源碼入手。此時(shí)如果能從源碼動(dòng)點(diǎn)手腳那是極好的。但是在mac環(huán)境build遠(yuǎn)遠(yuǎn)要比linux環(huán)境預(yù)想的要麻煩。
HG(Mercurial)
hg clone http://hg.openjdk.java.net/jdk9/jdk9
sh get_source.sh
使用HG從openjdk去下載源碼。這是萬事的第一步。但是這里就遇到了問題。下載速度實(shí)在是太雞兒慢了。家庭網(wǎng)絡(luò)速度下載大概在5kb/s。公司內(nèi)外下載速度能到100kb/s, 但是經(jīng)常會(huì)出現(xiàn)莫名其妙的錯(cuò)誤。
你可能會(huì)看到這樣的報(bào)錯(cuò):
jaxp: transaction abort!
jaxp: rollback completed
jaxp: abort: stream ended unexpectedly (got 33981 bytes, expected 44934)
或者這樣
jaxws: transaction abort!
jaxws: rollback completed
jaxws: abort: HTTP request error (incomplete response)
jaxws: (this may be an intermittent network failure; if the error persists, consider contacting the network or server operator)
所以此時(shí)不建議使用mercurial去下載(其實(shí)本身速度并不慢, 我嘗試用洛杉磯的vps去下載速度可到1M/s)。網(wǎng)上早有人有類似的需求, 將不同版本的源碼放在了github??蓞⒖既缦聝蓚€(gè)地址。
https://github.com/unofficial-openjdk/openjdk/
https://github.com/dmlloyd/openjdk
當(dāng)然git獲取也有可能失敗。那只能多嘗試幾次碰碰運(yùn)氣了。
$ git clone https://github.com/dmlloyd/openjdk.git
Cloning into 'openjdk'...
remote: Counting objects: 1205936, done.
remote: Compressing objects: 100% (4192/4192), done.
error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno
54
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
$ git clone https://github.com/dmlloyd/openjdk.git
Cloning into 'openjdk'...
remote: Counting objects: 1205936, done.
remote: Compressing objects: 100% (4192/4192), done.
error: RPC failed; curl 18 transfer closed with outstanding read data remaining
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
$ git clone https://github.com/dmlloyd/openjdk.git
Cloning into 'openjdk'...
remote: Counting objects: 1205936, done.
remote: Compressing objects: 100% (4192/4192), done.
error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
freetype
brew install freetype
參見維基百科: FreeType是一個(gè)用C語言實(shí)現(xiàn)的一個(gè)字體光柵化庫。它可以用來將字符柵格化并映射成位圖以及提供其他字體相關(guān)業(yè)務(wù)的支持。
X11
[https://www.xquartz.org/](https://www.xquartz.org/)
和UI界面有關(guān), build需要引入, 不做贅述。
configure階段
這里開始configure。
sh configure --with-debug-level=slowdebug --disable-warnings-as-errors --with-freetype-include=/usr/local/Cellar/freetype/2.9.1/include/freetype2 --with-freetype-lib=/usr/local/Cellar/freetype/2.9.1/lib --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home --with-target-bits=64
這里稍微解釋下:
--disable-warnings-as-errors: 不認(rèn)為告警是錯(cuò)誤
--with-freetype***:指定字體庫相關(guān)地址
--with-boot-jdk: 指定bootjdk。雞生蛋蛋生雞。
一切順利的話這里會(huì)生成:

OK??吹竭@里configure階段完成。下一步準(zhǔn)備make。
語法
JDK9的build需要稍微需改下源文件的三處地方, 這里不多說了, 參考前人趟的坑即可。 點(diǎn)我點(diǎn)我點(diǎn)我
Make
先描述下本機(jī)的一些配置。
BOOT-JDK: 1.8.0_161
XCode: 9.4
OS: Mojave(10.14)
第一個(gè)坑。不要亂升級XCode。下載地址
對于非Apple Developer來說, XCode有個(gè)很坑爹的事情, 就是他升級到高版本后, 無法回退到低版本。如果要重新安裝低版本, 需要Apple Developer賬號去下載。而且XCode的安裝包要5G, 解壓后分分鐘10個(gè)G。
第二個(gè)坑。XCode升級到10之后, 刪除了底層目錄下的libstdc++文件。導(dǎo)致在JDK的Make時(shí)會(huì)報(bào)錯(cuò), 無法識(shí)別<new>類似這樣的C++語法。而在XCode9時(shí)對應(yīng)文件還是存在的。官方給出的意思是libstdc++已經(jīng)被標(biāo)記為過期5年了, 現(xiàn)在統(tǒng)一使用自己libc++。這個(gè)問題最簡單的問題就是XCode版本回退到9之前即可。
第三個(gè)坑。OpenJDK的build的官方README說建議XCode版本為8.3.2。我按照要求回退到該版本, 卻發(fā)現(xiàn)該版本和 Mojave(10.14) 不兼容。XCode打開一閃而過。同時(shí)在安裝目錄下發(fā)現(xiàn)了不兼容導(dǎo)致的crash文件。無奈重新把版本拉到XCode9。
OK, 搞定了3個(gè)坑貨?,F(xiàn)在開始Make:
make all
正常來講這里經(jīng)過15分鐘左右就好了, 但是我在這里遇到了迄今為止最頭痛的bug, JVM...crash了???
問題截圖如下:

更細(xì)節(jié)的錯(cuò)誤描述如下:
siginfo: si_signo: 4 (SIGILL), si_code: 1 (ILL_ILLOPC), si_addr: 0x000000010c14b458
在這里困了很久。搜到了有人給官方 提交的bug, 但是官方和客氣啊說我們不管這種slowdebug的問題的, 有疑問你去openjdk的mailinglist去問問? 沒搜到之后, 我去stackoverflow發(fā)了個(gè)貼。 無人響應(yīng)。這特么就很尷尬了啊。
一番猜測后我們有了以下結(jié)論和嘗試:
- 代碼最后停留在native棧的 V [libjvm.dylib+0xd4b458] PerfData::~PerfData()+0x8。和PerfData有關(guān), 而PerfData是存儲(chǔ)jvm的一些采樣監(jiān)控?cái)?shù)據(jù)的, 比如jstat。
- 最后停留的函數(shù)是析構(gòu)函數(shù), 屬于資源釋放環(huán)節(jié)。
- 打開PerfDataManager看到這部分的源碼長這樣:

考慮到本身PerfDataManager就是個(gè)采樣環(huán)節(jié), 所以這里直接嘗試把該處代碼注釋掉, 就像這樣。

再次重新make, 老淚縱橫, 終于通過了。
----- Build times -------
Start 2019-03-02 00:53:13
End 2019-03-02 01:00:44
00:07:31 TOTAL
-------------------------
Finished building target 'all' in configuration 'macosx-x86_64-normal-server-
slowdebug'
新build出來的jdk在images/jdk目錄下, 嘗試配置到ide下試試看:

OK。希望此文對于在macos下的各位有幫助。