1+ //! The `InstrumentCoverage` MIR pass implementation includes debugging tools and options
2+ //! to help developers understand and/or improve the analysis and instrumentation of a MIR.
3+ //!
4+ //! To enable coverage, include the rustc command line option:
5+ //!
6+ //! * `-Z instrument-coverage`
7+ //!
8+ //! MIR Dump Files, with additional `CoverageGraph` graphviz and `CoverageSpan` spanview
9+ //! ------------------------------------------------------------------------------------
10+ //!
11+ //! Additional debugging options include:
12+ //!
13+ //! * `-Z dump-mir=InstrumentCoverage` - Generate `.mir` files showing the state of the MIR,
14+ //! before and after the `InstrumentCoverage` pass, for each compiled function.
15+ //!
16+ //! * `-Z dump-mir-graphviz` - If `-Z dump-mir` is also enabled for the current MIR node path,
17+ //! each MIR dump is accompanied by a before-and-after graphical view of the MIR, in Graphviz
18+ //! `.dot` file format (which can be visually rendered as a graph using any of a number of free
19+ //! Graphviz viewers and IDE extensions).
20+ //!
21+ //! For the `InstrumentCoverage` pass, this option also enables generation of an additional
22+ //! Graphviz `.dot` file for each function, rendering the `CoverageGraph`: the control flow
23+ //! graph (CFG) of `BasicCoverageBlocks` (BCBs), as nodes, internally labeled to show the
24+ //! `CoverageSpan`-based MIR elements each BCB represents (`BasicBlock`s, `Statement`s and
25+ //! `Terminator`s), assigned coverage counters and/or expressions, and edge counters, as needed.
26+ //!
27+ //! (Note the additional option, `-Z graphviz-dark-mode`, can be added, to change the rendered
28+ //! output from its default black-on-white background to a dark color theme, if desired.)
29+ //!
30+ //! * `-Z dump-mir-spanview` - If `-Z dump-mir` is also enabled for the current MIR node path,
31+ //! each MIR dump is accompanied by a before-and-after `.html` document showing the function's
32+ //! original source code, highlighted by it's MIR spans, at the `statement`-level (by default),
33+ //! `terminator` only, or encompassing span for the `Terminator` plus all `Statement`s, in each
34+ //! `block` (`BasicBlock`).
35+ //!
36+ //! For the `InstrumentCoverage` pass, this option also enables generation of an additional
37+ //! spanview `.html` file for each function, showing the aggregated `CoverageSpan`s that will
38+ //! require counters (or counter expressions) for accurate coverage analysis.
39+ //!
40+ //! Debug Logging
41+ //! -------------
42+ //!
43+ //! The `InstrumentCoverage` pass includes debug logging messages at various phases and decision
44+ //! points, which can be enabled via environment variable:
45+ //!
46+ //! ```shell
47+ //! RUSTC_LOG=rustc_mir::transform::coverage=debug
48+ //! ```
49+ //!
50+ //! Other module paths with coverage-related debug logs may also be of interest, particularly for
51+ //! debugging the coverage map data, injected as global variables in the LLVM IR (during rustc's
52+ //! code generation pass). For example:
53+ //!
54+ //! ```shell
55+ //! RUSTC_LOG=rustc_mir::transform::coverage,rustc_codegen_ssa::coverageinfo,rustc_codegen_llvm::coverageinfo=debug
56+ //! ```
57+ //!
58+ //! Coverage Debug Options
59+ //! ---------------------------------
60+ //!
61+ //! Additional debugging options can be enabled using the environment variable:
62+ //!
63+ //! ```shell
64+ //! RUSTC_COVERAGE_DEBUG_OPTIONS=<options>
65+ //! ```
66+ //!
67+ //! These options are comma-separated, and specified in the format `option-name=value`. For example:
68+ //!
69+ //! ```shell
70+ //! $ RUSTC_COVERAGE_DEBUG_OPTIONS=counter-format=id+operation,allow-unused-expressions=yes cargo build
71+ //! ```
72+ //!
73+ //! Coverage debug options include:
74+ //!
75+ //! * `allow-unused-expressions=yes` or `no` (default: `no`)
76+ //!
77+ //! The `InstrumentCoverage` algorithms _should_ only create and assign expressions to a
78+ //! `BasicCoverageBlock`, or an incoming edge, if that expression is either (a) required to
79+ //! count a `CoverageSpan`, or (b) a dependency of some other required counter expression.
80+ //!
81+ //! If an expression is generated that does not map to a `CoverageSpan` or dependency, this
82+ //! probably indicates there was a bug in the algorithm that creates and assigns counters
83+ //! and expressions.
84+ //!
85+ //! When this kind of bug is encountered, the rustc compiler will panic by default. Setting:
86+ //! `allow-unused-expressions=yes` will log a warning message instead of panicking (effectively
87+ //! ignoring the unused expressions), which may be helpful when debugging the root cause of
88+ //! the problem.
89+ //!
90+ //! * `counter-format=<choices>`, where `<choices>` can be any plus-separated combination of `id`,
91+ //! `block`, and/or `operation` (default: `block+operation`)
92+ //!
93+ //! This option effects both the `CoverageGraph` (graphviz `.dot` files) and debug logging, when
94+ //! generating labels for counters and expressions.
95+ //!
96+ //! Depending on the values and combinations, counters can be labeled by:
97+ //!
98+ //! * `id` - counter or expression ID (ascending counter IDs, starting at 1, or descending
99+ //! expression IDs, starting at `u32:MAX`)
100+ //! * `block` - the `BasicCoverageBlock` label (for example, `bcb0`) or edge label (for
101+ //! example `bcb0->bcb1`), for counters or expressions assigned to count a
102+ //! `BasicCoverageBlock` or edge. Intermediate expressions (not directly associated with
103+ //! a BCB or edge) will be labeled by their expression ID, unless `operation` is also
104+ //! specified.
105+ //! * `operation` - applied to expressions only, labels include the left-hand-side counter
106+ //! or expression label (lhs operand), the operator (`+` or `-`), and the right-hand-side
107+ //! counter or expression (rhs operand). Expression operand labels are generated
108+ //! recursively, generating labels with nested operations, enclosed in parentheses
109+ //! (for example: `bcb2 + (bcb0 - bcb1)`).
110+
1111use super :: graph:: { BasicCoverageBlock , BasicCoverageBlockData , CoverageGraph } ;
2112use super :: spans:: CoverageSpan ;
3113
@@ -20,21 +130,19 @@ const RUSTC_COVERAGE_DEBUG_OPTIONS: &str = "RUSTC_COVERAGE_DEBUG_OPTIONS";
20130pub ( crate ) fn debug_options < ' a > ( ) -> & ' a DebugOptions {
21131 static DEBUG_OPTIONS : SyncOnceCell < DebugOptions > = SyncOnceCell :: new ( ) ;
22132
23- & DEBUG_OPTIONS . get_or_init ( || DebugOptions :: new ( ) )
133+ & DEBUG_OPTIONS . get_or_init ( || DebugOptions :: from_env ( ) )
24134}
25135
26136/// Parses and maintains coverage-specific debug options captured from the environment variable
27- /// "RUSTC_COVERAGE_DEBUG_OPTIONS", if set. Options can be set on the command line by, for example:
28- ///
29- /// $ RUSTC_COVERAGE_DEBUG_OPTIONS=counter-format=block,allow_unused_expressions=n cargo build
137+ /// "RUSTC_COVERAGE_DEBUG_OPTIONS", if set.
30138#[ derive( Debug , Clone ) ]
31139pub ( crate ) struct DebugOptions {
32140 pub allow_unused_expressions : bool ,
33141 counter_format : ExpressionFormat ,
34142}
35143
36144impl DebugOptions {
37- fn new ( ) -> Self {
145+ fn from_env ( ) -> Self {
38146 let mut allow_unused_expressions = true ;
39147 let mut counter_format = ExpressionFormat :: default ( ) ;
40148
@@ -152,10 +260,11 @@ impl DebugCounters {
152260 }
153261
154262 pub fn enable ( & mut self ) {
263+ debug_assert ! ( !self . is_enabled( ) ) ;
155264 self . some_counters . replace ( FxHashMap :: default ( ) ) ;
156265 }
157266
158- pub fn is_enabled ( & mut self ) -> bool {
267+ pub fn is_enabled ( & self ) -> bool {
159268 self . some_counters . is_some ( )
160269 }
161270
@@ -294,12 +403,13 @@ impl GraphvizData {
294403 }
295404
296405 pub fn enable ( & mut self ) {
406+ debug_assert ! ( !self . is_enabled( ) ) ;
297407 self . some_bcb_to_coverage_spans_with_counters = Some ( FxHashMap :: default ( ) ) ;
298408 self . some_bcb_to_dependency_counters = Some ( FxHashMap :: default ( ) ) ;
299409 self . some_edge_to_counter = Some ( FxHashMap :: default ( ) ) ;
300410 }
301411
302- pub fn is_enabled ( & mut self ) -> bool {
412+ pub fn is_enabled ( & self ) -> bool {
303413 self . some_bcb_to_coverage_spans_with_counters . is_some ( )
304414 }
305415
@@ -399,11 +509,12 @@ impl UsedExpressions {
399509 }
400510
401511 pub fn enable ( & mut self ) {
512+ debug_assert ! ( !self . is_enabled( ) ) ;
402513 self . some_used_expression_operands = Some ( FxHashMap :: default ( ) ) ;
403514 self . some_unused_expressions = Some ( Vec :: new ( ) ) ;
404515 }
405516
406- pub fn is_enabled ( & mut self ) -> bool {
517+ pub fn is_enabled ( & self ) -> bool {
407518 self . some_used_expression_operands . is_some ( )
408519 }
409520
@@ -416,7 +527,7 @@ impl UsedExpressions {
416527 }
417528 }
418529
419- pub fn expression_is_used ( & mut self , expression : & CoverageKind ) -> bool {
530+ pub fn expression_is_used ( & self , expression : & CoverageKind ) -> bool {
420531 if let Some ( used_expression_operands) = self . some_used_expression_operands . as_ref ( ) {
421532 used_expression_operands. contains_key ( & expression. as_operand_id ( ) )
422533 } else {
0 commit comments