-
Notifications
You must be signed in to change notification settings - Fork 219
/
CygwinFileMapper.cs
298 lines (255 loc) · 11.6 KB
/
CygwinFileMapper.cs
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
// 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.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using MICore;
namespace Microsoft.MIDebugEngine
{
internal class CygwinFilePathMapper
{
private DebuggedProcess _debuggedProcess;
private Dictionary<string, string> _cygwinToWindows;
public CygwinFilePathMapper(DebuggedProcess debuggedProcess)
{
_debuggedProcess = debuggedProcess;
_cygwinToWindows = new Dictionary<string, string>();
}
/// <summary>
/// Maps cygwin paths (/usr/bin) to Windows Paths (C:\User\bin)
///
/// We cache these paths because most of the time it comes from callstacks that we need to provide
/// source file maps.
/// </summary>
/// <param name="origCygwinPath">A string representing the cygwin path.</param>
/// <returns>A string as a full Windows path</returns>
public string MapCygwinToWindows(string origCygwinPath)
{
if (!(_debuggedProcess.LaunchOptions is LocalLaunchOptions))
{
return origCygwinPath;
}
LocalLaunchOptions localLaunchOptions = (LocalLaunchOptions)_debuggedProcess.LaunchOptions;
string cygwinPath = PlatformUtilities.WindowsPathToUnixPath(origCygwinPath);
string windowsPath = cygwinPath;
lock (_cygwinToWindows)
{
if (!_cygwinToWindows.TryGetValue(cygwinPath, out windowsPath))
{
if (!LaunchCygPathAndReadResult(cygwinPath, localLaunchOptions.MIDebuggerPath, convertToWindowsPath: true, out windowsPath))
{
return origCygwinPath;
}
_cygwinToWindows.Add(cygwinPath, windowsPath);
}
}
return windowsPath;
}
/// <summary>
/// Maps Windows paths (C:\User\bin) to Cygwin Paths (/usr/bin)
///
/// Not cached since we only do this for setting the program, symbol, and working dir.
/// </summary>
/// <param name="origWindowsPath">A string representing the Windows path.</param>
/// <returns>A string as a full unix path</returns>
public string MapWindowsToCygwin(string origWindowsPath)
{
if (!(_debuggedProcess.LaunchOptions is LocalLaunchOptions))
{
return origWindowsPath;
}
LocalLaunchOptions localLaunchOptions = (LocalLaunchOptions)_debuggedProcess.LaunchOptions;
string windowsPath = PlatformUtilities.UnixPathToWindowsPath(origWindowsPath);
if (!LaunchCygPathAndReadResult(windowsPath, localLaunchOptions.MIDebuggerPath, convertToWindowsPath: false, out string cygwinPath))
{
return origWindowsPath;
}
return cygwinPath;
}
// There is an issue launching Cygwin apps that if a process is launched using a bitness mismatched console,
// process launch will fail. To avoid that, launch cygpath with its own console. This requires calling CreateProcess
// directly because the creation flags are not exposed in System.Diagnostics.Process.
//
// Return true if successful. False otherwise.
private bool LaunchCygPathAndReadResult(string inputPath, string miDebuggerPath, bool convertToWindowsPath, out string outputPath)
{
outputPath = "";
if (String.IsNullOrEmpty(miDebuggerPath))
{
return false;
}
string cygpathPath = Path.Combine(Path.GetDirectoryName(miDebuggerPath), "cygpath.exe");
if (!File.Exists(cygpathPath))
{
return false;
}
List<IDisposable> disposableHandles = new List<IDisposable>();
try
{
// Create the anonymous pipe that will act as stdout for the cygpath process
SECURITY_ATTRIBUTES pPipeSec = new SECURITY_ATTRIBUTES();
pPipeSec.bInheritHandle = 1;
SafeFileHandle stdoutRead;
SafeFileHandle stdoutWrite;
if (!CreatePipe(out stdoutRead, out stdoutWrite, ref pPipeSec, 4096))
{
Debug.Fail("Unexpected failure CreatePipe in LaunchCygPathAndReadResult");
return false;
}
SetHandleInformation(stdoutRead, HANDLE_FLAGS.INHERIT, 0);
disposableHandles.Add(stdoutRead);
disposableHandles.Add(stdoutWrite);
const int STARTF_USESTDHANDLES = 0x00000100;
const int STARTF_USESHOWWINDOW = 0x00000001;
const int SW_HIDE = 0;
STARTUPINFO startupInfo = new STARTUPINFO();
startupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
startupInfo.hStdOutput = stdoutWrite;
startupInfo.wShowWindow = SW_HIDE;
startupInfo.cb = Marshal.SizeOf(startupInfo);
PROCESS_INFORMATION processInfo = new PROCESS_INFORMATION();
SECURITY_ATTRIBUTES processSecurityAttributes = new SECURITY_ATTRIBUTES();
SECURITY_ATTRIBUTES threadSecurityAttributes = new SECURITY_ATTRIBUTES();
processSecurityAttributes.nLength = Marshal.SizeOf(processSecurityAttributes);
threadSecurityAttributes.nLength = Marshal.SizeOf(threadSecurityAttributes);
const uint DETACHED_PROCESS = 0x00000008;
uint flags = DETACHED_PROCESS;
string command = string.Empty;
if (convertToWindowsPath)
{
// -w, --windows print Windows form of NAMEs (C:\WINNT)
// ex: "C:\\cygwin64\\bin\\cygpath.exe -w " + inputPath,
command = String.Concat(cygpathPath, " -w ", inputPath);
}
else
{
// -u, --unix (default) print Unix form of NAMEs (/cygdrive/c/winnt)
// ex: "C:\\cygwin64\\bin\\cygpath.exe -u " + inputPath,
command = String.Concat(cygpathPath, " -u ", inputPath);
}
if (!CreateProcess(
null,
command,
ref processSecurityAttributes,
ref threadSecurityAttributes,
true,
flags,
IntPtr.Zero,
null,
ref startupInfo,
out processInfo
))
{
Debug.Fail("Launching cygpath for source mapping failed");
return false;
}
SafeFileHandle processSH = new SafeFileHandle(processInfo.hProcess, true);
SafeFileHandle threadSH = new SafeFileHandle(processInfo.hThread, true);
disposableHandles.Add(processSH);
disposableHandles.Add(threadSH);
const int timeout = 5000;
if (WaitForSingleObject(processInfo.hProcess, timeout) != 0)
{
Debug.Fail("cygpath failed to map source file.");
return false;
}
uint exitCode = 0;
if (!GetExitCodeProcess(processInfo.hProcess, out exitCode))
{
Debug.Fail("cygpath failed to get exit code from cygpath.");
return false;
}
if (exitCode != 0)
{
Debug.Fail("cygpath returned error exit code.");
return false;
}
FileStream fs = new FileStream(stdoutRead, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
outputPath = sr.ReadLine();
}
finally
{
foreach (IDisposable h in disposableHandles)
{
h.Dispose();
}
}
return true;
}
#region pinvoke definitions
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct STARTUPINFO
{
public Int32 cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public Int32 dwX;
public Int32 dwY;
public Int32 dwXSize;
public Int32 dwYSize;
public Int32 dwXCountChars;
public Int32 dwYCountChars;
public Int32 dwFillAttribute;
public Int32 dwFlags;
public Int16 wShowWindow;
public Int16 cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public SafeFileHandle hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
private struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool CreateProcess(
string lpApplicationName,
string lpCommandLine,
ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes,
bool bInheritHandles,
uint dwCreationFlags,
IntPtr lpEnvironment,
string lpCurrentDirectory,
[In] ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation
);
[StructLayout(LayoutKind.Sequential)]
private struct SECURITY_ATTRIBUTES
{
public int nLength;
private IntPtr _lpSecurityDescriptor;
public int bInheritHandle;
}
[DllImport("kernel32.dll", SetLastError = true, PreserveSig = true)]
private static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);
[DllImport("kernel32.dll", SetLastError = true, PreserveSig = true)]
private static extern bool CreatePipe(out SafeFileHandle hReadPipe, out SafeFileHandle hWritePipe, ref SECURITY_ATTRIBUTES lpPipeAttributes, uint nSize);
[DllImport("kernel32.dll", SetLastError = true, PreserveSig = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetExitCodeProcess(IntPtr hProcess, out uint lpExitCode);
[DllImport("kernel32.dll", SetLastError = true, PreserveSig = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DuplicateHandle(SafeFileHandle hSourceProcessHandle, SafeFileHandle hSourceHandle, SafeFileHandle hTargetProcessHandle, out SafeFileHandle lpTargetHandle, uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwOptions);
[Flags]
private enum HANDLE_FLAGS : uint
{
None = 0,
INHERIT = 1,
PROTECT_FROM_CLOSE = 2
}
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetHandleInformation(SafeFileHandle hObject, HANDLE_FLAGS dwMask, uint flags);
#endregion
}
}