import java.lang.*;
public class Sample
{
public static void main(String[] args)
{
System.out.println("Brought to you by "+getName(10));
}
public static String getName(int i)
{
int j = 20;
while(i>0)
i--;
System.out.println("Value of J is : "+j);
return http://krishnabhargav.blogspot.com;
}
}
javac -g Sample.java
jdb Sample
stop at Sample.main
run
Breakpoint hit: "thread=main", Sample.main(), line=7 bci=0
7 System.out.println("Brought to you by "+getName(10));
3 public class Sample
4 {
5 public static void main(String[] args)
6 {
7 => System.out.println("Brought to you by "+getName(10));
8
9 }
10
11 public static String getName(int i)
12 {
step
9 }
10
11 public static String getName(int i)
12 {
13 => int j = 20;
14 while(i>0)
15 i--;
16 System.out.println("Value of J is : "+j);
17 return http://krishnabhargav.blogspot.com;
18 }
stop at Sample:16
cont
main[1] locals
Method arguments:
i = 0
Local variables:
j = 20
set j = 100
step up
main[1] step up
> Value of J is : 100
Step completed: "thread=main", Sample.main(), line=7 bci=20
7 System.out.println("Brought to you by "+getName(10));
main[1] cont
> Brought to you by http://krishnabhargav.blogspot.com
We have seen how to look at the local variables. We also have seen how to change the value of a variable in scope. But we have not seen how to work with dump <ref>, print <ref> and the other useful "eval" command. You could figure out doing this. But for now, try to repeat the debugging process again. Then set up a breakpoint at main() method and when it breaks, just type in "dump this". See what you get? You get an exception saying that you are currently either in a static method or native method, so to test the dump functionality, you would have to have a object in your scope. And then you can dump that. Suppose you have an instance of Point in your context. Then you can set pt.x using "set pt.x = 100", by this I mean to show that you can use set to manipulate all variables, even those that belong to an object.
Other important thing that we are missing in this tutorial is remote debugging using JDB. I would not present a step by step way to do that but for remote debugging a program, you need to
1. Start the program using Java command, as shown
java -Djava.compiler=NONE -Xnoagent -Xdebug
-Xrunjdwp:transport=dt_socket,address=2502,server=y,suspend=y
Sample
2. Attach jdb from the command line to the server. assuming it is running on localhost, it is done as shown.
jdb -attach localhost:2502