Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotnet-dsrouter android does not appear to work for Android devices #4337

Closed
jonathanpeppers opened this issue Oct 17, 2023 · 5 comments · Fixed by #4342
Closed

dotnet-dsrouter android does not appear to work for Android devices #4337

jonathanpeppers opened this issue Oct 17, 2023 · 5 comments · Fixed by #4342
Assignees
Labels
bug Something isn't working ds-router
Milestone

Comments

@jonathanpeppers
Copy link
Member

Description

If I do:

> dotnet-dsrouter android --verbose debug
WARNING: dotnet-dsrouter is a development tool not intended for production environments.

Start an application on android emulator with one of the following environment variables set:
DOTNET_DiagnosticPorts=127.0.0.1:9000,nosuspend,connect
DOTNET_DiagnosticPorts=127.0.0.1:9000,suspend,connect

info: dotnet-dsrouter[0]
      Starting dotnet-dsrouter using pid=31220
dbug: dotnet-dsrouter[0]
      Using default IPC server path, dotnet-diagnostic-dsrouter-31220.
dbug: dotnet-dsrouter[0]
      Attach to default dotnet-dsrouter IPC server using --process-id 31220 diagnostic tooling argument.
dbug: dotnet-dsrouter[0]
      Executing D:\android-toolchain\sdk\\platform-tools\adb reverse --list.
dbug: dotnet-dsrouter[0]
      Executing D:\android-toolchain\sdk\\platform-tools\adb reverse tcp:9000 tcp:9000.
info: dotnet-dsrouter[0]
      Starting IPC server (dotnet-diagnostic-dsrouter-31220) <--> TCP server (127.0.0.1:9000) router.
dbug: dotnet-dsrouter[0]
      Trying to create new router instance.
dbug: dotnet-dsrouter[0]
      Waiting for a new TCP connection at endpoint "127.0.0.1:9000".
dbug: dotnet-dsrouter[0]
      Waiting for new ipc connection at endpoint "dotnet-diagnostic-dsrouter-31220".
dbug: dotnet-dsrouter[0]

At this point, I can launch an Android application on the device, but it appears to never be able to connect. I never get a log message like:

dbug: dotnet-dsrouter[0]
      Successfully connected TCP stream, runtime id=0c733d99-d2c3-432d-a611-543df3a824c5, runtime pid=27293.

I had run adb shell setprop debug.mono.profile '127.0.0.1:9000,suspend,connect' as the log message mentions, but I believe the forwarded port is incorrect:

Executing D:\android-toolchain\sdk\\platform-tools\adb reverse tcp:9000 tcp:9000.

For the old method, I would normally do:

> adb reverse tcp:9000 tcp:9001
> adb shell setprop debug.mono.profile '127.0.0.1:9000,suspend'
> dotnet-dsrouter client-server -tcps 127.0.0.1:9001 -ipcc /tmp/maui-app --verbose debug
> dotnet-trace collect --diagnostic-port /tmp/maui-app

https://github.com/xamarin/xamarin-android/blob/main/Documentation/guides/tracing.md

I think the "old way" works because it uses two different ports 9000 and 9001.

The android-emu method also works fine, because it doesn't need adb reverse:

> dotnet-dsrouter android-emu
Start an application on android emulator with one of the following environment variables set:
DOTNET_DiagnosticPorts=10.0.2.2:9000,nosuspend,connect
DOTNET_DiagnosticPorts=10.0.2.2:9000,suspend,connect
> adb shell setprop debug.mono.profile '10.0.2.2:9000,suspend,connect'
> dotnet-trace ps
  3248  dotnet-dsrouter
> dotnet-trace collect -p 3248
...
[00:00:00:09]   Recording trace 3.2522   (MB)
Press <Enter> or <Ctrl+C> to exit...

Configuration

> dotnet --version
8.0.100-rc.2.23502.2
> dotnet tool list -g
Package Id                         Version                       Commands
--------------------------------------------------------------------------------------
dotnet-dsrouter                    7.0.447801                    dotnet-dsrouter
dotnet-gcdump                      7.0.447801                    dotnet-gcdump
dotnet-trace                       7.0.447801                    dotnet-trace

Regression?

No, this is a new command.

/cc @lateralusX

@jonathanpeppers jonathanpeppers added the bug Something isn't working label Oct 17, 2023
@tommcdon tommcdon added this to the 8.0.0 milestone Oct 17, 2023
@lateralusX
Copy link
Member

