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

Add debug file dump #7375

Merged
merged 7 commits into from
Oct 23, 2024
Merged

Conversation

piohei
Copy link
Contributor

@piohei piohei commented Mar 12, 2024

Motivation

It could be very helpful to be able to dump to a JSON file whole debugger context: calls, source code, refs, etc. It would allow then to do a post processing like for example creating heatmap for most costly lines in terms of gas usage.

Closes #7364
Closes #7112

Solution

New --dump param is being added to forge debug and forge test --debug commands. It requires a path to file where to dump data and passing --debug flag same time. Debugger code is refactored to create abstraction over it. It is then used by two different presenters: TUI (interactive debugger) and file dumper. I also added to debugger data original path for the source file to allow figuring out where to make changes when debugging.

Copy link
Member

@onbjerg onbjerg left a comment

Choose a reason for hiding this comment

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

i am not too sure i am fond of this approach, ideally we'd move away from allocating a big chunk of memory for the debugger, i.e. ideally we'd move away from record/replay

@piohei
Copy link
Contributor Author

piohei commented Mar 12, 2024

Thanks for comment! Do you find the idea of changing record/replay approach as a new feature request or a blocker for that functionality? This record/replay could be also run only for the dumping or dump can be created on the fly and written to file. The main idea is to be able to use that output for post processing things. Other approach could be to create a plugin system and move things like that out of the core of foundry. What do you think?

@onbjerg
Copy link
Member

onbjerg commented Mar 12, 2024

hmm, thinking about this more, I think this is ok. if/when we move away from record/replay we can always change this as well

would like input from @DaniPopes iirc there are some coming changes to the debugger?

@piohei piohei force-pushed the add_debug_file_dump branch 6 times, most recently from a131d07 to 940393c Compare March 19, 2024 06:51
@piohei piohei closed this Mar 19, 2024
@piohei piohei reopened this Mar 19, 2024
@piohei piohei marked this pull request as ready for review March 19, 2024 07:57
@onbjerg
Copy link
Member

onbjerg commented Mar 19, 2024

ping @DaniPopes

Copy link
Member

@DaniPopes DaniPopes left a comment

Choose a reason for hiding this comment

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

Makes sense

would like input from @DaniPopes iirc there are some coming changes to the debugger?

No

@piohei
Copy link
Contributor Author

piohei commented Mar 19, 2024

Just fixed tests (clippy + fmt)

@piohei
Copy link
Contributor Author

piohei commented Mar 20, 2024

Fixed (again) - now using proper rust version locally. I don't know if this is intentional or not but in Cargo.toml there is rust-version = "1.76" while CI is using nightly version.

@piohei
Copy link
Contributor Author

piohei commented Mar 22, 2024

Any chances for merging this one? :) I don't want to mix same code and I could then work on fixing this #7292.

@mattsse
Copy link
Member

mattsse commented Apr 8, 2024

very sorry about this @piohei

this now has a few conflicts that must be resolved first, still supportive of that feature

@piohei
Copy link
Contributor Author

piohei commented Apr 8, 2024

No problem. I will fix them. :)

@zerosnacks
Copy link
Member

Hi @piohei thanks for your PR! Would you mind updating the PR to resolve the merge conflicts? Looks like there is consensus on the feature and the approach so it would be great to get this merged

@zerosnacks zerosnacks self-assigned this Jul 12, 2024
Copy link
Member

@zerosnacks zerosnacks left a comment

Choose a reason for hiding this comment

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

Thanks for your updates!

Pending minor nits, implementation LGTM

General note, the file dumps are quite large (7MB / 100K lines) for basic Counter w/ test_Increment but I can see this be useful as a preprocessed source

crates/forge/bin/cmd/test/mod.rs Outdated Show resolved Hide resolved
crates/script/src/lib.rs Outdated Show resolved Hide resolved
@piohei
Copy link
Contributor Author

piohei commented Oct 11, 2024

@zerosnacks Updated with suggestions and rebased with master again to allow merging. :)

Copy link
Member

@zerosnacks zerosnacks left a comment

Choose a reason for hiding this comment

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

Thanks, latest changes LGTM

Would like to get one more review before merging cc @grandizzy

To setup:

$ forge init counter
$ git clone https://github.com/reilabs/foundry.git reilabs-foundry
$ cd reilabs-foundry
$ git checkout add_debug_file_dump
$ make build
$ cd ..
$ cd counter
$ ../reilabs-foundry/target/debug/forge test --debug test_Increment --dump dump.json
$ ../reilabs-foundry/target/debug/forge debug --debug script/Counter.s.sol --dump dump.json

Side note: forge debug --debug is a bit of a weird syntax but changing it is out of scope of this PR

@zerosnacks zerosnacks enabled auto-merge (squash) October 21, 2024 11:55
@zerosnacks
Copy link
Member

zerosnacks commented Oct 21, 2024

@piohei would you mind fixing the merge conflicts one last time? I don't have the permissions to push directly to your fork as I normally would.

@zerosnacks zerosnacks self-requested a review October 21, 2024 12:07
@piohei piohei force-pushed the add_debug_file_dump branch 2 times, most recently from 58dc7d1 to fbaf708 Compare October 21, 2024 12:44
@piohei
Copy link
Contributor Author

piohei commented Oct 21, 2024

@zerosnacks rebased with master. :) should be fine now

Copy link
Member

@zerosnacks zerosnacks left a comment

Choose a reason for hiding this comment

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

Thanks! Pending review of @grandizzy

Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

looks good, thank you! couple of nits and I suggest adding a test too (like make sure json file is created when running forge test --debug

crates/debugger/src/file_dumper/mod.rs Outdated Show resolved Hide resolved
crates/debugger/src/file_dumper/mod.rs Outdated Show resolved Hide resolved
crates/debugger/src/file_dumper/mod.rs Outdated Show resolved Hide resolved
@piohei
Copy link
Contributor Author

piohei commented Oct 22, 2024

@grandizzy Thanks for suggestions! Test added and code refactored as suggested. :)
@zerosnacks I have rebased with master once again.

Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

thank you, lgtm! CC @DaniPopes if you want to have a look too before merging

@zerosnacks
Copy link
Member

zerosnacks commented Oct 23, 2024

Merging this, any additional feedback or improvements can be handled in follow-ups

Thanks @piohei for your contribution!

@zerosnacks zerosnacks merged commit 3b2e57a into foundry-rs:master Oct 23, 2024
21 checks passed
leovct pushed a commit to leovct/foundry that referenced this pull request Oct 23, 2024
* Refactored debugger to extract TUI abstraction. Added option to dump debugger context to file as json.

* Update crates/forge/bin/cmd/test/mod.rs

Co-authored-by: zerosnacks <[email protected]>

* Update crates/script/src/lib.rs

Co-authored-by: zerosnacks <[email protected]>

* Cleanup code.

* Added test.

* Reformat code.

* Reformat code.

---------

Co-authored-by: zerosnacks <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-debugger Area: debugger Cmd-forge-debug Command: forge run
Projects
Status: Ready For Review
6 participants