Пример #1
0
        string GetDotNetCoreApp(EnvDTE80.Process2 prc, string fullCommandLine)
        {
            if (fullCommandLine.Contains("dotnet.exe") == false)
            {
                return(null);
            }
            var match = Regex.Match(fullCommandLine, @"dotnet.exe"".*exec.*""(.*)""");

            if (match.Success && match.Groups.Count >= 1)
            {
                var tp = System.Diagnostics.Process.GetProcessById(prc.ProcessID);

                string title = Path.GetFileName(match.Groups[1].Value.Trim());

                try
                {
                    var windowtitle = tp.MainWindowTitle;

                    if (windowtitle != null)
                    {
                        title += " >> " + windowtitle;
                    }
                }
                catch
                {
                }

                return($"dotnet[\"{title}\"]");
            }
            return(null);
        }
Пример #2
0
 /// <summary>
 /// Creates a new ProcHolder instance.
 /// </summary>
 public ProcHolder(EnvDTE80.Process2 prc)
 {
     if (prc == null)
     {
         return;
     }
     Process = prc;
     try
     {
         using (var mos = new ManagementObjectSearcher(
                    @"\\{0}\root\cimv2".FormatWith(prc.TransportQualifier.Replace("Geeks@", "")),
                    "SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + prc.ProcessID))
         {
             foreach (var mo in mos.Get())
             {
                 var commandLine = mo["CommandLine"];
                 if (commandLine != null)
                 {
                     AppPool += GetAppPool(commandLine.ToString());
                 }
             }
         }
     }
     catch (Exception)
     {
     }
 }
Пример #3
0
        /// <summary>
        /// Attempts to load the specified process into a debugger using the specified debug engine
        /// and the best debugger available.
        /// If any instance of Visual Studio process is already associated with the current
        /// (calling) process, then that instance's debugger is targetd.
        /// Otherwise, the any first instance of Visual Studio is targetd.
        /// </summary>
        /// <param name="subjectProcessId">Id of the process to be debugged.</param>
        /// <param name="debugEngine">Name of the debugger script engine to use e.g. "script" or "native" or "managed".</param>
        public static void DebugAttachToProcess(int subjectProcessId, string debugEngine)
        {
            // Register COM message filter on this thd to enforce a call retry policy
            // should our COM calls into the DTE (in another apartment) be rejected.
            using (var mf = new ResilientMessageFilterScope())
            {
                EnvDTE.DTE dte = GetAllDTEs().FirstOrDefault(x => ProcessExtensions.IsProcessAAncestorOfProcessB(x.Key, Process.GetCurrentProcess().Id)).Value;
                if (dte == null)
                {
                    dte = GetAllDTEs().FirstOrDefault().Value;
                }

                IEnumerable <EnvDTE.Process> processes = dte.Debugger.LocalProcesses.OfType <EnvDTE.Process>();
                EnvDTE.Process process = processes.SingleOrDefault(x => x.ProcessID == subjectProcessId);
                // A.
                if (process != null)
                {
                    EnvDTE80.Process2 p2 = (EnvDTE80.Process2)process;
                    p2.Attach2(debugEngine);
                }
                // B.
                else
                {
                    throw new InvalidOperationException("Unable to debug the process: Visual Studio debugger not found.");
                    // TODO: spin up an instance of Visual Studio in this case?
                }
            }
        }
Пример #4
0
 private static bool TryAttach(EnvDTE80.Process2 process, string[] engines)
 {
     try
     {
         process.Attach2(engines);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Пример #5
0
        /// <summary>
        /// Attaches Visual Studio debugger to the target process
        /// </summary>
        /// <param name="visualStudioProcess">Visual studio process object</param>
        /// <param name="applicationProcess">Application process object</param>
        /// <param name="engines">List of debug engines to use</param>
        private static void AttachTo(
            this Process visualStudioProcess,
            Process applicationProcess,
            string[] engines)
        {
            MessageFilter.Register();

            EnvDTE._DTE visualStudioInstance;

            if (true == TryGetVsInstance(visualStudioProcess.Id, out visualStudioInstance))
            {
                EnvDTE.Processes procs = visualStudioInstance.Debugger.LocalProcesses;

                EnvDTE80.Process2 processToAttachTo = null;
                foreach (EnvDTE80.Process2 process in procs)
                {
                    if (process.ProcessID == applicationProcess.Id)
                    {
                        processToAttachTo = process;
                    }
                }

                if (null != processToAttachTo)
                {
                    for (int i = 0; i < 6; ++i)
                    {
                        if (TryAttach(processToAttachTo, engines))
                        {
                            break;
                        }

                        if (i < 5)
                        {
                            Thread.Sleep(5000);
                        }
                        else
                        {
                            // NB! We could not attach automatically. This is the right moment to do it.
                            // Do this manually now. You want to attach to iisexpess.
                            Debugger.Break();
                        }
                    }
                }
                else
                {
                    throw new InvalidOperationException(
                              "Visual Studio process cannot find specified application '" + applicationProcess.Id + "'");
                }
            }

            MessageFilter.Revoke();
        }
Пример #6
0
        ProcHolder(EnvDTE80.Process2 prc)
        {
            try
            {
                Process = prc;

                using (var mos = new ManagementObjectSearcher(
                           @"\\{0}\root\cimv2".FormatWith(prc.TransportQualifier.Replace("Geeks@", "")),
                           "SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + prc.ProcessID))
                {
                    foreach (var mo in mos.Get())
                    {
                        var commandLine = mo["CommandLine"];
                        if (commandLine != null)
                        {
                            var cmdLineStr = commandLine.ToString();

                            if (ExcludedProcessesManager.CheckCommandLine(cmdLineStr.ToLower()))
                            {
                                Process = null;
                                return;
                            }

                            var appPool = GetAppPool(commandLine.ToString());

                            if (appPool != null)
                            {
                                AppPool += appPool;
                            }

                            var dotNetCoreApp = GetDotNetCoreApp(prc, commandLine.ToString());

                            if (dotNetCoreApp != null)
                            {
                                AppPool += dotNetCoreApp;
                            }
                        }
                    }
                }

                if (AppPool == null)
                {
                    AppPool = Path.GetFileName(Process.Name);
                }
            }
            catch (Exception)
            {
            }
        }
Пример #7
0
        public static ProcHolder CheckAndAddProcHolder(EnvDTE80.Process2 process)
        {
            ProcHolder returnProcessHolder = null;

            try
            {
                if (process == null)
                {
                    return(null);
                }
                if (process.Name.Contains("dotnet.exe"))
                {
                    return(null);
                }
                var startTime   = ExcludedProcessesManager.CheckAndReturnStartTime(process);
                var ProcessPath = process.Name;

                if (startTime == null)
                {
                    return(null);
                }

                if (alreadyCheckedProcesses.TryGetValue(process.ProcessID, out returnProcessHolder))
                {
                    if (returnProcessHolder.Process.Name == process.Name)
                    {
                        returnProcessHolder.StartTime   = startTime;
                        returnProcessHolder.ProcessPath = ProcessPath;
                        return(returnProcessHolder);
                    }
                }

                returnProcessHolder = new ProcHolder(process);
                if (returnProcessHolder.Process != null)
                {
                    alreadyCheckedProcesses.TryAdd(process.ProcessID, returnProcessHolder);
                    returnProcessHolder.StartTime   = startTime;
                    returnProcessHolder.ProcessPath = ProcessPath;
                    return(returnProcessHolder);
                }

                return(null);
            }
            catch (Exception ex)
            {
            }

            return(returnProcessHolder);
        }
        public DateTime?CheckAndReturnStartTime(EnvDTE80.Process2 prc)
        {
            var processFullName = prc.Name.ToLower();

            if (IsEnabled)
            {
                if (ExcludedNoneDotNetProcesses.Any(x => x.ToLower() == processFullName))
                {
                    return(null);
                }
            }

            if (ExcludedProcessNames.Any(x => prc.Name.ToLower().StartsWith(x)))
            {
                return(null);
            }

            var fileName = Path.GetFileName(prc.Name);

            if (fileName.HasValue())
            {
                if (ExcludedProcessNames.Any(x => fileName.Equals(x, StringComparison.OrdinalIgnoreCase)))
                {
                    return(null);
                }
            }

            try
            {
                var tp = Process.GetProcessById(prc.ProcessID);

                if (ExcludedProcessNames.Any(x => tp.ProcessName.ToLower().StartsWith(x)))
                {
                    return(null);
                }

                if (WebServerProcessNames.Any(n => processFullName.IndexOf(n) >= 0) == false && IsDotNetProcess(tp, processFullName) == false)
                {
                    return(null);
                }

                return(tp.StartTime);
            }
            catch
            {
            }

            return(null);
        }
Пример #9
0
        private void AttachToW3wp(List <int> currentWorkerProcess)
        {
            EnvDTE80.Debugger2 dbg2      = _applicationObject.Debugger as EnvDTE80.Debugger2;
            EnvDTE80.Transport transport = dbg2.Transports.Item("Default");

            EnvDTE80.Engine dbgEngine = null;

            string targetVersion = GetFrameworkVersion();

            Trace.WriteLine("Attach Debug Engine: " + targetVersion);

            foreach (EnvDTE80.Engine item in transport.Engines)
            {
                if (item.Name.IndexOf(targetVersion) != -1)
                {
                    dbgEngine = item;
                    break;
                }
            }

            if (dbgEngine == null)
            {
                ShowMessage("Can't determine project's TargetFrameworkVersion - " + targetVersion);
                return;
            }

            Trace.WriteLine("Debug Engine Type: " + dbgEngine.Name);

            EnvDTE80.Process2 w3wpProcess = null;

            foreach (var proc in dbg2.LocalProcesses)
            {
                EnvDTE80.Process2 process = proc as EnvDTE80.Process2;
                if (process == null)
                {
                    continue;
                }

                // Skip process in recycle.
                if (currentWorkerProcess.Contains(process.ProcessID) == true)
                {
                    continue;
                }

                if (process.Name.IndexOf("W3WP.EXE", 0, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    w3wpProcess = process;
                    break;
                }
            }

            if (w3wpProcess != null)
            {
                bool   attached = false;
                string txt      = string.Format("{0}({1})", w3wpProcess.Name, w3wpProcess.ProcessID);
                System.Diagnostics.Trace.WriteLine("Attaching to: " + txt);
                try
                {
                    // I don't know why Attach2 method hang
                    //              when Visual Studio 2013 try to attach w3wp.exe hosting .NET 2.0/3.0/3.5 Web Application.
                    if (targetVersion == "v2.0")
                    {
                        w3wpProcess.Attach();
                    }
                    else
                    {
                        w3wpProcess.Attach2(dbgEngine);
                    }

                    attached = true;
                    System.Diagnostics.Trace.WriteLine("Attached to: " + txt);
                }
                catch
                {
                }

                if (attached == true)
                {
                    string startupUrl = GetProjectItemValue("WebApplication.IISUrl");
                    if (string.IsNullOrEmpty(startupUrl) == false)
                    {
                        RunInternetExplorer(startupUrl);
                    }
                }
            }
            else
            {
                ShowMessage("Make sure the AppPool's Start Mode is AlwaysRunning");
            }
        }