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

Update MonoProxy bits to mono@f8d27a4847d9e28f707d6fd9bdf117fcdcc5f33d #24134

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,14 @@ class AssemblyInfo {
readonly List<SourceFile> sources = new List<SourceFile>();
internal string Url { get; }

public AssemblyInfo (string url, byte[] assembly, byte[] pdb)
public AssemblyInfo (IAssemblyResolver resolver, string url, byte[] assembly, byte[] pdb)
{
this.id = Interlocked.Increment (ref next_id);

try {
Url = url;
ReaderParameters rp = new ReaderParameters (/*ReadingMode.Immediate*/);

rp.AssemblyResolver = resolver;
// set ReadSymbols = true unconditionally in case there
// is an embedded pdb then handle ArgumentException
// and assume that if pdb == null that is the cause
Expand All @@ -391,7 +391,6 @@ public AssemblyInfo (string url, byte[] assembly, byte[] pdb)
if (pdb != null)
rp.SymbolStream = new MemoryStream (pdb);
rp.ReadingMode = ReadingMode.Immediate;
rp.InMemory = true;

this.image = ModuleDefinition.ReadModule (new MemoryStream (assembly), rp);
} catch (BadImageFormatException ex) {
Expand All @@ -405,14 +404,14 @@ public AssemblyInfo (string url, byte[] assembly, byte[] pdb)

if (this.image == null) {
ReaderParameters rp = new ReaderParameters (/*ReadingMode.Immediate*/);
rp.AssemblyResolver = resolver;
if (pdb != null) {
rp.ReadSymbols = true;
rp.SymbolReaderProvider = new PdbReaderProvider ();
rp.SymbolStream = new MemoryStream (pdb);
}

rp.ReadingMode = ReadingMode.Immediate;
rp.InMemory = true;

this.image = ModuleDefinition.ReadModule (new MemoryStream (assembly), rp);
}
Expand Down Expand Up @@ -500,19 +499,10 @@ private Uri GetSourceLinkUrl (string document)
return null;
}

private static string GetRelativePath (string relativeTo, string path)
{
var uri = new Uri (relativeTo, UriKind.RelativeOrAbsolute);
var rel = Uri.UnescapeDataString (uri.MakeRelativeUri (new Uri (path, UriKind.RelativeOrAbsolute)).ToString ()).Replace (Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (rel.Contains (Path.DirectorySeparatorChar.ToString ()) == false) {
rel = $".{ Path.DirectorySeparatorChar }{ rel }";
}
return rel;
}

public IEnumerable<SourceFile> Sources
=> this.sources;

public Dictionary<string, TypeInfo> TypesByName => this.typesByName;
public int Id => id;
public string Name => image.Name;

Expand Down Expand Up @@ -588,13 +578,13 @@ async Task<MemoryStream> GetDataAsync (Uri uri, CancellationToken token)
try {
if (uri.IsFile && File.Exists (uri.LocalPath)) {
using (var file = File.Open (SourceUri.LocalPath, FileMode.Open)) {
await file.CopyToAsync (mem, token);
await file.CopyToAsync (mem, token).ConfigureAwait (false);
mem.Position = 0;
}
} else if (uri.Scheme == "http" || uri.Scheme == "https") {
var client = new HttpClient ();
using (var stream = await client.GetStreamAsync (uri)) {
await stream.CopyToAsync (mem, token);
await stream.CopyToAsync (mem, token).ConfigureAwait (false);
mem.Position = 0;
}
}
Expand Down Expand Up @@ -641,18 +631,12 @@ byte[] ComputePdbHash (Stream sourceStream)
if (doc.EmbeddedSource.Length > 0)
return new MemoryStream (doc.EmbeddedSource, false);

MemoryStream mem;

mem = await GetDataAsync (SourceUri, token);
if (mem != null && (!checkHash || CheckPdbHash (ComputePdbHash (mem)))) {
mem.Position = 0;
return mem;
}

mem = await GetDataAsync (SourceLinkUri, token);
if (mem != null && (!checkHash || CheckPdbHash (ComputePdbHash (mem)))) {
mem.Position = 0;
return mem;
foreach (var url in new [] { SourceUri, SourceLinkUri }) {
var mem = await GetDataAsync (url, token).ConfigureAwait (false);
if (mem != null && (!checkHash || CheckPdbHash (ComputePdbHash (mem)))) {
mem.Position = 0;
return mem;
}
}

return MemoryStream.Null;
Expand Down Expand Up @@ -718,11 +702,12 @@ static bool MatchPdb (string asm, string pdb)
}
}

var resolver = new DefaultAssemblyResolver ();
foreach (var step in steps) {
AssemblyInfo assembly = null;
try {
var bytes = await step.Data;
assembly = new AssemblyInfo (step.Url, bytes [0], bytes [1]);
var bytes = await step.Data.ConfigureAwait (false);
assembly = new AssemblyInfo (resolver, step.Url, bytes [0], bytes [1]);
} catch (Exception e) {
logger.LogDebug ($"Failed to load {step.Url} ({e.Message})");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using System.Threading;
Expand Down Expand Up @@ -190,8 +191,11 @@ public static MonoCommands ClearAllBreakpoints ()
public static MonoCommands GetDetails (DotnetObjectId objectId, JToken args = null)
=> new MonoCommands ($"MONO.mono_wasm_get_details ('{objectId}', {(args ?? "{}")})");

public static MonoCommands GetScopeVariables (int scopeId, params int[] vars)
=> new MonoCommands ($"MONO.mono_wasm_get_variables({scopeId}, [ {string.Join (",", vars)} ])");
public static MonoCommands GetScopeVariables (int scopeId, params VarInfo[] vars)
{
var var_ids = vars.Select (v => new { index = v.Index, name = v.Name }).ToArray ();
return new MonoCommands ($"MONO.mono_wasm_get_variables({scopeId}, {JsonConvert.SerializeObject (var_ids)})");
}

public static MonoCommands SetBreakpoint (string assemblyName, uint methodToken, int ilOffset)
=> new MonoCommands ($"MONO.mono_wasm_set_breakpoint (\"{assemblyName}\", {methodToken}, {ilOffset})");
Expand All @@ -204,6 +208,9 @@ public static MonoCommands ReleaseObject (DotnetObjectId objectId)

public static MonoCommands CallFunctionOn (JToken args)
=> new MonoCommands ($"MONO.mono_wasm_call_function_on ({args.ToString ()})");

public static MonoCommands Resume ()
=> new MonoCommands ($"MONO.mono_wasm_debugger_resume ()");
}

internal enum MonoErrorCodes {
Expand Down Expand Up @@ -274,6 +281,7 @@ internal class ExecutionContext {

public List<Frame> CallStack { get; set; }

public string[] LoadedFiles { get; set; }
internal DebugStore store;
public TaskCompletionSource<DebugStore> Source { get; } = new TaskCompletionSource<DebugStore> ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,42 +78,58 @@ public async Task<SyntaxTree> ReplaceVars (SyntaxTree syntaxTree, MonoProxy prox
if (value == null)
throw new Exception ($"The name {var.Identifier.Text} does not exist in the current context");

values.Add (ConvertJSToCSharpType (value ["value"] ["value"].ToString (), value ["value"] ["type"].ToString ()));
values.Add (ConvertJSToCSharpType (value ["value"]));

var updatedMethod = method.AddParameterListParameters (
SyntaxFactory.Parameter (
SyntaxFactory.Identifier (var.Identifier.Text))
.WithType (SyntaxFactory.ParseTypeName (GetTypeFullName(value["value"]["type"].ToString()))));
.WithType (SyntaxFactory.ParseTypeName (GetTypeFullName(value["value"]))));
root = root.ReplaceNode (method, updatedMethod);
}
syntaxTree = syntaxTree.WithRootAndOptions (root, syntaxTree.Options);
return syntaxTree;
}

private object ConvertJSToCSharpType (string v, string type)
private object ConvertJSToCSharpType (JToken variable)
{
var value = variable["value"];
var type = variable["type"].Value<string>();
var subType = variable["subtype"]?.Value<string>();

switch (type) {
case "number":
return Convert.ChangeType (v, typeof (int));
case "string":
return v;
case "string":
return value?.Value<string> ();
case "number":
return value?.Value<double> ();
case "boolean":
return value?.Value<bool> ();
case "object":
if (subType == "null")
return null;
break;
}

throw new Exception ($"Evaluate of this datatype {type} not implemented yet");
}

private string GetTypeFullName (string type)
private string GetTypeFullName (JToken variable)
{
var type = variable["type"].ToString ();
var subType = variable["subtype"]?.Value<string>();
object value = ConvertJSToCSharpType (variable);

switch (type) {
case "number":
return typeof (int).FullName;
case "string":
return typeof (string).FullName;
case "object": {
if (subType == "null")
return variable["className"].Value<string>();
break;
}
default:
return value.GetType ().FullName;
}

throw new Exception ($"Evaluate of this datatype {type} not implemented yet");
}
}

static SyntaxNode GetExpressionFromSyntaxTree (SyntaxTree syntaxTree)
{
CompilationUnitSyntax root = syntaxTree.GetCompilationUnitRoot ();
Expand All @@ -126,6 +142,7 @@ static SyntaxNode GetExpressionFromSyntaxTree (SyntaxTree syntaxTree)
ParenthesizedExpressionSyntax expressionParenthesized = expressionMember.Expression as ParenthesizedExpressionSyntax;
return expressionParenthesized.Expression;
}

internal static async Task<string> CompileAndRunTheExpression (MonoProxy proxy, MessageId msg_id, int scope_id, string expression, CancellationToken token)
{
FindVariableNMethodCall findVarNMethodCall = new FindVariableNMethodCall ();
Expand Down Expand Up @@ -172,7 +189,6 @@ public string Evaluate()
BindingFlags.Default | BindingFlags.InvokeMethod,
null,
obj,
//new object [] { 10 }
findVarNMethodCall.values.ToArray ());
retString = ret.ToString ();
}
Expand Down
Loading