使用Eclipse的debug调试功能 写给刚走出校门的童鞋

本文写给那些像几年前的我一样刚刚走出校门,及一些未使用过这些高级些的调试技巧的人。

记得刚刚毕业的时候,自己连断点也不会打,当时还在用JCreate,就连毕业设计也是用System.outBug的,想想真的很笨。开始工作后,一个星期过去了,在一个12百万行的系统中找Bug,我依然在用System.out,当时最痛苦的就是修改代码,每次找到疑似Bug,就输出一下,然后重启(那时也不知道代码热替换),直到有一天带我的导师发现了这样笨笨的调试Bug,才让我第一次认识了断点,也知道了代码修改完了可以进行热替换,我这个中国教育的半牺牲品才算向美好生活迈进了一小步。

1、条件断点

断点大家都比较熟悉,在Eclipse Java编辑区的行头双击就会得到一个断点,代码会运行到此处时停止。

条件断点,顾名思义就是一个有一定条件的断点,只有满足了用户设置的条件,代码才会在运行到断点处时停止。

在断点处点击鼠标右键,选择最后一个"Breakpoint Properties"

断点的属性界面及各个选项的意思如下图,

2、变量断点

断点不仅能打在语句上,变量也可以接受断点,

上图就是一个变量的打的断点,在变量的值初始化,或是变量值改变时可以停止,当然变量断点上也是可以加条件的,和上面的介绍的条件断点的设置是一样的。

3、方法断点

方法断点就是将断点打在方法的入口处,

方法断点的特别之处在于它可以打在JDK的源码里,由于JDK在编译时去掉了调试信息,所以普通断点是不能打到里面的,但是方法断点却可以,可以通过这种方法查看方法的调用栈。

4、改变变量值

代码停在了断点处,但是传过来的值不正确,如何修改一下变量值保证代码继续走正确的流程,或是说有一个异常分支老是进不去,能不能调试时改一下条件,看一下异常分支代码是否正确?

Debug视图的Variables小窗口中,我们可以看到mDestJarName变量的值为"F:\Study\eclipsepro\JarDir\jarHelp.jar"

我们可以在变量上右键,选择"Change Value..."在弹出的对话框中修改变量的值,

或是在下面的值查看窗口中修改,保用Ctr+S保存后,变量值就会变成修改后的新值了。

5、重新调试

这种调试的回退不是万能的,只能在当前线程的栈帧中回退,也就说最多只能退回到当前线程的调用的开始处。

回退时,请在需要回退的线程方法上点右键,选择"Dropto Frame"

6、远程调试

用于调试不在本机上的程序,有两种方式,

1、本机作为客户端

2、本机作为服务端

使用远程调试的前提是服务器端和客户端的代码是一致的。

本机作为客户端

本机作客户端比较常用,需要在远端的服务器上的java程序在启动时打开远程调试开关,

服务器端需要加上虚拟机参数

1.5以前版本(1.5以后也可用):【-Xdebug-Xrunjdwp:transport=dt_socket,server=y,address=8000 】

1.5及以上版本:【-agentlib:jdwp=transport=dt_socket,server=y,address=8000】

F:\Study\eclipsepro\screensnap>java-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000 -jarscreensnap3.jar

连接时远程服务器时,需要在Eclipse中新建一个远程调试程序

这里有一个小地方需注意,连接上的时候貌似不能自动切换到Debug视图,不要以为本机的调试程序没有连接到服务器端。

本机作为服务端

同本机作为客户端相比,只需要修改一下“ConnectionType”

这时Eclipse会进入到等待连接的状态

连接程序使用如下参数即可连接本机服务器,IP地址请用实现IP替换~~

【-agentlib:jdwp=transport=dt_socket,suspend=y,address=127.0.0.1:8000】

F:\Study\eclipsepro\screensnap>java-agentlib:jdwp=transport=dt_socket,suspend=y,address=127.0.0.1:8000 -jarscreensnap3.jar

远程调试时本地的代码修改可同步到远程,但不会写到远程的文件里,也就是说本地修改会在下次启动远程程序时就没有了,不会影响到下次使用时的远程代码。

有关远程调试更详细点的介绍请参考【使用Eclipse 远程调试 Java 应用程序】

好像漏了一个断点,异常断点,补一下。

7、异常断点

经常遇见一些异常,然后程序就退出来了,要找到异常发生的地方就比较难了,还好可以打一个异常断点,

上图中我们增加了一个NullPointException的异常断点,当异常发生时,代码会停在异常发生处,定位问题时应该比较有帮助。




           二   英文版本帮助

              

Eclipse Debugging

Thisarticle describes how to debug a Java application in Eclipse.

Thisarticle is based on Eclipse 3.5 (Eclipse Galileo).

