Skip to content

Commit

Permalink
Add a ProjectAssembliesOnly option for the debugger. Prevent `step …
Browse files Browse the repository at this point in the history
…into` System code.
  • Loading branch information
JaneySprings committed Apr 5, 2024
1 parent 0d0d882 commit 6632b50
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 23 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
"minimum": 0,
"description": "%configuration.description.debuggerOptions.ellipsizedLength%"
},
"dotnetMeteor.debuggerOptions.projectAssembliesOnly": {
"type": "boolean",
"default": true,
"description": "%configuration.description.debuggerOptions.projectAssembliesOnly%"
},
"dotnetMeteor.debuggerOptions.integerDisplayFormat": {
"type": "string",
"default": "Decimal",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@
"configuration.description.debuggerOptions.currentExceptionTag": "Specifies the display name of the current exception in the debugging window.",
"configuration.description.debuggerOptions.ellipsizeStrings": "Truncates strings in the debugging window.",
"configuration.description.debuggerOptions.ellipsizedLength": "The maximum length of a truncated string.",
"configuration.description.debuggerOptions.projectAssembliesOnly": "Specifies whether the debugger should consider only project assemblies as user code.",
"configuration.description.debuggerOptions.integerDisplayFormat": "Specifies the format of displayed integers in the debugging window."
}
6 changes: 2 additions & 4 deletions src/DotNet.Meteor.Debug/Agents/BaseLaunchAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
using DotNet.Meteor.Processes;
using Microsoft.VisualStudio.Shared.VSCodeDebugProtocol.Messages;
using Mono.Debugging.Soft;
using NLog;
using DebuggerLoggingService = Mono.Debugging.Client.DebuggerLoggingService;

namespace DotNet.Meteor.Debug;

