This is an archive of the discontinued LLVM Phabricator instance.

[LLDB][Android] Fix Android serial number handling
AbandonedPublic

Authored by splhack on Nov 16 2022, 4:36 PM.

Details

Reviewers
clayborg
Summary

First, recap: what is the Android serial number.
In Android ADB protocal, the serial number following the host-serial: is URL-safe string and it could be a host:port string.

https://cs.android.com/android/platform/superproject/+/763ab7bb17e9c63b238222085e1e69552d978367:packages/modules/adb/sockets.cpp;l=825?q=host-serial:

if (android::base::ConsumePrefix(&service, "host-serial:")) {
    // serial number should follow "host:" and could be a host:port string.

Second, LLDB is not able to connect it to Android device when multiple devices are connected to the host.

(lldb) platform select remote-android
(lldb) platform connect unix-abstract-connect:///data/local/tmp/debug.sock
error: Expected a single connected device, got instead 2 - try setting 'ANDROID_SERIAL'

However setting ANDROID_SERIAL environment variable is not always a viable option.
For instance, VS Code + lldb-server and debugging multiple Android devices at the same time.

The potential solution is to use platform connect URL.

(lldb) platform select remote-android
(lldb) platform connect unix-abstract-connect://emulator-5554/data/local/tmp/debug.sock

This works for USB-connected devices and emulators.
But it does not work for TCP/IP-connected devices.

(lldb) platform select remote-android
(lldb) platform connect unix-abstract-connect://127.0.0.1:5556/data/local/tmp/debug.sock

The reason is that the current code only uses the hostname part of the URL.

https://github.com/llvm/llvm-project/blob/ed9638c44bc0c95314694fb878b21006e6c87510/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp#L154-L155

So m_device_id will be 127.0.0.1. But this should be 127.0.0.1:5556
Also localhost should not be skipped when the port number is available. m_device_id should be localhost:5556

(lldb) platform connect unix-abstract-connect://localhost:5556/data/local/tmp/debug.sock

Third, the host could be IPv6, for instance [::1].

(lldb) platform connect unix-abstract-connect://[::1]:5556/data/local/tmp/debug.sock

This is a valid URL and serial number [::1]:5556.

The parsed_url does not look it has the information of IPv6 brackets. Thus, GetDeviceIDFromURL checks the raw URL again for the bracket.

Fourth, when a port number is available in the connect URL, MakeConnectURL function create a new TCP/IP forwarding with the port number. When adb devices shows a device with host:port style serial number, the port in the serial number is already connected or forwarded to the adb server. host-serial:host:port should work.

https://github.com/llvm/llvm-project/blob/ed9638c44bc0c95314694fb878b21006e6c87510/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp#L191-L192

https://github.com/llvm/llvm-project/blob/ed9638c44bc0c95314694fb878b21006e6c87510/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp#L42-L46

Diff Detail

Event Timeline

splhack created this revision.Nov 16 2022, 4:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 16 2022, 4:36 PM
Herald added a subscriber: danielkiss. · View Herald Transcript
splhack updated this revision to Diff 475962.Nov 16 2022, 4:51 PM
This comment was removed by splhack.
splhack published this revision for review.Nov 16 2022, 4:53 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 16 2022, 4:53 PM

Just wanted to let you know that the "platform connect" parameters are not fixed. Each platform can make their own arguments. The Android platform currently expects a URL that will get forwarded to an internal Connection class that has registered a connection URL prefix ("unix-abstract-connect" in this case). But it doesn't have to be this way.

If the "unix-abstract-connect://emulator-5554/data/local/tmp/debug.sock" is not actually just being passed to the ConnectionFileDescriptor::Connect(...) function in lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp then we might want just modify the "platform connect" in the android platform to accept extra options since each Platform plug-in could have any number of options needed to connect. So instead of adding the device ID to the URL we could use a --device option. Currently we have:

(lldb) platform connect unix-abstract-connect://emulator-5554/data/local/tmp/debug.sock

But this could be:

platform connect --device emulator-5554 unix-abstract-connect://data/local/tmp/debug.sock

Same kind of thing for the TCP address. Not sure if we would specify this with "--device 127.0.0.1:5556" or if we would add a "--tcp 127.0.0.1:5556" option?

So I just wanted to let you know that we don't have to fit all of the connection information into a single URL. I am also not sure if the current android platform code was already doing this kind of thing? The "unix-abstract-connect" is a url that should forward the contents directly to a connection plug-in where we can have connection subclasses register connection URL prefixes that any plug-in can use. So I am not sure if adding the device name to this URL ("unix-abstract-connect://data/local/tmp/debug.sock") is the right way to go.

Let me know what you think and if I have misunderstood anything of how things currently work versus how this patch is modifying things to work.

Either way works as long as we can pass the Android serial number to AdbClient.cpp.

This diff just followed the current code which is actually already working for USB-connected devices and emulator. So adding TCPIP-connected device support here makes sense.

m_device_id = parsed_url->hostname.str();

If platform connect will accept platform-specific flags, potentially it may want these.

  • --serial (Android serial number)
  • --local-gdb-port (D136465)
  • --local-port (D136465)

@clayborg https://reviews.llvm.org/D139332 is the platform Android options version.

I'm happy with either of this connect url version https://reviews.llvm.org/D138164 or platform options version https://reviews.llvm.org/D139332.

splhack abandoned this revision.Jun 13 2023, 4:29 PM