/
remote_debugger.go
231 lines (202 loc) · 5.78 KB
/
remote_debugger.go
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
package main
import (
"github.com/google/pprof/profile"
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/flow-go/fvm/environment"
runtime2 "github.com/onflow/flow-go/fvm/runtime"
"github.com/onflow/flow-go/fvm/state"
"github.com/rs/zerolog"
"os"
"path/filepath"
"github.com/onflow/flow-go/fvm"
"github.com/onflow/flow-go/model/flow"
)
type RemoteDebugger struct {
vm *fvm.VirtualMachine
ctx fvm.Context
view state.View
profileBuilder *ProfileBuilder
}
func NewRemoteDebugger(
view *RemoteView,
chain flow.Chain,
directory string,
logger zerolog.Logger) *RemoteDebugger {
vm := fvm.NewVirtualMachine()
profileBuilder := NewProfileBuilder(
directory,
)
// no signature processor here
// TODO Maybe we add fee-deduction step as well
ctx := fvm.NewContext(
fvm.WithLogger(logger),
fvm.WithChain(chain),
fvm.WithTransactionProcessors(fvm.NewTransactionInvoker()),
fvm.WithReusableCadenceRuntimePool(runtime2.NewReusableCadenceRuntimePool(
1,
runtime2.ReusableCadenceRuntimePoolConfig{
OnCadenceStatement: profileBuilder.OnCadenceStatement,
},
)),
)
return &RemoteDebugger{
ctx: ctx,
vm: vm,
view: view,
profileBuilder: profileBuilder,
}
}
// RunTransaction runs the transaction given the latest sealed block data
func (d *RemoteDebugger) RunTransaction(txBody *flow.TransactionBody) (txErr, processError error) {
blockCtx := fvm.NewContextFromParent(d.ctx, fvm.WithBlockHeader(d.ctx.BlockHeader))
tx := fvm.Transaction(txBody, 0)
err := d.vm.Run(blockCtx, tx, d.view)
if err != nil {
return nil, err
}
return tx.Err, nil
}
func (d *RemoteDebugger) RunScript(code []byte, arguments [][]byte) (value cadence.Value, scriptError, processError error) {
scriptCtx := fvm.NewContextFromParent(d.ctx, fvm.WithBlockHeader(d.ctx.BlockHeader))
script := fvm.Script(code).WithArguments(arguments...)
err := d.vm.Run(scriptCtx, script, d.view)
if err != nil {
return nil, nil, err
}
return script.Value, script.Err, nil
}
func (d *RemoteDebugger) Close() error {
return d.profileBuilder.Close()
}
type ProfileBuilder struct {
Profile *profile.Profile
profileFunctionMap map[string]uint64
lastComputation uint64
profileLocationMap map[string]uint64
nextLocID uint64
nextFunID uint64
directory string
}
func NewProfileBuilder(directory string) *ProfileBuilder {
// https://www.polarsignals.com/blog/posts/2021/08/03/diy-pprof-profiles-using-go/
p := &profile.Profile{
Function: []*profile.Function{},
Location: []*profile.Location{},
}
p.SampleType = []*profile.ValueType{{
Type: "execution effort",
Unit: "effort",
}}
return &ProfileBuilder{
Profile: p,
profileFunctionMap: make(map[string]uint64),
profileLocationMap: make(map[string]uint64),
directory: directory,
}
}
func (p *ProfileBuilder) Close() error {
filename := p.directory + "/profile.pb.gz"
err := os.MkdirAll(filepath.Dir(filename), os.ModePerm)
if err != nil {
return err
}
f, err := os.Create(filename)
if err != nil {
return err
}
defer func() {
err := f.Close()
if err != nil {
// log
}
}()
// Write the profile to the file.
err = p.Profile.Write(f)
if err != nil {
return err
}
return nil
}
func (p *ProfileBuilder) OnCadenceStatement(fvmEnv runtime2.Environment, inter *interpreter.Interpreter, statement ast.Statement) {
stack := inter.CallStack()
if len(stack) == 0 {
// what now?
return
}
newComputation := fvmEnv.(environment.Environment).ComputationUsed()
computation := newComputation - p.lastComputation
p.lastComputation = newComputation
locationIds := make([]uint64, 0, len(stack))
// var lastFrame interpreter.Invocation
for _, frame := range stack {
// lastFrame = frame
fn := p.toFunction(inter, frame)
fnIndex, ok := p.profileFunctionMap[p.fnID(fn)]
if !ok {
p.Profile.Function = append(p.Profile.Function, fn)
p.Profile.Location = append(p.Profile.Location,
&profile.Location{
ID: p.nextLocID + 1,
Address: p.nextLocID + 1,
Line: []profile.Line{
{
Function: fn,
Line: fn.StartLine,
},
},
},
)
fnIndex = p.nextFunID
p.profileFunctionMap[p.fnID(fn)] = p.nextFunID
p.profileLocationMap[p.fnID(fn)] = p.nextLocID
p.nextFunID++
p.nextLocID++
}
locationIds = append(locationIds, fnIndex)
}
locations := make([]*profile.Location, 0, len(locationIds))
// revers iterate locations
for i := len(locationIds) - 1; i >= 0; i-- {
locations = append(locations, p.Profile.Location[locationIds[i]])
}
p.Profile.Sample = append(p.Profile.Sample, &profile.Sample{
Location: locations,
Value: []int64{int64(computation)},
})
}
func (p *ProfileBuilder) fnID(fn *profile.Function) string {
return fn.Filename + "_" + fn.Name
}
func (p *ProfileBuilder) toFunction(inter *interpreter.Interpreter, frame interpreter.Invocation) *profile.Function {
filename := frame.Self.StaticType(inter).String()
name := ""
line := int64(0)
if frame.LocationRange.HasPosition != nil {
switch frame.LocationRange.HasPosition.(type) {
case *ast.InvocationExpression:
expression := frame.LocationRange.HasPosition.(*ast.InvocationExpression)
line = int64(expression.InvokedExpression.StartPosition().Line)
switch expression.InvokedExpression.(type) {
case *ast.MemberExpression:
me := expression.InvokedExpression.(*ast.MemberExpression)
name = me.Identifier.String()
case *ast.IdentifierExpression:
ie := expression.InvokedExpression.(*ast.IdentifierExpression)
name = ie.Identifier.String()
default:
panic("")
}
default:
panic("")
}
}
return &profile.Function{
ID: p.nextFunID + 1,
Name: name,
SystemName: name,
Filename: filename,
StartLine: line,
}
}