Ejemplo n.º 1
0
        public void RunClient(Object threadContext)
        {
            m_client = (TcpClient)threadContext;
            m_stream = m_client.GetStream();

            string remoteAddress = m_client.Client.RemoteEndPoint.ToString();
            var items = remoteAddress.Split(':');
            m_remoteHost = items[0];

            if (m_remoteHost != "127.0.0.1" && DebuggerServer.AllowedClients != "*" &&
                !DebuggerServer.AllowedClients.Contains(m_remoteHost))
            {
                Console.WriteLine("Host [" + m_remoteHost + "] is not allowed.");
                return;
            }

            Connected = true;

            Byte[] bytes = new Byte[4096];
            string data = null;
            Console.WriteLine("Starting client {0}", m_client.Client.RemoteEndPoint);

#if UNITY_EDITOR == false && UNITY_STANDALONE == false && __ANDROID__ == false && __IOS__ == false
      Interpreter.Instance.Init ();
#endif

            m_debugger = new Debugger();

            int i;
            try
            {
                while ((i = m_stream.Read(bytes, 0, bytes.Length)) != 0 && Connected)
                {
                    data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
                    string rest = "";
                    DebuggerUtils.DebugAction action = DebuggerUtils.StringToAction(data, ref rest);

                    if (action == DebuggerUtils.DebugAction.BYE)
                    {
                        break;
                    }
                    else if (m_debugger.CanProcess(action))
                    {
                        ThreadPool.QueueUserWorkItem(DebuggerServer.RunRequestBlocked, data);
                    }
                    else
                    {
                        DebuggerServer.Queue.Add(data);
                    }
                }
            }
            catch (Exception exc)
            {
                Console.Write("Client disconnected: {0}", exc.Message);
            }

            // Shutdown and end connection
            Console.Write("Closed connection.");
            Disconnect();
        }
Ejemplo n.º 2
0
        public void RunClient(Object threadContext)
        {
            m_client = (TcpClient)threadContext;
            m_stream = m_client.GetStream();
            Connected = true;

            Byte[] bytes = new Byte[4096];
            string data = null;
            Console.WriteLine("Starting client {0}", m_client.Client.RemoteEndPoint);

#if UNITY_EDITOR == false && UNITY_STANDALONE == false && __ANDROID__ == false && __IOS__ == false
      Interpreter.Instance.Init ();
#endif

            m_debugger = new Debugger();

            int i;
            try
            {
                while ((i = m_stream.Read(bytes, 0, bytes.Length)) != 0 && Connected)
                {
                    data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
                    string rest = "";
                    DebuggerUtils.DebugAction action = DebuggerUtils.StringToAction(data, ref rest);

                    if (action == DebuggerUtils.DebugAction.BYE)
                    {
                        break;
                    }
                    else if (action == DebuggerUtils.DebugAction.REPL)
                    {
                        m_isRepl = true;
                        DebuggerServer.Queue.Add(data);
                    }
                    else if (m_debugger.CanProcess(action))
                    {
                        ThreadPool.QueueUserWorkItem(DebuggerServer.RunRequestBlocked, data);
                    }
                    else
                    {
                        DebuggerServer.Queue.Add(data);
                    }
                }
            }
            catch (Exception exc)
            {
                Console.Write("Client disconnected: {0}", exc.Message);
            }

            if (m_debugger == Debugger.MainInstance)
            {
                Debugger.MainInstance = null;
            }

            // Shutdown and end connection
            Console.Write("Closed connection.");
            Disconnect();
        }
Ejemplo n.º 3
0
        async Task <string> ProcessRepl(string repl, string filename = "")
        {
            ReplMode = true;

            Dictionary <int, int> char2Line;
            string        script     = Utils.ConvertToScript(repl, out char2Line);
            ParsingScript tempScript = new ParsingScript(script, 0, char2Line);

            tempScript.OriginalScript = repl;
            tempScript.Debugger       = this;
            if (!string.IsNullOrWhiteSpace(filename))
            {
                tempScript.Filename = filename;
            }

            Variable result    = null;
            bool     excThrown = false;
            string   stringRes = "";

            try
            {
                while (tempScript.Pointer < script.Length)
                {
                    result = await DebuggerUtils.Execute(tempScript);

                    tempScript.GoToNextStatement();
                    while (tempScript.TryCurrent() == Constants.END_STATEMENT)
                    {
                        tempScript.Forward();
                    }
                }

                if (TrySendFile(result, tempScript, ref excThrown) || excThrown)
                {
                    return("");
                }

                stringRes = string.IsNullOrEmpty(Output) ? "" : Output + (Output.EndsWith("\n") ? "" : "\n");

                stringRes += result == null ? "" : result.AsString();
                stringRes += (stringRes.EndsWith("\n") ? "" : "\n");
            }
            catch (Exception exc)
            {
                Console.WriteLine("ProcessRepl Exception: " + exc);
                return(""); // The exception was already thrown and sent back.
            }
            finally
            {
                ReplMode = false;
            }


            return(stringRes);
        }
