Skip to content

Commit

Permalink
[wasm][debugger] Removing console.debug message helper to make debugg…
Browse files Browse the repository at this point in the history
…er work. (#72812)

* Removing console.debug message helper to make debugger work.

* Update src/mono/wasm/runtime/debug.ts

Co-authored-by: Larry Ewing <[email protected]>

* Addressing @radical comments offline.

* Fixing merge.

* Fix line change error.

* Totally removing the message.

* Remove unused comment

Co-authored-by: Larry Ewing <[email protected]>
Co-authored-by: Ilona Tomkowicz <[email protected]>
  • Loading branch information
3 people authored Jan 10, 2023
1 parent d4907cf commit 8a2b72e
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 70 deletions.
2 changes: 0 additions & 2 deletions src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,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";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<Result>? LastDebuggerAgentBufferReceived { get; set; }

public FirefoxExecutionContext(MonoSDBHelper sdbAgent, int id, string actorName) : base(sdbAgent, id, actorName, PauseOnExceptionsKind.Unset)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,24 +234,6 @@ protected override Task SendEventInternal(SessionId sessionId, string method, JO

protected override async Task<bool> 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<JArray>();
foreach (var message in messages)
{
var messageArgs = message["message"]?["arguments"]?.Value<JArray>();
if (messageArgs != null && messageArgs.Count == 2)
{
if (messageArgs[0].Value<string>() == MonoConstants.RUNTIME_IS_READY && messageArgs[1].Value<string>() == 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<string>()));
Expand All @@ -262,7 +244,8 @@ protected override async Task<bool> AcceptEvent(SessionId sessionId, JObject arg
return true;

if (args["type"] == null)
return await Task.FromResult(false);
return false;

switch (args["type"].Value<string>())
{
case "paused":
Expand Down Expand Up @@ -299,25 +282,24 @@ protected override async Task<bool> AcceptEvent(SessionId sessionId, JObject arg
}
if (message["resourceType"].Value<string>() != "console-message")
continue;
var messageArgs = message["message"]?["arguments"]?.Value<JArray>();
var ctx = GetContextFixefox(sessionId);
ctx.GlobalName = args["from"].Value<string>();
if (messageArgs != null && messageArgs.Count == 2)
{
if (messageArgs[0].Value<string>() == MonoConstants.RUNTIME_IS_READY && messageArgs[1].Value<string>() == 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<string>()));
var ctx = GetContextFixefox(sessionId);
ctx.GlobalName = args["target"]["actor"].Value<string>();
ctx.ThreadName = args["target"]["threadActor"].Value<string>();
ResetCmdId();
if (await IsRuntimeAlreadyReadyAlready(sessionId, token))
{
await ForwardMessageToIde(args, token);
await RuntimeReady(sessionId, token);
return true;
}
break;
}
}
Expand Down Expand Up @@ -364,6 +346,8 @@ protected override async Task<bool> AcceptCommand(MessageId sessionId, JObject a
{
var ctx = GetContextFixefox(sessionId);
ctx.ThreadName = args["to"].Value<string>();
if (await IsRuntimeAlreadyReadyAlready(sessionId, token))
await RuntimeReady(sessionId, token);
break;
}
case "source":
Expand Down Expand Up @@ -691,25 +675,33 @@ protected override async Task<bool> 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<Result> 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<bool> 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<string>());
using var retDebuggerCmdReader = new MonoBinaryReader(newBytes);
retDebuggerCmdReader.ReadBytes(11);
Expand Down
41 changes: 14 additions & 27 deletions src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class MonoProxy : DevToolsProxy
{
private IList<string> urlSymbolServerList;
private HashSet<SessionId> sessions = new HashSet<SessionId>();
private static readonly string[] s_executionContextIndependentCDPCommandNames = { "DotnetDebugger.setDebuggerProperty" };
private static readonly string[] s_executionContextIndependentCDPCommandNames = { "DotnetDebugger.setDebuggerProperty", "DotnetDebugger.runTests" };
protected Dictionary<SessionId, ExecutionContext> contexts = new Dictionary<SessionId, ExecutionContext>();

public static HttpClient HttpClient => new HttpClient();
Expand Down Expand Up @@ -94,7 +94,7 @@ protected override async Task<bool> 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")
Expand All @@ -104,28 +104,7 @@ protected override async Task<bool> 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<string[]>();
}
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)
{
Expand Down Expand Up @@ -587,6 +566,13 @@ protected override async Task<bool> 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);
Expand Down Expand Up @@ -1060,14 +1046,15 @@ protected virtual async Task<bool> SendCallStack(SessionId sessionId, ExecutionC
return true;
}

internal virtual void SaveLastDebuggerAgentBufferReceivedToContext(SessionId sessionId, Result res)
internal virtual void SaveLastDebuggerAgentBufferReceivedToContext(SessionId sessionId, Task<Result> debuggerAgentBufferTask)
{
}

internal async Task<bool> 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public async Task<bool> 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",
Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/debugger/DebuggerTestSuite/Inspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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#")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
function invoke_getters_js_test () {
getters_js_test ();
}

function invoke_add () {
return App.int_add (10, 20);
}
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
<body>
<script type='text/javascript'>
var App = {
init: function () { },
init: function () {
console.debug ("#debugger-app-ready#");
},
};
function reload_wasm_page() {
function reload_wasm_page () {
window.location.replace("http://localhost:9400/debugger-driver.html");
}
</script>
Expand Down
3 changes: 0 additions & 3 deletions src/mono/wasm/runtime/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ export function mono_wasm_runtime_ready(): void {
if ((<any>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 {
Expand Down

0 comments on commit 8a2b72e

Please sign in to comment.