2016-10-04 5 views
-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); 
    } 
} 
+2

당신도 찾기 위해 코드를 단계별로'Debugger'을 사용한 변경 : 여기

는 소스 코드 어디서 왜 이런 일이 .. ..? – MethodMan

+0

스톱워치 및/또는 타이머를 사용할 때 일부 활성화/비활성화 호출이 시간이 걸리고 철저히 디버깅 될 것으로 예상됩니다 – MethodMan

+0

[MCVE] 지침을 읽기를 강력히 권장합니다 - 전에 이와 같은 오류를 발견하는 데 도움이됩니다 * 무겁게 downvoted 질문을 요구. –

답변

2

시도

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

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