forked from dotnet/diagnostics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertCommandHandler.cs
More file actions
105 lines (86 loc) · 3.73 KB
/
Copy pathConvertCommandHandler.cs
File metadata and controls
105 lines (86 loc) · 3.73 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.CommandLine;
using System.IO;
using System.Threading.Tasks;
using Graphs;
using Microsoft.Internal.Common;
namespace Microsoft.Diagnostics.Tools.GCDump
{
internal static class ConvertCommandHandler
{
public static int ConvertFile(FileInfo input, string output, bool verbose)
{
if (!input.Exists)
{
Console.Error.WriteLine($"File '{input.FullName}' does not exist.");
return -1;
}
output = string.IsNullOrEmpty(output)
? Path.ChangeExtension(input.FullName, "gcdump")
: output;
FileInfo outputFileInfo = new(output);
if (outputFileInfo.Exists)
{
outputFileInfo.Delete();
}
if (string.IsNullOrEmpty(outputFileInfo.Extension) || outputFileInfo.Extension != ".gcdump")
{
outputFileInfo = new FileInfo(outputFileInfo.FullName + ".gcdump");
}
Console.Out.WriteLine($"Writing gcdump to '{outputFileInfo.FullName}'...");
DotNetHeapInfo heapInfo = new();
TextWriter log = verbose ? Console.Out : TextWriter.Null;
MemoryGraph memoryGraph = new(50_000);
if (!EventPipeDotNetHeapDumper.DumpFromEventPipeFile(input.FullName, memoryGraph, log, heapInfo))
{
Console.Error.WriteLine($"Failed to convert '{input.FullName}' to gcdump. The input file may not be a valid nettrace file, or it may not contain GC heap events. Try running with '-v' for more information.");
return -1;
}
try
{
memoryGraph.AllowReading();
}
catch (ApplicationException ex)
{
Console.Error.WriteLine($"Failed to convert '{input.FullName}': {ex.Message} The nettrace file may not contain a complete GC heap dump. Try running with '-v' for more information.");
return -1;
}
GCHeapDump.WriteMemoryGraph(memoryGraph, outputFileInfo.FullName, "dotnet-gcdump");
return 0;
}
public static Command ConvertCommand()
{
Command convertCommand = new(
name: "convert",
description: "Converts nettrace file into .gcdump file handled by analysis tools. Can only convert from the nettrace format.")
{
InputPathArgument,
OutputPathOption,
VerboseOption
};
convertCommand.SetAction((parseResult, ct) => Task.FromResult(ConvertFile(
input: parseResult.GetValue(InputPathArgument),
output: parseResult.GetValue(OutputPathOption) ?? string.Empty,
verbose: parseResult.GetValue(VerboseOption))));
return convertCommand;
}
private static readonly Argument<FileInfo> InputPathArgument =
new Argument<FileInfo>("input")
{
Description = "Input trace file to be converted.",
Arity = new ArgumentArity(0, 1)
}.AcceptExistingOnly();
private static readonly Option<string> OutputPathOption =
new("--output", "-o")
{
Description = "The path where converted gcdump should be written. Defaults to '<input>.gcdump'",
};
private static readonly Option<bool> VerboseOption =
new("--verbose", "-v")
{
Description = "Output the log while converting the gcdump."
};
}
}