> For the complete documentation index, see [llms.txt](https://docs.megaeth.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.megaeth.com/mega-evme/tracing/opcode-tracer.md). Agents can also search and read this documentation through its [MCP server](https://docs.megaeth.com/~gitbook/mcp).

# Opcode Tracer

Log every instruction with gas, stack, memory, and storage at each step.

The opcode tracer records every EVM instruction executed, along with gas costs, stack state, memory, and storage changes. This is the default tracer when you pass `--trace` without specifying `--tracer`.

## Usage

```bash
mega-evme run 0x60016000526001601ff3 --trace --tracer opcode
```

## Options

These flags control what the opcode tracer captures. Disabling memory and stack capture significantly reduces trace size for large executions.

| Flag                                | Default | Description                                                |
| ----------------------------------- | ------- | ---------------------------------------------------------- |
| `--trace.opcode.disable-memory`     | `false` | Omit memory snapshots from each step                       |
| `--trace.opcode.disable-stack`      | `false` | Omit stack snapshots from each step                        |
| `--trace.opcode.disable-storage`    | `false` | Omit storage change records                                |
| `--trace.opcode.enable-return-data` | `false` | Include return data in each step (off by default for size) |

## Output Format

The opcode tracer emits Geth-compatible JSON. The top-level object wraps the trace:

### Schema

| Field         | Type       | Description                      |
| ------------- | ---------- | -------------------------------- |
| `failed`      | bool       | Whether the transaction reverted |
| `gas`         | number     | Total gas used                   |
| `returnValue` | hex string | Return data from the transaction |
| `structLogs`  | array      | Array of per-opcode step objects |

Each entry in `structLogs`:

| Field        | Type       | Description                                              |
| ------------ | ---------- | -------------------------------------------------------- |
| `pc`         | number     | Program counter                                          |
| `op`         | string     | Opcode name (e.g., `PUSH1`, `SSTORE`)                    |
| `gas`        | number     | Gas remaining before this step                           |
| `gasCost`    | number     | Gas consumed by this step                                |
| `depth`      | number     | Call depth                                               |
| `stack`      | array      | Stack contents as hex strings (unless disabled)          |
| `memory`     | array      | Memory contents as 32-byte hex chunks (unless disabled)  |
| `storage`    | object     | Storage changes as key-value hex pairs (unless disabled) |
| `refund`     | number     | Gas refund counter at this step                          |
| `returnData` | hex string | Return data at this step (only if enabled)               |

### Example Output

Running `mega-evme run 0x60016000526001601ff3 --trace --tracer opcode` produces:

```json
{
  "failed": false,
  "gas": 60018,
  "returnValue": "0x01",
  "structLogs": [
    {
      "pc": 0,
      "op": "PUSH1",
      "gas": 9940000,
      "gasCost": 3,
      "depth": 1,
      "stack": [],
      "memory": [],
      "refund": 0
    },
    {
      "pc": 2,
      "op": "PUSH1",
      "gas": 9939997,
      "gasCost": 3,
      "depth": 1,
      "stack": ["0x1"],
      "memory": [],
      "refund": 0
    },
    {
      "pc": 4,
      "op": "MSTORE",
      "gas": 9939994,
      "gasCost": 6,
      "depth": 1,
      "stack": ["0x1", "0x0"],
      "memory": [],
      "refund": 0
    },
    {
      "pc": 5,
      "op": "PUSH1",
      "gas": 9939988,
      "gasCost": 3,
      "depth": 1,
      "stack": [],
      "memory": [
        "0x0000000000000000000000000000000000000000000000000000000000000001"
      ],
      "refund": 0
    },
    {
      "pc": 7,
      "op": "PUSH1",
      "gas": 9939985,
      "gasCost": 3,
      "depth": 1,
      "stack": ["0x1"],
      "memory": [
        "0x0000000000000000000000000000000000000000000000000000000000000001"
      ],
      "refund": 0
    },
    {
      "pc": 9,
      "op": "RETURN",
      "gas": 9939982,
      "gasCost": 0,
      "depth": 1,
      "stack": ["0x1", "0x1f"],
      "memory": [
        "0x0000000000000000000000000000000000000000000000000000000000000001"
      ],
      "refund": 0
    }
  ]
}
```

## Examples

Minimal trace (no memory or stack, for compact output):

```bash
mega-evme run 0x60016000526001601ff3 \
  --trace --tracer opcode \
  --trace.opcode.disable-memory \
  --trace.opcode.disable-stack
```

Full trace to file with return data:

```bash
mega-evme run 0x60016000526001601ff3 \
  --trace --tracer opcode \
  --trace.opcode.enable-return-data \
  --trace.output trace.json
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.megaeth.com/mega-evme/tracing/opcode-tracer.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
