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

Fix debuginfo for generators #84752

Merged
merged 2 commits into from
May 2, 2021
Merged

Conversation

lrh2000
Copy link
Contributor

@lrh2000 lrh2000 commented Apr 30, 2021

First, all fields except the discriminant (including outer_fields) should be put into structures inside the variant part, which gives an equivalent layout but offers us much better integration with debuggers.

Second, artificial flags in generator variants should be removed.

  • Literally, variants are not artificial. We have yield statements, upvars and inner variables in the source code.
  • Functionally, we don't want debuggers to suppress the variants. It contains the state of the generator, which is useful when debugging. So they shouldn't be marked artificial.
  • Debuggers may use artificial flags to find the active variant. In this case, marking variants artificial will make debuggers not work properly.

Fixes #62572.
Fixes #79009.

And refer https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.

All fields except the discriminant (including `outer_fields`)
should be put into structures inside the variant part, which gives
an equivalent layout but offers us much better integration with
debuggers.
@rust-highfive
Copy link
Collaborator

r? @matthewjasper

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 30, 2021
@lrh2000
Copy link
Contributor Author

lrh2000 commented Apr 30, 2021

There is actually another commit (lrh2000@e65c460) that names the upvars for closures and generators in debuginfo. Finally we'll be able to see something like

y::main::closure-0 {_upvar_ref_mut__b: 0x[...], _upvar_ref__a: 0x[...]}

But I am not sure whether what I did is right. Could I add the above commit into this PR or should I open a separate PR for it later?

Any comments are welcome. And thanks in advance.

 - Literally, variants are not artificial. We have `yield` statements,
   upvars and inner variables in the source code.
 - Functionally, we don't want debuggers to suppress the variants. It
   contains the state of the generator, which is useful when debugging.
   So they shouldn't be marked artificial.
 - Debuggers may use artificial flags to find the active variant. In
   this case, marking variants artificial will make debuggers not work
   properly.

Fixes rust-lang#79009.
@lrh2000
Copy link
Contributor Author

lrh2000 commented May 1, 2021

cc @tmandry

@tmandry
Copy link
Member

tmandry commented May 1, 2021

There is actually another commit (lrh2000@e65c460) that names the upvars for closures and generators in debuginfo. Finally we'll be able to see something like

y::main::closure-0 {_upvar_ref_mut__b: 0x[...], _upvar_ref__a: 0x[...]}

I'd remove upvar since it doesn't have meaning outside the compiler. mut also doesn't really matter in this context, imo. ref does help explain why you're seeing a pointer value, I like that.

@tmandry
Copy link
Member

tmandry commented May 1, 2021

Closes #62572 too I think.

I'd like to eventually do this for GeneratorLayout itself, but that's another change.

Thanks! This is a nice improvement. I think this change is ready so I'm going to approve for now and you can open another PR with the upvar naming change.

@bors r+

@bors
Copy link
Contributor

bors commented May 1, 2021

📌 Commit 5bf989e has been approved by tmandry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 1, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request May 1, 2021
Fix debuginfo for generators

First, all fields except the discriminant (including `outer_fields`) should be put into structures inside the variant part, which gives an equivalent layout but offers us much better integration with debuggers.

Second, artificial flags in generator variants should be removed.
 - Literally, variants are not artificial. We have `yield` statements, upvars and inner variables in the source code.
 - Functionally, we don't want debuggers to suppress the variants. It contains the state of the generator, which is useful when debugging. So they shouldn't be marked artificial.
 - Debuggers may use artificial flags to find the active variant. In this case, marking variants artificial will make debuggers not work properly.

Fixes rust-lang#79009.

And refer https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.
@lrh2000
Copy link
Contributor Author

lrh2000 commented May 2, 2021

I'd remove upvar since it doesn't have meaning outside the compiler. mut also doesn't really matter in this context, imo. ref does help explain why you're seeing a pointer value, I like that.

