Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth/tracers/logger: support tracing system calls #30923

Merged
merged 4 commits into from
Jan 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions eth/tracers/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ type StructLogger struct {

interrupt atomic.Bool // Atomic flag to signal execution interruption
reason error // Textual reason for the interruption
skip bool // skip processing hooks.
}

// NewStreamingStructLogger returns a new streaming logger.
Expand All @@ -240,10 +241,12 @@ func NewStructLogger(cfg *Config) *StructLogger {

func (l *StructLogger) Hooks() *tracing.Hooks {
return &tracing.Hooks{
OnTxStart: l.OnTxStart,
OnTxEnd: l.OnTxEnd,
OnExit: l.OnExit,
OnOpcode: l.OnOpcode,
OnTxStart: l.OnTxStart,
OnTxEnd: l.OnTxEnd,
OnSystemCallStartV2: l.OnSystemCallStart,
OnSystemCallEnd: l.OnSystemCallEnd,
OnExit: l.OnExit,
OnOpcode: l.OnOpcode,
}
}

Expand All @@ -255,6 +258,10 @@ func (l *StructLogger) OnOpcode(pc uint64, opcode byte, gas, cost uint64, scope
if l.interrupt.Load() {
return
}
// Processing a system call.
if l.skip {
return
}
// check if already accumulated the size of the response.
if l.cfg.Limit != 0 && l.resultSize > l.cfg.Limit {
return
Expand Down Expand Up @@ -320,6 +327,9 @@ func (l *StructLogger) OnExit(depth int, output []byte, gasUsed uint64, err erro
if depth != 0 {
return
}
if l.skip {
return
}
l.output = output
l.err = err
// TODO @holiman, should we output the per-scope output?
Expand Down Expand Up @@ -360,6 +370,13 @@ func (l *StructLogger) Stop(err error) {
func (l *StructLogger) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from common.Address) {
l.env = env
}
func (l *StructLogger) OnSystemCallStart(env *tracing.VMContext) {
l.skip = true
}

func (l *StructLogger) OnSystemCallEnd() {
l.skip = false
}

func (l *StructLogger) OnTxEnd(receipt *types.Receipt, err error) {
if err != nil {
Expand Down Expand Up @@ -389,9 +406,10 @@ func WriteTrace(writer io.Writer, logs []StructLog) {
}

type mdLogger struct {
out io.Writer
cfg *Config
env *tracing.VMContext
out io.Writer
cfg *Config
env *tracing.VMContext
skip bool
}

// NewMarkdownLogger creates a logger which outputs information in a format adapted
Expand All @@ -406,19 +424,32 @@ func NewMarkdownLogger(cfg *Config, writer io.Writer) *mdLogger {

func (t *mdLogger) Hooks() *tracing.Hooks {
return &tracing.Hooks{
OnTxStart: t.OnTxStart,
OnEnter: t.OnEnter,
OnExit: t.OnExit,
OnOpcode: t.OnOpcode,
OnFault: t.OnFault,
OnTxStart: t.OnTxStart,
OnSystemCallStartV2: t.OnSystemCallStart,
OnSystemCallEnd: t.OnSystemCallEnd,
OnEnter: t.OnEnter,
OnExit: t.OnExit,
OnOpcode: t.OnOpcode,
OnFault: t.OnFault,
}
}

func (t *mdLogger) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from common.Address) {
t.env = env
}

func (t *mdLogger) OnSystemCallStart(env *tracing.VMContext) {
t.skip = true
}

func (t *mdLogger) OnSystemCallEnd() {
t.skip = false
}

func (t *mdLogger) OnEnter(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
if t.skip {
return
}
if depth != 0 {
return
}
Expand Down Expand Up @@ -446,6 +477,9 @@ func (t *mdLogger) OnEnter(depth int, typ byte, from common.Address, to common.A
}

func (t *mdLogger) OnExit(depth int, output []byte, gasUsed uint64, err error, reverted bool) {
if t.skip {
return
}
if depth == 0 {
fmt.Fprintf(t.out, "\nPost-execution info:\n"+
" - output: `%#x`\n"+
Expand All @@ -457,6 +491,9 @@ func (t *mdLogger) OnExit(depth int, output []byte, gasUsed uint64, err error, r

// OnOpcode also tracks SLOAD/SSTORE ops to track storage change.
func (t *mdLogger) OnOpcode(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
if t.skip {
return
}
stack := scope.StackData()
fmt.Fprintf(t.out, "| %4d | %10v | %3d |%10v |", pc, vm.OpCode(op).String(),
cost, t.env.StateDB.GetRefund())
Expand All @@ -477,6 +514,9 @@ func (t *mdLogger) OnOpcode(pc uint64, op byte, gas, cost uint64, scope tracing.
}

func (t *mdLogger) OnFault(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, depth int, err error) {
if t.skip {
return
}
fmt.Fprintf(t.out, "\nError: at pc=%d, op=%v: %v\n", pc, op, err)
}

Expand Down