static string PreprocessScript(string script, string outFile, LogProgress progress) { // Insert the output path in the script script = script.Replace("{LOG_FILE}", outFile); // Set the proper intruction pointer register script = script.Replace("{INSTRUCT_PTR}", is32bitDump ? "@eip" : "@rip"); // Adapt or remove the progress information if (progress != null) { script = progress.PrepareScript(script, outFile, is32bitDump); } else { script = LogProgress.RemoveProgressMark(script); } return(script); }
// Launches the debugger, which automatically executes a script and stores the output into a file public static void LaunchDebugger(string script, string outFile, bool showProgress = false) { LogProgress progress = showProgress ? new LogProgress() : null; // Set the path of the temporary script file string scriptFile = Path.Combine(Path.GetTempPath(), "WinDbgScript.txt"); // Replace special marks in the original script script = PreprocessScript(script, outFile, progress); // Remove old files File.Delete(scriptFile); File.Delete(outFile); // Create the script file using (StreamWriter stream = new StreamWriter(scriptFile)) stream.WriteLine(script); // Start the debugger string arguments = string.Format(@"-y ""{0};srv*{1}*http://msdl.microsoft.com/download/symbols"" -z ""{2}"" -c ""$$><{3};q""", config.PdbFolder, config.SymbolCache, config.DumpFile, scriptFile); ProcessStartInfo psi = new ProcessStartInfo { FileName = GetDebugger(), Arguments = arguments, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden // WinDBG will only hide the main window }; Process process = new Process(); process.StartInfo = psi; if (!process.Start()) { throw new Exception("The debugger could not be launched."); } Task <bool> task = LaunchDebuggerAsync(process); while (!task.IsCompleted) { task.Wait(500); if (showProgress) { progress.ShowLogProgress(); } } bool exited = task.Result; File.Delete(scriptFile); if (!exited) { process.Kill(); throw new Exception(String.Format("Execution has been cancelled after {0} minutes.", config.DbgTimeout)); } if (process.ExitCode != 0) { throw new Exception("The debugger did not finish properly."); } if (showProgress) { progress.DeleteProgressFile(); } // Check that the output log has been generated if (!File.Exists(outFile)) { throw new Exception("The debugger did not generate any output."); } }