Quellcode durchsuchen

Move debugger logic to haxe library file

Hugh Sanderson vor 13 Jahren
Ursprung
Commit
7c58a72d3e
3 geänderte Dateien mit 118 neuen und 17 gelöschten Zeilen
  1. 104 0
      std/cpp/vm/DebugStdio.hx
  2. 13 16
      std/cpp/vm/Debugger.hx
  3. 1 1
      std/cpp/vm/Thread.hx

+ 104 - 0
std/cpp/vm/DebugStdio.hx

@@ -0,0 +1,104 @@
+package cpp.vm;
+
+
+class DebugStdio
+{
+   var inDebugger:Bool;
+   var inputThread:Thread;
+   var debugQueue:Deque<Dynamic>;
+   
+
+   public function new()
+   {
+      inDebugger = false;
+      Debugger.setHandler(onDebug);
+      debugQueue= new Deque<Dynamic>();
+      inputThread = Thread.create(inputLoop);
+   }
+
+   function onDebug()
+   {
+      inDebugger = true;
+      while(inDebugger)
+      {
+         var job = debugQueue.pop(true);
+         job();
+      }
+   }
+
+   
+   function waitDebugger()
+   {
+      debugQueue.add( function() inputThread.sendMessage("Ok")  );
+      var result = Thread.readMessage(true);
+      Sys.println(result);
+   }
+
+   function where()
+   {
+      var stack = haxe.Stack.callStack();
+      var idx = 0;
+      for(item in stack)
+      {
+         idx++;
+         Sys.println(idx + ":" + item);
+      }
+   }
+
+   function inputLoop()
+   {
+      var input = Sys.stdin();
+      while(true)
+      {
+         Sys.print("debug >");
+         var command = input.readLine();
+         switch(command)
+         {
+            case "exit":
+               Debugger.exit();
+
+            case "break":
+               if (inDebugger)
+                  Sys.println("already stopped.");
+               else
+               {
+                  Debugger.setBreak(Debugger.BRK_ASAP,inputThread);
+                  waitDebugger();
+               }
+
+            case "cont":
+               if (!inDebugger)
+                  Sys.println("Already running.");
+               else
+                  debugQueue.add( function() inDebugger = false );
+
+            case "where":
+               if (!inDebugger)
+                  Sys.println("Must break first.");
+               else
+               {
+                  debugQueue.add( function() where() );
+                  waitDebugger();
+               }
+
+
+               debugQueue.add( function() inDebugger = false );
+
+            case "": // ignore
+
+            case "help":
+               Sys.println("help  - print this message");
+               Sys.println("break - pause execution of one thread");
+               Sys.println("cont  - continue execution");
+               Sys.println("where - print call stack");
+               Sys.println("exit  - exit programme");
+
+            default:
+               Sys.println("Unknown command:" + command);
+         }
+      }
+   }
+
+}
+
+

+ 13 - 16
std/cpp/vm/Debugger.hx

@@ -6,37 +6,34 @@ typedef StackFrame = Dynamic;
 
 class Debugger
 {
+   public static inline var BRK_TERMINATE = -1;
+
+   public static inline var BRK_NONE  = 0;
+   public static inline var BRK_ASAP  = 1;
+   public static inline var BRK_STEP  = 2;
+   public static inline var BRK_ENTER = 3;
+   public static inline var BRK_LEAVE = 4;
+
    public static function setHandler(inHandler:Void->Void)
    {
       untyped __global__.__hxcpp_dbg_set_handler(inHandler);
    }
 
    // Generate a handler callback ASAP
-   public static function breakASAP(?inThread:Thread)
-   {
-      untyped __global__.__hxcpp_dbg_break_asap(inThread);
-   }
-
-   // Stepping
-   public static function stepOver(?inThread:Thread)
-   {
-      untyped __global__.__hxcpp_dbg_step_over(inThread);
-   }
-
-   public static function stepInto(?inThread:Thread)
+   public static function setBreak(inMode:Int,?inIgnoreThread:Thread)
    {
-      untyped __global__.__hxcpp_dbg_step_into(inThread);
+      untyped __global__.__hxcpp_dbg_set_break(inMode,inIgnoreThread==null?null:inIgnoreThread.handle);
    }
 
-   public static function stepOut(?inThread:Thread)
+   public static function exit()
    {
-      untyped __global__.__hxcpp_dbg_step_out(inThread);
+      untyped __global__.__hxcpp_dbg_set_break(BRK_TERMINATE,null);
    }
 
    // Breakpoint
    public static function addBreakpoint(inBreakpoint:Breakpoint)
    {
-      return untyped __global__.__hxcpp_breakpoints_add(inBreakpoint);
+      untyped __global__.__hxcpp_breakpoints_add(inBreakpoint);
    }
 
    public static function getBreakpoints() : Array<Breakpoint>

+ 1 - 1
std/cpp/vm/Thread.hx

@@ -28,7 +28,7 @@ typedef ThreadHandle = Dynamic;
 
 class Thread {
 
-	var handle : ThreadHandle;
+	public var handle(default,null) : ThreadHandle;
 
 	function new(h) {
 		handle = h;