forked from xquintana/DumpReport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cs
304 lines (286 loc) · 14 KB
/
Config.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Xml;
using System.IO;
namespace DumpReport
{
/// <summary>
/// Contains the input parameters, specified from command line and from the XML configuration file.
/// </summary>
class Config
{
public string DbgExe64 { get; set; } // Full path of the 64-bit version debugger
public string DbgExe32 { get; set; } // Full path of the 32-bit version debugger
public Int32 DbgTimeout { get; set; } // Maximum number of minutes to wait for the debugger to finish
public string StyleFile { get; set; } // Full path of a custom CSS file to use
public string ReportFile { get; set; } // Full path of the report to be created
public bool ReportShow { get; set; } // If true, the report will be displayed automatically in the default browser
public bool QuietMode { get; set; } // If true. the application will not show progress messages in the console
public string SymbolCache { get; set; } // Folder to use as the debugger's symbol cache
public string DumpFile { get; set; } // Full path of the DMP file
public string PdbFolder { get; set; } // Folder where the PDBs are located
public string LogFile { get; set; } // Full path of the debugger's output file
public string LogFolder { get; set; } // Folder where the debugger's output file is stored
public bool LogClean { get; set; } // If true, log files are deleted after execution
public string SourceCodeRoot { get; set; } // Specifies a root folder for the source files
public Config()
{
DbgExe64 = String.Empty;
DbgExe32 = String.Empty;
DbgTimeout = 10;
StyleFile = String.Empty;
ReportFile = "DumpReport.html";
ReportShow = false;
QuietMode = false;
SymbolCache = "";
DumpFile = String.Empty;
PdbFolder = String.Empty;
LogFolder = String.Empty;
LogFile = String.Empty;
SourceCodeRoot = String.Empty;
}
// If the user requested for help, displays the help in the console and returns true.
// Otherwise returns false.
public bool CheckHelp(string[] args)
{
if (args.Length == 0 || (args.Length == 1 && args[0] == "/?"))
return PrintAppHelp();
for (int idx = 0; idx < args.Length; idx++)
{
if (args[idx] == "/CONFIG")
{
if ((idx + 1 < args.Length) && args[idx + 1] == "HELP")
return PrintConfigHelp();
if ((idx + 1 < args.Length) && args[idx + 1] == "CREATE")
return CreateConfigFile();
throw new ArgumentException("Use /CONFIG with HELP or CREATE");
}
if (args[idx] == "/STYLE")
{
if ((idx + 1 < args.Length) && args[idx + 1] == "HELP")
return PrintStyleHelp();
if ((idx + 1 < args.Length) && args[idx + 1] == "CREATE")
return CreateCSS();
throw new ArgumentException("Use /STYLE with HELP or CREATE");
}
}
return false;
}
// Reads the parameters from the configuration file
public void ReadConfigFile(string configPath)
{
string value;
ReportFile = Path.Combine(Program.appDirectory, ReportFile);
if (!File.Exists(configPath))
throw new Exception("Configuration file does not exist.\nPlease run 'DumpReport /CONFIG CREATE' to create it.");
try
{
using (XmlReader reader = XmlReader.Create(configPath))
{
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "Debugger":
value = reader["exe64"];
if (value != null && value.Length > 0)
DbgExe64 = value;
value = reader["exe32"];
if (value != null && value.Length > 0)
DbgExe32 = value;
value = reader["timeout"];
if (value != null && value.Length > 0)
DbgTimeout = Convert.ToInt32(value);
break;
case "Pdb":
value = reader["folder"];
if (value != null && value.Length > 0)
PdbFolder = value;
break;
case "Style":
value = reader["file"];
if (value != null && value.Length > 0)
StyleFile = value;
break;
case "Report":
value = reader["file"];
if (value != null && value.Length > 0)
ReportFile = Utils.GetAbsolutePath(value);
value = reader["show"];
if (value != null && value.Length > 0)
ReportShow = (value == "1");
break;
case "Log":
value = reader["folder"];
if (value != null && value.Length > 0)
LogFolder = value;
value = reader["clean"];
if (value != null && value.Length > 0)
LogClean = Convert.ToInt32(value) == 1;
break;
case "SymbolCache":
value = reader["folder"];
if (value != null && value.Length > 0)
SymbolCache = value;
break;
case "SourceCodeRoot":
value = reader["folder"];
if (value != null && value.Length > 0)
SourceCodeRoot = value.ToUpper();
break;
}
}
}
}
}
catch (Exception)
{
throw new Exception("Configuration file contains errors.\nPlease run 'DumpReport /CONFIG HELP' for XML syntax.");
}
}
// Reads the parameters from the command-line
public void ReadCommandLine(string[] args)
{
if (args.Length == 1 && Path.GetExtension(args[0]).ToUpper() == ".DMP")
{
if (!File.Exists(args[0])) throw new Exception("Dump file not found: " + args[0]);
DumpFile = args[0];
return;
}
try
{
for (int idx = 0; idx < args.Length; idx++)
{
if (args[idx] == "/DUMPFILE") DumpFile = GetParamValue(args, ref idx);
else if (args[idx] == "/PDBFOLDER") PdbFolder = GetParamValue(args, ref idx);
else if (args[idx] == "/REPORTFILE") ReportFile = GetParamValue(args, ref idx);
else if (args[idx] == "/SHOWREPORT") ReportShow = (GetParamValue(args, ref idx) == "1");
else if (args[idx] == "/QUIET") QuietMode = (GetParamValue(args, ref idx) == "1");
else throw new ArgumentException("Invalid parameter " + args[idx]);
}
if (DumpFile.Length == 0)
throw new ArgumentException("/DUMPFILE parameter not found");
}
catch (ArgumentException ex)
{
throw new ArgumentException(ex.Message + "\r\nPlease type 'DumpReport' for help." );
}
}
// Retrieves the value from the pair '/PARAMETER value'
string GetParamValue(string[] args, ref int idx)
{
if (idx + 1 >= args.Length || args[idx + 1].Length == 0 || args[idx + 1][0] == '/')
throw new ArgumentException("Value not found for parameter " + args[idx]);
return args[++idx];
}
// Checks that the files and folders exist and sets all paths to absolute paths.
public void CheckParams()
{
if (!File.Exists(DumpFile))
throw new ArgumentException("Dump file not found: " + DumpFile);
if (DbgExe64.Length == 0 && DbgExe32.Length == 0)
throw new ArgumentException("No debuggers specified in the configuration file.\r\nPlease type 'DumpReport /CONFIG HELP' for help.");
if (!Environment.Is64BitOperatingSystem && DbgExe32.Length == 0)
throw new Exception("The attribute 'exe32' must be set on 32-bit computers.");
if (DbgExe64.Length > 0 && !File.Exists(DbgExe64))
throw new ArgumentException("64-bit debugger not found: " + DbgExe64);
if (DbgExe32.Length > 0 && !File.Exists(DbgExe32))
throw new ArgumentException("32-bit debugger not found: " + DbgExe32);
if (PdbFolder.Length > 0 && !Directory.Exists(PdbFolder))
throw new ArgumentException("PDB folder not found: " + PdbFolder);
if (PdbFolder.Length == 0)
PdbFolder = Path.GetDirectoryName(DumpFile);
if (StyleFile.Length > 0 && !File.Exists(StyleFile))
throw new ArgumentException("Style file (CSS) not found: " + StyleFile);
if (LogFolder.Length > 0)
{
if (!Directory.Exists(LogFolder))
throw new ArgumentException("Invalid log folder " + LogFolder);
LogFile = Path.Combine(LogFolder, Path.GetFileName(DumpFile) + ".log");
}
else
LogFile = DumpFile + ".log";
if (SymbolCache.Length > 0 && !Directory.Exists(SymbolCache))
throw new ArgumentException("Symbol cache folder not found: " + SymbolCache);
// Set absolute paths
DbgExe64 = Utils.GetAbsolutePath(DbgExe64);
DbgExe32 = Utils.GetAbsolutePath(DbgExe32);
StyleFile = Utils.GetAbsolutePath(StyleFile);
ReportFile = Utils.GetAbsolutePath(ReportFile);
SymbolCache = Utils.GetAbsolutePath(SymbolCache);
DumpFile = Utils.GetAbsolutePath(DumpFile);
PdbFolder = Utils.GetAbsolutePath(PdbFolder);
LogFile = Utils.GetAbsolutePath(LogFile);
LogFolder = Utils.GetAbsolutePath(LogFolder);
}
static void PrintColor(string line, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine(line);
Console.ResetColor();
}
// Prints the application title to the console
static void PrintTitle()
{
PrintColor("\n" + Resources.appTitle, ConsoleColor.Yellow);
}
// Prints the application usage to the console
static bool PrintAppHelp()
{
PrintTitle();
Console.WriteLine(string.Format(Resources.appHelp, Path.GetFileName(Program.configFile)));
return true;
}
// Prints the configuration file syntax to the console
static bool PrintConfigHelp()
{
PrintTitle();
Console.WriteLine(string.Format(Resources.xmlHelpIntro, Path.GetFileName(Program.configFile)));
PrintColor("\r\nSample:\r\n", ConsoleColor.White);
Console.WriteLine(Resources.xml);
PrintColor("Nodes:", ConsoleColor.White);
Console.WriteLine(Resources.xmlHelpNodes);
return true;
}
// Prints the CSS file syntax to the console
static bool PrintStyleHelp()
{
PrintTitle();
Console.WriteLine(Resources.cssHelp);
return true;
}
// Creates an empty configuration file.
static bool CreateConfigFile()
{
string file = Program.configFile;
if (File.Exists(file))
{
Console.Write("File already exists. Overwrite? [Y/N] > ");
if (Console.ReadKey().Key != ConsoleKey.Y)
return true;
Console.WriteLine();
}
using (StreamWriter stream = new StreamWriter(file))
stream.WriteLine(Resources.xml);
Console.WriteLine("Configuration file created.\nPlease edit the path to the debuggers (WinDBG.exe or CDB.exe).");
return true;
}
// Creates a default CSS file (style.css).
static bool CreateCSS()
{
string file = Path.Combine(Program.appDirectory, "style.css");
if (File.Exists(file))
{
Console.Write("File " + file + " already exists. Overwrite? [Y/N] > ");
if (Console.ReadKey().Key != ConsoleKey.Y)
return true;
Console.WriteLine();
}
using (StreamWriter stream = new StreamWriter(file))
stream.WriteLine(Resources.css);
Console.WriteLine("File " + file + " has been created.\nEdit the <Style> entry in the XML configuration file in order to use it.");
return true;
}
}
}