0

I am trying to connect to JDWP process using jdb.exe. It all worked before I have reinstalled my Windows 10 and everything else.

I have a following Batch script:

"adb.exe" shell am force-stop com.rockstar.gta3
"adb.exe" shell am start -D -W -n com.rockstar.gta3/.GTA3
setlocal
for /f "delims=" %%a in ('"adb.exe" shell pidof com.rockstar.gta3') do @set pid=%%a
echo PID=%pid%
"adb.exe" forward tcp:23915 jdwp:%pid%

set VALUE=CurrentVersion

set KEY="HKLM\SOFTWARE\JavaSoft\JDK"
reg query %KEY% /v %VALUE% 2>nul && goto JDKisInstalled

set KEY="HKLM\SOFTWARE\JavaSoft\Java Development Kit"
reg query %KEY% /v %VALUE% 2>nul && goto JDKisInstalled

echo JDK not installed, path to JDK could not be read from registry.
pause

:JDKisInstalled
set JDK_VERSION=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
set JDK_VERSION=%%b
)

set JDK_HOME=
for /f "tokens=2,*" %%a in ('reg query %KEY%\%JDK_VERSION% /v JavaHome ^| findstr JavaHome') do (
    set JDK_HOME=%%b
)

echo exit | "%JDK_HOME%\bin\jdb.exe" -connect com.sun.jdi.SocketAttach:hostname=localhost,port=23915
pause

It worked before. Now after reinstalling Windows 10 I get the following error, while executing jdb.exe:

C:\Users\fastm\Desktop>"adb.exe" shell am force-stop com.rockstar.gta3

C:\Users\fastm\Desktop>"adb.exe" shell am start -D -W -n com.rockstar.gta3/.GTA3
Starting: Intent { cmp=com.rockstar.gta3/.GTA3 }
Status: timeout
Activity: com.rockstar.gta3/.GTA3
WaitTime: 10103
Complete

C:\Users\fastm\Desktop>setlocal

C:\Users\fastm\Desktop>for /F "delims=" %a in ('"adb.exe" shell pidof com.rockstar.gta3') do @set pid=%a

C:\Users\fastm\Desktop>echo PID=13456
PID=13456

C:\Users\fastm\Desktop>"adb.exe" forward tcp:23915 jdwp:13456

C:\Users\fastm\Desktop>set VALUE=CurrentVersion

C:\Users\fastm\Desktop>set KEY="HKLM\SOFTWARE\JavaSoft\JDK"

C:\Users\fastm\Desktop>reg query "HKLM\SOFTWARE\JavaSoft\JDK" /v CurrentVersion   2>nul  && goto JDKisInstalled

C:\Users\fastm\Desktop>set KEY="HKLM\SOFTWARE\JavaSoft\Java Development Kit"

C:\Users\fastm\Desktop>reg query "HKLM\SOFTWARE\JavaSoft\Java Development Kit" /v CurrentVersion   2>nul  && goto JDKisInstalled

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit
    CurrentVersion    REG_SZ    1.8


C:\Users\fastm\Desktop>set JDK_VERSION=

C:\Users\fastm\Desktop>for /F "tokens=2,*" %a in ('reg query "HKLM\SOFTWARE\JavaSoft\Java Development Kit" /v CurrentVersion | findstr CurrentVersion') do (set JDK_VERSION=%b )

C:\Users\fastm\Desktop>(set JDK_VERSION=1.8 )

C:\Users\fastm\Desktop>set JDK_HOME=

C:\Users\fastm\Desktop>for /F "tokens=2,*" %a in ('reg query "HKLM\SOFTWARE\JavaSoft\Java Development Kit"\1.8 /v JavaHome | findstr JavaHome') do (set JDK_HOME=%b )

C:\Users\fastm\Desktop>(set JDK_HOME=C:\Program Files\Java\jdk1.8.0_121 )

C:\Users\fastm\Desktop>echo exit   | "C:\Program Files\Java\jdk1.8.0_121\bin\jdb.exe" -connect com.sun.jdi.SocketAttach:hostname=localhost,port=23915
java.io.IOException: handshake failed - connection prematurally closed
        at com.sun.tools.jdi.SocketTransportService.handshake(SocketTransportService.java:136)
        at com.sun.tools.jdi.SocketTransportService.attach(SocketTransportService.java:232)
        at com.sun.tools.jdi.GenericAttachingConnector.attach(GenericAttachingConnector.java:116)
        at com.sun.tools.jdi.SocketAttachingConnector.attach(SocketAttachingConnector.java:90)
        at com.sun.tools.example.debug.tty.VMConnection.attachTarget(VMConnection.java:519)
        at com.sun.tools.example.debug.tty.VMConnection.open(VMConnection.java:328)
        at com.sun.tools.example.debug.tty.Env.init(Env.java:63)
        at com.sun.tools.example.debug.tty.TTY.main(TTY.java:1082)

Fatal error:
Unable to attach to target VM.

C:\Users\fastm\Desktop>pause

I have tried many different JDK versions 8, 11 and 17 to mention. No luck. Then I have looked into Java source code for this message. [https://docs.oracle.com/javase/8/docs/technotes/guides/jpda/jdwp-spec.html][1] The error comes from function void handshake(Socket s, long timeout) throws IOException

            try {
            n = s.getInputStream().read(b, received, hello.length-received);
        } catch (SocketTimeoutException x) {
            throw new IOException("handshake timeout");
        }
        if (n < 0) {
            s.close();
            throw new IOException("handshake failed - connection prematurally closed");
        }
        received += n;

Having analyzed this function it appears that the JDB application should receive a reply "JDWP-Handshake".

This is specified in the documentation: https://docs.oracle.com/javase/8/docs/technotes/guides/jpda/jdwp-spec.html

The handshake process has the following steps:

The debugger side sends 14 bytes to the VM side, consisting of the 14 ASCII characters of the string "JDWP-Handshake" in the beginning and reply with the same characters.

However it replied with nothing here.

This makes me think that "adb forward" with JDWP functionality may have a problem on my Windows 10. Tried adb forward tcp:7000 tcp:7000 and this works, but it's something else.

Does anyone know how to solve this problem?

fastman92
  • 351
  • 2
  • 9

1 Answers1

2

I have solved my problem. It was quite tricky.

It was neccessary to quit Android Studio.

Conclusion: Android Studio gets an exclusive access to the JDWP functionality.

fastman92
  • 351
  • 2
  • 9