how to debug in java

jdb normal usages

1.compile period

javac -g, add complie information

mikeli@dell-pc:~/code/algo_java$ javac -g DemToBin3.java 

 

2.debug class in one window

jdb <className>

set breakpoint:

1:to method : stop in <class name>:<method name>

2:to line : stop at <calss name>.<line number>

run className

 

example 1:

mikeli@dell-pc:~/code/algo_java$ jdb DemToBin3
Initializing jdb ...
> stop in DemToBin3.main
Deferring breakpoint DemToBin3.main.
It will be set after the class is loaded.
> run DemToBin3
run  DemToBin3
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable

VM Started: Set deferred breakpoint DemToBin3.main

 

example 2:

mikeli@dell-pc:~/code/algo_java$ jdb DemToBin3
Initializing jdb ...
> stop at DemToBin3:5
Deferring breakpoint DemToBin3:5.
It will be set after the class is loaded.
> run DemToBin3
run  DemToBin3
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable

VM Started: Set deferred breakpoint DemToBin3:5

 

3.debug another class in other window

for example , A class is running in another window,or remote environment

we compile A class like below:

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y Acalss

jvm will show the running port(such as :8008)

then we use this port to debug:

jdb -attach 8008

 

 

4.debug common usage

execute usage: step  and cont

show parameter information:print  ,  dump  ,locals

show executing code:list

 

show variable's value:   print variable_name

show object's value:   dump object_name

show all variables in stack and heap:   locals

 

 

 

main[1] step

Step completed: "thread=main", DemToBin3.main(), line=6 bci=3
6            String rs="";

main[1] step

Step completed: "thread=main", DemToBin3.main(), line=13 bci=6
13                if(x!=0)

 

cont   Continues execution of the debugged application after a breakpoint, exception, or step.

VM Started: Set deferred breakpoint DemToBin3.main

Breakpoint hit: "thread=main", DemToBin3.main(), line=5 bci=0
5            int x=10;

main[1] cont     
> 1010

 

 

              The print command supports many simple Java expressions including those with method invocations, for example:

              print MyClass.myStaticField
              print myObj.myInstanceField
              print i + j + k (i, j, k are primities and either fields or local variables)
              print myObj.myMethod() (if myMethod returns a non-null)
              print new java.lang.String("Hello").length()

 

 

main[1] list
1    public class DemToBin3
2    {
3        public static void main(String[] args)
4        {
5 =>         int x=10;
6            String rs="";
7    
8            //loop,until x=0
9            //if x!=0, x%2 as mod,link to result.
10            //then x/2,into next loop

 

 

attach source code:

like java option

jdb -sourcepath dir1:dir2:dir3 ...

such as      jdb -sourcepath  .:/bin:/

posted @ 2022-10-30 02:40  MikeLi  阅读(34)  评论(0编辑  收藏  举报