1

I want to debug a bpf program deployed to solana-test-validator.

besides the solana-test-validator, i have pulled a github project:

https://github.com/hashblock/solana-cli-program-template

which enables the logging in bpf program deployed to solana-test-validator.

After the succes of logging, I want to debug too.

My basic procedure is:

  1. pulled the project.
  2. modify ~/Documents/SolCli/solana-cli-program-template/program/src/entry_point.rs:
entrypoint!(process_instruction);
fn process_instruction(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8],
) -> ProgramResult {
    // below are my code
    msg!("Hello, world!");
    let sleep_time = time::Duration::from_secs(180);
    thread::sleep(sleep_time);
    msg!("Sleep over!");
    //above are my code
    if let Err(error) = process(program_id, accounts, instruction_data) {
        // catch the error so we can print it
        error.print::<SampleError>();
        return Err(error);
    }
    Ok(())
}

build using:

cargo build-sbf --debug
  1. run this command in solana-test-validator:
solana-test-validator --bpf-program SampGgdt3wioaoMZhC6LTSbg4pnuvQnSfJpDYeuXQBv ~/Documents/SolCli/solana-cli-program-template/target/deploy/solana_cli_template_program_bpf.debug --reset
  1. under ~/Documents/SolCli/solana-cli-program-template, run :

cargo run -- mint --url http://127.0.0.1:8899 -t User1 -k AKey --value Minted key value pair

  1. the error shows:

