-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathRunInTerminalTransport.cs
More file actions
352 lines (301 loc) · 14.7 KB
/
RunInTerminalTransport.cs
File metadata and controls
352 lines (301 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DebugEngineHost;
namespace MICore
{
public class RunInTerminalTransport : StreamTransport
{
private int _debuggerPid;
private StreamReader _pidReader;
private ProcessMonitor _shellProcessMonitor;
private CancellationTokenSource _streamReadPidCancellationTokenSource = new CancellationTokenSource();
private Task _waitForConnection = null;
private StreamWriter _commandStream = null;
private StreamReader _outputStream = null;
private StreamReader _errorStream = null;
public override int DebuggerPid
{
get
{
return _debuggerPid;
}
}
public override async void Init(ITransportCallback transportCallback, LaunchOptions options, Logger logger, HostWaitLoop waitLoop = null)
{
LocalLaunchOptions localOptions = options as LocalLaunchOptions;
Encoding encNoBom = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
string commandPipeName;
string outputPipeName;
string pidPipeName;
List<string> cmdArgs = new List<string>();
string windowtitle = FormattableString.Invariant($"cppdbg: {Path.GetFileName(options.ExePath)}");
if (PlatformUtilities.IsWindows())
{
// Create Windows Named pipes
commandPipeName = Utilities.GetMIEngineTemporaryFilename("In");
outputPipeName = Utilities.GetMIEngineTemporaryFilename("Out");
pidPipeName = Utilities.GetMIEngineTemporaryFilename("Pid");
string errorPipeName = Utilities.GetMIEngineTemporaryFilename("Error");
NamedPipeServerStream inputToDebugger = new NamedPipeServerStream(commandPipeName, PipeDirection.Out, 1, PipeTransmissionMode.Byte);
NamedPipeServerStream outputFromDebugger = new NamedPipeServerStream(outputPipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte);
NamedPipeServerStream errorFromDebugger = new NamedPipeServerStream(errorPipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte);
NamedPipeServerStream pidPipe = new NamedPipeServerStream(pidPipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte);
_pidReader = new StreamReader(pidPipe, encNoBom, false, UnixUtilities.StreamBufferSize);
string thisModulePath = typeof(RunInTerminalTransport).GetTypeInfo().Assembly.ManifestModule.FullyQualifiedName;
string launchCommand = Path.Combine(Path.GetDirectoryName(thisModulePath), "WindowsDebugLauncher.exe");
if (!File.Exists(launchCommand))
{
string errorMessage = string.Format(CultureInfo.CurrentCulture, MICoreResources.Error_InternalFileMissing, launchCommand);
transportCallback.OnStdErrorLine(errorMessage);
transportCallback.OnDebuggerProcessExit(null);
return;
}
cmdArgs.Add(launchCommand);
cmdArgs.Add("--stdin=" + commandPipeName);
cmdArgs.Add("--stdout=" + outputPipeName);
cmdArgs.Add("--stderr=" + errorPipeName);
cmdArgs.Add("--pid=" + pidPipeName);
cmdArgs.Add("--dbgExe=" + localOptions.MIDebuggerPath);
cmdArgs.Add(localOptions.GetMiDebuggerArgs());
_waitForConnection = Task.WhenAll(
inputToDebugger.WaitForConnectionAsync(),
outputFromDebugger.WaitForConnectionAsync(),
errorFromDebugger.WaitForConnectionAsync(),
pidPipe.WaitForConnectionAsync());
_commandStream = new StreamWriter(inputToDebugger, encNoBom);
_outputStream = new StreamReader(outputFromDebugger, encNoBom, false, UnixUtilities.StreamBufferSize);
_errorStream = new StreamReader(errorFromDebugger, encNoBom, false, UnixUtilities.StreamBufferSize);
}
else
{
// Do Linux style pipes
commandPipeName = UnixUtilities.MakeFifo(identifier: "In", logger: logger);
outputPipeName = UnixUtilities.MakeFifo(identifier: "Out", logger: logger);
pidPipeName = UnixUtilities.MakeFifo(identifier: "Pid", logger: logger);
// Create filestreams
FileStream stdInStream = new FileStream(commandPipeName, FileMode.Open);
FileStream stdOutStream = new FileStream(outputPipeName, FileMode.Open);
_pidReader = new StreamReader(new FileStream(pidPipeName, FileMode.Open), encNoBom, false, UnixUtilities.StreamBufferSize);
string debuggerCmd = UnixUtilities.GetDebuggerCommand(localOptions);
// Default working directory is next to the app
string debuggeeDir;
if (Path.IsPathRooted(options.ExePath) && File.Exists(options.ExePath))
{
debuggeeDir = Path.GetDirectoryName(options.ExePath);
}
else
{
// If we don't know where the app is, default to HOME, and if we somehow can't get that, go with the root directory.
debuggeeDir = Environment.GetEnvironmentVariable("HOME");
if (string.IsNullOrEmpty(debuggeeDir))
debuggeeDir = "/";
}
string dbgCmdScript = Path.Combine(Path.GetTempPath(), Utilities.GetMIEngineTemporaryFilename(identifier: "Cmd"));
string launchDebuggerCommand = UnixUtilities.LaunchLocalDebuggerCommand(
debuggeeDir,
commandPipeName,
outputPipeName,
pidPipeName,
dbgCmdScript,
debuggerCmd,
localOptions.GetMiDebuggerArgs());
logger?.WriteTextBlock("DbgCmd:", launchDebuggerCommand);
using (FileStream dbgCmdStream = new FileStream(dbgCmdScript, FileMode.CreateNew))
using (StreamWriter dbgCmdWriter = new StreamWriter(dbgCmdStream, encNoBom) { AutoFlush = true })
{
dbgCmdWriter.WriteLine("#!/usr/bin/env sh");
dbgCmdWriter.Write(launchDebuggerCommand);
dbgCmdWriter.Flush();
}
if (PlatformUtilities.IsOSX())
{
string osxLaunchScript = GetOSXLaunchScript();
// Call osascript with a path to the AppleScript. The apple script takes 2 parameters: a title for the terminal and the launch script.
cmdArgs.Add("/usr/bin/osascript");
cmdArgs.Add(osxLaunchScript);
cmdArgs.Add(FormattableString.Invariant($"\"{windowtitle}\""));
cmdArgs.Add(FormattableString.Invariant($"sh {dbgCmdScript} ;")); // needs a semicolon because this command is running through the launchscript.
}
else
{
cmdArgs.Add("/bin/sh");
cmdArgs.Add(dbgCmdScript);
}
_outputStream = new StreamReader(stdOutStream, encNoBom, false, UnixUtilities.StreamBufferSize);
_commandStream = new StreamWriter(stdInStream, encNoBom);
}
// Do not pass the launchOptions Environment entries as those are used for the debuggee only.
RunInTerminalLauncher launcher = new RunInTerminalLauncher(windowtitle, new List<EnvironmentEntry>(0).AsReadOnly());
launcher.Launch(
cmdArgs,
localOptions.UseExternalConsole,
LaunchSuccess,
(error) =>
{
transportCallback.OnStdErrorLine(error);
throw new InvalidOperationException(error);
},
logger);
logger?.WriteLine("Wait for connection completion.");
if (_waitForConnection != null)
{
// Add a timeout for waiting for connection - 20 seconds
Task waitOrTimeout = Task.WhenAny(_waitForConnection, Task.Delay(20000));
await waitOrTimeout;
if (waitOrTimeout.Status != TaskStatus.RanToCompletion)
{
string errorMessage = String.Format(CultureInfo.CurrentCulture, MICoreResources.Error_DebuggerInitializeFailed_NoStdErr, "WindowsDebugLauncher.exe");
transportCallback.OnStdErrorLine(errorMessage);
transportCallback.OnDebuggerProcessExit(null);
return;
}
}
base.Init(transportCallback, options, logger, waitLoop);
}
private static string GetOSXLaunchScript()
{
string thisModulePath = typeof(RunInTerminalTransport).GetTypeInfo().Assembly.ManifestModule.FullyQualifiedName;
string launchScript = Path.Combine(Path.GetDirectoryName(thisModulePath), "osxlaunchhelper.scpt");
if (!File.Exists(launchScript))
{
string message = string.Format(CultureInfo.CurrentCulture, MICoreResources.Error_InternalFileMissing, launchScript);
throw new FileNotFoundException(message);
}
return launchScript;
}
private void LogDebuggerErrors()
{
if (_errorStream != null)
{
while (!_streamReadPidCancellationTokenSource.IsCancellationRequested)
{
string line = this.GetLineFromStream(_errorStream, _streamReadPidCancellationTokenSource.Token);
if (line == null)
break;
Logger?.WriteTextBlock("dbgerr:", line);
}
}
}
public override void InitStreams(LaunchOptions options, out StreamReader reader, out StreamWriter writer)
{
// Mono seems to stop responding when the debugger sends a large response unless we specify a larger buffer here
writer = _commandStream;
reader = _outputStream;
}
private Action<int> debuggerPidCallback;
public void RegisterDebuggerPidCallback(Action<int> pidCallback)
{
debuggerPidCallback = pidCallback;
}
private void LaunchSuccess(int? pid)
{
if (_pidReader != null)
{
int shellPid;
Task<string> readShellPidTask = _pidReader.ReadLineAsync();
if (readShellPidTask.Wait(TimeSpan.FromSeconds(10)))
{
shellPid = int.Parse(readShellPidTask.Result, CultureInfo.InvariantCulture);
// Used for testing
Logger?.WriteLine(string.Concat("ShellPid=", shellPid));
}
else
{
// Something is wrong because we didn't get the pid of shell
ForceDisposeStreamReader(_pidReader);
Close();
throw new TimeoutException(string.Format(CultureInfo.CurrentCulture, MICoreResources.Error_RunInTerminalFailure, MICoreResources.Error_TimeoutWaitingForConnection));
}
if (!PlatformUtilities.IsWindows())
{
_shellProcessMonitor = new ProcessMonitor(shellPid);
_shellProcessMonitor.ProcessExited += ShellExited;
_shellProcessMonitor.Start();
}
else
{
Process shellProcess = Process.GetProcessById(shellPid);
shellProcess.EnableRaisingEvents = true;
shellProcess.Exited += ShellExited;
}
Task<string> readDebuggerPidTask = _pidReader.ReadLineAsync();
try
{
readDebuggerPidTask.Wait(_streamReadPidCancellationTokenSource.Token);
_debuggerPid = int.Parse(readDebuggerPidTask.Result, CultureInfo.InvariantCulture);
}
catch (OperationCanceledException)
{
// Something is wrong because we didn't get the pid of the debugger
ForceDisposeStreamReader(_pidReader);
Close();
throw new OperationCanceledException(string.Format(CultureInfo.CurrentCulture, MICoreResources.Error_RunInTerminalFailure, MICoreResources.Error_UnableToEstablishConnectionToLauncher));
}
}
if (debuggerPidCallback != null)
{
debuggerPidCallback(_debuggerPid);
}
}
private void ShellExited(object sender, EventArgs e)
{
if (sender is ProcessMonitor)
{
((ProcessMonitor)sender).ProcessExited -= ShellExited;
}
if (sender is Process)
{
((Process)sender).Exited -= ShellExited;
}
Logger?.WriteLine("Shell exited, stop debugging");
this.Callback.OnDebuggerProcessExit(null);
Close();
}
public override void Close()
{
base.Close();
_shellProcessMonitor?.Dispose();
try
{
_commandStream?.Dispose();
if (_outputStream != null)
{
ForceDisposeStreamReader(_outputStream);
}
if (_errorStream != null)
{
ForceDisposeStreamReader(_errorStream);
}
if (_pidReader != null)
{
ForceDisposeStreamReader(_pidReader);
}
}
catch
{ }
}
protected override string GetThreadName()
{
return "MI.VsCodeTerminalTransport";
}
public override bool CanExecuteCommand()
{
return false;
}
public override int ExecuteSyncCommand(string commandDescription, string commandText, int timeout, out string output, out string error)
{
throw new NotImplementedException();
}
}
}