Skip to content
Merged
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
23 changes: 23 additions & 0 deletions src/mono/mono/component/debugger-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -7012,6 +7012,29 @@ vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
buffer_add_assemblyid (buf, mono_get_root_domain (), assembly);
break;
}
case MDBGPROT_CMD_GET_MODULE_BY_GUID: {
int len = 0;
uint8_t* guid = m_dbgprot_decode_byte_array (p, &p, end, &len);
MonoAssembly *assembly = NULL;
GPtrArray *assemblies = mono_alc_get_all_loaded_assemblies ();
for (int i = 0; i < assemblies->len; ++i) {
MonoAssembly *assemblyOnALC = (MonoAssembly*)g_ptr_array_index (assemblies, i);
if (!memcmp(assemblyOnALC->image->heap_guid.data, guid, len)) {
assembly = assemblyOnALC;
break;
}
}
g_ptr_array_free (assemblies, TRUE);
if (!assembly) {
PRINT_DEBUG_MSG (1, "Could not resolve guid\n");
g_free (guid);
buffer_add_int (buf, -1);
break;
}
g_free (guid);
buffer_add_moduleid (buf, mono_get_root_domain (), assembly->image);
break;
}
default:
return ERR_NOT_IMPLEMENTED;
}
Expand Down
5 changes: 3 additions & 2 deletions src/mono/mono/component/debugger-protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ typedef enum {
MDBGPROT_CMD_VM_STOP_BUFFERING = 15,
MDBGPROT_CMD_VM_READ_MEMORY = 16,
MDBGPROT_CMD_VM_WRITE_MEMORY = 17,
MDBGPROT_CMD_GET_ASSEMBLY_BY_NAME = 18
MDBGPROT_CMD_GET_ASSEMBLY_BY_NAME = 18,
MDBGPROT_CMD_GET_MODULE_BY_GUID = 19
} MdbgProtCmdVM;

