Skip to content
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
Prev Previous commit
Next Next commit
pushing @radical changes and addressing @radical comments
  • Loading branch information
thaystg committed Nov 24, 2022
commit 49e5dad9026569b19769ab07e55d0906b7ee92cf
69 changes: 32 additions & 37 deletions src/mono/wasm/debugger/BrowserDebugProxy/DebugStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ public bool IsMatch(SourceFile sourceFile)
{
string urlRegex = request?["urlRegex"].Value<string>();
var regex = new Regex(urlRegex);
return regex.IsMatch(sourceFile.Url.ToString()) || regex.IsMatch(sourceFile.DocUrl);
return regex.IsMatch(sourceFile.Url.ToString()) || regex.IsMatch(sourceFile.FilePath);
}

return sourceFile.Url.ToString() == url || sourceFile.DotNetUrl == url;
return sourceFile.Url.ToString() == url || sourceFile.DotNetUrlEscaped == url;
}

public bool TryResolve(SourceFile sourceFile)
Expand All @@ -117,7 +117,7 @@ public bool TryResolve(SourceFile sourceFile)
return false;

Assembly = sourceFile.AssemblyName;
File = sourceFile.DebuggerFileName;
File = sourceFile.FilePath;
Line = line.Value;
Column = column.Value;
return true;
Expand Down Expand Up @@ -327,7 +327,7 @@ internal sealed class MethodInfo

public SourceId SourceId => Source.SourceId;

public string SourceName => Source.DebuggerFileName;
public string SourceName => Source.FilePath;

