/// <summary> /// Trigger a core dump generation. /// </summary> /// <param name="dumpType">Type of the dump to be generated</param> /// <param name="dumpPath">Full path to the dump to be generated. By default it is /tmp/coredump.{pid}</param> /// <param name="flags">logging and crash report flags. On runtimes less than 6.0, only LoggingEnabled is supported.</param> public void WriteDump(DumpType dumpType, string dumpPath, WriteDumpFlags flags) { IpcMessage request = CreateWriteDumpMessage2(dumpType, dumpPath, flags); IpcMessage response = IpcClient.SendMessage(_endpoint, request); if (!ValidateResponseMessage(response, nameof(WriteDump), ValidateResponseOptions.UnknownCommandReturnsFalse)) { if ((flags & ~WriteDumpFlags.LoggingEnabled) != 0) { throw new ArgumentException($"Only {nameof(WriteDumpFlags.LoggingEnabled)} flag is supported by this runtime version", nameof(flags)); } WriteDump(dumpType, dumpPath, logDumpGeneration: (flags & WriteDumpFlags.LoggingEnabled) != 0); } }
/// <summary> /// Trigger a core dump generation. /// </summary> /// <param name="dumpType">Type of the dump to be generated</param> /// <param name="dumpPath">Full path to the dump to be generated. By default it is /tmp/coredump.{pid}</param> /// <param name="flags">logging and crash report flags. On runtimes less than 6.0, only LoggingEnabled is supported.</param> /// <param name="token">The token to monitor for cancellation requests.</param> public async Task WriteDumpAsync(DumpType dumpType, string dumpPath, WriteDumpFlags flags, CancellationToken token) { IpcMessage request = CreateWriteDumpMessage2(dumpType, dumpPath, flags); IpcMessage response = await IpcClient.SendMessageAsync(_endpoint, request, token).ConfigureAwait(false); if (!ValidateResponseMessage(response, nameof(WriteDumpAsync), ValidateResponseOptions.UnknownCommandReturnsFalse)) { if ((flags & ~WriteDumpFlags.LoggingEnabled) != 0) { throw new ArgumentException($"Only {nameof(WriteDumpFlags.LoggingEnabled)} flag is supported by this runtime version", nameof(flags)); } await WriteDumpAsync(dumpType, dumpPath, logDumpGeneration : (flags & WriteDumpFlags.LoggingEnabled) != 0, token); } }
public int Collect(IConsole console, int processId, string output, bool diag, bool crashreport, DumpTypeOption type, string name) { Console.WriteLine(name); if (name != null) { if (processId != 0) { Console.WriteLine("Can only specify either --name or --process-id option."); return(0); } processId = CommandUtils.FindProcessIdWithName(name); if (processId < 0) { return(0); } } if (processId == 0) { console.Error.WriteLine("ProcessId is required."); return(1); } 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_{timestamp}.dmp" : $"core_{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 = null; switch (type) { case DumpTypeOption.Full: dumpTypeMessage = "full"; break; case DumpTypeOption.Heap: dumpTypeMessage = "dump with heap"; break; case DumpTypeOption.Mini: dumpTypeMessage = "dump"; break; } console.Out.WriteLine($"Writing {dumpTypeMessage} to {output}"); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (crashreport) { Console.WriteLine("Crash reports not supported on Windows."); return(0); } Windows.CollectDump(processId, output, type); } else { var client = new DiagnosticsClient(processId); DumpType dumpType = DumpType.Normal; switch (type) { case DumpTypeOption.Full: dumpType = DumpType.Full; break; case DumpTypeOption.Heap: dumpType = DumpType.WithHeap; break; case DumpTypeOption.Mini: dumpType = DumpType.Normal; break; case DumpTypeOption.Triage: dumpType = DumpType.Triage; break; } WriteDumpFlags flags = WriteDumpFlags.None; if (diag) { flags |= WriteDumpFlags.LoggingEnabled; } if (crashreport) { flags |= WriteDumpFlags.CrashReportEnabled; } // Send the command to the runtime to initiate the core dump client.WriteDump(dumpType, output, flags); } } catch (Exception ex) when (ex is FileNotFoundException || ex is ArgumentException || ex is DirectoryNotFoundException || ex is UnauthorizedAccessException || ex is PlatformNotSupportedException || ex is UnsupportedCommandException || ex is InvalidDataException || ex is InvalidOperationException || ex is NotSupportedException || ex is DiagnosticsClientException) { console.Error.WriteLine($"{ex.Message}"); return(1); } console.Out.WriteLine($"Complete"); return(0); }