jdb 调试

短命女 2022-11-20 06:58 212阅读 0赞

目录

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、代码

  1. /**
  2. * @ClassName Num
  3. * @Description:
  4. * @Author ybwei
  5. * @Date 2020/10/30
  6. * @Version V1.0
  7. **/
  8. public class Num {
  9. public static int add(int a,int b){
  10. return a+b;
  11. }
  12. public static void main(String[] args) {
  13. int a=3;
  14. int b=5;
  15. int c=a+b;
  16. System.out.println(c);
  17. }
  18. }

2、编译代码

  1. >javac -g Num.java

-g

生成所有的调试信息,包括局部变量。缺省情况下,只生成行号和源文件信息。

通过这个参数,可以在生成的class文件中,添加具体的 line,source,vars 信息,具体的作用体现在:

  1. 因为调试的时候需要符号信息,所以一定需要这个参数
  2. 在抛出Exception的时候,可以知道具体的错误位置,而非Unknown Source。

添加了这个参数后,不可避免的class文件会变大一点,但是为了以后的维护,还是非常值得的。

3、调试

3.1 jdb

Num.class添加到JDB进行调试。

  1. [root@iZ2ze8o82mhgjvdsmrwz8bZ test]# jdb Num
  2. Initializing jdb ...

3.2 stop

加载成功后,我们通常会在 main函数的位置加上断点

  1. > stop in Num.main
  2. Deferring breakpoint Num.main.
  3. It will be set after the class is loaded.

3.3 list

查看被成功加入的断点

  1. > clear
  2. Breakpoints set:
  3. breakpoint Num.main

3.4 run

运行程序,程序暂停到main函数的第一行

  1. > run
  2. run Num
  3. Set uncaught java.lang.Throwable
  4. Set deferred uncaught java.lang.Throwable
  5. >
  6. VM Started: Set deferred breakpoint Num.main
  7. Breakpoint hit: "thread=main", Num.main(), line=15 bci=0
  8. 15 int a=3;
  9. main[1]

3.5 list

查看源代码,=>当前正在执行的行

  1. main[1] list
  2. 11 return a+b;
  3. 12 }
  4. 13
  5. 14 public static void main(String[] args) {
  6. 15 => int a=3;
  7. 16 int b=5;
  8. 17 int c=a+b;
  9. 18 System.out.println(c);
  10. 19 }
  11. 20 }

3.6 step

下一步

  1. main[1] step
  2. Step completed: "thread=main", Num.main(), line=16 bci=2
  3. 16 int b=5;

3.7 print dump eval

  1. main[1] main[1] print a
  2. a = 3
  3. main[1] dump a
  4. a = 3
  5. main[1] eval a
  6. a = 3

3.8 set

修改变量

  1. main[1] set a=1
  2. a=1 = 1
  3. main[1] print a
  4. a = 1

3.9 locals

显示当前堆栈帧中的所有本地变量

  1. main[1] locals
  2. Method arguments:
  3. args = instance of java.lang.String[0] (id=385)
  4. Local variables:
  5. a = 1

3.10 next

下一步

  1. main[1] next
  2. >
  3. Step completed: "thread=main", Num.main(), line=17 bci=4
  4. 17 int c=a+b;

next和step的区别

在执行有方法调用的语句时,next会把方法执行完,step会进入方法体。所以在调试时,单步执行我们要优先使用next,这样效率比较高。

3.11 cont

从当前位置直接执行到程序结束(或者下一个断点或信号)

  1. main[1] cont
  2. 6
  3. >
  4. The application exited
  5. [root@iZ2ze8o82mhgjvdsmrwz8bZ test]#

3.12 n 命令

将某个命令重复执行n次

  1. [root@iZ2ze8o82mhgjvdsmrwz8bZ test]# jdb Num
  2. Initializing jdb ...
  3. > stop in Num.main
  4. Deferring breakpoint Num.main.
  5. It will be set after the class is loaded.
  6. > run
  7. run Num
  8. Set uncaught java.lang.Throwable
  9. Set deferred uncaught java.lang.Throwable
  10. >
  11. VM Started: Set deferred breakpoint Num.main
  12. Breakpoint hit: "thread=main", Num.main(), line=15 bci=0
  13. 15 int a=3;
  14. main[1] 2 step
  15. Step completed: "thread=main", Num.main(), line=16 bci=2
  16. 16 int b=5;
  17. main[1] main[1]
  18. Step completed: "thread=main", Num.main(), line=17 bci=4
  19. 17 int c=a+b;

