Recently in the synchronized lock optimization aspect of the content, some places look not very convenient, simply compile a source code to see.

Compile on Windows

Because of their common computer operating system is Win10, so the beginning is to want to compile on Win10, but one online article is too little, two to compile on Windows is really too much trouble (Windows can refer to the in-depth understanding of the JVM virtual machine this book), so give up.

MAC environment

macos.png

To prepare

Access to the source code

The OpenJDK source code is managed using Mercurial. If you download it through the release library, you need to install Mercurial, which we use homebrew package Manager to install

brew install mercurial
Copy the code

If homebrew is not installed on your computer, use the following command to install it

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Copy the code

Clone the source code using the following command after the installation is complete

cd ~/jvm
hg clone https://hg.openjdk.java.net/jdk/jdk12
Copy the code

The openJDK source code is not downloaded after the command is run

Because the way I did not use the hg, about this part can be reference flash www.jianshu.com/p/ee7e91766…

Of course, I strongly advise against using Mercurial, which takes a long time to download and requires an Internet connection. I recommend you download it directly from the pageOpenJdk12 source code package)

openjdk12-source-code.png

Let’s say I downloaded a file in GZ format (it doesn’t matter what format I choose).

If you’re using Chrome, you can enable multi-threaded downloads and get a noticeable speed boost.

Bootstrap JDK

Because the various components of OpenJDK are written in either C++ or Java, compiling the Java code requires a working JDK, officially called “Bootstrap. “JDK”, generally only the first version of the compiled JDK is required. Here is OpenJDK11, which can be downloaded from jdk.java.net/archive/. Be sure to download OpenJDK11 for Mac.

The Bootstrap JDK version I used is as follows

openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
Copy the code

Install dependencies

A tool for generating shell scripts that can compile packages on different systems
brew install autoconf

# Font engine
brew install freetype
Copy the code

XCode and Command Line Tools for XCode

These SDKS provide the compiler required by the OpenJDK and external commands used in makefiles. It’s usually installed on your computer.

Many compilation errors on the Internet are due to XCode version problems, resulting in errors, I also encountered this pit.

configure: error: No xcodebuild tool and no system framework headers found, use --with-sysroot or --with-sdk-name to provide a path to a valid SDK

/Users/... /... /xxx.sh: line 82: 5: Bad file descriptor
configure exiting with result code 1
 Copy the code

I finally through the website to download developer.apple.com/download/mo… XCode 9.4.1 was installed.

To download Xcode, you need to log in. If you don’t have an account, you can apply for one

I chose to manually download Xcode and install it. You can first download Command Line Tools (macOS 10.13) for Xcode 9.4.1 and see if it can solve your problem. If you can’t download Xcode(this is very large, about 4 gigabytes), you can download The Command Line Tools for Xcode 9.4.1 and see if it can solve your problem.

download-xcode.png

Compile the JDK

/Users/naver/ JVM/jdk12-06222165C35f directory where the following commands are executed.

bash configure  --with-boot-jdk='/ usr/local/JDK - 11.0.2. JDK/Contents/Home' --with-debug-level=slowdebug --with-target-bits=64 --disable-warnings-as-errors --enable-dtrace --with-jvm-variants=server
Copy the code

–with-boot-jdk: specifies the path to the Bootstrap JDK –with-debug-level: Compile level. The options are Release, fastDebug, slowDebug, and Optimized. The default value is Release –with-target-bits: specifies whether to compile 32-bit or 64-bit VMS. –disable-warnings-as-errors: avoids compilation interrupts due to warnings. –enable-dtrace: specifies whether to compile 32-bit or 64-bit VMS. Variants –with-conf-name — will run a performance tool which will later compile VMS in a specific mode. Specifies the name of the compile configuration. If not specified, the default configuration name macosx-x86_64-server-slowDebug will be generated, which I used here

The –enable-ccache parameter is used to speed up the build of the OpenJDK in many cases, but I don’t use it because the build speed is not slow, and if you add this parameter, you will see a lot of red text when importing it into CLion But it doesn’t look good

Generate the Compilation Database

When configuring CLion, directly import the compiled JDK source code, you will find that the header files are all red, you can’t find the prompt, because there is a problem with cmakelists.txt produced by CLion. If you want to solve this problem, you need to modify this file, obviously I won’t fix it.

At last, by using the Compilation JetBrains said the Database (blog.jetbrains.com/clion/2020/…). Building the OpenJDK in CLion solves this problem.

make CONF=macosx-x86_64-server-slowdebug compile-commands
Copy the code

After running this command, compile_commands.json will be generated under ${source_root}/build/macosx-x86_64-server-slowdebug.

compile

Compile before importing CLion, because some modules use precompiled headers, and CLion will tell you that various files cannot be found during indexing if not compiled.

make CONF=macosx-x86_64-server-slowdebug
Copy the code

test

open-jdk-version.png

This proves that we have compiled JDK12

CLion debugging

Import the project

Configure Toolchains before importing Project (Enter Preferences)

toolchains.png

After Toolchains is configured, select ${source_root}/build/macosx-x86_64-server-slowdebug/compile_commands.json,As a from File -> Open Project opens, which imports the Compilation Database file, and CLion begins indexing.

You will find that you cannot see the source code, so you need to modify the root directory of the Project by using Tools -> Compilation Database -> Change Project For Root, select your source directory, ${source_root}, so that the Settings can see the source code in CLion.

${source_root} refers to /Users/naver/ JVM/jdk12-06222165C35f

Debug Configuration

The Build target needs to be configured in Preferences –> Build, Exceution, Deployment –> Custom Build Targets

build.png
clean.png

Since I have already configured it here, the edit page is displayed here. Click + for the first configuration and add it.

With these two configurations, our JDK is recompiled before each build, and JVM code changes can be directly redebugged.

The debug configuration

debug_config.png

Executable: select ${source_root}/build/macosx-x86_64-server-slowdebug/ JDK /bin/ Java, or any other file you want to debug, such as javac; Before Luanch: There is a bug in this, it will not Build every time, but in fact the incremental Build method of OpenJDK is very fast, so it is up to you to choose.

debug

In ${source_root} / SRC/Java. The base/share/native/libjli/Java. C 401 lines of the break point, click the Debug, and then release the F9, and not surprisingly you will encounter this problem

debug-sigsegv.png

Since we use LLDB for debugging, executing the following command under LLDB when entering the first breakpoint can avoid this problem

pro hand -p true -s false SIGSEGV SIGBUS
Copy the code
lldb-pro-hand.png

Finally, you can see the java-version output as follows

java-version.png

Lldbinit ** : pro hand -p true -s false SIGSEGV pro hand -p true -s false SIGSEGV SIGBUS, and then continue the follow-up process. The file content is as follows (the path of main.c file is replaced by itself).

breakpoint set --file /Users/naver/jvm/jdk12-06222165c35f/src/java.base/share/native/launcher/main.c --line 98 -C "pro hand -p true -s false SIGSEGV SIGBUS" --auto-continue true
Copy the code

Combine debugging with Java programs

How to debug java-version by writing your own Java code as a program entry?

First, the Java code is as follows (I wrote it in IDEA):

java-code.png

In CLion, the configuration is as follows

debug-java-code.png

The running results are as follows:

debug-java-code-result.png

Reference documentation

Build and debug OpenJDK using Clion on MacOS

Focus on me not getting lost

Your attention is the biggest encouragement to me, is a brother to pay attention to me (dog head save life)