core/evm, core/vm: improved evm trace output

* Improved the standard evm tracer output and renamed it to WriteTrace
which now takes an io.Writer to write the logs to.
* Added WriteLogs which writes logs to the given writer in a readable
format.
* evm utility now also prints logs generated during the execution.
This commit is contained in:
Jeffrey Wilcke 2017-03-01 10:19:15 +01:00
parent 230cf2ec91
commit bf4155846c
No known key found for this signature in database
GPG Key ID: 63FF149CD6945F9E
2 changed files with 49 additions and 42 deletions

View File

@ -53,7 +53,6 @@ func runCmd(ctx *cli.Context) error {
statedb, _ = state.New(common.Hash{}, db) statedb, _ = state.New(common.Hash{}, db)
sender = common.StringToAddress("sender") sender = common.StringToAddress("sender")
logger = vm.NewStructLogger(nil) logger = vm.NewStructLogger(nil)
tstart = time.Now()
) )
statedb.CreateAccount(sender) statedb.CreateAccount(sender)
@ -84,9 +83,7 @@ func runCmd(ctx *cli.Context) error {
code = common.Hex2Bytes(string(bytes.TrimRight(hexcode, "\n"))) code = common.Hex2Bytes(string(bytes.TrimRight(hexcode, "\n")))
} }
if ctx.GlobalBool(CreateFlag.Name) { runtimeConfig := runtime.Config{
input := append(code, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name))...)
ret, _, err = runtime.Create(input, &runtime.Config{
Origin: sender, Origin: sender,
State: statedb, State: statedb,
GasLimit: ctx.GlobalUint64(GasFlag.Name), GasLimit: ctx.GlobalUint64(GasFlag.Name),
@ -97,23 +94,17 @@ func runCmd(ctx *cli.Context) error {
Debug: ctx.GlobalBool(DebugFlag.Name), Debug: ctx.GlobalBool(DebugFlag.Name),
DisableGasMetering: ctx.GlobalBool(DisableGasMeteringFlag.Name), DisableGasMetering: ctx.GlobalBool(DisableGasMeteringFlag.Name),
}, },
}) }
tstart := time.Now()
if ctx.GlobalBool(CreateFlag.Name) {
input := append(code, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name))...)
ret, _, err = runtime.Create(input, &runtimeConfig)
} else { } else {
receiver := common.StringToAddress("receiver") receiver := common.StringToAddress("receiver")
statedb.SetCode(receiver, code) statedb.SetCode(receiver, code)
ret, err = runtime.Call(receiver, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)), &runtime.Config{ ret, err = runtime.Call(receiver, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)), &runtimeConfig)
Origin: sender,
State: statedb,
GasLimit: ctx.GlobalUint64(GasFlag.Name),
GasPrice: utils.GlobalBig(ctx, PriceFlag.Name),
Value: utils.GlobalBig(ctx, ValueFlag.Name),
EVMConfig: vm.Config{
Tracer: logger,
Debug: ctx.GlobalBool(DebugFlag.Name),
DisableGasMetering: ctx.GlobalBool(DisableGasMeteringFlag.Name),
},
})
} }
vmdone := time.Since(tstart) vmdone := time.Since(tstart)
@ -121,7 +112,13 @@ func runCmd(ctx *cli.Context) error {
statedb.Commit(true) statedb.Commit(true)
fmt.Println(string(statedb.Dump())) fmt.Println(string(statedb.Dump()))
} }
vm.StdErrFormat(logger.StructLogs())
if ctx.GlobalBool(DebugFlag.Name) {
fmt.Fprintln(os.Stderr, "#### TRACE ####")
vm.WriteTrace(os.Stderr, logger.StructLogs())
fmt.Fprintln(os.Stderr, "#### LOGS ####")
vm.WriteLogs(os.Stderr, statedb.Logs())
}
if ctx.GlobalBool(SysStatFlag.Name) { if ctx.GlobalBool(SysStatFlag.Name) {
var mem goruntime.MemStats var mem goruntime.MemStats
@ -136,7 +133,7 @@ num gc: %d
`, mem.Alloc, mem.TotalAlloc, mem.Mallocs, mem.HeapAlloc, mem.HeapObjects, mem.NumGC) `, mem.Alloc, mem.TotalAlloc, mem.Mallocs, mem.HeapAlloc, mem.HeapObjects, mem.NumGC)
} }
fmt.Printf("OUT: 0x%x", ret) fmt.Printf("0x%x", ret)
if err != nil { if err != nil {
fmt.Printf(" error: %v", err) fmt.Printf(" error: %v", err)
} }

View File

@ -19,11 +19,12 @@ package vm
import ( import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io"
"math/big" "math/big"
"os"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
) )
type Storage map[common.Hash]common.Hash type Storage map[common.Hash]common.Hash
@ -168,29 +169,38 @@ func (l *StructLogger) StructLogs() []StructLog {
return l.logs return l.logs
} }
// StdErrFormat formats a slice of StructLogs to human readable format // WriteTrace writes a formatted trace to the given writer
func StdErrFormat(logs []StructLog) { func WriteTrace(writer io.Writer, logs []StructLog) {
fmt.Fprintf(os.Stderr, "VM STAT %d OPs\n", len(logs))
for _, log := range logs { for _, log := range logs {
fmt.Fprintf(os.Stderr, "PC %08d: %s GAS: %v COST: %v", log.Pc, log.Op, log.Gas, log.GasCost) fmt.Fprintf(writer, "%-10spc=%08d gas=%v cost=%v", log.Op, log.Pc, log.Gas, log.GasCost)
if log.Err != nil { if log.Err != nil {
fmt.Fprintf(os.Stderr, " ERROR: %v", log.Err) fmt.Fprintf(writer, " ERROR: %v", log.Err)
} }
fmt.Fprintf(os.Stderr, "\n") fmt.Fprintf(writer, "\n")
fmt.Fprintln(os.Stderr, "STACK =", len(log.Stack))
for i := len(log.Stack) - 1; i >= 0; i-- { for i := len(log.Stack) - 1; i >= 0; i-- {
fmt.Fprintf(os.Stderr, "%04d: %x\n", len(log.Stack)-i-1, math.PaddedBigBytes(log.Stack[i], 32)) fmt.Fprintf(writer, "%08d %x\n", len(log.Stack)-i-1, math.PaddedBigBytes(log.Stack[i], 32))
} }
fmt.Fprintln(os.Stderr, "MEM =", len(log.Memory)) fmt.Fprint(writer, hex.Dump(log.Memory))
fmt.Fprintln(os.Stderr, hex.Dump(log.Memory))
fmt.Fprintln(os.Stderr, "STORAGE =", len(log.Storage))
for h, item := range log.Storage { for h, item := range log.Storage {
fmt.Fprintf(os.Stderr, "%x: %x\n", h, item) fmt.Fprintf(writer, "%x: %x\n", h, item)
} }
fmt.Fprintln(os.Stderr) fmt.Fprintln(writer)
}
}
// WriteLogs writes vm logs in a readable format to the given writer
func WriteLogs(writer io.Writer, logs []*types.Log) {
for _, log := range logs {
fmt.Fprintf(writer, "LOG%d: %x bn=%d txi=%x\n", len(log.Topics), log.Address, log.BlockNumber, log.TxIndex)
for i, topic := range log.Topics {
fmt.Fprintf(writer, "%08d %x\n", i, topic)
}
fmt.Fprint(writer, hex.Dump(log.Data))
fmt.Fprintln(writer)
} }
} }