Thanks a lot for your feedback. I'll take your advice. The trickiest problem is that if we simply remove upvar, we may encounter collision of names accidentally. For example, see the following code:

fn main() {
    let a = 3;
    let mut c = move || {
        let a = a;
        yield;
        let b = a;
    };
    Pin::new(&mut c).resume(());
    _zzz();
}

We could get something like

$1 = generator_objects::main::generator-0::Suspend0{a: 3, _upvar__a: 3}

But if we simply remove _upvar__, there are two fields named a in the same structure. I'll think about how to solve the problem, or if you have any suggestions, I'll appreciate them.

@lrh2000
Copy link
Contributor Author

lrh2000 commented May 2, 2021

I'd remove upvar since it doesn't have meaning outside the compiler.

I think what you said is reasonable. How about just replacing upvar with captured? Do you think it is better? capture is widely used in Rust official documentation, here and there.

mut also doesn't really matter in this context, imo.

I agree. I added mut because it is consistent with the style of the Rust language. It may be helpful if finally we can add a pretty-printer in debuggers. It demangles something like _captured_ref_mut__a into &mut a and show it to the end users. (So in the meantime, it also hides captured or upvar, thus their name doesn't really matter if we can achieve the final goal.)

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request May 2, 2021
Fix debuginfo for generators

First, all fields except the discriminant (including `outer_fields`) should be put into structures inside the variant part, which gives an equivalent layout but offers us much better integration with debuggers.

Second, artificial flags in generator variants should be removed.
 - Literally, variants are not artificial. We have `yield` statements, upvars and inner variables in the source code.
 - Functionally, we don't want debuggers to suppress the variants. It contains the state of the generator, which is useful when debugging. So they shouldn't be marked artificial.
 - Debuggers may use artificial flags to find the active variant. In this case, marking variants artificial will make debuggers not work properly.

Fixes rust-lang#62572.
Fixes rust-lang#79009.

And refer https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 2, 2021
Rollup of 5 pull requests

Successful merges:

 - rust-lang#84358 (Update closure capture error logging for disjoint captures for disjoint captures)
 - rust-lang#84392 (Make AssertKind::fmt_assert_args public)
 - rust-lang#84752 (Fix debuginfo for generators)
 - rust-lang#84763 (shrink doctree::Module)
 - rust-lang#84821 (Fix nit in rustc_session::options)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 2553053 into rust-lang:master May 2, 2021
@rustbot rustbot added this to the 1.54.0 milestone May 2, 2021
@tmandry
Copy link
Member

tmandry commented May 4, 2021

I'd remove upvar since it doesn't have meaning outside the compiler.

I think what you said is reasonable. How about just replacing upvar with captured? Do you think it is better? capture is widely used in Rust official documentation, here and there.

I'd be fine with it. This should also affect closures. We should also support printing the original name of the variable while stepping through the closure/generator (I think we already do).

mut also doesn't really matter in this context, imo.

I agree. I added mut because it is consistent with the style of the Rust language. It may be helpful if finally we can add a pretty-printer in debuggers. It demangles something like _captured_ref_mut__a into &mut a and show it to the end users. (So in the meantime, it also hides captured or upvar, thus their name doesn't really matter if we can achieve the final goal.)

My sense is that could be confusing, since the user never writes &mut a in their code to get the capture. I don't feel too strongly about this.

bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 14, 2021
Name the captured upvars for closures/generators in debuginfo

Previously, debuggers print closures as something like
```
y::main::closure-0 (0x7fffffffdd34)
```
The pointer actually references to an upvar. It is not very obvious, especially for beginners.

It's because upvars don't have names before, as they are packed into a tuple. This PR names the upvars, so we can expect to see something like
```
y::main::closure-0 {_captured_ref__b: 0x[...]}
```

r? `@tmandry`
Discussed at rust-lang#84752 (comment) .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1.48 beta test failures, debuginfo-gdb 9.2 -> 10.1 Pretty-printing support for Rust generators
6 participants