paritytech / polkavm

A fast and secure RISC-V based virtual machine
Apache License 2.0
258 stars 59 forks source link

How to debug a guest program #115

Open xlc opened 6 months ago

xlc commented 6 months ago

Any tips on how to debug a guest program? e.g. How to make it log something

koute commented 6 months ago

For classic printf-style debugging you can use a host function to produce some logs.

You can also set RUST_LOG=trace POLKAVM_BACKEND=interpreter environment variables (assuming you're using env_logger and using Config::from_env) to get it to log whatever it is executing, values of the registers, etc. For example:

[TRACE polkavm::interpreter] 441195: jump 441260 if a0 == 0
[TRACE polkavm::interpreter] 441199: s1 = s1 - 1
[TRACE polkavm::interpreter] s1 = 0x30
[TRACE polkavm::interpreter] 441202: s0 = s0 + 0x18
[TRACE polkavm::interpreter] s0 = 0x8b760
[TRACE polkavm::interpreter] 441205: jump 441182 if s1 != 0
[TRACE polkavm::interpreter] 441182: a1 = u32 [s0]
[TRACE polkavm::interpreter] a1 = u32 [0x8b760]
[TRACE polkavm::interpreter] a1 = 0x1aba4
[TRACE polkavm::interpreter] 441184: a0 = u32 [sp + 4]
[TRACE polkavm::interpreter] a0 = u32 [0xfffefdbc]
[TRACE polkavm::interpreter] a0 = 0xfffefdd4
[TRACE polkavm::interpreter] 441187: ra = 27748, jump 277367

It's also useful to use polkatool disassemble to disassemble your program for debugging.

In the future I want to have a fully fledged debugger for guest programs with nice IDE integration and time-travel debugging, but we're not there yet.