|
| 1 | +--- |
| 2 | +title: "Debugging Move" |
| 3 | +--- |
| 4 | + |
| 5 | +# Debugging Move |
| 6 | + |
| 7 | +Move was designed to be simple and safe, but like with all programming languages, |
| 8 | +bugs can still occur. This guide will help you debug your Move code and figure out |
| 9 | +what went wrong. |
| 10 | + |
| 11 | +Please feel free to contribute with additional tooling and information that can |
| 12 | +help others in the community. |
| 13 | + |
| 14 | +## Debugging with the Aptos CLI |
| 15 | + |
| 16 | +### Simulation on transaction submission |
| 17 | + |
| 18 | +You can use the Aptos CLI to simulate entry functions prior to executing them. |
| 19 | + |
| 20 | +Normally, a transaction will fail in simulation if it won't work on-chain. For example: |
| 21 | + |
| 22 | +```bash name="Terminal" |
| 23 | +aptos move run --function-id 0x1::aptos_account::transfer --args address:0x1 u64:1000000000000000000 |
| 24 | +{ |
| 25 | + "Error": "Simulation failed with status: Move abort in 0x1::coin: EINSUFFICIENT_BALANCE(0x10006): Not enough coins to complete transaction" |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +The same applies to Move scripts as well. For example: |
| 30 | + |
| 31 | +```bash name="Terminal" |
| 32 | + aptos move run-script --script-path <script_path> ... |
| 33 | +``` |
| 34 | + |
| 35 | +### Local Simulation |
| 36 | + |
| 37 | +Additionally, for some situations, local simulation, may give additional information and |
| 38 | +[print out any debug statements you have in your code](https://aptos.dev/en/build/cli/working-with-move-contracts#printing-debugging-information). |
| 39 | + |
| 40 | +```bash name="Terminal" |
| 41 | +aptos move run --function-id 0x1::aptos_account::transferred --args address:0x1 u64:1000000000000000000 --local |
| 42 | + |
| 43 | +Simulating transaction locally... |
| 44 | +{ |
| 45 | + "Result": { |
| 46 | + "transaction_hash": "0x4115316915d409ba4106632c82d4b09220035ffdbd0b86bbe29a586d03d06318", |
| 47 | + "gas_used": 3, |
| 48 | + "gas_unit_price": 100, |
| 49 | + "sender": "78077fe8db589e1a3407170cf8af3bd60a8c95737918c15dd6f49dcbecc7900a", |
| 50 | + "success": false, |
| 51 | + "version": 56634003, |
| 52 | + "vm_status": "status FUNCTION_RESOLUTION_FAILURE of type Verification with message Function 0x1::aptos_account::transferred does not exist" |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +### Gas Profiling and Tracing |
| 58 | + |
| 59 | +Adding the gas profile will additionally add the ability to trace how much gas is |
| 60 | +used in computation: |
| 61 | + |
| 62 | +```bash name="Terminal" |
| 63 | +aptos move run --function-id 0x1::aptos_account::transferred --args address:0x1 u64:1000000000000000000 --profile-gas |
| 64 | + |
| 65 | +Simulating transaction locally using the gas profiler... |
| 66 | +Gas report saved to gas-profiling/txn-a90ca655-0x1-aptos_account-transferred. |
| 67 | +{ |
| 68 | + "Result": { |
| 69 | + "transaction_hash": "0xa90ca6550dcdd7f514f4cdcdee7dc1fbee17082fcf68f3db3e5755a93b89bcfc", |
| 70 | + "gas_used": 3, |
| 71 | + "gas_unit_price": 100, |
| 72 | + "sender": "78077fe8db589e1a3407170cf8af3bd60a8c95737918c15dd6f49dcbecc7900a", |
| 73 | + "success": false, |
| 74 | + "version": 56651618, |
| 75 | + "vm_status": "status FUNCTION_RESOLUTION_FAILURE of type Verification with message Function 0x1::aptos_account::transferred does not exist" |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +And this will generate a gas report viewable in HTML format: |
| 81 | + |
| 82 | +```bash name="Terminal" |
| 83 | +open gas-profiling/txn-a90ca655-0x1-aptos_account-transferred/index.html |
| 84 | +``` |
| 85 | + |
| 86 | +## Evaluating performance |
| 87 | + |
| 88 | +```bash name="Terminal" |
| 89 | +aptos move run --function-id 0x1::aptos_account::transfer --args address:0x1 u64:1 --benchmark |
| 90 | + |
| 91 | +Benchmarking transaction locally... |
| 92 | +Running time (cold code cache): 22.144458ms |
| 93 | +Running time (warm code cache): 669.5µs |
| 94 | +{ |
| 95 | + "Result": { |
| 96 | + "transaction_hash": "0x7cdf37ff4d798b3ac3f1e860a40428853e381598a511b9291f2a49e5ff6262a0", |
| 97 | + "gas_used": 11, |
| 98 | + "gas_unit_price": 100, |
| 99 | + "sender": "78077fe8db589e1a3407170cf8af3bd60a8c95737918c15dd6f49dcbecc7900a", |
| 100 | + "success": true, |
| 101 | + "version": 56679764, |
| 102 | + "vm_status": "status EXECUTED of type Execution" |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
0 commit comments