Skip to content

Commit bf20df1

Browse files
[release/6.0] [wasm][debugger] Fix loading a non wasm page and then returning to a wasm page. (#60777)
* Fix loading a non wasm page and then returning to a wasm page. * Adding non-wasm-page.html * fixing other method * addressing @radical comments * adding last empty line Co-authored-by: DESKTOP-GEPIA6N\Thays <thaystg@gmail.com>
1 parent de75159 commit bf20df1

File tree

6 files changed

+124
-43
lines changed

6 files changed

+124
-43
lines changed

src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1216,11 +1216,7 @@ private async Task<DebugStore> RuntimeReady(SessionId sessionId, CancellationTok
12161216
return await context.ready.Task;
12171217

12181218
var commandParams = new MemoryStream();
1219-
var retDebuggerCmdReader = await SdbHelper.SendDebuggerAgentCommand<CmdEventRequest>(sessionId, CmdEventRequest.ClearAllBreakpoints, commandParams, token);
1220-
if (retDebuggerCmdReader == null)
1221-
{
1222-
Log("verbose", $"Failed to clear breakpoints");
1223-
}
1219+
await SdbHelper.SendDebuggerAgentCommand<CmdEventRequest>(sessionId, CmdEventRequest.ClearAllBreakpoints, commandParams, token);
12241220

12251221
if (context.PauseOnExceptions != PauseOnExceptionsKind.None && context.PauseOnExceptions != PauseOnExceptionsKind.Unset)
12261222
await SdbHelper.EnableExceptions(sessionId, context.PauseOnExceptions, token);
@@ -1233,7 +1229,7 @@ private async Task<DebugStore> RuntimeReady(SessionId sessionId, CancellationTok
12331229
DebugStore store = await LoadStore(sessionId, token);
12341230
context.ready.SetResult(store);
12351231
SendEvent(sessionId, "Mono.runtimeReady", new JObject(), token);
1236-
SdbHelper.SetStore(store);
1232+
SdbHelper.ResetStore(store);
12371233
return store;
12381234
}
12391235

src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs

+24-22
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,11 @@ public TypeInfoWithDebugInformation(TypeInfo typeInfo, int debugId, string name)
379379

380380
internal class MonoBinaryReader : BinaryReader
381381
{
382-
public MonoBinaryReader(Stream stream) : base(stream) {}
382+
public bool HasError { get; }
383+
public MonoBinaryReader(Stream stream, bool hasError = false) : base(stream)
384+
{
385+
HasError = hasError;
386+
}
383387

384388
internal static unsafe void PutBytesBE (byte *dest, byte *src, int count)
385389
{
@@ -656,9 +660,9 @@ internal class MonoSDBHelper
656660
private static int MINOR_VERSION = 61;
657661
private static int MAJOR_VERSION = 2;
658662

659-
private Dictionary<int, MethodInfoWithDebugInformation> methods = new();
660-
private Dictionary<int, AssemblyInfo> assemblies = new();
661-
private Dictionary<int, TypeInfoWithDebugInformation> types = new();
663+
private Dictionary<int, MethodInfoWithDebugInformation> methods;
664+
private Dictionary<int, AssemblyInfo> assemblies;
665+
private Dictionary<int, TypeInfoWithDebugInformation> types;
662666

663667
internal Dictionary<int, ValueTypeClass> valueTypes = new Dictionary<int, ValueTypeClass>();
664668
internal Dictionary<int, PointerValue> pointerValues = new Dictionary<int, PointerValue>();
@@ -673,12 +677,16 @@ public MonoSDBHelper(MonoProxy proxy, ILogger logger)
673677
{
674678
this.proxy = proxy;
675679
this.logger = logger;
676-
this.store = null;
680+
ResetStore(null);
677681
}
678682

679-
public void SetStore(DebugStore store)
683+
public void ResetStore(DebugStore store)
680684
{
681685
this.store = store;
686+
this.methods = new();
687+
this.assemblies = new();
688+
this.types = new();
689+
ClearCache();
682690
}
683691

684692
public async Task<AssemblyInfo> GetAssemblyInfo(SessionId sessionId, int assemblyId, CancellationToken token)
@@ -816,12 +824,12 @@ public async Task<bool> EnableReceiveRequests(SessionId sessionId, EventKind eve
816824
internal async Task<MonoBinaryReader> SendDebuggerAgentCommandInternal(SessionId sessionId, int command_set, int command, MemoryStream parms, CancellationToken token)
817825
{
818826
Result res = await proxy.SendMonoCommand(sessionId, MonoCommands.SendDebuggerAgentCommand(GetId(), command_set, command, Convert.ToBase64String(parms.ToArray())), token);
819-
if (res.IsErr) {
820-
throw new Exception($"SendDebuggerAgentCommand Error - {(CommandSet)command_set} - {command}");
827+
byte[] newBytes = Array.Empty<byte>();
828+
if (!res.IsErr) {
829+
newBytes = Convert.FromBase64String(res.Value?["result"]?["value"]?["value"]?.Value<string>());
821830
}
822-
byte[] newBytes = Convert.FromBase64String(res.Value?["result"]?["value"]?["value"]?.Value<string>());
823831
var retDebuggerCmd = new MemoryStream(newBytes);
824-
var retDebuggerCmdReader = new MonoBinaryReader(retDebuggerCmd);
832+
var retDebuggerCmdReader = new MonoBinaryReader(retDebuggerCmd, res.IsErr);
825833
return retDebuggerCmdReader;
826834
}
827835

@@ -854,12 +862,12 @@ internal Task<MonoBinaryReader> SendDebuggerAgentCommandWithParms<T>(SessionId s
854862
internal async Task<MonoBinaryReader> SendDebuggerAgentCommandWithParmsInternal(SessionId sessionId, int command_set, int command, MemoryStream parms, int type, string extraParm, CancellationToken token)
855863
{
856864
Result res = await proxy.SendMonoCommand(sessionId, MonoCommands.SendDebuggerAgentCommandWithParms(GetId(), command_set, command, Convert.ToBase64String(parms.ToArray()), parms.ToArray().Length, type, extraParm), token);
857-
if (res.IsErr) {
858-
throw new Exception("SendDebuggerAgentCommandWithParms Error");
865+
byte[] newBytes = Array.Empty<byte>();
866+
if (!res.IsErr) {
867+
newBytes = Convert.FromBase64String(res.Value?["result"]?["value"]?["value"]?.Value<string>());
859868
}
860-
byte[] newBytes = Convert.FromBase64String(res.Value?["result"]?["value"]?["value"]?.Value<string>());
861869
var retDebuggerCmd = new MemoryStream(newBytes);
862-
var retDebuggerCmdReader = new MonoBinaryReader(retDebuggerCmd);
870+
var retDebuggerCmdReader = new MonoBinaryReader(retDebuggerCmd, res.IsErr);
863871
return retDebuggerCmdReader;
864872
}
865873

@@ -2530,15 +2538,9 @@ public async Task<bool> SetVariableValue(SessionId sessionId, int thread_id, int
25302538
JArray locals = new JArray();
25312539
retDebuggerCmdReader = await SendDebuggerAgentCommand<CmdFrame>(sessionId, CmdFrame.GetValues, commandParams, token);
25322540
int etype = retDebuggerCmdReader.ReadByte();
2533-
try
2534-
{
2535-
retDebuggerCmdReader = await SendDebuggerAgentCommandWithParms<CmdFrame>(sessionId, CmdFrame.SetValues, commandParams, etype, newValue, token);
2536-
}
2537-
catch (Exception)
2538-
{
2541+
retDebuggerCmdReader = await SendDebuggerAgentCommandWithParms<CmdFrame>(sessionId, CmdFrame.SetValues, commandParams, etype, newValue, token);
2542+
if (retDebuggerCmdReader.HasError)
25392543
return false;
2540-
}
2541-
25422544
return true;
25432545
}
25442546
}

src/mono/wasm/debugger/DebuggerTestSuite/BreakpointTests.cs

+70-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Newtonsoft.Json.Linq;
99
using System.IO;
1010
using Xunit;
11+
using System.Threading;
1112

1213
namespace DebuggerTests
1314
{
@@ -131,11 +132,6 @@ public async Task CreateGoodBreakpointAndHit()
131132
{
132133
var bp = await SetBreakpoint("dotnet://debugger-test.dll/debugger-test.cs", 10, 8);
133134

134-
var eval_req = JObject.FromObject(new
135-
{
136-
expression = "window.setTimeout(function() { invoke_add(); }, 1);",
137-
});
138-
139135
await EvaluateAndCheck(
140136
"window.setTimeout(function() { invoke_add(); }, 1);",
141137
"dotnet://debugger-test.dll/debugger-test.cs", 10, 8,
@@ -577,5 +573,74 @@ await SendCommandAndCheck(null, "Debugger.resume",
577573
CheckNumber(locals, "i", 2);
578574
});
579575
}
576+
577+
[Fact]
578+
public async Task CreateGoodBreakpointAndHitGoToNonWasmPageComeBackAndHitAgain()
579+
{
580+
var bp = await SetBreakpoint("dotnet://debugger-test.dll/debugger-test.cs", 10, 8);
581+
var pause_location = await EvaluateAndCheck(
582+
"window.setTimeout(function() { invoke_add(); }, 1);",
583+
"dotnet://debugger-test.dll/debugger-test.cs", 10, 8,
584+
"IntAdd");
585+
Assert.Equal("other", pause_location["reason"]?.Value<string>());
586+
Assert.Equal(bp.Value["breakpointId"]?.ToString(), pause_location["hitBreakpoints"]?[0]?.Value<string>());
587+
588+
var top_frame = pause_location["callFrames"][0];
589+
Assert.Equal("IntAdd", top_frame["functionName"].Value<string>());
590+
Assert.Contains("debugger-test.cs", top_frame["url"].Value<string>());
591+
592+
CheckLocation("dotnet://debugger-test.dll/debugger-test.cs", 8, 4, scripts, top_frame["functionLocation"]);
593+
594+
//now check the scope
595+
var scope = top_frame["scopeChain"][0];
596+
Assert.Equal("local", scope["type"]);
597+
Assert.Equal("IntAdd", scope["name"]);
598+
599+
Assert.Equal("object", scope["object"]["type"]);
600+
CheckLocation("dotnet://debugger-test.dll/debugger-test.cs", 8, 4, scripts, scope["startLocation"]);
601+
CheckLocation("dotnet://debugger-test.dll/debugger-test.cs", 14, 4, scripts, scope["endLocation"]);
602+
603+
await cli.SendCommand("Debugger.resume", null, token);
604+
605+
var run_method = JObject.FromObject(new
606+
{
607+
expression = "window.setTimeout(function() { load_non_wasm_page(); }, 1);"
608+
});
609+
await cli.SendCommand("Runtime.evaluate", run_method, token);
610+
await Task.Delay(1000, token);
611+
612+
run_method = JObject.FromObject(new
613+
{
614+
expression = "window.setTimeout(function() { reload_wasm_page(); }, 1);"
615+
});
616+
await cli.SendCommand("Runtime.evaluate", run_method, token);
617+
await insp.WaitFor(Inspector.READY);
618+
await EvaluateAndCheck(
619+
"window.setTimeout(function() { invoke_add(); }, 1);",
620+
"dotnet://debugger-test.dll/debugger-test.cs", 10, 8,
621+
"IntAdd",
622+
wait_for_event_fn: (pause_location) =>
623+
{
624+
Assert.Equal("other", pause_location["reason"]?.Value<string>());
625+
Assert.Equal(bp.Value["breakpointId"]?.ToString(), pause_location["hitBreakpoints"]?[0]?.Value<string>());
626+
627+
var top_frame = pause_location["callFrames"][0];
628+
Assert.Equal("IntAdd", top_frame["functionName"].Value<string>());
629+
Assert.Contains("debugger-test.cs", top_frame["url"].Value<string>());
630+
631+
CheckLocation("dotnet://debugger-test.dll/debugger-test.cs", 8, 4, scripts, top_frame["functionLocation"]);
632+
633+
//now check the scope
634+
var scope = top_frame["scopeChain"][0];
635+
Assert.Equal("local", scope["type"]);
636+
Assert.Equal("IntAdd", scope["name"]);
637+
638+
Assert.Equal("object", scope["object"]["type"]);
639+
CheckLocation("dotnet://debugger-test.dll/debugger-test.cs", 8, 4, scripts, scope["startLocation"]);
640+
CheckLocation("dotnet://debugger-test.dll/debugger-test.cs", 14, 4, scripts, scope["endLocation"]);
641+
return Task.CompletedTask;
642+
}
643+
);
644+
}
580645
}
581646
}

src/mono/wasm/debugger/tests/debugger-test/debugger-driver.html

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!doctype html>
22
<html lang="en-us">
3-
<head>
4-
</head>
5-
<body>
3+
<head>
4+
</head>
5+
<body>
66
<script type='text/javascript'>
77
var App = {
88
init: function () {
@@ -82,10 +82,15 @@
8282
function invoke_add_with_parms (a, b) {
8383
return App.int_add (a, b);
8484
}
85-
</script>
86-
<script type="text/javascript" src="runtime-debugger.js"></script>
87-
<script type="text/javascript" src="other.js"></script>
88-
<script async type="text/javascript" src="dotnet.js"></script>
89-
Stuff goes here
90-
</body>
91-
</html>
85+
86+
function load_non_wasm_page () {
87+
console.log("load_non_wasm_page")
88+
window.location.replace("http://localhost:9400/non-wasm-page.html");
89+
}
90+
</script>
91+
<script type="text/javascript" src="runtime-debugger.js"></script>
92+
<script type="text/javascript" src="other.js"></script>
93+
<script async type="text/javascript" src="dotnet.js"></script>
94+
Stuff goes here
95+
</body>
96+
</html>

src/mono/wasm/debugger/tests/debugger-test/debugger-test.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<ItemGroup>
1212
<WasmExtraFilesToDeploy Include="debugger-driver.html" />
13+
<WasmExtraFilesToDeploy Include="non-wasm-page.html" />
1314
<WasmExtraFilesToDeploy Include="other.js" />
1415
<WasmExtraFilesToDeploy Include="runtime-debugger.js" />
1516
<WasmExtraFilesToDeploy Include="weather.json" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en-us">
3+
<head>
4+
</head>
5+
<body>
6+
<script type='text/javascript'>
7+
function reload_wasm_page () {
8+
window.location.replace("http://localhost:9400/debugger-driver.html");
9+
}
10+
</script>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)