3.13 help

  1. main[1] main[1] help
  2. ** command list **
  3. connectors -- list available connectors and transports in this VM
  4. run [class [args]] -- start execution of application's main class
  5. threads [threadgroup] -- list threads
  6. thread <thread id> -- set default thread
  7. suspend [thread id(s)] -- suspend threads (default: all)
  8. resume [thread id(s)] -- resume threads (default: all)
  9. where [<thread id> | all] -- dump a thread's stack
  10. wherei [<thread id> | all]-- dump a thread's stack, with pc info
  11. up [n frames] -- move up a thread's stack
  12. down [n frames] -- move down a thread's stack
  13. kill <thread id> <expr> -- kill a thread with the given exception object
  14. interrupt <thread id> -- interrupt a thread
  15. print <expr> -- print value of expression
  16. dump <expr> -- print all object information
  17. eval <expr> -- evaluate expression (same as print)
  18. set <lvalue> = <expr> -- assign new value to field/variable/array element
  19. locals -- print all local variables in current stack frame
  20. classes -- list currently known classes
  21. class <class id> -- show details of named class
  22. methods <class id> -- list a class's methods
  23. fields <class id> -- list a class's fields
  24. threadgroups -- list threadgroups
  25. threadgroup <name> -- set current threadgroup
  26. stop in <class id>.<method>[(argument_type,...)]
  27. -- set a breakpoint in a method
  28. stop at <class id>:<line> -- set a breakpoint at a line
  29. clear <class id>.<method>[(argument_type,...)]
  30. -- clear a breakpoint in a method
  31. clear <class id>:<line> -- clear a breakpoint at a line
  32. clear -- list breakpoints
  33. catch [uncaught|caught|all] <class id>|<class pattern>
  34. -- break when specified exception occurs
  35. ignore [uncaught|caught|all] <class id>|<class pattern>
  36. -- cancel 'catch' for the specified exception
  37. watch [access|all] <class id>.<field name>
  38. -- watch access/modifications to a field
  39. unwatch [access|all] <class id>.<field name>
  40. -- discontinue watching access/modifications to a field
  41. trace [go] methods [thread]
  42. -- trace method entries and exits.
  43. -- All threads are suspended unless 'go' is specified
  44. trace [go] method exit | exits [thread]
  45. -- trace the current method's exit, or all methods' exits
  46. -- All threads are suspended unless 'go' is specified
  47. untrace [methods] -- stop tracing method entrys and/or exits
  48. step -- execute current line
  49. step up -- execute until the current method returns to its caller
  50. stepi -- execute current instruction
  51. next -- step one line (step OVER calls)
  52. cont -- continue execution from breakpoint
  53. list [line number|method] -- print source code
  54. use (or sourcepath) [source file path]
  55. -- display or change the source path
  56. exclude [<class pattern>, ... | "none"]
  57. -- do not report step or method events for specified classes
  58. classpath -- print classpath info from target VM
  59. monitor <command> -- execute command each time the program stops
  60. monitor -- list monitors
  61. unmonitor <monitor#> -- delete a monitor
  62. read <filename> -- read and execute a command file
  63. lock <expr> -- print lock info for an object
  64. threadlocks [thread id] -- print lock info for a thread
  65. pop -- pop the stack through and including the current frame
  66. reenter -- same as pop, but current frame is reentered
  67. redefine <class id> <class file name>
  68. -- redefine the code for a class
  69. disablegc <expr> -- prevent garbage collection of an object
  70. enablegc <expr> -- permit garbage collection of an object
  71. !! -- repeat last command
  72. <n> <command> -- repeat command n times
  73. # <command> -- discard (no-op)
  74. help (or ?) -- list commands
  75. version -- print version information
  76. exit (or quit) -- exit debugger
  77. <class id>: a full class name with package qualifiers
  78. <class pattern>: a class name with a leading or trailing wildcard ('*')
  79. <thread id>: thread number as reported in the 'threads' command
  80. <expr>: a Java(TM) Programming Language expression.
  81. Most common syntax is supported.
  82. Startup commands can be placed in either "jdb.ini" or ".jdbrc"
  83. in user.home or user.dir

3.14 !!

重复执行上一命令,如下重新执行step

  1. main[1] step
  2. >
  3. Step completed: "thread=main", Num.main(), line=18 bci=8
  4. 18 System.out.println(c);
  5. main[1] !!
  6. step
  7. 8
  8. >
  9. Step completed: "thread=main", Num.main(), line=19 bci=15
  10. 19 }

3.15 version

jdb版本号

4、spring boot调试

4.1 代码

代码参考https://blog.csdn.net/xixingzhe2/article/details/109311203

4.2 启动脚本

