Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC gdbstub for debugging guest programs #107

Merged
merged 8 commits into from
Oct 18, 2024
Merged

PoC gdbstub for debugging guest programs #107

merged 8 commits into from
Oct 18, 2024

Conversation

poszu
Copy link
Collaborator

@poszu poszu commented Sep 13, 2024

A basic gdbstub that allows for debugging VM guest programs.

Example:

First, run the program with GDB on selected port:

let elf = include_bytes!("path to program elf");
let stdin = AthenaStdin::new();
let client = ExecutionClient::new();
client.execute_with_gdb(ELF, stdin.clone(), None, None, None, 9001);

It will spin up GDB server listening on port 9001 and halt the guest program before executing the first instruction. Note that the Rust VM host program is running, not stopped on any breakpoint etc.

Connect with gdb or IDE debugger:
GDB:

> gdb
(gdb) file <path to elf with built with symbols>
(gdb) target remote localhost:9001
(gdb) debug normally

Vscode:
Edit launch.json configuration and add:

{
    "type": "gdb",
    "request": "attach",
    "name": "Attach to guest program",
    "executable": "<path to elf>",
    "target": ":9001",
    "remote": true,
    "cwd": "${workspaceRoot}",
    "valuesFormatting": "parseText",
    "stopAtConnect": true
},

and start debugging with it.

@poszu poszu requested a review from jellonek September 13, 2024 12:21
@lrettig
Copy link
Contributor

lrettig commented Sep 13, 2024

Worth taking a look at this: risc0/risc0#129

@lrettig
Copy link
Contributor

lrettig commented Oct 13, 2024

I tried adding the recommended configuration to launch.json in vscode but I get a bunch of errors, any idea why?

image
image

@lrettig
Copy link
Contributor

lrettig commented Oct 13, 2024

Found it, it requires this extension: https://marketplace.visualstudio.com/items?itemName=webfreak.debug

@lrettig
Copy link
Contributor

lrettig commented Oct 13, 2024

A few other notes on getting this to work:

  • by default athena builds programs without debug info. You need to manually compile the program in question with a command like this:
> RUSTUP_TOOLCHAIN=athena cargo build --target riscv32em-athena-zkvm-elf

and use the path to this binary in the launch.json config.

  • the default Linux gdb doesn't support the riscv architecture. You need gdb from something like https://github.com/riscv-collab/riscv-gnu-toolchain and then you need to set gdbpath in launch.json as well.
  • gdb gets a bit confused about where it is, but if you set a breakpoint in the main code and run "c" (continue) it seems to work. Note that the "Step over", "Step into" etc. commands don't seem to work correctly; you need to use breakpoints.

@lrettig
Copy link
Contributor

lrettig commented Oct 14, 2024

Curious why this is a draft, should we aim to merge this? It's clearly useful. IMHO the only thing missing here is documentation.

@poszu
Copy link
Collaborator Author

poszu commented Oct 14, 2024

Curious why this is a draft, should we aim to merge this? It's clearly useful. IMHO the only thing missing here is documentation.

Yeah, documentation is missing but also because it's very basic support and not everything works perfectly. There are some issues with presenting local variables (I'm not sure if this is issue with the integration or vscode though - e.g. gdb itself prints variables nicely from when asked to with p <variable name>). I'm curious, what problems with stepping did you have?

Also, it would be great to have an option to build guest programs with debug profile (currently release is hardcoded).


by default athena builds programs without debug info. You need to manually compile the program in question with a command like this:

RUSTUP_TOOLCHAIN=athena cargo build --target riscv32em-athena-zkvm-elf

Debug info can be easily turned on with placing this in the Cargo.toml:

[profile.release]
debug=true

@lrettig
Copy link
Contributor

lrettig commented Oct 14, 2024

I'm curious, what problems with stepping did you have?

It didn't work at all. Every time I tried to step over, in, or out, it was the same as "continue."

@poszu poszu marked this pull request as ready for review October 17, 2024 09:00
Add notes on RISC-V gdb
Copy link
Contributor

@lrettig lrettig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tweaked README a little, lgtm

@poszu poszu enabled auto-merge (squash) October 18, 2024 13:47
@poszu poszu merged commit b4eaa89 into main Oct 18, 2024
8 checks passed
@lrettig lrettig deleted the basic-gdbstub branch October 18, 2024 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants