-
Notifications
You must be signed in to change notification settings - Fork 16
/
LogProgress.cs
80 lines (71 loc) · 3.25 KB
/
LogProgress.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
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace DumpReport
{
/// <summary>
/// Shows the progress of the debugger's log generation in the console.
/// </summary>
// The progress of the creation of the debugger's log file is measured by reading the number of bytes of an auxiliar
// file. For example, if the size is 5 and the total number of steps is 10, we know that the progress is 50%.
// This auxiliary file is created by using the command 'writemem', and its size is increased by one byte in every
// progress step.
class LogProgress
{
const string progressMark = "{PROGRESS_STEP}";
string progressCommand = @".writemem ""{file}"" {reg} L?{step}";
double maxSteps;
string progressFile; // Auxiliary file used to measure the progress of the creation of the log
// Inserts the command to measure the progress of the creation of the log
public string PrepareScript(string script, string outFile, bool is32bitDump)
{
// Create a temporary progress file
progressFile = outFile + ".prg";
File.Delete(progressFile); // Delete any older file
progressFile = progressFile.Replace("\\", "\\\\");
// Format the command used to measure the progress
progressCommand = progressCommand.Replace("{reg}", is32bitDump ? "@esp" : "@rsp");
progressCommand = progressCommand.Replace("{file}", progressFile);
// Get the number of progress steps in the script
maxSteps = Regex.Matches(script, progressMark).Count;
if (maxSteps == 0)
return script;
// Separate the script into progress steps
string[] steps = script.Split(new string[] { progressMark }, StringSplitOptions.None);
// Insert the command used to measure the progress into the main script
int numStep = 1;
string scriptWithProgress = "";
foreach (string step in steps)
{
scriptWithProgress += step;
if (numStep <= maxSteps)
scriptWithProgress += progressCommand.Replace("{step}", numStep.ToString());
++numStep;
}
return scriptWithProgress;
}
// Removes the progress marks from the debugger's script.
// It must be called when the progress is not enabled.
static public string RemoveProgressMark(string script)
{
return script.Replace(progressMark, String.Empty);
}
// Shows the progress of the creation of the log file by reading the size of the auxiliary file
public void ShowLogProgress()
{
double progress = 0;
if (File.Exists(progressFile))
{
// The number of bytes in the file indicates the current progress step
FileInfo fileInfo = new FileInfo(progressFile);
progress = 100.0 * (double)fileInfo.Length / maxSteps;
}
Console.Write("\rCreating log... {0}%", (int)progress);
}
public void DeleteProgressFile()
{
if (progressFile.Length > 0)
File.Delete(progressFile);
}
}
}