-
Notifications
You must be signed in to change notification settings - Fork 358
/
Copy pathDumper.cs
120 lines (107 loc) · 4.66 KB
/
Dumper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Diagnostics.NETCore.Client;
using System;
using System.CommandLine;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Diagnostics.Tools.Dump
{
public partial class Dumper
{
/// <summary>
/// The dump type determines the kinds of information that are collected from the process.
/// </summary>
public enum DumpTypeOption
{
Full, // The largest dump containing all memory including the module images.
Heap, // A large and relatively comprehensive dump containing module lists, thread lists, all
// stacks, exception information, handle information, and all memory except for mapped images.
Mini, // A small dump containing module lists, thread lists, exception information and all stacks.
}
public Dumper()
{
}
public async Task<int> Collect(IConsole console, int processId, string output, bool diag, DumpTypeOption type)
{
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))
{
// 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 = 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;
}
// 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)
{
console.Error.WriteLine($"{ex.Message}");
return 1;
}
console.Out.WriteLine($"Complete");
return 0;
}
}
}