Ejemplo n.º 4
0
        public async Task <bool> ExecuteNextStatement()
        {
            int endGroupRead = 0;

            if (m_debugging.Pointer >= m_script.Length - 1)
            {
                LastResult = null;
                End        = true;
                return(true);
            }

            //int startPointer = m_debugging.Pointer;
            if (ProcessingBlock)
            {
                endGroupRead = m_debugging.GoToNextStatement();
                if (ProcessingBlock && endGroupRead > 0)
                {
                    return(true);
                }
            }

            Executing = true;
            try
            {
                LastResult = await DebuggerUtils.Execute(m_debugging);
            }
            catch (ParsingException exc)
            {
                if (m_debugging.InTryBlock)
                {
                    throw exc;
                }
                ProcessException(m_debugging, exc);
                return(true);
            }
            finally
            {
                Executing = false;
            }

            endGroupRead = m_debugging.GoToNextStatement();

            // Check if after this statement the Step In is completed and we can unwind the stack:
            bool completedSteppingIn = Completed(m_debugging) || (ProcessingBlock && endGroupRead > 0) ||
                                       LastResult.Type == Variable.VarType.CONTINUE ||
                                       LastResult.Type == Variable.VarType.BREAK ||
                                       LastResult.IsReturn;

            return(completedSteppingIn);
        }
Ejemplo n.º 5
0
        public bool ExecuteNextStatement()
        {
            int endGroupRead = 0;

            if (m_debugging.Pointer >= m_script.Length - 1)
            {
                LastResult = null;
#if UNITY_EDITOR == false && UNITY_STANDALONE == false && __ANDROID__ == false && __IOS__ == false
                End = true;
                Trace("END!");
#endif
                return(true);
            }

            //int startPointer = m_debugging.Pointer;
            if (ProcessingBlock)
            {
                endGroupRead = m_debugging.GoToNextStatement();
                if (ProcessingBlock && endGroupRead > 0)
                {
                    return(true);
                }
            }

            Executing = true;
            try
            {
                LastResult = DebuggerUtils.Execute(m_debugging);
            }
            catch (ParsingException exc)
            {
                ProcessException(m_debugging, exc);
                return(true);
            }
            finally
            {
                Executing = false;
            }

            endGroupRead = m_debugging.GoToNextStatement();

            //int endPointer = m_debugging.Pointer;
            //processed = m_debugging.Substr(startPointer, endPointer - startPointer);

            return(Completed(m_debugging) || (ProcessingBlock && endGroupRead > 0));
        }
Ejemplo n.º 6
0
        public async Task <bool> ExecuteNextStatement()
        {
            int endGroupRead = 0;

            if (m_debugging.Pointer >= m_script.Length - 1)
            {
                LastResult = null;
                End        = true;
                Trace("END of parsing");
                return(true);
            }

            //int startPointer = m_debugging.Pointer;
            if (ProcessingBlock)
            {
                endGroupRead = m_debugging.GoToNextStatement();
                if (ProcessingBlock && endGroupRead > 0)
                {
                    return(true);
                }
            }

            Executing = true;
            try
            {
                LastResult = await DebuggerUtils.Execute(m_debugging);
            }
            catch (ParsingException exc)
            {
                ProcessException(m_debugging, exc);
                return(true);
            }
            finally
            {
                Executing = false;
            }

            endGroupRead = m_debugging.GoToNextStatement();

            //int endPointer = m_debugging.Pointer;
            //processed = m_debugging.Substr(startPointer, endPointer - startPointer);

            return(Completed(m_debugging) || (ProcessingBlock && endGroupRead > 0));
        }
Ejemplo n.º 7
0
        public static void ProcessException(ParsingScript script, ParsingException exc)
        {
            Debugger debugger = script != null && script.Debugger != null ?
                                script.Debugger : MainInstance;

            if (debugger == null)
            {
                return;
            }

            if (debugger.ReplMode)
            {
                string replResult = DebuggerUtils.ResponseMainToken(DebuggerUtils.DebugAction.REPL) +
                                    "Exception thrown: " + exc.Message + "\n";
                debugger.SendBack(replResult, false);
                debugger.LastResult = null;
                ParserFunction.InvalidateStacksAfterLevel(0);
                return;
            }

            string stack     = exc.ExceptionStack;
            string vars      = debugger.GetAllVariables(script);
            int    varsCount = vars.Split('\n').Length;

            string result = "exc\n" + exc.Message + "\n";

            //result += exc. + "\n";
            result += varsCount + "\n";
            result += vars + "\n";
            result += stack + "\n";

            debugger.SendBack(result, !debugger.ReplMode);
            debugger.LastResult = null;

            ParserFunction.InvalidateStacksAfterLevel(0);
        }