编辑、启动脚本

  1. [root remote]# vim start-jdb.sh
  2. java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar remote.debug-1.0.jar
  3. [root remote]# ./start-jdb.sh
  4. Listening for transport dt_socket at address: 5005
  5. 15:45:05,473 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
  6. 15:45:05,473 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
  7. 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]
  8. 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
  9. 15:45:05,609 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
  10. 15:45:05,611 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
  11. 15:45:05,631 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [stdout]
  12. 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
  13. 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]
  14. 15:45:05,738 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [InfoFile]
  15. 15:45:05,763 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@294184992 - No compression will be used
  16. 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
  17. 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'.
  18. 15:45:05,769 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2f490758 - Roll-over at midnight.
  19. 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
  20. 15:45:05,772 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2f490758 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
  21. 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
  22. 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.
  23. 15:45:05,776 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[InfoFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
  24. 15:45:05,776 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[InfoFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
  25. 15:45:05,781 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[InfoFile] - Active log file name: d:/logs/info/info.log
  26. 15:45:05,781 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[InfoFile] - File property is set to [d:/logs/info/info.log]
  27. 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]
  28. 15:45:05,783 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [ErrorFile]
  29. 15:45:05,786 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@270397815 - No compression will be used
  30. 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
  31. 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'.
  32. 15:45:05,786 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@166fa74d - Roll-over at midnight.
  33. 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
  34. 15:45:05,787 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@166fa74d - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
  35. 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
  36. 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.
  37. 15:45:05,788 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
  38. 15:45:05,788 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
  39. 15:45:05,789 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - Active log file name: d:/logs/error/error.log
  40. 15:45:05,789 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - File property is set to [d:/logs/error/error.log]
  41. 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]
  42. 15:45:05,789 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [AllFile]
  43. 15:45:05,790 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1089504328 - No compression will be used
  44. 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
  45. 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'.
  46. 15:45:05,791 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@276438c9 - Roll-over at midnight.
  47. 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
  48. 15:45:05,791 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@276438c9 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
  49. 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
  50. 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.
  51. 15:45:05,792 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
  52. 15:45:05,792 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
  53. 15:45:05,792 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - Active log file name: d:/logs/all/all.log
  54. 15:45:05,792 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - File property is set to [d:/logs/all/all.log]
  55. 15:45:05,794 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.mybatis] to TRACE
  56. 15:45:05,794 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.cloud.mapper] to DEBUG
  57. 15:45:05,794 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
  58. 15:45:05,794 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [stdout] to Logger[ROOT]
  59. 15:45:05,795 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [InfoFile] to Logger[ROOT]
  60. 15:45:05,795 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [ErrorFile] to Logger[ROOT]
  61. 15:45:05,795 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [AllFile] to Logger[ROOT]
  62. 15:45:05,795 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
  63. 15:45:05,800 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@588df31b - Registering current configuration as safe fallback point
  64. . ____ _ __ _ _
  65. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  66. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  67. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  68. ' |____| .__|_| |_|_| |_\__, | / / / /
  69. =========|_|==============|___/=/_/_/_/
  70. :: Spring Boot :: (v2.1.7.RELEASE)
  71. [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)
  72. [INFO ] 2020-10-30 15:45:06.688 [main] com.cloud.ProviderApplication - No active profile set, falling back to default profiles: default
  73. [INFO ] 2020-10-30 15:45:08.408 [main] o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
  74. [INFO ] 2020-10-30 15:45:08.435 [main] o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
  75. [INFO ] 2020-10-30 15:45:08.465 [main] o.a.catalina.core.StandardService - Starting service [Tomcat]
  76. [INFO ] 2020-10-30 15:45:08.465 [main] o.a.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.22]
  77. [INFO ] 2020-10-30 15:45:08.592 [main] o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
  78. [INFO ] 2020-10-30 15:45:08.593 [main] o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 1800 ms
  79. [INFO ] 2020-10-30 15:45:09.097 [main] o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
  80. [INFO ] 2020-10-30 15:45:09.355 [main] o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
  81. [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 ''
  82. [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 连接

  1. [root@iZbp18ily7toc0qsr1pbb6Z ~]# jdb -connect com.sun.jdi.SocketAttach:hostname=127.0.0.1,port=5005
  2. Set uncaught java.lang.Throwable
  3. Set deferred uncaught java.lang.Throwable
  4. Initializing jdb ...
  5. >

4.3.2 打断点

  1. > stop in com.cloud.controller.TestController.getUser
  2. Set breakpoint com.cloud.controller.TestController.getUser

4.3.3 访问接口

http://IP:8080/getUser?id=2

4.3.4 调试

  1. >
  2. Breakpoint hit: "thread=http-nio-8080-exec-1", com.cloud.controller.TestController.getUser(), line=40 bci=0
  3. http-nio-8080-exec-1[1] step
  4. >
  5. Step completed: "thread=http-nio-8080-exec-1", com.cloud.service.impl.UserServiceImpl.getUser(), line=21 bci=0
  6. http-nio-8080-exec-1[1] print id
  7. id = "2"
  8. http-nio-8080-exec-1[1]

发表评论

表情:
评论列表 (有 0 条评论,212人围观)

还没有评论,来说两句吧...

相关阅读

    相关 jdb(jdbc连接数据库步骤)

    JDB自润滑轴承国标尺寸哪里有啊? 该产物具有联合强度高、承载才能大、耐磨功能好国标等长处。特殊合适于中速中载及低速高载等场所。经过特别工艺手腕,能够在磨擦面上加工出各类

    相关 调试

    Inspector调试 Vs Code cmd中输入node --inspect-brk debug.js意思是使程序在入口的地方停下来 function tes