Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.12, 2006

'jdb' - The Java Debugger

Part:   1  2  3  4  5  6  7  8  9  

Java Tool Tutorials

© 2006 Dr. Herong Yang

Latest updates:

  'javac' - The Java Compiler

  'java' - The Java Launcher

  'jdb' - The Java Debugger

  JAR File & 'jar' Tool

  Certificates and 'keytool'

  Installing J2SE 1.5.0

... Table of Contents

(Continued from previous part...)

What I have done here:

  • I let the main thread executed a full period stopping at line 19 again. Of course, the sub thread execute some number of statements and stopped inside the sleep() native method.
  • Then I listed all threads and want to find a way to switch the command prompt to the sub thread to check local variable there. The manual says to use "thread n", where n is the thread index to change the current thread. But where can I to get the thread index? I tried to search the Web and did not get any clear answer.
  • However I got the answer from the output of the threads output. The thread index was listed as a hex number next to the class name. For example, "(PrimeNumberSeeker)0x118" means thread 0x118 = 280.
  • So I used "thread 280" to switch to the sub thread as the current thread. Notice that the command prompt changed.
  • The first "list" command did not work, because the sub thread was stopped inside the "sleep()" method. The "step out" command continued the execution just enough to finish "sleep()" and back to the caller, run().

6. Running one thread only:

Thread-0[1] stop at PrimeNumberSeeker:39
Set breakpoint PrimeNumberSeeker:39

Thread-0[1] cont
>
Breakpoint hit: "thread=Thread-0", PrimeNumberSeeker.run(), line=39
39                primes[count-1] = current;

Thread-0[1] cont
>
Breakpoint hit: "thread=main", PrimeNumberSeeker.main(), line=19
19             System.out.println( i+", "+t.current+", "+t.count);

main[1] cont
> 12, 23, 9

Breakpoint hit: "thread=Thread-0", PrimeNumberSeeker.run(), line=39
39                primes[count-1] = current;

Thread-0[1] cont
>
Breakpoint hit: "thread=main", PrimeNumberSeeker.main(), line=19
19             System.out.println( i+", "+t.current+", "+t.count);

main[1] suspend 1

main[1] cont
>
Breakpoint hit: "thread=Thread-0", PrimeNumberSeeker.run(), line=39
39                primes[count-1] = current;

Thread-0[1] cont
>
Breakpoint hit: "thread=Thread-0", PrimeNumberSeeker.run(), line=39
39                primes[count-1] = current;

Thread-0[1] cont
>
Breakpoint hit: "thread=Thread-0", PrimeNumberSeeker.run(), line=39
39                primes[count-1] = current;

Thread-0[1] print current
 current = 41

Thread-0[1] print count
 count = 13

What I have done here:

  • After check different areas of the code, I wanted to only the break the execution when a new prime number is found. So I created a breakpoint at line 39.
  • Then I used "cont" to let the execution to continue. But two threads always executed at the same time, and stopped at the same time whenever one thread reached a breakpoint.
  • So I used "suspend 1" to suspend the main thread. This is a cool command, allowing me to concentrate on a single thread. Of course, you can use "resume 1" to release the suspension.

I think I have done enough debugging practice and want to stop here now. However, my program does have a calculation error. I want to leave it to you to find out.

Conclusions

  • "jdb" is a nice debugging tool. But it only offers a command line interface, not so easy to use. It is much more efficient to use graphical interface debugger.
  • JPDA is well designed, allowing us to debug Java applications remotely.
  • Debugging multi-thread application is tricky. The following "jdb" notes may help you.
  • Whenever one thread reaches a break point, all other threads are stopped also.
  • The command prompt tells what is the current thread.
  • "where all" tells where the execution are currently in all threads.
  • "threads" lists all the threads with thread indexes as Hex numbers.

Part:   1  2  3  4  5  6  7  8  9  

Dr. Herong Yang, updated in 2006
Java Tool Tutorials - Herong's Tutorial Notes - 'jdb' - The Java Debugger