Skip to content

How to Debug a Contract


UTXO_Compiler includes an interactive command-line debugger that single-steps through compiled bytecode on a local BVM simulator. You can set breakpoints, inspect stack state, trace function calls, and load real transaction data to simulate the on-chain environment.


Starting the Debugger

bash
# Basic start
./utxo_compiler my_contract.ct --debug

# Contract uses sub-scope alt stack operations (--asa mode)
./utxo_compiler my_contract.ct --debug --asa

After starting the debugger, you first select a language:

bash
Select CLI language [zh/en] (default: zh):

Selecting the Debug Target

If the contract has multiple public functions, the debugger will prompt you to select:

=====================================
Debuggable function list:
=====================================
[1] getCountFromPreTX (public, 1 parameter)
[2] verifyCurrentTX (public, 1 parameter)

Please select the function to debug (enter number 1-2, or press Enter to skip): _
  • Enter a number to debug only that function
  • Press Enter to input parameters for all public functions and debug them in sequence

Then input function parameters:

=====================================
Debugging function: getCountFromPreTX (public)
Provide initial values for the following parameters:
=====================================

Parameter 1/1: pretx (struct: PreTX, 17 fields)

Parameter input format:

TypeInput method
Integer42, -10, 0x1a (hexadecimal integer)
Hex bytes0x1234abcd
String"hello" (enclosed in quotes)
Default valuePress Enter to use the type's default value

Command Reference

After selecting the function to debug and entering parameters, the REPL prompt appears:

=====================================
Pushed 17 parameters onto the main stack
=====================================
=====================================
  UTXO_Compiler Debugger v1.0
=====================================
Loaded: ../../contract_file/counter.ct
Type 'help' to see available commands.

(apc-debug) _

Type help to see all available commands.

Execution Control

CommandShortDescription
runrStart execution (or continue from current state)
continuecContinue from a breakpoint or pause
stepsStep into (enters function calls)
nextnStep over (doesn't enter function calls; treats whole call as one step)
finishfStep out of current function, execute to the next line in the caller
resetCompletely reset the VM to the initial state when entering the REPL
pausePause running execution

Breakpoint Management

CommandShortDescription
break <line>b <N>Set a breakpoint at the specified source line
break <funcname>b <name>Set a breakpoint at a function entry
delete <breakpoint ID>d <ID>Delete the specified breakpoint
disable <breakpoint ID>Disable a breakpoint (doesn't trigger, but retained)
enable <breakpoint ID>Re-enable a breakpoint
info breakpointsinfo bpList all breakpoints and their status

State Inspection

CommandShortDescription
list [line]lDisplay source code; defaults to centering on the current execution line
stackDisplay current contents of main stack and alt stack
backtracebtDisplay function call stack
bytecode [N]bc [N]Display bytecode near the current execution position; N is context lines

Transaction Data

CommandDescription
settxfile <path>Load transaction data from a JSON file (for simulating BVM.* fields)
showtxDisplay currently loaded transaction data

Other

CommandDescription
help / hDisplay command help
quit / q / exitExit the debugger
clearClear the screen

Generating Debug Info (Without Starting the Debugger)

If you only need to generate debug info files for use with other tools, use the -d flag:

bash
./utxo_compiler my_contract.ct -d

The debug info contains a mapping from source line numbers to bytecode offsets, as well as the function symbol table, for analysis by third-party tools.


Next Steps


🇨🇳 中文版

Released under the MIT License.