lateralusX commented Oct 17, 2023

OK, I don't have a physical Android device to test on, but I tried using the "android" argument against an emulator and in that case the port reverse worked as expected using the same port on emulator and host. I was under the impression that you actually could use the same port, and since it worked running with the same port using the "android" argument on emulator I simplified that configuration, but apparently it have issues.

@jonathanpeppers Fix is simple, maybe you could do local build and test it on a real Android device to make sure defaults works? Alternative is to simulate what the default argument would have done, so you could do like this:

adb reverse tcp:9000 tcp:9001
dotnet-dsrouter server-server -tcps 127.0.0.1:9001 --verbose debug

//Run the app using 127.0.0.1:9000,suspend,connect
//Run dotnet-trace with dsrouter pid.

So I guess we should follow the defaults you used in the past? device port 9000 and host port 9001 in case we do adb reverse or forward of ports.

In the case when doing manual config with the --port-forward argument (dsrouter runs adb forward/reverse) and user pass in host port as part of tcps or tcpc arguments then device port would be selected host port - 1 in order to be in sync with old defaults, so if host port would be something like 9232 and passed to dotnet-dsrouter then the device port for the port forwarding would end up as 9231.

@jonathanpeppers
Copy link
Member Author

These instructions appear to work on an Android device:

adb reverse tcp:9000 tcp:9001
dotnet-dsrouter server-server -tcps 127.0.0.1:9001 --verbose debug
adb shell setprop debug.mono.profile '127.0.0.1:9000,suspend,connect'
//Run dotnet-trace with dsrouter pid.

In the case that doesn't work, dotnet-dsrouter android, I enabled some more logging on the Android device with:

adb shell setprop debug.mono.log default,assembly,mono_log_level=debug,mono_log_mask=all

Then it seems like Mono is just trying to connect and can't:

10-17 11:13:45.249 25702 25719 D Mono    : ds_ipc_stream_factory_get_next_available_stream - SIG :: Poll attempt: 1, connection 0 signalled.
10-17 11:13:45.249 25702 25719 D Mono    : ds_ipc_stream_factory_get_next_available_stream - EXIT :: Poll attempt: 1, stream using handle 5.
10-17 11:13:45.250 25702 25719 D Mono    : ds_ipc_stream_factory_get_next_available_stream - ENTER
10-17 11:13:45.250 25702 25719 D Mono    : connect_port_get_ipc_poll_handle - ENTER.
10-17 11:13:45.250 25702 25719 D Mono    : connect_port_get_ipc_poll_handle - cache was empty, trying to reconnect!
10-17 11:13:45.252 25702 25719 D Mono    : connect_port_get_ipc_poll_handle - returned connection { client_socket = 5 }
10-17 11:13:45.253 25702 25719 D Mono    : connect_port_get_ipc_poll_handle - EXIT.
10-17 11:13:45.253 25702 25719 D Mono    : ds_ipc_stream_factory_get_next_available_stream - Poll attempt: 1, timeout: -1ms.
10-17 11:13:45.253 25702 25719 D Mono    : 	CLIENT IpcPollHandle[0] = { client_socket = 5 }

Here is the full log: log.txt

jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this issue Oct 17, 2023
Context: dotnet/diagnostics#4081
Context: dotnet/diagnostics#4090
Context: dotnet/runtime#88634
Context: dotnet/diagnostics#4337

Using the latest `dotnet-trace` tooling:

    > dotnet tool list -g
    Package Id                         Version                       Commands
    --------------------------------------------------------------------------------------
    dotnet-dsrouter                    7.0.447801                    dotnet-dsrouter
    dotnet-gcdump                      7.0.447801                    dotnet-gcdump
    dotnet-trace                       7.0.447801                    dotnet-trace

There is a simplified way to profile Android apps.

For an Android emulator:

    > dotnet-dsrouter android-emu
    Start an application on android emulator with one of the following environment variables set:
    DOTNET_DiagnosticPorts=10.0.2.2:9000,nosuspend,connect
    DOTNET_DiagnosticPorts=10.0.2.2:9000,suspend,connect
    > adb shell setprop debug.mono.profile '10.0.2.2:9000,suspend,connect'
    > dotnet-trace ps
    3248  dotnet-dsrouter
    > dotnet-trace collect -p 3248
    ...
    [00:00:00:09]   Recording trace 3.2522   (MB)
    Press <Enter> or <Ctrl+C> to exit...

