美文网首页
ieda-远程调试(java -agentlib:jdwp)

ieda-远程调试(java -agentlib:jdwp)

作者: lgtn | 来源:发表于2021-06-23 11:28 被阅读0次
     当项目投产后,可能会遇到之前测试环境上没有遇到的问题,而又无法快速定位问题原因,令人十分捉急。
  如果开发人员电脑可以接入生产环境,则可以采用java -agentlib:jdwp命令、idea或eclipse进行远程调试。
  本文中使用IDEA进行远程调试。

一、服务端启动jar命令,增加java -agentlib:jdwp,如下:

需要保证-agentlib:jdwp命令在 -jar命令之前
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005  -Xmx512m -Xms256m -Dspring.config.location=application.yml -jar abc-1.0.0.jar

命令解释

1、输入java命令后,可以看到agentlib的介绍,表示加载本机的代理库

    -agentlib:<libname>[=<选项>]
                  加载本机代理库 <libname>, 例如 -agentlib:hprof
                  另请参阅 -agentlib:jdwp=help 和 -agentlib:hprof=help
    -agentpath:<pathname>[=<选项>]
                  按完整路径名加载本机代理库
    -javaagent:<jarpath>[=<选项>]
                  加载 Java 编程语言代理, 请参阅 java.lang.instrument

2、继续输入:java -agentlib:jdwp=help。
JDWP 是 Java Debug Wire Protocol 的缩写,它定义了调试器(debugger)和被调试的 Java 虚拟机(target vm)之间的通信协议。
当使用java -agentlib:jdwp启动jar时,表示也在会address对应的端口上启动一个tcp监听,等待客户端(调试端,比如idea、eclipse)连接。


               Java Debugger JDWP Agent Library
               --------------------------------

  (see http://java.sun.com/products/jpda for more information)

jdwp usage: java -agentlib:jdwp=[help]|[<option>=<value>, ...]

Option Name and Value            Description                       Default
---------------------            -----------                       -------
suspend=y|n                      wait on startup?                  y
transport=<name>                 transport spec                    none
address=<listen/attach address>  transport spec                    ""
server=y|n                       listen for debugger?              n
launch=<command line>            run debugger on event             none
onthrow=<exception name>         debug on throw                    none
onuncaught=y|n                   debug on any uncaught?            n
timeout=<timeout value>          for listen/attach in milliseconds n
mutf8=y|n                        output modified utf-8             n
quiet=y|n                        control over terminal messages    n

Obsolete Options
----------------
strict=y|n
stdalloc=y|n

Examples
--------
  - Using sockets connect to a debugger at a specific address:
    java -agentlib:jdwp=transport=dt_socket,address=localhost:8000 ...
  - Using sockets listen for a debugger to attach:
    java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y ...

Notes
-----
  - A timeout value of 0 (the default) is no timeout.

Warnings
--------
  - The older -Xrunjdwp interface can still be used, but will be removed in
    a future release, for example:
        java -Xdebug -Xrunjdwp:[help]|[<option>=<value>, ...]

二、idea配置

iShot2021-06-23 11.25.42.png

三、启动调试

1、启动jar
2、在idea中启动配置好的Remote
3、在源码中打断点,就可以调试

另Arthas很强大,也可以使用Arthas进行线上调试。

参考资料

1、JDWP:https://paper.seebug.org/933/
2、# Arthas
3、https://arthas.aliyun.com/doc/download.html

相关文章

网友评论

      本文标题:ieda-远程调试(java -agentlib:jdwp)

      本文链接:https://www.haomeiwen.com/subject/pgfbyltx.html