3

I'm trying to debug an issue with the Solana JSON RPC, which I can reproduce using the solana-test-validator locally.

I've built the solana-labs/solana repo locally using ./cargo build. I'm able to run the test validator from that repo locally: ./target/debug/solana-test-validator --account-dir ~/scratch/solana-validator-tokens

I'm able to insert println! statements into the solana-labs/solana code, and see them in the terminal where my built test validator is running.

Is there a way to put a debugger in the solana-labs/solana code and trigger its breakpoints using my solana-test-validator, so that I can more easily debug the code?

1 Answer 1

4

I was able to do this in VSCode by following the documentation here: https://code.visualstudio.com/docs/languages/rust#_installation

You need the rust-analyzer extension installed in VSCode.

You'll also need to install one of the debug extensions, I installed CodeLLDB. The VSCode docs say to install that one for MacOS/Linux, and MS C++ Tools for Windows. I only tested on MacOS.

Now go to the "Run and Debug" tab in VSCode, and you'll see a "create a launch.json file" link: Run and Debug tab in VSCode

If you click that you should get a popup offering to create launch.json from the cargo.toml file in the repo: create launch.json automatically

You can do that if you like, but it'll generate a lot of configurations, including eg. one for each set of tests in the repo. You probably don't need that. You can manually create a .vscode/launch.json with the following content:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'solana-test-validator'",
            "cargo": {
                "args": [
                    "build",
                    "--bin=solana-test-validator",
                    "--package=solana-validator"
                ],
                "filter": {
                    "name": "solana-test-validator",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        },
    ]
}

This task will build the ./target/debug/solana-test-validator binary (and all its dependencies), and then run it. In my case I was running it with ./target/debug/solana-test-validator --account-dir ~/scratch/solana-validator-tokens, so I need to include these arguments. You can do this by modifying args:

"args": ["--account-dir", "/Users/callum/scratch/solana-validator-tokens"],

Note that the ~ shell expansion doesn't work here, you need to give a complete path.

Now you can find and run this task from the Run and Debug UI:

run debug task from Run and Debug UI

Select it and click the green tick to run. You'll see a cargo terminal option run and compile everything, and then the familiar test validator UI:

Test validator UI

Note that ctrl+c in that terminal window doesn't seem to work. Use the Stop button in the floating debug controls instead:

Stop the debugger

And now when you interact with the test validator, for example by making RPC calls, breakpoints in VSCode will work. You'll also get debug access to variables etc:

Debug UI at a breakpoint

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.