The configuration of the development machine is as follows: Linux kernel is 2.6, Centos version is 6.9, there should be little difference.
1、 Prepare the environment
Because the kernel is written in C++, GCC is required, and the version of my development machine is 8.1.0;
The make is 3.8.1, and a newer version of 4.2 was originally installed, but it doesn't work, just change to 3.8.1;
JDK
You may need to pay attention to this, because the entire JDK source code only the core code such as JVM is written in C/C++, and some class libraries are written in Java, and finally need to be compiled into class files. This part of the work needs another JDK to compile; To compile 8 JDK, you need to use 1.7, I can also compile with 1.8, and the version I use is 1.8.0_231.
There are a lot of other libraries, you can use yum to install
yum -y install libXtst-devel libXt-devel libXrender-devel libXrandr-devel libXi-devel
yum -y install cups-devel
yum -y install alsa-lib-devel
2、 Download source code
There are two download methods, one is github, the address is:
https://github.com/openjdk/jdk
The other is to download with hg, I recommend you to use hg, I used the code downloaded by gitlab to compile a bunch of errors, changed to hg to download and compile and it passed.
hg download command
hg clone http://hg.openjdk.java.net/jdk8u/jdk8u jdk8u
3、 Compile
3.1、 First enter the source code root directory
cd jdk8u
3.2、 Then download the source code, some codes are not downloaded by default
bash ./get_source.sh
3.3、 Then configure the compilation parameters
bash ./configure --with-target-bits=64--with-debug-level=slowdebug --enable-debug-symbols --with-native-debug-symbols=internal --with-extra-ldflags=-liconv
Pay attention to several parameters
with-debug-level=slowdebug: With debugging information, there are several options: none means no debugging information, and there is a fastdebug, which has limited debugging information. It is recommended to use slowdebug, which has the most debugging information.
enable-debug-symbols and with-native-debug-symbols: both enable debugging information. Only enable-debug-symbols is enabled during the first compilation. There is no breakpoint information when debugging JVM related functions, so bring with -native-debug-symbols will do.
with-extra-ldflags=-liconv: This is because I reported that iconv related functions could not be found when compiling, and the error was reported as follows:
utf_md.c:(.text+0xb6): undefined reference to `libiconv_open'
utf_md.c:(.text+0xf8): undefined reference to `libiconv_open'
This is added according to your own situation.
3.4 Start compiling
make CONF=linux-x86_64-normal-server-slowdebug
The CONF parameter is a compilation configuration, we can configure different compilation parameters, such as one for production without debugging information, the other with debugging information; after configuration, the corresponding folder will be generated in the build directory.
4、 Common compilation errors
4.1、 invalid suffix on literal; C++11 requires a space between literal and string macro [-Werror=literal-suffix]
This is because the compiler warning level is too high, the solution:
Modification: hotspot/make/linux/makefiles/gcc.make
# WARNINGS_ARE_ERRORS =-Werror
WARNINGS_ARE_ERRORS =-Wno-all
4.2、 error: left operand of shift expression ‘(-1 << 28)’ is negative [-fpermissive]
Solution: modify: hotspot/make/linux/makefiles/gcc.make
Add the following content:
CFLAGS +=-fpermissive
Other errors depend on the situation under Baidu. Generally speaking, it is a lack of libraries or something, just install it with yum; there is also a compilation parameter, just modify the relevant Makefile.
5、 debugging
After the above compilation, we can compile with GDB. The GDB version of my development machine is 9.1.
First write a HelloWorld Java code and save it as Hello.java:
publicclassHello{publicstaticvoidmain(String[] args){
System.out.println("hello world");}}
Then compile with javac, the path of javac is:
. /build/linux-x86_64-normal-server-slowdebug/jdk/bin/javac
Start debugging Java:
gdb ./build/linux-x86_64-normal-server-slowdebug/jdk/bin/java
Set the debugging parameters after running gdb:
set args Hello
Then set a breakpoint in the main function of the entry:
break jdk/src/share/bin/main.c:97
Then you can start to roam in the jdk source code.
Some key functions are as follows:
JNI_CreateJavaVM
JNI_GetDefaultJavaVMInitArgs
JNI_GetCreatedJavaVMs
JavaMain
create_vm
Recommended Posts