Skip to content

Conversation

@lateralusX
Copy link
Member

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

@mdh1418
Copy link
Member

mdh1418 commented Oct 19, 2023

I built a local branch based on this PR and validated that dotnet-dsrouter android worked.


dotnet-dsrouter

./artifacts/bin/dotnet-dsrouter/Debug/net6.0/dotnet-dsrouter android -v debug

dbug: dotnet-dsrouter-82752[0]
      Successfully connected ipc stream.
dbug: dotnet-dsrouter-82752[0]
      New router instance successfully created.
dbug: dotnet-dsrouter-82752[0]
      Trying to create new router instance.
dbug: dotnet-dsrouter-82752[0]
      Waiting for a new TCP connection at endpoint "127.0.0.1:9001".
dbug: dotnet-dsrouter-82752[0]
      Waiting for new ipc connection at endpoint "/var/folders/5l/4tmwls794z557k3gnf65p84r0000gp/T/dotnet-diagnostic-dsrouter-82752-1697724826-socket".
dbug: dotnet-dsrouter-82752[0]
      Diposed stats: Back End->Front End 24 bytes, Front End->Back End 20 bytes.
dbug: dotnet-dsrouter-82752[0]
      Active instances: 1
dbug: dotnet-dsrouter-82752[0]
      Successfully connected TCP stream, runtime id=c3e86a19-64c2-469d-92a6-ef4ab3178efd, runtime pid=28409.
dbug: dotnet-dsrouter-82752[0]
      Diposed stats: Back End->Front End 1369094 bytes, Front End->Back End 119 bytes.
dbug: dotnet-dsrouter-82752[0]
      Active instances: 0
info: dotnet-dsrouter-82752[0]
      Broken TCP connection detected, aborting ipc connection.
dbug: dotnet-dsrouter-82752[0]
      Failed creating new router instance.
dbug: dotnet-dsrouter-82752[0]
      Trying to create new router instance.

Started an Android app with DIAGNOSTIC_PORTS=127.0.0.1:9000,suspend,connect


dotnet-trace

./artifacts/bin/dotnet-trace/Debug/net6.0/dotnet-trace ps
./artifacts/bin/dotnet-trace/Debug/net6.0/dotnet-trace collect -p 82752 --providers Microsoft-Windows-DotNETRuntime:0x1F000080018:5


Provider Name                           Keywords            Level               Enabled By
Microsoft-Windows-DotNETRuntime         0x000001F000080018  Verbose(5)          --providers

Process        : /Users/mihw/diagnostics/artifacts/bin/dotnet-dsrouter/Debug/net6.0/dotnet-dsrouter
Output File    : /Users/mihw/diagnostics/dotnet-dsrouter_20231019_102210.nettrace

[00:00:00:00]	Recording trace 755.232  (KB)
Press <Enter> or <Ctrl+C> to exit...

Trace completed.

Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appears to work for me, too:

D:\src\dotnet\diagnostics\src\Tools\dotnet-dsrouter [lateralusX/fix-android-port-reverse]> dotnet run -- android -v debug
WARNING: dotnet-dsrouter is a development tool not intended for production environments.

How to connect current dotnet-dsrouter pid=12968 with android device and diagnostics tooling.
Start an application on android device with ONE of the following environment variables set:
[Default Tracing]
DOTNET_DiagnosticPorts=127.0.0.1:9000,nosuspend,connect
[Startup Tracing]
DOTNET_DiagnosticPorts=127.0.0.1:9000,suspend,connect
Run diagnotic tool connecting application on android device through dotnet-dsrouter pid=12968:
dotnet-trace collect -p 12968
See https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-dsrouter for additional details and examples.

info: dotnet-dsrouter-12968[0]
      Starting dotnet-dsrouter using pid=12968
dbug: dotnet-dsrouter-12968[0]
      Using default IPC server path, dotnet-diagnostic-dsrouter-12968.
dbug: dotnet-dsrouter-12968[0]
      Attach to default dotnet-dsrouter IPC server using --process-id 12968 diagnostic tooling argument.
dbug: dotnet-dsrouter-12968[0]
      Executing D:\android-toolchain\sdk\platform-tools\adb reverse --list.
fail: dotnet-dsrouter-12968[0]
      stderr: * daemon not running; starting now at tcp:5037
      * daemon started successfully
dbug: dotnet-dsrouter-12968[0]
      Executing D:\android-toolchain\sdk\platform-tools\adb reverse tcp:9000 tcp:9001.
info: dotnet-dsrouter-12968[0]
      Starting IPC server (dotnet-diagnostic-dsrouter-12968) <--> TCP server (127.0.0.1:9001) router.

Then I can get a profile from a device:

> dotnet-trace collect -p 12968 --format speedscope
No profile or providers specified, defaulting to trace profile 'cpu-sampling'

Provider Name                           Keywords            Level               Enabled By
Microsoft-DotNETCore-SampleProfiler     0x0000F00000000000  Informational(4)    --profile
Microsoft-Windows-DotNETRuntime         0x00000014C14FCCBD  Informational(4)    --profile

Process        : D:\src\dotnet\diagnostics\artifacts\bin\dotnet-dsrouter\Debug\net6.0\dotnet-dsrouter.exe
Output File    : D:\src\dotnet-dsrouter.exe_20231019_102830.nettrace

[00:00:00:04]   Recording trace 2.7577   (MB)
Press <Enter> or <Ctrl+C> to exit...
Stopping the trace. This may take several minutes depending on the application being traced.

Trace completed.
Writing:        D:\src\dotnet-dsrouter.exe_20231019_102830.speedscope.json
Conversion complete

@lateralusX lateralusX marked this pull request as ready for review October 19, 2023 16:17
@lateralusX lateralusX requested a review from a team as a code owner October 19, 2023 16:17
@lateralusX lateralusX merged commit 12f8ef3 into dotnet:main Oct 19, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jan 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants