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); } }
public void LoadProcesses() { _processes.Clear(); List<ChromiumProcess> chromes = new List<ChromiumProcess>(); HashSet<int> chromePids = new HashSet<int>(); foreach (Process p in Process.GetProcesses()) { // System.Diagnostics.Process uses a naive implementation that is unable to deal with many // types of processes (such as those already under a debugger, or those with a high // privilege level), so use NtProcess instead. NtProcess ntproc = new NtProcess(p.Id); if (!ntproc.IsValid) continue; FullPath processPath = new FullPath(ntproc.Win32ProcessImagePath); if (processPath.StartsWith(_installationData.InstallationPath)) { chromes.Add(new ChromiumProcess(ntproc, _installationData)); chromePids.Add(p.Id); } } foreach (ChromiumProcess chrome in chromes) { // Only insert root processes at this level, child processes will be children of one of // these processes. if (!chromePids.Contains(chrome.ParentPid)) { ChromeProcessViewModel viewModel = new ChromeProcessViewModel(_root, chrome); viewModel.LoadProcesses(chromes.ToArray()); _processes.Add(viewModel); } } }
public static InstallationData Create(NtProcess proc) { InstallationEnumerator enumerator = new InstallationEnumerator(); foreach (InstallationData data in enumerator) { FullPath fullPath = new FullPath(proc.Win32ProcessImagePath); if (fullPath.StartsWith(data.InstallationPath)) return data; } return new InstallationData( proc.Win32ProcessImagePath, InstallationLevel.Developer, 0, "Developer Chrome", String.Empty); }
public static Process[] GetChildren(this Process process) { List<Process> processes = new List<Process>(); foreach (Process proc in Process.GetProcesses()) { if (proc.Id == process.Id) continue; NtProcess ntproc = new NtProcess(proc.Id); if (!ntproc.IsValid) continue; if (ntproc.ParentProcessId == process.Id) processes.Add(proc); } return processes.ToArray(); }
private void ReloadNativeProcessInfo() { Process[] chromes = Process.GetProcessesByName("chrome"); Process[] delegate_executes = Process.GetProcessesByName("delegate_execute"); Process[] processes = new Process[chromes.Length + delegate_executes.Length]; chromes.CopyTo(processes, 0); delegate_executes.CopyTo(processes, chromes.Length); foreach (Process p in processes) { var item = new ProcessViewItem(); NtProcess ntproc = new NtProcess(p.Id); if (!ntproc.IsValid) continue; item.Process = ChromiumProcess.Create(ntproc); if (item.Process == null) continue; if (ntproc.CommandLine != null) { item.DisplayCmdLine = GetFilteredCommandLineString(item.Process.CommandLineArgs); } item.SessionId = p.SessionId; item.Title = p.MainWindowTitle; item.Exe = p.ProcessName; item.Text = item.Exe; item.SubItems.Add(item.Process.Pid.ToString()); item.SubItems.Add(item.Title); item.SubItems.Add(item.Process.Category.ToString()); item.SubItems.Add(item.Process.InstallationData.Architecture.ToString()); item.SubItems.Add(item.DisplayCmdLine); listViewProcesses.Items.Add(item); // Add the item to the list view before setting its image, // otherwise the ImageList field will be null. Icon icon = item.Process.Icon; item.ImageList.Images.Add(item.Process.Icon); item.ImageIndex = item.ImageList.Images.Count - 1; } }
public void Execute(object sender, EventArgs e) { var dte = (EnvDTE.DTE)_visualStudioPackageProvider.Package.DTE; //GetService(typeof(EnvDTE.DTE)); HashSet<int> roots = new HashSet<int>(); foreach (EnvDTE90.Process3 p in dte.Debugger.DebuggedProcesses) { if (p.IsBeingDebugged && ChromeUtility.IsChromeProcess(p.Name)) roots.Add(p.ProcessID); } foreach (EnvDTE90.Process3 p in dte.Debugger.LocalProcesses) { if (p.IsBeingDebugged || !ChromeUtility.IsChromeProcess(p.Name)) continue; NtProcess process = new NtProcess(p.ProcessID); if (!roots.Contains(process.ParentProcessId)) continue; p.Attach(); } }
public ChromiumProcess(NtProcess process, InstallationData installationData) { _ntProcess = process; _installationData = installationData; _category = ProcessCategory.Unknown; _commandLine = null; }
public static ChromiumProcess Create(NtProcess process) { InstallationData data = InstallationData.Create(process); if (data == null) return null; return new ChromiumProcess(process, data); }