For an Android device you will eventually be able to do `dotnet-dsrouter
android`, except we found one issue.

Until dotnet/diagnostics#4337 is resolved, we can instead do:

    > adb reverse tcp:9000 tcp:9001
    > dotnet-dsrouter server-server -tcps 127.0.0.1:9001
    > adb shell setprop debug.mono.profile '127.0.0.1:9000,suspend,connect'
    > dotnet-trace ps
    > dotnet-trace collect -p 3248

We can also now use `dotnet-gcdump`! Instead of `dotnet-trace collect
-p`, you can simply do:

    > dotnet-gcdump collect -p 3248

In both cases these tools *know* that the process ID is a
`dotnet-dsrouter` process and to do the right thing.
jonathanpeppers added a commit to dotnet/android that referenced this issue Oct 18, 2023
Context: dotnet/diagnostics#4081
Context: dotnet/diagnostics#4090
Context: dotnet/runtime#88634
Context: dotnet/diagnostics#4337

Using the latest `dotnet-trace` tooling:

    > dotnet tool list -g
    Package Id                         Version                       Commands
    --------------------------------------------------------------------------------------
    dotnet-dsrouter                    7.0.447801                    dotnet-dsrouter
    dotnet-gcdump                      7.0.447801                    dotnet-gcdump
    dotnet-trace                       7.0.447801                    dotnet-trace

There is a simplified way to profile Android apps.

For an Android emulator:

    > dotnet-dsrouter android-emu
    Start an application on android emulator with one of the following environment variables set:
    DOTNET_DiagnosticPorts=10.0.2.2:9000,nosuspend,connect
    DOTNET_DiagnosticPorts=10.0.2.2:9000,suspend,connect
    > adb shell setprop debug.mono.profile '10.0.2.2:9000,suspend,connect'
    > dotnet-trace ps
    3248  dotnet-dsrouter
    > dotnet-trace collect -p 3248
    ...
    [00:00:00:09]   Recording trace 3.2522   (MB)
    Press <Enter> or <Ctrl+C> to exit...

For an Android device you will eventually be able to do `dotnet-dsrouter
android`, except we found one issue.

Until dotnet/diagnostics#4337 is resolved, we can instead do:

    > adb reverse tcp:9000 tcp:9001
    > dotnet-dsrouter server-server -tcps 127.0.0.1:9001
    > adb shell setprop debug.mono.profile '127.0.0.1:9000,suspend,connect'
    > dotnet-trace ps
    > dotnet-trace collect -p 3248

We can also now use `dotnet-gcdump`! Instead of `dotnet-trace collect
-p`, you can simply do:

    > dotnet-gcdump collect -p 3248

In both cases these tools *know* that the process ID is a
`dotnet-dsrouter` process and to do the right thing.
@lateralusX
Copy link
Member

@jonathanpeppers Do you have ability to do local build of #4342 and try out the fix to make sure it solves the issue when running against a physical Android device?

lateralusX added a commit that referenced this issue Oct 19, 2023
The use of automatic port forwarding on Android device used the same
port as local and remote, that should be possible, but turns out there
is some issue with `adb` around that configuration when running against
a physical Android device (works when using port forwarding/reverse
against Android emulator).

This PR change the port defaults and align to Xamarin Android
documentation as well as using different ports for local and remove when
using -`tcps` or -`tcpc` arguments together with `--forward-port
android`.

When running using the "Android" connect profile, `dotnet-dsrouter
android` we will default to 9001 as local/host and 9000 as remote/device
port.

When running with -`tcps` or -`tcpc` we will use passed port as
local/host port and then set the remote/device port to local/host port -
1 in call to `adb reverse|forward`.

Fixes #4337
@jeanplevesque
Copy link

Should the documentation be updated now that this issue is resolved?
https://github.com/xamarin/xamarin-android/blob/main/Documentation/guides/tracing.md#for-android-devices

Eventually, we will be able to simply do dotnet-dsrouter android when #4337 is resolved. adb reverse tcp:9000 tcp:9001 is also currently required as mentioned above.

@jonathanpeppers
Copy link
Member Author

jonathanpeppers commented Nov 2, 2023

@jeanplevesque send a PR! I planned to update when I got a chance. Thanks.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working ds-router
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants