Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm][debugger] Implement get bytes from loaded_files using debugger protocol. #69072

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@ -11,7 +11,7 @@
*/

#define MAJOR_VERSION 2
#define MINOR_VERSION 60
#define MINOR_VERSION 61

typedef enum {
MDBGPROT_CMD_COMPOSITE = 100
Expand All @@ -36,7 +36,8 @@ typedef enum {
MDBGPROT_CMD_VM_READ_MEMORY = 16,
MDBGPROT_CMD_VM_WRITE_MEMORY = 17,
MDBGPROT_CMD_GET_ASSEMBLY_BY_NAME = 18,
MDBGPROT_CMD_GET_MODULE_BY_GUID = 19
MDBGPROT_CMD_GET_MODULE_BY_GUID = 19,
MDBGPROT_CMD_GET_ASSEMBLY_BYTES = 20, //wasm specific
} MdbgProtCmdVM;

typedef enum {
Expand Down
10 changes: 10 additions & 0 deletions src/mono/mono/component/mini-wasm-debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,16 @@ mono_wasm_send_dbg_command (int id, MdbgProtCommandSet command_set, int command,
invoke_data.endp = data + size;
error = mono_do_invoke_method (tls, &buf, &invoke_data, data, &data);
}
else if (command_set == MDBGPROT_CMD_SET_VM && (command == MDBGPROT_CMD_GET_ASSEMBLY_BYTES))
{
char* assembly_name = m_dbgprot_decode_string (data, &data, data + size);
radical marked this conversation as resolved.
Show resolved Hide resolved
int assembly_size = 0;
int symfile_size = 0;
const char* assembly_bytes = mono_wasm_get_assembly_bytes (assembly_name, &assembly_size);
radical marked this conversation as resolved.
Show resolved Hide resolved
const char* pdb_bytes = mono_get_symfile_bytes_from_bundle (assembly_name, &symfile_size);
m_dbgprot_buffer_add_byte_array (&buf, (uint8_t *) assembly_bytes, assembly_size);
m_dbgprot_buffer_add_byte_array (&buf, (uint8_t *) pdb_bytes, symfile_size);
}
else
error = mono_process_dbg_packet (id, command_set, command, &no_reply, data, data + size, &buf);

Expand Down
13 changes: 13 additions & 0 deletions src/mono/mono/metadata/mono-debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,19 @@ open_symfile_from_bundle (MonoImage *image)
return NULL;
}

const mono_byte *
mono_get_symfile_bytes_from_bundle (const char *assembly_name, int *size)
{
BundledSymfile *bsymfile;
for (bsymfile = bundled_symfiles; bsymfile; bsymfile = bsymfile->next) {
if (strcmp (bsymfile->aname, assembly_name))
continue;
*size = bsymfile->size;
return bsymfile->raw_contents;
}
return NULL;
}

void
mono_debugger_lock (void)
{
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/mini/mini-wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ G_EXTERN_C void mono_wasm_enable_debugging (int log_level);
void mono_wasm_set_timeout (int timeout);

int mono_wasm_assembly_already_added (const char *assembly_name);
const unsigned char *mono_wasm_get_assembly_bytes (const char *name, unsigned int *size);

void mono_wasm_print_stack_trace (void);

gboolean
Expand Down
63 changes: 41 additions & 22 deletions src/mono/wasm/debugger/BrowserDebugProxy/DebugStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,36 +1285,55 @@ public IEnumerable<SourceFile> Add(SessionId id, string name, byte[] assembly_da
}
}

public async IAsyncEnumerable<SourceFile> Load(SessionId id, string[] loaded_files, [EnumeratorCancellation] CancellationToken token)
public async IAsyncEnumerable<SourceFile> Load(SessionId id, string[] loaded_files, MonoSDBHelper sdbAgent, [EnumeratorCancellation] CancellationToken token)
{
var asm_files = new List<string>();
var pdb_files = new List<string>();
foreach (string file_name in loaded_files)
{
if (file_name.EndsWith(".pdb", StringComparison.OrdinalIgnoreCase))
pdb_files.Add(file_name);
else
asm_files.Add(file_name);
}

List<DebugItem> steps = new List<DebugItem>();
foreach (string url in asm_files)

if (sdbAgent is null)
{
try
var pdb_files = new List<string>();
foreach (string file_name in loaded_files)
{
string candidate_pdb = Path.ChangeExtension(url, "pdb");
string pdb = pdb_files.FirstOrDefault(n => n == candidate_pdb);
if (file_name.EndsWith(".pdb", StringComparison.OrdinalIgnoreCase))
pdb_files.Add(file_name);
else
asm_files.Add(file_name);
}

steps.Add(
new DebugItem
{
Url = url,
Data = Task.WhenAll(client.GetByteArrayAsync(url, token), pdb != null ? client.GetByteArrayAsync(pdb, token) : Task.FromResult<byte[]>(null))
});
foreach (string url in asm_files)
{
try
{
string candidate_pdb = Path.ChangeExtension(url, "pdb");
string pdb = pdb_files.FirstOrDefault(n => n == candidate_pdb);

steps.Add(
new DebugItem
{
Url = url,
Data = Task.WhenAll(client.GetByteArrayAsync(url, token), pdb != null ? client.GetByteArrayAsync(pdb, token) : Task.FromResult<byte[]>(null))
});
}
catch (Exception e)
{
logger.LogDebug($"Failed to read {url} ({e.Message})");
}
}
catch (Exception e)
}
else
{
foreach (string file_name in loaded_files)
{
logger.LogDebug($"Failed to read {url} ({e.Message})");
if (!file_name.EndsWith(".pdb", StringComparison.OrdinalIgnoreCase))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: invert the condition, and continue. it would save on the extra indentation of rest of the code.

{
steps.Add(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try/catch to handle exceptions

new DebugItem
{
Url = file_name,
Data = sdbAgent.GetBytesFromAssemblyAndPdb(System.IO.Path.GetFileName(file_name), token)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the loop processing the results, check if asm bytes is null, so we can log a better error, and continue.

});
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,12 @@ internal async Task<DebugStore> LoadStore(SessionId sessionId, CancellationToken
}
else
{
await foreach (SourceFile source in context.store.Load(sessionId, loaded_files, token).WithCancellation(token))
var useDebuggerProtocol = false;
(int MajorVersion, int MinorVersion) = await context.SdbAgent.GetVMVersion(token);
if (MajorVersion == 2 && MinorVersion >= 61)
useDebuggerProtocol = true;

await foreach (SourceFile source in context.store.Load(sessionId, loaded_files, useDebuggerProtocol ? context.SdbAgent : null, token).WithCancellation(token))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pass just context instead of loaded_files, and sdbagent? context, and a bool useDebuggerProtocol.
We'll need to ensure that the context.LoadedFiles is set when GetLoadedFiles returns. Would that work?

{
await OnSourceFileAdded(sessionId, source, context, token);
}
Expand Down
35 changes: 34 additions & 1 deletion src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ internal enum CmdVM {
VmReadMemory = 16,
VmWriteMemory = 17,
GetAssemblyByName = 18,
GetModuleByGUID = 19
GetModuleByGUID = 19,
GetAssemblyAndPdbBytes = 20
}

internal enum CmdFrame {
Expand Down Expand Up @@ -747,6 +748,9 @@ internal sealed class MonoSDBHelper
private static int MINOR_VERSION = 61;
private static int MAJOR_VERSION = 2;

private int VmMinorVersion { get; set; }
private int VmMajorVersion { get; set; }

private Dictionary<int, MethodInfoWithDebugInformation> methods;
private Dictionary<int, AssemblyInfo> assemblies;
private Dictionary<int, TypeInfoWithDebugInformation> types;
Expand All @@ -769,6 +773,8 @@ public MonoSDBHelper(MonoProxy proxy, ILogger logger, SessionId sessionId)
this.proxy = proxy;
this.logger = logger;
this.sessionId = sessionId;
this.VmMajorVersion = -1;
this.VmMinorVersion = -1;
ResetStore(null);
}

Expand Down Expand Up @@ -885,6 +891,18 @@ public void ClearCache()
pointerValues = new Dictionary<int, PointerValue>();
}

public async Task<(int, int)> GetVMVersion(CancellationToken token)
{
if (VmMajorVersion != -1)
return (VmMajorVersion, VmMinorVersion);
using var commandParamsWriter = new MonoBinaryWriter();
using var retDebuggerCmdReader = await SendDebuggerAgentCommand(CmdVM.Version, commandParamsWriter, token);
retDebuggerCmdReader.ReadString(); //vm version
VmMajorVersion = retDebuggerCmdReader.ReadInt32();
VmMinorVersion = retDebuggerCmdReader.ReadInt32();
return (VmMajorVersion, VmMinorVersion);
}

public async Task<bool> SetProtocolVersion(CancellationToken token)
{
using var commandParamsWriter = new MonoBinaryWriter();
Expand Down Expand Up @@ -2504,6 +2522,21 @@ public async Task<bool> ApplyUpdates(int moduleId, string dmeta, string dil, str
await SendDebuggerAgentCommand(CmdModule.ApplyChanges, commandParamsWriter, token);
return true;
}

public async Task<byte[][]> GetBytesFromAssemblyAndPdb(string assemblyName, CancellationToken token)
{
using var commandParamsWriter = new MonoBinaryWriter();
commandParamsWriter.Write(assemblyName);
var retDebuggerCmdReader = await SendDebuggerAgentCommand(CmdVM.GetAssemblyAndPdbBytes, commandParamsWriter, token);
int assembly_size = retDebuggerCmdReader.ReadInt32();
byte[] assembly_buf = retDebuggerCmdReader.ReadBytes(assembly_size);
int pdb_size = retDebuggerCmdReader.ReadInt32();
byte[] pdb_buf = retDebuggerCmdReader.ReadBytes(pdb_size);
byte[][] ret = new byte[2][];
ret[0] = assembly_buf;
ret[1] = pdb_buf;
return ret;
}
}

internal static class HelperExtensions
Expand Down
18 changes: 18 additions & 0 deletions src/mono/wasm/runtime/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ mono_wasm_assembly_already_added (const char *assembly_name)
return 0;
}

const unsigned char *
mono_wasm_get_assembly_bytes (const char *assembly_name, unsigned int *size)
{
if (assembly_count == 0)
return 0;

WasmAssembly *entry = assemblies;
while (entry != NULL) {
if (strcmp (entry->assembly.name, assembly_name) == 0)
{
*size = entry->assembly.size;
return entry->assembly.data;
}
entry = entry->next;
}
return NULL;
}

typedef struct WasmSatelliteAssembly_ WasmSatelliteAssembly;

struct WasmSatelliteAssembly_ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ MONO_API_FUNCTION(MONO_RT_EXTERNAL_ONLY void, mono_assembly_name_free, (MonoAsse

MONO_API_FUNCTION(void, mono_register_bundled_assemblies, (const MonoBundledAssembly **assemblies))
MONO_API_FUNCTION(void, mono_register_symfile_for_assembly, (const char* assembly_name, const mono_byte *raw_contents, int size))
MONO_API_FUNCTION(const mono_byte *, mono_get_symfile_bytes_from_bundle, (const char* assembly_name, int *size))

MONO_API_FUNCTION(void, mono_set_assemblies_path, (const char* path))

Expand Down