private void MonitorLaunchBrowserMessage(object sender, PreviewProtocolEventEventArgs e) { if (e.Event.Type.Equals("output") && e.Event is OutputEvent message && UrlParserRegex.Matches(message.Output).Count == 1 ) { var vsDebugger = (IVsDebugger2)VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService(typeof(SVsShellDebugger)); var info = new VsDebugTargetInfo2(); var infoSize = Marshal.SizeOf(info); info.cbSize = (uint)infoSize; info.bstrExe = _webBrowserUrl; info.dlo = (uint)_DEBUG_LAUNCH_OPERATION3.DLO_LaunchBrowser; info.LaunchFlags = (uint)__VSDBGLAUNCHFLAGS4.DBGLAUNCH_UseDefaultBrowser | (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_NoDebug; IntPtr infoPtr = Marshal.AllocCoTaskMem(infoSize); Marshal.StructureToPtr(info, infoPtr, false); try { vsDebugger.LaunchDebugTargets2(1, infoPtr); } finally { if (infoPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(infoPtr); } } _adapterHostContext.Events.PreviewProtocolEvent -= MonitorLaunchBrowserMessage; } }
private void CopyDebugTargetInfo(ref VsDebugTargetInfo2 source, ref VsDebugTargetInfo4 destination, uint grfLaunch) { destination.AppPackageLaunchInfo.AppUserModelID = this.deployAppUserModelID; destination.AppPackageLaunchInfo.PackageMoniker = this.deployPackageMoniker; destination.dlo = (uint)_DEBUG_LAUNCH_OPERATION4.DLO_AppPackageDebug; destination.LaunchFlags = grfLaunch; destination.bstrRemoteMachine = "###HOSTNAME_PLACEHOLDER###"; destination.bstrExe = source.bstrExe; destination.bstrArg = source.bstrArg; destination.bstrCurDir = source.bstrCurDir; destination.bstrEnv = source.bstrEnv; destination.dwProcessId = source.dwProcessId; destination.pStartupInfo = IntPtr.Zero; destination.guidLaunchDebugEngine = source.guidLaunchDebugEngine; destination.dwDebugEngineCount = source.dwDebugEngineCount; destination.pDebugEngines = source.pDebugEngines; destination.guidPortSupplier = VSConstants.DebugPortSupplierGuids.NoAuth_guid; destination.bstrPortName = source.bstrPortName; destination.bstrOptions = source.bstrOptions; destination.fSendToOutputWindow = source.fSendToOutputWindow; destination.pUnknown = source.pUnknown; destination.guidProcessLanguage = source.guidProcessLanguage; IVsSolution solution = (IVsSolution)Package.GetGlobalService(typeof(SVsSolution)); IVsHierarchy hierarchy; solution.GetProjectOfUniqueName(GetProjectUniqueName(), out hierarchy); destination.project = hierarchy; }
public static void AttachToProcess(Process[] processes, ChildDebuggingMode mode) { List<VsDebugTargetInfo2> targetList = new List<VsDebugTargetInfo2>(); IntPtr targetsBuffer = IntPtr.Zero; int targetSize = Marshal.SizeOf(typeof(VsDebugTargetInfo2)); int guidSize = Marshal.SizeOf(typeof(Guid)); try { foreach (Process process in processes) { NtProcess ntproc = new NtProcess(process.Id); VsDebugTargetInfo2 target = new VsDebugTargetInfo2(); DebugProcessOptions options = new DebugProcessOptions { ChildDebuggingMode = mode }; target.dwDebugEngineCount = 1; target.dwProcessId = (uint)process.Id; target.dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_AlreadyRunning; if (process.Threads.Count == 1) { // If this is a suspended process, then using DLO_AttachToSuspendedLaunchProcess will // bypass the initial loader breakpoint, causing a seamless and transparent attach. // This is usually the desired behavior, as child processes frequently startup and // shutdown, and it is intrusive to be constantly breaking into the debugger. ProcessThread mainThread = process.Threads[0]; if (mainThread.ThreadState == ThreadState.Wait && mainThread.WaitReason == ThreadWaitReason.Suspended) target.dlo |= (uint)_DEBUG_LAUNCH_OPERATION4.DLO_AttachToSuspendedLaunchProcess; } target.bstrExe = ntproc.Win32ProcessImagePath; target.cbSize = (uint)targetSize; target.bstrCurDir = null; target.guidPortSupplier = DkmIntegration.Guids.PortSupplier.Default; target.LaunchFlags = (uint)(__VSDBGLAUNCHFLAGS.DBGLAUNCH_Silent | __VSDBGLAUNCHFLAGS.DBGLAUNCH_WaitForAttachComplete); target.bstrOptions = options.OptionsString; target.pDebugEngines = Marshal.AllocCoTaskMem(guidSize); Marshal.StructureToPtr(DkmEngineId.NativeEng, target.pDebugEngines, false); targetList.Add(target); } int elementSize = Marshal.SizeOf(typeof(VsDebugTargetInfo2)); targetsBuffer = Marshal.AllocCoTaskMem(targetList.Count * elementSize); for (int i = 0; i < targetList.Count; ++i) { IntPtr writeAddr = targetsBuffer + i * elementSize; Marshal.StructureToPtr(targetList[i], writeAddr, false); } IVsDebugger2 debugger = (IVsDebugger2)VsPackage.GetGlobalService(typeof(SVsShellDebugger)); Core.Logger.Log("Launching {0} debug targets", processes.Length); int hr = debugger.LaunchDebugTargets2((uint)processes.Length, targetsBuffer); if (hr != 0) { IVsUIShell shell = (IVsUIShell)VsPackage.GetGlobalService(typeof(SVsUIShell)); string error; shell.GetErrorInfo(out error); Core.Logger.LogError("An error occured while attaching to process (hr = 0x{0:x}). {1}", hr, error); } } finally { foreach (VsDebugTargetInfo2 target in targetList) { if (target.pDebugEngines != IntPtr.Zero) Marshal.FreeCoTaskMem(target.pDebugEngines); } if (targetsBuffer != IntPtr.Zero) Marshal.FreeCoTaskMem(targetsBuffer); } }
private unsafe void AttachDebugger(Uri uri) { var debugger = (IVsDebugger2)NodejsPackage.GetGlobalService(typeof(SVsShellDebugger)); var debugInfo = new VsDebugTargetInfo2(); var pDebugEngines = stackalloc Guid[1]; pDebugEngines[0] = AD7Engine.DebugEngineGuid; debugInfo.cbSize = (uint)Marshal.SizeOf(typeof(VsDebugTargetInfo2)); debugInfo.dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_AlreadyRunning; debugInfo.guidLaunchDebugEngine = AD7Engine.DebugEngineGuid; debugInfo.dwDebugEngineCount = 1; debugInfo.pDebugEngines = (IntPtr)pDebugEngines; debugInfo.guidPortSupplier = NodeRemoteDebugPortSupplier.PortSupplierGuid; debugInfo.bstrPortName = uri.ToString(); debugInfo.dwProcessId = NodeRemoteDebugProcess.RemoteId; debugInfo.bstrExe = (char)0 + "0x" + debugInfo.dwProcessId.ToString("X"); // this must be set to NUL + process ID in hex when DLO_AlreadyRunning is specified debugInfo.LaunchFlags = 0; var pDebugInfo = stackalloc byte[Marshal.SizeOf(debugInfo)]; Marshal.StructureToPtr(debugInfo, (IntPtr)pDebugInfo, false); Marshal.ThrowExceptionForHR(debugger.LaunchDebugTargets2(1, (IntPtr)pDebugInfo)); }
int IVsQueryDebuggableProjectCfg.QueryDebugTargets(uint grfLaunch, uint cTargets, VsDebugTargetInfo2[] rgDebugTargetInfo, uint[] pcActual) { var project = PythonConfig; if (pcActual != null && pcActual.Length > 0) { pcActual[0] = 1; } if (rgDebugTargetInfo != null && rgDebugTargetInfo.Length > 0) { IList <Guid> debugEngineGuids = new Guid[] { VSConstants.DebugEnginesGuids.NativeOnly_guid }; rgDebugTargetInfo[0] = new VsDebugTargetInfo2(); rgDebugTargetInfo[0].bstrExe = project.GetProjectProperty("Name"); rgDebugTargetInfo[0].bstrRemoteMachine = project.GetProjectProperty("RemoteDebugMachine"); rgDebugTargetInfo[0].guidPortSupplier = VSConstants.DebugPortSupplierGuids.NoAuth_guid; rgDebugTargetInfo[0].guidLaunchDebugEngine = debugEngineGuids[0]; rgDebugTargetInfo[0].dwDebugEngineCount = (uint)debugEngineGuids.Count; rgDebugTargetInfo[0].pDebugEngines = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid)) * debugEngineGuids.Count); for (var i = 0; i < debugEngineGuids.Count; i++) { Marshal.StructureToPtr(debugEngineGuids[i], IntPtr.Add(rgDebugTargetInfo[0].pDebugEngines, i * Marshal.SizeOf(typeof(Guid))), false); } } return(VSConstants.S_OK); }
/// <summary> /// Now launch the actual IDE debugger around the given debugger. /// </summary> public void LaunchDebugEngine(string apkPath, DebuggerLib.Debugger debugger, Guid debuggerGuid, int launchFlags, Action<LauncherStates, string> stateUpdate) { var info = new VsDebugTargetInfo2[1]; info[0].cbSize = (uint)Marshal.SizeOf(typeof(VsDebugTargetInfo2)); info[0].dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_CreateProcess; info[0].dwProcessId = 0; info[0].dwReserved = 0; info[0].bstrOptions = debuggerGuid.ToString("B"); info[0].bstrExe = Path.GetFullPath(apkPath); info[0].bstrEnv = ""; info[0].bstrArg = null; info[0].bstrCurDir = Path.GetDirectoryName(apkPath); //info[0].clsidCustom = new Guid(GuidList.items.guidDot42Debugger); info[0].LaunchFlags = (uint)launchFlags | (uint)__VSDBGLAUNCHFLAGS2.DBGLAUNCH_StopAtEntryPoint; info[0].fSendToOutputWindow = 0; info[0].bstrRemoteMachine = null; info[0].guidLaunchDebugEngine = new Guid(GuidList.Strings.guidDot42DebuggerId); info[0].bstrPortName = "Default Dot42 Port"; info[0].guidPortSupplier = new Guid(GuidList.Strings.guidDot42PortSupplierId); //info[0].guidProcessLanguage = new Guid("{3F5162F8-07C6-11D3-9053-00C04FA302A1}"); info[0].pDebugEngines = IntPtr.Zero; info[0].dwDebugEngineCount = 0; info[0].pUnknown = null; info[0].guidProcessLanguage = Guid.Empty; info[0].hStdError = 0; info[0].hStdInput = 0; info[0].hStdOutput = 0; //var engineGuid = new Guid(GuidList.Strings.guidDot42DebuggerId); //var engineArrPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(engineGuid)); //Marshal.StructureToPtr(engineGuid, engineArrPtr, false); //info[0].pDebugEngines = engineArrPtr; //info[0].dwDebugEngineCount = 1; var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VsDebugTargetInfo2))); Marshal.StructureToPtr(info[0], ptr, false); try { var d = Package.GetGlobalService(typeof(IVsDebugger)) as IVsDebugger2; if (d == null) throw new InvalidOperationException("Cannot find IVsDebugger"); var rc = d.LaunchDebugTargets2(1, ptr); ErrorHandler.ThrowOnFailure(rc); } finally { if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); } //if (engineArrPtr != IntPtr.Zero) //{ //Marshal.FreeCoTaskMem(engineArrPtr); //} } }
internal int QueryDebugTargets(out VsDebugTargetInfo2[] targets) { IntPtr queryDebuggableProjectCfgPtr = IntPtr.Zero; targets = null; Guid guid = typeof(IVsQueryDebuggableProjectCfg).GUID; int hr = get_CfgType(ref guid, out queryDebuggableProjectCfgPtr); if (ErrorHandler.Failed(hr)) { return(hr); } object queryDebuggableProjectCfgObject = Marshal.GetObjectForIUnknown(queryDebuggableProjectCfgPtr); if (queryDebuggableProjectCfgObject == null) { return(VSConstants.E_UNEXPECTED); } IVsQueryDebuggableProjectCfg baseQueryDebugbableCfg = queryDebuggableProjectCfgObject as IVsQueryDebuggableProjectCfg; if (baseQueryDebugbableCfg == null) { return(VSConstants.E_UNEXPECTED); } uint[] targetsCountOutput = new uint[1]; hr = baseQueryDebugbableCfg.QueryDebugTargets(0, 0, null, targetsCountOutput); if (ErrorHandler.Failed(hr)) { return(hr); } uint numberOfDebugTargets = targetsCountOutput[0]; targets = new VsDebugTargetInfo2[numberOfDebugTargets]; hr = baseQueryDebugbableCfg.QueryDebugTargets(0, numberOfDebugTargets, targets, null); if (ErrorHandler.Failed(hr)) { return(hr); } if (string.IsNullOrEmpty(targets[0].bstrRemoteMachine)) { MessageBox.Show( Resources.NoRemoteMachine, Resources.ProductTitle, MessageBoxButtons.OK, MessageBoxIcon.Error ); return(VSConstants.E_ABORT); } return(hr); }
internal int QueryDebugTargets(out VsDebugTargetInfo2[] targets) { IntPtr queryDebuggableProjectCfgPtr = IntPtr.Zero; targets = null; Guid guid = typeof(IVsQueryDebuggableProjectCfg).GUID; int hr = get_CfgType(ref guid, out queryDebuggableProjectCfgPtr); if (ErrorHandler.Failed(hr)) { return(hr); } object queryDebuggableProjectCfgObject = Marshal.GetObjectForIUnknown(queryDebuggableProjectCfgPtr); if (queryDebuggableProjectCfgObject == null) { return(VSConstants.E_UNEXPECTED); } IVsQueryDebuggableProjectCfg baseQueryDebugbableCfg = queryDebuggableProjectCfgObject as IVsQueryDebuggableProjectCfg; if (baseQueryDebugbableCfg == null) { return(VSConstants.E_UNEXPECTED); } uint[] targetsCountOutput = new uint[1]; hr = baseQueryDebugbableCfg.QueryDebugTargets(0, 0, null, targetsCountOutput); if (ErrorHandler.Failed(hr)) { return(hr); } uint numberOfDebugTargets = targetsCountOutput[0]; targets = new VsDebugTargetInfo2[numberOfDebugTargets]; hr = baseQueryDebugbableCfg.QueryDebugTargets(0, numberOfDebugTargets, targets, null); if (ErrorHandler.Failed(hr)) { return(hr); } if (string.IsNullOrEmpty(targets[0].bstrRemoteMachine)) { MessageBox.Show( "The project cannot be deployed or debugged because there is no remote machine specified in project settings.", "Node Tools for Visual Studio", MessageBoxButtons.OK, MessageBoxIcon.Error); return(VSConstants.E_ABORT); } return(hr); }
public static int LaunchDebugTargets(this IVsDebugger2 debugger, params DebugTargetInfo[] targets) { VsDebugTargetInfo2[] vstargets = new VsDebugTargetInfo2[targets.Length]; try { for (int i = 0; i < targets.Length; i++) { vstargets[i].bstrArg = targets[i].Arguments; vstargets[i].bstrCurDir = targets[i].CurrentDirectory; vstargets[i].bstrEnv = GetEnvironmentString(targets[i].Environment); vstargets[i].bstrExe = targets[i].Executable; vstargets[i].bstrOptions = targets[i].Options; vstargets[i].bstrPortName = targets[i].PortName; vstargets[i].bstrRemoteMachine = targets[i].RemoteMachine; vstargets[i].cbSize = (uint)Marshal.SizeOf(typeof(VsDebugTargetInfo2)); vstargets[i].dlo = (uint)targets[i].LaunchOperation; vstargets[i].dwProcessId = targets[i].ProcessId; vstargets[i].dwReserved = 0; vstargets[i].fSendToOutputWindow = targets[i].SendToOutputWindow ? 1 : 0; vstargets[i].guidLaunchDebugEngine = Guid.Empty; vstargets[i].guidPortSupplier = targets[i].PortSupplier; vstargets[i].guidProcessLanguage = targets[i].ProcessLanguage; //vstargets[i].hStdError = targets[i].StdError; //vstargets[i].hStdInput = targets[i].StdInput; //vstargets[i].hStdOutput = targets[i].StdOutput; vstargets[i].LaunchFlags = (uint)targets[i].LaunchFlags; vstargets[i].pUnknown = null; vstargets[i].dwDebugEngineCount = (uint)targets[i].DebugEngines.Length; vstargets[i].pDebugEngines = Marshal.AllocHGlobal(targets[i].DebugEngines.Length * Marshal.SizeOf(typeof(Guid))); for (int j = 0; j < targets[i].DebugEngines.Length; j++) { Marshal.StructureToPtr(targets[i].DebugEngines[j], new IntPtr(vstargets[i].pDebugEngines.ToInt64() + j * Marshal.SizeOf(typeof(Guid))), false); } } return(debugger.LaunchDebugTargets(vstargets)); } finally { for (int i = 0; i < vstargets.Length; i++) { if (vstargets[i].pDebugEngines != IntPtr.Zero) { Marshal.FreeHGlobal(vstargets[i].pDebugEngines); } } } }
protected unsafe override void Handle() { if (!RSession.IsHostRunning) { return; } var debugger = VsAppShell.Current.GetGlobalService <IVsDebugger2>(typeof(SVsShellDebugger)); if (debugger == null) { return; } var pDebugEngines = stackalloc Guid[1]; pDebugEngines[0] = DebuggerGuids.DebugEngine; uint pid = RDebugPortSupplier.GetProcessId(RSession.Id); var debugTarget = new VsDebugTargetInfo2 { cbSize = (uint)Marshal.SizeOf <VsDebugTargetInfo2>(), dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_AlreadyRunning, guidPortSupplier = DebuggerGuids.PortSupplier, bstrPortName = "dummy", // doesn't actually matter, but must not be an empty string guidLaunchDebugEngine = DebuggerGuids.DebugEngine, dwDebugEngineCount = 1, pDebugEngines = (IntPtr)pDebugEngines, //LaunchFlags = (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_DetachOnStop, // R debug port provider represents sessions as pseudo-processes with process ID mapped // to session ID. For LaunchDebugTargets2 to attach a process by ID rather than by name, // dwProcessId must be set accordingly, _and_ bstrExe must be set to \0 + hex ID. dwProcessId = pid, bstrExe = (char)0 + "0x" + pid.ToString("X", CultureInfo.InvariantCulture), }; var pDebugTarget = stackalloc byte[Marshal.SizeOf(debugTarget)]; Marshal.StructureToPtr(debugTarget, (IntPtr)pDebugTarget, false); Marshal.ThrowExceptionForHR(debugger.LaunchDebugTargets2(1, (IntPtr)pDebugTarget)); // If we have successfully attached, VS has switched to debugging UI context, which hides // the REPL window. Show it again and give it focus. _interactiveWorkflow.ActiveWindow?.Container.Show(true); }
public static int LaunchDebugTargets(this IVsDebugger2 debugger, params DebugTargetInfo[] targets) { VsDebugTargetInfo2[] vstargets = new VsDebugTargetInfo2[targets.Length]; try { for (int i = 0; i < targets.Length; i++) { vstargets[i].bstrArg = targets[i].Arguments; vstargets[i].bstrCurDir = targets[i].CurrentDirectory; vstargets[i].bstrEnv = GetEnvironmentString(targets[i].Environment); vstargets[i].bstrExe = targets[i].Executable; vstargets[i].bstrOptions = targets[i].Options; vstargets[i].bstrPortName = targets[i].PortName; vstargets[i].bstrRemoteMachine = targets[i].RemoteMachine; vstargets[i].cbSize = (uint)Marshal.SizeOf(typeof(VsDebugTargetInfo2)); vstargets[i].dlo = (uint)targets[i].LaunchOperation; vstargets[i].dwProcessId = targets[i].ProcessId; vstargets[i].dwReserved = 0; vstargets[i].fSendToOutputWindow = targets[i].SendToOutputWindow ? 1 : 0; vstargets[i].guidLaunchDebugEngine = Guid.Empty; vstargets[i].guidPortSupplier = targets[i].PortSupplier; vstargets[i].guidProcessLanguage = targets[i].ProcessLanguage; //vstargets[i].hStdError = targets[i].StdError; //vstargets[i].hStdInput = targets[i].StdInput; //vstargets[i].hStdOutput = targets[i].StdOutput; vstargets[i].LaunchFlags = (uint)targets[i].LaunchFlags; vstargets[i].pUnknown = null; vstargets[i].dwDebugEngineCount = (uint)targets[i].DebugEngines.Length; vstargets[i].pDebugEngines = Marshal.AllocHGlobal(targets[i].DebugEngines.Length * Marshal.SizeOf(typeof(Guid))); for (int j = 0; j < targets[i].DebugEngines.Length; j++) { Marshal.StructureToPtr(targets[i].DebugEngines[j], new IntPtr(vstargets[i].pDebugEngines.ToInt64() + j * Marshal.SizeOf(typeof(Guid))), false); } } return debugger.LaunchDebugTargets(vstargets); } finally { for (int i = 0; i < vstargets.Length; i++) { if (vstargets[i].pDebugEngines != IntPtr.Zero) Marshal.FreeHGlobal(vstargets[i].pDebugEngines); } } }
protected unsafe override void Handle() { if (!RSession.IsHostRunning) { return; } var debugger = VsAppShell.Current.GetGlobalService <IVsDebugger2>(typeof(SVsShellDebugger)); if (debugger == null) { return; } var pDebugEngines = stackalloc Guid[1]; pDebugEngines[0] = DebuggerGuids.DebugEngine; uint pid = RDebugPortSupplier.GetProcessId(RSession.Id); var debugTarget = new VsDebugTargetInfo2 { cbSize = (uint)Marshal.SizeOf <VsDebugTargetInfo2>(), dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_AlreadyRunning, guidPortSupplier = DebuggerGuids.PortSupplier, bstrPortName = RDebugPortSupplier.PortName, guidLaunchDebugEngine = DebuggerGuids.DebugEngine, dwDebugEngineCount = 1, pDebugEngines = (IntPtr)pDebugEngines, dwProcessId = pid, bstrExe = RDebugPortSupplier.GetExecutableForAttach(pid), }; var pDebugTarget = stackalloc byte[Marshal.SizeOf(debugTarget)]; Marshal.StructureToPtr(debugTarget, (IntPtr)pDebugTarget, false); Marshal.ThrowExceptionForHR(debugger.LaunchDebugTargets2(1, (IntPtr)pDebugTarget)); // If we have successfully attached, VS has switched to debugging UI context, which hides // the REPL window. Show it again and give it focus. _interactiveWorkflow.ActiveWindow?.Container.Show(focus: true, immediate: false); }
protected unsafe override void Handle() { if (!RSession.IsHostRunning) { return; } var debugger = VsAppShell.Current.GetGlobalService<IVsDebugger2>(typeof(SVsShellDebugger)); if (debugger == null) { return; } var pDebugEngines = stackalloc Guid[1]; pDebugEngines[0] = DebuggerGuids.DebugEngine; uint pid = RDebugPortSupplier.GetProcessId(RSession.Id); var debugTarget = new VsDebugTargetInfo2 { cbSize = (uint)Marshal.SizeOf<VsDebugTargetInfo2>(), dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_AlreadyRunning, guidPortSupplier = DebuggerGuids.PortSupplier, bstrPortName = "dummy", // doesn't actually matter, but must not be an empty string guidLaunchDebugEngine = DebuggerGuids.DebugEngine, dwDebugEngineCount = 1, pDebugEngines = (IntPtr)pDebugEngines, //LaunchFlags = (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_DetachOnStop, // R debug port provider represents sessions as pseudo-processes with process ID mapped // to session ID. For LaunchDebugTargets2 to attach a process by ID rather than by name, // dwProcessId must be set accordingly, _and_ bstrExe must be set to \0 + hex ID. dwProcessId = pid, bstrExe = (char)0 + "0x" + pid.ToString("X"), }; var pDebugTarget = stackalloc byte[Marshal.SizeOf(debugTarget)]; Marshal.StructureToPtr(debugTarget, (IntPtr)pDebugTarget, false); Marshal.ThrowExceptionForHR(debugger.LaunchDebugTargets2(1, (IntPtr)pDebugTarget)); // If we have successfully attached, VS has switched to debugging UI context, which hides // the REPL window. Show it again and give it focus. _interactiveWorkflow.ActiveWindow?.Container.Show(true); }
private VsDebugTargetInfo2 CreateTargetInfo(string remotingAddress, IUnitTestRun run) { var runnerPath = GetTaskRunnerPathForRun(run); var runnerArgs = GetTaskRunnerCommandLineArgs(remotingAddress, run.ID); var silverlightDebugEngineGuid = new Guid("032F4B8C-7045-4B24-ACCF-D08C9DA108FE"); var debugTargetInfo = new VsDebugTargetInfo2 { dlo = 1U, bstrExe = runnerPath.FullPath, bstrCurDir = runnerPath.Directory.FullPath, bstrArg = runnerArgs, guidLaunchDebugEngine = silverlightDebugEngineGuid, LaunchFlags = 97U }; debugTargetInfo.cbSize = (uint)Marshal.SizeOf(debugTargetInfo); return(debugTargetInfo); }
private VsDebugTargetInfo2 CreateTargetInfo(IUnitTestRun run) { var runnerPath = GetTaskRunnerPathForRun(run); var runnerArgs = GetTaskRunnerCommandLineArgs(run.ID, port); var silverlightDebugEngineGuid = new Guid("032F4B8C-7045-4B24-ACCF-D08C9DA108FE"); var debugTargetInfo = new VsDebugTargetInfo2 { dlo = 1U, bstrExe = runnerPath.FullPath, bstrCurDir = runnerPath.Directory.FullPath, bstrArg = runnerArgs, guidLaunchDebugEngine = silverlightDebugEngineGuid, LaunchFlags = 97U }; debugTargetInfo.cbSize = (uint)Marshal.SizeOf(debugTargetInfo); return debugTargetInfo; }
int IVsQueryDebuggableProjectCfg.QueryDebugTargets(uint grfLaunch, uint cTargets, VsDebugTargetInfo2[] rgDebugTargetInfo, uint[] pcActual) { string debuggerMachineName; if (pcActual != null && pcActual.Length > 0) { pcActual[0] = 1; } grfLaunch |= (uint)__VSDBGLAUNCHFLAGS140.DBGLAUNCH_ContainsStartupTask; if (rgDebugTargetInfo != null && rgDebugTargetInfo.Length > 0) { IList <Guid> debugEngineGuids = new Guid[] { VSConstants.DebugEnginesGuids.Script_guid }; rgDebugTargetInfo[0] = new VsDebugTargetInfo2(); IVsBuildPropertyStorage bps = this;//.project as IVsBuildPropertyStorage; rgDebugTargetInfo[0].bstrExe = "nodeuwp.exe"; propertiesList.TryGetValue(NodejsUwpConstants.DebuggerMachineName, out debuggerMachineName); rgDebugTargetInfo[0].bstrRemoteMachine = debuggerMachineName; rgDebugTargetInfo[0].guidLaunchDebugEngine = debugEngineGuids[0]; rgDebugTargetInfo[0].guidPortSupplier = NativePortSupplier; rgDebugTargetInfo[0].fSendToOutputWindow = 1; rgDebugTargetInfo[0].dwDebugEngineCount = (uint)debugEngineGuids.Count; rgDebugTargetInfo[0].pDebugEngines = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid)) * debugEngineGuids.Count); for (var i = 0; i < debugEngineGuids.Count; i++) { Marshal.StructureToPtr(debugEngineGuids[i], IntPtr.Add(rgDebugTargetInfo[0].pDebugEngines, i * Marshal.SizeOf(typeof(Guid))), false); } } return(VSConstants.S_OK); }
protected unsafe override void Handle() { if (!RSession.IsHostRunning) { return; } var debugger = VsAppShell.Current.GetGlobalService<IVsDebugger2>(typeof(SVsShellDebugger)); if (debugger == null) { return; } var pDebugEngines = stackalloc Guid[1]; pDebugEngines[0] = DebuggerGuids.DebugEngine; uint pid = RDebugPortSupplier.GetProcessId(RSession.Id); var debugTarget = new VsDebugTargetInfo2 { cbSize = (uint)Marshal.SizeOf<VsDebugTargetInfo2>(), dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_AlreadyRunning, guidPortSupplier = DebuggerGuids.PortSupplier, bstrPortName = RDebugPortSupplier.PortName, guidLaunchDebugEngine = DebuggerGuids.DebugEngine, dwDebugEngineCount = 1, pDebugEngines = (IntPtr)pDebugEngines, dwProcessId = pid, bstrExe = RDebugPortSupplier.GetExecutableForAttach(pid), }; var pDebugTarget = stackalloc byte[Marshal.SizeOf(debugTarget)]; Marshal.StructureToPtr(debugTarget, (IntPtr)pDebugTarget, false); Marshal.ThrowExceptionForHR(debugger.LaunchDebugTargets2(1, (IntPtr)pDebugTarget)); // If we have successfully attached, VS has switched to debugging UI context, which hides // the REPL window. Show it again and give it focus. _interactiveWorkflow.ActiveWindow?.Container.Show(focus: true, immediate: false); }
private void LaunchBrowserDebugger() { var vsDebugger = (IVsDebugger2)VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService(typeof(SVsShellDebugger)); var info = new VsDebugTargetInfo2(); var infoSize = Marshal.SizeOf(info); info.cbSize = (uint)infoSize; info.bstrExe = _webBrowserUrl; info.dlo = (uint)_DEBUG_LAUNCH_OPERATION3.DLO_LaunchBrowser; info.LaunchFlags = (uint)__VSDBGLAUNCHFLAGS4.DBGLAUNCH_UseDefaultBrowser | (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_NoDebug; IntPtr infoPtr = Marshal.AllocCoTaskMem(infoSize); Marshal.StructureToPtr(info, infoPtr, false); try { vsDebugger.LaunchDebugTargets2(1, infoPtr); } finally { if (infoPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(infoPtr); } } }
internal int QueryDebugTargets(out VsDebugTargetInfo2[] targets) { IntPtr queryDebuggableProjectCfgPtr = IntPtr.Zero; targets = null; Guid guid = typeof(IVsQueryDebuggableProjectCfg).GUID; int hr = get_CfgType(ref guid, out queryDebuggableProjectCfgPtr); if (ErrorHandler.Failed(hr)) return hr; object queryDebuggableProjectCfgObject = Marshal.GetObjectForIUnknown(queryDebuggableProjectCfgPtr); if (queryDebuggableProjectCfgObject == null) return VSConstants.E_UNEXPECTED; IVsQueryDebuggableProjectCfg baseQueryDebugbableCfg = queryDebuggableProjectCfgObject as IVsQueryDebuggableProjectCfg; if (baseQueryDebugbableCfg == null) return VSConstants.E_UNEXPECTED; uint[] targetsCountOutput = new uint[1]; hr = baseQueryDebugbableCfg.QueryDebugTargets(0, 0, null, targetsCountOutput); if (ErrorHandler.Failed(hr)) return hr; uint numberOfDebugTargets = targetsCountOutput[0]; targets = new VsDebugTargetInfo2[numberOfDebugTargets]; hr = baseQueryDebugbableCfg.QueryDebugTargets(0, numberOfDebugTargets, targets, null); if (ErrorHandler.Failed(hr)) return hr; if (string.IsNullOrEmpty(targets[0].bstrRemoteMachine)) { MessageBox.Show( "The project cannot be deployed or debugged because there is no remote machine specified in project settings.", "Node Tools for Visual Studio", MessageBoxButtons.OK, MessageBoxIcon.Error); return VSConstants.E_ABORT; } return hr; }
/// <summary> /// Now launch the actual IDE debugger around the given debugger. /// </summary> public void LaunchDebugEngine(string apkPath, DebuggerLib.Debugger debugger, Guid debuggerGuid, int launchFlags, Action <LauncherStates, string> stateUpdate) { var info = new VsDebugTargetInfo2[1]; info[0].cbSize = (uint)Marshal.SizeOf(typeof(VsDebugTargetInfo2)); info[0].dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_CreateProcess; info[0].dwProcessId = 0; info[0].dwReserved = 0; info[0].bstrOptions = debuggerGuid.ToString("B"); info[0].bstrExe = Path.GetFullPath(apkPath); info[0].bstrEnv = ""; info[0].bstrArg = null; info[0].bstrCurDir = Path.GetDirectoryName(apkPath); //info[0].clsidCustom = new Guid(GuidList.items.guidDot42Debugger); info[0].LaunchFlags = (uint)launchFlags | (uint)__VSDBGLAUNCHFLAGS2.DBGLAUNCH_StopAtEntryPoint; info[0].fSendToOutputWindow = 0; info[0].bstrRemoteMachine = null; info[0].guidLaunchDebugEngine = new Guid(GuidList.Strings.guidDot42DebuggerId); info[0].bstrPortName = "Default Dot42 Port"; info[0].guidPortSupplier = new Guid(GuidList.Strings.guidDot42PortSupplierId); //info[0].guidProcessLanguage = new Guid("{3F5162F8-07C6-11D3-9053-00C04FA302A1}"); info[0].pDebugEngines = IntPtr.Zero; info[0].dwDebugEngineCount = 0; info[0].pUnknown = null; info[0].guidProcessLanguage = Guid.Empty; info[0].hStdError = 0; info[0].hStdInput = 0; info[0].hStdOutput = 0; //var engineGuid = new Guid(GuidList.Strings.guidDot42DebuggerId); //var engineArrPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(engineGuid)); //Marshal.StructureToPtr(engineGuid, engineArrPtr, false); //info[0].pDebugEngines = engineArrPtr; //info[0].dwDebugEngineCount = 1; var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VsDebugTargetInfo2))); Marshal.StructureToPtr(info[0], ptr, false); try { var d = Package.GetGlobalService(typeof(IVsDebugger)) as IVsDebugger2; if (d == null) { throw new InvalidOperationException("Cannot find IVsDebugger"); } var rc = d.LaunchDebugTargets2(1, ptr); ErrorHandler.ThrowOnFailure(rc); } finally { if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); } //if (engineArrPtr != IntPtr.Zero) //{ //Marshal.FreeCoTaskMem(engineArrPtr); //} } }
private static uint LaunchDebugger(string exe, string curDir, string projectDir, string args, string machineName, string options, out string error) { var debugger = (IVsDebugger2)HpcSupportPackage.GetGlobalService(typeof(SVsShellDebugger)); VsDebugTargetInfo2 debugInfo = new VsDebugTargetInfo2(); debugInfo.cbSize = (uint)Marshal.SizeOf(typeof(VsDebugTargetInfo2)); debugInfo.dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_Custom; debugInfo.guidLaunchDebugEngine = DebugEngineGuid; debugInfo.dwDebugEngineCount = 1; debugInfo.guidPortSupplier = new Guid("{708C1ECA-FF48-11D2-904F-00C04FA302A1}"); // local port supplier debugInfo.LaunchFlags = (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_WaitForAttachComplete | (uint)__VSDBGLAUNCHFLAGS5.DBGLAUNCH_BreakOneProcess; debugInfo.bstrRemoteMachine = machineName; debugInfo.bstrExe = exe; debugInfo.bstrCurDir = curDir; debugInfo.bstrArg = args; debugInfo.bstrOptions = DirMappingSetting + "=" + projectDir + "|" + curDir; if (!String.IsNullOrWhiteSpace(options)) { debugInfo.bstrOptions += ";" + options; } debugInfo.pDebugEngines = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid))); if (debugInfo.pDebugEngines == IntPtr.Zero) { error = "Out of memory"; return 0; } try { Marshal.StructureToPtr(DebugEngineGuid, debugInfo.pDebugEngines, false); IntPtr memory = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(VsDebugTargetInfo2))); if (memory == IntPtr.Zero) { error = "Out of memory"; return 0; } try { Marshal.StructureToPtr(debugInfo, memory, false); int hr = debugger.LaunchDebugTargets2(1, memory); if (ErrorHandler.Failed(hr)) { var uiShell = (IVsUIShell)HpcSupportPackage.GetGlobalService(typeof(SVsUIShell)); string errorText; if (ErrorHandler.Succeeded(uiShell.GetErrorInfo(out errorText))) { error = errorText; } else { error = "Unknown error"; } OutputState(String.Format("Launching debugger on server failed ({0:X}):\r\n\r\n{1}\r\n", hr, error)); return 0; } else { var structure = (VsDebugTargetInfo2)Marshal.PtrToStructure(memory, typeof(VsDebugTargetInfo2)); error = ""; return structure.dwProcessId; } } finally { Marshal.FreeCoTaskMem(memory); } } finally { //Marshal.FreeCoTaskMem(debugInfo.pDebugEngines); } }
public virtual int QueryDebugTargets(uint grfLaunch, uint cTargets, VsDebugTargetInfo2[] debugTargetInfo, uint[] actualTargets) { if (debugTargetInfo == null) // caller only queries for number of targets { actualTargets[0] = 1; return VSConstants.S_OK; } if (cTargets == 0) { actualTargets[0] = 1; return VSConstants.E_FAIL; } VsDebugTargetInfo targetInfo; targetInfo = GetDebugTargetInfo(grfLaunch, forLaunch: false); // Copying parameters from VsDebugTargetInfo to VsDebugTargetInfo2. // Only relevant fields are copied (that is, those that are set by GetDebugTargetInfo) var targetInfo2 = new VsDebugTargetInfo2() { cbSize = (uint) Marshal.SizeOf(typeof(VsDebugTargetInfo2)), bstrArg = targetInfo.bstrArg, bstrCurDir = targetInfo.bstrCurDir, bstrEnv = targetInfo.bstrEnv, bstrOptions = targetInfo.bstrOptions, bstrExe = targetInfo.bstrExe, bstrPortName = targetInfo.bstrPortName, bstrRemoteMachine = targetInfo.bstrRemoteMachine, fSendToOutputWindow = targetInfo.fSendStdoutToOutputWindow, guidLaunchDebugEngine = targetInfo.clsidCustom, LaunchFlags = targetInfo.grfLaunch, dlo = (uint)targetInfo.dlo, }; targetInfo2.dwDebugEngineCount = 1; // Disposed by caller. targetInfo2.pDebugEngines = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid))); byte[] guidBytes = targetInfo.clsidCustom.ToByteArray(); Marshal.Copy(guidBytes, 0, targetInfo2.pDebugEngines, guidBytes.Length); debugTargetInfo[0] = targetInfo2; if (actualTargets != null) actualTargets[0] = 1; return VSConstants.S_OK; }
private void LaunchBrowserDebugger() { var vsDebugger = (IVsDebugger2)ServiceProvider.GlobalProvider.GetService(typeof(SVsShellDebugger)); ; VsDebugTargetInfo2 info = new VsDebugTargetInfo2(); var infoSize = Marshal.SizeOf(info); info.cbSize = (uint)infoSize; info.bstrExe = _webBrowserUrl; info.dlo = (uint)_DEBUG_LAUNCH_OPERATION3.DLO_LaunchBrowser; info.LaunchFlags = (uint)__VSDBGLAUNCHFLAGS4.DBGLAUNCH_UseDefaultBrowser | (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_NoDebug; info.guidLaunchDebugEngine = DebugEngineGuid; IntPtr infoPtr = Marshal.AllocCoTaskMem(infoSize); Marshal.StructureToPtr(info, infoPtr, false); try { vsDebugger.LaunchDebugTargets2(1, infoPtr); } finally { if (infoPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(infoPtr); } } }
/// <summary> /// </summary> private void onStartDebugging(object sender, EventArgs e) { m_context |= (int)UIContext.AlcStart; DTE dte = (DTE)GetService(typeof(DTE)); IVsOutputWindowPane pane = (IVsOutputWindowPane)Package.GetGlobalService(typeof(SVsGeneralOutputWindowPane)); VCProjectW vcproj; VCConfigurationW cfg; if (!ProjectUtil.getStartupProjectAndConfig(dte, out vcproj, out cfg)) { return; } String exepath = cfg.Evaluate("$(TargetPath)"); String workdir = cfg.Evaluate("$(LocalDebuggerWorkingDirectory)"); String environment = cfg.Evaluate("$(LocalDebuggerEnvironment)"); String platform = cfg.Evaluate("$(Platform)"); String addindir = m_addindir; if (pane != null) { pane.OutputString(exepath); } if (!File.Exists(exepath)) { ShowMessage("Alcantarea: Error", "Executabe " + exepath + " not found"); return; } // プロセスを suspend で起動して DynamicPatcher を inject uint pid = AlcantareaHelper.ExecuteSuspended(exepath, workdir, addindir, environment, platform); if (pid != 0) { // デバッガを attach VsDebugTargetInfo2 info = new VsDebugTargetInfo2(); info.cbSize = (uint)Marshal.SizeOf(info); info.bstrExe = exepath; info.dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_AlreadyRunning; //info.dlo = (uint)_DEBUG_LAUNCH_OPERATION4.DLO_AttachToSuspendedLaunchProcess; // somehow this makes debugger not work info.dwProcessId = pid; info.LaunchFlags = (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_StopDebuggingOnEnd | (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_DetachOnStop; Guid guidDbgEngine = VSConstants.DebugEnginesGuids.ManagedAndNative_guid; IntPtr pGuids = Marshal.AllocCoTaskMem(Marshal.SizeOf(guidDbgEngine)); Marshal.StructureToPtr(guidDbgEngine, pGuids, false); info.pDebugEngines = pGuids; info.dwDebugEngineCount = 1; IVsDebugger2 idbg = (IVsDebugger2)Package.GetGlobalService(typeof(SVsShellDebugger)); IntPtr ptr = Marshal.AllocCoTaskMem((int)info.cbSize); Marshal.StructureToPtr(info, ptr, false); int ret = idbg.LaunchDebugTargets2(1, ptr); // プロセスのスレッドを resume AlcantareaHelper.Resume(pid); } }
public unsafe int DebugLaunch(uint grfLaunch) { try { if (!this.m_project.CanLaunch) { Utility.ShowMessageBox("The project must have either an output type of 'Console Application', or an output type of 'Class Library' and the start action set to a valid .NET MicroFramework application"); return(Utility.COM_HResults.E_FAIL); } //Consider Launching ourselves (at least for device), and then calling Attach. //This would get rid of the ugly dummy thread hack in CorDebugProcess. //However, we would need to jump through some other hoops, like resuming the process, //perhaps setting up the entry point breakpoint ourselves, ..? VsDebugTargetInfo2 vsDebugTargetInfo = new VsDebugTargetInfo2(); DebugPort port = GetDebugPort(); Process processWin32 = null; bool fNoDebug = (__VSDBGLAUNCHFLAGS.DBGLAUNCH_NoDebug & (__VSDBGLAUNCHFLAGS)grfLaunch) != 0; int hRes = Utility.COM_HResults.S_OK; PlatformInfo.Emulator emulatorConfig = GetEmulatorConfig(); if (port == null) { throw new Exception("Cannot find port to deploy to"); } string commandLine = GetCommandLineForLaunch(fNoDebug, emulatorConfig, IsTargetBigEndian()); string exe = port.IsLocalPort ? emulatorConfig.application : typeof(CorDebugProcess).Assembly.Location; if (!fNoDebug) { commandLine = string.Format("{0} \"{1}{2}\"", commandLine, CorDebugProcess.c_DeployDeviceName, this.DeployDeviceName); } //use emulator args even though this may be a device launch. This is needed to store //paths to assemblies. vsDebugTargetInfo.bstrArg = commandLine; vsDebugTargetInfo.bstrCurDir = (string)m_project.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ProjectDir); vsDebugTargetInfo.bstrEnv = null; vsDebugTargetInfo.bstrExe = exe; vsDebugTargetInfo.bstrOptions = null; vsDebugTargetInfo.bstrPortName = DeployTransportName; vsDebugTargetInfo.bstrRemoteMachine = null; vsDebugTargetInfo.cbSize = (uint)Marshal.SizeOf(vsDebugTargetInfo); vsDebugTargetInfo.dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_CreateProcess; vsDebugTargetInfo.dwDebugEngineCount = 0; vsDebugTargetInfo.dwProcessId = 0; vsDebugTargetInfo.dwReserved = 0; vsDebugTargetInfo.fSendToOutputWindow = 0; vsDebugTargetInfo.guidLaunchDebugEngine = CorDebug.s_guidDebugEngine; vsDebugTargetInfo.guidPortSupplier = DebugPortSupplier.s_guidPortSupplier; vsDebugTargetInfo.guidProcessLanguage = Guid.Empty; vsDebugTargetInfo.hStdError = 0; vsDebugTargetInfo.hStdInput = 0; vsDebugTargetInfo.hStdOutput = 0; vsDebugTargetInfo.LaunchFlags = grfLaunch; vsDebugTargetInfo.pDebugEngines = IntPtr.Zero; vsDebugTargetInfo.pUnknown = null; if (fNoDebug) { if (port.IsLocalPort) { processWin32 = new Process(); processWin32.StartInfo = new ProcessStartInfo(); processWin32.StartInfo.FileName = vsDebugTargetInfo.bstrExe; processWin32.StartInfo.Arguments = vsDebugTargetInfo.bstrArg; processWin32.StartInfo.WorkingDirectory = vsDebugTargetInfo.bstrCurDir; processWin32.StartInfo.UseShellExecute = false; processWin32.Start(); } else { RebootAndResumeExecution(); } } else { byte * bpDebugTargetInfo = stackalloc byte[(int)vsDebugTargetInfo.cbSize]; IntPtr ipDebugTargetInfo = (IntPtr)bpDebugTargetInfo; try { IVsDebugger2 iVsDebugger = ( IVsDebugger2 )ServiceProvider.GlobalProvider.GetService(typeof(IVsDebugger)); Marshal.StructureToPtr(vsDebugTargetInfo, ipDebugTargetInfo, false); hRes = iVsDebugger.LaunchDebugTargets2(1, ipDebugTargetInfo); } finally { if (ipDebugTargetInfo != null) { Marshal.DestroyStructure(ipDebugTargetInfo, vsDebugTargetInfo.GetType()); } } } return(hRes); } catch (Exception ex) { Utility.ShowMessageBox(String.Format("An exception occurred while attempting to launch the debugger: {0}", ex.Message)); return(Utility.COM_HResults.E_FAIL); } }
public static void AttachToProcess(Process[] processes, ChildDebuggingMode mode) { List <VsDebugTargetInfo2> targetList = new List <VsDebugTargetInfo2>(); IntPtr targetsBuffer = IntPtr.Zero; int targetSize = Marshal.SizeOf(typeof(VsDebugTargetInfo2)); int guidSize = Marshal.SizeOf(typeof(Guid)); try { foreach (Process process in processes) { NtProcess ntproc = new NtProcess(process.Id); VsDebugTargetInfo2 target = new VsDebugTargetInfo2(); DebugProcessOptions options = new DebugProcessOptions { ChildDebuggingMode = mode }; target.dwDebugEngineCount = 1; target.dwProcessId = (uint)process.Id; target.dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_AlreadyRunning; if (process.Threads.Count == 1) { // If this is a suspended process, then using DLO_AttachToSuspendedLaunchProcess will // bypass the initial loader breakpoint, causing a seamless and transparent attach. // This is usually the desired behavior, as child processes frequently startup and // shutdown, and it is intrusive to be constantly breaking into the debugger. ProcessThread mainThread = process.Threads[0]; if (mainThread.ThreadState == ThreadState.Wait && mainThread.WaitReason == ThreadWaitReason.Suspended) { target.dlo |= (uint)_DEBUG_LAUNCH_OPERATION4.DLO_AttachToSuspendedLaunchProcess; } } target.bstrExe = ntproc.Win32ProcessImagePath; target.cbSize = (uint)targetSize; target.bstrCurDir = null; target.guidPortSupplier = DkmIntegration.Guids.PortSupplier.Default; target.LaunchFlags = (uint)(__VSDBGLAUNCHFLAGS.DBGLAUNCH_Silent | __VSDBGLAUNCHFLAGS.DBGLAUNCH_WaitForAttachComplete); target.bstrOptions = options.OptionsString; target.pDebugEngines = Marshal.AllocCoTaskMem(guidSize); Marshal.StructureToPtr(DkmEngineId.NativeEng, target.pDebugEngines, false); targetList.Add(target); } int elementSize = Marshal.SizeOf(typeof(VsDebugTargetInfo2)); targetsBuffer = Marshal.AllocCoTaskMem(targetList.Count * elementSize); for (int i = 0; i < targetList.Count; ++i) { IntPtr writeAddr = targetsBuffer + i * elementSize; Marshal.StructureToPtr(targetList[i], writeAddr, false); } IVsDebugger2 debugger = (IVsDebugger2)VsPackage.GetGlobalService(typeof(SVsShellDebugger)); Logger.LogInfo("Launching {0} debug targets", processes.Length); int hr = debugger.LaunchDebugTargets2((uint)processes.Length, targetsBuffer); if (hr != 0) { IVsUIShell shell = (IVsUIShell)VsPackage.GetGlobalService(typeof(SVsUIShell)); string error; shell.GetErrorInfo(out error); Logger.LogError("An error occured while attaching to process (hr = 0x{0:x}). {1}", hr, error); } } finally { foreach (VsDebugTargetInfo2 target in targetList) { if (target.pDebugEngines != IntPtr.Zero) { Marshal.FreeCoTaskMem(target.pDebugEngines); } } if (targetsBuffer != IntPtr.Zero) { Marshal.FreeCoTaskMem(targetsBuffer); } } }
private void LaunchBrowserDebugger() { LiveLogger.WriteLine("LaunchBrowserDebugger Started"); var vsDebugger = (IVsDebugger2)ServiceProvider.GlobalProvider.GetService(typeof(SVsShellDebugger)); var info = new VsDebugTargetInfo2(); var infoSize = Marshal.SizeOf(info); info.cbSize = (uint)infoSize; info.bstrExe = _webBrowserUrl; info.dlo = (uint)_DEBUG_LAUNCH_OPERATION3.DLO_LaunchBrowser; var defaultBrowsers = GetDefaultBrowsers(); if (defaultBrowsers.Count != 1 || defaultBrowsers[0].DisplayName != "Internet Explorer") { // if we use UseDefaultBrowser we lose the nice control & debugging of IE, so // instead launch w/ no debugging when the user has selected a browser other than IE. info.LaunchFlags |= (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_StopDebuggingOnEnd | (uint)__VSDBGLAUNCHFLAGS4.DBGLAUNCH_UseDefaultBrowser | (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_NoDebug; } info.guidLaunchDebugEngine = DebugEngineGuid; IntPtr infoPtr = Marshal.AllocCoTaskMem(infoSize); Marshal.StructureToPtr(info, infoPtr, false); try { vsDebugger.LaunchDebugTargets2(1, infoPtr); } finally { if (infoPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(infoPtr); } } LiveLogger.WriteLine("LaunchBrowserDebugger Completed"); }
internal string DoDebugAttach() { if (_eval._attached) { return "Cannot attach to debugger when already attached."; } var pyService = _eval._serviceProvider.GetPythonToolsService(); // Only pass options that make sense for the REPL. var debugOptions = PythonDebugOptions.None; if (pyService.DebuggerOptions.DebugStdLib) { debugOptions |= PythonDebugOptions.DebugStdLib; } PythonProcess debugProcess; using (new StreamLock(this, throwIfDisconnected: true)) { _stream.Write(DebugAttachCommandBytes); debugProcess = PythonProcess.AttachRepl(_stream, _process.Id, _eval.AnalyzerProjectLanguageVersion, debugOptions); } var debugTarget = new VsDebugTargetInfo2(); IntPtr pDebugInfo = IntPtr.Zero, pDebugEngines = IntPtr.Zero; try { debugTarget.guidLaunchDebugEngine = AD7Engine.DebugEngineGuid; debugTarget.dwDebugEngineCount = 1; debugTarget.dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_Custom; debugTarget.bstrExe = debugProcess.ProcessGuid.ToString(); debugTarget.cbSize = (uint)Marshal.SizeOf(typeof(VsDebugTargetInfo2)); debugTarget.bstrCurDir = ""; debugTarget.guidPortSupplier = new Guid("{708C1ECA-FF48-11D2-904F-00C04FA302A1}"); // local port supplier debugTarget.LaunchFlags = (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_WaitForAttachComplete | (uint)__VSDBGLAUNCHFLAGS5.DBGLAUNCH_BreakOneProcess; debugTarget.bstrOptions = AD7Engine.AttachRunning + "=True"; debugTarget.pDebugEngines = pDebugEngines = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid))); Marshal.StructureToPtr(AD7Engine.DebugEngineGuid, debugTarget.pDebugEngines, false); pDebugInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(VsDebugTargetInfo2))); Marshal.StructureToPtr(debugTarget, pDebugInfo, false); var debugger = (IVsDebugger2)_eval._serviceProvider.GetService(typeof(SVsShellDebugger)); int hr = debugger.LaunchDebugTargets2(1, pDebugInfo); if (ErrorHandler.Failed(hr)) { var uiShell = (IVsUIShell)_eval._serviceProvider.GetService(typeof(SVsUIShell)); string errorText; uiShell.GetErrorInfo(out errorText); if (String.IsNullOrWhiteSpace(errorText)) { errorText = "Unknown Error: " + hr; } return errorText; } else { _eval._attached = true; } } finally { Marshal.FreeCoTaskMem(pDebugInfo); Marshal.FreeCoTaskMem(pDebugEngines); } GC.KeepAlive(debugProcess); return null; }
/// <summary> /// </summary> private void onStartDebugging(object sender, EventArgs e) { m_context |= (int)UIContext.AlcStart; DTE dte = (DTE)GetService(typeof(DTE)); IVsOutputWindowPane pane = (IVsOutputWindowPane)Package.GetGlobalService(typeof(SVsGeneralOutputWindowPane)); VCProjectW vcproj; VCConfigurationW cfg; if (!ProjectUtil.getStartupProjectAndConfig(dte, out vcproj, out cfg)) { return; } String exepath = cfg.Evaluate("$(TargetPath)"); String workdir = cfg.Evaluate("$(LocalDebuggerWorkingDirectory)"); String environment = cfg.Evaluate("$(LocalDebuggerEnvironment)"); String platform = cfg.Evaluate("$(Platform)"); String addindir = m_addindir; if (pane!=null) { pane.OutputString(exepath); } if (!File.Exists(exepath)) { ShowMessage("Alcantarea: Error", "Executabe "+exepath+" not found"); return; } // プロセスを suspend で起動して DynamicPatcher を inject uint pid = AlcantareaHelper.ExecuteSuspended(exepath, workdir, addindir, environment, platform); if (pid != 0) { // デバッガを attach VsDebugTargetInfo2 info = new VsDebugTargetInfo2(); info.cbSize = (uint)Marshal.SizeOf(info); info.bstrExe = exepath; info.dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_AlreadyRunning; //info.dlo = (uint)_DEBUG_LAUNCH_OPERATION4.DLO_AttachToSuspendedLaunchProcess; // somehow this makes debugger not work info.dwProcessId = pid; info.LaunchFlags = (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_StopDebuggingOnEnd | (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_DetachOnStop; Guid guidDbgEngine = VSConstants.DebugEnginesGuids.ManagedAndNative_guid; IntPtr pGuids = Marshal.AllocCoTaskMem(Marshal.SizeOf(guidDbgEngine)); Marshal.StructureToPtr(guidDbgEngine, pGuids, false); info.pDebugEngines = pGuids; info.dwDebugEngineCount = 1; IVsDebugger2 idbg = (IVsDebugger2)Package.GetGlobalService(typeof(SVsShellDebugger)); IntPtr ptr = Marshal.AllocCoTaskMem((int)info.cbSize); Marshal.StructureToPtr(info, ptr, false); int ret = idbg.LaunchDebugTargets2(1, ptr); // プロセスのスレッドを resume AlcantareaHelper.Resume(pid); } }
int IVsQueryDebuggableProjectCfg.QueryDebugTargets(uint grfLaunch, uint cTargets, VsDebugTargetInfo2[] rgDebugTargetInfo, uint[] pcActual) { string debuggerMachineName; if (pcActual != null && pcActual.Length > 0) { pcActual[0] = 1; } grfLaunch |= (uint)__VSDBGLAUNCHFLAGS140.DBGLAUNCH_ContainsStartupTask; if (rgDebugTargetInfo != null && rgDebugTargetInfo.Length > 0) { IList<Guid> debugEngineGuids = new Guid[] { VSConstants.DebugEnginesGuids.Script_guid }; rgDebugTargetInfo[0] = new VsDebugTargetInfo2(); IVsBuildPropertyStorage bps = this;//.project as IVsBuildPropertyStorage; rgDebugTargetInfo[0].bstrExe = "nodeuwp.exe"; propertiesList.TryGetValue(NodejsUwpConstants.DebuggerMachineName, out debuggerMachineName); rgDebugTargetInfo[0].bstrRemoteMachine = debuggerMachineName; rgDebugTargetInfo[0].guidLaunchDebugEngine = debugEngineGuids[0]; rgDebugTargetInfo[0].guidPortSupplier = NativePortSupplier; rgDebugTargetInfo[0].fSendToOutputWindow = 1; rgDebugTargetInfo[0].dwDebugEngineCount = (uint)debugEngineGuids.Count; rgDebugTargetInfo[0].pDebugEngines = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid)) * debugEngineGuids.Count); for (var i = 0; i < debugEngineGuids.Count; i++) { Marshal.StructureToPtr(debugEngineGuids[i], IntPtr.Add(rgDebugTargetInfo[0].pDebugEngines, i * Marshal.SizeOf(typeof(Guid))), false); } } return VSConstants.S_OK; }
private static uint LaunchDebugger(string exe, string curDir, string projectDir, string args, string machineName, out string error) { var debugger = (IVsDebugger2)HpcSupportPackage.GetGlobalService(typeof(SVsShellDebugger)); VsDebugTargetInfo2 debugInfo = new VsDebugTargetInfo2(); debugInfo.cbSize = (uint)Marshal.SizeOf(typeof(VsDebugTargetInfo2)); debugInfo.dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_Custom; debugInfo.guidLaunchDebugEngine = AD7Engine.DebugEngineGuid; debugInfo.dwDebugEngineCount = 1; debugInfo.guidPortSupplier = new Guid("{708C1ECA-FF48-11D2-904F-00C04FA302A1}"); // local port supplier debugInfo.LaunchFlags = (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_WaitForAttachComplete | (uint)__VSDBGLAUNCHFLAGS5.DBGLAUNCH_BreakOneProcess; debugInfo.bstrRemoteMachine = machineName; debugInfo.bstrExe = exe; debugInfo.bstrCurDir = curDir; debugInfo.bstrArg = args; debugInfo.bstrOptions = AD7Engine.DirMappingSetting + "=" + projectDir + "|" + curDir + ";" + AD7Engine.RedirectOutputSetting + "=True"; debugInfo.pDebugEngines = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid))); if (debugInfo.pDebugEngines == IntPtr.Zero) { error = "Out of memory"; return(0); } try { Marshal.StructureToPtr(AD7Engine.DebugEngineGuid, debugInfo.pDebugEngines, false); IntPtr memory = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(VsDebugTargetInfo2))); if (memory == IntPtr.Zero) { error = "Out of memory"; return(0); } try { Marshal.StructureToPtr(debugInfo, memory, false); int hr = debugger.LaunchDebugTargets2(1, memory); if (ErrorHandler.Failed(hr)) { var uiShell = (IVsUIShell)HpcSupportPackage.GetGlobalService(typeof(SVsUIShell)); string errorText; if (ErrorHandler.Succeeded(uiShell.GetErrorInfo(out errorText))) { error = errorText; } else { error = "Unknown error"; } return(0); } else { var structure = (VsDebugTargetInfo2)Marshal.PtrToStructure(memory, typeof(VsDebugTargetInfo2)); error = ""; return(structure.dwProcessId); } } finally { Marshal.FreeCoTaskMem(memory); } } finally { //Marshal.FreeCoTaskMem(debugInfo.pDebugEngines); } }
internal string DoDebugAttach() { if (_eval._attached) { return "Cannot attach to debugger when already attached."; } JProcess debugProcess; using (new SocketLock(this)) { Stream.Write(DebugAttachCommandBytes); debugProcess = JProcess.AttachRepl(_stream, _process.Id, _eval.AnalyzerProjectLanguageVersion); } // TODO: Surround in SocketUnlock var debugTarget = new VsDebugTargetInfo2(); debugTarget.guidLaunchDebugEngine = AD7Engine.DebugEngineGuid; debugTarget.dwDebugEngineCount = 1; debugTarget.dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_Custom; debugTarget.bstrExe = debugProcess.ProcessGuid.ToString(); debugTarget.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VsDebugTargetInfo)); debugTarget.bstrCurDir = ""; debugTarget.guidPortSupplier = new Guid("{708C1ECA-FF48-11D2-904F-00C04FA302A1}"); // local port supplier debugTarget.LaunchFlags = (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_WaitForAttachComplete | (uint)__VSDBGLAUNCHFLAGS5.DBGLAUNCH_BreakOneProcess; debugTarget.bstrOptions = AD7Engine.AttachRunning + "=True"; debugTarget.pDebugEngines = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid))); Marshal.StructureToPtr(AD7Engine.DebugEngineGuid, debugTarget.pDebugEngines, false); IntPtr memory = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(VsDebugTargetInfo2))); Marshal.StructureToPtr(debugTarget, memory, false); var debugger = (IVsDebugger2)JToolsPackage.GetGlobalService(typeof(SVsShellDebugger)); int hr = debugger.LaunchDebugTargets2(1, memory); if (ErrorHandler.Failed(hr)) { var uiShell = (IVsUIShell)JToolsPackage.GetGlobalService(typeof(SVsUIShell)); string errorText; uiShell.GetErrorInfo(out errorText); if (String.IsNullOrWhiteSpace(errorText)) { errorText = "Unknown Error: " + hr; } return errorText; } else { _eval._attached = true; } GC.KeepAlive(debugProcess); return null; }
/// <inheritdoc /> public Process LaunchProcess(ProcessStartInfo processStartInfo) { if (processStartInfo == null) throw new ArgumentNullException("processStartInfo"); Process foundProcess = null; try { IVisualStudio visualStudio = GetVisualStudio(true, logger); if (visualStudio != null) { visualStudio.Call(dte => { List<int> currentDebuggedProcesses = new List<int>(); if (dte.Debugger.DebuggedProcesses != null) { foreach (EnvDTE.Process dteProcess in dte.Debugger.DebuggedProcesses) currentDebuggedProcesses.Add(dteProcess.ProcessID); } var serviceProvider = new VisualStudioServiceProvider(dte); var debugger = serviceProvider.GetService<SVsShellDebugger, IVsDebugger2>(); var engineGuids = GetEngineGuids(); int debugTargetInfoSize = Marshal.SizeOf(typeof(VsDebugTargetInfo2)); int guidSize = Marshal.SizeOf(typeof(Guid)); IntPtr debugTargetInfoPtr = Marshal.AllocCoTaskMem(debugTargetInfoSize + guidSize * engineGuids.Count); try { IntPtr engineGuidsPtr = new IntPtr(debugTargetInfoPtr.ToInt64() + debugTargetInfoSize); var environment = new StringBuilder(); foreach (DictionaryEntry variable in processStartInfo.EnvironmentVariables) environment.Append(variable.Key).Append('=').Append(variable.Value).Append('\0'); var debugTargetInfo = new VsDebugTargetInfo2() { cbSize = (uint) debugTargetInfoSize, bstrExe = Path.GetFullPath(processStartInfo.FileName), bstrArg = processStartInfo.Arguments, bstrCurDir = string.IsNullOrEmpty(processStartInfo.WorkingDirectory) ? Environment.CurrentDirectory : Path.GetFullPath(processStartInfo.WorkingDirectory), bstrEnv = environment.Length != 0 ? environment.ToString() : null, dwDebugEngineCount = (uint) engineGuids.Count, pDebugEngines = engineGuidsPtr, fSendToOutputWindow = 1, guidLaunchDebugEngine = VisualStudioDebugEngines.ManagedAndNative, dlo = (int) DEBUG_LAUNCH_OPERATION.DLO_CreateProcess, LaunchFlags = (int)(__VSDBGLAUNCHFLAGS.DBGLAUNCH_WaitForAttachComplete | __VSDBGLAUNCHFLAGS.DBGLAUNCH_Silent) }; Marshal.StructureToPtr(debugTargetInfo, debugTargetInfoPtr, false); for (int i = 0; i < engineGuids.Count; i++) Marshal.StructureToPtr(engineGuids[i], new IntPtr(engineGuidsPtr.ToInt64() + i * guidSize), false); int hresult = debugger.LaunchDebugTargets2(1, debugTargetInfoPtr); if (hresult != S_OK) Marshal.ThrowExceptionForHR(hresult); } finally { Marshal.FreeCoTaskMem(debugTargetInfoPtr); } if (dte.Debugger.DebuggedProcesses != null) { foreach (EnvDTE.Process dteProcess in dte.Debugger.DebuggedProcesses) { if (!currentDebuggedProcesses.Contains(dteProcess.ProcessID)) { foundProcess = Process.GetProcessById(dteProcess.ProcessID); break; } } } if (foundProcess == null) { logger.Log(LogSeverity.Debug, "Could not find the newly launched process."); } }); } } catch (VisualStudioException ex) { logger.Log(LogSeverity.Debug, "Failed to attach Visual Studio debugger to process.", ex); } return foundProcess; }
int IVsQueryDebuggableProjectCfg.QueryDebugTargets(uint grfLaunch, uint cTargets, VsDebugTargetInfo2[] rgDebugTargetInfo, uint[] pcActual) { var project = PythonConfig; if (pcActual != null && pcActual.Length > 0) { pcActual[0] = 1; } if (rgDebugTargetInfo != null && rgDebugTargetInfo.Length > 0) { IList<Guid> debugEngineGuids = new Guid[] { VSConstants.DebugEnginesGuids.NativeOnly_guid }; rgDebugTargetInfo[0] = new VsDebugTargetInfo2(); rgDebugTargetInfo[0].bstrExe = project.GetProjectProperty("Name"); rgDebugTargetInfo[0].bstrRemoteMachine = project.GetProjectProperty("RemoteDebugMachine"); rgDebugTargetInfo[0].guidPortSupplier = VSConstants.DebugPortSupplierGuids.NoAuth_guid; rgDebugTargetInfo[0].guidLaunchDebugEngine = debugEngineGuids[0]; rgDebugTargetInfo[0].dwDebugEngineCount = (uint)debugEngineGuids.Count; rgDebugTargetInfo[0].pDebugEngines = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid)) * debugEngineGuids.Count); for (var i = 0; i < debugEngineGuids.Count; i++) { Marshal.StructureToPtr(debugEngineGuids[i], IntPtr.Add(rgDebugTargetInfo[0].pDebugEngines, i * Marshal.SizeOf(typeof(Guid))), false); } } return VSConstants.S_OK; }
public unsafe int DebugLaunch(uint grfLaunch) { try { if (!this.m_project.CanLaunch) { Utility.ShowMessageBox("The project must have either an output type of 'Console Application', or an output type of 'Class Library' and the start action set to a valid .NET MicroFramework application"); return Utility.COM_HResults.E_FAIL; } //Consider Launching ourselves (at least for device), and then calling Attach. //This would get rid of the ugly dummy thread hack in CorDebugProcess. //However, we would need to jump through some other hoops, like resuming the process, //perhaps setting up the entry point breakpoint ourselves, ..? VsDebugTargetInfo2 vsDebugTargetInfo = new VsDebugTargetInfo2(); DebugPort port = GetDebugPort(); Process processWin32 = null; bool fNoDebug = (__VSDBGLAUNCHFLAGS.DBGLAUNCH_NoDebug & (__VSDBGLAUNCHFLAGS)grfLaunch) != 0; int hRes = Utility.COM_HResults.S_OK; PlatformInfo.Emulator emulatorConfig = GetEmulatorConfig(); if (port == null) { throw new Exception("Cannot find port to deploy to"); } string commandLine = GetCommandLineForLaunch(fNoDebug, emulatorConfig, IsTargetBigEndian() ); string exe = port.IsLocalPort ? emulatorConfig.application : typeof(CorDebugProcess).Assembly.Location; if (!fNoDebug) { commandLine = string.Format("{0} \"{1}{2}\"", commandLine, CorDebugProcess.c_DeployDeviceName, this.DeployDeviceName); } //use emulator args even though this may be a device launch. This is needed to store //paths to assemblies. vsDebugTargetInfo.bstrArg = commandLine; vsDebugTargetInfo.bstrCurDir = (string)m_project.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ProjectDir); vsDebugTargetInfo.bstrEnv = null; vsDebugTargetInfo.bstrExe = exe; vsDebugTargetInfo.bstrOptions = null; vsDebugTargetInfo.bstrPortName = DeployTransportName; vsDebugTargetInfo.bstrRemoteMachine = null; vsDebugTargetInfo.cbSize = (uint)Marshal.SizeOf(vsDebugTargetInfo); vsDebugTargetInfo.dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_CreateProcess; vsDebugTargetInfo.dwDebugEngineCount = 0; vsDebugTargetInfo.dwProcessId = 0; vsDebugTargetInfo.dwReserved = 0; vsDebugTargetInfo.fSendToOutputWindow = 0; vsDebugTargetInfo.guidLaunchDebugEngine = CorDebug.s_guidDebugEngine; vsDebugTargetInfo.guidPortSupplier = DebugPortSupplier.s_guidPortSupplier; vsDebugTargetInfo.guidProcessLanguage = Guid.Empty; vsDebugTargetInfo.hStdError = 0; vsDebugTargetInfo.hStdInput = 0; vsDebugTargetInfo.hStdOutput = 0; vsDebugTargetInfo.LaunchFlags = grfLaunch; vsDebugTargetInfo.pDebugEngines = IntPtr.Zero; vsDebugTargetInfo.pUnknown = null; if (fNoDebug) { if (port.IsLocalPort) { processWin32 = new Process(); processWin32.StartInfo = new ProcessStartInfo(); processWin32.StartInfo.FileName = vsDebugTargetInfo.bstrExe; processWin32.StartInfo.Arguments = vsDebugTargetInfo.bstrArg; processWin32.StartInfo.WorkingDirectory = vsDebugTargetInfo.bstrCurDir; processWin32.StartInfo.UseShellExecute = false; processWin32.Start(); } else { RebootAndResumeExecution(); } } else { byte* bpDebugTargetInfo = stackalloc byte[(int)vsDebugTargetInfo.cbSize]; IntPtr ipDebugTargetInfo = (IntPtr)bpDebugTargetInfo; try { IVsDebugger2 iVsDebugger = ( IVsDebugger2 )ServiceProvider.GlobalProvider.GetService( typeof( IVsDebugger ) ); Marshal.StructureToPtr(vsDebugTargetInfo, ipDebugTargetInfo, false); hRes = iVsDebugger.LaunchDebugTargets2(1, ipDebugTargetInfo); } finally { if (ipDebugTargetInfo != null) Marshal.DestroyStructure(ipDebugTargetInfo, vsDebugTargetInfo.GetType()); } } return hRes; } catch (Exception ex) { Utility.ShowMessageBox(String.Format("An exception occurred while attempting to launch the debugger: {0}", ex.Message)); return Utility.COM_HResults.E_FAIL; } }
/// <inheritdoc /> public Process LaunchProcess(ProcessStartInfo processStartInfo) { if (processStartInfo == null) { throw new ArgumentNullException("processStartInfo"); } Process foundProcess = null; try { IVisualStudio visualStudio = GetVisualStudio(true, logger); if (visualStudio != null) { visualStudio.Call(dte => { List <int> currentDebuggedProcesses = new List <int>(); if (dte.Debugger.DebuggedProcesses != null) { foreach (EnvDTE.Process dteProcess in dte.Debugger.DebuggedProcesses) { currentDebuggedProcesses.Add(dteProcess.ProcessID); } } var serviceProvider = new VisualStudioServiceProvider(dte); var debugger = serviceProvider.GetService <SVsShellDebugger, IVsDebugger2>(); var engineGuids = GetEngineGuids(); int debugTargetInfoSize = Marshal.SizeOf(typeof(VsDebugTargetInfo2)); int guidSize = Marshal.SizeOf(typeof(Guid)); IntPtr debugTargetInfoPtr = Marshal.AllocCoTaskMem(debugTargetInfoSize + guidSize * engineGuids.Count); try { IntPtr engineGuidsPtr = new IntPtr(debugTargetInfoPtr.ToInt64() + debugTargetInfoSize); var environment = new StringBuilder(); foreach (DictionaryEntry variable in processStartInfo.EnvironmentVariables) { environment.Append(variable.Key).Append('=').Append(variable.Value).Append('\0'); } var debugTargetInfo = new VsDebugTargetInfo2() { cbSize = (uint)debugTargetInfoSize, bstrExe = Path.GetFullPath(processStartInfo.FileName), bstrArg = processStartInfo.Arguments, bstrCurDir = string.IsNullOrEmpty(processStartInfo.WorkingDirectory) ? Environment.CurrentDirectory : Path.GetFullPath(processStartInfo.WorkingDirectory), bstrEnv = environment.Length != 0 ? environment.ToString() : null, dwDebugEngineCount = (uint)engineGuids.Count, pDebugEngines = engineGuidsPtr, fSendToOutputWindow = 1, guidLaunchDebugEngine = VisualStudioDebugEngines.ManagedAndNative, dlo = (int)DEBUG_LAUNCH_OPERATION.DLO_CreateProcess, LaunchFlags = (int)(__VSDBGLAUNCHFLAGS.DBGLAUNCH_WaitForAttachComplete | __VSDBGLAUNCHFLAGS.DBGLAUNCH_Silent) }; Marshal.StructureToPtr(debugTargetInfo, debugTargetInfoPtr, false); for (int i = 0; i < engineGuids.Count; i++) { Marshal.StructureToPtr(engineGuids[i], new IntPtr(engineGuidsPtr.ToInt64() + i * guidSize), false); } int hresult = debugger.LaunchDebugTargets2(1, debugTargetInfoPtr); if (hresult != S_OK) { Marshal.ThrowExceptionForHR(hresult); } } finally { Marshal.FreeCoTaskMem(debugTargetInfoPtr); } if (dte.Debugger.DebuggedProcesses != null) { foreach (EnvDTE.Process dteProcess in dte.Debugger.DebuggedProcesses) { if (!currentDebuggedProcesses.Contains(dteProcess.ProcessID)) { foundProcess = Process.GetProcessById(dteProcess.ProcessID); break; } } } if (foundProcess == null) { logger.Log(LogSeverity.Debug, "Could not find the newly launched process."); } }); } } catch (VisualStudioException ex) { logger.Log(LogSeverity.Debug, "Failed to attach Visual Studio debugger to process.", ex); } return(foundProcess); }