Configuring the launch of the remote virtual machine to debug

FrankYou發表於2018-08-23

 

Options need to be added to the standard launch of a virtual machine (VM) to enable the debugging architecture, allowing us to attach (hook in) and collect data.

-Xdebug 

Enables remote debugging.

-Xnoagent 

On a Sun VM, disables the proprietary debugging interface thus letting JPDA work correctly.在Sun VM上,禁用專有除錯介面,從而允許JPDA正確工作。

-Djava.compiler=NONE 

Disables Just-In-Time (JIT) compilation..

我們一般debug程式的時候,只是關注其中的一部分程式碼,而且大部分情況下是設定斷點,然後單步執行,而JIT的編譯單位是class,只要我們執行了class裡面的程式碼,JIT就會對整個class進行編譯,而我們實際執行的程式碼一般都是其中的一部分程式碼,所以從整個時間效率上來看,採用JIT反而更費時間。也就是說在JVM遠端除錯這個事情上,禁用JIT(只使用轉譯器,解釋一行執行一條)更合理,所以通過-Djava.compiler=NONE來禁止JIT。 

-Xrunjdwp:

transport=dt_socket,

server=y,

address=5000,

suspend=y

Loads JDWP (Java Debug Wire Protocol), the reference implementation of JPDA. Sub-options further specify the option. 

transport=dt_socket 

Sets the transport type for the connection with the debugging application; in this example, we will connect via a socket.

server=y 

Tells the VM whether it must be receptive to an attaching debugging application.

address=5000 

The port address for our connection; we will need 5000 for our example.

suspend=y 

This option can be set depending on whether we want to suspend the exectution of the VM until a debugging application connects. This is useful if we seek to understand what happens when a server starts.

1、If we wish to make the VM wait for a connection before a full launch, we will need a command line such as this :

java …. -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=5000,suspend=y

2、If we want the VM to be fully executing and listening for a debugging application, we will require a command line such as this one :

java …. -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=5000,suspend=n

 第2種模式是一種比較好的方式,VM採用偵聽的方式等待除錯程式的連線。

相關文章