Skip to content

Commit

Permalink
XML Translation Removal (#1056)
Browse files Browse the repository at this point in the history
Remove the Launch.json to XML conversion for OpenDebugAD7. This will now pull the necessary information that OpenDebugAD7 uses and continue passing it to MICore.
  • Loading branch information
WardenGnaw authored Oct 19, 2020
1 parent 378be00 commit 728f403
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 958 deletions.
6 changes: 6 additions & 0 deletions src/MICore/JsonLaunchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ public partial class LaunchOptions : BaseOptions
[JsonProperty("externalConsole", DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool? ExternalConsole { get; set; }

/// <summary>
/// If true, disables debuggee console redirection that is required for Integrated Terminal support.
/// </summary>
[JsonProperty("avoidWindowsConsoleRedirection", DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool? AvoidWindowsConsoleRedirection { get; set; }

#endregion

#region Constructors
Expand Down
78 changes: 76 additions & 2 deletions src/MICore/LaunchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1805,10 +1805,83 @@ protected void InitializeCommonOptions(Xml.LaunchOptions.BaseLaunchOptions sourc
this.Environment = new ReadOnlyCollection<EnvironmentEntry>(GetEnvironmentEntries(source.Environment));
}

private static List<string> TryAddWindowsDebuggeeConsoleRedirection(List<string> arguments)
{
if (PlatformUtilities.IsWindows()) // Only do this on Windows
{
bool stdInRedirected = false;
bool stdOutRedirected = false;
bool stdErrRedirected = false;

if (arguments != null)
{
foreach (string rawArgument in arguments)
{
string argument = rawArgument.TrimStart();
if (argument.TrimStart().StartsWith("2>", StringComparison.Ordinal))
{
stdErrRedirected = true;
}
if (argument.TrimStart().StartsWith("1>", StringComparison.Ordinal) || argument.TrimStart().StartsWith(">", StringComparison.Ordinal))
{
stdOutRedirected = true;
}
if (argument.TrimStart().StartsWith("0>", StringComparison.Ordinal) || argument.TrimStart().StartsWith("<", StringComparison.Ordinal))
{
stdInRedirected = true;
}
}
}

// If one (or more) are not redirected, then add redirection
if (!stdInRedirected || !stdOutRedirected || !stdErrRedirected)
{
int argLength = arguments.Count;
List<string> argList = new List<string>(argLength + 3);
if (arguments != null)
{
argList.AddRange(arguments);
}

if (!stdErrRedirected)
{
argList.Add("2>CON");
}

if (!stdOutRedirected)
{
argList.Add("1>CON");
}

if (!stdInRedirected)
{
argList.Add("<CON");
}

return argList;
}
}

return arguments;
}

public void InitializeLaunchOptions(Json.LaunchOptions.LaunchOptions launch)
{
this.DebuggerMIMode = ConvertMIModeString(RequireAttribute(launch.MIMode, nameof(launch.MIMode)));
this.ExeArguments = ParseArguments(launch.Args);

List<string> args = launch.Args;

if (Host.GetHostUIIdentifier() == HostUIIdentifier.VSCode &&
HostRunInTerminal.IsRunInTerminalAvailable() &&
!launch.ExternalConsole.GetValueOrDefault(false) &&
string.IsNullOrEmpty(launch.CoreDumpPath) &&
!launch.AvoidWindowsConsoleRedirection.GetValueOrDefault(false) &&
!(this is PipeLaunchOptions)) // Make sure we are not doing a PipeLaunch
{
args = TryAddWindowsDebuggeeConsoleRedirection(args);
}

this.ExeArguments = ParseArguments(args);
this.WorkingDirectory = launch.Cwd ?? String.Empty;

this.CoreDumpPath = launch.CoreDumpPath;
Expand Down Expand Up @@ -2046,7 +2119,8 @@ protected static string ParseArguments(IEnumerable<string> arguments, bool quote
return String.Empty;
}

private static char[] s_ARGUMENT_SEPARATORS = new char[] { ' ', '\t' };
// gdb does not like parenthesis without being quoted
private static char[] s_ARGUMENT_SEPARATORS = { ' ', '\t', '(', ')' };
protected static string QuoteArgument(string arg)
{
// If its not quoted and it has an argument separater, then quote it.
Expand Down
13 changes: 11 additions & 2 deletions src/MIDebugEngine/Engine.Impl/DebuggedProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2226,9 +2226,18 @@ public bool MapCompileTimeSrcToCurrentSrc(string compilerSrc, out string current
if (hostOSCompilerSrc.StartsWith(e.CompileTimePath, comp))
{
var file = hostOSCompilerSrc.Substring(e.CompileTimePath.Length);
if (string.IsNullOrEmpty(file)) // matched the whole directory string
if (string.IsNullOrEmpty(file)) // matched the whole string
{
break; // use default
if (hostOSCompilerSrc.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal) || hostOSCompilerSrc.EndsWith(Path.AltDirectorySeparatorChar.ToString(), StringComparison.Ordinal))
{
break; // directory matched, use default.
}
else
{
// Is a file
currentName = e.EditorPath; // return the matches compile time path
return true;
}
}
// must do the path break at a directory boundry, i.e. at a '\' or '/' char
char firstFilechar = file[0];
Expand Down
125 changes: 35 additions & 90 deletions src/OpenDebugAD7/AD7DebugSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ internal class AD7DebugSession : DebugAdapterBase, IDebugPortNotify2, IDebugEven
private int m_firstStoppingEvent;
private uint m_breakCounter = 0;
private bool m_isAttach;
private bool m_isCoreDump;
private bool m_isStopped = false;
private bool m_isStepping = false;

Expand Down Expand Up @@ -174,7 +173,7 @@ private bool ValidateProgramPath(ref string program)
return true;
}

private void SetCommonDebugSettings(Dictionary<string, JToken> args, out int sourceFileMappings)
private void SetCommonDebugSettings(Dictionary<string, JToken> args)
{
// Save the Just My Code setting. We will set it once the engine is created.
m_sessionConfig.JustMyCode = args.GetValueAsBool("justMyCode").GetValueOrDefault(m_sessionConfig.JustMyCode);
Expand Down Expand Up @@ -210,26 +209,26 @@ private void SetCommonDebugSettings(Dictionary<string, JToken> args, out int sou
m_logger.SetLoggingConfiguration(LoggingCategory.AdapterResponse, traceResponse.Value);
}
}
}

private void SetCommonMISettings(Dictionary<string, JToken> args)
{
string miMode = args.GetValueAsString("MIMode");

sourceFileMappings = 0;
Dictionary<string, string> sourceFileMap = null;
// If MIMode is not provided, set default to GDB.
if (string.IsNullOrEmpty(miMode))
{
args["MIMode"] = "gdb";
}
else
{
dynamic sourceFileMapProperty = args.GetValueAsObject("sourceFileMap");
if (sourceFileMapProperty != null)
// If lldb and there is no miDebuggerPath, set it.
bool hasMiDebuggerPath = args.ContainsKey("miDebuggerPath");
if (miMode == "lldb" && !hasMiDebuggerPath)
{
try
{
sourceFileMap = sourceFileMapProperty.ToObject<Dictionary<string, string>>();
sourceFileMappings = sourceFileMap.Count;
}
catch (Exception e)
{
SendMessageEvent(MessagePrefix.Error, "Configuration for 'sourceFileMap' has a format error and will be ignored.\nException: " + e.Message);
sourceFileMap = null;
}
args["miDebuggerPath"] = MILaunchOptions.GetLLDBMIPath();
}
}
m_pathConverter.m_pathMapper = new PathMapper(sourceFileMap);
}

private ProtocolException VerifyLocalProcessId(string processId, string telemetryEventName, out int pid)
Expand Down Expand Up @@ -310,14 +309,11 @@ private static long FileTimeToPosix(FILETIME ft)

public void BeforeContinue()
{
if (!m_isCoreDump)
{
m_isStepping = false;
m_isStopped = false;
m_variableManager.Reset();
m_frameHandles.Reset();
m_threadFrameEnumInfos.Clear();
}
m_isStepping = false;
m_isStopped = false;
m_variableManager.Reset();
m_frameHandles.Reset();
m_threadFrameEnumInfos.Clear();
}

public void Stopped(IDebugThread2 thread)
Expand Down Expand Up @@ -774,8 +770,7 @@ protected override void HandleLaunchRequestAsync(IRequestResponder<LaunchArgumen
}
}

int sourceFileMappings = 0;
SetCommonDebugSettings(responder.Arguments.ConfigurationProperties, sourceFileMappings: out sourceFileMappings);
SetCommonDebugSettings(responder.Arguments.ConfigurationProperties);

bool success = false;
try
Expand All @@ -790,22 +785,7 @@ protected override void HandleLaunchRequestAsync(IRequestResponder<LaunchArgumen
// Don't convert the workingDirectory string if we are a pipeTransport connection. We are assuming that the user has the correct directory separaters for their target OS
string workingDirectoryString = pipeTransport != null ? workingDirectory : m_pathConverter.ConvertClientPathToDebugger(workingDirectory);

bool debugServerUsed = false;
bool isOpenOCD = false;
bool stopAtEntrypoint;
bool visualizerFileUsed;
string launchOptions = MILaunchOptions.CreateLaunchOptions(
program: program,
workingDirectory: workingDirectoryString,
args: JsonConvert.SerializeObject(responder.Arguments.ConfigurationProperties),
isPipeLaunch: responder.Arguments.ConfigurationProperties.ContainsKey("pipeTransport"),
stopAtEntry: out stopAtEntrypoint,
isCoreDump: out m_isCoreDump,
debugServerUsed: out debugServerUsed,
isOpenOCD: out isOpenOCD,
visualizerFileUsed: out visualizerFileUsed);

m_sessionConfig.StopAtEntrypoint = stopAtEntrypoint;
m_sessionConfig.StopAtEntrypoint = responder.Arguments.ConfigurationProperties.GetValueAsBool("stopAtEntry").GetValueOrDefault(false);

m_processId = Constants.InvalidProcessId;
m_processName = program;
Expand All @@ -816,14 +796,18 @@ protected override void HandleLaunchRequestAsync(IRequestResponder<LaunchArgumen
flags = enum_LAUNCH_FLAGS.LAUNCH_NODEBUG;
}

SetCommonMISettings(responder.Arguments.ConfigurationProperties);

string launchJson = JsonConvert.SerializeObject(responder.Arguments.ConfigurationProperties);

// Then attach
hr = m_engineLaunch.LaunchSuspended(null,
m_port,
program,
null,
null,
null,
launchOptions,
launchJson,
flags,
0,
0,
Expand Down Expand Up @@ -869,18 +853,11 @@ protected override void HandleLaunchRequestAsync(IRequestResponder<LaunchArgumen

var properties = new Dictionary<string, object>(StringComparer.Ordinal);

properties.Add(DebuggerTelemetry.TelemetryIsCoreDump, m_isCoreDump);
if (debugServerUsed)
{
properties.Add(DebuggerTelemetry.TelemetryUsesDebugServer, isOpenOCD ? "openocd" : "other");
}
if (flags.HasFlag(enum_LAUNCH_FLAGS.LAUNCH_NODEBUG))
{
properties.Add(DebuggerTelemetry.TelemetryIsNoDebug, true);
}

properties.Add(DebuggerTelemetry.TelemetryVisualizerFileUsed, visualizerFileUsed);
properties.Add(DebuggerTelemetry.TelemetrySourceFileMappings, sourceFileMappings);
properties.Add(DebuggerTelemetry.TelemetryMIMode, mimode);

DebuggerTelemetry.ReportTimedEvent(telemetryEventName, DateTime.Now - launchStartTime, properties);
Expand Down Expand Up @@ -930,8 +907,6 @@ protected override void HandleAttachRequestAsync(IRequestResponder<AttachArgumen
JObject pipeTransport = responder.Arguments.ConfigurationProperties.GetValueAsObject("pipeTransport");
bool isPipeTransport = (pipeTransport != null);
bool isLocal = string.IsNullOrEmpty(miDebuggerServerAddress) && !isPipeTransport;
bool visualizerFileUsed = false;
int sourceFileMappings = 0;
string mimode = responder.Arguments.ConfigurationProperties.GetValueAsString("MIMode");

if (isLocal)
Expand Down Expand Up @@ -968,11 +943,10 @@ protected override void HandleAttachRequestAsync(IRequestResponder<AttachArgumen
return;
}

SetCommonDebugSettings(responder.Arguments.ConfigurationProperties, sourceFileMappings: out sourceFileMappings);
SetCommonDebugSettings(responder.Arguments.ConfigurationProperties);

string program = responder.Arguments.ConfigurationProperties.GetValueAsString("program");
string executable = null;
string launchOptions = null;
bool success = false;
try
{
Expand All @@ -990,21 +964,6 @@ protected override void HandleAttachRequestAsync(IRequestResponder<AttachArgumen
responder.SetError(CreateProtocolExceptionAndLogTelemetry(telemetryEventName, 1011, "debuggerPath is required for attachTransport."));
return;
}
bool debugServerUsed = false;
bool isOpenOCD = false;
bool stopAtEntrypoint = false;

launchOptions = MILaunchOptions.CreateLaunchOptions(
program: program,
workingDirectory: String.Empty, // No cwd for attach
args: JsonConvert.SerializeObject(responder.Arguments.ConfigurationProperties),
isPipeLaunch: responder.Arguments.ConfigurationProperties.ContainsKey("pipeTransport"),
stopAtEntry: out stopAtEntrypoint,
isCoreDump: out m_isCoreDump,
debugServerUsed: out debugServerUsed,
isOpenOCD: out isOpenOCD,
visualizerFileUsed: out visualizerFileUsed);


if (string.IsNullOrEmpty(program))
{
Expand All @@ -1031,19 +990,6 @@ protected override void HandleAttachRequestAsync(IRequestResponder<AttachArgumen
return;
}

bool debugServerUsed = false;
bool isOpenOCD = false;
bool stopAtEntrypoint = false;
launchOptions = MILaunchOptions.CreateLaunchOptions(
program: program,
workingDirectory: string.Empty,
args: JsonConvert.SerializeObject(responder.Arguments.ConfigurationProperties),
isPipeLaunch: responder.Arguments.ConfigurationProperties.ContainsKey("pipeTransport"),
stopAtEntry: out stopAtEntrypoint,
isCoreDump: out m_isCoreDump,
debugServerUsed: out debugServerUsed,
isOpenOCD: out isOpenOCD,
visualizerFileUsed: out visualizerFileUsed);
executable = program;
m_isAttach = true;
}
Expand All @@ -1054,8 +1000,12 @@ protected override void HandleAttachRequestAsync(IRequestResponder<AttachArgumen
}
m_processName = program ?? string.Empty;

SetCommonMISettings(responder.Arguments.ConfigurationProperties);

string launchJson = JsonConvert.SerializeObject(responder.Arguments.ConfigurationProperties);

// attach
int hr = m_engineLaunch.LaunchSuspended(null, m_port, executable, null, null, null, launchOptions, 0, 0, 0, 0, this, out m_process);
int hr = m_engineLaunch.LaunchSuspended(null, m_port, executable, null, null, null, launchJson, 0, 0, 0, 0, this, out m_process);

if (hr != HRConstants.S_OK)
{
Expand Down Expand Up @@ -1096,8 +1046,6 @@ protected override void HandleAttachRequestAsync(IRequestResponder<AttachArgumen

var properties = new Dictionary<string, object>(StringComparer.Ordinal);
properties.Add(DebuggerTelemetry.TelemetryMIMode, mimode);
properties.Add(DebuggerTelemetry.TelemetryVisualizerFileUsed, visualizerFileUsed);
properties.Add(DebuggerTelemetry.TelemetrySourceFileMappings, sourceFileMappings);

DebuggerTelemetry.ReportTimedEvent(telemetryEventName, DateTime.Now - attachStartTime, properties);
success = true;
Expand Down Expand Up @@ -1252,10 +1200,7 @@ protected override void HandleStepInRequestAsync(IRequestResponder<StepInArgumen
}
catch (AD7Exception e)
{
if (m_isCoreDump)
{
responder.SetError(new ProtocolException(e.Message));
}
responder.SetError(new ProtocolException(e.Message));
}
}

Expand Down
Loading

0 comments on commit 728f403

Please sign in to comment.