Variables in compiler are optimized out

It seems I am not quite able to see the contents of variables. At this moment, they got optimized out:

(gdb) print name
$5 = rustc_span::symbol::Symbol (rustc_span::symbol::SymbolIndex {private: 662})
(gdb) print orig_name
$6 = <optimized out>
(gdb) print item
$7 = <optimized out>
(gdb) 

Is there a way to fix it? according to https://github.com/rust-lang/rust/issues/46698, it probably has something to do with the optimization flag?

I have the related flags as follows in config.toml:

debuginfo-level = 1

# Debuginfo level for the compiler.
debuginfo-level-rustc = 2

How can I handle this problem?

Maybe using std::hint::blackbox on the variables?

This is an inevitable consequence of building an optimized compiler (you can build it unoptimized, but generally that's too slow to be useful from what I understand, though I've never tried it myself).

The conventional way of getting at values like this inside rustc is to use debug!() logs.

then is it conventional to use a debugger at all? or should I just learn how to use logs? the way I find it difficult to rely on logging all the time is recompilation overhead is a bit too high. any suggestions?

You'll probably want debuginfo-level-std = 2 as well, or you'll be blind to some calls the compiler may make.

You could try setting RUSTFLAGS=-Copt-level=1 in the environment, as a compromise between the performance of the compiler and its debug-ability.

This is one of the huge pains of working on rustc. I really wish the compiler only had to be built once instead of twice, because then using a slow, unoptimized compiler would be practical (for debugging, at least).

Is it possible to use an optimized build for stage 1 but a unoptimized build for stage 2? If so that would help.

You generally don't need a stage2 compiler. The stage1 compiler works fine for most things (including procedural macros).

1 Like

I actually tried it, and if I remember correctly, it was OK to debug small programs (think single ui test).

I never build stage 2. I run ./x.py test src/test/ui --stage 1 --keep-stage 1 which is what you want 90% of the time.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.