15

I’ve been trying to connect jdb to the android emulator for a little while, and have been met repeatedly with:

jdb -sourcepath ./src -attach localhost:8700

java.io.IOException: shmemBase_attach failed: The system cannot find the file specified
        at com.sun.tools.jdi.SharedMemoryTransportService.attach0(Native Method)
        at com.sun.tools.jdi.SharedMemoryTransportService.attach(SharedMemoryTransportService.java:90)
        at com.sun.tools.jdi.GenericAttachingConnector.attach(GenericAttachingConnector.java:98)
        at com.sun.tools.jdi.SharedMemoryAttachingConnector.attach(SharedMemoryAttachingConnector.java:45)
        at com.sun.tools.example.debug.tty.VMConnection.attachTarget(VMConnection.java:358)
        at com.sun.tools.example.debug.tty.VMConnection.open(VMConnection.java:168)
        at com.sun.tools.example.debug.tty.Env.init(Env.java:64)
        at com.sun.tools.example.debug.tty.TTY.main(TTY.java:1010)

Fatal error:
Unable to attach to target VM.

Not so great. What's the best way of getting round this? I'm running on Windows 7 64bit.

Tim Barrass
  • 4,813
  • 2
  • 29
  • 55
  • Can you connect via the debugger at all? [Check my answer over here](http://stackoverflow.com/questions/4199323/android-app-not-launching-on-emulator/4199575#4199575) – Mark Storer Nov 18 '10 at 22:21
  • Hm, looks like the entries in my hosts file are all commented out, saying: # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost – Tim Barrass Nov 18 '10 at 22:29
  • 4
    Found *an* answer. From some Googling it appears that jdb on Windows defaults to a shared memory connection with a remote VM (http://www.herongyang.com/jtool/jdb_3.html). Turns out we can specify a different connection type: > jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=8700 Set uncaught java.lang.Throwable Set deferred uncaught java.lang.Throwable Initializing jdb ... > That appears to connect, at least. – Tim Barrass Nov 18 '10 at 22:34
  • 2
    I still recommend aiming your localhost at 127.0.0.1. DNS shouldn't have to touch it... and your DNS just might return an IPv6 instead of an IPv4 address. You have no control over that. You can control your "hosts". – Mark Storer Nov 19 '10 at 19:12

4 Answers4

20

Currently this is working for me -- making a socket rather than a shared memory connection.

>jdb –sourcepath .\src -connect com.sun.jdi.SocketAttach:hostname=localhost,port=8700

Beforehand you need to do some setup -- for example, see this set of useful details on setting up a non-eclipse debugger. It includes a good tip for setting your initial breakpoint -- create or edit a jdb.ini file in your home directory, with content like:

stop at com.mine.of.package.some.AClassIn:14

and they'll get loaded and deferred until connection.

edit: forgot to reference Herong Yang's page.

Community
  • 1
  • 1
Tim Barrass
  • 4,813
  • 2
  • 29
  • 55
2

Try quitting Android Studio.

I had a similar problem on the Mac due to the ADB daemon already running. Once you quit any running daemons, you should see output similar to the following:

$ adb -d jdwp
28462
1939
^C
$ adb -d forward tcp:7777 jdwp:1939
$ jdb -attach localhost:7777 -sourcepath ./src
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
Initializing jdb ...
> 

See my other answer to a similar question for more details and how to start/stop the daemon.

Zack Morris
  • 4,727
  • 2
  • 55
  • 83
0

Answer #1: Map localhost in your hosts file, as I linked to earlier. Just to be sure.

Answer #2: If you're using shared memory, bit-size could easily become an issue. Make sure you're using the same word width everywhere.

Mark Storer
  • 15,672
  • 3
  • 42
  • 80
  • Thanks Mark -- I'm not explicitly using shared memory -- at least I think I'd need to find out what the android emulator is using, if I understand correctly. The fact I can connect with a socket connection suggests it's not. – Tim Barrass Nov 19 '10 at 21:58
0

In order to debug application follow this steps:

Open the application on the device.

Find the PID with jdwp (make sure that 'android:debuggable' is set to true in the manifest):

adb jdwp

Start JVM with the following parameters:

java -agentlib:jdwp=transport=dt_shmem,server=y,address=<port> <class>

Expected output for this command:

Listening for transport dt_shmem at address: <port>

Use jdb to attach the application:

jdb -attach <port>

If jdb successful attached we will see the jdb cli.

Example:

> adb jdwp
  12300
> java -agentlib:jdwp=transport=dt_shmem,server=y,address=8700 com.app.app
  Listening for transport dt_shmem at address: 8700
> jdb -attach 8700
  main[1]
ic205
  • 131
  • 1
  • 9
  • I don't understand how to start this command below "Start JVM with the following parameters". Where should I execute it? Can I do it via ADB? – westman379 Nov 13 '22 at 12:59