Module rustc_mir_transform::coverage::debug
source · Expand description
The InstrumentCoverage
MIR pass implementation includes debugging tools and options
to help developers understand and/or improve the analysis and instrumentation of a MIR.
To enable coverage, include the rustc command line option:
-C instrument-coverage
MIR Dump Files, with additional CoverageGraph
graphviz and CoverageSpan
spanview
Additional debugging options include:
-
-Z dump-mir=InstrumentCoverage
- Generate.mir
files showing the state of the MIR, before and after theInstrumentCoverage
pass, for each compiled function. -
-Z dump-mir-graphviz
- If-Z dump-mir
is also enabled for the current MIR node path, each MIR dump is accompanied by a before-and-after graphical view of the MIR, in Graphviz.dot
file format (which can be visually rendered as a graph using any of a number of free Graphviz viewers and IDE extensions).For the
InstrumentCoverage
pass, this option also enables generation of an additional Graphviz.dot
file for each function, rendering theCoverageGraph
: the control flow graph (CFG) ofBasicCoverageBlocks
(BCBs), as nodes, internally labeled to show theCoverageSpan
-based MIR elements each BCB represents (BasicBlock
s,Statement
s andTerminator
s), assigned coverage counters and/or expressions, and edge counters, as needed.(Note the additional option,
-Z graphviz-dark-mode
, can be added, to change the rendered output from its default black-on-white background to a dark color theme, if desired.) -
-Z dump-mir-spanview
- If-Z dump-mir
is also enabled for the current MIR node path, each MIR dump is accompanied by a before-and-after.html
document showing the function’s original source code, highlighted by it’s MIR spans, at thestatement
-level (by default),terminator
only, or encompassing span for theTerminator
plus allStatement
s, in eachblock
(BasicBlock
).For the
InstrumentCoverage
pass, this option also enables generation of an additional spanview.html
file for each function, showing the aggregatedCoverageSpan
s that will require counters (or counter expressions) for accurate coverage analysis.
Debug Logging
The InstrumentCoverage
pass includes debug logging messages at various phases and decision
points, which can be enabled via environment variable:
RUSTC_LOG=rustc_mir_transform::transform::coverage=debug
Other module paths with coverage-related debug logs may also be of interest, particularly for debugging the coverage map data, injected as global variables in the LLVM IR (during rustc’s code generation pass). For example:
RUSTC_LOG=rustc_mir_transform::transform::coverage,rustc_codegen_ssa::coverageinfo,rustc_codegen_llvm::coverageinfo=debug
Coverage Debug Options
Additional debugging options can be enabled using the environment variable:
RUSTC_COVERAGE_DEBUG_OPTIONS=<options>
These options are comma-separated, and specified in the format option-name=value
. For example:
$ RUSTC_COVERAGE_DEBUG_OPTIONS=counter-format=id+operation,allow-unused-expressions=yes cargo build
Coverage debug options include:
-
allow-unused-expressions=yes
orno
(default:no
)The
InstrumentCoverage
algorithms should only create and assign expressions to aBasicCoverageBlock
, or an incoming edge, if that expression is either (a) required to count aCoverageSpan
, or (b) a dependency of some other required counter expression.If an expression is generated that does not map to a
CoverageSpan
or dependency, this probably indicates there was a bug in the algorithm that creates and assigns counters and expressions.When this kind of bug is encountered, the rustc compiler will panic by default. Setting:
allow-unused-expressions=yes
will log a warning message instead of panicking (effectively ignoring the unused expressions), which may be helpful when debugging the root cause of the problem. -
counter-format=<choices>
, where<choices>
can be any plus-separated combination ofid
,block
, and/oroperation
(default:block+operation
)This option effects both the
CoverageGraph
(graphviz.dot
files) and debug logging, when generating labels for counters and expressions.Depending on the values and combinations, counters can be labeled by:
id
- counter or expression ID (ascending counter IDs, starting at 1, or descending expression IDs, starting atu32:MAX
)block
- theBasicCoverageBlock
label (for example,bcb0
) or edge label (for examplebcb0->bcb1
), for counters or expressions assigned to count aBasicCoverageBlock
or edge. Intermediate expressions (not directly associated with a BCB or edge) will be labeled by their expression ID, unlessoperation
is also specified.operation
- applied to expressions only, labels include the left-hand-side counter or expression label (lhs operand), the operator (+
or-
), and the right-hand-side counter or expression (rhs operand). Expression operand labels are generated recursively, generating labels with nested operations, enclosed in parentheses (for example:bcb2 + (bcb0 - bcb1)
).
Structs
DebugCounters
.CoverageKind
IDs (as ExpressionOperandId
) to
the CoverageKind
data and optional label (normally, the counter’s associated
BasicCoverageBlock
format string, if any).CoverageGraph
, for debugging purposes.CoverageSpan
s, and any that are
not used are retained in the unused_expressions
Vec, to be included in debug output (logs
and/or a CoverageGraph
graphviz output).Constants
Functions
CoverageSpan
-specific spanview dump file.BasicCoverageBlockData
s into SpanViewable
s.TerminatorKind
variant, independent of any
values it might hold.