2014-07-02 01:13:50 +03:00
|
|
|
package main
|
2014-05-27 11:42:37 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/ethereum/eth-go/ethchain"
|
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
|
|
|
"github.com/go-qml/qml"
|
2014-06-05 10:00:57 +03:00
|
|
|
"math/big"
|
2014-07-02 14:08:18 +03:00
|
|
|
"strconv"
|
2014-05-27 17:09:04 +03:00
|
|
|
"strings"
|
2014-05-27 11:42:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type DebuggerWindow struct {
|
|
|
|
win *qml.Window
|
|
|
|
engine *qml.Engine
|
|
|
|
lib *UiLib
|
2014-07-02 14:08:18 +03:00
|
|
|
|
|
|
|
vm *ethchain.Vm
|
|
|
|
Db *Debugger
|
|
|
|
breakPoints []int64
|
2014-05-27 11:42:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDebuggerWindow(lib *UiLib) *DebuggerWindow {
|
|
|
|
engine := qml.NewEngine()
|
|
|
|
component, err := engine.LoadFile(lib.AssetPath("debugger/debugger.qml"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
win := component.CreateWindow(nil)
|
2014-07-02 14:08:18 +03:00
|
|
|
db := &Debugger{win, make(chan bool), make(chan bool), true, false}
|
2014-05-27 11:42:37 +03:00
|
|
|
|
2014-07-02 14:08:18 +03:00
|
|
|
return &DebuggerWindow{engine: engine, win: win, lib: lib, Db: db, vm: ðchain.Vm{}}
|
2014-05-27 11:42:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *DebuggerWindow) Show() {
|
2014-05-27 14:09:47 +03:00
|
|
|
context := self.engine.Context()
|
|
|
|
context.SetVar("dbg", self)
|
|
|
|
|
2014-05-27 11:42:37 +03:00
|
|
|
go func() {
|
|
|
|
self.win.Show()
|
|
|
|
self.win.Wait()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-05-28 14:00:45 +03:00
|
|
|
func (self *DebuggerWindow) SetCode(code string) {
|
|
|
|
self.win.Set("codeText", code)
|
|
|
|
}
|
2014-05-27 17:09:04 +03:00
|
|
|
|
2014-05-28 14:00:45 +03:00
|
|
|
func (self *DebuggerWindow) SetData(data string) {
|
|
|
|
self.win.Set("dataText", data)
|
2014-05-27 17:09:04 +03:00
|
|
|
}
|
2014-07-04 14:04:25 +03:00
|
|
|
func (self *DebuggerWindow) SetAsm(data []byte) {
|
|
|
|
self.win.Root().Call("clearAsm")
|
|
|
|
|
|
|
|
dis := ethchain.Disassemble(data)
|
2014-05-28 16:48:17 +03:00
|
|
|
for _, str := range dis {
|
|
|
|
self.win.Root().Call("setAsm", str)
|
|
|
|
}
|
|
|
|
}
|
2014-05-27 17:09:04 +03:00
|
|
|
|
2014-07-04 14:04:25 +03:00
|
|
|
func (self *DebuggerWindow) Compile(code string) {
|
|
|
|
var err error
|
|
|
|
script := ethutil.StringToByteFunc(code, func(s string) (ret []byte) {
|
2014-07-04 14:42:43 +03:00
|
|
|
ret, err = ethutil.Compile(s, true)
|
2014-07-04 14:04:25 +03:00
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
self.SetAsm(script)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 16:31:13 +03:00
|
|
|
func (self *DebuggerWindow) ClearLog() {
|
|
|
|
self.win.Root().Call("clearLog")
|
|
|
|
}
|
|
|
|
|
2014-05-27 17:09:04 +03:00
|
|
|
func (self *DebuggerWindow) Debug(valueStr, gasStr, gasPriceStr, scriptStr, dataStr string) {
|
2014-05-27 14:28:11 +03:00
|
|
|
if !self.Db.done {
|
|
|
|
self.Db.Q <- true
|
|
|
|
}
|
|
|
|
|
2014-06-09 23:04:16 +03:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
self.Logf("compile FAULT: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-05-28 14:00:45 +03:00
|
|
|
data := ethutil.StringToByteFunc(dataStr, func(s string) (ret []byte) {
|
|
|
|
slice := strings.Split(dataStr, "\n")
|
|
|
|
for _, dataItem := range slice {
|
2014-05-29 00:14:23 +03:00
|
|
|
d := ethutil.FormatData(dataItem)
|
2014-05-28 14:00:45 +03:00
|
|
|
ret = append(ret, d...)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
var err error
|
|
|
|
script := ethutil.StringToByteFunc(scriptStr, func(s string) (ret []byte) {
|
2014-07-04 14:42:43 +03:00
|
|
|
ret, err = ethutil.Compile(s, false)
|
2014-05-28 14:00:45 +03:00
|
|
|
return
|
|
|
|
})
|
|
|
|
|
2014-05-27 11:42:37 +03:00
|
|
|
if err != nil {
|
2014-05-29 00:14:23 +03:00
|
|
|
self.Logln(err)
|
2014-05-27 11:42:37 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-04 14:04:25 +03:00
|
|
|
self.SetAsm(script)
|
2014-05-28 14:00:45 +03:00
|
|
|
|
2014-06-26 13:10:11 +03:00
|
|
|
var (
|
|
|
|
gas = ethutil.Big(gasStr)
|
|
|
|
gasPrice = ethutil.Big(gasPriceStr)
|
|
|
|
value = ethutil.Big(valueStr)
|
|
|
|
// Contract addr as test address
|
2014-07-04 14:04:25 +03:00
|
|
|
keyPair = self.lib.eth.KeyManager().KeyPair()
|
2014-06-26 13:10:11 +03:00
|
|
|
)
|
2014-05-27 11:42:37 +03:00
|
|
|
|
2014-07-04 14:04:25 +03:00
|
|
|
state := self.lib.eth.StateManager().TransState()
|
2014-05-27 14:09:47 +03:00
|
|
|
account := self.lib.eth.StateManager().TransState().GetAccount(keyPair.Address())
|
2014-07-04 14:04:25 +03:00
|
|
|
contract := ethchain.NewStateObject([]byte{0})
|
2014-06-26 13:10:11 +03:00
|
|
|
contract.Amount = value
|
2014-07-04 14:04:25 +03:00
|
|
|
|
2014-06-05 10:00:57 +03:00
|
|
|
callerClosure := ethchain.NewClosure(account, contract, script, state, gas, gasPrice)
|
2014-05-27 11:42:37 +03:00
|
|
|
|
|
|
|
block := self.lib.eth.BlockChain().CurrentBlock
|
|
|
|
vm := ethchain.NewVm(state, self.lib.eth.StateManager(), ethchain.RuntimeVars{
|
2014-06-20 01:42:53 +03:00
|
|
|
Block: block,
|
2014-05-27 11:42:37 +03:00
|
|
|
Origin: account.Address(),
|
2014-06-20 01:42:53 +03:00
|
|
|
BlockNumber: block.Number,
|
2014-05-27 11:42:37 +03:00
|
|
|
PrevHash: block.PrevHash,
|
|
|
|
Coinbase: block.Coinbase,
|
|
|
|
Time: block.Time,
|
|
|
|
Diff: block.Difficulty,
|
2014-05-29 00:14:23 +03:00
|
|
|
Value: ethutil.Big(valueStr),
|
2014-05-27 11:42:37 +03:00
|
|
|
})
|
2014-06-19 14:47:18 +03:00
|
|
|
vm.Verbose = true
|
2014-07-02 02:04:32 +03:00
|
|
|
vm.Hook = self.Db.halting
|
2014-07-02 14:08:18 +03:00
|
|
|
vm.BreakPoints = self.breakPoints
|
2014-05-27 11:42:37 +03:00
|
|
|
|
2014-07-02 14:08:18 +03:00
|
|
|
self.vm = vm
|
2014-05-27 11:42:37 +03:00
|
|
|
self.Db.done = false
|
2014-06-05 10:00:57 +03:00
|
|
|
self.Logf("callsize %d", len(script))
|
2014-05-27 11:42:37 +03:00
|
|
|
go func() {
|
2014-07-02 02:04:32 +03:00
|
|
|
ret, g, err := callerClosure.Call(vm, data)
|
2014-06-06 13:13:13 +03:00
|
|
|
tot := new(big.Int).Mul(g, gasPrice)
|
|
|
|
self.Logf("gas usage %v total price = %v (%v)", g, tot, ethutil.CurrencyToString(tot))
|
2014-05-29 00:14:23 +03:00
|
|
|
if err != nil {
|
|
|
|
self.Logln("exited with errors:", err)
|
|
|
|
} else {
|
2014-06-05 10:00:57 +03:00
|
|
|
if len(ret) > 0 {
|
|
|
|
self.Logf("exited: % x", ret)
|
|
|
|
} else {
|
|
|
|
self.Logf("exited: nil")
|
|
|
|
}
|
2014-05-29 00:14:23 +03:00
|
|
|
}
|
2014-05-27 11:42:37 +03:00
|
|
|
|
|
|
|
state.Reset()
|
|
|
|
|
2014-06-09 23:04:16 +03:00
|
|
|
if !self.Db.interrupt {
|
|
|
|
self.Db.done = true
|
|
|
|
} else {
|
|
|
|
self.Db.interrupt = false
|
|
|
|
}
|
2014-05-27 11:42:37 +03:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-05-29 00:14:23 +03:00
|
|
|
func (self *DebuggerWindow) Logf(format string, v ...interface{}) {
|
|
|
|
self.win.Root().Call("setLog", fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *DebuggerWindow) Logln(v ...interface{}) {
|
|
|
|
str := fmt.Sprintln(v...)
|
|
|
|
self.Logf("%s", str[:len(str)-1])
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:42:37 +03:00
|
|
|
func (self *DebuggerWindow) Next() {
|
|
|
|
self.Db.Next()
|
|
|
|
}
|
2014-05-27 14:09:47 +03:00
|
|
|
|
2014-07-02 14:08:18 +03:00
|
|
|
func (self *DebuggerWindow) Continue() {
|
|
|
|
self.vm.Stepping = false
|
|
|
|
self.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *DebuggerWindow) ExecCommand(command string) {
|
|
|
|
if len(command) > 0 {
|
|
|
|
cmd := strings.Split(command, " ")
|
|
|
|
switch cmd[0] {
|
|
|
|
case "help":
|
2014-07-04 14:04:25 +03:00
|
|
|
self.Logln("Debugger commands:")
|
2014-07-04 16:31:13 +03:00
|
|
|
self.Logln("break, bp Set breakpoint on instruction")
|
|
|
|
self.Logln("clear [log, break, bp] Clears previous set sub-command(s)")
|
2014-07-02 14:08:18 +03:00
|
|
|
case "break", "bp":
|
|
|
|
if len(cmd) > 1 {
|
|
|
|
lineNo, err := strconv.Atoi(cmd[1])
|
|
|
|
if err != nil {
|
|
|
|
self.Logln(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
self.breakPoints = append(self.breakPoints, int64(lineNo))
|
|
|
|
self.vm.BreakPoints = self.breakPoints
|
|
|
|
self.Logf("break point set on instruction %d", lineNo)
|
|
|
|
} else {
|
|
|
|
self.Logf("'%s' requires line number", cmd[0])
|
|
|
|
}
|
|
|
|
case "clear":
|
|
|
|
if len(cmd) > 1 {
|
|
|
|
switch cmd[1] {
|
|
|
|
case "break", "bp":
|
|
|
|
self.breakPoints = nil
|
|
|
|
self.vm.BreakPoints = nil
|
|
|
|
|
|
|
|
self.Logln("Breakpoints cleared")
|
2014-07-04 16:31:13 +03:00
|
|
|
case "log":
|
|
|
|
self.ClearLog()
|
2014-07-02 14:08:18 +03:00
|
|
|
default:
|
|
|
|
self.Logf("clear '%s' is not valid", cmd[1])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.Logln("'clear' requires sub command")
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
self.Logf("Unknown command %s", cmd[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 14:09:47 +03:00
|
|
|
type Debugger struct {
|
2014-06-09 23:04:16 +03:00
|
|
|
win *qml.Window
|
|
|
|
N chan bool
|
|
|
|
Q chan bool
|
|
|
|
done, interrupt bool
|
2014-05-27 14:09:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type storeVal struct {
|
|
|
|
Key, Value string
|
|
|
|
}
|
|
|
|
|
2014-05-27 14:28:11 +03:00
|
|
|
func (d *Debugger) halting(pc int, op ethchain.OpCode, mem *ethchain.Memory, stack *ethchain.Stack, stateObject *ethchain.StateObject) bool {
|
2014-05-27 14:09:47 +03:00
|
|
|
d.win.Root().Call("setInstruction", pc)
|
|
|
|
d.win.Root().Call("clearMem")
|
|
|
|
d.win.Root().Call("clearStack")
|
|
|
|
d.win.Root().Call("clearStorage")
|
|
|
|
|
|
|
|
addr := 0
|
|
|
|
for i := 0; i+32 <= mem.Len(); i += 32 {
|
|
|
|
d.win.Root().Call("setMem", memAddr{fmt.Sprintf("%03d", addr), fmt.Sprintf("% x", mem.Data()[i:i+32])})
|
|
|
|
addr++
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range stack.Data() {
|
|
|
|
d.win.Root().Call("setStack", val.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
stateObject.State().EachStorage(func(key string, node *ethutil.Value) {
|
|
|
|
d.win.Root().Call("setStorage", storeVal{fmt.Sprintf("% x", key), fmt.Sprintf("% x", node.Str())})
|
|
|
|
})
|
|
|
|
|
2014-07-02 14:08:18 +03:00
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-d.N:
|
|
|
|
break out
|
|
|
|
case <-d.Q:
|
|
|
|
d.interrupt = true
|
|
|
|
d.clearBuffers()
|
|
|
|
|
|
|
|
return false
|
2014-05-27 14:09:47 +03:00
|
|
|
}
|
|
|
|
}
|
2014-05-27 14:28:11 +03:00
|
|
|
|
|
|
|
return true
|
2014-05-27 14:09:47 +03:00
|
|
|
}
|
|
|
|
|
2014-06-09 23:04:16 +03:00
|
|
|
func (d *Debugger) clearBuffers() {
|
|
|
|
out:
|
|
|
|
// drain
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-d.N:
|
|
|
|
case <-d.Q:
|
|
|
|
default:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 14:09:47 +03:00
|
|
|
func (d *Debugger) Next() {
|
|
|
|
if !d.done {
|
|
|
|
d.N <- true
|
|
|
|
}
|
|
|
|
}
|