Skip to content

Commit 9a44e10

Browse files
holimanfjl
authored andcommitted
cmd/evm, core/vm: add --nomemory, --nostack to evm (ethereum#14617)
1 parent 9012863 commit 9a44e10

File tree

5 files changed

+68
-47
lines changed

5 files changed

+68
-47
lines changed

cmd/evm/json_logger.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,32 @@ import (
2828

2929
type JSONLogger struct {
3030
encoder *json.Encoder
31+
cfg *vm.LogConfig
3132
}
3233

33-
func NewJSONLogger(writer io.Writer) *JSONLogger {
34-
return &JSONLogger{json.NewEncoder(writer)}
34+
func NewJSONLogger(cfg *vm.LogConfig, writer io.Writer) *JSONLogger {
35+
return &JSONLogger{json.NewEncoder(writer), cfg}
3536
}
3637

3738
// CaptureState outputs state information on the logger.
3839
func (l *JSONLogger) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error {
39-
return l.encoder.Encode(vm.StructLog{
40-
Pc: pc,
41-
Op: op,
42-
Gas: gas + cost,
43-
GasCost: cost,
44-
Memory: memory.Data(),
45-
Stack: stack.Data(),
46-
Storage: nil,
47-
Depth: depth,
48-
Err: err,
49-
})
40+
log := vm.StructLog{
41+
Pc: pc,
42+
Op: op,
43+
Gas: gas + cost,
44+
GasCost: cost,
45+
MemorySize: memory.Len(),
46+
Storage: nil,
47+
Depth: depth,
48+
Err: err,
49+
}
50+
if !l.cfg.DisableMemory {
51+
log.Memory = memory.Data()
52+
}
53+
if !l.cfg.DisableStack {
54+
log.Stack = stack.Data()
55+
}
56+
return l.encoder.Encode(log)
5057
}
5158

5259
// CaptureEnd is triggered at end of execution.

cmd/evm/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ var (
102102
Name: "sender",
103103
Usage: "The transaction origin",
104104
}
105+
DisableMemoryFlag = cli.BoolFlag{
106+
Name: "nomemory",
107+
Usage: "disable memory output",
108+
}
109+
DisableStackFlag = cli.BoolFlag{
110+
Name: "nostack",
111+
Usage: "disable stack output",
112+
}
105113
)
106114

107115
func init() {
@@ -123,6 +131,8 @@ func init() {
123131
GenesisFlag,
124132
MachineFlag,
125133
SenderFlag,
134+
DisableMemoryFlag,
135+
DisableStackFlag,
126136
}
127137
app.Commands = []cli.Command{
128138
compileCommand,

cmd/evm/runner.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ func runCmd(ctx *cli.Context) error {
7373
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
7474
glogger.Verbosity(log.Lvl(ctx.GlobalInt(VerbosityFlag.Name)))
7575
log.Root().SetHandler(glogger)
76+
logconfig := &vm.LogConfig{
77+
DisableMemory: ctx.GlobalBool(DisableMemoryFlag.Name),
78+
DisableStack: ctx.GlobalBool(DisableStackFlag.Name),
79+
}
7680

7781
var (
7882
tracer vm.Tracer
@@ -82,12 +86,12 @@ func runCmd(ctx *cli.Context) error {
8286
sender = common.StringToAddress("sender")
8387
)
8488
if ctx.GlobalBool(MachineFlag.Name) {
85-
tracer = NewJSONLogger(os.Stdout)
89+
tracer = NewJSONLogger(logconfig, os.Stdout)
8690
} else if ctx.GlobalBool(DebugFlag.Name) {
87-
debugLogger = vm.NewStructLogger(nil)
91+
debugLogger = vm.NewStructLogger(logconfig)
8892
tracer = debugLogger
8993
} else {
90-
debugLogger = vm.NewStructLogger(nil)
94+
debugLogger = vm.NewStructLogger(logconfig)
9195
}
9296
if ctx.GlobalString(GenesisFlag.Name) != "" {
9397
gen := readGenesis(ctx.GlobalString(GenesisFlag.Name))

core/vm/gen_structlog.go

Lines changed: 15 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/vm/logger.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,31 @@ type LogConfig struct {
5454
// StructLog is emitted to the EVM each cycle and lists information about the current internal state
5555
// prior to the execution of the statement.
5656
type StructLog struct {
57-
Pc uint64 `json:"pc"`
58-
Op OpCode `json:"op"`
59-
Gas uint64 `json:"gas"`
60-
GasCost uint64 `json:"gasCost"`
61-
Memory []byte `json:"memory"`
62-
Stack []*big.Int `json:"stack"`
63-
Storage map[common.Hash]common.Hash `json:"-"`
64-
Depth int `json:"depth"`
65-
Err error `json:"error"`
57+
Pc uint64 `json:"pc"`
58+
Op OpCode `json:"op"`
59+
Gas uint64 `json:"gas"`
60+
GasCost uint64 `json:"gasCost"`
61+
Memory []byte `json:"memory"`
62+
MemorySize int `json:"memSize"`
63+
Stack []*big.Int `json:"stack"`
64+
Storage map[common.Hash]common.Hash `json:"-"`
65+
Depth int `json:"depth"`
66+
Err error `json:"error"`
6667
}
6768

6869
// overrides for gencodec
6970
type structLogMarshaling struct {
70-
Stack []*math.HexOrDecimal256
71-
Gas math.HexOrDecimal64
72-
GasCost math.HexOrDecimal64
73-
Memory hexutil.Bytes
74-
OpName string `json:"opName"`
75-
MemorySize int `json:"memSize"`
71+
Stack []*math.HexOrDecimal256
72+
Gas math.HexOrDecimal64
73+
GasCost math.HexOrDecimal64
74+
Memory hexutil.Bytes
75+
OpName string `json:"opName"`
7676
}
7777

7878
func (s *StructLog) OpName() string {
7979
return s.Op.String()
8080
}
8181

82-
func (s *StructLog) MemorySize() int {
83-
return len(s.Memory)
84-
}
85-
8682
// Tracer is used to collect execution traces from an EVM transaction
8783
// execution. CaptureState is called for each step of the VM with the
8884
// current VM state.
@@ -181,7 +177,7 @@ func (l *StructLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost ui
181177
}
182178
}
183179
// create a new snaptshot of the EVM.
184-
log := StructLog{pc, op, gas, cost, mem, stck, storage, env.depth, err}
180+
log := StructLog{pc, op, gas, cost, mem, memory.Len(), stck, storage, depth, err}
185181

186182
l.logs = append(l.logs, log)
187183
return nil

0 commit comments

Comments
 (0)