-
Notifications
You must be signed in to change notification settings - Fork 315
/
AttachVs.cs
315 lines (276 loc) · 10.2 KB
/
AttachVs.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;
namespace Microsoft.TestPlatform.AttachVS;
internal class DebuggerUtility
{
internal static bool AttachVSToProcess(int? pid, int? vsPid)
{
try
{
if (pid == null)
{
Trace($"FAIL: Pid is null.");
return false;
}
var process = Process.GetProcessById(pid.Value);
Trace($"Starting with pid '{pid}({process.ProcessName})', and vsPid '{vsPid}'");
Trace($"Using pid: {pid} to get parent VS.");
var vs = GetVsFromPid(Process.GetProcessById(vsPid ?? process.Id));
if (vs != null)
{
Trace($"Parent VS is {vs.ProcessName} ({vs.Id}).");
AttachTo(process, vs);
return true;
}
Trace($"Parent VS not found, finding the first VS that started.");
var firstVs = Process.GetProcesses()
.Where(p => p.ProcessName == "devenv")
.Select(p =>
{
try
{
return new { Process = p, p.StartTime, p.HasExited };
}
catch
{
return null;
}
})
.Where(p => p != null && !p.HasExited)
.OrderBy(p => p!.StartTime)
.FirstOrDefault();
if (firstVs != null)
{
Trace($"Found VS {firstVs.Process.Id}");
AttachTo(process, firstVs.Process);
return true;
}
Trace("Could not find any started VS.");
}
catch (Exception ex)
{
Trace($"ERROR: {ex}, {ex.StackTrace}");
}
return false;
}
private static void AttachTo(Process process, Process vs)
{
var attached = AttachVs(vs, process.Id);
if (attached)
{
// You won't see this in DebugView++ because at this point VS is already attached and all the output goes into Debug window in VS.
Trace($"SUCCESS: Attached process: {process.ProcessName} ({process.Id})");
}
else
{
Trace($"FAIL: Could not attach process: {process.ProcessName} ({process.Id})");
}
}
private static bool AttachVs(Process vs, int pid)
{
IBindCtx? bindCtx = null;
IRunningObjectTable? runninObjectTable = null;
IEnumMoniker? enumMoniker = null;
try
{
var r = CreateBindCtx(0, out bindCtx);
Marshal.ThrowExceptionForHR(r);
if (bindCtx == null)
{
Trace($"BindCtx is null. Cannot attach VS.");
return false;
}
bindCtx.GetRunningObjectTable(out runninObjectTable);
if (runninObjectTable == null)
{
Trace($"RunningObjectTable is null. Cannot attach VS.");
return false;
}
runninObjectTable.EnumRunning(out enumMoniker);
if (enumMoniker == null)
{
Trace($"EnumMoniker is null. Cannot attach VS.");
return false;
}
var dteSuffix = ":" + vs.Id;
var moniker = new IMoniker[1];
while (enumMoniker.Next(1, moniker, IntPtr.Zero) == 0 && moniker[0] != null)
{
moniker[0].GetDisplayName(bindCtx, null, out string dn);
if (dn.StartsWith("!VisualStudio.DTE.") && dn.EndsWith(dteSuffix))
{
object dbg, lps;
runninObjectTable.GetObject(moniker[0], out object dte);
// The COM object can be busy, we retry few times, hoping that it won't be busy next time.
for (var i = 0; i < 10; i++)
{
try
{
dbg = dte.GetType().InvokeMember("Debugger", BindingFlags.GetProperty, null, dte, null, CultureInfo.InvariantCulture)!;
lps = dbg.GetType().InvokeMember("LocalProcesses", BindingFlags.GetProperty, null, dbg, null, CultureInfo.InvariantCulture)!;
var lpn = (System.Collections.IEnumerator)lps.GetType().InvokeMember("GetEnumerator", BindingFlags.InvokeMethod, null, lps, null, CultureInfo.InvariantCulture)!;
while (lpn.MoveNext())
{
var pn = Convert.ToInt32(lpn.Current.GetType().InvokeMember("ProcessID", BindingFlags.GetProperty, null, lpn.Current, null, CultureInfo.InvariantCulture), CultureInfo.InvariantCulture);
if (pn == pid)
{
lpn.Current.GetType().InvokeMember("Attach", BindingFlags.InvokeMethod, null, lpn.Current, null, CultureInfo.InvariantCulture);
return true;
}
}
}
// Catch the exception if it is COMException coming directly, or coming from methodInvocation, otherwise just let it be.
catch (Exception ex) when (ex is COMException || (ex is TargetInvocationException tie && tie.InnerException is COMException))
{
Trace($"ComException: Retrying in 250ms.\n{ex}");
Thread.Sleep(250);
}
}
Marshal.ReleaseComObject(moniker[0]);
break;
}
Marshal.ReleaseComObject(moniker[0]);
}
return false;
}
finally
{
if (enumMoniker != null)
{
try
{
Marshal.ReleaseComObject(enumMoniker);
}
catch { }
}
if (runninObjectTable != null)
{
try
{
Marshal.ReleaseComObject(runninObjectTable);
}
catch { }
}
if (bindCtx != null)
{
try
{
Marshal.ReleaseComObject(bindCtx);
}
catch { }
}
}
}
private static Process? GetVsFromPid(Process process)
{
var parent = process;
while (!IsVsOrNull(parent))
{
parent = GetParentProcess(parent);
}
return parent;
}
private static bool IsVsOrNull([NotNullWhen(false)] Process? process)
{
if (process == null)
{
Trace("Parent process is null..");
return true;
}
var isVs = process.ProcessName.Equals("devenv", StringComparison.InvariantCultureIgnoreCase);
if (isVs)
{
Trace($"Process {process.ProcessName} ({process.Id}) is VS.");
}
else
{
Trace($"Process {process.ProcessName} ({process.Id}) is not VS.");
}
return isVs;
}
private static bool IsCorrectParent(Process currentProcess, Process parent)
{
try
{
// Parent needs to start before the child, otherwise it might be a different process
// that is just reusing the same PID.
if (parent.StartTime <= currentProcess.StartTime)
{
return true;
}
Trace($"Process {parent.ProcessName} ({parent.Id}) is not a valid parent because it started after the current process.");
}
catch
{
// Access denied or process exited while we were holding the Process object.
}
return false;
}
private static Process? GetParentProcess(Process process)
{
int id = GetParentProcessId(process);
if (id != -1)
{
try
{
var parent = Process.GetProcessById(id);
if (IsCorrectParent(process, parent))
return parent;
}
catch
{
// throws when parent no longer runs
}
}
return null;
static int GetParentProcessId(Process process)
{
try
{
var handle = process.Handle;
var res = NtQueryInformationProcess(handle, 0, out var pbi, Marshal.SizeOf<PROCESS_BASIC_INFORMATION>(), out int size);
var p = res != 0 ? -1 : pbi.InheritedFromUniqueProcessId.ToInt32();
return p;
}
catch
{
return -1;
}
}
}
private static void Trace(string message, [CallerMemberName] string? methodName = null)
{
System.Diagnostics.Trace.WriteLine($"[AttachVS]{methodName}: {message}");
}
[StructLayout(LayoutKind.Sequential)]
private struct PROCESS_BASIC_INFORMATION
{
public readonly IntPtr ExitStatus;
public readonly IntPtr PebBaseAddress;
public readonly IntPtr AffinityMask;
public readonly IntPtr BasePriority;
public readonly IntPtr UniqueProcessId;
public IntPtr InheritedFromUniqueProcessId;
}
[DllImport("ntdll.dll", SetLastError = true)]
private static extern int NtQueryInformationProcess(
IntPtr processHandle,
int processInformationClass,
out PROCESS_BASIC_INFORMATION processInformation,
int processInformationLength,
out int returnLength);
[DllImport("Kernel32")]
private static extern uint GetTickCount();
[DllImport("ole32.dll")]
private static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc);
}