aboutsummaryrefslogtreecommitdiffstats
path: root/ethereal/ui/debugger.go
blob: 5ad1b4a2c6b862c72b64fa5a5fbb7b8c0a45c2d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package ethui

import (
    "fmt"
    "github.com/ethereum/eth-go/ethchain"
    "github.com/ethereum/eth-go/ethutil"
    "github.com/go-qml/qml"
    "math/big"
    "strings"
)

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)
    db := &Debugger{win, make(chan bool), make(chan bool), true, false, true}

    return &DebuggerWindow{engine: engine, win: win, lib: lib, Db: db}
}

func (self *DebuggerWindow) Show() {
    context := self.engine.Context()
    context.SetVar("dbg", self)

    go func() {
        self.win.Show()
        self.win.Wait()
    }()
}

func (self *DebuggerWindow) SetCode(code string) {
    self.win.Set("codeText", code)
}

func (self *DebuggerWindow) SetData(data string) {
    self.win.Set("dataText", data)
}
func (self *DebuggerWindow) SetAsm(data string) {
    dis := ethchain.Disassemble(ethutil.Hex2Bytes(data))
    for _, str := range dis {
        self.win.Root().Call("setAsm", str)
    }
}

func (self *DebuggerWindow) Debug(valueStr, gasStr, gasPriceStr, scriptStr, dataStr string) {
    if !self.Db.done {
        self.Db.Q <- true
    }
    self.Db.breakOnInstr = self.win.Root().ObjectByName("breakEachLine").Bool("checked")

    defer func() {
        if r := recover(); r != nil {
            self.Logf("compile FAULT: %v", r)
        }
    }()

    data := ethutil.StringToByteFunc(dataStr, func(s string) (ret []byte) {
        slice := strings.Split(dataStr, "\n")
        for _, dataItem := range slice {
            d := ethutil.FormatData(dataItem)
            ret = append(ret, d...)
        }
        return
    })

    var err error
    script := ethutil.StringToByteFunc(scriptStr, func(s string) (ret []byte) {
        ret, err = ethutil.Compile(s)
        return
    })

    if err != nil {
        self.Logln(err)

        return
    }

    dis := ethchain.Disassemble(script)
    self.win.Root().Call("clearAsm")
    self.win.Root().Call("clearLog")

    for _, str := range dis {
        self.win.Root().Call("setAsm", str)
    }

    var (
        gas      = ethutil.Big(gasStr)
        gasPrice = ethutil.Big(gasPriceStr)
        value    = ethutil.Big(valueStr)
        // Contract addr as test address
        keyPair  = self.lib.eth.KeyManager().KeyPair()
        callerTx = ethchain.NewContractCreationTx(ethutil.Big(valueStr), gas, gasPrice, script)
    )
    callerTx.Sign(keyPair.PrivateKey)

    state := self.lib.eth.BlockChain().CurrentBlock.State()
    account := self.lib.eth.StateManager().TransState().GetAccount(keyPair.Address())
    contract := ethchain.MakeContract(callerTx, state)
    contract.Amount = value
    callerClosure := ethchain.NewClosure(account, contract, script, state, gas, gasPrice)

    block := self.lib.eth.BlockChain().CurrentBlock
    vm := ethchain.NewVm(state, self.lib.eth.StateManager(), ethchain.RuntimeVars{
        Block:       block,
        Origin:      account.Address(),
        BlockNumber: block.Number,
        PrevHash:    block.PrevHash,
        Coinbase:    block.Coinbase,
        Time:        block.Time,
        Diff:        block.Difficulty,
        Value:       ethutil.Big(valueStr),
    })
    vm.Verbose = true

    self.Db.done = false
    self.Logf("callsize %d", len(script))
    go func() {
        ret, g, err := callerClosure.Call(vm, data, self.Db.halting)
        tot := new(big.Int).Mul(g, gasPrice)
        self.Logf("gas usage %v total price = %v (%v)", g, tot, ethutil.CurrencyToString(tot))
        if err != nil {
            self.Logln("exited with errors:", err)
        } else {
            if len(ret) > 0 {
                self.Logf("exited: % x", ret)
            } else {
                self.Logf("exited: nil")
            }
        }

        state.Reset()

        if !self.Db.interrupt {
            self.Db.done = true
        } else {
            self.Db.interrupt = false
        }
    }()
}

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])
}

func (self *DebuggerWindow) Next() {
    self.Db.Next()
}

type Debugger struct {
    win             *qml.Window
    N               chan bool
    Q               chan bool
    done, interrupt bool
    breakOnInstr    bool
}

type storeVal struct {
    Key, Value string
}

func (d *Debugger) halting(pc int, op ethchain.OpCode, mem *ethchain.Memory, stack *ethchain.Stack, stateObject *ethchain.StateObject) bool {
    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())})
    })

    if d.breakOnInstr {
    out:
        for {
            select {
            case <-d.N:
                break out
            case <-d.Q:
                d.interrupt = true
                d.clearBuffers()

                return false
            }
        }
    }

    return true
}

func (d *Debugger) clearBuffers() {
out:
    // drain
    for {
        select {
        case <-d.N:
        case <-d.Q:
        default:
            break out
        }
    }
}

func (d *Debugger) Next() {
    if !d.done {
        d.N <- true
    }
}