1. Overview

The installation and usage of Eclipse as Java IDE isdescribed in Eclipse Java IDE

Thisarticle will focus on how to debug Java applications in Eclipse.

2. Example

The following assumes you know know to develop simplestandard Java programs. If you don't know this please see Eclipse Java IDE .

Createa Java project "de.vogella.debug.first" with the package"de.vogella.debug.first" and the following classes.

//Counter.java                        
package de.vogella.debug.first;

publicclass Counter {
        private intresult=0;
        public int getResult(){
                returnresult;
        }

publicvoid count(){
                for(int i = 0; i < 100; i++){
                        result+= i +1;
                }
        }
}

//Main.java                        
package de.vogella.debug.first;

publicclass Main {

/**
         * @paramargs
        */
        public static voidmain(String[] args) {
                Countercounter = newCounter();
                counter.count();
                System.out.println("Wehave counted " +counter.getResult());
        }

}

3. Debug

3.1. Setting Breakpoints

To setbreakpoints right click in the small left column in your source code editor andselect toggle breakpoint. Or you can double click on this place.

3.2. Starting the Debugger

You can debug your applicationwith selecting your Java file which contains a main method, right click it andselect Run -> Debug.

 

If you have notdefined any breakpoints this will run your program as normal. To debug theprogram you need to define breakpoints.

 

If youstart the debugger the first time Eclipse will asked you if you want to switchto the debug perspective. Answer "yes", you should then see aperspective similar to the following.

 

Youcan use F5 / F6, F7 and F8 to step through your coding.

Table 1. Debugging Key bindings

Command

Description

F5

Goes to the next step in your program. If the next step is a method / function this command will jump into the associated code.

F6

F6 will step over the call, e.g. it will call a method / function without entering the associated code.

F7

F7 will go to the caller of the method/ function. So this will leave the current code and go to the calling code.

F8

Use F8 to go to the next breakpoint. If no further breakpoint is encountered then the program will normally run.

 

Youcan of course use the ui to debug. The following displays the keybindings forthe debug buttons.

 

3.3. Stack

Thecurrent stack is displayed in the "Debug" view.

 

3.4. Variables

Theview "Variables" displays fields and local variables from the currentstack.

Usethe menu to display static variables.

 

 

Viathe menu you can also customize the displayed columns, e.g. you can show theacutual and the declared type.

 

Anothernice feature is the the "New Detail Formater" in which you can definehow a variable is displayed. For example the toString method in our countershows something meaningless, e.g."de.vogella.debug.first.Counter@587c94". Use right mouse click on thevariable -> "New Details Formater"

 

Maintainthe following code to get the output "0" (or whatever is field resultholds at this point).

 

 

4. Advanced Debugging

4.1. Skip all breakpoints

If youwant to temporary de-activate all your breakpoints you can press the button"Skip all breakpoints" which is visible if you select the tabbreakpoints.

If youpress this button again the breakpoints will get activated again.

 

 

4.2. Breakpoint Properties

Aftersetting a breakpoint you can select the properties of the breakpoint to forexample use a condition to restrict when the breakpoint should get toggeled. Inthe properties you can for example restrict that the breakpoint should only beexecuted the 12 hit (Hit Count) or you can put in a conditional expression(which you can also use for logging if you want).

 

 

 

4.3. Watchpoint

Awatchpoint is a breakpoint at which is stop whenever a field read or changed.You can set a watchpoint through a double-click on the left side before thefield declaration. Via the properties of the watchpoint you can define if thebreakpoint should be hit during read access (Field Access) or during writeaccess (Field Modification).

 

4.4. Exception Breakpoint

Theapplication is stopped if the specified exception is thrown. To define anexception breakpoint click on the following icon.

Youcan define if you want to stop and caught and / or uncaught exceptions.

4.5. Method Breakpoint

Amethod breakpoint is defined via double-click in the left border of the editorand the method head. You can define if you want to stop the program duringmethod entry of after leaving the method.

 

4.6. Class breakpoint

AClass Load Breakpoint will stop when the class is loaded. Right-click on aclass in the Outline View and choose "Toggle Class Load Breakpoint"

4.7. Hit Count

Forevery breakpoint you can define via the properties the hit count. If you usethe hit count then the application is stop then the breakpoint is reached thenumber of times defined in the hit count.

4.8. Drop to frame

Eclipseallows you to select any level (frame) in the call stack during debugging andset the JVM to restart to that point.

Thisallows you to rerun a part of your program. Be aware that variables which havebeen modified by the code which you reset remain modified. The drop to framewill return to the the code. Changes made by the code, e.g. to variables /databases will not be reset


原文地址:https://www.cnblogs.com/allenzhaox/p/3201836.html