Example #1
0
        public async Task <int> Collect(CommandLineApplication cmd, int processId, string output, bool diag, DumpTypeOption type)
        {
            if (processId == 0)
            {
                return(cmd.ExitWithError("ProcessId is required."));
            }

            try
            {
                if (output == null)
                {
                    // Build timestamp based file path
                    string timestamp = $"{DateTime.Now:yyyyMMdd_HHmmss}";
                    output = Path.Combine(Directory.GetCurrentDirectory(), RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? $"dump_{processId}_{timestamp}.dmp" : $"core_{processId}_{timestamp}");
                }
                // Make sure the dump path is NOT relative. This path could be sent to the runtime
                // process on Linux which may have a different current directory.
                output = Path.GetFullPath(output);

                // Display the type of dump and dump path
                string dumpTypeMessage = type == DumpTypeOption.Mini ? "minidump" : "minidump with heap";
                cmd.Out.WriteLine($"Writing {dumpTypeMessage} to {output}");

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    // Get the process
                    Process process = Process.GetProcessById(processId);

                    await Windows.CollectDumpAsync(process, output, type);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    var      client   = new DiagnosticsClient(processId);
                    DumpType dumpType = type == DumpTypeOption.Heap ? DumpType.WithHeap : DumpType.Normal;

                    // Send the command to the runtime to initiate the core dump
                    client.WriteDump(dumpType, output, diag);
                }
                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 ||
                ex is DiagnosticsClientException)
            {
                return(cmd.ExitWithError($"{ex.Message}"));
            }

            cmd.Out.WriteLine($"Complete");
            return(0);
        }
Example #2
0
        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);
        }
Example #3
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);
        }
Example #4
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.");
     }
 }