-2
我爲我的解釋器做了一個簡單的調試器。秒錶不會計時
它適用於除了一件事以外 - 無限循環檢測系統無法正常工作。它應該詢問用戶他是否想每20秒停止一次調試器,但沒有任何反應。
這裏是源代碼:
using System;
using System.Diagnostics;
using System.Text;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace MOPL.Debugger
{
class Debugger
{
static void Main(string[] args)
{
if (File.Exists("debug.log"))
{
File.Delete("debug.log");
}
handler = new ConsoleEventDelegate(ConsoleEventCallback);
SetConsoleCtrlHandler(handler, true);
bool stop = true, five = false;
if (Console.IsOutputRedirected || Console.IsErrorRedirected)
{
Console.WriteLine("This is a debugger, it shall not get debugged.");
return;
}
if (Console.IsInputRedirected)
{
Console.WriteLine("Control is not yet implemented.");
}
if (args.Count() < 1)
{
Console.WriteLine("Invalid file.");
return;
}
if (args.Count() > 1)
{
if (args[1].ToUpper() == "NOSTOP")
{
stop = false;
}
else
{
stop = true;
}
}
string exe = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + "MOPL.RunHelper.exe";
startInfo = new ProcessStartInfo(exe, args[0]);
if (!File.Exists(exe))
{
Console.WriteLine("The run helper was not found!");
return;
}
args[0] = args[0].Replace(":", "@");
args[0] = "deb:" + args[0];
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
App = Process.Start(startInfo);
try
{
Stopwatch s = new Stopwatch();
s.Stop();
int i = 0;
App.Start();
Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine("Debugging..." + "\n" + "Make sure that there is no infinite loop!");
while (!App.HasExited)
{
if ((s.Elapsed > TimeSpan.FromSeconds(20)))
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Red;
if (five == false)
{
Console.WriteLine("The debugger has been running for 20 secods.");
}
else
{
Console.WriteLine("20 more seconds have passed.");
}
Console.WriteLine("You may have made an infinite loop!");
stopdbg = true;
five = true;
s.Restart();
}
Errors.Append(App.StandardError.ReadToEnd());
Output.Append(App.StandardOutput.ReadToEnd());
App.StandardInput.WriteLine("0");
i++;
System.Threading.Thread.Sleep(75);
if (stopdbg)
{
stopdbg = true;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine();
Console.WriteLine("Do you want to stop debugging? [Y/N]");
bool quit = Ask();
if (quit == true)
break;
}
}
App.Close();
File.AppendAllText("debugger_errors.log", Errors.ToString());
File.AppendAllText("debugger_output.log", Output.ToString());
Safe = true;
Console.Clear();
Console.WriteLine();
Console.ResetColor();
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine("Data sent to debugger:");
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.WriteLine("{0}", Errors.ToString());
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine();
Console.WriteLine("Full output:");
Console.BackgroundColor = ConsoleColor.DarkYellow;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("{0}", Output.ToString());
if (File.Exists("debug.log"))
{
try
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine("Data sent to debug log:");
Console.BackgroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.DarkGreen;
string[] lines = File.ReadAllLines("debug.log");
foreach (string line in lines)
{
Console.WriteLine(line);
}
}
catch (Exception e)
{
Console.WriteLine("Failed to open debug.log ({0})", e);
};
}
Console.ResetColor();
if (stop) Console.ReadKey(true);
}
catch (Exception e)
{
App.Close();
Console.WriteLine("Debugger has crashed with exception: {0}", e);
if (stop) Console.ReadKey(true);
}
}
private static bool Ask()
{
while (true)
{
ConsoleKeyInfo result = Console.ReadKey(true);
if ((result.KeyChar == 'Y') || (result.KeyChar == 'y'))
{
return true;
}
else if ((result.KeyChar == 'N') || (result.KeyChar == 'n'))
{
return false;
}
}
}
static bool ConsoleEventCallback(int eventType)
{
if (eventType == 2)
{
if (!Safe)
{
App.Close();
File.AppendAllText("debugger_errors.log", Errors.ToString());
File.AppendAllText("debugger_output.log", Output.ToString());
Safe = true;
Console.WriteLine("The console will close, check the log files.");
}
return true;
}
return false;
}
static ConsoleEventDelegate handler;
static StringBuilder Errors = new StringBuilder();
static StringBuilder Output = new StringBuilder();
private static bool stopdbg = false;
static Process App;
static ProcessStartInfo startInfo;
private static bool Safe;
private delegate bool ConsoleEventDelegate(int eventType);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
}
}
你竟然用'Debugger'逐步執行代碼,找出在哪裏以及爲什麼發生這種情況..? – MethodMan
當使用秒錶和或計時器我希望看到一些'啓用/禁用'調用您的時間和徹底調試 – MethodMan
我強烈建議您閱讀[MCVE]指導 - 它往往有助於發現此類*之前*詢問嚴重低估的問題。 –