/// <summary> /// Attach the debugger /// </summary> /// <param name="pid">Process ID</param> /// <param name="callback">DebuggerInterruptCallback implementation</param> /// <returns></returns> public void AttachDebugger(int pid, DebuggerInterruptCallback callback) { CheckConnected(); if (IsDebugging || debugThread != null) { throw new Exception("libdbg: debugger already running?"); } IsDebugging = false; debugThread = new Thread(DebuggerThread) { IsBackground = true }; debugThread.Start(callback); // wait until server is started while (!IsDebugging) { Thread.Sleep(100); } SendCMDPacket(CMDS.CMD_DEBUG_ATTACH, CMD_DEBUG_ATTACH_PACKET_SIZE, pid); CheckStatus(); }
private void DebuggerThread(object obj) { DebuggerInterruptCallback callback = (DebuggerInterruptCallback)obj; IPAddress ip = IPAddress.Parse("0.0.0.0"); IPEndPoint endpoint = new IPEndPoint(ip, PS4DBG_DEBUG_PORT); Socket server = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp); server.Bind(endpoint); server.Listen(0); IsDebugging = true; Socket cl = server.Accept(); cl.NoDelay = true; cl.Blocking = false; while (IsDebugging) { if (cl.Available == DEBUG_INTERRUPT_SIZE) { byte[] data = new byte[DEBUG_INTERRUPT_SIZE]; int bytes = cl.Receive(data, DEBUG_INTERRUPT_SIZE, SocketFlags.None); if (bytes == DEBUG_INTERRUPT_SIZE) { DebuggerInterruptPacket packet = (DebuggerInterruptPacket)GetObjectFromBytes(data, typeof(DebuggerInterruptPacket)); callback(packet.lwpid, packet.status, packet.tdname, packet.reg64, packet.savefpu, packet.dbreg64); } } Thread.Sleep(100); } server.Close(); }