(4) Debug JDK source 无法查看局部变量的问题解决方案

一.问题阐述

首先我们要明白JDK source为什么在debug的时候无法观察局部变量,因为在jdk中,sun对rt.jar中的类编译时,去除了调试信息,这样在eclipse中就不能看到局部变量的值。这样的话,如果在debug的时候查看局部变量,就必须自己编译相应的源码使之拥有调试信息。

要达到这个目的,

一是找网上人家已经编译好的版本,剩下的只能自己去编译。

下面我们对于自己编译提供一个方法,希望对大家有所帮助。

二.解决方案

  1.   选择或创建你的工作目录,比如我选择:E:\
  2.   在你的工作目录中,创建文件夹jdk_src,用于存放源码;创建文件夹jdk_debug,用于输出编译结果。
  3.   从你的机器装jdk的地方找到src.zip,在你得JDK_HOME可以找到,比如我的在C:\Program Files\Java\jdk1.7.0_09目录下。然后把src解压到jdk_src目录下。
  4.   解压完后,选择你需要编译的内容,删除剩下的。一般选择如下的几个文件夹就可以了:java  javax  org 这三个目录就可以了啊
  5. .从你得JDK_HOMME\jre\lib\rt.jar中复制到你的工作目录中,这样做的目的可以让你减少在命令行中输入的文件名。
  6. .执行如下这条命令:
    [plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
    1. dir /B /S /X jdk_src\*.java > filelist.txt  
    创建一个叫做filelist.txt的文件,这个文件存放了所有你将要编译的类的名称。
  7. 执行如下的命令:
    [plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
    1. javac -J-Xms16m -J-Xmx1024m -sourcepath e:\jdk_src -cp e:\rt.jar -d e:\jdk_debug -g @filelist.txt >> log.txt 2>&1  
    这条命令将要编译所有你指定的文件,并把编译结果输出到jdk_debug目录中,同时产生log.txt日记文件。这个日记文件记录着编译警告,但是没有错误。
  8. 进入到jdk_debug目录中,输入如下命令:
    [plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
    1. jar cf0 rt_debug.jar *  
    这个命令可以生成我们需要的rt.jar带编译信息的jar包。
  9. 把这个生成的rt_debug.jar包复制到JDK_HOME\jre\lib\endorsed。如果没有endorsed目录,自己创建一下。
  10. 如果你是在eclipse中debug的。点击Window->Installed JRES,选择相应的JDK,点击Edit,r然后选择点击Add External jars,选择我们步骤9中目中的rt_debug.jar,就可以了。现在完成了所有的步骤了,赶快尝试debug一下,如果可以查看局部变量了,那么恭喜你成功了。
--------------------------------------------------------------
(1)我的pc win7 64位
首先是下载了 jdk-6u27-ea-bin-b03-windows-i586-debug-27_may_2011.jar 版本的jdk 安装之后eclipse3.6 debug源码 
报错  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000006c964419, pid=1100, tid=8028  
在项目路径下生成一堆 错误日志文件,无从解决 。

(2)对D:\jdk1.7.0_32bit自行编译。自后得到相关jar包。点击源码查看时 提示没有源码包,于是添加文件夹指向e:\debug_src目录。可以看到源码,在源码上打断点 还是报错 EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000006c964419, pid=1100, tid=8028  
考虑是不是ied的问题。换了另一个版本的eclipse 正常可以看见源码中的变量值。
--------------------------------------------------------------

三.参考文献

1.debug jdk source can't watch variable what it is  

    http://stackoverflow.com/questions/18255474/debug-jdk-source-cant-watch-variable-what-it-is

    http://www.softwareengineeringsolutions.com/thoughts/Java-Debug-Build.htm

I'm debugging the JDK source like:

 public static int codePointAt(CharSequence seq, int index) {
        char c1 = seq.charAt(index++);
        if (isHighSurrogate(c1)) {
            if (index < seq.length()) {
                char c2 = seq.charAt(index);
                if (isLowSurrogate(c2)) {
                    return toCodePoint(c1, c2);
                }
            }
        }
        return c1;
    }

and I want to see c1 variable before I step into if (isHighSurrogate(c1)). However, when I debug watch c1 variable it display :

enter image description here

I really have tried added rt.jar source, and it really can step into breakpoint of JDK source, like: enter image description here

but why c1 variable can't display?

2 Answers

up vote 24 down vote accepted

Generally speaking, to be able to watch the variables while stepping through JDK source code, you need the class files to be compiled with debug information i.e. compile using javac -g.

So your best bet is to either find an already compiled version with debug information (I couldn't find anything for JDK 7) or you can try compiling the source for yourself.

According to this post (please note that I haven't tried it) you don't need to compile all sources, only the ones you need. Putting your newly compiled classes in the $jdk/jre/lib/ext/endorsed directory, the new classes would be used instead the ones in the original rt.jar.

I believe that should get you started.

Update: Actually I have just tried this process and it is not hard at all. Tested on Windows, JDK 1.7.0_11. All the commands are invoked from command line:

  1. Create your working folder. I chose d:\ root folder
  2. Inside your working folder create the source folder i.e. jdk7_src and output folder jdk_debug
  3. From your JDK_HOME folder get the src.zip file and unzip it inside jdk7_src
  4. Select what you will compile and delete the rest. For all of them you might need additional steps. I have chosen the folders:
    • java
    • javax
    • org
  5. From your JDK_HOME\jre\lib get the file rt.jar and put in the work folder (this is only for convenience to not specify too large file names in the command line).
  6. Execute the command: dir /B /S /X jdk7_src\*.java > filelist.txt to create a file named filelist.txt with the list of all java files that will be compiled. This will be given as input to javac
  7. Execute javac using the command:
    javac -J-Xms16m -J-Xmx1024m -sourcepath d:\jdk7_src -cp d:\rt.jar -d d:\jdk_debug -g @filelist.txt >> log.txt 2>&1 This will compile all the files in the jdk_debug folder and will generate a log.txt file in your working folder. Check the log contents. You should get a bunch of warnings but no error.
  8. Go inside the jdk_debug folder and run the command: jar cf0 rt_debug.jar *. This will generate your new runtime library with degug information.
  9. Copy that new jar to the folder JDK_HOME\jre\lib\endorsed. If the endorsed folder does not exist, create it.

Debug your program in Eclipse. Note how the variables are named normally (no more arg0, arg1 etc). Happy debugging :)

JDK debug

share | improve this answer
 
 
thanks very much, and i encounter a problem in item6:dir /B /S /X jdk7_src*.java > filelist.txt what is /B /S /X?–   user2245634  Aug 16 '13 at 7:09
 
/B: no additional file info. /S: read subdirectories too. X: shows the short names of the files instead of long file names. You can see this information if you run dir /?. –   c.s.  Aug 16 '13 at 7:15
 
Worked great! Excellent response! You can also do this for parts of the Java codebase that aren't included in src.zip, like the SunJCE crypto provider. –   voetsjoeba  May 27 at 18:56 
 
This is great info, but for convenience see @weberjn's answer. –   dgtc  Jul 28 at 14:49 
 
I just compiled the source code of jdk1.7.0-51 in windows7, got 45 errors "error: cannot find symbol." It seems the package (sun.awt.*) is needed, however does not locate by default in the jdk source code. –   rekinyz  Aug 1 at 13:47



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值