typedef enum {
Expand Down Expand Up @@ -150,7 +151,7 @@ typedef enum {

typedef enum {
MDBGPROT_CMD_MODULE_GET_INFO = 1,
MDBGPROT_CMD_MODULE_APPLY_CHANGES = 2,
MDBGPROT_CMD_MODULE_APPLY_CHANGES = 2
} MdbgProtCmdModule;

typedef enum {
Expand Down
21 changes: 21 additions & 0 deletions src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,14 @@ protected override async Task<bool> AcceptCommand(MessageId id, string method, J
}

// Protocol extensions
case "DotnetDebugger.applyUpdates":
{
if (await ApplyUpdates(id, args, token))
SendResponse(id, Result.OkFromObject(new { }), token);
else
SendResponse(id, Result.Err("ApplyUpdate failed."), token);
return true;
}
case "DotnetDebugger.addSymbolServerUrl":
{
string url = args["url"]?.Value<string>();
Expand Down Expand Up @@ -593,6 +601,19 @@ protected override async Task<bool> AcceptCommand(MessageId id, string method, J

return false;
}

private async Task<bool> ApplyUpdates(MessageId id, JObject args, CancellationToken token)
{
var context = GetContext(id);
string moduleGUID = args["moduleGUID"]?.Value<string>();
string dmeta = args["dmeta"]?.Value<string>();
string dil = args["dil"]?.Value<string>();
string dpdb = args["dpdb"]?.Value<string>();
var moduleId = await context.SdbAgent.GetModuleId(moduleGUID, token);
var applyUpdates = await context.SdbAgent.ApplyUpdates(moduleId, dmeta, dil, dpdb, token);
return applyUpdates;
}

private void SetJustMyCode(MessageId id, JObject args, CancellationToken token)
{
var isEnabled = args["enabled"]?.Value<bool>();
Expand Down
56 changes: 53 additions & 3 deletions src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ internal enum CmdVM {
StopBuffering = 15,
VmReadMemory = 16,
VmWriteMemory = 17,
GetAssemblyByName = 18
GetAssemblyByName = 18,
GetModuleByGUID = 19
}

internal enum CmdFrame {
Expand Down Expand Up @@ -483,8 +484,7 @@ public MonoBinaryWriter() : base(new MemoryStream(20)) {}
public override void Write(string val)
{
var bytes = Encoding.UTF8.GetBytes(val);
Write(bytes.Length);
Write(bytes);
WriteByteArray(bytes);
}

public override void Write(long val) => WriteBigEndian<long>(val);
Expand Down Expand Up @@ -520,6 +520,13 @@ public void WriteObj(DotnetObjectId objectId, MonoSDBHelper SdbHelper)
Write(SdbHelper.valueTypes[objectId.Value].valueTypeBuffer);
}
}

public void WriteByteArray(byte[] bytes)
{
Write(bytes.Length);
Write(bytes);
}

public async Task<bool> WriteConst(LiteralExpressionSyntax constValue, MonoSDBHelper SdbHelper, CancellationToken token)
{
switch (constValue.Kind())
Expand Down Expand Up @@ -996,6 +1003,16 @@ public async Task<int> GetAssemblyId(string asm_name, CancellationToken token)
return retDebuggerCmdReader.ReadInt32();
}

public async Task<int> GetModuleId(string moduleGuid, CancellationToken token)
{
using var commandParamsWriter = new MonoBinaryWriter();
var guidArray = Convert.FromBase64String(moduleGuid);
commandParamsWriter.WriteByteArray(guidArray);

using var retDebuggerCmdReader = await SendDebuggerAgentCommand(CmdVM.GetModuleByGUID, commandParamsWriter, token);
return retDebuggerCmdReader.ReadInt32();
}

public async Task<string> GetAssemblyNameFromModule(int moduleId, CancellationToken token)
{
using var command_params_writer = new MonoBinaryWriter();
Expand Down Expand Up @@ -2665,6 +2682,39 @@ public async Task<bool> SetVariableValue(int thread_id, int frame_id, int varId,
return false;
return true;
}

public async Task<int> CreateByteArray(string diff, CancellationToken token)
{
var diffArr = Convert.FromBase64String(diff);
using var commandParamsWriter = new MonoBinaryWriter();
using var retDebuggerCmdReader = await SendDebuggerAgentCommand(CmdAppDomain.GetRootDomain, commandParamsWriter, token);
var root = retDebuggerCmdReader.ReadInt32();

commandParamsWriter.Write(root);
commandParamsWriter.WriteByteArray(diffArr);
using var arrayDebuggerCmdReader = await SendDebuggerAgentCommand(CmdAppDomain.CreateByteArray, commandParamsWriter, token);
return arrayDebuggerCmdReader.ReadInt32();
}

public async Task<bool> ApplyUpdates(int moduleId, string dmeta, string dil, string dpdb, CancellationToken token)
{
int dpdbId = -1;
var dmetaId = await CreateByteArray(dmeta, token);
var dilId = await CreateByteArray(dil, token);
if (dpdb != null)
dpdbId = await CreateByteArray(dpdb, token);

using var commandParamsWriter = new MonoBinaryWriter();
commandParamsWriter.Write(moduleId);
commandParamsWriter.Write(dmetaId);
commandParamsWriter.Write(dilId);
if (dpdbId != -1)
commandParamsWriter.Write(dpdbId);
else
commandParamsWriter.Write((byte)ValueTypeId.Null);
await SendDebuggerAgentCommand(CmdModule.ApplyChanges, commandParamsWriter, token);
return true;
}
}

internal static class HelperExtensions
Expand Down
183 changes: 183 additions & 0 deletions src/mono/wasm/debugger/DebuggerTestSuite/BreakpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,190 @@ await SendCommandAndCheck(null, "Debugger.resume",
8,
"VisibleMethodDebuggerBreak");
}

[Fact]
public async Task DebugHotReloadMethodChangedUserBreakUsingSDB()
{
string asm_file = Path.Combine(DebuggerTestAppPath, "ApplyUpdateReferencedAssembly.dll");
string pdb_file = Path.Combine(DebuggerTestAppPath, "ApplyUpdateReferencedAssembly.pdb");
string asm_file_hot_reload = Path.Combine(DebuggerTestAppPath, "../wasm/ApplyUpdateReferencedAssembly.dll");

var pause_location = await LoadAssemblyAndTestHotReloadUsingSDBWithoutChanges(
asm_file, pdb_file, "MethodBody1", "StaticMethod1");

var locals = await GetProperties(pause_location["callFrames"][0]["callFrameId"].Value<string>());
CheckNumber(locals, "a", 10);
pause_location = await LoadAssemblyAndTestHotReloadUsingSDB(
asm_file_hot_reload, "MethodBody1", "StaticMethod1", 1);

JToken top_frame = pause_location["callFrames"]?[0];
AssertEqual("StaticMethod1", top_frame?["functionName"]?.Value<string>(), top_frame?.ToString());
CheckLocation("dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 12, 16, scripts, top_frame["location"]);

locals = await GetProperties(pause_location["callFrames"][0]["callFrameId"].Value<string>());
CheckNumber(locals, "b", 15);
pause_location = await LoadAssemblyAndTestHotReloadUsingSDB(
asm_file_hot_reload, "MethodBody1", "StaticMethod1", 2);

top_frame = pause_location["callFrames"]?[0];
AssertEqual("StaticMethod1", top_frame?["functionName"]?.Value<string>(), top_frame?.ToString());
CheckLocation("dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 12, 12, scripts, top_frame["location"]);

locals = await GetProperties(pause_location["callFrames"][0]["callFrameId"].Value<string>());
await CheckBool(locals, "c", true);
}

[Fact]
public async Task DebugHotReloadMethodUnchangedUsingSDB()
{
string asm_file = Path.Combine(DebuggerTestAppPath, "ApplyUpdateReferencedAssembly.dll");
string pdb_file = Path.Combine(DebuggerTestAppPath, "ApplyUpdateReferencedAssembly.pdb");
string asm_file_hot_reload = Path.Combine(DebuggerTestAppPath, "../wasm/ApplyUpdateReferencedAssembly.dll");

var pause_location = await LoadAssemblyAndTestHotReloadUsingSDBWithoutChanges(
asm_file, pdb_file, "MethodBody2", "StaticMethod1");

var locals = await GetProperties(pause_location["callFrames"][0]["callFrameId"].Value<string>());
CheckNumber(locals, "a", 10);
pause_location = await LoadAssemblyAndTestHotReloadUsingSDB(
asm_file_hot_reload, "MethodBody2", "StaticMethod1", 1);

JToken top_frame = pause_location["callFrames"]?[0];
AssertEqual("StaticMethod1", top_frame?["functionName"]?.Value<string>(), top_frame?.ToString());
CheckLocation("dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 21, 12, scripts, top_frame["location"]);

locals = await GetProperties(pause_location["callFrames"][0]["callFrameId"].Value<string>());
CheckNumber(locals, "a", 10);
pause_location = await LoadAssemblyAndTestHotReloadUsingSDB(
asm_file_hot_reload, "MethodBody2", "StaticMethod1", 2);

top_frame = pause_location["callFrames"]?[0];
AssertEqual("StaticMethod1", top_frame?["functionName"]?.Value<string>(), top_frame?.ToString());
CheckLocation("dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 21, 12, scripts, top_frame["location"]);
}

[Fact]
public async Task DebugHotReloadMethodAddBreakpointUsingSDB()
{
string asm_file = Path.Combine(DebuggerTestAppPath, "ApplyUpdateReferencedAssembly.dll");
string pdb_file = Path.Combine(DebuggerTestAppPath, "ApplyUpdateReferencedAssembly.pdb");
string asm_file_hot_reload = Path.Combine(DebuggerTestAppPath, "../wasm/ApplyUpdateReferencedAssembly.dll");

int line = 30;
await SetBreakpoint(".*/MethodBody1.cs$", line, 12, use_regex: true);
var pause_location = await LoadAssemblyAndTestHotReloadUsingSDBWithoutChanges(
asm_file, pdb_file, "MethodBody3", "StaticMethod3");

var locals = await GetProperties(pause_location["callFrames"][0]["callFrameId"].Value<string>());
CheckNumber(locals, "a", 10);

//apply first update
pause_location = await LoadAssemblyAndTestHotReloadUsingSDB(
asm_file_hot_reload, "MethodBody3", "StaticMethod3", 1);

JToken top_frame = pause_location["callFrames"]?[0];
AssertEqual("StaticMethod3", top_frame?["functionName"]?.Value<string>(), top_frame?.ToString());
CheckLocation("dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 30, 12, scripts, top_frame["location"]);

locals = await GetProperties(pause_location["callFrames"][0]["callFrameId"].Value<string>());
CheckNumber(locals, "b", 15);

//apply second update
pause_location = await LoadAssemblyAndTestHotReloadUsingSDB(
asm_file_hot_reload, "MethodBody3", "StaticMethod3", 2);

top_frame = pause_location["callFrames"]?[0];
AssertEqual("StaticMethod3", top_frame?["functionName"]?.Value<string>(), top_frame?.ToString());
CheckLocation("dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 30, 12, scripts, top_frame["location"]);

locals = await GetProperties(pause_location["callFrames"][0]["callFrameId"].Value<string>());
await CheckBool(locals, "c", true);

await StepAndCheck(StepKind.Over, "dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 31, 12, "StaticMethod3",
locals_fn: async (locals) =>
{
CheckNumber(locals, "d", 10);
await Task.CompletedTask;
}
);
await StepAndCheck(StepKind.Over, "dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 32, 12, "StaticMethod3",
locals_fn: async (locals) =>
{
CheckNumber(locals, "d", 10);
CheckNumber(locals, "e", 20);
await Task.CompletedTask;
}
);
await StepAndCheck(StepKind.Over, "dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 33, 8, "StaticMethod3",
locals_fn: async (locals) =>
{
CheckNumber(locals, "d", 10);
CheckNumber(locals, "e", 20);
CheckNumber(locals, "f", 50);
await Task.CompletedTask;
}
);
}


[Fact]
public async Task DebugHotReloadMethodEmptyUsingSDB()
{
string asm_file = Path.Combine(DebuggerTestAppPath, "ApplyUpdateReferencedAssembly.dll");
string pdb_file = Path.Combine(DebuggerTestAppPath, "ApplyUpdateReferencedAssembly.pdb");
string asm_file_hot_reload = Path.Combine(DebuggerTestAppPath, "../wasm/ApplyUpdateReferencedAssembly.dll");

int line = 38;
await SetBreakpoint(".*/MethodBody1.cs$", line, 0, use_regex: true);
var pause_location = await LoadAssemblyAndTestHotReloadUsingSDBWithoutChanges(
asm_file, pdb_file, "MethodBody4", "StaticMethod4");

//apply first update
pause_location = await LoadAssemblyAndTestHotReloadUsingSDB(
asm_file_hot_reload, "MethodBody4", "StaticMethod4", 1);

await StepAndCheck(StepKind.Over, "dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 39, 12, "StaticMethod4",
locals_fn: async (locals) =>
{
CheckNumber(locals, "a", 10);
await Task.CompletedTask;
}
);
await StepAndCheck(StepKind.Over, "dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 40, 12, "StaticMethod4",
locals_fn: async (locals) =>
{
CheckNumber(locals, "a", 10);
CheckNumber(locals, "b", 20);
await Task.CompletedTask;
}
);
await StepAndCheck(StepKind.Over, "dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 41, 12, "StaticMethod4",
locals_fn: async (locals) =>
{
CheckNumber(locals, "a", 10);
CheckNumber(locals, "b", 20);
await Task.CompletedTask;
}
);
await StepAndCheck(StepKind.Over, "dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 42, 12, "StaticMethod4",
locals_fn: async (locals) =>
{
CheckNumber(locals, "a", 10);
CheckNumber(locals, "b", 20);
await Task.CompletedTask;
}
);
await StepAndCheck(StepKind.Over, "dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 43, 8, "StaticMethod4",
locals_fn: async (locals) =>
{
CheckNumber(locals, "a", 10);
CheckNumber(locals, "b", 20);
await Task.CompletedTask;
}
);
//pause_location = await SendCommandAndCheck(JObject.FromObject(new { }), "Debugger.resume", "dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 38, 8, "StaticMethod4");
}

[Theory]
[InlineData(false, "RunStepThrough")]
[InlineData(true, "RunStepThrough")]
Expand Down
Loading