Skip to content

Commit 36e9923

Browse files
[xabt] Compute $DOTNET_DiagnosticPorts using MSBuild properties (#10351)
If you run `dotnet-trace collect --dsrouter android`, it says: 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 Setting `$DOTNET_DiagnosticPorts` is non-trivial, so as a step to simplify this, we are adding multiple MSBuild properties to configure this value. This would allow the log message to say: Build and run an Android application such as: [Default Tracing] dotnet build -t:Run -c Release -p:DiagnosticAddress=127.0.0.1 -p:DiagnosticPort=9000 -p:DiagnosticSuspend=false -p:DiagnosticListenMode=connect [Startup Tracing] dotnet build -t:Run -c Release -p:DiagnosticAddress=127.0.0.1 -p:DiagnosticPort=9000 -p:DiagnosticSuspend=true -p:DiagnosticListenMode=connect You could also set `$(DiagnosticConfiguration)`, but you would need to escape the `,` character with `%2c`. Setting any of the new properties also implicitly means that `$(AndroidEnableProfiler)` is set to `true`.
1 parent a0ee973 commit 36e9923

File tree

5 files changed

+101
-8
lines changed

5 files changed

+101
-8
lines changed

Documentation/docs-mobile/building-apps/build-properties.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,67 @@ If `DebugType` is not set or is the empty string, then the
15291529
`DebugSymbols` property controls whether or not the Application is
15301530
debuggable.
15311531

1532+
## DiagnosticAddress
1533+
1534+
A value provided by `dotnet-dsrouter` such as `127.0.0.1`, the IP
1535+
address component of `$(DiagnosticConfiguration)` or `$DOTNET_DiagnosticPorts`.
1536+
1537+
Implicitly enables the Mono diagnostic component, meaning that
1538+
`$(EnableDiagnostics)`/`$(AndroidEnableProfiler)` is set to `true`.
1539+
1540+
Defaults to `127.0.0.1`.
1541+
1542+
## DiagnosticConfiguration
1543+
1544+
A value provided by `dotnet-dsrouter` for `$DOTNET_DiagnosticPorts` such as:
1545+
1546+
* `127.0.0.1:9000,suspend,connect`
1547+
* `127.0.0.1:9000,nosuspend,connect`
1548+
1549+
Note that the `,` character will need to be escaped with `%2c` if
1550+
passed in command-line to `dotnet build`:
1551+
1552+
```dotnetcli
1553+
dotnet build -c Release -p:DiagnosticConfiguration=127.0.0.1:9000%2csuspend%2cconnect
1554+
```
1555+
1556+
This will automatically set the `$DOTNET_DiagnosticPorts` environment
1557+
variable packaged inside the application.
1558+
1559+
Implicitly enables the Mono diagnostic component, meaning that
1560+
`$(EnableDiagnostics)`/`$(AndroidEnableProfiler)` is set to `true`.
1561+
1562+
## DiagnosticListenMode
1563+
1564+
A value provided by `dotnet-dsrouter` such as `connect`, the listening
1565+
mode component of `$(DiagnosticConfiguration)` or `$DOTNET_DiagnosticPorts`.
1566+
1567+
Implicitly enables the Mono diagnostic component, meaning that
1568+
`$(EnableDiagnostics)`/`$(AndroidEnableProfiler)` is set to `true`.
1569+
1570+
Defaults to `connect`.
1571+
1572+
## DiagnosticPort
1573+
1574+
A value provided by `dotnet-dsrouter` such as `9000`, the port
1575+
component of `$(DiagnosticConfiguration)` or `$DOTNET_DiagnosticPorts`.
1576+
1577+
Implicitly enables the Mono diagnostic component, meaning that
1578+
`$(EnableDiagnostics)`/`$(AndroidEnableProfiler)` is set to `true`.
1579+
1580+
Defaults to `9000`.
1581+
1582+
## DiagnosticSuspend
1583+
1584+
A boolean value provided by `dotnet-dsrouter` such as `true/suspend`
1585+
or `false/nosuspend`, a component of `$(DiagnosticConfiguration)`
1586+
or `$DOTNET_DiagnosticPorts`.
1587+
1588+
Implicitly enables the Mono diagnostic component, meaning that
1589+
`$(EnableDiagnostics)`/`$(AndroidEnableProfiler)` is set to `true`.
1590+
1591+
Defaults to `false`.
1592+
15321593
## EmbedAssembliesIntoApk
15331594

15341595
A boolean property that

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@
4848
<AndroidEnableRestrictToAttributes Condition=" '$(AndroidEnableRestrictToAttributes)' == '' ">obsolete</AndroidEnableRestrictToAttributes>
4949

5050
<!-- Mono components -->
51-
<AndroidEnableProfiler Condition=" '$(AndroidEnableProfiler)' == ''">$(EnableDiagnostics)</AndroidEnableProfiler>
52-
<AndroidEnableProfiler Condition=" '$(AndroidEnableProfiler)' == ''">false</AndroidEnableProfiler>
51+
<AndroidEnableProfiler Condition=" '$(AndroidEnableProfiler)' == '' ">$(EnableDiagnostics)</AndroidEnableProfiler>
52+
<AndroidEnableProfiler Condition=" '$(AndroidEnableProfiler)' == '' and ('$(DiagnosticConfiguration)' != '' or '$(DiagnosticAddress)' != '' or '$(DiagnosticPort)' != '' or '$(DiagnosticSuspend)' != '' or '$(DiagnosticListenMode)' != '') ">true</AndroidEnableProfiler>
53+
<AndroidEnableProfiler Condition=" '$(AndroidEnableProfiler)' == '' ">false</AndroidEnableProfiler>
5354

5455
<!--
5556
Android package (apt/aab) alignment, expressed as the page size in kilobytes. Two values are supported: 4 and 16.
@@ -60,6 +61,16 @@
6061
<AndroidZipAlignment Condition=" '$(AndroidZipAlignment)' == '' ">16</AndroidZipAlignment>
6162
</PropertyGroup>
6263

64+
<PropertyGroup Condition=" '$(AndroidEnableProfiler)' == 'true' and '$(DiagnosticConfiguration)' == '' ">
65+
<DiagnosticAddress Condition=" '$(DiagnosticAddress)' == '' ">127.0.0.1</DiagnosticAddress>
66+
<DiagnosticPort Condition=" '$(DiagnosticPort)' == '' ">9000</DiagnosticPort>
67+
<DiagnosticSuspend Condition=" '$(DiagnosticSuspend)' == '' ">false</DiagnosticSuspend>
68+
<DiagnosticListenMode Condition=" '$(DiagnosticListenMode)' == '' ">connect</DiagnosticListenMode>
69+
<DiagnosticConfiguration>$(DiagnosticAddress):$(DiagnosticPort),$(DiagnosticListenMode)</DiagnosticConfiguration>
70+
<DiagnosticConfiguration Condition=" '$(DiagnosticSuspend)' == 'true' ">$(DiagnosticConfiguration),suspend</DiagnosticConfiguration>
71+
<DiagnosticConfiguration Condition=" '$(DiagnosticSuspend)' != 'true' ">$(DiagnosticConfiguration),nosuspend</DiagnosticConfiguration>
72+
</PropertyGroup>
73+
6374
<!-- User-facing configuration-specific defaults -->
6475
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
6576
<AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' ">None</AndroidLinkMode>

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,19 +1289,29 @@ public void AbiNameInIntermediateOutputPath ()
12891289
}
12901290

