Exemple #1
0
        private void ProfileService()
        {
            if (targetv2DesktopCLR())
            {
                RegisterDLL.Register();  // Register profilerOBJ.dll for v2 CLR, which doesn't support registry free activation
            }

            StopService(serviceName, serviceStopCommand);

            string logDir = GetLogDir();

            string[] profilerEnvironment = CreateProfilerEnvironment(logDir);

            // set environment variables

            // this is a bit intricate - if the service is running as LocalSystem, we need to set the environment
            // variables in the registry for the service, otherwise it's better to temporarily set it for the account,
            // assuming we can find out the account SID
            string serviceAccountName = GetServiceAccountName(serviceName);

            if (serviceAccountName.StartsWith(@".\"))
            {
                serviceAccountName = Environment.MachineName + serviceAccountName.Substring(1);
            }
            if (serviceAccountName != null && serviceAccountName != "LocalSystem")
            {
                serviceAccountSid = LookupAccountSid(serviceAccountName);
            }
            if (serviceAccountSid != null)
            {
                SetAccountEnvironment(serviceAccountSid, profilerEnvironment);
            }
            else
            {
                string[] baseEnvironment = GetServicesEnvironment();
                baseEnvironment = ReplaceTempDir(baseEnvironment, GetLogDir());
                string[] combinedEnvironment = CombineEnvironmentVariables(baseEnvironment, profilerEnvironment);
                SetEnvironmentVariables(serviceName, combinedEnvironment);
            }

            System.Diagnostics.Process cmdProcess = StartService(serviceName, serviceStartCommand);

            // wait for service to start up and connect
            breinstormin.tools.log.LogEngine.WriteLog("breinstormin.profile",
                                                      string.Format("Waiting for {0} to start up", serviceName));

            Thread.Sleep(1000);
            int pid = WaitForProcessToConnect(logDir, "Waiting for service to start common language runtime");

            if (pid > 0)
            {
                profiledProcess = System.Diagnostics.Process.GetProcessById(pid);

                breinstormin.tools.log.LogEngine.WriteLog("breinstormin.profile", "Profiling: " + serviceName);
                breinstormin.tools.log.LogEngine.WriteLog("breinstormin.profile", "Start " + serviceName);

                processFileName = serviceName;
            }

            /* Delete the environment variables as early as possible, so that even if CLRProfiler crashes, the user's machine
             * won't be screwed up.
             * */

            if (serviceAccountSid != null)
            {
                ResetAccountEnvironment(serviceAccountSid, profilerEnvironment);
            }
            else
            {
                DeleteEnvironmentVariables(serviceName);
            }
        }
Exemple #2
0
        public void ProfileProcess(string profilingProcessfilename, CLRSKU clrruntime, ProfilingProcessType processtype)
        {
            processFileName = profilingProcessfilename;

            switch (processtype)
            {
            case ProfilingProcessType.ASPNET:
                serviceName = null;
                break;

            case ProfilingProcessType.Executable:
                serviceName = null;
                break;

            case ProfilingProcessType.Service:
                break;
            }

            if (processFileName == null)
            {
                throw new Exception("The processFileName is empty!");
            }
            else if (processFileName == "ASP.NET")
            {
                profileASP_NET();
                return;
            }
            else if (serviceName != null)
            {
                ProfileService();
                return;
            }

            if (targetv2DesktopCLR())
            {
                RegisterDLL.Register();  // Register profilerOBJ.dll for v2 CLR, which doesn't support registry free activation
            }

            if (processFileName == null)
            {
                return;
            }

            if (profiledProcess == null || ProfiledProcessHasExited())
            {
                System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo(processFileName);
                if (targetv4CoreCLR())
                {
                    processStartInfo.EnvironmentVariables["CoreCLR_Enable_Profiling"] = "0x1";
                    processStartInfo.EnvironmentVariables["CORECLR_PROFILER"]         = "{8C29BC4E-1F57-461a-9B51-1200C32E6F1F}";
                    processStartInfo.EnvironmentVariables["CORECLR_PROFILER_PATH"]    = getProfilerFullPath();
                }
                else
                {
                    processStartInfo.EnvironmentVariables["Cor_Enable_Profiling"] = "0x1";
                    processStartInfo.EnvironmentVariables["COR_PROFILER"]         = "{8C29BC4E-1F57-461a-9B51-1200C32E6F1F}";
                    processStartInfo.EnvironmentVariables["COR_PROFILER_PATH"]    = getProfilerFullPath();
                }

                processStartInfo.EnvironmentVariables["OMV_USAGE"]  = CreateUsageString();
                processStartInfo.EnvironmentVariables["OMV_SKIP"]   = "0";
                processStartInfo.EnvironmentVariables["OMV_PATH"]   = GetLogDir();
                processStartInfo.EnvironmentVariables["OMV_STACK"]  = trackCallStacks ? "1" : "0";
                processStartInfo.EnvironmentVariables["OMV_FORMAT"] = "v2";
                processStartInfo.EnvironmentVariables["OMV_DynamicObjectTracking"] = "0x1";
                processStartInfo.EnvironmentVariables["OMV_FORCE_GC_ON_COMMENT"]   = gcOnLogFileComments ? "1" : "0";
                processStartInfo.EnvironmentVariables["OMV_INITIAL_SETTING"]       = CreateInitialString();
                processStartInfo.EnvironmentVariables["OMV_TargetCLRVersion"]      = targetv2DesktopCLR() ? "v2" : "v4";


                if (commandLine != null)
                {
                    processStartInfo.Arguments = commandLine;
                }

                if (workingDirectory != null)
                {
                    processStartInfo.WorkingDirectory = workingDirectory;
                }

                processStartInfo.UseShellExecute = false;

                profiledProcess = System.Diagnostics.Process.Start(processStartInfo);

                if (WaitForProcessToConnect(GetLogDir(), "Waiting for application to start common language runtime") <= 0)
                {
                    profiledProcessEnded();
                }
            }
        }
Exemple #3
0
        private void profileASP_NET()
        {
            if (targetv2DesktopCLR())
            {
                RegisterDLL.Register();  // Register profilerOBJ.dll for v2 CLR, which doesn't support registry free activation
            }

            StopIIS();

            // set environment variables

            string logDir = GetLogDir();

            string[] profilerEnvironment = CreateProfilerEnvironment(logDir);

            string[] baseEnvironment = GetServicesEnvironment();
            baseEnvironment = ReplaceTempDir(baseEnvironment, GetLogDir());
            string[] combinedEnvironment = CombineEnvironmentVariables(baseEnvironment, profilerEnvironment);
            SetEnvironmentVariables("IISADMIN", combinedEnvironment);
            SetEnvironmentVariables("W3SVC", combinedEnvironment);
            SetEnvironmentVariables("WAS", combinedEnvironment);

            string asp_netAccountName = GetASP_NETaccountName();
            string asp_netAccountSid  = null;

            if (asp_netAccountName != null)
            {
                asp_netAccountSid = LookupAccountSid(asp_netAccountName);
                if (asp_netAccountSid != null)
                {
                    SetAccountEnvironment(asp_netAccountSid, profilerEnvironment);
                }
            }

            if (StartIIS())
            {
                // wait for worker process to start up and connect
                breinstormin.tools.log.LogEngine.WriteLog("breinstormin.profile",
                                                          "Waiting for ASP.NET worker process to start up");

                Thread.Sleep(1000);
                int pid = WaitForProcessToConnect(logDir,
                                                  "Waiting for ASP.NET to start common language runtime - this is the time to load your test page");

                if (pid > 0)
                {
                    profiledProcess = System.Diagnostics.Process.GetProcessById(pid);

                    breinstormin.tools.log.LogEngine.WriteLog("breinstormin.profile", "Profiling: ASP.NET");
                    breinstormin.tools.log.LogEngine.WriteLog("breinstormin.profile", "Start ASP.NET");

                    processFileName = "ASP.NET";
                }
            }

            /* Delete the environment variables as early as possible, so that even if CLRProfiler crashes, the user's machine
             * won't be screwed up.
             * */
            DeleteEnvironmentVariables("IISADMIN");
            DeleteEnvironmentVariables("W3SVC");
            DeleteEnvironmentVariables("WAS");

            if (asp_netAccountSid != null)
            {
                ResetAccountEnvironment(asp_netAccountSid, profilerEnvironment);
            }

            serviceName = null;
        }