public static List <String> getListOfManagedProcess(bool showDetails)
        {
            var managedProcesses = new List <String>();
            int currentProcessId = Process.GetCurrentProcess().Id;
            var corPublish       = new CorPublish();

            foreach (CorPublishProcess corPublishProcess in corPublish.EnumProcesses())
            {
                if (currentProcessId != corPublishProcess.ProcessId)
                {
                    // don't include the current process
                    if (showDetails)
                    {
                        managedProcesses.Add("[" + corPublishProcess.ProcessId + "] [ver=" +
                                             CorDebugger.GetDebuggerVersionFromPid(corPublishProcess.ProcessId) + "] " +
                                             corPublishProcess.DisplayName);
                    }
                    else
                    {
                        managedProcesses.Add(corPublishProcess.DisplayName);
                    }
                }
            }
            return(managedProcesses);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the version of the runtime to debug in a process
        /// we are attaching to, assuming we can only pick one
        /// </summary>
        /// <param name="processId">The process to attach to</param>
        /// <returns>The version of the runtime to debug, or null if the policy can't decide</returns>
        public static string GetDefaultAttachVersion(int processId)
        {
            try
            {
                CLRMetaHost           mh       = new CLRMetaHost();
                List <CLRRuntimeInfo> runtimes = new List <CLRRuntimeInfo>(mh.EnumerateLoadedRuntimes(processId));
                if (runtimes.Count > 1)
                {
                    // It is ambiguous so just give up
                    return(null);
                }
                else if (runtimes.Count == 1)
                {
                    return(runtimes[0].GetVersionString());
                }
            }
            catch (EntryPointNotFoundException)
            {
                try
                {
                    return(CorDebugger.GetDebuggerVersionFromPid(processId));
                }
                catch (COMException) { }
            }

            // if we have neither failed nor detected a version at this point then there was no
            // CLR loaded. Now we try to determine what CLR the process will load by examining its
            // binary
            string binaryPath = GetBinaryPathFromPid(processId);

            if (binaryPath != null)
            {
                string version = GetDefaultRuntimeForFile(binaryPath);
                if (version != null)
                {
                    return(version);
                }
            }

            // and if that doesn't work, return the version of the CLR the debugger is
            // running against (a very arbitrary choice)
            return(Environment.Version.ToString());
        }
        /// <summary>
        /// Attach to a process with the given Process ID
        /// </summary>
        /// <param name="processId">The Process ID to attach to.</param>
        /// <returns>The resulting MDbgProcess.</returns>
        public MDbgProcess Attach(int processId)
        {
            string deeVersion;

            try
            {
                deeVersion = CorDebugger.GetDebuggerVersionFromPid(processId);
            }
            catch
            {
                // GetDebuggerVersionFromPid isn't implemented on Win9x and so will
                // throw NotImplementedException.  We'll also get an ArgumentException
                // if the specified process doesn't have the CLR loaded yet.
                // Rather than be selective (and potentially brittle), we'll handle all errors.
                // Ideally we'd assert (or log) that we're only getting the errors we expect,
                // but it's complex and ugly to do that in C#.

                // Fall back to guessing the version based on the filename.
                // Environment variables (eg. COMPLUS_Version) may have resulted
                // in a different choice.
                try
                {
                    var cp = new CorPublish.CorPublish();
                    CorPublishProcess cpp           = cp.GetProcess(processId);
                    string            programBinary = cpp.DisplayName;

                    deeVersion = CorDebugger.GetDebuggerVersionFromFile(programBinary);
                }
                catch
                {
                    // This will also fail if the process doesn't have the CLR loaded yet.
                    // It could also fail if the image EXE has been renamed since it started.
                    // For whatever reason, fall back to using the default CLR
                    deeVersion = null;
                }
            }

            MDbgProcess p = m_processMgr.CreateLocalProcess(deeVersion);

            p.Attach(processId);
            return(p);
        }
        private void RefreshProcesses()
        {
            listBoxProcesses.Items.Clear();

            CorPublish cp = null;

            int curPid = Process.GetCurrentProcess().Id;

            try
            {
                int count = 0;

                cp = new CorPublish();
                {
                    foreach (CorPublishProcess cpp in cp.EnumProcesses())
                    {
                        if (curPid != cpp.ProcessId) // let's hide our process
                        {
                            string version = CorDebugger.GetDebuggerVersionFromPid(cpp.ProcessId);
                            string s       = "[" + cpp.ProcessId + "] [ver=" + version + "] " + cpp.DisplayName;
                            listBoxProcesses.Items.Add(new Item(cpp.ProcessId, s));
                            count++;
                        }
                    }
                } // using

                if (count == 0)
                {
                    listBoxProcesses.Items.Add(new Item(0, "(No active processes)"));
                }
            }
            catch (Exception)
            {
                if (cp == null)
                {
                    listBoxProcesses.Items.Add(new Item(0, "(Can't enumerate processes"));
                }
            }
        }