Ejemplo n.º 8
0
        string ProcessRepl(string repl)
        {
            ReplMode = true;

            Dictionary <int, int> char2Line;
            string        script     = Utils.ConvertToScript(repl, out char2Line);
            ParsingScript tempScript = new ParsingScript(script, 0, char2Line);

            tempScript.OriginalScript = repl;
            tempScript.Debugger       = this;

            Variable result = null;

            try
            {
                while (tempScript.Pointer < script.Length)
                {
                    result = DebuggerUtils.Execute(tempScript);
                    tempScript.GoToNextStatement();
                }
            }
            catch (Exception exc)
            {
                return("Exception thrown: " + exc.Message);
            }
            finally
            {
                ReplMode = false;
            }

            string stringRes = Output + "\n";

            stringRes += result == null ? "" : result.AsString();

            return(stringRes);
        }
Ejemplo n.º 9
0
        async Task ProcessClientCommand(string data)
        {
            string load = "";

            DebuggerUtils.DebugAction action = DebuggerUtils.StringToAction(data, ref load);
            string result = "N/A";

            SteppingIn     = SteppingOut = false;
            SendBackResult = true;
            m_firstBlock   = true;
            End            = false;
            string responseToken = DebuggerUtils.ResponseMainToken(action);

            //Trace("REQUEST: " + data);
            if (action == DebuggerUtils.DebugAction.REPL ||
                action == DebuggerUtils.DebugAction._REPL)
            {
                string filename = "";
                if (action == DebuggerUtils.DebugAction.REPL)
                {
                    int ind = load.IndexOf('|');
                    if (ind >= 0)
                    {
                        if (ind > 0)
                        {
                            filename = load.Substring(0, ind);
                        }
                        if (ind + 1 < load.Length)
                        {
                            load = load.Substring(ind + 1);
                        }
                    }
                }
                result = await ProcessRepl(load, filename);

                result = responseToken + (result == null ? "" : result);
                SendBack(result, false);
                return;
            }
            if (action == DebuggerUtils.DebugAction.SET_BP)
            {
                TheBreakpoints.AddBreakpoints(this, load);
                return;
            }

            if (action == DebuggerUtils.DebugAction.FILE)
            {
                MainInstance = this;
                string filename  = load;
                string rawScript = Utils.GetFileContents(filename);

                if (string.IsNullOrWhiteSpace(rawScript))
                {
                    ProcessException(null, new ParsingException("Could not load script " + filename));
                    return;
                }

                try
                {
                    m_script = Utils.ConvertToScript(rawScript, out m_char2Line, filename);
                }
                catch (ParsingException exc)
                {
                    ProcessException(m_debugging, exc);
                    return;
                }
                m_debugging                = new ParsingScript(m_script, 0, m_char2Line);
                m_debugging.Filename       = filename;
                m_debugging.MainFilename   = m_debugging.Filename;
                m_debugging.OriginalScript = rawScript;
                m_debugging.Debugger       = this;

                m_steppingIns.Clear();
                m_completedStepIn.Reset();
                ProcessingBlock = SendBackResult = InInclude = End = false;
                m_blockLevel    = m_maxBlockLevel = 0;
            }
            else if (action == DebuggerUtils.DebugAction.VARS)
            {
                result = GetAllVariables(m_debugging);
            }
            else if (action == DebuggerUtils.DebugAction.ALL)
            {
                result = await DebugScript();
            }
            else if (action == DebuggerUtils.DebugAction.STACK)
            {
                result = GetStack();
            }
            else if (action == DebuggerUtils.DebugAction.CONTINUE)
            {
                Continue = true;
                action   = DebuggerUtils.DebugAction.NEXT;
            }
            else if (action == DebuggerUtils.DebugAction.STEP_IN)
            {
                SteppingIn = true;
                Continue   = false;
                action     = DebuggerUtils.DebugAction.NEXT;
            }
            else if (action == DebuggerUtils.DebugAction.STEP_OUT)
            {
                SteppingOut = true;
                Continue    = false;
                action      = DebuggerUtils.DebugAction.NEXT;
            }
            else if (action == DebuggerUtils.DebugAction.NEXT)
            {
                Continue = false;
            }
            else
            {
                Console.WriteLine("UNKNOWN CMD: {0}", data);
                return;
            }

            if (action == DebuggerUtils.DebugAction.NEXT)
            {
                if (m_debugging == null)
                {
                    result = "Error: Not initialized";
                    Console.WriteLine(result);
                }
                else
                {
                    Variable res = await ProcessNext();

                    //Trace("MAIN Ret:" + (LastResult != null && LastResult.IsReturn) +
                    //      " NULL:" + (res == null) + " db.sb=" + m_debugging.Debugger.SendBackResult +
                    //      " PTR:" + m_debugging.Pointer + "/" + m_script.Length);
                    if (End)
                    {
                        responseToken = DebuggerUtils.ResponseMainToken(DebuggerUtils.DebugAction.END);
                    }
                    else if (res == null || !SendBackResult)
                    {
                        // It will be processed by the stepped-out OR by completed stepped-in code.
                        return;
                    }
                    else
                    {
                        result = CreateResult(Output);
                        if (string.IsNullOrEmpty(result))
                        {
                            return;
                        }
                    }
                }
            }

            result = responseToken + result;
            SendBack(result, true);
        }
