diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs b/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs index 5738eb07d2f0d1..4a7f6b9017e461 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs @@ -331,8 +331,6 @@ internal enum MonoErrorCodes internal static class MonoConstants { - public const string RUNTIME_IS_READY = "mono_wasm_runtime_ready"; - public const string RUNTIME_IS_READY_ID = "fe00e07a-5519-4dfe-b35a-f867dbaf2e28"; public const string EVENT_RAISED = "mono_wasm_debug_event_raised:aef14bca-5519-4dfe-b35a-f867abc123ae"; } diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FireforDebuggerProxy.cs b/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxDebuggerProxy.cs similarity index 100% rename from src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FireforDebuggerProxy.cs rename to src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxDebuggerProxy.cs diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxExecutionContext.cs b/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxExecutionContext.cs index 253b7316779537..095970f4c799cb 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxExecutionContext.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxExecutionContext.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Threading; +using System.Threading.Tasks; #nullable enable @@ -12,7 +13,7 @@ internal sealed class FirefoxExecutionContext : ExecutionContext public string? ActorName { get; set; } public string? ThreadName { get; set; } public string? GlobalName { get; set; } - public Result LastDebuggerAgentBufferReceived { get; set; } + public Task? LastDebuggerAgentBufferReceived { get; set; } public FirefoxExecutionContext(MonoSDBHelper sdbAgent, int id, string actorName) : base(sdbAgent, id, actorName, PauseOnExceptionsKind.Unset) { diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxMonoProxy.cs b/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxMonoProxy.cs index 6d721631b8f334..3d811d85f0d6e7 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxMonoProxy.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxMonoProxy.cs @@ -234,24 +234,6 @@ protected override Task SendEventInternal(SessionId sessionId, string method, JO protected override async Task AcceptEvent(SessionId sessionId, JObject args, CancellationToken token) { - if (args["messages"] != null) - { - // FIXME: duplicate, and will miss any non-runtime-ready messages being forwarded - var messages = args["messages"].Value(); - foreach (var message in messages) - { - var messageArgs = message["message"]?["arguments"]?.Value(); - if (messageArgs != null && messageArgs.Count == 2) - { - if (messageArgs[0].Value() == MonoConstants.RUNTIME_IS_READY && messageArgs[1].Value() == MonoConstants.RUNTIME_IS_READY_ID) - { - ResetCmdId(); - await RuntimeReady(sessionId, token); - } - } - } - return true; - } if (args["frame"] != null && args["type"] == null) { OnDefaultContextUpdate(sessionId, new FirefoxExecutionContext(new MonoSDBHelper (this, logger, sessionId), 0, args["frame"]["consoleActor"].Value())); @@ -262,7 +244,8 @@ protected override async Task AcceptEvent(SessionId sessionId, JObject arg return true; if (args["type"] == null) - return await Task.FromResult(false); + return false; + switch (args["type"].Value()) { case "paused": @@ -299,25 +282,24 @@ protected override async Task AcceptEvent(SessionId sessionId, JObject arg } if (message["resourceType"].Value() != "console-message") continue; - var messageArgs = message["message"]?["arguments"]?.Value(); var ctx = GetContextFixefox(sessionId); ctx.GlobalName = args["from"].Value(); - if (messageArgs != null && messageArgs.Count == 2) - { - if (messageArgs[0].Value() == MonoConstants.RUNTIME_IS_READY && messageArgs[1].Value() == MonoConstants.RUNTIME_IS_READY_ID) - { - ResetCmdId(); - await Task.WhenAll( - ForwardMessageToIde(args, token), - RuntimeReady(sessionId, token)); - } - } } break; } case "target-available-form": { OnDefaultContextUpdate(sessionId, new FirefoxExecutionContext(new MonoSDBHelper (this, logger, sessionId), 0, args["target"]["consoleActor"].Value())); + var ctx = GetContextFixefox(sessionId); + ctx.GlobalName = args["target"]["actor"].Value(); + ctx.ThreadName = args["target"]["threadActor"].Value(); + ResetCmdId(); + if (await IsRuntimeAlreadyReadyAlready(sessionId, token)) + { + await ForwardMessageToIde(args, token); + await RuntimeReady(sessionId, token); + return true; + } break; } } @@ -364,6 +346,8 @@ protected override async Task AcceptCommand(MessageId sessionId, JObject a { var ctx = GetContextFixefox(sessionId); ctx.ThreadName = args["to"].Value(); + if (await IsRuntimeAlreadyReadyAlready(sessionId, token)) + await RuntimeReady(sessionId, token); break; } case "source": @@ -691,25 +675,33 @@ protected override async Task AcceptCommand(MessageId sessionId, JObject a await SendEvent(sessionId, "", ret.Value, token); return true; } + case "DotnetDebugger.runTests": + { + await RuntimeReady(sessionId, token); + return true; + } default: return false; } return false; } - internal override void SaveLastDebuggerAgentBufferReceivedToContext(SessionId sessionId, Result res) + internal override void SaveLastDebuggerAgentBufferReceivedToContext(SessionId sessionId, Task debuggerAgentBufferTask) { var context = GetContextFixefox(sessionId); - context.LastDebuggerAgentBufferReceived = res; + if (context.LastDebuggerAgentBufferReceived != null) + logger.LogTrace($"Trying to reset debugger agent buffer before use it."); + + context.LastDebuggerAgentBufferReceived = debuggerAgentBufferTask; } private async Task SendPauseToBrowser(SessionId sessionId, JObject args, CancellationToken token) { var context = GetContextFixefox(sessionId); - Result res = context.LastDebuggerAgentBufferReceived; + Result res = await context.LastDebuggerAgentBufferReceived; if (!res.IsOk) return false; - + context.LastDebuggerAgentBufferReceived = null; byte[] newBytes = Convert.FromBase64String(res.Value?["result"]?["value"]?["value"]?.Value()); using var retDebuggerCmdReader = new MonoBinaryReader(newBytes); retDebuggerCmdReader.ReadBytes(11); diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs b/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs index 0ed9f9ca99d150..d0f68b017240a2 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs @@ -20,7 +20,7 @@ internal class MonoProxy : DevToolsProxy { private IList urlSymbolServerList; private HashSet sessions = new HashSet(); - private static readonly string[] s_executionContextIndependentCDPCommandNames = { "DotnetDebugger.setDebuggerProperty" }; + private static readonly string[] s_executionContextIndependentCDPCommandNames = { "DotnetDebugger.setDebuggerProperty", "DotnetDebugger.runTests" }; protected Dictionary contexts = new Dictionary(); public static HttpClient HttpClient => new HttpClient(); @@ -94,7 +94,7 @@ protected override async Task AcceptEvent(SessionId sessionId, JObject par case "Runtime.consoleAPICalled": { // Don't process events from sessions we aren't tracking - if (!contexts.TryGetValue(sessionId, out ExecutionContext context)) + if (!contexts.ContainsKey(sessionId)) return false; string type = args["type"]?.ToString(); if (type == "debug") @@ -104,28 +104,7 @@ protected override async Task AcceptEvent(SessionId sessionId, JObject par break; int aCount = a.Count(); - if (aCount >= 2 && - a[0]?["value"]?.ToString() == MonoConstants.RUNTIME_IS_READY && - a[1]?["value"]?.ToString() == MonoConstants.RUNTIME_IS_READY_ID) - { - if (aCount > 2) - { - try - { - // The optional 3rd argument is the stringified assembly - // list so that we don't have to make more round trips - string loaded = a[2]?["value"]?.ToString(); - if (loaded != null) - context.LoadedFiles = JToken.Parse(loaded).ToObject(); - } - catch (InvalidCastException ice) - { - Log("verbose", ice.ToString()); - } - } - await RuntimeReady(sessionId, token); - } - else if (aCount > 1 && a[0]?["value"]?.ToString() == MonoConstants.EVENT_RAISED) + if (aCount > 1 && a[0]?["value"]?.ToString() == MonoConstants.EVENT_RAISED) { if (a.Type != JTokenType.Array) { @@ -587,6 +566,13 @@ protected override async Task AcceptCommand(MessageId id, JObject parms, C } return true; } + case "DotnetDebugger.runTests": + { + SendResponse(id, Result.OkFromObject(new { }), token); + if (await IsRuntimeAlreadyReadyAlready(id, token)) + await RuntimeReady(id, token); + return true; + } } // for Dotnetdebugger.* messages, treat them as handled, thus not passing them on to the browser return method.StartsWith("DotnetDebugger.", StringComparison.OrdinalIgnoreCase); @@ -1060,14 +1046,15 @@ protected virtual async Task SendCallStack(SessionId sessionId, ExecutionC return true; } - internal virtual void SaveLastDebuggerAgentBufferReceivedToContext(SessionId sessionId, Result res) + internal virtual void SaveLastDebuggerAgentBufferReceivedToContext(SessionId sessionId, Task debuggerAgentBufferTask) { } internal async Task OnReceiveDebuggerAgentEvent(SessionId sessionId, JObject args, CancellationToken token) { - Result res = await SendMonoCommand(sessionId, MonoCommands.GetDebuggerAgentBufferReceived(RuntimeId), token); - SaveLastDebuggerAgentBufferReceivedToContext(sessionId, res); + var debuggerAgentBufferTask = SendMonoCommand(sessionId, MonoCommands.GetDebuggerAgentBufferReceived(RuntimeId), token); + SaveLastDebuggerAgentBufferReceivedToContext(sessionId, debuggerAgentBufferTask); + var res = await debuggerAgentBufferTask; if (!res.IsOk) return false; diff --git a/src/mono/wasm/debugger/DebuggerTestSuite/FirefoxInspectorClient.cs b/src/mono/wasm/debugger/DebuggerTestSuite/FirefoxInspectorClient.cs index f536e066fa3b2e..d0eff4c5e2c12a 100644 --- a/src/mono/wasm/debugger/DebuggerTestSuite/FirefoxInspectorClient.cs +++ b/src/mono/wasm/debugger/DebuggerTestSuite/FirefoxInspectorClient.cs @@ -84,6 +84,8 @@ public async Task ProcessTabInfo(Result command, CancellationToken token) res = await SendCommand("watchResources", JObject.FromObject(new { type = "watchResources", resourceTypes = new JArray("console-message"), to = watcherId}), token); res = await SendCommand("watchTargets", JObject.FromObject(new { type = "watchTargets", targetType = "frame", to = watcherId}), token); UpdateTarget(res.Value?["result"]?["value"]?["target"] as JObject); + if (ThreadActorId == null) + return false; res = await SendCommand("attach", JObject.FromObject(new { type = "attach", diff --git a/src/mono/wasm/debugger/DebuggerTestSuite/Inspector.cs b/src/mono/wasm/debugger/DebuggerTestSuite/Inspector.cs index d045d652885b13..62462481685a6a 100644 --- a/src/mono/wasm/debugger/DebuggerTestSuite/Inspector.cs +++ b/src/mono/wasm/debugger/DebuggerTestSuite/Inspector.cs @@ -224,6 +224,8 @@ async Task OnMessage(string method, JObject args, CancellationToken token) case "trace": _logger.LogTrace(line); break; default: _logger.LogInformation(line); break; } + if (line == "console.debug: #debugger-app-ready#") + await Client.SendCommand("DotnetDebugger.runTests", JObject.FromObject(new { type = "DotnetDebugger.runTests", to = "root" }), token); if (!_gotAppReady && line == "console.debug: #debugger-app-ready#") { diff --git a/src/mono/wasm/debugger/tests/debugger-test/debugger-driver.html b/src/mono/wasm/debugger/tests/debugger-test/debugger-driver.html index 3cbda702d6ce77..3bdd429f7ab7da 100644 --- a/src/mono/wasm/debugger/tests/debugger-test/debugger-driver.html +++ b/src/mono/wasm/debugger/tests/debugger-test/debugger-driver.html @@ -44,7 +44,6 @@ function invoke_getters_js_test () { getters_js_test (); } - function invoke_add () { return App.int_add (10, 20); } @@ -91,6 +90,7 @@ function load_wasm_page_without_assets () { console.log("load_wasm_page_without_assets") window.location.replace("http://localhost:9400/wasm-page-without-assets.html"); + console.debug ("#debugger-app-ready#"); } function load_non_wasm_page_forcing_runtime_ready () { console.log("load_non_wasm_page_forcing_runtime_ready") diff --git a/src/mono/wasm/debugger/tests/debugger-test/wasm-page-without-assets.html b/src/mono/wasm/debugger/tests/debugger-test/wasm-page-without-assets.html index 9e62a550b22e46..49a836c8e88598 100644 --- a/src/mono/wasm/debugger/tests/debugger-test/wasm-page-without-assets.html +++ b/src/mono/wasm/debugger/tests/debugger-test/wasm-page-without-assets.html @@ -7,9 +7,11 @@ diff --git a/src/mono/wasm/runtime/debug.ts b/src/mono/wasm/runtime/debug.ts index 43e9ffb83d3e11..66b8cecaa14a60 100644 --- a/src/mono/wasm/runtime/debug.ts +++ b/src/mono/wasm/runtime/debug.ts @@ -27,9 +27,6 @@ export function mono_wasm_runtime_ready(): void { if ((globalThis).dotnetDebugger) // eslint-disable-next-line no-debugger debugger; - else - console.debug("mono_wasm_runtime_ready", "fe00e07a-5519-4dfe-b35a-f867dbaf2e28"); - } export function mono_wasm_fire_debugger_agent_message(): void {