Exemplo n.º 1
0
        public async Task <HttpResponseMessage> StopProfileAsync(int id)
        {
            using (_tracer.Step("ProcessController.StopProfileAsync"))
            {
                // check if the process Ids exists in the sandbox. If it doesn't, this method returns a 404 and we are done.
                var process = GetProcessById(id);

                bool iisProfiling = ProfileManager.IsIisProfileRunning(process.Id);

                var result = await ProfileManager.StopProfileAsync(process.Id, _tracer, iisProfiling);

                if (result.StatusCode != HttpStatusCode.OK)
                {
                    return(Request.CreateErrorResponse(result.StatusCode, result.Message));
                }
                else
                {
                    string profileFileFullPath = ProfileManager.GetProfilePath(process.Id, iisProfiling);

                    string profileFileName = Path.GetFileName(profileFileFullPath);

                    HttpResponseMessage response = Request.CreateResponse();
                    response.Content = new StreamContent(FileStreamWrapper.OpenRead(profileFileFullPath));
                    response.Content.Headers.ContentType                 = new MediaTypeHeaderValue("application/octet-stream");
                    response.Content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
                    response.Content.Headers.ContentDisposition.FileName = profileFileName;
                    return(response);
                }
            }
        }
Exemplo n.º 2
0
        private ProcessInfo GetProcessInfo(Process process, string href, bool details = false)
        {
            href = href.TrimEnd('/');
            if (href.EndsWith("/0", StringComparison.OrdinalIgnoreCase))
            {
                href = href.Substring(0, href.Length - 1) + process.Id;
            }

            var selfLink = new Uri(href);
            var info     = new ProcessInfo
            {
                Id       = process.Id,
                Name     = process.ProcessName,
                Href     = selfLink,
                UserName = SafeGetValue(process.GetUserName, null)
            };

            if (details)
            {
                // this could fail access denied
                info.HandleCount = SafeGetValue(() => process.HandleCount, -1);
                info.ThreadCount = SafeGetValue(() => process.Threads.Count, -1);
                info.ModuleCount = SafeGetValue(() => process.Modules.Count, -1);
                info.FileName    = SafeGetValue(() => process.MainModule.FileName, "N/A");

                // always return empty
                //info.Arguments = SafeGetValue(() => process.StartInfo.Arguments, "N/A");

                info.StartTime               = SafeGetValue(() => process.StartTime.ToUniversalTime(), DateTime.MinValue);
                info.TotalProcessorTime      = SafeGetValue(() => process.TotalProcessorTime, TimeSpan.FromSeconds(-1));
                info.UserProcessorTime       = SafeGetValue(() => process.UserProcessorTime, TimeSpan.FromSeconds(-1));
                info.PrivilegedProcessorTime = SafeGetValue(() => process.PrivilegedProcessorTime, TimeSpan.FromSeconds(-1));

                info.PagedSystemMemorySize64    = SafeGetValue(() => process.PagedSystemMemorySize64, -1);
                info.NonpagedSystemMemorySize64 = SafeGetValue(() => process.NonpagedSystemMemorySize64, -1);
                info.PagedMemorySize64          = SafeGetValue(() => process.PagedMemorySize64, -1);
                info.PeakPagedMemorySize64      = SafeGetValue(() => process.PeakPagedMemorySize64, -1);
                info.WorkingSet64            = SafeGetValue(() => process.WorkingSet64, -1);
                info.PeakWorkingSet64        = SafeGetValue(() => process.PeakWorkingSet64, -1);
                info.VirtualMemorySize64     = SafeGetValue(() => process.VirtualMemorySize64, -1);
                info.PeakVirtualMemorySize64 = SafeGetValue(() => process.PeakVirtualMemorySize64, -1);
                info.PrivateMemorySize64     = SafeGetValue(() => process.PrivateMemorySize64, -1);

                info.MiniDump                   = new Uri(selfLink + "/dump");
                info.OpenFileHandles            = SafeGetValue(() => GetOpenFileHandles(process.Id), Enumerable.Empty <string>());
                info.Parent                     = new Uri(selfLink, SafeGetValue(() => process.GetParentId(_tracer), 0).ToString());
                info.Children                   = SafeGetValue(() => process.GetChildren(_tracer, recursive: false), Enumerable.Empty <Process>()).Select(c => new Uri(selfLink, c.Id.ToString()));
                info.Threads                    = SafeGetValue(() => GetThreads(process, selfLink.ToString()), Enumerable.Empty <ProcessThreadInfo>());
                info.Modules                    = SafeGetValue(() => GetModules(process, selfLink.ToString().TrimEnd('/') + "/modules"), Enumerable.Empty <ProcessModuleInfo>());
                info.TimeStamp                  = DateTime.UtcNow;
                info.EnvironmentVariables       = SafeGetValue(process.GetEnvironmentVariables, null);
                info.CommandLine                = SafeGetValue(process.GetCommandLine, null);
                info.IsProfileRunning           = ProfileManager.IsProfileRunning(process.Id);
                info.IsIisProfileRunning        = ProfileManager.IsIisProfileRunning(process.Id);
                info.IisProfileTimeoutInSeconds = ProfileManager.IisProfileTimeoutInSeconds;
                SetEnvironmentInfo(info);
            }

            return(info);
        }