通过jdb命令连接远程调试的方法

概述

有的时候在判断是否可以本地连接到远程的调试端口,比如是不是网络的限制,还是环境等问题,这个时候可以使用java自带的jdb命令,尝试连接到远程的调试端口,如果可以连接,则排除网络问题,从其他的方向入手进行排查,本文介绍在linux环境,windows环境中通过jdb连接远程调试端口的方法。

jdb = java debugger

linux环境

1.确保宿主机上有jdk

2.远程主机开启调试端口

可通过参数-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000启动jvm

3.jdb命令连接远程

# 存在jdk环境
[root@nctest-snap-test-02 bin]# pwd
/data/jdk/bin

# 有jdb命令
[root@nctest-snap-test-02 bin]# ls
appletviewer  jarsigner       javah         jcmd      jhat   jmc.ini     jstat         orbd        rmiregistry  unpack200
ControlPanel  java            javap         jconsole  jinfo  jps         jstatd        pack200     schemagen    wsgen
extcheck      javac           javapackager  jcontrol  jjs    jrunscript  jvisualvm     policytool  serialver    wsimport
idlj          javadoc         java-rmi.cgi  jdb       jmap   jsadebugd   keytool       rmic        servertool   xjc
jar           javafxpackager  javaws        jdeps     jmc    jstack      native2ascii  rmid        tnameserv

# 使用jdb连接远程服务的调试端口
[root@nctest-snap-test-02 bin]# jdb -attach 172.20.45.87:37500
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
Initializing jdb ...
> 

这样就附加到远程vm上了。

执行threads命令,可看到线程信息

> threads
Group system:
  (java.lang.ref.Reference$ReferenceHandler)0x31be       Reference Handler                         cond. waiting
  (java.lang.ref.Finalizer$FinalizerThread)0x31bd        Finalizer                                 cond. waiting
  (java.lang.Thread)0x31bc                               Signal Dispatcher                         running
  (sun.misc.GC$Daemon)0x31b9                             GC Daemon                                 cond. waiting
Group main:
  (java.lang.Thread)0x31bf                               main                                      running
  (org.apache.juli.AsyncFileHandler$LoggerThread)0x31bb  AsyncFileHandlerWriter-1504109395         cond. waiting
  (java.lang.Thread)0x31ba                               FileHandlerLogFilesCleaner-1              cond. waiting
  (org.apache.logging.log4j.core.util.Log4jThread)0x31b8 Log4j2-TF-2-Scheduled-1                   cond. waiting
  (java.lang.Thread)0x31b7                               check-offline-msg-valid                   sleeping
  (java.lang.Thread)0x31b6                               ConfigurationMonitor                      sleeping
  (nc.bs.framework.execute.impl.ExecutorThread)0x31b5    Thread-7                                  cond. waiting
  (nc.bs.framework.mx.thread.NotifyNMC)0x31b4            notifynmc                                 sleeping
  (nc.bs.framework.execute.impl.ExecutorThread)0x31b3    Thread-9                                  cond. waiting
  (nc.bs.framework.execute.impl.ExecutorThread)0x31b2    Thread-10                                 cond. waiting
  (nc.bs.framework.execute.impl.ExecutorThread)0x31b1    Thread-11                                 cond. waiting
  (nc.bs.framework.execute.impl.ExecutorThread)0x31b0    Thread-12                                 cond. waiting
  (nc.bs.framework.execute.impl.ExecutorThread)0x31af    Thread-13                                 cond. waiting
  (nc.bs.framework.execute.impl.ExecutorThread)0x31ae    Thread-14                                 sleeping
  (java.lang.Thread)0x31ad                               Thread-15                                 sleeping
  (java.util.TimerThread)0x31ac                          Timer-0                                   cond. waiting
  (nc.bs.framework.execute.impl.ExecutorThread)0x31ab    Thread-17                                 cond. waiting
  (nc.bs.framework.execute.impl.ExecutorThread)0x31aa    Thread-19                                 cond. waiting
  (java.lang.Thread)0x31a9                               DisExeManager                             sleeping
  (java.lang.Thread)0x31a8                               Dist_MTaskPro                             sleeping
  (nc.bs.framework.execute.impl.ExecutorThread)0x31a7    Thread-21                                 cond. waiting
  (nc.bs.framework.execute.impl.ExecutorThread)0x31a6    Thread-22                                 cond. waiting
  (nc.bs.framework.execute.impl.ExecutorThread)0x31a5    Thread-23                                 cond. waiting
  (nc.bs.framework.execute.impl.ExecutorThread)0x31a4    Thread-24                                 cond. waiting
  (nc.bs.framework.execute.impl.ExecutorThread)0x31a3    Thread-25                                 cond. waiting
  (nc.bs.framework.execute.impl.ExecutorThread)0x31a2    Thread-26                                 cond. waiting
  (nc.bs.framework.execute.impl.ExecutorThread)0x31a1    Thread-27                                 cond. waiting
  Group middleware-group:
  (java.lang.Thread)0x3198                               middleware-RPCTokenRefreshTimer-1         cond. waiting
  (java.lang.Thread)0x318e                               middleware-HttpNodeWatcher-1              sleeping
  (java.lang.Thread)0x318d                               middleware-HttpNodeWatcher-2              sleeping
  (java.lang.Thread)0x318c                               middleware-HttpNodeWatcher-3              sleeping
> 

这样就可以在交互式的环境中进行调试了。

windows环境

1.确保有jdk的存在

2.此处shift +右键,打开powershell

3.执行jdb命令

注意:linux如果连接了,先断掉,同一时间调试端口只能有一个连接

.\jdb -connect com.sun.jdi.SocketAttach:hostname=172.20.45.87,port=37500

已经连接上

执行threads命令,查看线程信息

可以继续的进行调试的操作。

posted @ 2021-12-31 09:36  Zhai_David  阅读(859)  评论(0编辑  收藏  举报