Skip to content

Update debug proxy #19600

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

Merged
1 commit merged into from
Mar 5, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,76 @@
using System.Threading.Tasks;
using System.Threading;
using Microsoft.Extensions.Logging;
using System.Runtime.CompilerServices;

namespace WebAssembly.Net.Debugging {
internal class BreakpointRequest {
public string Id { get; private set; }
public string Assembly { get; private set; }
public string File { get; private set; }
public int Line { get; private set; }
public int Column { get; private set; }

JObject request;

public bool IsResolved => Assembly != null;
public List<Breakpoint> Locations { get; } = new List<Breakpoint> ();

public override string ToString () {
return $"BreakpointRequest Assembly: {Assembly} File: {File} Line: {Line} Column: {Column}";
}

public static BreakpointRequest Parse (JObject args, DebugStore store)
public object AsSetBreakpointByUrlResponse ()
=> new { breakpointId = Id, locations = Locations.Select(l => l.Location.AsLocation ()) };

public static BreakpointRequest Parse (string id, JObject args)
{
// Events can potentially come out of order, so DebugStore may not be initialized
// The BP being set in these cases are JS ones, which we can safely ignore
if (args == null || store == null)
return null;
var breakRequest = new BreakpointRequest () {
Id = id,
request = args
};
return breakRequest;
}

var url = args? ["url"]?.Value<string> ();
if (url == null) {
var urlRegex = args?["urlRegex"].Value<string>();
var sourceFile = store?.GetFileByUrlRegex (urlRegex);
public BreakpointRequest Clone ()
=> new BreakpointRequest { Id = Id, request = request };

url = sourceFile?.DotNetUrl;
public bool IsMatch (SourceFile sourceFile)
{
var url = request? ["url"]?.Value<string> ();
if (url == null) {
var urlRegex = request?["urlRegex"].Value<string>();
var regex = new Regex (urlRegex);
return regex.IsMatch (sourceFile.Url.ToString ()) || regex.IsMatch (sourceFile.DocUrl);
}

if (url != null && !url.StartsWith ("dotnet://", StringComparison.Ordinal)) {
var sourceFile = store.GetFileByUrl (url);
url = sourceFile?.DotNetUrl;
}
return sourceFile.Url.ToString () == url || sourceFile.DotNetUrl == url;
}

if (url == null)
return null;
public bool TryResolve (SourceFile sourceFile)
{
if (!IsMatch (sourceFile))
return false;

var parts = ParseDocumentUrl (url);
if (parts.Assembly == null)
return null;
var line = request? ["lineNumber"]?.Value<int> ();
var column = request? ["columnNumber"]?.Value<int> ();

var line = args? ["lineNumber"]?.Value<int> ();
var column = args? ["columnNumber"]?.Value<int> ();
if (line == null || column == null)
return null;
return false;

return new BreakpointRequest () {
Assembly = parts.Assembly,
File = parts.DocumentPath,
Line = line.Value,
Column = column.Value
};
Assembly = sourceFile.AssemblyName;
File = sourceFile.DebuggerFileName;
Line = line.Value;
Column = column.Value;
return true;
}

public bool TryResolve (DebugStore store)
{
if (request == null || store == null)
return false;

return store.AllSources().FirstOrDefault (source => TryResolve (source)) != null;
}

static (string Assembly, string DocumentPath) ParseDocumentUrl (string url)
Expand Down Expand Up @@ -99,7 +119,6 @@ public override string ToString ()
}
}


internal class CliLocation {
public CliLocation (MethodInfo method, int offset)
{
Expand All @@ -111,7 +130,6 @@ public CliLocation (MethodInfo method, int offset)
public int Offset { get; private set; }
}


internal class SourceLocation {
SourceId id;
int line;
Expand Down Expand Up @@ -268,11 +286,26 @@ public MethodInfo (AssemblyInfo assembly, MethodDefinition methodDef, SourceFile
this.source = source;

var sps = methodDef.DebugInformation.SequencePoints;
if (sps != null && sps.Count > 0) {
StartLocation = new SourceLocation (this, sps [0]);
EndLocation = new SourceLocation (this, sps [sps.Count - 1]);
if (sps == null || sps.Count() < 1)
return;

SequencePoint start = sps [0];
SequencePoint end = sps [0];

foreach (var sp in sps) {
if (sp.StartLine < start.StartLine)
start = sp;
else if (sp.StartLine == start.StartLine && sp.StartColumn < start.StartColumn)
start = sp;

if (sp.EndLine > end.EndLine)
end = sp;
else if (sp.EndLine == end.EndLine && sp.EndColumn > end.EndColumn)
end = sp;
}

StartLocation = new SourceLocation (this, start);
EndLocation = new SourceLocation (this, end);
}

public SourceLocation GetLocationByIl (int pos)
Expand Down Expand Up @@ -556,7 +589,7 @@ class DebugItem {
public Task<byte[][]> Data { get; set; }
}

public async Task Load (SessionId sessionId, string [] loaded_files, CancellationToken token)
public async IAsyncEnumerable<SourceFile> Load (SessionId sessionId, string [] loaded_files, [EnumeratorCancellation] CancellationToken token)
{
static bool MatchPdb (string asm, string pdb)
=> Path.ChangeExtension (asm, "pdb") == pdb;
Expand Down Expand Up @@ -585,20 +618,27 @@ static bool MatchPdb (string asm, string pdb)
}

foreach (var step in steps) {
AssemblyInfo assembly = null;
try {
var bytes = await step.Data;
assemblies.Add (new AssemblyInfo (step.Url, bytes[0], bytes[1]));
assembly = new AssemblyInfo (step.Url, bytes [0], bytes [1]);
} catch (Exception e) {
logger.LogDebug ($"Failed to Load {step.Url} ({e.Message})");
}
if (assembly == null)
continue;

assemblies.Add (assembly);
foreach (var source in assembly.Sources)
yield return source;
}
}

public IEnumerable<SourceFile> AllSources ()
=> assemblies.SelectMany (a => a.Sources);

public SourceFile GetFileById (SourceId id)
=> AllSources ().FirstOrDefault (f => f.SourceId.Equals (id));
=> AllSources ().SingleOrDefault (f => f.SourceId.Equals (id));

public AssemblyInfo GetAssemblyByName (string name)
=> assemblies.FirstOrDefault (a => a.Name.Equals (name, StringComparison.InvariantCultureIgnoreCase));
Expand All @@ -612,15 +652,16 @@ static bool Match (SequencePoint sp, SourceLocation start, SourceLocation end)
var spStart = (Line: sp.StartLine - 1, Column: sp.StartColumn - 1);
var spEnd = (Line: sp.EndLine - 1, Column: sp.EndColumn - 1);

if (start.Line > spStart.Line)
if (start.Line > spEnd.Line)
return false;
if (start.Column > spStart.Column && start.Line == sp.StartLine)

if (start.Column > spEnd.Column && start.Line == spEnd.Line)
return false;

if (end.Line < spEnd.Line)
if (end.Line < spStart.Line)
return false;

if (end.Column < spEnd.Column && end.Line == spEnd.Line)
if (end.Column < spStart.Column && end.Line == spStart.Line)
return false;

return true;
Expand All @@ -629,22 +670,25 @@ static bool Match (SequencePoint sp, SourceLocation start, SourceLocation end)
public List<SourceLocation> FindPossibleBreakpoints (SourceLocation start, SourceLocation end)
{
//XXX FIXME no idea what todo with locations on different files
if (start.Id != end.Id)
if (start.Id != end.Id) {
logger.LogDebug ($"FindPossibleBreakpoints: documents differ (start: {start.Id}) (end {end.Id}");
return null;
var src_id = start.Id;
}

var doc = GetFileById (src_id);
var sourceId = start.Id;

var doc = GetFileById (sourceId);

var res = new List<SourceLocation> ();
if (doc == null) {
logger.LogDebug ($"Could not find document {src_id}");
logger.LogDebug ($"Could not find document {sourceId}");
return res;
}

foreach (var m in doc.Methods) {
foreach (var sp in m.methodDef.DebugInformation.SequencePoints) {
if (Match (sp, start, end))
res.Add (new SourceLocation (m, sp));
foreach (var method in doc.Methods) {
foreach (var sequencePoint in method.methodDef.DebugInformation.SequencePoints) {
if (!sequencePoint.IsHidden && Match (sequencePoint, start, end))
res.Add (new SourceLocation (method, sequencePoint));
}
}
return res;
Expand Down Expand Up @@ -674,35 +718,26 @@ static bool Match (SequencePoint sp, int line, int column)
return true;
}

public SourceLocation FindBestBreakpoint (BreakpointRequest req)
public IEnumerable<SourceLocation> FindBreakpointLocations (BreakpointRequest request)
{
var asm = assemblies.FirstOrDefault (a => a.Name.Equals (req.Assembly, StringComparison.OrdinalIgnoreCase));
var src = asm?.Sources?.FirstOrDefault (s => s.DebuggerFileName.Equals (req.File, StringComparison.OrdinalIgnoreCase));
request.TryResolve (this);

if (src == null)
return null;
var asm = assemblies.FirstOrDefault (a => a.Name.Equals (request.Assembly, StringComparison.OrdinalIgnoreCase));
var sourceFile = asm?.Sources?.SingleOrDefault (s => s.DebuggerFileName.Equals (request.File, StringComparison.OrdinalIgnoreCase));

if (sourceFile == null)
yield break;

foreach (var m in src.Methods) {
foreach (var sp in m.methodDef.DebugInformation.SequencePoints) {
foreach (var method in sourceFile.Methods) {
foreach (var sequencePoint in method.methodDef.DebugInformation.SequencePoints) {
//FIXME handle multi doc methods
if (Match (sp, req.Line, req.Column))
return new SourceLocation (m, sp);
if (!sequencePoint.IsHidden && Match (sequencePoint, request.Line, request.Column))
yield return new SourceLocation (method, sequencePoint);
}
}

return null;
}

public string ToUrl (SourceLocation location)
=> location != null ? GetFileById (location.Id).Url : "";

public SourceFile GetFileByUrlRegex (string urlRegex)
{
var regex = new Regex (urlRegex);
return AllSources ().FirstOrDefault (file => regex.IsMatch (file.Url.ToString()) || regex.IsMatch (file.DocUrl));
}

public SourceFile GetFileByUrl (string url)
=> AllSources ().FirstOrDefault (file => file.Url.ToString() == url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,14 @@ void SendEventInternal (SessionId sessionId, string method, JObject args, Cancel

internal void SendResponse (MessageId id, Result result, CancellationToken token)
{
//Log ("verbose", $"sending response: {id}: {result.ToJObject (id)}");
SendResponseInternal (id, result, token);
}

void SendResponseInternal (MessageId id, Result result, CancellationToken token)
{
JObject o = result.ToJObject (id);
if (result.IsErr)
logger.LogError ("sending error response {result}", result);

Send (this.ide, o, token);
}
Expand Down
Loading