public async Task <int> Collect(IConsole console, int processId, string output, DumpType type) { if (processId == 0) { console.Error.WriteLine("ProcessId is required."); return(1); } try { // Get the process Process process = Process.GetProcessById(processId); if (output == null) { // Build timestamp based file path string timestamp = $"{DateTime.Now:yyyyMMdd_HHmmss}"; output = Path.Combine(Directory.GetCurrentDirectory(), RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? $"dump_{timestamp}.dmp" : $"core_{timestamp}"); } if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // Matches createdump's output on Linux string dumpType = type == DumpType.Mini ? "minidump" : "minidump with heap"; console.Out.WriteLine($"Writing {dumpType} to {output}"); await Windows.CollectDumpAsync(process, output, type); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { await Linux.CollectDumpAsync(process, output, type); } else { throw new PlatformNotSupportedException($"Unsupported operating system: {RuntimeInformation.OSDescription}"); } } catch (Exception ex) when (ex is FileNotFoundException || ex is DirectoryNotFoundException || ex is UnauthorizedAccessException || ex is PlatformNotSupportedException || ex is InvalidDataException || ex is InvalidOperationException || ex is NotSupportedException) { console.Error.WriteLine($"{ex.Message}"); return(1); } console.Out.WriteLine($"Complete"); return(0); }
public async Task <int> Collect(IConsole console, int processId, string outputDirectory) { if (processId == 0) { console.Error.WriteLine("ProcessId is required."); return(1); } // System.CommandLine has a bug in the default value handling if (outputDirectory == null) { outputDirectory = Directory.GetCurrentDirectory(); } // Get the process Process process = null; try { process = Process.GetProcessById(processId); } catch (Exception ex) when(ex is ArgumentException || ex is InvalidOperationException) { console.Error.WriteLine($"Invalid process id: {processId}"); return(1); } // Generate the file name string fileName = Path.Combine(outputDirectory, $"{process.ProcessName}-{process.Id}-{DateTime.Now:yyyyMMdd-HHmmss-fff}.dmp"); console.Out.WriteLine($"Collecting memory dump for {process.ProcessName} (ID: {process.Id}) ..."); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { await Windows.CollectDumpAsync(process, fileName); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { await Linux.CollectDumpAsync(process, fileName); } else { console.Error.WriteLine($"Unsupported operating system {RuntimeInformation.OSDescription}"); return(1); } console.Out.WriteLine($"Dump saved to {fileName}"); return(0); }
public static Task CollectDumpAsync(Process process, string fileName) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { return(Windows.CollectDumpAsync(process, fileName)); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { return(Linux.CollectDumpAsync(process, fileName)); } else { throw new PlatformNotSupportedException("Can't collect a memory dump on this platform."); } }