Java 远程调试
写服务端程序,在开发环境下打开远程调试还是非常有用的,还原现场非常容易,让请求方再发个请求即可。如果下来本地调试的话很多环境与管理服务的地址配置什么的都可能不一样,增加了可变因素。
在需要启动服务调试的jvm启动参数中加入(注意:参数要排在启动类名的前面)
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1234
dt_socket:使用的通信方式
server:是主动连接调试器还是作为服务器等待调试器连接
suspend:是否在启动JVM时就暂停,并等待调试器连接
address:地址和端口,地址可以省略,两者用冒号分隔
根据文档和stackoverflow上的讨论,JVM 1.5以后的版本应该使用类似下面的命令(老的还是可以使用的):
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1234
就是一个agentlib就行了,后面的参数都没有变,如果用intellij的远程调试配置的话,它会默认给出这两种参数,让你放到服务器的JVM参数里。
下面摘抄一下官方文档:
-Xdebug
Table 2-8.
| address | yes, if server=n no, otherwise |
"" | Transport address for the connection. If server=n, attempt to attach to debugger application at this address. If server=y, listen for a connection at this address. |
| timeout | no | "" | If server=y specifies the timeout, in milliseconds, to wait for the debugger to attach. If server=n specifies the timeout, in milliseconds, to use when attaching to the debugger. Note that the timeout option may be ignored by some transport implementations. |
| launch | no | none | At completion of JDWP initialization, launch the process given in this string. This option is used in combination with onthrow and/or onuncaughtto provide "Just-In-Time debugging" in which a debugger process is launched when a particular event occurs in this VM.
Note that the launched process is not started in its own window. In most cases the launched process should be a small application which in turns launches the debugger application in its own window. The following strings are appended to the string given in this argument (space-delimited). They can aid the launched debugger in establishing a connection with this VM. The resulting string is executed.
|
| onthrow | no | none | Delay initialization of the JDWP library until an exception of the given class is thrown in this VM. The exception class name must be package-qualified.Connection establishment is included in JDWP initialization, so it will not begin until the exception is thrown. |
| onuncaught | no | "n" | If "y", delay initialization of the JDWP library until an uncaught exception is thrown in this VM. Connection establishment is included in JDWP initialization, so it will not begin until the exception is thrown. See the JDI specification for com.sun.jdi.ExceptionEvent for a definition of uncaught exceptions. |
| suspend | no | "y" | If "y", VMStartEvent has a suspendPolicy of SUSPEND_ALL. If "n", VMStartEvent has a suspendPolicy of SUSPEND_NONE. |
Examples
- -agentlib:jdwp=transport=dt_socket,server=y,address=8000
- Listen for a socket connection on port 8000. Suspend this VM before main class loads (suspend=y by default). Once the debugger application connects, it can send a JDWP command to resume the VM.
- -agentlib:jdwp=transport=dt_socket,server=y,address=localhost:8000,timeout=5000
- Listen for a socket connection on port 8000 on the loopback address only. Terminate if the debugger does not attach within 5 seconds. Suspend this VM before main class loads (suspend=y by default). Once the debugger application connects, it can send a JDWP command to resume the VM.
- -agentlib:jdwp=transport=dt_shmem,server=y,suspend=n
- Choose an available shared memory transport address and print it to stdout. Listen for a shared memory connection at that address. Allow the VM to begin executing before the debugger application attaches.
- -agentlib:jdwp=transport=dt_socket,address=myhost:8000
- Attach to a running debugger application via socket on host myhost at port 8000. Suspend this VM before the main class loads.
- -agentlib:jdwp=transport=dt_shmem,address=mysharedmemory
- Attach to a running debugger application via shared memory at transport address "mysharedmemory". Suspend this VM before the main class loads.
- -agentlib:jdwp=transport=dt_socket,server=y,address=8000,onthrow=java.io.IOException,launch=/usr/local/bin/debugstub
- Wait for an instance of java.io.IOException to be thrown in this VM. Suspend the VM (suspend=y by default). Listen for a socket connection on port 8000. Execute the following: "/usr/local/bin/debugstub dt_socket myhost:8000".This program can launch a debugger process in a separate window which will attach to this VM and begin debugging it.
- -agentlib:jdwp=transport=dt_shmem,server=y,onuncaught=y,launch=d:\bin\debugstub.exe
- Wait for an uncaught exception to be thrown in this VM. Suspend the VM. Select a shared memory transport address and listen for a connection at that address. Execute the following: "d:\bin\debugstub.exe dt_shmem ", where is the selected shared memory address. This program can launch a debugger process in a separate window which will attach to this VM and begin debugging it.