jdb 调试
目录
1、代码
2、编译代码
3、调试
3.1 jdb
3.2 stop
3.3 list
3.4 run
3.5 list
3.6 step
3.7 print dump eval
3.8 set
3.9 locals
3.10 next
3.11 cont
3.12 n 命令
3.13 help
3.14 !!
3.15 version
4、spring boot调试
4.1 代码
4.2 启动脚本
4.3 连接debug端口
4.3.1 连接
4.3.2 打断点
4.3.3 访问接口
4.3.4 调试
1、代码
/**
* @ClassName Num
* @Description:
* @Author ybwei
* @Date 2020/10/30
* @Version V1.0
**/
public class Num {
public static int add(int a,int b){
return a+b;
}
public static void main(String[] args) {
int a=3;
int b=5;
int c=a+b;
System.out.println(c);
}
}
2、编译代码
>javac -g Num.java
-g
生成所有的调试信息,包括局部变量。缺省情况下,只生成行号和源文件信息。
通过这个参数,可以在生成的class文件中,添加具体的 line,source,vars 信息,具体的作用体现在:
- 因为调试的时候需要符号信息,所以一定需要这个参数
- 在抛出Exception的时候,可以知道具体的错误位置,而非Unknown Source。
添加了这个参数后,不可避免的class文件会变大一点,但是为了以后的维护,还是非常值得的。
3、调试
3.1 jdb
Num.class添加到JDB进行调试。
[root@iZ2ze8o82mhgjvdsmrwz8bZ test]# jdb Num
Initializing jdb ...
3.2 stop
加载成功后,我们通常会在 main函数的位置加上断点
> stop in Num.main
Deferring breakpoint Num.main.
It will be set after the class is loaded.
3.3 list
查看被成功加入的断点
> clear
Breakpoints set:
breakpoint Num.main
3.4 run
运行程序,程序暂停到main函数的第一行
> run
run Num
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint Num.main
Breakpoint hit: "thread=main", Num.main(), line=15 bci=0
15 int a=3;
main[1]
3.5 list
查看源代码,=>当前正在执行的行
main[1] list
11 return a+b;
12 }
13
14 public static void main(String[] args) {
15 => int a=3;
16 int b=5;
17 int c=a+b;
18 System.out.println(c);
19 }
20 }
3.6 step
下一步
main[1] step
Step completed: "thread=main", Num.main(), line=16 bci=2
16 int b=5;
3.7 print dump eval
main[1] main[1] print a
a = 3
main[1] dump a
a = 3
main[1] eval a
a = 3
3.8 set
修改变量
main[1] set a=1
a=1 = 1
main[1] print a
a = 1
3.9 locals
显示当前堆栈帧中的所有本地变量
main[1] locals
Method arguments:
args = instance of java.lang.String[0] (id=385)
Local variables:
a = 1
3.10 next
下一步
main[1] next
>
Step completed: "thread=main", Num.main(), line=17 bci=4
17 int c=a+b;
next和step的区别
在执行有方法调用的语句时,next会把方法执行完,step会进入方法体。所以在调试时,单步执行我们要优先使用next,这样效率比较高。
3.11 cont
从当前位置直接执行到程序结束(或者下一个断点或信号)
main[1] cont
6
>
The application exited
[root@iZ2ze8o82mhgjvdsmrwz8bZ test]#
3.12 n 命令
将某个命令重复执行n次
[root@iZ2ze8o82mhgjvdsmrwz8bZ test]# jdb Num
Initializing jdb ...
> stop in Num.main
Deferring breakpoint Num.main.
It will be set after the class is loaded.
> run
run Num
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint Num.main
Breakpoint hit: "thread=main", Num.main(), line=15 bci=0
15 int a=3;
main[1] 2 step
Step completed: "thread=main", Num.main(), line=16 bci=2
16 int b=5;
main[1] main[1]
Step completed: "thread=main", Num.main(), line=17 bci=4
17 int c=a+b;
3.13 help
main[1] main[1] help
** command list **
connectors -- list available connectors and transports in this VM
run [class [args]] -- start execution of application's main class
threads [threadgroup] -- list threads
thread <thread id> -- set default thread
suspend [thread id(s)] -- suspend threads (default: all)
resume [thread id(s)] -- resume threads (default: all)
where [<thread id> | all] -- dump a thread's stack
wherei [<thread id> | all]-- dump a thread's stack, with pc info
up [n frames] -- move up a thread's stack
down [n frames] -- move down a thread's stack
kill <thread id> <expr> -- kill a thread with the given exception object
interrupt <thread id> -- interrupt a thread
print <expr> -- print value of expression
dump <expr> -- print all object information
eval <expr> -- evaluate expression (same as print)
set <lvalue> = <expr> -- assign new value to field/variable/array element
locals -- print all local variables in current stack frame
classes -- list currently known classes
class <class id> -- show details of named class
methods <class id> -- list a class's methods
fields <class id> -- list a class's fields
threadgroups -- list threadgroups
threadgroup <name> -- set current threadgroup
stop in <class id>.<method>[(argument_type,...)]
-- set a breakpoint in a method
stop at <class id>:<line> -- set a breakpoint at a line
clear <class id>.<method>[(argument_type,...)]
-- clear a breakpoint in a method
clear <class id>:<line> -- clear a breakpoint at a line
clear -- list breakpoints
catch [uncaught|caught|all] <class id>|<class pattern>
-- break when specified exception occurs
ignore [uncaught|caught|all] <class id>|<class pattern>
-- cancel 'catch' for the specified exception
watch [access|all] <class id>.<field name>
-- watch access/modifications to a field
unwatch [access|all] <class id>.<field name>
-- discontinue watching access/modifications to a field
trace [go] methods [thread]
-- trace method entries and exits.
-- All threads are suspended unless 'go' is specified
trace [go] method exit | exits [thread]
-- trace the current method's exit, or all methods' exits
-- All threads are suspended unless 'go' is specified
untrace [methods] -- stop tracing method entrys and/or exits
step -- execute current line
step up -- execute until the current method returns to its caller
stepi -- execute current instruction
next -- step one line (step OVER calls)
cont -- continue execution from breakpoint
list [line number|method] -- print source code
use (or sourcepath) [source file path]
-- display or change the source path
exclude [<class pattern>, ... | "none"]
-- do not report step or method events for specified classes
classpath -- print classpath info from target VM
monitor <command> -- execute command each time the program stops
monitor -- list monitors
unmonitor <monitor#> -- delete a monitor
read <filename> -- read and execute a command file
lock <expr> -- print lock info for an object
threadlocks [thread id] -- print lock info for a thread
pop -- pop the stack through and including the current frame
reenter -- same as pop, but current frame is reentered
redefine <class id> <class file name>
-- redefine the code for a class
disablegc <expr> -- prevent garbage collection of an object
enablegc <expr> -- permit garbage collection of an object
!! -- repeat last command
<n> <command> -- repeat command n times
# <command> -- discard (no-op)
help (or ?) -- list commands
version -- print version information
exit (or quit) -- exit debugger
<class id>: a full class name with package qualifiers
<class pattern>: a class name with a leading or trailing wildcard ('*')
<thread id>: thread number as reported in the 'threads' command
<expr>: a Java(TM) Programming Language expression.
Most common syntax is supported.
Startup commands can be placed in either "jdb.ini" or ".jdbrc"
in user.home or user.dir
3.14 !!
重复执行上一命令,如下重新执行step
main[1] step
>
Step completed: "thread=main", Num.main(), line=18 bci=8
18 System.out.println(c);
main[1] !!
step
8
>
Step completed: "thread=main", Num.main(), line=19 bci=15
19 }
3.15 version
jdb版本号
4、spring boot调试
4.1 代码
代码参考https://blog.csdn.net/xixingzhe2/article/details/109311203
4.2 启动脚本
编辑、启动脚本
[root remote]# vim start-jdb.sh
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar remote.debug-1.0.jar
[root remote]# ./start-jdb.sh
Listening for transport dt_socket at address: 5005
15:45:05,473 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
15:45:05,473 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
15:45:05,474 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/root/remote/remote.debug-1.0.jar!/BOOT-INF/classes!/logback.xml]
15:45:05,517 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@130f889 - URL [jar:file:/root/remote/remote.debug-1.0.jar!/BOOT-INF/classes!/logback.xml] is not of type file
15:45:05,609 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
15:45:05,611 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
15:45:05,631 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [stdout]
15:45:05,644 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:45:05,732 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:45:05,738 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [InfoFile]
15:45:05,763 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@294184992 - No compression will be used
15:45:05,765 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@294184992 - Will use the pattern d:/logs/info/info-%d{yyyy-MM-dd}.%i.log for the active file
15:45:05,769 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2f490758 - The date pattern is 'yyyy-MM-dd' from file name pattern 'd:/logs/info/info-%d{yyyy-MM-dd}.%i.log'.
15:45:05,769 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2f490758 - Roll-over at midnight.
15:45:05,772 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2f490758 - Setting initial period to Fri Oct 30 15:42:11 CST 2020
15:45:05,772 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2f490758 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
15:45:05,772 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2f490758 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
15:45:05,776 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[InfoFile] - This appender no longer admits a layout as a sub-component, set an encoder instead.
15:45:05,776 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[InfoFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
15:45:05,776 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[InfoFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
15:45:05,781 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[InfoFile] - Active log file name: d:/logs/info/info.log
15:45:05,781 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[InfoFile] - File property is set to [d:/logs/info/info.log]
15:45:05,782 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:45:05,783 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [ErrorFile]
15:45:05,786 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@270397815 - No compression will be used
15:45:05,786 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@270397815 - Will use the pattern d:/logs/error/error-%d{yyyy-MM-dd}.%i.log for the active file
15:45:05,786 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@166fa74d - The date pattern is 'yyyy-MM-dd' from file name pattern 'd:/logs/error/error-%d{yyyy-MM-dd}.%i.log'.
15:45:05,786 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@166fa74d - Roll-over at midnight.
15:45:05,787 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@166fa74d - Setting initial period to Tue Oct 27 14:51:05 CST 2020
15:45:05,787 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@166fa74d - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
15:45:05,787 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@166fa74d - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
15:45:05,788 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - This appender no longer admits a layout as a sub-component, set an encoder instead.
15:45:05,788 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
15:45:05,788 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
15:45:05,789 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - Active log file name: d:/logs/error/error.log
15:45:05,789 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - File property is set to [d:/logs/error/error.log]
15:45:05,789 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:45:05,789 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [AllFile]
15:45:05,790 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1089504328 - No compression will be used
15:45:05,790 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1089504328 - Will use the pattern d:/logs/all/all-%d{yyyy-MM-dd}.%i.log for the active file
15:45:05,791 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@276438c9 - The date pattern is 'yyyy-MM-dd' from file name pattern 'd:/logs/all/all-%d{yyyy-MM-dd}.%i.log'.
15:45:05,791 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@276438c9 - Roll-over at midnight.
15:45:05,791 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@276438c9 - Setting initial period to Fri Oct 30 15:42:11 CST 2020
15:45:05,791 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@276438c9 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
15:45:05,791 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@276438c9 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
15:45:05,792 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - This appender no longer admits a layout as a sub-component, set an encoder instead.
15:45:05,792 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
15:45:05,792 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
15:45:05,792 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - Active log file name: d:/logs/all/all.log
15:45:05,792 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - File property is set to [d:/logs/all/all.log]
15:45:05,794 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.mybatis] to TRACE
15:45:05,794 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.cloud.mapper] to DEBUG
15:45:05,794 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
15:45:05,794 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [stdout] to Logger[ROOT]
15:45:05,795 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [InfoFile] to Logger[ROOT]
15:45:05,795 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [ErrorFile] to Logger[ROOT]
15:45:05,795 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [AllFile] to Logger[ROOT]
15:45:05,795 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
15:45:05,800 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@588df31b - Registering current configuration as safe fallback point
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
[INFO ] 2020-10-30 15:45:06.651 [main] com.cloud.ProviderApplication - Starting ProviderApplication v1.0 on iZbp18ily7toc0qsr1pbb6Z with PID 1897 (/root/remote/remote.debug-1.0.jar started by root in /root/remote)
[INFO ] 2020-10-30 15:45:06.688 [main] com.cloud.ProviderApplication - No active profile set, falling back to default profiles: default
[INFO ] 2020-10-30 15:45:08.408 [main] o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
[INFO ] 2020-10-30 15:45:08.435 [main] o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
[INFO ] 2020-10-30 15:45:08.465 [main] o.a.catalina.core.StandardService - Starting service [Tomcat]
[INFO ] 2020-10-30 15:45:08.465 [main] o.a.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.22]
[INFO ] 2020-10-30 15:45:08.592 [main] o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
[INFO ] 2020-10-30 15:45:08.593 [main] o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 1800 ms
[INFO ] 2020-10-30 15:45:09.097 [main] o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
[INFO ] 2020-10-30 15:45:09.355 [main] o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
[INFO ] 2020-10-30 15:45:09.381 [main] o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
[INFO ] 2020-10-30 15:45:09.391 [main] com.cloud.ProviderApplication - Started ProviderApplication in 3.436 seconds (JVM running for 4.265)
4.3 连接debug端口
4.3.1 连接
[root@iZbp18ily7toc0qsr1pbb6Z ~]# jdb -connect com.sun.jdi.SocketAttach:hostname=127.0.0.1,port=5005
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
Initializing jdb ...
>
4.3.2 打断点
> stop in com.cloud.controller.TestController.getUser
Set breakpoint com.cloud.controller.TestController.getUser
4.3.3 访问接口
http://IP:8080/getUser?id=2
4.3.4 调试
>
Breakpoint hit: "thread=http-nio-8080-exec-1", com.cloud.controller.TestController.getUser(), line=40 bci=0
http-nio-8080-exec-1[1] step
>
Step completed: "thread=http-nio-8080-exec-1", com.cloud.service.impl.UserServiceImpl.getUser(), line=21 bci=0
http-nio-8080-exec-1[1] print id
id = "2"
http-nio-8080-exec-1[1]
还没有评论,来说两句吧...