Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems when trying to attach to a process using jdb

I need to attach a jdb debug session to a java application that is being executed in a remote host, but I am unable to do it. I am working on linux, with openjdk 1.8.0_65, 64-Bit Server VM.

What I have tried

In order to enable the port listening, I have run the java application adding the following arguments to the command line:

-Xdebug -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:8000,server=y,suspend=n

The following message is displayed in the console:

Listening for transport dt_socket at address: 8000

And the application starts running normally.

Then, from the remote host, I execute the following command:

> jdb -connect com.sun.jdi.SocketAttach:hostname=<remote_host>,port=8000

It fails, the output is:

java.net.ConnectException: Conexión rehusada
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
[...]
Fatal error:
Unable to attach to target VM.

What I have checked

In order to check that the port is actually open and I can connect to it from the remote host, I have performed the following operations:

Lets call the host that is executing the java app. hostA, and the one from which I wan to attach the jdb hostB, then:

Check that there is actually a socket listening on port 8000 in hostA

> netstat -tualpn  | grep :8000
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      1399/<app_name>

In hostA, check that I can connect to the port 8000 (in other words, try to connect from the local host)

> nc -vz localhost 8000
nc: connect to localhost port 8000 (tcp) failed: Connection refused
Connection to localhost 8000 port [tcp/irdmi] succeeded!

With telnet, it seems that it can connect but the connection is closed as soon as it is stablished, maybe because the JVM is expecting some sort of request?

> telnet localhost 8000
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

The java app. displays the following message when telnet connection is closed:

Debugger failed to attach: timeout during handshak

From hostB, check that I can connect to hostA, port 8000

> nc -vz hostA 8000
nc: connect to hostA port 8000 (tcp) failed: Connection refused

With telnet:

> telnet hostA 8000
Trying 172.17.10.127...
telnet: connect to address 172.17.10.127: Connection refused

So, I can't connect from hostA to hostB through the port 8000, although the JVM is listenning in the port 8000, in hostA.

Since the above fails, I have checked if the firewall is causing the connection refused. I have done it by using the nc command:

In hostA:

# First kill the java app (otherwise the port is busy), then:
> nc -l 8000

In hostB:

> nc -vz <hostA> 8000
Connection to hostA 8000 port [tcp/irdmi] succeeded!

As far as I understand, the above means that there is no firewall (or equivalent) blocking the port.

EDIT

Of course, I have tried to do jdb -attach but it fails even doing it from hostA.

like image 971
Dan Avatar asked Mar 13 '17 14:03

Dan


2 Answers

I don't have enough points to comment. So I'm including this as an answer. It really isn't. BUT:

-Xdebug -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:8000,serv=y,suspend=n

Isn't it supposed to be:

-Xdebug -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:8000,server=y,suspend=n

??

[EDIT] You are probably already accounting for this - but also, if you are listening on 127.0.0.1, then it stands to reason that you won't connect from a remote computer. No doubt you are using an actual address, and just didn't include it here...

like image 112
Chris Parker Avatar answered Sep 21 '22 21:09

Chris Parker


I have found the connection problem. In the command I use to launch the java application, I have changed the address parameter as following:

Before:

-Xdebug -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:8000,server=y,suspend=n

After (see address):

-Xdebug -agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n
like image 44
Dan Avatar answered Sep 21 '22 21:09

Dan