chrome-devtools-app icon indicating copy to clipboard operation
chrome-devtools-app copied to clipboard

Detect chrome remote debug targets over ADB like chrome://inspect

Open auchenberg opened this issue 10 years ago • 9 comments

By browsing the Stetho source code, it seems like chrome://inspect is using Unix sockets to discover remote debug endpoints on the network.

At least Stetho opening a socket on stetho_PROCESSNAME_devtools_remote, which seem to be the convention used by chrome://inspect, so it should be possible to emulate this behavior by simply probing on sockets with the suffix _devtools_remote.

Link: https://github.com/facebook/stetho/blob/master/stetho/src/main/java/com/facebook/stetho/server/LocalSocketHttpServer.java

auchenberg avatar Feb 21 '15 08:02 auchenberg

I had a quick look at all the open sockets on my machine using lsof. Was not able to find anything that looked like it would help.

Did a bit of digging into the chrome://inspect page and that page is being updated from c++ directly (not over the network). This is the function being called https://code.google.com/p/chromium/codesearch#chromium/src/chrome/browser/resources/inspect/inspect.js&q=PopulateTargets&sq=package:chromium&l=100&type=cs

It looks like it is being called from here https://code.google.com/p/chromium/codesearch#chromium/src/chrome/browser/ui/webui/inspect_ui.cc&q=PopulateTargets&sq=package:chromium&l=514&type=cs

I tried to trace that back to where it is being called. Looks like there is some sort of internal notification centre which it is subscribing to for updates https://code.google.com/p/chromium/codesearch#chromium/src/chrome/browser/devtools/devtools_targets_ui.cc&sq=package:chromium&rcl=1424438238&l=195&type=cs

My c++ foo is very weak but hopefully this info might help

gingermusketeer avatar Feb 21 '15 09:02 gingermusketeer

For Android it looks like it binds to the port cast_shell_devtools_remote https://code.google.com/p/chromium/codesearch#chromium/src/chrome/browser/android/dev_tools_server.cc&q=_devtools_remote&sq=package:chromium&dr=C&l=65

I still haven't found the discovery.

auchenberg avatar Feb 21 '15 17:02 auchenberg

Here's the discovery and mapping of sockets to processes: https://code.google.com/p/chromium/codesearch#chromium/src/chrome/browser/devtools/device/android_device_info_query.cc&q=_devtools_remote&sq=package:chromium&dr=C&l=126

auchenberg avatar Feb 21 '15 18:02 auchenberg

Debugged this further. We need to use ADB in order to connect to the device, then run a port query, and then use ADB to setup a forward:

Steps how to find remote debug devices using ADB:

adb devices
<choose device>
adb -s <device> shell “cat /proc/net/unix |grep devtools_remote”
adb -s <device> forward tcp:9222 local abstract:<service name>
http://localhost:9222/json 
Bam!

auchenberg avatar Mar 11 '15 11:03 auchenberg

Got a working prototype here https://github.com/auchenberg/chrome-devtools-app/tree/adb-devices

auchenberg avatar Mar 19 '15 17:03 auchenberg

Nice!!

gingermusketeer avatar Mar 20 '15 08:03 gingermusketeer

For the curious of heart:

Here's the diff off of master https://github.com/auchenberg/chrome-devtools-app/compare/adb-devices

jasonLaster avatar Mar 26 '15 19:03 jasonLaster

Yeah, I wrote a little node module to abstract the ADB communication away, https://github.com/auchenberg/adb-devtools-devices

auchenberg avatar Mar 29 '15 09:03 auchenberg

Just to make things crystal clear:

  1. If you just have one device connected, you don't need the -s option, therefore adb <cmd> is a useful short

  2. To get the service name `adb shell "cat /proc/net/unix | grep devtools_remote", which will give you something like this output:

00000000: 00000002 00000000 00010000 0001 01 423897 @webview_devtools_remote_18279
  1. Now we need to make a port forwarding between the open socket on the android device and your local machine: adb forward tcp:9222 localabstract:webview_devtools_remote_18279 (assuming that webview_devtools_remote_18279 was returned by the command on step 2.

  2. To make sure it worked, just open http://localhost:9222 in your browser, and you should see something like this

screenshot 2018-08-28 10 32 19

  1. Now you can curl the JSON endpoint to get the so much desired webSocketDebuggerUrl: curl localhost:9222/json or curl localhost:9222/json/version (varies depending on the Chrome/Chromium version)

yuriteixeira avatar Aug 28 '18 08:08 yuriteixeira