public abstract class BaseLaunchAgent {
private readonly Logger sessionLogger = LogManager.GetCurrentClassLogger();

public const string CommandPrefix = "/";
public const string LanguageSeparator = "!";
public List<Action> Disposables { get; init; }
Expand All @@ -28,7 +26,7 @@ public virtual void HandleCommand(string command, IProcessLogger logger) {}
public virtual void Dispose() {
foreach(var disposable in Disposables) {
disposable.Invoke();
sessionLogger.Debug($"Disposing {disposable.Method.Name}");
DebuggerLoggingService.CustomLogger.LogMessage($"Disposing {disposable.Method.Name}");
}

Disposables.Clear();
Expand Down
7 changes: 6 additions & 1 deletion src/DotNet.Meteor.Debug/Agents/DebugLaunchAgent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Net;
using DotNet.Meteor.Debug.Extensions;
using DotNet.Meteor.Debug.Sdb;
Expand All @@ -20,8 +21,12 @@ public override void Connect(SoftDebuggerSession session) {
else if (Configuration.Device.IsIPhone || Configuration.Device.IsMacCatalyst)
arguments = new ServerConnectionProvider(IPAddress.Loopback, Configuration.DebugPort, Configuration.Project.Name);

var debuggerStartInfo = new SoftDebuggerStartInfo(arguments);
if (Configuration.DebuggerSessionOptions.ProjectAssembliesOnly)
debuggerStartInfo.SetUserAssemblyNames(Path.GetDirectoryName(Configuration.OutputAssembly));

ArgumentNullException.ThrowIfNull(arguments, "Debugger connection arguments not implemented.");
session.Run(new SoftDebuggerStartInfo(arguments), Configuration.DebuggerSessionOptions);
session.Run(debuggerStartInfo, Configuration.DebuggerSessionOptions);
}
public override void Launch(IProcessLogger logger) {
if (Configuration.Device.IsAndroid)
Expand Down
3 changes: 3 additions & 0 deletions src/DotNet.Meteor.Debug/DebuggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class DebuggerOptions {
[JsonPropertyName("ellipsized_length")]
public int EllipsizedLength { get; set; } = ServerExtensions.DefaultDebuggerOptions.EvaluationOptions.EllipsizedLength;

[JsonPropertyName("project_assemblies_only")]
public bool ProjectAssembliesOnly { get; set; } = ServerExtensions.DefaultDebuggerOptions.ProjectAssembliesOnly;

internal static IntegerDisplayFormat GetIntegerDisplayFormat(string value) {
if (value == Mono.Debugging.Client.IntegerDisplayFormat.Decimal.ToString())
return Mono.Debugging.Client.IntegerDisplayFormat.Decimal;
Expand Down
43 changes: 43 additions & 0 deletions src/DotNet.Meteor.Debug/Extensions/MonoExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Mono.Debugging.Client;
using Mono.Debugging.Soft;
Expand Down Expand Up @@ -53,4 +56,44 @@ public static ObjectValue GetExpressionValue(this StackFrame frame, string expre
options.UseExternalTypeResolver = useExternalTypeResolver;
return frame.GetExpressionValue(expression, options);
}
public static void SetUserAssemblyNames(this SoftDebuggerStartInfo startInfo, string assembliesDirectory) {
var assembliesPaths = Directory.EnumerateFiles(assembliesDirectory, "*.dll");
var files = assembliesPaths.Where(it => File.Exists(Path.ChangeExtension(it, ".pdb")));
if (!files.Any())
return;

var pathMap = new Dictionary<string, string>();
var names = new List<AssemblyName>();

foreach (var file in files) {
try {
using var asm = Mono.Cecil.AssemblyDefinition.ReadAssembly(file);
if (string.IsNullOrEmpty(asm.Name.Name)) {
DebuggerLoggingService.CustomLogger.LogMessage($"Assembly '{file}' has no name");
continue;
}

AssemblyName name = new AssemblyName(asm.Name.FullName);
if (!pathMap.ContainsKey(asm.Name.FullName))
pathMap.Add(asm.Name.FullName, file);

names.Add(name);
DebuggerLoggingService.CustomLogger.LogMessage($"User assembly '{name.Name}' added");
} catch (Exception e) {
DebuggerLoggingService.CustomLogger.LogError($"Error reading assembly '{file}'", e);
}
}

startInfo.UserAssemblyNames = names;
startInfo.AssemblyPathMap = pathMap;
}

public static void WriteSdbCommand(Stream stream, string command) {
byte[] commandBytes = new byte[command.Length + 1];
commandBytes[0] = (byte)command.Length;
for (int i = 0; i < command.Length; i++)
commandBytes[i + 1] = (byte)command[i];

stream.Write(commandBytes, 0, commandBytes.Length);
}
}
1 change: 1 addition & 0 deletions src/DotNet.Meteor.Debug/Extensions/ServerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static class ServerExtensions {
IntegerDisplayFormat = IntegerDisplayFormat.Decimal,
StackFrameFormat = new StackFrameFormat()
},
ProjectAssembliesOnly = true
};

public static int FindFreePort() {
Expand Down
1 change: 1 addition & 0 deletions src/DotNet.Meteor.Debug/LaunchConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private DebuggerSessionOptions GetDebuggerSessionOptions(JToken debuggerJsonToke
debuggerOptions.EvaluationOptions.EllipsizeStrings = options.EllipsizeStrings;
debuggerOptions.EvaluationOptions.EllipsizedLength = options.EllipsizedLength;
debuggerOptions.EvaluationOptions.IntegerDisplayFormat = DebuggerOptions.GetIntegerDisplayFormat(options.IntegerDisplayFormat);
debuggerOptions.ProjectAssembliesOnly = options.ProjectAssembliesOnly;

return debuggerOptions;
}
Expand Down
3 changes: 2 additions & 1 deletion src/DotNet.Meteor.Debug/Sdb/ClientConnectionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Mono.Debugging.Soft;
using System.Net;
using System.Net.Sockets;
using DotNet.Meteor.Debug.Extensions;

namespace DotNet.Meteor.Debug.Sdb;

Expand All @@ -29,7 +30,7 @@ public void EndConnect(IAsyncResult result, out VirtualMachine vm, out string ap
this.client.EndConnect(result);
var stream = this.client.GetStream();

Protocol.WriteCommand(stream, "start debugger: sdb");
MonoExtensions.WriteSdbCommand(stream, "start debugger: sdb");

var transportConnection = new ClientConnection(this.client, stream);
vm = VirtualMachineManager.Connect(transportConnection, null, null);
Expand Down
14 changes: 0 additions & 14 deletions src/DotNet.Meteor.Debug/Sdb/Protocol.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/DotNet.Meteor.Debug/Sdb/ServerConnectionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Mono.Debugging.Soft;
using System.Net;
using System.Net.Sockets;
using DotNet.Meteor.Debug.Extensions;

namespace DotNet.Meteor.Debug.Sdb;

Expand All @@ -28,7 +29,7 @@ public void EndConnect(IAsyncResult result, out VirtualMachine vm, out string ap
var socket = this.listener.EndAcceptSocket(result);
var stream = new NetworkStream(socket);

Protocol.WriteCommand(stream, "start debugger: sdb");
MonoExtensions.WriteSdbCommand(stream, "start debugger: sdb");

var transportConnection = new ServerConnection(this.listener, stream);
vm = VirtualMachineManager.Connect(transportConnection, null, null);
Expand Down
3 changes: 2 additions & 1 deletion src/VSCode.Extension/configurationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export class ConfigurationController {
integer_display_format: ConfigurationController.getSettingOrDefault<string>(res.configIdDebuggerOptionsIntegerDisplayFormat),
current_exception_tag: ConfigurationController.getSettingOrDefault<string>(res.configIdDebuggerOptionsCurrentExceptionTag),
ellipsize_strings: ConfigurationController.getSettingOrDefault<boolean>(res.configIdDebuggerOptionsEllipsizeStrings),
ellipsized_length: ConfigurationController.getSettingOrDefault<number>(res.configIdDebuggerOptionsEllipsizedLength)
ellipsized_length: ConfigurationController.getSettingOrDefault<number>(res.configIdDebuggerOptionsEllipsizedLength),
project_assemblies_only: ConfigurationController.getSettingOrDefault<boolean>(res.configIdDebuggerOptionsProjectAssembliesOnly),
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/VSCode.Extension/resources/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ export const configIdDebuggerOptionsUseExternalTypeResolver = `${configIdDebugge
export const configIdDebuggerOptionsIntegerDisplayFormat = `${configIdDebuggerOptions}.integerDisplayFormat`;
export const configIdDebuggerOptionsCurrentExceptionTag = `${configIdDebuggerOptions}.currentExceptionTag`;
export const configIdDebuggerOptionsEllipsizeStrings = `${configIdDebuggerOptions}.ellipsizeStrings`;
export const configIdDebuggerOptionsEllipsizedLength = `${configIdDebuggerOptions}.ellipsizedLength`;
export const configIdDebuggerOptionsEllipsizedLength = `${configIdDebuggerOptions}.ellipsizedLength`;
export const configIdDebuggerOptionsProjectAssembliesOnly = `${configIdDebuggerOptions}.projectAssembliesOnly`;
1 change: 1 addition & 0 deletions src/VSCode.Extension/tasks/dotnetTaskProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class DotNetTaskProvider implements vscode.TaskProvider {
if (ConfigurationController.isAndroid()) {
builder.appendFix(`-p:AndroidSdkDirectory="${ConfigurationController.androidSdkDirectory}"`);
builder.conditional('-p:EmbedAssembliesIntoApk=true', () => defaultTarget);
builder.conditional('-p:CopyLocalLockFileAssemblies=true', () => !ConfigurationController.profiler && defaultTarget);
builder.conditional('-p:AndroidEnableProfiler=true', () => ConfigurationController.profiler && defaultTarget);
}
if (ConfigurationController.isAppleMobile()) {
Expand Down

0 comments on commit 6632b50

Please sign in to comment.