2016-10-04 7 views
-2

Ich habe einen einfachen Debugger für meinen Interpreter gemacht.Stoppuhr zählt keine Zeit

Es funktioniert perfekt für nur eine Sache - die Endlosschleife Erkennung funktioniert nicht. Es soll den Benutzer fragen, ob er den Debugger alle 20 Sekunden stoppen will, aber nichts passiert. Hier

ist der Quellcode:

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); 
    } 
} 
+2

haben Sie sogar die 'Debugger' verwendet, um zu Schritt durch den Code zu ändern, um herauszufinden, Wo und warum passiert das? – MethodMan

+0

Wenn ich eine Stoppuhr und/oder Timer verwende, würde ich erwarten, dass einige 'enable/disable' Calls sich Zeit nehmen und gründlich debuggen – MethodMan

+0

Ich empfehle dringend, [MCVE] Anleitung zu lesen - es hilft oft, Fehler wie diese * vor * zu erkennen stark nach unten gestellte Fragen stellen. –

Antwort

2

Try

Stopwatch s = new Stopwatch(); 
s.Stop(); 

zu

Stopwatch s = new Stopwatch(); 
s.Start();