public string Name { get; }
public MethodDebugInformation DebugInformation;
Expand Down Expand Up @@ -1167,38 +1167,47 @@ internal void UpdatePdbInformation(Stream streamToReadFrom)
}
internal sealed class SourceFile
{
private static readonly Regex regexForEscapeFileName = new(@"([:/])", RegexOptions.Compiled);
private Dictionary<int, MethodInfo> methods;
private AssemblyInfo assembly;
private Document doc;
private DocumentHandle docHandle;
private string url;
internal List<int> BreakableLines { get; }

internal SourceFile(AssemblyInfo assembly, int id, DocumentHandle docHandle, Uri sourceLinkUri, string url)
public string FilePath { get; init; }
public string FileUriEscaped { get; init; }
public string DotNetUrlEscaped { get; init; }

public Uri Url { get; init; }
public Uri SourceLinkUri { get; init; }

public int Id { get; }
public string AssemblyName => assembly.Name;
public SourceId SourceId => new SourceId(assembly.Id, this.Id);
public IEnumerable<MethodInfo> Methods => this.methods.Values;

internal SourceFile(AssemblyInfo assembly, int id, DocumentHandle docHandle, Uri sourceLinkUri, string documentName)
{
this.methods = new Dictionary<int, MethodInfo>();
this.SourceLinkUri = sourceLinkUri;
this.assembly = assembly;
this.Id = id;
this.doc = assembly.pdbMetadataReader.GetDocument(docHandle);
this.docHandle = docHandle;
this.url = url;

this.DebuggerFileName = EscapeAscii(url.Replace("\\", "/"));
this.BreakableLines = new List<int>();

this.FilePath = documentName;

this.SourceUri = new Uri("file://" + DebuggerFileName, UriKind.RelativeOrAbsolute);
if (File.Exists(url))
this.Url = "file:///" + DebuggerFileName;
else
this.Url = DotNetUrl;
string escapedDocumentName = EscapePathForUri(documentName.Replace("\\", "/"));
this.FileUriEscaped = $"file:///{escapedDocumentName}";
this.DotNetUrlEscaped = $"dotnet://{assembly.Name}/{escapedDocumentName}";
this.Url = new Uri(File.Exists(documentName) ? FileUriEscaped : DotNetUrlEscaped, UriKind.Absolute);
}

private static string EscapeAscii(string path)
private static string EscapePathForUri(string path)
{
var builder = new StringBuilder();
foreach (var part in Regex.Split(path, @"([:/])"))
foreach (var part in regexForEscapeFileName.Split(path))
{
if (part == ":" || part == "/")
builder.Append(part);
Expand All @@ -1216,35 +1225,21 @@ internal void AddMethod(MethodInfo mi)
}
}

public string DebuggerFileName { get; }
public string Url { get; }
public int Id { get; }
public string AssemblyName => assembly.Name;
public string DotNetUrl => $"dotnet://{assembly.Name}/{DebuggerFileName}";

public SourceId SourceId => new SourceId(assembly.Id, this.Id);
public Uri SourceLinkUri { get; }
public Uri SourceUri { get; }

public IEnumerable<MethodInfo> Methods => this.methods.Values;

public string DocUrl => url;

public (int startLine, int startColumn, int endLine, int endColumn) GetExtents()
{
MethodInfo start = Methods.OrderBy(m => m.StartLocation.Line).ThenBy(m => m.StartLocation.Column).First();
MethodInfo end = Methods.OrderByDescending(m => m.EndLocation.Line).ThenByDescending(m => m.EndLocation.Column).First();
return (start.StartLocation.Line, start.StartLocation.Column, end.EndLocation.Line, end.EndLocation.Column);
}

private async Task<MemoryStream> GetDataAsync(Uri uri, CancellationToken token)
private static async Task<MemoryStream> GetDataAsync(Uri uri, CancellationToken token)
{
var mem = new MemoryStream();
try
{
if (uri.IsFile && File.Exists(uri.LocalPath))
{
using (FileStream file = File.Open(SourceUri.LocalPath, FileMode.Open))
using (FileStream file = File.Open(uri.LocalPath, FileMode.Open))
{
await file.CopyToAsync(mem, token).ConfigureAwait(false);
mem.Position = 0;
Expand Down Expand Up @@ -1318,7 +1313,7 @@ where reader.GetGuid(cdi.Kind) == PortableCustomDebugInfoKinds.EmbeddedSource
}
}

foreach (Uri url in new[] { SourceUri, SourceLinkUri })
foreach (Uri url in new[] { new Uri(FileUriEscaped), SourceLinkUri })
{
MemoryStream mem = await GetDataAsync(url, token).ConfigureAwait(false);
if (mem != null && mem.Length > 0 && (!checkHash || CheckPdbHash(ComputePdbHash(mem))))
Expand All @@ -1336,11 +1331,11 @@ public object ToScriptSource(int executionContextId, object executionContextAuxD
return new
{
scriptId = SourceId.ToString(),
url = Url,
url = Url.OriginalString,
executionContextId,
executionContextAuxData,
//hash: should be the v8 hash algo, managed implementation is pending
dotNetUrl = DotNetUrl,
dotNetUrl = DotNetUrlEscaped
};
}
}
Expand Down Expand Up @@ -1592,7 +1587,7 @@ public IEnumerable<SourceLocation> FindBreakpointLocations(BreakpointRequest req
request.TryResolve(this);

AssemblyInfo asm = assemblies.FirstOrDefault(a => a.Name.Equals(request.Assembly, StringComparison.OrdinalIgnoreCase));
SourceFile sourceFile = asm?.Sources?.SingleOrDefault(s => s.DebuggerFileName.Equals(request.File, StringComparison.OrdinalIgnoreCase));
SourceFile sourceFile = asm?.Sources?.SingleOrDefault(s => s.FilePath.Equals(request.File, StringComparison.OrdinalIgnoreCase));

if (sourceFile == null)
yield break;
Expand Down Expand Up @@ -1658,6 +1653,6 @@ static List<MethodInfo> FindMethodsContainingLine(SourceFile sourceFile, int lin
}
}

public string ToUrl(SourceLocation location) => location != null ? GetFileById(location.Id).Url : "";
public string ToUrl(SourceLocation location) => location != null ? GetFileById(location.Id).Url.OriginalString : "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ internal override async Task OnSourceFileAdded(SessionId sessionId, SourceFile s
isBlackBoxed = false,
introductionType = "scriptElement",
resourceType = "source",
dotNetUrl = source.DotNetUrl
dotNetUrl = source.DotNetUrlEscaped
});
JObject sourcesJObj;
if (!string.IsNullOrEmpty(ctx.GlobalName))
Expand Down Expand Up @@ -1014,8 +1014,7 @@ internal override async Task<bool> OnGetScriptSource(MessageId msg_id, string sc

try
{
var uri = new Uri(src_file.Url);
string source = $"// Unable to find document {src_file.SourceUri}";
string source = $"// Unable to find document {src_file.FileUriEscaped}";

using (Stream data = await src_file.GetSourceAsync(checkHash: false, token: token))
{
Expand All @@ -1032,7 +1031,7 @@ internal override async Task<bool> OnGetScriptSource(MessageId msg_id, string sc
var o = JObject.FromObject(new
{
source = $"// Unable to read document ({e.Message})\n" +
$"Local path: {src_file?.SourceUri}\n" +
$"Local path: {src_file?.FileUriEscaped}\n" +
$"SourceLink path: {src_file?.SourceLinkUri}\n",
from = script_id
});
Expand Down
9 changes: 4 additions & 5 deletions src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ internal async Task<Result> GetMethodLocation(MessageId id, JObject args, Cancel
return Result.Err($"Method '{typeName}:{methodName}' not found.");
}

string src_url = methodInfo.Assembly.Sources.Single(sf => sf.SourceId == methodInfo.SourceId).Url;
string src_url = methodInfo.Assembly.Sources.Single(sf => sf.SourceId == methodInfo.SourceId).Url.ToString();

return Result.OkFromObject(new
{
Expand Down Expand Up @@ -1330,7 +1330,7 @@ private async Task OnSetEntrypointBreakpoint(SessionId sessionId, JObject args,
logger.LogDebug($"Could not source file {method.SourceName} for method {method.Name} in assembly {assemblyName}");
return;
}
string bpId = $"auto:{method.StartLocation.Line}:{method.StartLocation.Column}:{sourceFile.DotNetUrl}";
string bpId = $"auto:{method.StartLocation.Line}:{method.StartLocation.Column}:{sourceFile.DotNetUrlEscaped}";
BreakpointRequest request = new(bpId, JObject.FromObject(new
{
lineNumber = method.StartLocation.Line,
Expand Down Expand Up @@ -1746,8 +1746,7 @@ internal virtual async Task<bool> OnGetScriptSource(MessageId msg_id, string scr

try
{
var uri = new Uri(src_file.Url);
string source = $"// Unable to find document {src_file.SourceUri}";
string source = $"// Unable to find document {src_file.FileUriEscaped}";

using (Stream data = await src_file.GetSourceAsync(checkHash: false, token: token))
{
Expand All @@ -1764,7 +1763,7 @@ internal virtual async Task<bool> OnGetScriptSource(MessageId msg_id, string scr
var o = new
{
scriptSource = $"// Unable to read document ({e.Message})\n" +
$"Local path: {src_file?.SourceUri}\n" +
$"Local path: {src_file?.FileUriEscaped}\n" +
$"SourceLink path: {src_file?.SourceLinkUri}\n"
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk" InitialTargets="CreateProjectWithColonInSourceName">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(AspNetCoreAppCurrent)</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down Expand Up @@ -98,8 +98,8 @@
Overwrite="true" />
</Target>


<Target Name="CreateProjectWithColonInSourceName"
BeforeTargets="Build"
Condition="!$([MSBuild]::IsOSPlatform('windows'))">
<PropertyGroup>
<CsprojContent>
Expand Down Expand Up @@ -129,5 +129,4 @@
Overwrite="true"/>
</Target>


</Project>