|
| 1 | +# Debugging with Forc Call |
| 2 | + |
| 3 | +The `forc call` command includes interactive debugging capabilities through the `--debug` flag, enabling developers to debug contract function calls step-by-step after transaction execution. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +When you add the `--debug` flag to any `forc call` command, it will: |
| 8 | + |
| 9 | +1. Execute the contract function call as normal |
| 10 | +2. Automatically launch an interactive debugging session |
| 11 | +3. Allow you to step through the execution, inspect values, and set breakpoints |
| 12 | +4. Provide full access to the `forc-debug` interface |
| 13 | + |
| 14 | +This integration seamlessly combines contract interaction with debugging, making it easy to understand what happens during contract execution. |
| 15 | + |
| 16 | +## Basic Usage |
| 17 | + |
| 18 | +Simply add the `--debug` flag to any existing `forc call` command: |
| 19 | + |
| 20 | +```bash |
| 21 | +forc call <CONTRACT_ID> \ |
| 22 | + --abi <ABI_PATH> \ |
| 23 | + <FUNCTION_NAME> [ARGS...] \ |
| 24 | + --debug |
| 25 | +``` |
| 26 | + |
| 27 | +## Example: Debugging a Contract Call |
| 28 | + |
| 29 | +Let's say you have a contract with a function that performs some calculations: |
| 30 | + |
| 31 | +```sway |
| 32 | +contract; |
| 33 | +
|
| 34 | +abi Calculator { |
| 35 | + fn factorial(n: u64) -> u64; |
| 36 | +} |
| 37 | +
|
| 38 | +impl Calculator for Contract { |
| 39 | + fn factorial(n: u64) -> u64 { |
| 40 | + let mut result = 1; |
| 41 | + let mut counter = 0; |
| 42 | + while counter < n { |
| 43 | + counter = counter + 1; |
| 44 | + result = result * counter; |
| 45 | + } |
| 46 | + result |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### Debugging the Factorial Function |
| 52 | + |
| 53 | +```bash |
| 54 | +forc call 0x1234567890abcdef1234567890abcdef12345678 \ |
| 55 | + --abi ./out/debug/calculator-abi.json \ |
| 56 | + factorial 5 \ |
| 57 | + --debug |
| 58 | +``` |
| 59 | + |
| 60 | +This command will: |
| 61 | + |
| 62 | +1. Execute `factorial(5)` on the deployed contract |
| 63 | +2. Show the transaction result (return value: 120) |
| 64 | +3. Launch the interactive debugger with the transaction data pre-loaded |
| 65 | + |
| 66 | +### Interactive Debugging Session |
| 67 | + |
| 68 | +Once the debugger launches, you'll see the pre-buffered debugger prompt to start the transaction: |
| 69 | + |
| 70 | +```text |
| 71 | +Welcome to the Sway Debugger! Type "help" for a list of commands. |
| 72 | +>> start_tx /var/folders/xz/5djvk4596k5c1fj2prcd0d880000gn/T/.tmpexrnFE.json /var/folders/xz/5djvk4596k5c1fj2prcd0d880000gn/T/.tmpuaPRtF.json |
| 73 | +``` |
| 74 | + |
| 75 | +You can now use all the [standard debugging commands](./debugging_with_cli.md#using-the-debugger). |
0 commit comments