Esempio n. 1
0
        private void StepDebugger()
        {
            if (shouldReset)
            {
                if (!this.ResetDebugger())
                {
                    return;
                }
            }

            int oldLine = currentLine;

            RemoveCurrentHighlight();
            do
            {
                debugState = debugger.Step();

                UpdateState();

                if (shouldReset)
                {
                    return;
                }
            } while (currentLine <= 0 || oldLine == currentLine);

            UpdateStackPanel();
            UpdateGasCost();

            var targetLine = TextArea.Lines[currentLine];

            targetLine.EnsureVisible();

            targetLine.MarkerAdd(STEP_BG);

            var firstVisible = TextArea.FirstVisibleLine;

            var lastVisible = firstVisible + TextArea.LinesOnScreen;

            if (lastVisible > TextArea.Lines.Count)
            {
                lastVisible = TextArea.Lines.Count;
            }

            if (oldLine < 0)
            {
                targetLine.Goto();
            }
            else
            if (currentLine == oldLine + 1)
            {
                if (currentLine >= lastVisible)
                {
                    TextArea.LineScroll(1, 0);
                }
            }
            else
            {
                targetLine.Goto();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// executes the script until it finishes, fails or hits a breakpoint
        /// </summary>
        public DebuggerState Run()
        {
            do
            {
                lastState = Step();
            } while (lastState.state == DebuggerState.State.Running);

            return(lastState);
        }
Esempio n. 3
0
        public void Reset()
        {
            if (lastState.state == DebuggerState.State.Reset)
            {
                return;
            }

            engine = new ExecutionEngine(null, Crypto.Default, null, interop);
            engine.LoadScript(contractBytes);

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                var items = new Stack <object>();
                foreach (var item in ContractArgs)
                {
                    items.Push(item);
                }

                while (items.Count > 0)
                {
                    var item = items.Pop();

                    if (item == null)
                    {
                        sb.EmitPush("");
                    }
                    else
                    if (item is string)
                    {
                        sb.EmitPush((string)item);
                    }
                    else
                    if (item is int)
                    {
                        BigInteger num = (int)item;
                        sb.EmitPush(num);
                    }
                    else
                    {
                        throw new Exception("Unsupport contract param: " + item.ToString());
                    }
                }

                engine.LoadScript(sb.ToArray());
            }

            foreach (var pos in _breakpoints)
            {
                engine.AddBreakPoint((uint)pos);
            }

            engine.Reset();

            lastState = new DebuggerState(DebuggerState.State.Reset, 0);
        }
Esempio n. 4
0
        private void RunDebugger()
        {
            if (shouldReset)
            {
                if (!this.ResetDebugger())
                {
                    return;
                }
            }

            debugState = debugger.Run();
            UpdateState();
        }
Esempio n. 5
0
        private void RunDebugger()
        {
            if (this.debugger == null)
            {
                shouldReset = true;
            }

            if (shouldReset)
            {
                if (!this.ResetDebugger())
                {
                    return;
                }
            }

            debugState = debugger.Run();
            UpdateState();
        }
Esempio n. 6
0
        /// <summary>
        /// executes a single instruction in the current script, and returns the last script offset
        /// </summary>
        public DebuggerState Step()
        {
            if (lastState.state == DebuggerState.State.Finished || lastState.state == DebuggerState.State.Invalid)
            {
                return(lastState);
            }

            engine.ExecuteSingleStep();

            try
            {
                lastOffset = engine.CurrentContext.InstructionPointer;
            }
            catch
            {
                // failed to get instruction pointer
            }

            if (engine.State.HasFlag(VMState.FAULT))
            {
                lastState = new DebuggerState(DebuggerState.State.Exception, lastOffset);
                return(lastState);
            }

            if (engine.State.HasFlag(VMState.BREAK))
            {
                lastState = new DebuggerState(DebuggerState.State.Break, lastOffset);
                return(lastState);
            }

            if (engine.State.HasFlag(VMState.HALT))
            {
                lastState = new DebuggerState(DebuggerState.State.Finished, lastOffset);
                return(lastState);
            }

            lastState = new DebuggerState(DebuggerState.State.Running, lastOffset);
            return(lastState);
        }
        public void Reset()
        {
            if (lastState.state == DebuggerState.State.Reset)
            {
                return;
            }

            _usedGas = 0;

            engine = new ExecutionEngine(this, Crypto.Default, null, interop);
            engine.LoadScript(contractBytes);

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                var items = new Stack <object>();
                foreach (var item in ContractArgs)
                {
                    items.Push(item);
                }

                while (items.Count > 0)
                {
                    var item = items.Pop();
                    EmitObject(sb, item);
                }

                engine.LoadScript(sb.ToArray());
            }

            foreach (var pos in _breakpoints)
            {
                engine.AddBreakPoint((uint)pos);
            }

            engine.Reset();

            lastState = new DebuggerState(DebuggerState.State.Reset, 0);
        }
        /// <summary>
        /// executes a single instruction in the current script, and returns the last script offset
        /// </summary>
        public DebuggerState Step()
        {
            if (lastState.state == DebuggerState.State.Finished || lastState.state == DebuggerState.State.Invalid)
            {
                return(lastState);
            }

            engine.ExecuteSingleStep();

            try
            {
                lastOffset = engine.CurrentContext.InstructionPointer;

                var    opcode = engine.lastOpcode;
                double opCost;

                if (opcode <= OpCode.PUSH16)
                {
                    opCost = 0;
                }
                else
                {
                    switch (opcode)
                    {
                    case OpCode.SYSCALL:
                    {
                        var callInfo = interop.FindCall(engine.lastSysCall);
                        opCost = (callInfo != null) ? callInfo.gasCost : 0;

                        if (engine.lastSysCall.EndsWith("Storage.Put"))
                        {
                            opCost *= (Emulator.API.Storage.lastStorageLength / 1024.0);
                        }
                        break;
                    }

                    case OpCode.CHECKMULTISIG:
                    case OpCode.CHECKSIG: opCost = 0.1; break;

                    case OpCode.APPCALL:
                    case OpCode.TAILCALL:
                    case OpCode.SHA256:
                    case OpCode.SHA1: opCost = 0.01; break;

                    case OpCode.HASH256:
                    case OpCode.HASH160: opCost = 0.02; break;

                    case OpCode.NOP: opCost = 0; break;

                    default: opCost = 0.001; break;
                    }
                }

                _usedGas += opCost;
            }
            catch
            {
                // failed to get instruction pointer
            }

            if (engine.State.HasFlag(VMState.FAULT))
            {
                lastState = new DebuggerState(DebuggerState.State.Exception, lastOffset);
                return(lastState);
            }

            if (engine.State.HasFlag(VMState.BREAK))
            {
                lastState = new DebuggerState(DebuggerState.State.Break, lastOffset);
                return(lastState);
            }

            if (engine.State.HasFlag(VMState.HALT))
            {
                lastState = new DebuggerState(DebuggerState.State.Finished, lastOffset);
                return(lastState);
            }

            lastState = new DebuggerState(DebuggerState.State.Running, lastOffset);
            return(lastState);
        }