| <!DOCTYPE HTML> |
| <html lang="en" class="light sidebar-visible" dir="ltr"> |
| <head> |
| <!-- Book generated using mdBook --> |
| <meta charset="UTF-8"> |
| <title>Debugging bootstrap - Rust Compiler Development Guide</title> |
| |
| |
| <!-- Custom HTML head --> |
| |
| <meta name="description" content="A guide to developing the Rust compiler (rustc)"> |
| <meta name="viewport" content="width=device-width, initial-scale=1"> |
| <meta name="theme-color" content="#ffffff"> |
| |
| <link rel="icon" href="../../favicon.svg"> |
| <link rel="shortcut icon" href="../../favicon.png"> |
| <link rel="stylesheet" href="../../css/variables.css"> |
| <link rel="stylesheet" href="../../css/general.css"> |
| <link rel="stylesheet" href="../../css/chrome.css"> |
| <link rel="stylesheet" href="../../css/print.css" media="print"> |
| |
| <!-- Fonts --> |
| <link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css"> |
| <link rel="stylesheet" href="../../fonts/fonts.css"> |
| |
| <!-- Highlight.js Stylesheets --> |
| <link rel="stylesheet" id="highlight-css" href="../../highlight.css"> |
| <link rel="stylesheet" id="tomorrow-night-css" href="../../tomorrow-night.css"> |
| <link rel="stylesheet" id="ayu-highlight-css" href="../../ayu-highlight.css"> |
| |
| <!-- Custom theme stylesheets --> |
| <link rel="stylesheet" href="../../pagetoc.css"> |
| |
| |
| <!-- Provide site root and default themes to javascript --> |
| <script> |
| const path_to_root = "../../"; |
| const default_light_theme = "light"; |
| const default_dark_theme = "navy"; |
| window.path_to_searchindex_js = "../../searchindex.js"; |
| </script> |
| <!-- Start loading toc.js asap --> |
| <script src="../../toc.js"></script> |
| </head> |
| <body> |
| <div id="mdbook-help-container"> |
| <div id="mdbook-help-popup"> |
| <h2 class="mdbook-help-title">Keyboard shortcuts</h2> |
| <div> |
| <p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p> |
| <p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p> |
| <p>Press <kbd>?</kbd> to show this help</p> |
| <p>Press <kbd>Esc</kbd> to hide this help</p> |
| </div> |
| </div> |
| </div> |
| <div id="body-container"> |
| <!-- Work around some values being stored in localStorage wrapped in quotes --> |
| <script> |
| try { |
| let theme = localStorage.getItem('mdbook-theme'); |
| let sidebar = localStorage.getItem('mdbook-sidebar'); |
| |
| if (theme.startsWith('"') && theme.endsWith('"')) { |
| localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1)); |
| } |
| |
| if (sidebar.startsWith('"') && sidebar.endsWith('"')) { |
| localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1)); |
| } |
| } catch (e) { } |
| </script> |
| |
| <!-- Set the theme before any content is loaded, prevents flash --> |
| <script> |
| const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme; |
| let theme; |
| try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } |
| if (theme === null || theme === undefined) { theme = default_theme; } |
| const html = document.documentElement; |
| html.classList.remove('light') |
| html.classList.add(theme); |
| html.classList.add("js"); |
| </script> |
| |
| <input type="checkbox" id="sidebar-toggle-anchor" class="hidden"> |
| |
| <!-- Hide / unhide sidebar before it is displayed --> |
| <script> |
| let sidebar = null; |
| const sidebar_toggle = document.getElementById("sidebar-toggle-anchor"); |
| if (document.body.clientWidth >= 1080) { |
| try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { } |
| sidebar = sidebar || 'visible'; |
| } else { |
| sidebar = 'hidden'; |
| sidebar_toggle.checked = false; |
| } |
| if (sidebar === 'visible') { |
| sidebar_toggle.checked = true; |
| } else { |
| html.classList.remove('sidebar-visible'); |
| } |
| </script> |
| |
| <nav id="sidebar" class="sidebar" aria-label="Table of contents"> |
| <!-- populated by js --> |
| <mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox> |
| <noscript> |
| <iframe class="sidebar-iframe-outer" src="../../toc.html"></iframe> |
| </noscript> |
| <div id="sidebar-resize-handle" class="sidebar-resize-handle"> |
| <div class="sidebar-resize-indicator"></div> |
| </div> |
| </nav> |
| |
| <div id="page-wrapper" class="page-wrapper"> |
| |
| <div class="page"> |
| <div id="menu-bar-hover-placeholder"></div> |
| <div id="menu-bar" class="menu-bar sticky"> |
| <div class="left-buttons"> |
| <label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar"> |
| <i class="fa fa-bars"></i> |
| </label> |
| <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list"> |
| <i class="fa fa-paint-brush"></i> |
| </button> |
| <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu"> |
| <li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="light">Light</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li> |
| </ul> |
| <button id="search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="searchbar"> |
| <i class="fa fa-search"></i> |
| </button> |
| </div> |
| |
| <h1 class="menu-title">Rust Compiler Development Guide</h1> |
| |
| <div class="right-buttons"> |
| <a href="../../print.html" title="Print this book" aria-label="Print this book"> |
| <i id="print-button" class="fa fa-print"></i> |
| </a> |
| <a href="https://github.com/rust-lang/rustc-dev-guide" title="Git repository" aria-label="Git repository"> |
| <i id="git-repository-button" class="fa fa-github"></i> |
| </a> |
| <a href="https://github.com/rust-lang/rustc-dev-guide/edit/master/src/building/bootstrapping/debugging-bootstrap.md" title="Suggest an edit" aria-label="Suggest an edit" rel="edit"> |
| <i id="git-edit-button" class="fa fa-edit"></i> |
| </a> |
| |
| </div> |
| </div> |
| |
| <div id="search-wrapper" class="hidden"> |
| <form id="searchbar-outer" class="searchbar-outer"> |
| <div class="search-wrapper"> |
| <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header"> |
| <div class="spinner-wrapper"> |
| <i class="fa fa-spinner fa-spin"></i> |
| </div> |
| </div> |
| </form> |
| <div id="searchresults-outer" class="searchresults-outer hidden"> |
| <div id="searchresults-header" class="searchresults-header"></div> |
| <ul id="searchresults"> |
| </ul> |
| </div> |
| </div> |
| |
| <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM --> |
| <script> |
| document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible'); |
| document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible'); |
| Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) { |
| link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1); |
| }); |
| </script> |
| |
| <div id="content" class="content"> |
| <main> |
| <h1 id="debugging-bootstrap"><a class="header" href="#debugging-bootstrap">Debugging bootstrap</a></h1> |
| <p>There are two main ways of debugging (and profiling bootstrap). The first is through println logging, and the second is through the <code>tracing</code> feature.</p> |
| <h2 id="println-logging"><a class="header" href="#println-logging"><code>println</code> logging</a></h2> |
| <p>Bootstrap has extensive unstructured logging. Most of it is gated behind the <code>--verbose</code> flag (pass <code>-vv</code> for even more detail).</p> |
| <p>If you want to see verbose output of executed Cargo commands and other kinds of detailed logs, pass <code>-v</code> or <code>-vv</code> when invoking bootstrap. Note that the logs are unstructured and may be overwhelming.</p> |
| <pre><code>$ ./x dist rustc --dry-run -vv |
| learning about cargo |
| running: RUSTC_BOOTSTRAP="1" "/home/jyn/src/rust2/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "metadata" "--format-version" "1" "--no-deps" "--manifest-path" "/home/jyn/src/rust2/Cargo.toml" (failure_mode=Exit) (created at src/bootstrap/src/core/metadata.rs:81:25, executed at src/bootstrap/src/core/metadata.rs:92:50) |
| running: RUSTC_BOOTSTRAP="1" "/home/jyn/src/rust2/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "metadata" "--format-version" "1" "--no-deps" "--manifest-path" "/home/jyn/src/rust2/library/Cargo.toml" (failure_mode=Exit) (created at src/bootstrap/src/core/metadata.rs:81:25, executed at src/bootstrap/src/core/metadata.rs:92:50) |
| ... |
| </code></pre> |
| <h2 id="tracing-in-bootstrap"><a class="header" href="#tracing-in-bootstrap"><code>tracing</code> in bootstrap</a></h2> |
| <p>Bootstrap has a conditional <code>tracing</code> feature, which provides the following features:</p> |
| <ul> |
| <li>It enables structured logging using <a href="https://docs.rs/tracing/0.1.41/tracing/index.html"><code>tracing</code></a> events and spans.</li> |
| <li>It generates a <a href="https://www.chromium.org/developers/how-tos/trace-event-profiling-tool/">Chrome trace file</a> that can be used to visualize the hierarchy and durations of executed steps and commands. |
| <ul> |
| <li>You can open the generated <code>chrome-trace.json</code> file using Chrome, on the <code>chrome://tracing</code> tab, or e.g. using <a href="https://ui.perfetto.dev/">Perfetto</a>.</li> |
| </ul> |
| </li> |
| <li>It generates <a href="https://graphviz.org/doc/info/lang.html">GraphViz</a> graphs that visualize the dependencies between executed steps. |
| <ul> |
| <li>You can open the generated <code>step-graph-*.dot</code> file using e.g. <a href="https://github.com/jrfonseca/xdot.py">xdot</a> to visualize the step graph, or use e.g. <code>dot -Tsvg</code> to convert the GraphViz file to an SVG file.</li> |
| </ul> |
| </li> |
| <li>It generates a command execution summary, which shows which commands were executed, how many of their executions were cached, and what commands were the slowest to run. |
| <ul> |
| <li>The generated <code>command-stats.txt</code> file is in a simple human-readable format.</li> |
| </ul> |
| </li> |
| </ul> |
| <p>The structured logs will be written to standard error output (<code>stderr</code>), while the other outputs will be stored in files in the <code><build-dir>/bootstrap-trace/<pid></code> directory. For convenience, bootstrap will also create a symlink to the latest generated trace output directory at <code><build-dir>/bootstrap-trace/latest</code>.</p> |
| <blockquote> |
| <p>Note that if you execute bootstrap with <code>--dry-run</code>, the tracing output directory might change. Bootstrap will always print a path where the tracing output files were stored at the end of its execution.</p> |
| </blockquote> |
| <h3 id="enabling-tracing-output"><a class="header" href="#enabling-tracing-output">Enabling <code>tracing</code> output</a></h3> |
| <p>To enable the conditional <code>tracing</code> feature, run bootstrap with the <code>BOOTSTRAP_TRACING</code> environment variable.</p> |
| <pre><code class="language-bash">$ BOOTSTRAP_TRACING=trace ./x build library --stage 1 |
| </code></pre> |
| <p>Example output<sup class="footnote-reference" id="fr-unstable-1"><a href="#footnote-unstable">1</a></sup>:</p> |
| <pre><code>$ BOOTSTRAP_TRACING=trace ./x build library --stage 1 --dry-run |
| Building bootstrap |
| Finished `dev` profile [unoptimized] target(s) in 0.05s |
| 15:56:52.477 INFO > tool::LibcxxVersionTool {target: x86_64-unknown-linux-gnu} (builder/mod.rs:1715) |
| 15:56:52.575 INFO > compile::Assemble {target_compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }} (builder/mod.rs:1715) |
| 15:56:52.575 INFO > tool::Compiletest {compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu} (builder/mod.rs:1715) |
| 15:56:52.576 INFO > tool::ToolBuild {build_compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu, tool: "compiletest", path: "src/tools/compiletest", mode: ToolBootstrap, source_type: InTree, extra_features: [], allow_features: "internal_output_capture", cargo_args: [], artifact_kind: Binary} (builder/mod.rs:1715) |
| 15:56:52.576 INFO > builder::Libdir {compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu} (builder/mod.rs:1715) |
| 15:56:52.576 INFO > compile::Sysroot {compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, force_recompile: false} (builder/mod.rs:1715) |
| 15:56:52.578 INFO > compile::Assemble {target_compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }} (builder/mod.rs:1715) |
| 15:56:52.578 INFO > tool::Compiletest {compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu} (builder/mod.rs:1715) |
| 15:56:52.578 INFO > tool::ToolBuild {build_compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu, tool: "compiletest", path: "src/tools/compiletest", mode: ToolBootstrap, source_type: InTree, extra_features: [], allow_features: "internal_output_capture", cargo_args: [], artifact_kind: Binary} (builder/mod.rs:1715) |
| 15:56:52.578 INFO > builder::Libdir {compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu} (builder/mod.rs:1715) |
| 15:56:52.578 INFO > compile::Sysroot {compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, force_recompile: false} (builder/mod.rs:1715) |
| Finished `release` profile [optimized] target(s) in 0.11s |
| Tracing/profiling output has been written to <src-root>/build/bootstrap-trace/latest |
| Build completed successfully in 0:00:00 |
| </code></pre> |
| <h4 id="controlling-tracing-output"><a class="header" href="#controlling-tracing-output">Controlling tracing output</a></h4> |
| <p>The environment variable <code>BOOTSTRAP_TRACING</code> accepts a <a href="https://docs.rs/tracing-subscriber/0.3.19/tracing_subscriber/filter/struct.EnvFilter.html"><code>tracing_subscriber</code> filter</a>. If you set <code>BOOTSTRAP_TRACING=trace</code>, you will enable all logs, but that can be overwhelming. You can thus use the filter to reduce the amount of data logged.</p> |
| <p>There are two orthogonal ways to control which kind of tracing logs you want:</p> |
| <ol> |
| <li>You can specify the log <strong>level</strong>, e.g. <code>debug</code> or <code>trace</code>. |
| <ul> |
| <li>If you select a level, all events/spans with an equal or higher priority level will be shown.</li> |
| </ul> |
| </li> |
| <li>You can also control the log <strong>target</strong>, e.g. <code>bootstrap</code> or <code>bootstrap::core::config</code> or a custom target like <code>CONFIG_HANDLING</code> or <code>STEP</code>. |
| <ul> |
| <li>Custom targets are used to limit what kinds of spans you are interested in, as the <code>BOOTSTRAP_TRACING=trace</code> output can be quite verbose. Currently, you can use the following custom targets: |
| <ul> |
| <li><code>CONFIG_HANDLING</code>: show spans related to config handling.</li> |
| <li><code>STEP</code>: show all executed steps. Executed commands have <code>info</code> event level.</li> |
| <li><code>COMMAND</code>: show all executed commands. Executed commands have <code>trace</code> event level.</li> |
| <li><code>IO</code>: show performed I/O operations. Executed commands have <code>trace</code> event level. |
| <ul> |
| <li>Note that many I/O are currently not being traced.</li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| </ol> |
| <p>You can of course combine them (custom target logs are typically gated behind <code>TRACE</code> log level additionally):</p> |
| <pre><code class="language-bash">$ BOOTSTRAP_TRACING=CONFIG_HANDLING=trace,STEP=info,COMMAND=trace ./x build library --stage 1 |
| </code></pre> |
| <p>Note that the level that you specify using <code>BOOTSTRAP_TRACING</code> also has an effect on the spans that will be recorded in the Chrome trace file.</p> |
| <h5 id="fixme96176-specific-tracing-for-compiler-vs-compiler_for"><a class="header" href="#fixme96176-specific-tracing-for-compiler-vs-compiler_for">FIXME(#96176): specific tracing for <code>compiler()</code> vs <code>compiler_for()</code></a></h5> |
| <p>The additional targets <code>COMPILER</code> and <code>COMPILER_FOR</code> are used to help trace what |
| <code>builder.compiler()</code> and <code>builder.compiler_for()</code> does. They should be removed |
| if <a href="https://github.com/rust-lang/rust/issues/96176">#96176</a> is resolved.</p> |
| <h3 id="using-tracing-in-bootstrap"><a class="header" href="#using-tracing-in-bootstrap">Using <code>tracing</code> in bootstrap</a></h3> |
| <p>Both <code>tracing::*</code> macros and the <code>tracing::instrument</code> proc-macro attribute need to be gated behind <code>tracing</code> feature. Examples:</p> |
| <pre><code class="language-rs">#[cfg(feature = "tracing")] |
| use tracing::instrument; |
| |
| struct Foo; |
| |
| impl Step for Foo { |
| type Output = (); |
| |
| #[cfg_attr(feature = "tracing", instrument(level = "trace", name = "Foo::should_run", skip_all))] |
| fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
| trace!(?run, "entered Foo::should_run"); |
| |
| todo!() |
| } |
| |
| fn run(self, builder: &Builder<'_>) -> Self::Output { |
| trace!(?run, "entered Foo::run"); |
| |
| todo!() |
| } |
| } |
| </code></pre> |
| <p>For <code>#[instrument]</code>, it's recommended to:</p> |
| <ul> |
| <li>Gate it behind <code>trace</code> level for fine-granularity, possibly <code>debug</code> level for core functions.</li> |
| <li>Explicitly pick an instrumentation name via <code>name = ".."</code> to distinguish between e.g. <code>run</code> of different steps.</li> |
| <li>Take care to not cause diverging behavior via tracing, e.g. building extra things only when tracing infra is enabled.</li> |
| </ul> |
| <h3 id="rust-analyzer-integration"><a class="header" href="#rust-analyzer-integration">rust-analyzer integration?</a></h3> |
| <p>Unfortunately, because bootstrap is a <code>rust-analyzer.linkedProjects</code>, you can't ask r-a to check/build bootstrap itself with <code>tracing</code> feature enabled to get relevant completions, due to lack of support as described in <a href="https://github.com/rust-lang/rust-analyzer/issues/8521">https://github.com/rust-lang/rust-analyzer/issues/8521</a>.</p> |
| <hr> |
| <ol class="footnote-definition"><li id="footnote-unstable"> |
| <p>This output is always subject to further changes. <a href="#fr-unstable-1">↩</a></p> |
| </li> |
| </ol> |
| </main> |
| |
| <nav class="nav-wrapper" aria-label="Page navigation"> |
| <!-- Mobile navigation buttons --> |
| <a rel="prev" href="../../building/bootstrapping/writing-tools-in-bootstrap.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> |
| <i class="fa fa-angle-left"></i> |
| </a> |
| |
| <a rel="next prefetch" href="../../building/bootstrapping/bootstrap-in-dependencies.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> |
| <i class="fa fa-angle-right"></i> |
| </a> |
| |
| <div style="clear: both"></div> |
| </nav> |
| </div> |
| </div> |
| |
| <nav class="nav-wide-wrapper" aria-label="Page navigation"> |
| <a rel="prev" href="../../building/bootstrapping/writing-tools-in-bootstrap.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> |
| <i class="fa fa-angle-left"></i> |
| </a> |
| |
| <a rel="next prefetch" href="../../building/bootstrapping/bootstrap-in-dependencies.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> |
| <i class="fa fa-angle-right"></i> |
| </a> |
| </nav> |
| |
| </div> |
| |
| |
| |
| |
| <script> |
| window.playground_copyable = true; |
| </script> |
| |
| |
| <script src="../../elasticlunr.min.js"></script> |
| <script src="../../mark.min.js"></script> |
| <script src="../../searcher.js"></script> |
| |
| <script src="../../clipboard.min.js"></script> |
| <script src="../../highlight.js"></script> |
| <script src="../../book.js"></script> |
| |
| <!-- Custom JS scripts --> |
| <script src="../../mermaid.min.js"></script> |
| <script src="../../mermaid-init.js"></script> |
| <script src="../../pagetoc.js"></script> |
| |
| |
| |
| </div> |
| </body> |
| </html> |