文档章节

Java 远程调试

mrliuze
 mrliuze
发布于 2016/07/28 11:24
字数 2336
阅读 9
收藏 0

写服务端程序,在开发环境下打开远程调试还是非常有用的,还原现场非常容易,让请求方再发个请求即可。如果下来本地调试的话很多环境与管理服务的地址配置什么的都可能不一样,增加了可变因素。

在需要启动服务调试的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

-Xdebug enables debugging capabilities in the JVM which are used by the Java Virtual Machine Tools Interface (JVMTI). JVMTI is a low-level debugging interface used by debuggers and profiling tools. With it, you can inspect the state and control the execution of applications running in the JVM.

The subset of JVMTI that is most typically used by profilers is always available. However, the functionality used by debuggers to be able to step through the code and set breakpoints has some overhead associated with it and is not always available. To enable this functionality you must use the -Xdebug option.

 

WARNING: When running with -Xdebug the JVM is not running at its full speed. Thus, the option should not be used for applications when running in production environments.

Operation

Format: -Xdebug

Include this option at startup.

For Example:

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n myApp

 

-Xrunjdwp

This option loads the JPDA reference implementation of JDWP. This library resides in the target VM and uses JVMDI and JNI to interact with it. It uses a transport and the JDWP protocol to communicate with a separate debugger application.

Operation

Format: -Xrunjdwp:<name1>[=<value1>],<name2>[=<value2>]...

The -Xrunjdwp option can be further qualified by specifying one of the sub-options listed in Table 2-8.

Table 2-8 -Xrunjdwp Sub-options

Name

Required?

Default Value

Description

help

no

N/A

Prints a brief help message and exits the VM.

transport

yes

none

Name of the transport to use in connecting to debugger application.

server

no

“n”

If “y”, listen for a debugger application to attach; otherwise, attach to the debugger application at the specified address.

If “y” and no address is specified, choose a transport address at which to listen for a debugger application, and print the address to the standard output stream.

address

yes, ifserver=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.

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 onuncaught to 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.

The value of the transport sub-option.

The value of the address sub-option (or the generated address if one is not given)

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.

stdalloc

no

“n”

By default, the JDWP reference implementation uses an alternate allocator for its memory allocation. If “y”, the standard C runtime library allocator will be used. This option is mainly for testing; use it with care. Deadlocks can occur in this VM if the alternative allocator is disabled.

strict

no

“n”

If “y”, assume strict JVMDI conformance. This will disable all workarounds to known bugs in JVMDI implementations. This option is mainly for testing and should be used with care.

suspend

no

“y”

If “y”, VMStartEvent has a suspend Policy of SUSPEND_ALL. If “n”, VMStartEvent has a suspend policy of SUSPEND_NONE.

 

For example:

 

java -Xrunjdwp:transport=dt_socket,server=y,address=8000 myApp

This command:

    •  Listens for a socket connection on port 8000.
    •  Suspends 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.

 

-Xrunjdwp:transport=dt_shmem,server=y,suspend=n

This command:

    •  Chooses an available shared memory transport address and print it to stdout.
    •  Listens for a shared memory connection at that address.
    •  Allows the VM to begin executing before the debugger application attaches.

 

-Xrunjdwp:transport=dt_socket,address=myhost:8000

This command:

    •  Attaches to a running debugger application via socket on host myhost at port 8000.
    •  Suspends this VM before main class loads.

 

-Xrunjdwp:transport=dt_shmem,address=mysharedmemory

This command:

    •  Attaches to a running debugger application via shared memory at transport address mysharedmemory.
    •  Suspends this VM before main class loads.

 

-Xrunjdwp:transport=dt_socket,server=y,address=8000,onthrow=java.io.IOException,launch=/usr/local/bin/debugstub

This command:

    •  Waits for an instance of java.io.IOException to be thrown in this VM.
    •  Suspends the VM (suspend=y by default).
    •  Listens for a socket connection on port 8000.
    •  Executes 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.

 

-Xrunjdwp:transport=dt_shmem,server=y,onuncaught=y,launch=d:\bin\debugstub.exe

This command:

    •  Waits for an uncaught exception to be thrown in this VM.
    •  Suspends the VM.
    •  Selects a shared memory transport address and listen for a connection at that address.
    •  Executes the following:

d:\bin\debugstub.exe dt_shmem <address> 
where <address> 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.

 

另外一篇文档(JPDA Connection and Invocation Details  ):

 

Sun VM Invocation Options

This section describes the options necessary to invoke Sun VMs for debugging.

Sun's VM implementations require command line options to load the JDWP agent for debugging. From 5.0 onwards the -agentlib:jdwp option is used to load and specify options to the JDWP agent. For releases prior to 5.0, the -Xdebug and -Xrunjdwp options are used (the 5.0 implementation also supports the -Xdebugand -Xrunjdwp options but the newer -agentlib:jdwp option is preferable as the JDWP agent in 5.0 uses the JVM TI interface to the VM rather than the older JVMDI interface).

If your debugger application uses the JDI Sun Command Line Launching Connector, the connector will use the -Xdebug and -Xrunjdwp options as the Connector may be used to connect to a pre-5.0 target VM.

If the target VM is 5.0 or newer the -agentlib:jdwp option is specified as follows:

