42

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
  • Actually it isn't that difficult at all. See my updated answer
    – c.s.
    Commented Aug 15, 2013 at 17:40
  • Why the hack are there no downloads for JDKs with debug information included? Commented Jan 15, 2016 at 19:54

5 Answers 5

79

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

15
  • thanks very much, and i encounter a problem in item6:dir /B /S /X jdk7_src*.java > filelist.txt what is /B /S /X?
    – 王奕然
    Commented Aug 16, 2013 at 7:09
  • 1
    /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.
    Commented Aug 16, 2013 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
    Commented May 27, 2014 at 18:56
  • 2
    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
    Commented Aug 1, 2014 at 13:47
  • 1
    @subburoyal perhaps your filelist.txt was not generated correctly. The message says about a flag: D:\etr\java\java which I can only imagine that it finds it inside that file. I would open and examine the file to check if the source paths in there are correct. You might also want to remove any double quotes in all paths. Other than that I can't help much.
    – c.s.
    Commented Dec 8, 2016 at 10:57
14

c-s's jre\lib\endorsed solution is great. Easier to build is with Eclipse: create a Java project, put javax*, java* into src and let Eclipse compile. Then export the jar.

4
  • 1
    I can confirm this works, except that I built the jar using Intellij Idea. Simply put the jar in $jdk/jre/lib/endorsed and voila you can debug normally! Commented Jul 28, 2014 at 14:48
  • 1
    This worked for me in Netbeans as well. I did: 1. create a new empty project 2. copy the source files I wanted in to a new folder inside the project directory 3. add this directory to project source files 4. clean and build 5. copy the jar from the dist directory to the endorsed directory. Worked like a charm.
    – Radiodef
    Commented Jun 12, 2015 at 16:25
  • This is not an answer by itself; it should be a comment or edit to the accepted answer.
    – E-Riz
    Commented Mar 8, 2016 at 15:16
  • I was always having errors with javac (recompiling rt of jdk1.8), but it worked flawlessly in Eclipse. Thanks!
    – qwertzguy
    Commented Jul 7, 2016 at 23:29
1

This article http://www.thejavageek.com/2016/04/03/debug-jdk-source-code/ describe the same but in simple and nice way. You do stuff(compile,make jar) by using eclipse only.

0

Just so you know, the endorsed override mechanism is deprecated and will be removed in a future release (http://docs.oracle.com/javase/8/docs/technotes/guides/standards/).

Use this pom.xml to get JDK 1.8.0_111 sources with debug information:

<project>

  <modelVersion>4.0.0</modelVersion>
  <name>JDK sources with debug information</name>

  <groupId>ex.jdk.debug</groupId>
  <artifactId>jdk-debug-sources</artifactId>
  <version>1.8.0_111</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>jdk-rt</artifactId>
      <version>1.8.0_111</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
          <debug>true</debug>
          <debuglevel>lines,vars,source</debuglevel>
          <source>1.8</source>
          <target>1.8</target>
          <excludes>
            <exclude>com/sun/java/swing/**</exclude>
            <exclude>com/sun/source/util/**</exclude>
          </excludes>
        </configuration>
      </plugin>

    </plugins>
  </build>

 </project>

You'll have to do a manual install of the original rt.jar to be able to run mvn clean install

mvn install:install-file -Dfile=rt.jar -DgroupId=com.oracle -DartifactId=jdk-rt -Dversion=1.8.0_111 -Dpackaging=jar

The rt.jar I copied to the endorsed directory is the original rt.jar but with the original classes replaced by my newly generated classes.

0

In case anybody needs this with tomcat. You need to set up the VM argument Djava.endorsed.dirs and put your compiled jdk jar in it. You can do this c-s's solution or exported with eclipse(all the Java Compiler ClassFile Generation used by de debugger must be active)

Go to Run Configurations > Arguments > VM arguments

Djava.endorsed.dirs="/your/folder/apache-tomcat-xxx/endorsed"

Not the answer you're looking for? Browse other questions tagged or ask your own question.