12911291
[Test]
1292-
public void PackageNamingPolicy ([Values ("LowercaseMD5", "LowercaseCrc64")] string packageNamingPolicy)
1292+
[TestCase (true, "LowercaseMD5", "")]
1293+
[TestCase (true, "LowercaseCrc64", "")]
1294+
[TestCase (false, "", "127.0.0.1:9000,suspend,connect")]
1295+
public void EnvironmentVariables (bool useInterpreter, string packageNamingPolicy, string diagnosticConfiguration)
12931296
{
12941297
var proj = new XamarinAndroidApplicationProject ();
1295-
proj.SetProperty ("UseInterpreter", "true");
1296-
proj.SetProperty ("AndroidPackageNamingPolicy", packageNamingPolicy);
1298+
proj.SetProperty ("UseInterpreter", useInterpreter.ToString ());
1299+
if (!string.IsNullOrEmpty (packageNamingPolicy))
1300+
proj.SetProperty ("AndroidPackageNamingPolicy", packageNamingPolicy);
1301+
if (!string.IsNullOrEmpty (diagnosticConfiguration))
1302+
proj.SetProperty ("DiagnosticConfiguration", diagnosticConfiguration);
12971303
proj.SetAndroidSupportedAbis ("armeabi-v7a", "x86");
12981304
using (var b = CreateApkBuilder ()) {
12991305
Assert.IsTrue (b.Build (proj), "build should have succeeded.");
13001306
var environment = b.Output.GetIntermediaryPath (Path.Combine ("__environment__.txt"));
13011307
FileAssert.Exists (environment);
1302-
var values = new List<string> ();
1303-
values.Add ("mono.enable_assembly_preload=0");
1304-
values.Add ("DOTNET_MODIFIABLE_ASSEMBLIES=Debug");
1308+
var values = new List<string> {
1309+
"mono.enable_assembly_preload=0",
1310+
};
1311+
if (useInterpreter)
1312+
values.Add ("DOTNET_MODIFIABLE_ASSEMBLIES=Debug");
1313+
if (!string.IsNullOrEmpty (diagnosticConfiguration))
1314+
values.Add ($"DOTNET_DiagnosticPorts={diagnosticConfiguration}");
13051315
Assert.AreEqual (string.Join (Environment.NewLine, values), File.ReadAllText (environment).Trim ());
13061316
}
13071317
}

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,7 @@ because xbuild doesn't support framework reference assemblies.
17651765
<ItemGroup>
17661766
<_GeneratedAndroidEnvironment Include="mono.enable_assembly_preload=0" Condition=" '$(AndroidEnablePreloadAssemblies)' != 'True' " />
17671767
<_GeneratedAndroidEnvironment Include="DOTNET_MODIFIABLE_ASSEMBLIES=Debug" Condition=" '$(AndroidIncludeDebugSymbols)' == 'true' and '$(AndroidUseInterpreter)' == 'true' " />
1768+
<_GeneratedAndroidEnvironment Include="DOTNET_DiagnosticPorts=$(DiagnosticConfiguration)" Condition=" '$(DiagnosticConfiguration)' != '' " />
17681769
</ItemGroup>
17691770
<WriteLinesToFile
17701771
File="$(IntermediateOutputPath)__environment__.txt"

tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,12 +1054,17 @@ public void FastDeployEnvironmentFiles (bool isRelease, bool embedAssembliesInto
10541054
}
10551055
}
10561056
};
1057+
proj.SetProperty ("DiagnosticAddress", "127.0.0.1");
1058+
proj.SetProperty ("DiagnosticPort", "9000");
1059+
proj.SetProperty ("DiagnosticSuspend", "false");
1060+
proj.SetProperty ("DiagnosticListenMode", "connect");
10571061
proj.MainActivity = proj.DefaultMainActivity.Replace ("//${AFTER_ONCREATE}", @"
10581062
Console.WriteLine (""Foo="" + Environment.GetEnvironmentVariable(""Foo""));
10591063
Console.WriteLine (""Bar34="" + Environment.GetEnvironmentVariable(""Bar34""));
10601064
Console.WriteLine (""Empty="" + Environment.GetEnvironmentVariable(""Empty""));
10611065
Console.WriteLine (""MONO_GC_PARAMS="" + Environment.GetEnvironmentVariable(""MONO_GC_PARAMS""));
10621066
Console.WriteLine (""DOTNET_MODIFIABLE_ASSEMBLIES="" + Environment.GetEnvironmentVariable(""DOTNET_MODIFIABLE_ASSEMBLIES""));
1067+
Console.WriteLine (""DOTNET_DiagnosticPorts="" + Environment.GetEnvironmentVariable(""DOTNET_DiagnosticPorts""));
10631068
");
10641069
var builder = CreateApkBuilder ();
10651070
Assert.IsTrue (builder.Build (proj), "`dotnet build` should succeed");
@@ -1093,6 +1098,11 @@ public void FastDeployEnvironmentFiles (bool isRelease, bool embedAssembliesInto
10931098
logcatOutput,
10941099
"The Environment variable \"MONO_GC_PARAMS\" was not set to expected value \"bridge-implementation=new\"."
10951100
);
1101+
StringAssert.Contains (
1102+
"DOTNET_DiagnosticPorts=127.0.0.1:9000,connect,nosuspend",
1103+
logcatOutput,
1104+
"The Environment variable \"DOTNET_DiagnosticPorts\" was not set to expected value \"127.0.0.1:9000,connect,nosuspend\"."
1105+
);
10961106
// NOTE: set when $(UseInterpreter) is true, default for Debug mode
10971107
if (!isRelease) {
10981108
StringAssert.Contains (

0 commit comments

Comments
 (0)