-agentlib:jdwp=<sub-options>

Loads the JPDA reference implementation of JDWP. This library resides in the target VM and uses JVM TI and JNI to interact with it. It uses a transport and the JDWP protocol to communicate with a separate debugger application. Specific sub-options are described below.

For releases prior to 5.0 the -Xdebug and -Xrunjdwp options are used:

-Xdebug

Enables debugging

-Xrunjdwp:<sub-options>

Loads the JPDA reference implementation of JDWP. This library resides in the target VM and uses JVMDI and JNI to interact with it. It uses a transport and the JDWP protocol to communicate with a separate debugger application. Specific sub-options are described below.

-agentlib:jdwp and -Xrunjdwp sub-options

The -agentlib:jdwp and -Xrunjdwp option can be further qualified with sub-options. The sub-options are specified as follows:

    -agentlib:jdwp=<name1>[=<value1>],<name2>[=<value2>]...

or

    -Xrunjdwp:<name1>[=<value1>],<name2>[=<value2>]...

The table below describes the options that can be used: 
 

-Xrunjdwp Sub-options
name required? default value description
help no N/A Prints a brief help message and exits the VM.
transport yes none Name of the transport to use in connecting to debugger application. 
server no "n" If "y", listen for a debugger application to attach; otherwise, attach to the debugger application at the specified address. 

If "y" and no address is specified, choose a transport address at which to listen for a debugger application, and print the address to the standard output stream.

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.

  • The value of the transport sub-option.
  • The value of the address sub-option (or the generated address if one is not given)
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 <address>", where <address> 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.

本文转载自:http://www.cnblogs.com/lailailai/p/4560399.html

共有 人打赏支持
mrliuze
粉丝 14
博文 155
码字总数 12377
作品 0
浦东
程序员
私信 提问
eclipse远程debug,java项目

首先java项目要支付可远程调试得到启动时加入参数 启动远程调试 启用JMX,远程性能观察 eclipse配置 其中:host为远程java程序提供远程调试的IP,post为远程java程序提供远程调试的端口 使用 ...

有事没事
2014/12/05
0
0
Eclipse 远程调试 Java 程序

今天同事问我怎么远程调试 java 程序。我不会,那马上搜索看看。在被高度的程序中加个数据就行。然后 eclipse 调试时指定端口就行,太方便了。 比如,现在有一个代码: 把这个java 导出为 re...

鉴客
2010/08/05
1K
1
weblogic debug配置

1.1. 软件环境 Eclipse 3.1 Weblogic 8.1 1.2. 工作原理 远程调试就是允许利用 java tools 里面的jdb 程序连接远程的JAVA虚拟机,进行远程debug ,同时远程的虚拟机启动时候需要开启 debug ...

ianLi
2012/11/02
0
0
Eclipse远程调试Tomcat

最近,一直在研究Tomcat的工作内幕,主要的方法就是参考《How Tomcat Works》 这本书和Tomcat 5.5.26的源代码。 Tomcat的代码结构还是比较清晰的,注释也比较全。但是代码毕竟是静态的,难以...

一枚Sir
2014/08/04
0
0
远程调试运行在Resin上面的Web应用程序

有时候,我们不得不放弃在本地调试我们的程序,把我们的程序先布署到服务器,然后把调试信息都记在日志中,用眼睛瞅日志来调试程序。 不是用这种方式不行,只是效率太低,本来写程序是一件开...

古城痴人
2014/03/27
0
1

没有更多内容

加载失败,请刷新页面

加载更多

OSChina 周一乱弹 —— 加油,还有11个小时就下班了

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @_全村的希望 :吴亦凡把大碗面正儿八经做成单曲了,你别说,还挺好听 《大碗宽面》- 吴亦凡 手机党少年们想听歌,请使劲儿戳(这里) @tom_t...

小小编辑
25分钟前
53
7
C++ vector和list的区别

1.vector数据结构 vector和数组类似,拥有一段连续的内存空间,并且起始地址不变。 因此能高效的进行随机存取,时间复杂度为o(1); 但因为内存空间是连续的,所以在进行插入和删除操作时,会造...

shzwork
今天
6
0
Spring之invokeBeanFactoryPostProcessors详解

Spring的refresh的invokeBeanFactoryPostProcessors,就是调用所有注册的、原始的BeanFactoryPostProcessor。 相关源码 public static void invokeBeanFactoryPostProcessors(Configu......

cregu
昨天
5
0
ibmcom/db2express-c_docker官方使用文档

(DEPRECIATED) Please check DB2 Developer-C Edition for the replacement. What is IBM DB2 Express-C ? ``IBM DB2 Express-C``` is the no-charge community edition of DB2 server, a si......

BG2KNT
昨天
4
0
Ubuntu 18.04.2 LTS nvidia-docker2 : 依赖: docker-ce (= 5:18.09.0~3-0~ubuntu-bionic)

平台:Ubuntu 18.04.2 LTS nvidia-docker2 版本:2.0.3 错误描述:在安装nvidia-docker2的时候报dpkg依赖错误 nvidia-docker2 : 依赖: docker-ce (= 5:18.09.0~3-0~ubuntu-bionic) 先看一下依......

Pulsar-V
昨天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部