Ejemplo n.º 10
0
        void ProcessClientCommand(string data)
        {
            string load = "";

            DebuggerUtils.DebugAction action = DebuggerUtils.StringToAction(data, ref load);
            string result = "N/A";

            SteppingIn     = SteppingOut = false;
            SendBackResult = true;
            m_firstBlock   = true;
            string responseToken = DebuggerUtils.ResponseMainToken(action);

            Trace("REQUEST: " + data);
            if (action == DebuggerUtils.DebugAction.REPL ||
                action == DebuggerUtils.DebugAction._REPL)
            {
                result = responseToken + ProcessRepl(load);
                SendBack(result);
                return;
            }
            if (action == DebuggerUtils.DebugAction.SET_BP)
            {
                TheBreakpoints.AddBreakpoints(this, load);
                return;
            }

            if (action == DebuggerUtils.DebugAction.FILE)
            {
                MainInstance = this;
                string filename  = load;
                string rawScript = Utils.GetFileContents(filename);

                m_script                   = Utils.ConvertToScript(rawScript, out m_char2Line);
                m_debugging                = new ParsingScript(m_script, 0, m_char2Line);
                m_debugging.Filename       = filename;
                m_debugging.OriginalScript = m_script;
                m_debugging.Debugger       = this;
            }
            else if (action == DebuggerUtils.DebugAction.VARS)
            {
                result = GetAllVariables(m_debugging);
            }
            else if (action == DebuggerUtils.DebugAction.ALL)
            {
                result = DebugScript();
            }
            else if (action == DebuggerUtils.DebugAction.STACK)
            {
                result = GetStack();
            }
            else if (action == DebuggerUtils.DebugAction.CONTINUE)
            {
                Continue = true;
                action   = DebuggerUtils.DebugAction.NEXT;
            }
            else if (action == DebuggerUtils.DebugAction.STEP_IN)
            {
                SteppingIn = true;
                Continue   = false;
                action     = DebuggerUtils.DebugAction.NEXT;
            }
            else if (action == DebuggerUtils.DebugAction.STEP_OUT)
            {
                SteppingOut = true;
                Continue    = false;
                action      = DebuggerUtils.DebugAction.NEXT;
            }
            else if (action == DebuggerUtils.DebugAction.NEXT)
            {
                Continue = false;
            }
            else
            {
                Console.WriteLine("UNKNOWN CMD: {0}", data);
                return;
            }

            if (action == DebuggerUtils.DebugAction.NEXT)
            {
                if (m_debugging == null)
                {
                    result = "Error: Not initialized";
                    Console.WriteLine(result);
                }
                else
                {
                    Variable res = ProcessNext();
                    Trace("MAIN Ret:" + (LastResult != null && LastResult.IsReturn) +
                          " NULL:" + (res == null) + " db.sb=" + m_debugging.Debugger.SendBackResult +
                          " PTR:" + m_debugging.Pointer + "/" + m_script.Length);
                    if (End)
                    {
                        responseToken = DebuggerUtils.ResponseMainToken(DebuggerUtils.DebugAction.END);
                    }
                    else if (res == null || !SendBackResult)
                    {
                        // It will be processed by the stepped-out OR by completed stepped-in code.
                        return;
                    }
                    else
                    {
                        result = CreateResult(Output);
                    }
                }
            }

            result = responseToken + result;
            SendBack(result);
        }