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

[WIP] Previewer Debug #16032

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Avalonia.sln
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{A689DEF5-D50F-4975-8B72-124C9EB54066}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
src\Shared\DebuggerHelper.cs = src\Shared\DebuggerHelper.cs
src\Shared\IsExternalInit.cs = src\Shared\IsExternalInit.cs
src\Shared\ModuleInitializer.cs = src\Shared\ModuleInitializer.cs
src\Shared\NullableAttributes.cs = src\Shared\NullableAttributes.cs
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Build.Tasks/Avalonia.Build.Tasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<Compile Include="..\Avalonia.Base\Metadata\NullableAttributes.cs" Link="NullableAttributes.cs" />
<Compile Include="..\Avalonia.Base\Compatibility\TrimmingAttributes.cs" Link="TrimmingAttributes.cs" Visible="False" />
<Compile Include="..\Avalonia.Base\Utilities\SpanHelpers.cs" Link="Utilities\SpanHelpers.cs" />
<Compile Include="..\Shared\DebuggerHelper.cs" Link="DebuggerHelper.cs" />
<Compile Include="..\Shared\StringCompatibilityExtensions.cs" Link="Compatibility\StringCompatibilityExtensions.cs" />
<Compile Include="..\Shared\IsExternalInit.cs" Link="Compatibility\IsExternalInit.cs" />
<Compile Remove="../Markup/Avalonia.Markup.Xaml.Loader\xamlil.github\**\obj\**\*.cs" />
Expand Down
27 changes: 1 addition & 26 deletions src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,32 +111,7 @@ public CompileResult(bool success, bool writtenFile = false)
{
if (debuggerLaunch)
{
// According this https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.debugger.launch?view=net-6.0#remarks
// documentation, on not windows platform Debugger.Launch() always return true without running a debugger.
if (System.Diagnostics.Debugger.Launch())
{
// Set timeout at 1 minut.
var time = new System.Diagnostics.Stopwatch();
var timeout = TimeSpan.FromMinutes(1);
time.Start();

// wait for the debugger to be attacked or timeout.
while (!System.Diagnostics.Debugger.IsAttached && time.Elapsed < timeout)
{
engine.LogMessage($"[PID:{System.Diagnostics.Process.GetCurrentProcess().Id}] Wating attach debugger. Elapsed {time.Elapsed}...", MessageImportance.High);
System.Threading.Thread.Sleep(100);
}

time.Stop();
if (time.Elapsed >= timeout)
{
engine.LogMessage("Wating attach debugger timeout.", MessageImportance.Normal);
}
}
else
{
engine.LogMessage("Debugging cancelled.", MessageImportance.Normal);
}
Avalonia.Utilities.DebuggerHelper.Launch(m => engine.LogMessage(m, MessageImportance.High));
}

// Some transformers might need to parse "avares://" Uri.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<EmbeddedResource Include="Remote\HtmlTransport\webapp\build\**\*.gz" />
<EmbeddedResource Condition="'$(Configuration)' == 'Debug'" Remove="Remote\HtmlTransport\webapp\build\**\*.map.gz" />
<Compile Include="..\Shared\DebuggerHelper.cs" Link="DebuggerHelper.cs" />
<ProjectReference Include="..\Markup\Avalonia.Markup.Xaml\Avalonia.Markup.Xaml.csproj" />
<ProjectReference Include="..\Markup\Avalonia.Markup\Avalonia.Markup.csproj" />
<ProjectReference Include="..\Avalonia.Base\Avalonia.Base.csproj" />
Expand Down
18 changes: 14 additions & 4 deletions src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CommandLineArgs
public Uri HtmlMethodListenUri { get; set; }
public string Method { get; set; } = Methods.AvaloniaRemote;
public string SessionId { get; set; } = Guid.NewGuid().ToString();
public bool Debug { get; set; }
}

internal static class Methods
Expand Down Expand Up @@ -92,6 +93,11 @@ static CommandLineArgs ParseCommandLineArgs(string[] args)
next = a => rv.HtmlMethodListenUri = new Uri(a, UriKind.Absolute);
else if (arg == "--session-id")
next = a => rv.SessionId = a;
else if (arg == "--debug")
{
rv.Debug = true;
next = null;
}
else if (rv.AppPath == null)
rv.AppPath = arg;
else
Expand Down Expand Up @@ -169,6 +175,10 @@ class AppInitializer : IAppInitializer
public static void Main(string[] cmdline)
{
var args = ParseCommandLineArgs(cmdline);
if (args.Debug)
{
Avalonia.Utilities.DebuggerHelper.Launch(Log);
}
var transport = CreateTransport(args);
if (transport is ITransportWithEnforcedMethod enforcedMethod)
args.Method = enforcedMethod.PreviewerMethod;
Expand All @@ -178,15 +188,15 @@ public static void Main(string[] cmdline)
Design.IsDesignMode = true;
Log($"Obtaining AppBuilder instance from {entryPoint.DeclaringType!.FullName}");
var appBuilder = AppBuilder.Configure(entryPoint.DeclaringType);
var initializer =(IAppInitializer)Activator.CreateInstance(typeof(AppInitializer));
var initializer = (IAppInitializer)Activator.CreateInstance(typeof(AppInitializer));
transport = initializer.ConfigureApp(transport, args, appBuilder);
s_transport = transport;
transport.OnMessage += OnTransportMessage;
transport.OnException += (t, e) => Die(e.ToString());
transport.Start();
Log("Sending StartDesignerSessionMessage");
transport.Send(new StartDesignerSessionMessage {SessionId = args.SessionId});
transport.Send(new StartDesignerSessionMessage { SessionId = args.SessionId });

Dispatcher.UIThread.MainLoop(CancellationToken.None);
}

Expand Down Expand Up @@ -236,7 +246,7 @@ private static void RebuildPreFlight()
try
{
s_currentWindow = DesignWindowLoader.LoadDesignerWindow(xaml.Xaml, xaml.AssemblyPath, xaml.XamlFileProjectPath, s_lastRenderScaling);
s_transport.Send(new UpdateXamlResultMessage(){Handle = s_currentWindow.PlatformImpl?.Handle?.Handle.ToString()});
s_transport.Send(new UpdateXamlResultMessage() { Handle = s_currentWindow.PlatformImpl?.Handle?.Handle.ToString() });
}
catch (Exception e)
{
Expand Down
36 changes: 36 additions & 0 deletions src/Shared/DebuggerHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;

namespace Avalonia.Utilities;

internal sealed class DebuggerHelper
{
public static void Launch(Action<string> log)
{
// According this https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.debugger.launch?view=net-6.0#remarks
// documentation, on not windows platform Debugger.Launch() always return true without running a debugger.
if (System.Diagnostics.Debugger.Launch())
{
// Set timeout at 1 minut.
var time = new System.Diagnostics.Stopwatch();
var timeout = TimeSpan.FromMinutes(1);
time.Start();

// wait for the debugger to be attached or timeout.
while (!System.Diagnostics.Debugger.IsAttached && time.Elapsed < timeout)
{
log?.Invoke($"[PID:{System.Diagnostics.Process.GetCurrentProcess().Id}] Wating attach debugger. Elapsed {time.Elapsed}...");
System.Threading.Thread.Sleep(100);
}

time.Stop();
if (time.Elapsed >= timeout)
{
log?.Invoke("Wating attach debugger timeout.");
}
}
else
{
log?.Invoke("Debugging cancelled.");
}
}
}
Loading