以前一直在用Eclipse来设置断点调试程序。远程调试能够在应用程序的运行参数里面加上下面参数:java
JAVA_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9010"
应用程序启动的时候会打开一个端口,而后用Eclipse连上以后,就能够在eclipse里面设断点调试了。eclipse
下面要说的是用BTrace来调试。用BTrace调试不用重启系统,只须要把btrace的脚本运行起来就能够了:socket
import static com.sun.btrace.BTraceUtils.*; import com.sun.btrace.annotations.*;
@BTrace public class TraceMethod{ @OnMethod( clazz="com.example.Server", method="getServerId", location=@Location(Kind.RETURN) ) public static void traceExecute(@Self com.example.Server instance, String requestId, @Return String result){ println("call Server.getRequestId"); println(strcat("requestId is:",str(requestId))); println(strcat("type is:",str(get(field("com.example.Server","timeStamp"),instance)))); println(strcat("return value is:",str(result))); } }
这个脚本程序启动后能够动态的监控Server类里面的getServerId方法的入口参数requestId;Server的成员变量timeStamp;getServerId方法的返回值。ui
运行的命令:debug
btrace -cp "/example/server.jar:/btrace/build" 25045 TraceMethod.java调试