Finished dev [unoptimized + debuginfo] target(s) in 0.14s
         Running `target/debug/cli-program-template mint --url 'http://127.0.0.1:8899' -t User1 -k AKey --value Minted key value pair`
    thread 'main' panicked at src/utils/txn_utils.rs:138:10:
    called `Result::unwrap()` on an `Err` value: "error: send transaction: RPC response error -32002: Transaction simulation failed: Error processing Instruction 1: invalid account data for instruction [5 log messages]"
    stack backtrace:
       0:     0x55dcb8278666 - std::backtrace_rs::backtrace::libunwind::trace::hd6ffdc229294c06f
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
       1:     0x55dcb8278666 - std::backtrace_rs::backtrace::trace_unsynchronized::h991f79b6e9960513
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
       2:     0x55dcb8278666 - std::sys_common::backtrace::_print_fmt::hf4ebe716f7ccda10
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/sys_common/backtrace.rs:68:5
       3:     0x55dcb8278666 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h410d4c66be4e37f9
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/sys_common/backtrace.rs:44:22
       4:     0x55dcb82a31a0 - core::fmt::rt::Argument::fmt::he0ff0e0e188c0db5
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/fmt/rt.rs:142:9
       5:     0x55dcb82a31a0 - core::fmt::write::he40921d4802ce2ac
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/fmt/mod.rs:1120:17
       6:     0x55dcb827557f - std::io::Write::write_fmt::h5de5a4e7037c9b20
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/io/mod.rs:1846:15
       7:     0x55dcb8278444 - std::sys_common::backtrace::_print::hd8e2e3f655086663
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/sys_common/backtrace.rs:47:5
       8:     0x55dcb8278444 - std::sys_common::backtrace::print::h11c067a88e3bdb22
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/sys_common/backtrace.rs:34:9
       9:     0x55dcb8279b17 - std::panicking::default_hook::{{closure}}::h8c832ecb03fde8ea
      10:     0x55dcb8279879 - std::panicking::default_hook::h1633e272b4150cf3
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:292:9
      11:     0x55dcb8279fa8 - std::panicking::rust_panic_with_hook::hb164d19c0c1e71d4
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:781:13
      12:     0x55dcb8279e82 - std::panicking::begin_panic_handler::{{closure}}::h0369088c533c20e9
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:659:13
      13:     0x55dcb8278b66 - std::sys_common::backtrace::__rust_end_short_backtrace::hc11d910daf35ac2e
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/sys_common/backtrace.rs:171:18
      14:     0x55dcb8279bd4 - rust_begin_unwind
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:647:5
      15:     0x55dcb7492755 - core::panicking::panic_fmt::ha6effc2775a0749c
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/panicking.rs:72:14
      16:     0x55dcb7492d93 - core::result::unwrap_failed::ha188096f98826595
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/result.rs:1649:5
      17:     0x55dcb74e3994 - core::result::Result<T,E>::unwrap::h16662794dff12e62
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/result.rs:1073:23
      18:     0x55dcb74e3994 - cli_program_template::utils::txn_utils::new_account::ha68addd0c5b6bf5d
                                   at /home/jls/Documents/SolCli/solana-cli-program-template/src/utils/txn_utils.rs:135:22
      19:     0x55dcb74e3c9b - cli_program_template::utils::txn_utils::load_account::h0e6d2f5b2121d3bf
                                   at /home/jls/Documents/SolCli/solana-cli-program-template/src/utils/txn_utils.rs:158:17
      20:     0x55dcb7495348 - cli_program_template::validate_user_accounts_and_load::hb56208b4676db3fb
                                   at /home/jls/Documents/SolCli/solana-cli-program-template/src/main.rs:54:5
      21:     0x55dcb749d2a2 - cli_program_template::main::{{closure}}::h9c785578bb65e443
                                   at /home/jls/Documents/SolCli/solana-cli-program-template/src/main.rs:132:37
      22:     0x55dcb749a823 - tokio::park::thread::CachedParkThread::block_on::{{closure}}::h462f74f7da9fd11e
                                   at /home/jls/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.14.1/src/park/thread.rs:263:54
      23:     0x55dcb7499bea - tokio::coop::with_budget::{{closure}}::had5df15eb2209c5a
                                   at /home/jls/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.14.1/src/coop.rs:106:9
      24:     0x55dcb7493545 - std::thread::local::LocalKey<T>::try_with::h1a5c81eab93a9f68
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/thread/local.rs:286:16
      25:     0x55dcb749324d - std::thread::local::LocalKey<T>::with::hdbe1d95f418da9e9
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/thread/local.rs:262:9
      26:     0x55dcb749a643 - tokio::coop::with_budget::h11b5eec8fcf79f52
                                   at /home/jls/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.14.1/src/coop.rs:99:5
      27:     0x55dcb749a643 - tokio::coop::budget::hd6cfdef216dec3be
                                   at /home/jls/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.14.1/src/coop.rs:76:5
      28:     0x55dcb749a643 - tokio::park::thread::CachedParkThread::block_on::he4417b4e4e810f4c
                                   at /home/jls/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.14.1/src/park/thread.rs:263:31
      29:     0x55dcb749b710 - tokio::runtime::enter::Enter::block_on::h731159ab96fc0a7a
                                   at /home/jls/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.14.1/src/runtime/enter.rs:151:13
      30:     0x55dcb7499203 - tokio::runtime::thread_pool::ThreadPool::block_on::h5b9e81bd9dc9e247
                                   at /home/jls/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.14.1/src/runtime/thread_pool/mod.rs:77:9
      31:     0x55dcb7498eaf - tokio::runtime::Runtime::block_on::h00b1f1c0ee370819
                                   at /home/jls/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.14.1/src/runtime/mod.rs:463:43
      32:     0x55dcb7495548 - cli_program_template::main::hc4f2d253735ab878
                                   at /home/jls/Documents/SolCli/solana-cli-program-template/src/main.rs:229:5
      33:     0x55dcb749875b - core::ops::function::FnOnce::call_once::h35b96d8a82c6a205
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/ops/function.rs:250:5
      34:     0x55dcb74999ce - std::sys_common::backtrace::__rust_begin_short_backtrace::he76c24ddbb2b2eee
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/sys_common/backtrace.rs:155:18
      35:     0x55dcb74990f1 - std::rt::lang_start::{{closure}}::h142f08fc39eb7ddf
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/rt.rs:166:18
      36:     0x55dcb826f441 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h3f2a75cbf4c937aa
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/ops/function.rs:284:13
      37:     0x55dcb826f441 - std::panicking::try::do_call::h8b4251a3ff07cb79
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:554:40
      38:     0x55dcb826f441 - std::panicking::try::h7c5a033d33a17020
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:518:19
      39:     0x55dcb826f441 - std::panic::catch_unwind::hb11e6d2bc85d3f56
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panic.rs:142:14
      40:     0x55dcb826f441 - std::rt::lang_start_internal::{{closure}}::h0f4b130784f05f2d
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/rt.rs:148:48
      41:     0x55dcb826f441 - std::panicking::try::do_call::ha5e1e15d954bfd38
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:554:40
      42:     0x55dcb826f441 - std::panicking::try::h1ebdd47dd0e39c8c
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:518:19
      43:     0x55dcb826f441 - std::panic::catch_unwind::hbc815fbc36dbbcd0
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panic.rs:142:14
      44:     0x55dcb826f441 - std::rt::lang_start_internal::h4d236095b69a230b
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/rt.rs:148:20
      45:     0x55dcb74990ca - std::rt::lang_start::hda13d5e5a6eaa6ae
                                   at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/rt.rs:165:17
      46:     0x55dcb749560e - main
      47:     0x7f2c4fa29d90 - __libc_start_call_main
                                   at ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
      48:     0x7f2c4fa29e40 - __libc_start_main_impl
                                   at ./csu/../csu/libc-start.c:392:3
      49:     0x55dcb7493095 - _start
      50:                0x0 - <unknown>

it seems that put a sleep in bpf program is not possible.

my question is:

  1. why sleep is not allowed in bpf program?

  2. is it better to debug solana-test-validator itself and step into bpf program? or just as I did, set breakpoints in bpf program first?

  3. if my approach is right, how to set a waiting "thing" to let me attach a debugger?

1 Answer 1

1

You are very limited in what you can do in an on-chain program. For example, you can't access the internet, you can't use threads, you can't even get randomness. This is required so that all validators come to the same result when processing your program.

In your case, if on-chain programs could sleep, they could halt the network!

You can read more about the on-chain limits at https://solana.com/docs/programs/lang-rust#restrictions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.