2014-05-27 08:42:37 +00:00
|
|
|
package ethui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/ethereum/eth-go/ethchain"
|
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
|
|
|
"github.com/go-qml/qml"
|
2014-05-27 14:09:04 +00:00
|
|
|
"strings"
|
2014-05-27 08:42:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DebuggerWindow struct {
|
|
|
|
win *qml.Window
|
|
|
|
engine *qml.Engine
|
|
|
|
lib *UiLib
|
|
|
|
Db *Debugger
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-27 11:28:11 +00:00
|
|
|
db := &Debugger{win, make(chan bool), make(chan bool), true}
|
2014-05-27 08:42:37 +00:00
|
|
|
|
|
|
|
return &DebuggerWindow{engine: engine, win: win, lib: lib, Db: db}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *DebuggerWindow) Show() {
|
2014-05-27 11:09:47 +00:00
|
|
|
context := self.engine.Context()
|
|
|
|
context.SetVar("dbg", self)
|
|
|
|
|
2014-05-27 08:42:37 +00:00
|
|
|
go func() {
|
|
|
|
self.win.Show()
|
|
|
|
self.win.Wait()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-05-28 11:00:45 +00:00
|
|
|
func (self *DebuggerWindow) SetCode(code string) {
|
|
|
|
self.win.Set("codeText", code)
|
|
|
|
}
|
2014-05-27 14:09:04 +00:00
|
|
|
|
2014-05-28 11:00:45 +00:00
|
|
|
func (self *DebuggerWindow) SetData(data string) {
|
|
|
|
self.win.Set("dataText", data)
|
2014-05-27 14:09:04 +00:00
|
|
|
}
|
2014-05-28 13:48:17 +00:00
|
|
|
func (self *DebuggerWindow) SetAsm(data string) {
|
|
|
|
dis := ethchain.Disassemble(ethutil.FromHex(data))
|
|
|
|
for _, str := range dis {
|
|
|
|
self.win.Root().Call("setAsm", str)
|
|
|
|
}
|
|
|
|
}
|
2014-05-27 14:09:04 +00:00
|
|
|
|
|
|
|
func (self *DebuggerWindow) Debug(valueStr, gasStr, gasPriceStr, scriptStr, dataStr string) {
|
2014-05-27 11:28:11 +00:00
|
|
|
if !self.Db.done {
|
|
|
|
self.Db.Q <- true
|
|
|
|
}
|
|
|
|
|
2014-05-28 11:00:45 +00:00
|
|
|
data := ethutil.StringToByteFunc(dataStr, func(s string) (ret []byte) {
|
|
|
|
slice := strings.Split(dataStr, "\n")
|
|
|
|
for _, dataItem := range slice {
|
2014-05-28 21:14:23 +00:00
|
|
|
d := ethutil.FormatData(dataItem)
|
2014-05-28 11:00:45 +00:00
|
|
|
ret = append(ret, d...)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
var err error
|
|
|
|
script := ethutil.StringToByteFunc(scriptStr, func(s string) (ret []byte) {
|
|
|
|
ret, err = ethutil.Compile(s)
|
2014-05-29 00:05:06 +00:00
|
|
|
fmt.Printf("%x\n", ret)
|
2014-05-28 11:00:45 +00:00
|
|
|
return
|
|
|
|
})
|
|
|
|
|
2014-05-27 08:42:37 +00:00
|
|
|
if err != nil {
|
2014-05-28 21:14:23 +00:00
|
|
|
self.Logln(err)
|
2014-05-27 08:42:37 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dis := ethchain.Disassemble(script)
|
2014-05-27 11:28:11 +00:00
|
|
|
self.win.Root().Call("clearAsm")
|
2014-05-27 08:42:37 +00:00
|
|
|
|
|
|
|
for _, str := range dis {
|
2014-05-27 11:09:47 +00:00
|
|
|
self.win.Root().Call("setAsm", str)
|
2014-05-27 08:42:37 +00:00
|
|
|
}
|
2014-05-28 11:00:45 +00:00
|
|
|
|
2014-05-27 08:42:37 +00:00
|
|
|
// Contract addr as test address
|
|
|
|
keyPair := ethutil.GetKeyRing().Get(0)
|
|
|
|
callerTx := ethchain.NewContractCreationTx(ethutil.Big(valueStr), ethutil.Big(gasStr), ethutil.Big(gasPriceStr), script)
|
|
|
|
callerTx.Sign(keyPair.PrivateKey)
|
|
|
|
|
2014-05-28 11:00:45 +00:00
|
|
|
state := self.lib.eth.BlockChain().CurrentBlock.State()
|
2014-05-27 11:09:47 +00:00
|
|
|
account := self.lib.eth.StateManager().TransState().GetAccount(keyPair.Address())
|
2014-05-27 08:42:37 +00:00
|
|
|
contract := ethchain.MakeContract(callerTx, state)
|
2014-05-27 14:09:04 +00:00
|
|
|
callerClosure := ethchain.NewClosure(account, contract, script, state, ethutil.Big(gasStr), ethutil.Big(gasPriceStr))
|
2014-05-27 08:42:37 +00:00
|
|
|
|
|
|
|
block := self.lib.eth.BlockChain().CurrentBlock
|
|
|
|
vm := ethchain.NewVm(state, self.lib.eth.StateManager(), ethchain.RuntimeVars{
|
|
|
|
Origin: account.Address(),
|
|
|
|
BlockNumber: block.BlockInfo().Number,
|
|
|
|
PrevHash: block.PrevHash,
|
|
|
|
Coinbase: block.Coinbase,
|
|
|
|
Time: block.Time,
|
|
|
|
Diff: block.Difficulty,
|
2014-05-28 21:14:23 +00:00
|
|
|
Value: ethutil.Big(valueStr),
|
2014-05-27 08:42:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
self.Db.done = false
|
|
|
|
go func() {
|
2014-05-28 21:14:23 +00:00
|
|
|
ret, _, err := callerClosure.Call(vm, data, self.Db.halting)
|
|
|
|
if err != nil {
|
|
|
|
self.Logln("exited with errors:", err)
|
|
|
|
} else {
|
|
|
|
self.Logf("exited: %v", ret)
|
|
|
|
}
|
2014-05-27 08:42:37 +00:00
|
|
|
|
|
|
|
state.Reset()
|
|
|
|
|
|
|
|
self.Db.done = true
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-05-28 21:14:23 +00: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 08:42:37 +00:00
|
|
|
func (self *DebuggerWindow) Next() {
|
|
|
|
self.Db.Next()
|
|
|
|
}
|
2014-05-27 11:09:47 +00:00
|
|
|
|
|
|
|
type Debugger struct {
|
|
|
|
win *qml.Window
|
|
|
|
N chan bool
|
2014-05-27 11:28:11 +00:00
|
|
|
Q chan bool
|
2014-05-27 11:09:47 +00:00
|
|
|
done bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type storeVal struct {
|
|
|
|
Key, Value string
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:28:11 +00:00
|
|
|
func (d *Debugger) halting(pc int, op ethchain.OpCode, mem *ethchain.Memory, stack *ethchain.Stack, stateObject *ethchain.StateObject) bool {
|
2014-05-27 11:09:47 +00: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())})
|
|
|
|
})
|
|
|
|
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-d.N:
|
|
|
|
break out
|
2014-05-27 11:28:11 +00:00
|
|
|
case <-d.Q:
|
|
|
|
d.done = true
|
|
|
|
|
|
|
|
return false
|
2014-05-27 11:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-27 11:28:11 +00:00
|
|
|
|
|
|
|
return true
|
2014-05-27 11:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Debugger) Next() {
|
|
|
|
if !d.done {
|
|
|
|
d.N <- true
|
|
|
|
}
|
|
|
|
}
|