Skip to main content

Debugging

Move does not currently have a native debugger. You can use the std::debug module, however, to print arbitrary values to the console. Monitoring variable values in this manner can provide insight into the logic of your modules. To do so, first import the debug module in your source file:

use std::debug;

Then in places where you want to print out a value v, regardless of its type, add the following code:

debug::print(&v);

or the following if v is already a reference:

debug::print(v);

The debug module also provides a function to print out the current stacktrace:

debug::print_stack_trace();

Alternatively, any call to abort or assertion failure also prints the stacktrace at the point of failure.

Using debug in my_module

To see the module in action, update your my_module code to include debug calls. Specifically, update the new_sword function so that you print the value of forge before and after updating swords_created. Also, include a print_stack_trace so that the function looks like the following:

public fun new_sword(
forge: &mut Forge,
magic: u64,
strength: u64,
ctx: &mut TxContext,
): Sword {
debug::print(forge);
forge.swords_created = forge.swords_created + 1;
debug::print(forge);
debug::print_stack_trace();
Sword {
id: object::new(ctx),
magic: magic,
strength: strength,
}
}

To see the results, run the module's tests.

$ sui move test

The response prints out the expected results as the test calls the new_sword function.

INCLUDING DEPENDENCY Sui
INCLUDING DEPENDENCY MoveStdlib
BUILDING my_first_package
Running Move unit tests
[ PASS ] 0x0::my_module::test_module_init
[debug] 0x0::my_module::Forge {
id: 0x2::object::UID {
id: 0x2::object::ID {
bytes: @<OBJECT-ID>
}
},
swords_created: 0
}
[debug] 0x0::my_module::Forge {
id: 0x2::object::UID {
id: 0x2::object::ID {
bytes: @<OBJECT-ID>
}
},
swords_created: 1
}
Call Stack:
[0] 0000000000000000000000000000000000000000000000000000000000000000::my_module::test_sword_transactions

Code:
[19] LdU64(7)
[20] MutBorrowLoc(3)
[21] Call(14)
> [22] Call(4)
[23] LdConst(1)
[24] CallGeneric(2)
[25] ImmBorrowLoc(3)

Locals:
[0] -
[1] { { { <OBJECT-ID-WITHOUT-0x> } }, 1 }
[2] -
[3] { 2, { 00000000000000000000000000000000000000000000000000000000000000ad, [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0, 0, 0 } }


Operand Stack:

[ PASS ] 0x0::my_module::test_sword_transactions
Test result: OK. Total tests: 2; passed: 2; failed: 0