## Debugging

Forge provides detailed traces and an interactive debugger to understand contract execution.

### Traces

Run tests with `-vvvv` to see full execution traces:

:::terminal

```bash
$ forge test -vvvv
```

```ansi
// [!include ~/snippets/output/cheatcodes/forge-test-vvvv:output]
```

:::

The trace shows every call, its inputs, outputs, and gas usage.

### Understanding trace output

Each line shows:

* **Gas used** in brackets
* **Contract::function** being called
* **Call type** (staticcall, delegatecall, etc.)
* **Return value** or revert reason

Indentation indicates call depth.

### Tracing a failed transaction

Debug a transaction that failed on-chain:

```bash
$ cast run 0x<txhash> --rpc-url $RPC_URL
```

This replays the transaction and shows the execution trace.

### Interactive debugger

Launch the debugger for a specific test:

```bash
$ forge test --debug test_Increment
```

The debugger shows:

* Source code with current line highlighted
* Stack contents
* Memory contents
* Storage changes
* Call stack

#### Debugger commands

| Key | Action |
|-----|--------|
| `n` | Step to next opcode |
| `s` | Step into call |
| `o` | Step out of call |
| `g` | Go to start |
| `G` | Go to end |
| `c` | Continue to next breakpoint |
| `q` | Quit |
| `h` | Show help |

### Debugging scripts

Debug a script:

```bash
$ forge script script/Deploy.s.sol --debug
```

### Console logging

Add logs to your contracts for debugging:

```solidity
import {console} from "forge-std/console.sol";

function transfer(address to, uint256 amount) public {
    console.log("Transfer from:", msg.sender);
    console.log("Transfer to:", to);
    console.log("Amount:", amount);
    // ...
}
```

View logs with `-vv` or higher:

```bash
$ forge test -vv
```

For structured output, Foundry also supports `console.table`, which can make repeated values easier to scan than a long sequence of `console.log` lines.

### Labeling addresses

Make traces more readable by labeling addresses:

```solidity
function setUp() public {
    alice = makeAddr("alice");
    bob = makeAddr("bob");
    
    vm.label(address(token), "Token");
    vm.label(address(pool), "Pool");
}
```

Traces will show `Token::transfer()` instead of `0x1234...::transfer()`.

### Stack traces

When a test fails, use `-vvv` to see a stack trace showing exactly where the revert occurred:

:::terminal

```bash
$ forge test -vvv
```

```ansi
// [!include ~/snippets/output/cheatcodes/forge-test-fail-vvv:output]
```

:::

The trace shows the call hierarchy with the revert bubbling up, and the **Backtrace** pinpoints the exact location in your code.

### Inspecting inheritance linearization

When debugging overrides in a multiple-inheritance hierarchy, inspect the method-resolution order directly:

```bash
$ forge inspect src/MyContract.sol:MyContract linearization
```

This shows the order Solidity uses to resolve inherited functions, which is often the fastest way to understand why a particular override or `super` call is being selected.
