From f2a2027d03d0f84fceb932c0c3b15e72bc109f1a Mon Sep 17 00:00:00 2001 From: "DESKTOP-GEPIA6N\\Thays" Date: Thu, 27 Jan 2022 16:33:11 -0300 Subject: [PATCH 1/7] Fixing race condition. --- src/mono/mono/component/mini-wasm-debugger.c | 13 +++++------- src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js | 1 + src/mono/wasm/runtime/debug.ts | 22 ++++++++++++++++---- src/mono/wasm/runtime/es6/dotnet.es6.lib.js | 1 + src/mono/wasm/runtime/exports.ts | 2 ++ 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/mono/mono/component/mini-wasm-debugger.c b/src/mono/mono/component/mini-wasm-debugger.c index 33728e8ca0609..db78dd201f014 100644 --- a/src/mono/mono/component/mini-wasm-debugger.c +++ b/src/mono/mono/component/mini-wasm-debugger.c @@ -356,21 +356,20 @@ mono_wasm_set_is_debugger_attached (gboolean is_attached) } extern void mono_wasm_add_dbg_command_received(mono_bool res_ok, int id, void* buffer, int buffer_len); +extern void mono_wasm_add_dbg_command_result(mono_bool res_ok, int id, void* buffer, int buffer_len); EMSCRIPTEN_KEEPALIVE gboolean mono_wasm_send_dbg_command_with_parms (int id, MdbgProtCommandSet command_set, int command, guint8* data, unsigned int size, int valtype, char* newvalue) { if (!debugger_enabled) { - EM_ASM ({ - MONO.mono_wasm_add_dbg_command_received ($0, $1, $2, $3); - }, 0, id, 0, 0); + mono_wasm_add_dbg_command_result (0, id, 0, 0); return TRUE; } MdbgProtBuffer bufWithParms; buffer_init (&bufWithParms, 128); m_dbgprot_buffer_add_data (&bufWithParms, data, size); if (!write_value_to_buffer(&bufWithParms, valtype, newvalue)) { - mono_wasm_add_dbg_command_received(0, id, 0, 0); + mono_wasm_add_dbg_command_result(0, id, 0, 0); return TRUE; } mono_wasm_send_dbg_command(id, command_set, command, bufWithParms.buf, m_dbgprot_buffer_len(&bufWithParms)); @@ -382,9 +381,7 @@ EMSCRIPTEN_KEEPALIVE gboolean mono_wasm_send_dbg_command (int id, MdbgProtCommandSet command_set, int command, guint8* data, unsigned int size) { if (!debugger_enabled) { - EM_ASM ({ - MONO.mono_wasm_add_dbg_command_received ($0, $1, $2, $3); - }, 0, id, 0, 0); + mono_wasm_add_dbg_command_result(0, id, 0, 0); return TRUE; } ss_calculate_framecount (NULL, NULL, TRUE, NULL, NULL); @@ -403,7 +400,7 @@ mono_wasm_send_dbg_command (int id, MdbgProtCommandSet command_set, int command, else error = mono_process_dbg_packet (id, command_set, command, &no_reply, data, data + size, &buf); - mono_wasm_add_dbg_command_received(error == MDBGPROT_ERR_NONE, id, buf.buf, buf.p-buf.buf); + mono_wasm_add_dbg_command_result (error == MDBGPROT_ERR_NONE, id, buf.buf, buf.p-buf.buf); buffer_free (&buf); return TRUE; diff --git a/src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js b/src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js index a2add7ec2fd68..81dc64a6548ea 100644 --- a/src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js +++ b/src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js @@ -32,6 +32,7 @@ const linked_functions = [ "mono_wasm_fire_debugger_agent_message", "mono_wasm_debugger_log", "mono_wasm_add_dbg_command_received", + "mono_wasm_add_dbg_command_result", // mono-threads-wasm.c "schedule_background_exec", diff --git a/src/mono/wasm/runtime/debug.ts b/src/mono/wasm/runtime/debug.ts index 2aa8f121a55b8..e22a4b24b8955 100644 --- a/src/mono/wasm/runtime/debug.ts +++ b/src/mono/wasm/runtime/debug.ts @@ -7,6 +7,7 @@ import cwraps from "./cwraps"; import { VoidPtr, CharPtr } from "./types/emscripten"; let commands_received: CommandResponse; +let commands_result: CommandResponse; let _call_function_res_cache: any = {}; let _next_call_function_res_id = 0; let _debugger_buffer_len = -1; @@ -46,6 +47,19 @@ export function mono_wasm_add_dbg_command_received(res_ok: boolean, id: number, commands_received = buffer_obj; } +export function mono_wasm_add_dbg_command_result(res_ok: boolean, id: number, buffer: number, buffer_len: number): void { + const assembly_data = new Uint8Array(Module.HEAPU8.buffer, buffer, buffer_len); + const base64String = toBase64StringImpl(assembly_data); + const buffer_obj = { + res_ok, + res: { + id, + value: base64String + } + }; + commands_result = buffer_obj; +} + function mono_wasm_malloc_and_set_debug_buffer(command_parameters: string) { if (command_parameters.length > _debugger_buffer_len) { if (_debugger_buffer) @@ -63,7 +77,7 @@ export function mono_wasm_send_dbg_command_with_parms(id: number, command_set: n mono_wasm_malloc_and_set_debug_buffer(command_parameters); cwraps.mono_wasm_send_dbg_command_with_parms(id, command_set, command, _debugger_buffer, length, valtype, newvalue.toString()); - const { res_ok, res } = commands_received; + const { res_ok, res } = commands_result; if (!res_ok) throw new Error("Failed on mono_wasm_invoke_method_debugger_agent_with_parms"); return res; @@ -73,7 +87,7 @@ export function mono_wasm_send_dbg_command(id: number, command_set: number, comm mono_wasm_malloc_and_set_debug_buffer(command_parameters); cwraps.mono_wasm_send_dbg_command(id, command_set, command, _debugger_buffer, command_parameters.length); - const { res_ok, res } = commands_received; + const { res_ok, res } = commands_result; if (!res_ok) throw new Error("Failed on mono_wasm_send_dbg_command"); return res; @@ -137,7 +151,7 @@ function _create_proxy_from_object_id(objectId: string, details: any) { return mono_wasm_send_dbg_command(-1, prop.get.commandSet, prop.get.command, prop.get.buffer); }, set: function (newValue) { - mono_wasm_send_dbg_command_with_parms(-1, prop.set.commandSet, prop.set.command, prop.set.buffer, prop.set.length, prop.set.valtype, newValue); return commands_received.res_ok; + mono_wasm_send_dbg_command_with_parms(-1, prop.set.commandSet, prop.set.command, prop.set.buffer, prop.set.length, prop.set.valtype, newValue); return commands_result.res_ok; } } ); @@ -149,7 +163,7 @@ function _create_proxy_from_object_id(objectId: string, details: any) { return prop.value; }, set: function (newValue) { - mono_wasm_send_dbg_command_with_parms(-1, prop.set.commandSet, prop.set.command, prop.set.buffer, prop.set.length, prop.set.valtype, newValue); return commands_received.res_ok; + mono_wasm_send_dbg_command_with_parms(-1, prop.set.commandSet, prop.set.command, prop.set.buffer, prop.set.length, prop.set.valtype, newValue); return commands_result.res_ok; } } ); diff --git a/src/mono/wasm/runtime/es6/dotnet.es6.lib.js b/src/mono/wasm/runtime/es6/dotnet.es6.lib.js index 36f5b106a1f4f..e30de7ec27329 100644 --- a/src/mono/wasm/runtime/es6/dotnet.es6.lib.js +++ b/src/mono/wasm/runtime/es6/dotnet.es6.lib.js @@ -69,6 +69,7 @@ const linked_functions = [ "mono_wasm_fire_debugger_agent_message", "mono_wasm_debugger_log", "mono_wasm_add_dbg_command_received", + "mono_wasm_add_dbg_command_result", // mono-threads-wasm.c "schedule_background_exec", diff --git a/src/mono/wasm/runtime/exports.ts b/src/mono/wasm/runtime/exports.ts index 7942377aa411f..8d53ffca9736b 100644 --- a/src/mono/wasm/runtime/exports.ts +++ b/src/mono/wasm/runtime/exports.ts @@ -24,6 +24,7 @@ import { mono_wasm_debugger_log, mono_wasm_trace_logger, mono_wasm_add_dbg_command_received, + mono_wasm_add_dbg_command_result, } from "./debug"; import { runtimeHelpers, setImportsAndExports } from "./imports"; import { DotnetModuleConfigImports, DotnetModule } from "./types"; @@ -266,6 +267,7 @@ export const __linker_exports: any = { mono_wasm_fire_debugger_agent_message, mono_wasm_debugger_log, mono_wasm_add_dbg_command_received, + mono_wasm_add_dbg_command_result, // mono-threads-wasm.c schedule_background_exec, From 7921a856b8af8557e326f425d34e6a31613f6ede Mon Sep 17 00:00:00 2001 From: "DESKTOP-GEPIA6N\\Thays" Date: Sat, 29 Jan 2022 03:20:05 -0300 Subject: [PATCH 2/7] Completely avoid race condition as @lewing suggested. --- src/mono/mono/component/mini-wasm-debugger.c | 11 +++---- .../BrowserDebugProxy/MonoSDBHelper.cs | 12 ++++--- src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js | 1 - src/mono/wasm/runtime/debug.ts | 33 +++++++------------ src/mono/wasm/runtime/es6/dotnet.es6.lib.js | 1 - src/mono/wasm/runtime/exports.ts | 2 -- 6 files changed, 24 insertions(+), 36 deletions(-) diff --git a/src/mono/mono/component/mini-wasm-debugger.c b/src/mono/mono/component/mini-wasm-debugger.c index db78dd201f014..f72c3720e5162 100644 --- a/src/mono/mono/component/mini-wasm-debugger.c +++ b/src/mono/mono/component/mini-wasm-debugger.c @@ -356,20 +356,19 @@ mono_wasm_set_is_debugger_attached (gboolean is_attached) } extern void mono_wasm_add_dbg_command_received(mono_bool res_ok, int id, void* buffer, int buffer_len); -extern void mono_wasm_add_dbg_command_result(mono_bool res_ok, int id, void* buffer, int buffer_len); EMSCRIPTEN_KEEPALIVE gboolean mono_wasm_send_dbg_command_with_parms (int id, MdbgProtCommandSet command_set, int command, guint8* data, unsigned int size, int valtype, char* newvalue) { if (!debugger_enabled) { - mono_wasm_add_dbg_command_result (0, id, 0, 0); + mono_wasm_add_dbg_command_received (0, id, 0, 0); return TRUE; } MdbgProtBuffer bufWithParms; buffer_init (&bufWithParms, 128); m_dbgprot_buffer_add_data (&bufWithParms, data, size); if (!write_value_to_buffer(&bufWithParms, valtype, newvalue)) { - mono_wasm_add_dbg_command_result(0, id, 0, 0); + mono_wasm_add_dbg_command_received(0, id, 0, 0); return TRUE; } mono_wasm_send_dbg_command(id, command_set, command, bufWithParms.buf, m_dbgprot_buffer_len(&bufWithParms)); @@ -381,7 +380,7 @@ EMSCRIPTEN_KEEPALIVE gboolean mono_wasm_send_dbg_command (int id, MdbgProtCommandSet command_set, int command, guint8* data, unsigned int size) { if (!debugger_enabled) { - mono_wasm_add_dbg_command_result(0, id, 0, 0); + mono_wasm_add_dbg_command_received(0, id, 0, 0); return TRUE; } ss_calculate_framecount (NULL, NULL, TRUE, NULL, NULL); @@ -400,7 +399,7 @@ mono_wasm_send_dbg_command (int id, MdbgProtCommandSet command_set, int command, else error = mono_process_dbg_packet (id, command_set, command, &no_reply, data, data + size, &buf); - mono_wasm_add_dbg_command_result (error == MDBGPROT_ERR_NONE, id, buf.buf, buf.p-buf.buf); + mono_wasm_add_dbg_command_received (error == MDBGPROT_ERR_NONE, id, buf.buf, buf.p-buf.buf); buffer_free (&buf); return TRUE; @@ -409,7 +408,7 @@ mono_wasm_send_dbg_command (int id, MdbgProtCommandSet command_set, int command, static gboolean receive_debugger_agent_message (void *data, int len) { - mono_wasm_add_dbg_command_received(1, -1, data, len); + mono_wasm_add_dbg_command_received(1, 0, data, len); mono_wasm_save_thread_context(); mono_wasm_fire_debugger_agent_message (); return FALSE; diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs b/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs index 1d62e94f556f1..51042f817aefd 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs @@ -700,7 +700,7 @@ public PointerValue(long address, int typeId, string varName) internal class MonoSDBHelper { private static int debuggerObjectId; - private static int cmdId; + private static int cmdId = 1; private static int GetId() {return cmdId++;} private static int MINOR_VERSION = 61; private static int MAJOR_VERSION = 2; @@ -2180,6 +2180,7 @@ public async Task GetValueTypeProxy(int valueTypeId, CancellationToken t command = CmdVM.InvokeMethod, buffer = data, length = length, + id = GetId() }), name = propertyNameStr })); @@ -2505,7 +2506,8 @@ async Task GetFieldsValues(List fields, bool isOwn, bool command = CmdObject.RefSetValues, buffer = data, valtype, - length = length + length, + id = GetId() })); } if (!isRootHidden) @@ -2636,7 +2638,8 @@ public async Task GetObjectProxy(int objectId, CancellationToken token) command = CmdVM.InvokeMethod, buffer = data, valtype = attr["set"]["valtype"], - length = length + length, + id = GetId() }); } continue; @@ -2655,7 +2658,8 @@ public async Task GetObjectProxy(int objectId, CancellationToken token) commandSet = CommandSet.Vm, command = CmdVM.InvokeMethod, buffer = data, - length = length + length = length, + id = GetId() }), name = propertyNameStr })); diff --git a/src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js b/src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js index 81dc64a6548ea..a2add7ec2fd68 100644 --- a/src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js +++ b/src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js @@ -32,7 +32,6 @@ const linked_functions = [ "mono_wasm_fire_debugger_agent_message", "mono_wasm_debugger_log", "mono_wasm_add_dbg_command_received", - "mono_wasm_add_dbg_command_result", // mono-threads-wasm.c "schedule_background_exec", diff --git a/src/mono/wasm/runtime/debug.ts b/src/mono/wasm/runtime/debug.ts index e22a4b24b8955..01032aa8d755a 100644 --- a/src/mono/wasm/runtime/debug.ts +++ b/src/mono/wasm/runtime/debug.ts @@ -6,8 +6,7 @@ import { toBase64StringImpl } from "./base64"; import cwraps from "./cwraps"; import { VoidPtr, CharPtr } from "./types/emscripten"; -let commands_received: CommandResponse; -let commands_result: CommandResponse; +const commands_result = new Map(); let _call_function_res_cache: any = {}; let _next_call_function_res_id = 0; let _debugger_buffer_len = -1; @@ -44,20 +43,7 @@ export function mono_wasm_add_dbg_command_received(res_ok: boolean, id: number, value: base64String } }; - commands_received = buffer_obj; -} - -export function mono_wasm_add_dbg_command_result(res_ok: boolean, id: number, buffer: number, buffer_len: number): void { - const assembly_data = new Uint8Array(Module.HEAPU8.buffer, buffer, buffer_len); - const base64String = toBase64StringImpl(assembly_data); - const buffer_obj = { - res_ok, - res: { - id, - value: base64String - } - }; - commands_result = buffer_obj; + commands_result.set(id, buffer_obj); } function mono_wasm_malloc_and_set_debug_buffer(command_parameters: string) { @@ -77,7 +63,8 @@ export function mono_wasm_send_dbg_command_with_parms(id: number, command_set: n mono_wasm_malloc_and_set_debug_buffer(command_parameters); cwraps.mono_wasm_send_dbg_command_with_parms(id, command_set, command, _debugger_buffer, length, valtype, newvalue.toString()); - const { res_ok, res } = commands_result; + const { res_ok, res } = commands_result.get(id) as CommandResponse; + commands_result.delete(id); if (!res_ok) throw new Error("Failed on mono_wasm_invoke_method_debugger_agent_with_parms"); return res; @@ -87,7 +74,8 @@ export function mono_wasm_send_dbg_command(id: number, command_set: number, comm mono_wasm_malloc_and_set_debug_buffer(command_parameters); cwraps.mono_wasm_send_dbg_command(id, command_set, command, _debugger_buffer, command_parameters.length); - const { res_ok, res } = commands_result; + const { res_ok, res } = commands_result.get(id) as CommandResponse; + commands_result.delete(id); if (!res_ok) throw new Error("Failed on mono_wasm_send_dbg_command"); return res; @@ -95,7 +83,8 @@ export function mono_wasm_send_dbg_command(id: number, command_set: number, comm } export function mono_wasm_get_dbg_command_info(): CommandResponseResult { - const { res_ok, res } = commands_received; + const { res_ok, res } = commands_result.get(0) as CommandResponse; + commands_result.delete(0); if (!res_ok) throw new Error("Failed on mono_wasm_get_dbg_command_info"); return res; @@ -148,10 +137,10 @@ function _create_proxy_from_object_id(objectId: string, details: any) { prop.name, { get() { - return mono_wasm_send_dbg_command(-1, prop.get.commandSet, prop.get.command, prop.get.buffer); + return mono_wasm_send_dbg_command(prop.get.id, prop.get.commandSet, prop.get.command, prop.get.buffer); }, set: function (newValue) { - mono_wasm_send_dbg_command_with_parms(-1, prop.set.commandSet, prop.set.command, prop.set.buffer, prop.set.length, prop.set.valtype, newValue); return commands_result.res_ok; + mono_wasm_send_dbg_command_with_parms(prop.set.id, prop.set.commandSet, prop.set.command, prop.set.buffer, prop.set.length, prop.set.valtype, newValue); return true; } } ); @@ -163,7 +152,7 @@ function _create_proxy_from_object_id(objectId: string, details: any) { return prop.value; }, set: function (newValue) { - mono_wasm_send_dbg_command_with_parms(-1, prop.set.commandSet, prop.set.command, prop.set.buffer, prop.set.length, prop.set.valtype, newValue); return commands_result.res_ok; + mono_wasm_send_dbg_command_with_parms(prop.set.id, prop.set.commandSet, prop.set.command, prop.set.buffer, prop.set.length, prop.set.valtype, newValue); return true; } } ); diff --git a/src/mono/wasm/runtime/es6/dotnet.es6.lib.js b/src/mono/wasm/runtime/es6/dotnet.es6.lib.js index e30de7ec27329..36f5b106a1f4f 100644 --- a/src/mono/wasm/runtime/es6/dotnet.es6.lib.js +++ b/src/mono/wasm/runtime/es6/dotnet.es6.lib.js @@ -69,7 +69,6 @@ const linked_functions = [ "mono_wasm_fire_debugger_agent_message", "mono_wasm_debugger_log", "mono_wasm_add_dbg_command_received", - "mono_wasm_add_dbg_command_result", // mono-threads-wasm.c "schedule_background_exec", diff --git a/src/mono/wasm/runtime/exports.ts b/src/mono/wasm/runtime/exports.ts index 8d53ffca9736b..7942377aa411f 100644 --- a/src/mono/wasm/runtime/exports.ts +++ b/src/mono/wasm/runtime/exports.ts @@ -24,7 +24,6 @@ import { mono_wasm_debugger_log, mono_wasm_trace_logger, mono_wasm_add_dbg_command_received, - mono_wasm_add_dbg_command_result, } from "./debug"; import { runtimeHelpers, setImportsAndExports } from "./imports"; import { DotnetModuleConfigImports, DotnetModule } from "./types"; @@ -267,7 +266,6 @@ export const __linker_exports: any = { mono_wasm_fire_debugger_agent_message, mono_wasm_debugger_log, mono_wasm_add_dbg_command_received, - mono_wasm_add_dbg_command_result, // mono-threads-wasm.c schedule_background_exec, From bc4dde491553ab82c5e539fd68aaaaf3f9f3f0e6 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GEPIA6N\\Thays" Date: Sat, 29 Jan 2022 03:22:20 -0300 Subject: [PATCH 3/7] keeping old name --- src/mono/wasm/runtime/debug.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/mono/wasm/runtime/debug.ts b/src/mono/wasm/runtime/debug.ts index 01032aa8d755a..bb63c872255c1 100644 --- a/src/mono/wasm/runtime/debug.ts +++ b/src/mono/wasm/runtime/debug.ts @@ -6,7 +6,7 @@ import { toBase64StringImpl } from "./base64"; import cwraps from "./cwraps"; import { VoidPtr, CharPtr } from "./types/emscripten"; -const commands_result = new Map(); +const commands_received = new Map(); let _call_function_res_cache: any = {}; let _next_call_function_res_id = 0; let _debugger_buffer_len = -1; @@ -43,7 +43,7 @@ export function mono_wasm_add_dbg_command_received(res_ok: boolean, id: number, value: base64String } }; - commands_result.set(id, buffer_obj); + commands_received.set(id, buffer_obj); } function mono_wasm_malloc_and_set_debug_buffer(command_parameters: string) { @@ -63,8 +63,8 @@ export function mono_wasm_send_dbg_command_with_parms(id: number, command_set: n mono_wasm_malloc_and_set_debug_buffer(command_parameters); cwraps.mono_wasm_send_dbg_command_with_parms(id, command_set, command, _debugger_buffer, length, valtype, newvalue.toString()); - const { res_ok, res } = commands_result.get(id) as CommandResponse; - commands_result.delete(id); + const { res_ok, res } = commands_received.get(id) as CommandResponse; + commands_received.delete(id); if (!res_ok) throw new Error("Failed on mono_wasm_invoke_method_debugger_agent_with_parms"); return res; @@ -74,8 +74,8 @@ export function mono_wasm_send_dbg_command(id: number, command_set: number, comm mono_wasm_malloc_and_set_debug_buffer(command_parameters); cwraps.mono_wasm_send_dbg_command(id, command_set, command, _debugger_buffer, command_parameters.length); - const { res_ok, res } = commands_result.get(id) as CommandResponse; - commands_result.delete(id); + const { res_ok, res } = commands_received.get(id) as CommandResponse; + commands_received.delete(id); if (!res_ok) throw new Error("Failed on mono_wasm_send_dbg_command"); return res; @@ -83,8 +83,8 @@ export function mono_wasm_send_dbg_command(id: number, command_set: number, comm } export function mono_wasm_get_dbg_command_info(): CommandResponseResult { - const { res_ok, res } = commands_result.get(0) as CommandResponse; - commands_result.delete(0); + const { res_ok, res } = commands_received.get(0) as CommandResponse; + commands_received.delete(0); if (!res_ok) throw new Error("Failed on mono_wasm_get_dbg_command_info"); return res; From 532ee06218127bd7f44b857c6af266f124cb54d8 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GEPIA6N\\Thays" Date: Tue, 1 Feb 2022 11:04:33 -0300 Subject: [PATCH 4/7] Addressing @radical comments --- .../BrowserDebugProxy/MonoSDBHelper.cs | 18 +++++++++--------- src/mono/wasm/runtime/debug.ts | 2 ++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs b/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs index 51042f817aefd..51480c7e486bf 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs @@ -163,7 +163,7 @@ internal enum CmdThread { GetState = 3, GetInfo = 4, /* FIXME: Merge into GetInfo when the major protocol version is increased */ - GetId = 5, + GetNewId = 5, /* Ditto */ GetTid = 6, SetIp = 7, @@ -700,8 +700,8 @@ public PointerValue(long address, int typeId, string varName) internal class MonoSDBHelper { private static int debuggerObjectId; - private static int cmdId = 1; - private static int GetId() {return cmdId++;} + private static int cmdId = 1; //cmdId == 0 is used by events which come from runtime + private static int GetNewId() {return cmdId++;} private static int MINOR_VERSION = 61; private static int MAJOR_VERSION = 2; @@ -862,7 +862,7 @@ public async Task EnableReceiveRequests(EventKind eventKind, CancellationT } internal async Task SendDebuggerAgentCommand(T command, MonoBinaryWriter arguments, CancellationToken token) => - MonoBinaryReader.From (await proxy.SendMonoCommand(sessionId, MonoCommands.SendDebuggerAgentCommand(proxy.RuntimeId, GetId(), (int)GetCommandSetForCommand(command), (int)(object)command, arguments?.ToBase64().data ?? string.Empty), token)); + MonoBinaryReader.From (await proxy.SendMonoCommand(sessionId, MonoCommands.SendDebuggerAgentCommand(proxy.RuntimeId, GetNewId(), (int)GetCommandSetForCommand(command), (int)(object)command, arguments?.ToBase64().data ?? string.Empty), token)); internal CommandSet GetCommandSetForCommand(T command) => command switch { @@ -885,7 +885,7 @@ internal CommandSet GetCommandSetForCommand(T command) => }; internal async Task SendDebuggerAgentCommandWithParms(T command, (string data, int length) encoded, int type, string extraParm, CancellationToken token) => - MonoBinaryReader.From(await proxy.SendMonoCommand(sessionId, MonoCommands.SendDebuggerAgentCommandWithParms(proxy.RuntimeId, GetId(), (int)GetCommandSetForCommand(command), (int)(object)command, encoded.data, encoded.length, type, extraParm), token)); + MonoBinaryReader.From(await proxy.SendMonoCommand(sessionId, MonoCommands.SendDebuggerAgentCommandWithParms(proxy.RuntimeId, GetNewId(), (int)GetCommandSetForCommand(command), (int)(object)command, encoded.data, encoded.length, type, extraParm), token)); public async Task CreateString(string value, CancellationToken token) { @@ -2180,7 +2180,7 @@ public async Task GetValueTypeProxy(int valueTypeId, CancellationToken t command = CmdVM.InvokeMethod, buffer = data, length = length, - id = GetId() + id = GetNewId() }), name = propertyNameStr })); @@ -2507,7 +2507,7 @@ async Task GetFieldsValues(List fields, bool isOwn, bool buffer = data, valtype, length, - id = GetId() + id = GetNewId() })); } if (!isRootHidden) @@ -2639,7 +2639,7 @@ public async Task GetObjectProxy(int objectId, CancellationToken token) buffer = data, valtype = attr["set"]["valtype"], length, - id = GetId() + id = GetNewId() }); } continue; @@ -2659,7 +2659,7 @@ public async Task GetObjectProxy(int objectId, CancellationToken token) command = CmdVM.InvokeMethod, buffer = data, length = length, - id = GetId() + id = GetNewId() }), name = propertyNameStr })); diff --git a/src/mono/wasm/runtime/debug.ts b/src/mono/wasm/runtime/debug.ts index bb63c872255c1..f70a30ed089cd 100644 --- a/src/mono/wasm/runtime/debug.ts +++ b/src/mono/wasm/runtime/debug.ts @@ -43,6 +43,8 @@ export function mono_wasm_add_dbg_command_received(res_ok: boolean, id: number, value: base64String } }; + if (commands_received.has(id)) + console.warn("Addind an id that already exists in commands_received"); commands_received.set(id, buffer_obj); } From 743791cf3475db4a4c0f0be549026c8e5667e6f7 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GEPIA6N\\Thays" Date: Tue, 1 Feb 2022 18:05:54 -0300 Subject: [PATCH 5/7] Addressing @lewing comments. --- src/mono/wasm/runtime/debug.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/mono/wasm/runtime/debug.ts b/src/mono/wasm/runtime/debug.ts index f70a30ed089cd..a6f69b4773feb 100644 --- a/src/mono/wasm/runtime/debug.ts +++ b/src/mono/wasm/runtime/debug.ts @@ -61,12 +61,17 @@ function mono_wasm_malloc_and_set_debug_buffer(command_parameters: string) { } } +function mono_get_and_delete_from_commands_received(id: number): CommandResponse{ + const res = commands_received.get(id) as CommandResponse; + commands_received.delete(id); + return res; +} + export function mono_wasm_send_dbg_command_with_parms(id: number, command_set: number, command: number, command_parameters: string, length: number, valtype: number, newvalue: number): CommandResponseResult { mono_wasm_malloc_and_set_debug_buffer(command_parameters); cwraps.mono_wasm_send_dbg_command_with_parms(id, command_set, command, _debugger_buffer, length, valtype, newvalue.toString()); - const { res_ok, res } = commands_received.get(id) as CommandResponse; - commands_received.delete(id); + const { res_ok, res } = mono_get_and_delete_from_commands_received(id); if (!res_ok) throw new Error("Failed on mono_wasm_invoke_method_debugger_agent_with_parms"); return res; @@ -76,8 +81,8 @@ export function mono_wasm_send_dbg_command(id: number, command_set: number, comm mono_wasm_malloc_and_set_debug_buffer(command_parameters); cwraps.mono_wasm_send_dbg_command(id, command_set, command, _debugger_buffer, command_parameters.length); - const { res_ok, res } = commands_received.get(id) as CommandResponse; - commands_received.delete(id); + const { res_ok, res } = mono_get_and_delete_from_commands_received(id); + if (!res_ok) throw new Error("Failed on mono_wasm_send_dbg_command"); return res; @@ -85,8 +90,8 @@ export function mono_wasm_send_dbg_command(id: number, command_set: number, comm } export function mono_wasm_get_dbg_command_info(): CommandResponseResult { - const { res_ok, res } = commands_received.get(0) as CommandResponse; - commands_received.delete(0); + const { res_ok, res } = mono_get_and_delete_from_commands_received(0); + if (!res_ok) throw new Error("Failed on mono_wasm_get_dbg_command_info"); return res; From e511ff9933111aa3f9818f767f4aa903278ec402 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GEPIA6N\\Thays" Date: Tue, 1 Feb 2022 18:58:26 -0300 Subject: [PATCH 6/7] fix unrelated change --- src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs b/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs index 51480c7e486bf..75c3a6cf8fd35 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs @@ -163,7 +163,7 @@ internal enum CmdThread { GetState = 3, GetInfo = 4, /* FIXME: Merge into GetInfo when the major protocol version is increased */ - GetNewId = 5, + GetId = 5, /* Ditto */ GetTid = 6, SetIp = 7, From a183c575f3d2d3b144d0ccca0d00a75a8888a787 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GEPIA6N\\Thays" Date: Wed, 2 Feb 2022 11:34:50 -0300 Subject: [PATCH 7/7] Addressing @lewing comment offline. --- src/mono/wasm/runtime/debug.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/mono/wasm/runtime/debug.ts b/src/mono/wasm/runtime/debug.ts index a6f69b4773feb..ef21caf07b42d 100644 --- a/src/mono/wasm/runtime/debug.ts +++ b/src/mono/wasm/runtime/debug.ts @@ -6,7 +6,8 @@ import { toBase64StringImpl } from "./base64"; import cwraps from "./cwraps"; import { VoidPtr, CharPtr } from "./types/emscripten"; -const commands_received = new Map(); +const commands_received : any = new Map(); +commands_received.remove = function (key: number) : CommandResponse { const value = this.get(key); this.delete(key); return value;}; let _call_function_res_cache: any = {}; let _next_call_function_res_id = 0; let _debugger_buffer_len = -1; @@ -61,17 +62,11 @@ function mono_wasm_malloc_and_set_debug_buffer(command_parameters: string) { } } -function mono_get_and_delete_from_commands_received(id: number): CommandResponse{ - const res = commands_received.get(id) as CommandResponse; - commands_received.delete(id); - return res; -} - export function mono_wasm_send_dbg_command_with_parms(id: number, command_set: number, command: number, command_parameters: string, length: number, valtype: number, newvalue: number): CommandResponseResult { mono_wasm_malloc_and_set_debug_buffer(command_parameters); cwraps.mono_wasm_send_dbg_command_with_parms(id, command_set, command, _debugger_buffer, length, valtype, newvalue.toString()); - const { res_ok, res } = mono_get_and_delete_from_commands_received(id); + const { res_ok, res } = commands_received.remove(id); if (!res_ok) throw new Error("Failed on mono_wasm_invoke_method_debugger_agent_with_parms"); return res; @@ -81,7 +76,7 @@ export function mono_wasm_send_dbg_command(id: number, command_set: number, comm mono_wasm_malloc_and_set_debug_buffer(command_parameters); cwraps.mono_wasm_send_dbg_command(id, command_set, command, _debugger_buffer, command_parameters.length); - const { res_ok, res } = mono_get_and_delete_from_commands_received(id); + const { res_ok, res } = commands_received.remove(id); if (!res_ok) throw new Error("Failed on mono_wasm_send_dbg_command"); @@ -90,7 +85,7 @@ export function mono_wasm_send_dbg_command(id: number, command_set: number, comm } export function mono_wasm_get_dbg_command_info(): CommandResponseResult { - const { res_ok, res } = mono_get_and_delete_from_commands_received(0); + const { res_ok, res } = commands_received.remove(0); if (!res_ok) throw new Error("Failed on mono_wasm_get_dbg_command_info");