github.com/lmittmann/w3@v0.20.0/docs/pages/vm-tracing.mdx (about)

     1  # Tracing
     2  
     3  Tracing can give detailed insights into the execution of EVM contracts. `w3vm.VM` supports tracing via `go‑ethereum`'s <DocLink title="tracing.Hooks" />.
     4  
     5  ## Usage
     6  
     7  A `tracing.Hooks` can be passed to <DocLink title="VM.Apply" id="w3vm.VM.Apply" />, <DocLink title="VM.ApplyTx" id="w3vm.VM.ApplyTx" />, and <DocLink title="VM.Call" id="w3vm.VM.Call" />. These methods can also be called with multiple hooks at the same time.
     8  
     9  #### Example: Trace Calls and OpCodes of an Execution
    10  
    11  `w3vm` contains a powerful call an opcode tracer <DocLink title="hooks.CallTracer" id="hooks.NewCallTracer" /> that can be used gain detailed insights into the execution of EVM contracts ([Playground](https://pkg.go.dev/github.com/lmittmann/w3/w3vm#example-VM-Trace)):
    12  
    13  ```go
    14  callTracer := hooks.NewCallTracer(os.Stdout, &hooks.CallTracerOptions{
    15      ShowStaticcall: true,
    16      DecodeABI:      true,
    17  })
    18  vm.ApplyTx(tx, callTracer)
    19  ```
    20  
    21  ![Example Call Trace](/assets/call-trace.png)
    22  
    23  #### Example: Generate an Access List
    24  
    25  Access list tracing using `go-ethereum`'s <DocLink title="logger.AccessListTracer" /> ([Playground](https://pkg.go.dev/github.com/lmittmann/w3/w3vm#example-VM-TraceAccessList)):
    26  
    27  ```go {2-8}
    28  // setup access list tracer
    29  signer := types.MakeSigner(params.MainnetChainConfig, header.Number, header.Time)
    30  from, _ := signer.Sender(tx)
    31  accessListTracer := logger.NewAccessListTracer(
    32      nil,
    33      from, *tx.To(),
    34      gethVm.ActivePrecompiles(params.MainnetChainConfig.Rules(header.Number, header.Difficulty.Sign() == 0, header.Time)),
    35  )
    36  
    37  if _, err := vm.ApplyTx(tx, accessListTracer.Hooks()); err != nil {
    38      // ...
    39  }
    40  fmt.Println("Access List:", accessListTracer.AccessList())
    41  ```
    42  
    43  #### Example: Trace the Execution of all OpCodes in a Block
    44  
    45  Trace the execution of all op's in a block ([Playground](https://pkg.go.dev/github.com/lmittmann/w3/w3vm#example-VM-TraceBlock)):
    46  
    47  ```go {2-7}
    48  // setup block op's tracer
    49  var opCount [256]uint64
    50  tracer := &tracing.Hooks{
    51      OnOpcode: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
    52          opCount[op]++
    53      },
    54  }
    55  
    56  for _, tx := range block.Transactions() {
    57      vm.ApplyTx(tx, tracer)
    58  }
    59  ```