jdk源码调试功能

最近在研究jdk源码,发现debug时无法查看源码里的变量值。 因为sun提供的jdk并不能查看运行中的局部变量,需要重新编译一下rt.jar。

下面这六步是编译jdk的具体步骤:

Step 1:  Locate the JDK source

First navigate to the JDK install directory, and locate the src.zip file. This file contains the JDK sources – and is absolutely invaluable for the rest of this process.

Next, unzip this folder to some location, such as c:src.

Step 2: List all the source files to be compiled

Generate a list of all .java files in the unzipped folder, out to a separate file:
dir /B /S /X c:src*.java > jdk-src.txt

Step 3: Compile the source

Compile the source files named in this file, using the –g option.

javac-verbose -nowarn -g -source 1.6 -target 1.6 -J-Xms512m -J-Xmx1024m -bootclasspath C:javajdk1.6.0_07jrelib t.jar;C:javajdk1.6.0_07jrelibjce.jar;C:javajdk1.6.0_07jrelibjsse.jar;C:javajdk1.6.0_07jrelib esources.jar;C:javajdk1.6.0_07jrelibcharsets.jar;C:javajdk1.6.0_07jrelibdeploy.jar -sourcepath src -classpath src -d jdk-class @jdk-src.txt

 Note the presence of the –bootclasspath flag which makes the stated JARs available to the compiler. This is absolutely critical when trying to build the source distribution of JDK 6.

Step 4: Extract rt.jar

Extract the original rt.jar file, that is found in JAVA_HOMEjrelib, into a temporary folder.

Step 5:  Generate a composite build

Copy the newly compiled .class files from our jdk-class over the folder where the rt.jar file was expanded. This ensures that the final set has old classes overwritten by newer classes with debug information, while still retaining class files that we couldn't compile.

Step 6: Regenerate rt.jar

Finally, recompress all the files from the composite folder into a new rt.jar file, and overwrite the original rt.jar file with this new one.

如果想在eclipse中跟踪调试,需要在Windows -> Preferences -> Java-Installed JRE下,选择安装的jdk,点edit,然后在列出的jre system libraries列表中选择rt.jar,设置其中的Source attachment为C:javajdk1.6.0_07src.zip。

------------------------------------------------------------------------------------------------------

下面是一个方便的linux脚本, 只要设置了JAVA_HOME, 就可以轻松搞定上面的事情了:)

#!/bin/sh

if [ -z "$JAVA_HOME" ]

then

echo "Must set JAVA_HOME"

exit 1

fi

cd $JAVA_HOME

mkdir temp

cp src.zip temp/

cd temp/

mkdir out

unzip src.zip

rm src.zip

find . -name *.java > filelist

echo "$(wc -l filelist) java files to compile"

javac  -g -d out/ -J-Xmx1024m -cp "../jre/lib/tools.jar:../jre/lib/rt.jar" @filelist 

if [ $? != 0 ]

then

echo "compile error!"

exit 1

fi

unzip $JAVA_HOME/jre/lib/rt.jar -d $JAVA_HOME/temp/old_classes

cp -r  $JAVA_HOME/temp/out/* $JAVA_HOME/temp/old_classes/

cd $JAVA_HOME/temp/old_classes/

jar cf rt_debug.jar *

cp rt_debug.jar $JAVA_HOME/jre/lib/

mv $JAVA_HOME/jre/lib/rt.jar $JAVA_HOME/lib/rt_old.jar

cd $JAVA_HOME/jre/lib/

ln -s rt_debug.jar rt.jar

rm -rf $JAVA_HOME/temp

原文:http://hi.baidu.com/austincao/item/e6e91329892497c1a4275a1a

 
原文地址:https://www.cnblogs.com/hansongjiang/p/4823844.html