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.

@piohei piohei force-pushed the add_debug_file_dump branch from 14d6720 to 7cdf9ff Compare March 12, 2024 12:54
Copy link
Collaborator

@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
Collaborator

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 force-pushed the add_debug_file_dump branch from 940393c to 0026488 Compare March 19, 2024 06:54
@piohei piohei reopened this Mar 19, 2024
@piohei piohei marked this pull request as ready for review March 19, 2024 07:57
@piohei piohei force-pushed the add_debug_file_dump branch from 26f36b6 to 84608e0 Compare March 19, 2024 12:57
@onbjerg
Copy link
Collaborator

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 piohei force-pushed the add_debug_file_dump branch from 84608e0 to 27dbb83 Compare March 19, 2024 20:53
@piohei
Copy link
Contributor Author

piohei commented Mar 19, 2024

Just fixed tests (clippy + fmt)

@piohei piohei force-pushed the add_debug_file_dump branch from 27dbb83 to fc8a710 Compare March 20, 2024 09:07
@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
@zerosnacks zerosnacks self-requested a review October 7, 2024 15:23
@piohei piohei force-pushed the add_debug_file_dump branch from 1ca5252 to 04eee70 Compare October 11, 2024 11:27
@piohei
Copy link
Contributor Author

piohei commented Oct 11, 2024

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

@zerosnacks zerosnacks requested a review from onbjerg October 14, 2024 16:22
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 zerosnacks disabled auto-merge October 21, 2024 11:56
@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

@piohei piohei force-pushed the add_debug_file_dump branch from bdd7f75 to 62e1466 Compare October 22, 2024 11:19
@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.

@zerosnacks zerosnacks requested a review from grandizzy October 22, 2024 13:04
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]>
rplusq pushed a commit to rplusq/foundry that referenced this pull request Nov 29, 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: Completed
6 participants