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); } } }
public HttpResponseMessage GCDump(int id, int maxDumpCountK = 0, string format = null) { using (_tracer.Step("ProcessController.GCDump")) { DumpFormat dumpFormat = ParseDumpFormat(format, DumpFormat.DiagSession); var process = GetProcessById(id); var ext = dumpFormat == DumpFormat.DiagSession ? "diagsession" : "gcdump"; string dumpFile = Path.Combine(_environment.LogFilesPath, "minidump", "dump." + ext); FileSystemHelpers.EnsureDirectory(_fileSystem, Path.GetDirectoryName(dumpFile)); FileSystemHelpers.DeleteFileSafe(_fileSystem, dumpFile); string resourcePath = GetResponseFileName(process.ProcessName, "gcdump"); try { using (_tracer.Step(String.Format("GCDump pid={0}, name={1}, file={2}", process.Id, process.ProcessName, dumpFile))) { process.GCDump(dumpFile, resourcePath, maxDumpCountK, _tracer, _settings.GetCommandIdleTimeout()); _tracer.Trace("GCDump size={0}", new FileInfo(dumpFile).Length); } } catch (Exception ex) { _tracer.TraceError(ex); FileSystemHelpers.DeleteFileSafe(_fileSystem, dumpFile); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } if (dumpFormat == DumpFormat.Zip) { string responseFileName = GetResponseFileName(process.ProcessName, "zip"); HttpResponseMessage response = Request.CreateResponse(); response.Content = ZipStreamContent.Create(responseFileName, _tracer, zip => { try { zip.AddFile(dumpFile, String.Empty); } finally { FileSystemHelpers.DeleteFileSafe(_fileSystem, dumpFile); } }); return(response); } else { string responseFileName = GetResponseFileName(process.ProcessName, ext); HttpResponseMessage response = Request.CreateResponse(); response.Content = new StreamContent(FileStreamWrapper.OpenRead(dumpFile, _fileSystem)); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentDisposition.FileName = responseFileName; return(response); } } }
public HttpResponseMessage MiniDump(int id, int dumpType = 0, string format = null) { using (_tracer.Step("ProcessController.MiniDump")) { DumpFormat dumpFormat = ParseDumpFormat(format, DumpFormat.Raw); if (dumpFormat != DumpFormat.Raw && dumpFormat != DumpFormat.Zip) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, String.Format(CultureInfo.CurrentCulture, Resources.Error_DumpFormatNotSupported, dumpFormat))); } string sitePolicy = _settings.GetWebSitePolicy(); if ((MINIDUMP_TYPE)dumpType == MINIDUMP_TYPE.WithFullMemory && sitePolicy.Equals(FreeSitePolicy, StringComparison.OrdinalIgnoreCase)) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, String.Format(CultureInfo.CurrentCulture, Resources.Error_FullMiniDumpNotSupported, sitePolicy))); } var process = GetProcessById(id); string dumpFile = Path.Combine(_environment.LogFilesPath, "minidump", "minidump.dmp"); FileSystemHelpers.EnsureDirectory(_fileSystem, Path.GetDirectoryName(dumpFile)); FileSystemHelpers.DeleteFileSafe(_fileSystem, dumpFile); try { using (_tracer.Step(String.Format("MiniDump pid={0}, name={1}, file={2}", process.Id, process.ProcessName, dumpFile))) { process.MiniDump(dumpFile, (MINIDUMP_TYPE)dumpType); _tracer.Trace("MiniDump size={0}", new FileInfo(dumpFile).Length); } } catch (Exception ex) { _tracer.TraceError(ex); FileSystemHelpers.DeleteFileSafe(_fileSystem, dumpFile); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } if (dumpFormat == DumpFormat.Raw) { string responseFileName = GetResponseFileName(process.ProcessName, "dmp"); HttpResponseMessage response = Request.CreateResponse(); response.Content = new StreamContent(FileStreamWrapper.OpenRead(dumpFile, _fileSystem)); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentDisposition.FileName = responseFileName; return(response); } else if (dumpFormat == DumpFormat.Zip) { string responseFileName = GetResponseFileName(process.ProcessName, "zip"); HttpResponseMessage response = Request.CreateResponse(); response.Content = ZipStreamContent.Create(responseFileName, _tracer, zip => { try { zip.AddFile(dumpFile, String.Empty); } finally { FileSystemHelpers.DeleteFileSafe(_fileSystem, dumpFile); } foreach (var fileName in new[] { "sos.dll", "mscordacwks.dll" }) { string filePath = Path.Combine(ProcessExtensions.ClrRuntimeDirectory, fileName); if (_fileSystem.File.Exists(filePath)) { zip.AddFile(filePath, String.Empty); } } }); return(response); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, String.Format(CultureInfo.CurrentCulture, Resources.Error_DumpFormatNotSupported, dumpFormat))); } } }