Set Eclipse to debug JDK source code in Debug mode and display the value of local variables

Recently, the idea of ​​​​researching the JDK source code suddenly sprang up, so I thought of debugging the JDK source code on my commonly used Eclipse.

 

The whole setup process is also simple:

technology sharing

First of all, you need to install the JDK (the root directory of my JDK installation path is D:\Java\jdk-8u92-windows-x64 ), there is a " src.zip " in the JDK installation path, which is the JDK source file compressed package;

technology sharing

Set the JAVA_HOME variable and PATH variable of the environment variable (the value of the JAVA_HOME variable is also D:\Java\jdk-8u92-windows-x64 ).

 

Then open the Eclipse settings to debug the JDK source code in Debug mode:

technology sharing

1 Open the menu bar of Eclipse Windows -> Preferences : click Java -> Installed JREs , click Add -> Standard VM , JRE home will fill in the %JAVA_HOME% path in your environment variable (my %JAVA_HOME% is D:\Java \jdk-8u92-windows-x64 );

2 In the JRE system libraries, you can see the relevant JAR packages of the JDK source code, click on a JAR (such as D:\Java\jdk-8u92-windows-x64\jre\lib\ resources.jar , D:\Java\jdk- 8u92-windows-x64\jre\lib\ rt.jar ...), you can see "Source attachment:(none)", click "Source attachment..." on the right and select External location->Path to point to the JDK source file through the path Compressed package %JAVA_HOME%\ src.zip ;

Through the above two-step settings, there is a JRE that can debug the JDK source code.

Just specify the JRE of the project as this set JRE, you can debug the JDK source code.

 

 

Through the above settings, although debugging can enter the JDK source code, you will find that the incoming parameters of the method in the JDK source code and the local variables in the method body cannot display the value.

That's because in order to save the file size of the JAR package of JRE, Oracle removes the display of variable values ​​when compiling and typing into the JAR package.

So, we need to recompile and hit the JAR package:

1 Find an empty folder as the root directory of the workspace, such as E:\jdk , and create two new folders " jdk_src " and " jdk_debug " in it; " jdk_src " is used to store the .java source file to be compiled, " jdk_debug " is used to store the compiled .class bytecode file;

technology sharing

2 Find rt.jar in %JAVA_HOME%\jre\lib and copy it to workspace  E:\jdk ;

technology sharing

3 Open the JDK source file compressed package %JAVA_HOME%\ src.zip , drag and drop the three folders java , javax , org directly to the E:\jdk\ jdk_src file used to store the .java source file to be compiled folder;

4 Run CMD and execute the following commands:

4.1 Change the directory to the workspace CD E:\jdk ; list all the .java source files to be compiled in E:\jdk\jdk_src to filelist.txt: dir /B /S /X .\jdk_src\*.java > . \filelist.txt ; After completion, a filelist.txt will be generated in E:\jdk, which lists many lines **.java;

4.2 Compile all the .java source files in jdk_src to be compiled into .class bytecode files, and put them in jdk_debug , the folder used to store the compiled .class bytecode files: javac -J-Xms16m -J -Xmx1024m -sourcepath .\jdk_src -cp .\rt.jar -d .\jdk_debug -g @.\filelist.txt >> .\log.txt 2>&1 ; will be generated in E:\jdk\ during compilation Compile the log file log.txt. After the compilation is completed, you can see that the corresponding folder and .class bytecode file are generated in E:\jdk\jdk_debug;

4.3 Switch the directory to CD E:\jdk\jdk_debug , and type the .class bytecode file into a JAR package: jar cf0 .\rt_debug.jar .\* ; after the package is completed, rt_debug.jar will be generated in E:\jdk\jdk_debug .

4.4   Copy the rt_debug.jar package to JDK_HOME\jre\lib\endorsed. If there is no endorsed directory, create it yourself

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326175147&siteId=291194637