forked from dotnet/crank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDumper.cs
93 lines (82 loc) · 3.21 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
// 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 System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Crank.Models;
using Microsoft.Diagnostics.NETCore.Client;
namespace Microsoft.Crank.Agent
{
public partial class Dumper
{
public Dumper()
{
}
public int Collect(int processId, string outputFilePath, DumpTypeOption type)
{
try
{
// 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.
outputFilePath = Path.GetFullPath(outputFilePath);
// 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;
}
Log.Info($"Writing {dumpTypeMessage} to {outputFilePath}");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Get the process
Process process = Process.GetProcessById(processId);
Windows.CollectDump(process, outputFilePath, 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;
}
// Send the command to the runtime to initiate the core dump
client.WriteDump(dumpType, outputFilePath, logDumpGeneration: false);
}
}
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)
{
Log.Error(ex);
return 1;
}
Log.Info($"Dump complete");
return 0;
}
}
}