One Hat Cyber Team
Your IP :
216.73.216.115
Server IP :
194.44.31.54
Server :
Linux zen.imath.kiev.ua 4.18.0-553.77.1.el8_10.x86_64 #1 SMP Fri Oct 3 14:30:23 UTC 2025 x86_64
Server Software :
Apache/2.4.37 (Rocky Linux) OpenSSL/1.1.1k
PHP Version :
5.6.40
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
usr
/
share
/
doc
/
qemu-kvm
/
devel
/
View File Name :
style.html
<!DOCTYPE html> <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]--> <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]--> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>QEMU Coding Style — QEMU qemu-kvm-6.2.0-53.module+el8.10.0+2055+8eb7870b.4 documentation</title> <link rel="shortcut icon" href="../_static/qemu_32x32.png"/> <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="next" title="QEMU and Kconfig" href="kconfig.html" /> <link rel="prev" title="The QEMU build system architecture" href="build-system.html" /> <script src="../_static/js/modernizr.min.js"></script> </head> <body class="wy-body-for-nav"> <div class="wy-grid-for-nav"> <nav data-toggle="wy-nav-shift" class="wy-nav-side"> <div class="wy-side-scroll"> <div class="wy-side-nav-search"> <a href="../index.html" class="icon icon-home"> QEMU <img src="../_static/qemu_128x128.png" class="logo" alt="Logo"/> </a> <div class="version"> 6.2.0 </div> <div role="search"> <form id="rtd-search-form" class="wy-form" action="../search.html" method="get"> <input type="text" name="q" placeholder="Search docs" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation"> <p class="caption"><span class="caption-text">Contents:</span></p> <ul class="current"> <li class="toctree-l1"><a class="reference internal" href="../about/index.html">About QEMU</a></li> <li class="toctree-l1"><a class="reference internal" href="../system/index.html">System Emulation</a></li> <li class="toctree-l1"><a class="reference internal" href="../user/index.html">User Mode Emulation</a></li> <li class="toctree-l1"><a class="reference internal" href="../tools/index.html">Tools</a></li> <li class="toctree-l1"><a class="reference internal" href="../interop/index.html">System Emulation Management and Interoperability</a></li> <li class="toctree-l1"><a class="reference internal" href="../specs/index.html">System Emulation Guest Hardware Specifications</a></li> <li class="toctree-l1 current"><a class="reference internal" href="index.html">Developer Information</a><ul class="current"> <li class="toctree-l2"><a class="reference internal" href="code-of-conduct.html">Code of Conduct</a></li> <li class="toctree-l2"><a class="reference internal" href="conflict-resolution.html">Conflict Resolution Policy</a></li> <li class="toctree-l2"><a class="reference internal" href="build-system.html">The QEMU build system architecture</a></li> <li class="toctree-l2 current"><a class="current reference internal" href="#">QEMU Coding Style</a><ul> <li class="toctree-l3"><a class="reference internal" href="#formatting-and-style">Formatting and style</a><ul> <li class="toctree-l4"><a class="reference internal" href="#whitespace">Whitespace</a></li> <li class="toctree-l4"><a class="reference internal" href="#line-width">Line width</a></li> <li class="toctree-l4"><a class="reference internal" href="#naming">Naming</a></li> <li class="toctree-l4"><a class="reference internal" href="#block-structure">Block structure</a></li> <li class="toctree-l4"><a class="reference internal" href="#declarations">Declarations</a></li> <li class="toctree-l4"><a class="reference internal" href="#conditional-statements">Conditional statements</a></li> <li class="toctree-l4"><a class="reference internal" href="#comment-style">Comment style</a></li> </ul> </li> <li class="toctree-l3"><a class="reference internal" href="#language-usage">Language usage</a><ul> <li class="toctree-l4"><a class="reference internal" href="#preprocessor">Preprocessor</a></li> <li class="toctree-l4"><a class="reference internal" href="#c-types">C types</a></li> <li class="toctree-l4"><a class="reference internal" href="#low-level-memory-management">Low level memory management</a></li> <li class="toctree-l4"><a class="reference internal" href="#string-manipulation">String manipulation</a></li> <li class="toctree-l4"><a class="reference internal" href="#printf-style-functions">Printf-style functions</a></li> <li class="toctree-l4"><a class="reference internal" href="#c-standard-implementation-defined-and-undefined-behaviors">C standard, implementation defined and undefined behaviors</a></li> <li class="toctree-l4"><a class="reference internal" href="#automatic-memory-deallocation">Automatic memory deallocation</a></li> </ul> </li> <li class="toctree-l3"><a class="reference internal" href="#qemu-specific-idioms">QEMU Specific Idioms</a><ul> <li class="toctree-l4"><a class="reference internal" href="#error-handling-and-reporting">Error handling and reporting</a></li> <li class="toctree-l4"><a class="reference internal" href="#trace-events-style">trace-events style</a></li> </ul> </li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="kconfig.html">QEMU and Kconfig</a></li> <li class="toctree-l2"><a class="reference internal" href="testing.html">Testing in QEMU</a></li> <li class="toctree-l2"><a class="reference internal" href="fuzzing.html">Fuzzing</a></li> <li class="toctree-l2"><a class="reference internal" href="control-flow-integrity.html">Control-Flow Integrity (CFI)</a></li> <li class="toctree-l2"><a class="reference internal" href="loads-stores.html">Load and Store APIs</a></li> <li class="toctree-l2"><a class="reference internal" href="memory.html">The memory API</a></li> <li class="toctree-l2"><a class="reference internal" href="migration.html">Migration</a></li> <li class="toctree-l2"><a class="reference internal" href="atomics.html">Atomic operations in QEMU</a></li> <li class="toctree-l2"><a class="reference internal" href="stable-process.html">QEMU and the stable process</a></li> <li class="toctree-l2"><a class="reference internal" href="ci.html">CI</a></li> <li class="toctree-l2"><a class="reference internal" href="qtest.html">QTest Device Emulation Testing Framework</a></li> <li class="toctree-l2"><a class="reference internal" href="decodetree.html">Decodetree Specification</a></li> <li class="toctree-l2"><a class="reference internal" href="secure-coding-practices.html">Secure Coding Practices</a></li> <li class="toctree-l2"><a class="reference internal" href="tcg.html">Translator Internals</a></li> <li class="toctree-l2"><a class="reference internal" href="tcg-icount.html">TCG Instruction Counting</a></li> <li class="toctree-l2"><a class="reference internal" href="tracing.html">Tracing</a></li> <li class="toctree-l2"><a class="reference internal" href="multi-thread-tcg.html">Multi-threaded TCG</a></li> <li class="toctree-l2"><a class="reference internal" href="tcg-plugins.html">QEMU TCG Plugins</a></li> <li class="toctree-l2"><a class="reference internal" href="bitops.html">Bitwise operations</a></li> <li class="toctree-l2"><a class="reference internal" href="ui.html">QEMU UI subsystem</a></li> <li class="toctree-l2"><a class="reference internal" href="reset.html">Reset in QEMU: the Resettable interface</a></li> <li class="toctree-l2"><a class="reference internal" href="s390-dasd-ipl.html">Booting from real channel-attached devices on s390x</a></li> <li class="toctree-l2"><a class="reference internal" href="clocks.html">Modelling a clock tree in QEMU</a></li> <li class="toctree-l2"><a class="reference internal" href="qom.html">The QEMU Object Model (QOM)</a></li> <li class="toctree-l2"><a class="reference internal" href="modules.html">QEMU modules</a></li> <li class="toctree-l2"><a class="reference internal" href="block-coroutine-wrapper.html">block-coroutine-wrapper</a></li> <li class="toctree-l2"><a class="reference internal" href="multi-process.html">Multi-process QEMU</a></li> <li class="toctree-l2"><a class="reference internal" href="ebpf_rss.html">eBPF RSS virtio-net support</a></li> <li class="toctree-l2"><a class="reference internal" href="vfio-migration.html">VFIO device Migration</a></li> <li class="toctree-l2"><a class="reference internal" href="qapi-code-gen.html">How to use the QAPI code generator</a></li> <li class="toctree-l2"><a class="reference internal" href="writing-monitor-commands.html">How to write monitor commands</a></li> <li class="toctree-l2"><a class="reference internal" href="trivial-patches.html">Trivial Patches</a></li> <li class="toctree-l2"><a class="reference internal" href="submitting-a-patch.html">Submitting a Patch</a></li> <li class="toctree-l2"><a class="reference internal" href="submitting-a-pull-request.html">Submitting a Pull Request</a></li> </ul> </li> </ul> </div> </div> </nav> <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> <nav class="wy-nav-top" aria-label="top navigation"> <i data-toggle="wy-nav-top" class="fa fa-bars"></i> <a href="../index.html">QEMU</a> </nav> <div class="wy-nav-content"> <div class="rst-content"> <div role="navigation" aria-label="breadcrumbs navigation"> <ul class="wy-breadcrumbs"> <li><a href="../index.html">Docs</a> »</li> <li><a href="index.html">Developer Information</a> »</li> <li>QEMU Coding Style</li> <li class="wy-breadcrumbs-aside"> <a href="https://gitlab.com/qemu-project/qemu/blob/master/docs/devel/style.rst" class="fa fa-gitlab"> Edit on GitLab</a> </li> </ul> <hr/> </div> <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> <div itemprop="articleBody"> <div class="section" id="qemu-coding-style"> <span id="coding-style"></span><h1><a class="toc-backref" href="#id1">QEMU Coding Style</a><a class="headerlink" href="#qemu-coding-style" title="Permalink to this headline">¶</a></h1> <div class="contents topic" id="table-of-contents"> <p class="topic-title first">Table of Contents</p> <ul class="simple"> <li><a class="reference internal" href="#qemu-coding-style" id="id1">QEMU Coding Style</a><ul> <li><a class="reference internal" href="#formatting-and-style" id="id2">Formatting and style</a><ul> <li><a class="reference internal" href="#whitespace" id="id3">Whitespace</a><ul> <li><a class="reference internal" href="#multiline-indent" id="id4">Multiline Indent</a></li> </ul> </li> <li><a class="reference internal" href="#line-width" id="id5">Line width</a></li> <li><a class="reference internal" href="#naming" id="id6">Naming</a><ul> <li><a class="reference internal" href="#variable-naming-conventions" id="id7">Variable Naming Conventions</a></li> <li><a class="reference internal" href="#function-naming-conventions" id="id8">Function Naming Conventions</a></li> </ul> </li> <li><a class="reference internal" href="#block-structure" id="id9">Block structure</a></li> <li><a class="reference internal" href="#declarations" id="id10">Declarations</a></li> <li><a class="reference internal" href="#conditional-statements" id="id11">Conditional statements</a></li> <li><a class="reference internal" href="#comment-style" id="id12">Comment style</a></li> </ul> </li> <li><a class="reference internal" href="#language-usage" id="id13">Language usage</a><ul> <li><a class="reference internal" href="#preprocessor" id="id14">Preprocessor</a><ul> <li><a class="reference internal" href="#variadic-macros" id="id15">Variadic macros</a></li> <li><a class="reference internal" href="#include-directives" id="id16">Include directives</a></li> </ul> </li> <li><a class="reference internal" href="#c-types" id="id17">C types</a><ul> <li><a class="reference internal" href="#scalars" id="id18">Scalars</a></li> <li><a class="reference internal" href="#pointers" id="id19">Pointers</a></li> <li><a class="reference internal" href="#typedefs" id="id20">Typedefs</a></li> <li><a class="reference internal" href="#reserved-namespaces-in-c-and-posix" id="id21">Reserved namespaces in C and POSIX</a></li> </ul> </li> <li><a class="reference internal" href="#low-level-memory-management" id="id22">Low level memory management</a></li> <li><a class="reference internal" href="#string-manipulation" id="id23">String manipulation</a></li> <li><a class="reference internal" href="#printf-style-functions" id="id24">Printf-style functions</a></li> <li><a class="reference internal" href="#c-standard-implementation-defined-and-undefined-behaviors" id="id25">C standard, implementation defined and undefined behaviors</a></li> <li><a class="reference internal" href="#automatic-memory-deallocation" id="id26">Automatic memory deallocation</a></li> </ul> </li> <li><a class="reference internal" href="#qemu-specific-idioms" id="id27">QEMU Specific Idioms</a><ul> <li><a class="reference internal" href="#error-handling-and-reporting" id="id28">Error handling and reporting</a><ul> <li><a class="reference internal" href="#reporting-errors-to-the-human-user" id="id29">Reporting errors to the human user</a></li> <li><a class="reference internal" href="#propagating-errors" id="id30">Propagating errors</a></li> <li><a class="reference internal" href="#handling-errors" id="id31">Handling errors</a></li> </ul> </li> <li><a class="reference internal" href="#trace-events-style" id="id32">trace-events style</a><ul> <li><a class="reference internal" href="#x-prefix" id="id33">0x prefix</a></li> <li><a class="reference internal" href="#printf-flag" id="id34">‘#’ printf flag</a></li> </ul> </li> </ul> </li> </ul> </li> </ul> </div> <p>Please use the script checkpatch.pl in the scripts directory to check patches before submitting.</p> <div class="section" id="formatting-and-style"> <h2><a class="toc-backref" href="#id2">Formatting and style</a><a class="headerlink" href="#formatting-and-style" title="Permalink to this headline">¶</a></h2> <div class="section" id="whitespace"> <h3><a class="toc-backref" href="#id3">Whitespace</a><a class="headerlink" href="#whitespace" title="Permalink to this headline">¶</a></h3> <p>Of course, the most important aspect in any coding style is whitespace. Crusty old coders who have trouble spotting the glasses on their noses can tell the difference between a tab and eight spaces from a distance of approximately fifteen parsecs. Many a flamewar has been fought and lost on this issue.</p> <p>QEMU indents are four spaces. Tabs are never used, except in Makefiles where they have been irreversibly coded into the syntax. Spaces of course are superior to tabs because:</p> <ul class="simple"> <li>You have just one way to specify whitespace, not two. Ambiguity breeds mistakes.</li> <li>The confusion surrounding ‘use tabs to indent, spaces to justify’ is gone.</li> <li>Tab indents push your code to the right, making your screen seriously unbalanced.</li> <li>Tabs will be rendered incorrectly on editors who are misconfigured not to use tab stops of eight positions.</li> <li>Tabs are rendered badly in patches, causing off-by-one errors in almost every line.</li> <li>It is the QEMU coding style.</li> </ul> <p>Do not leave whitespace dangling off the ends of lines.</p> <div class="section" id="multiline-indent"> <h4><a class="toc-backref" href="#id4">Multiline Indent</a><a class="headerlink" href="#multiline-indent" title="Permalink to this headline">¶</a></h4> <p>There are several places where indent is necessary:</p> <ul class="simple"> <li>if/else</li> <li>while/for</li> <li>function definition & call</li> </ul> <p>When breaking up a long line to fit within line width, we need a proper indent for the following lines.</p> <p>In case of if/else, while/for, align the secondary lines just after the opening parenthesis of the first.</p> <p>For example:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="mi">1</span> <span class="o">&&</span> <span class="n">b</span> <span class="o">==</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span> <span class="k">while</span> <span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="mi">1</span> <span class="o">&&</span> <span class="n">b</span> <span class="o">==</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span> </pre></div> </div> <p>In case of function, there are several variants:</p> <ul class="simple"> <li>4 spaces indent from the beginning</li> <li>align the secondary lines just after the opening parenthesis of the first</li> </ul> <p>For example:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">do_something</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">);</span> <span class="n">do_something</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">);</span> <span class="n">do_something</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">do_another</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">));</span> </pre></div> </div> </div> </div> <div class="section" id="line-width"> <h3><a class="toc-backref" href="#id5">Line width</a><a class="headerlink" href="#line-width" title="Permalink to this headline">¶</a></h3> <p>Lines should be 80 characters; try not to make them longer.</p> <p>Sometimes it is hard to do, especially when dealing with QEMU subsystems that use long function or symbol names. If wrapping the line at 80 columns is obviously less readable and more awkward, prefer not to wrap it; better to have an 85 character line than one which is awkwardly wrapped.</p> <p>Even in that case, try not to make lines much longer than 80 characters. (The checkpatch script will warn at 100 characters, but this is intended as a guard against obviously-overlength lines, not a target.)</p> <p>Rationale:</p> <ul class="simple"> <li>Some people like to tile their 24” screens with a 6x4 matrix of 80x24 xterms and use vi in all of them. The best way to punish them is to let them keep doing it.</li> <li>Code and especially patches is much more readable if limited to a sane line length. Eighty is traditional.</li> <li>The four-space indentation makes the most common excuse (“But look at all that white space on the left!”) moot.</li> <li>It is the QEMU coding style.</li> </ul> </div> <div class="section" id="naming"> <h3><a class="toc-backref" href="#id6">Naming</a><a class="headerlink" href="#naming" title="Permalink to this headline">¶</a></h3> <p>Variables are lower_case_with_underscores; easy to type and read. Structured type names are in CamelCase; harder to type but standing out. Enum type names and function type names should also be in CamelCase. Scalar type names are lower_case_with_underscores_ending_with_a_t, like the POSIX uint64_t and family. Note that this last convention contradicts POSIX and is therefore likely to be changed.</p> <div class="section" id="variable-naming-conventions"> <h4><a class="toc-backref" href="#id7">Variable Naming Conventions</a><a class="headerlink" href="#variable-naming-conventions" title="Permalink to this headline">¶</a></h4> <p>A number of short naming conventions exist for variables that use common QEMU types. For example, the architecture independent CPUState is often held as a <code class="docutils literal notranslate"><span class="pre">cs</span></code> pointer variable, whereas the concrete CPUArchState is usually held in a pointer called <code class="docutils literal notranslate"><span class="pre">env</span></code>.</p> <p>Likewise, in device emulation code the common DeviceState is usually called <code class="docutils literal notranslate"><span class="pre">dev</span></code>.</p> </div> <div class="section" id="function-naming-conventions"> <h4><a class="toc-backref" href="#id8">Function Naming Conventions</a><a class="headerlink" href="#function-naming-conventions" title="Permalink to this headline">¶</a></h4> <p>Wrapped version of standard library or GLib functions use a <code class="docutils literal notranslate"><span class="pre">qemu_</span></code> prefix to alert readers that they are seeing a wrapped version, for example <code class="docutils literal notranslate"><span class="pre">qemu_strtol</span></code> or <code class="docutils literal notranslate"><span class="pre">qemu_mutex_lock</span></code>. Other utility functions that are widely called from across the codebase should not have any prefix, for example <code class="docutils literal notranslate"><span class="pre">pstrcpy</span></code> or bit manipulation functions such as <code class="docutils literal notranslate"><span class="pre">find_first_bit</span></code>.</p> <p>The <code class="docutils literal notranslate"><span class="pre">qemu_</span></code> prefix is also used for functions that modify global emulator state, for example <code class="docutils literal notranslate"><span class="pre">qemu_add_vm_change_state_handler</span></code>. However, if there is an obvious subsystem-specific prefix it should be used instead.</p> <p>Public functions from a file or subsystem (declared in headers) tend to have a consistent prefix to show where they came from. For example, <code class="docutils literal notranslate"><span class="pre">tlb_</span></code> for functions from <code class="docutils literal notranslate"><span class="pre">cputlb.c</span></code> or <code class="docutils literal notranslate"><span class="pre">cpu_</span></code> for functions from cpus.c.</p> <p>If there are two versions of a function to be called with or without a lock held, the function that expects the lock to be already held usually uses the suffix <code class="docutils literal notranslate"><span class="pre">_locked</span></code>.</p> </div> </div> <div class="section" id="block-structure"> <h3><a class="toc-backref" href="#id9">Block structure</a><a class="headerlink" href="#block-structure" title="Permalink to this headline">¶</a></h3> <p>Every indented statement is braced; even if the block contains just one statement. The opening brace is on the line that contains the control flow statement that introduces the new block; the closing brace is on the same line as the else keyword, or on a line by itself if there is no else keyword. Example:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="mi">5</span><span class="p">)</span> <span class="p">{</span> <span class="n">printf</span><span class="p">(</span><span class="s">"a was 5.</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="mi">6</span><span class="p">)</span> <span class="p">{</span> <span class="n">printf</span><span class="p">(</span><span class="s">"a was 6.</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">printf</span><span class="p">(</span><span class="s">"a was something else entirely.</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>Note that ‘else if’ is considered a single statement; otherwise a long if/ else if/else if/…/else sequence would need an indent for every else statement.</p> <p>An exception is the opening brace for a function; for reasons of tradition and clarity it comes on a line by itself:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span> <span class="nf">a_function</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">do_something</span><span class="p">();</span> <span class="p">}</span> </pre></div> </div> <p>Rationale: a consistent (except for functions…) bracing style reduces ambiguity and avoids needless churn when lines are added or removed. Furthermore, it is the QEMU coding style.</p> </div> <div class="section" id="declarations"> <h3><a class="toc-backref" href="#id10">Declarations</a><a class="headerlink" href="#declarations" title="Permalink to this headline">¶</a></h3> <p>Mixed declarations (interleaving statements and declarations within blocks) are generally not allowed; declarations should be at the beginning of blocks.</p> <p>Every now and then, an exception is made for declarations inside a #ifdef or #ifndef block: if the code looks nicer, such declarations can be placed at the top of the block even if there are statements above. On the other hand, however, it’s often best to move that #ifdef/#ifndef block to a separate function altogether.</p> </div> <div class="section" id="conditional-statements"> <h3><a class="toc-backref" href="#id11">Conditional statements</a><a class="headerlink" href="#conditional-statements" title="Permalink to this headline">¶</a></h3> <p>When comparing a variable for (in)equality with a constant, list the constant on the right, as in:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="cm">/* Reads like: "If a equals 1" */</span> <span class="n">do_something</span><span class="p">();</span> <span class="p">}</span> </pre></div> </div> <p>Rationale: Yoda conditions (as in ‘if (1 == a)’) are awkward to read. Besides, good compilers already warn users when ‘==’ is mis-typed as ‘=’, even when the constant is on the right.</p> </div> <div class="section" id="comment-style"> <h3><a class="toc-backref" href="#id12">Comment style</a><a class="headerlink" href="#comment-style" title="Permalink to this headline">¶</a></h3> <p>We use traditional C-style /<code class="docutils literal notranslate"><span class="pre">*</span></code> <code class="docutils literal notranslate"><span class="pre">*</span></code>/ comments and avoid // comments.</p> <p>Rationale: The // form is valid in C99, so this is purely a matter of consistency of style. The checkpatch script will warn you about this.</p> <p>Multiline comment blocks should have a row of stars on the left, and the initial /<code class="docutils literal notranslate"><span class="pre">*</span></code> and terminating <code class="docutils literal notranslate"><span class="pre">*</span></code>/ both on their own lines:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*</span> <span class="cm"> * like</span> <span class="cm"> * this</span> <span class="cm"> */</span> </pre></div> </div> <p>This is the same format required by the Linux kernel coding style.</p> <p>(Some of the existing comments in the codebase use the GNU Coding Standards form which does not have stars on the left, or other variations; avoid these when writing new comments, but don’t worry about converting to the preferred form unless you’re editing that comment anyway.)</p> <p>Rationale: Consistency, and ease of visually picking out a multiline comment from the surrounding code.</p> </div> </div> <div class="section" id="language-usage"> <h2><a class="toc-backref" href="#id13">Language usage</a><a class="headerlink" href="#language-usage" title="Permalink to this headline">¶</a></h2> <div class="section" id="preprocessor"> <h3><a class="toc-backref" href="#id14">Preprocessor</a><a class="headerlink" href="#preprocessor" title="Permalink to this headline">¶</a></h3> <div class="section" id="variadic-macros"> <h4><a class="toc-backref" href="#id15">Variadic macros</a><a class="headerlink" href="#variadic-macros" title="Permalink to this headline">¶</a></h4> <p>For variadic macros, stick with this C99-like syntax:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define DPRINTF(fmt, ...) \</span> <span class="cp"> do { printf("IRQ: " fmt, ## __VA_ARGS__); } while (0)</span> </pre></div> </div> </div> <div class="section" id="include-directives"> <h4><a class="toc-backref" href="#id16">Include directives</a><a class="headerlink" href="#include-directives" title="Permalink to this headline">¶</a></h4> <p>Order include directives as follows:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">"qemu/osdep.h" /* Always first... */</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf"><...> /* then system headers... */</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">"..." /* and finally QEMU headers. */</span><span class="cp"></span> </pre></div> </div> <p>The “qemu/osdep.h” header contains preprocessor macros that affect the behavior of core system headers like <stdint.h>. It must be the first include so that core system headers included by external libraries get the preprocessor macros that QEMU depends on.</p> <p>Do not include “qemu/osdep.h” from header files since the .c file will have already included it.</p> </div> </div> <div class="section" id="c-types"> <h3><a class="toc-backref" href="#id17">C types</a><a class="headerlink" href="#c-types" title="Permalink to this headline">¶</a></h3> <p>It should be common sense to use the right type, but we have collected a few useful guidelines here.</p> <div class="section" id="scalars"> <h4><a class="toc-backref" href="#id18">Scalars</a><a class="headerlink" href="#scalars" title="Permalink to this headline">¶</a></h4> <p>If you’re using “int” or “long”, odds are good that there’s a better type. If a variable is counting something, it should be declared with an unsigned type.</p> <p>If it’s host memory-size related, size_t should be a good choice (use ssize_t only if required). Guest RAM memory offsets must use ram_addr_t, but only for RAM, it may not cover whole guest address space.</p> <p>If it’s file-size related, use off_t. If it’s file-offset related (i.e., signed), use off_t. If it’s just counting small numbers use “unsigned int”; (on all but oddball embedded systems, you can assume that that type is at least four bytes wide).</p> <p>In the event that you require a specific width, use a standard type like int32_t, uint32_t, uint64_t, etc. The specific types are mandatory for VMState fields.</p> <p>Don’t use Linux kernel internal types like u32, __u32 or __le32.</p> <p>Use hwaddr for guest physical addresses except pcibus_t for PCI addresses. In addition, ram_addr_t is a QEMU internal address space that maps guest RAM physical addresses into an intermediate address space that can map to host virtual address spaces. Generally speaking, the size of guest memory can always fit into ram_addr_t but it would not be correct to store an actual guest physical address in a ram_addr_t.</p> <p>For CPU virtual addresses there are several possible types. vaddr is the best type to use to hold a CPU virtual address in target-independent code. It is guaranteed to be large enough to hold a virtual address for any target, and it does not change size from target to target. It is always unsigned. target_ulong is a type the size of a virtual address on the CPU; this means it may be 32 or 64 bits depending on which target is being built. It should therefore be used only in target-specific code, and in some performance-critical built-per-target core code such as the TLB code. There is also a signed version, target_long. abi_ulong is for the <code class="docutils literal notranslate"><span class="pre">*</span></code>-user targets, and represents a type the size of ‘void <code class="docutils literal notranslate"><span class="pre">*</span></code>’ in that target’s ABI. (This may not be the same as the size of a full CPU virtual address in the case of target ABIs which use 32 bit pointers on 64 bit CPUs, like sparc32plus.) Definitions of structures that must match the target’s ABI must use this type for anything that on the target is defined to be an ‘unsigned long’ or a pointer type. There is also a signed version, abi_long.</p> <p>Of course, take all of the above with a grain of salt. If you’re about to use some system interface that requires a type like size_t, pid_t or off_t, use matching types for any corresponding variables.</p> <p>Also, if you try to use e.g., “unsigned int” as a type, and that conflicts with the signedness of a related variable, sometimes it’s best just to use the <em>wrong</em> type, if “pulling the thread” and fixing all related variables would be too invasive.</p> <p>Finally, while using descriptive types is important, be careful not to go overboard. If whatever you’re doing causes warnings, or requires casts, then reconsider or ask for help.</p> </div> <div class="section" id="pointers"> <h4><a class="toc-backref" href="#id19">Pointers</a><a class="headerlink" href="#pointers" title="Permalink to this headline">¶</a></h4> <p>Ensure that all of your pointers are “const-correct”. Unless a pointer is used to modify the pointed-to storage, give it the “const” attribute. That way, the reader knows up-front that this is a read-only pointer. Perhaps more importantly, if we’re diligent about this, when you see a non-const pointer, you’re guaranteed that it is used to modify the storage it points to, or it is aliased to another pointer that is.</p> </div> <div class="section" id="typedefs"> <h4><a class="toc-backref" href="#id20">Typedefs</a><a class="headerlink" href="#typedefs" title="Permalink to this headline">¶</a></h4> <p>Typedefs are used to eliminate the redundant ‘struct’ keyword, since type names have a different style than other identifiers (“CamelCase” versus “snake_case”). Each named struct type should have a CamelCase name and a corresponding typedef.</p> <p>Since certain C compilers choke on duplicated typedefs, you should avoid them and declare a typedef only in one header file. For common types, you can use “include/qemu/typedefs.h” for example. However, as a matter of convenience it is also perfectly fine to use forward struct definitions instead of typedefs in headers and function prototypes; this avoids problems with duplicated typedefs and reduces the need to include headers from other headers.</p> </div> <div class="section" id="reserved-namespaces-in-c-and-posix"> <h4><a class="toc-backref" href="#id21">Reserved namespaces in C and POSIX</a><a class="headerlink" href="#reserved-namespaces-in-c-and-posix" title="Permalink to this headline">¶</a></h4> <p>Underscore capital, double underscore, and underscore ‘t’ suffixes should be avoided.</p> </div> </div> <div class="section" id="low-level-memory-management"> <h3><a class="toc-backref" href="#id22">Low level memory management</a><a class="headerlink" href="#low-level-memory-management" title="Permalink to this headline">¶</a></h3> <p>Use of the <code class="docutils literal notranslate"><span class="pre">malloc/free/realloc/calloc/valloc/memalign/posix_memalign</span></code> APIs is not allowed in the QEMU codebase. Instead of these routines, use the GLib memory allocation routines <code class="docutils literal notranslate"><span class="pre">g_malloc/g_malloc0/g_new/g_new0/g_realloc/g_free</span></code> or QEMU’s <code class="docutils literal notranslate"><span class="pre">qemu_memalign/qemu_blockalign/qemu_vfree</span></code> APIs.</p> <p>Please note that <code class="docutils literal notranslate"><span class="pre">g_malloc</span></code> will exit on allocation failure, so there is no need to test for failure (as you would have to with <code class="docutils literal notranslate"><span class="pre">malloc</span></code>). Generally using <code class="docutils literal notranslate"><span class="pre">g_malloc</span></code> on start-up is fine as the result of a failure to allocate memory is going to be a fatal exit anyway. There may be some start-up cases where failing is unreasonable (for example speculatively loading a large debug symbol table).</p> <p>Care should be taken to avoid introducing places where the guest could trigger an exit by causing a large allocation. For small allocations, of the order of 4k, a failure to allocate is likely indicative of an overloaded host and allowing <code class="docutils literal notranslate"><span class="pre">g_malloc</span></code> to <code class="docutils literal notranslate"><span class="pre">exit</span></code> is a reasonable approach. However for larger allocations where we could realistically fall-back to a smaller one if need be we should use functions like <code class="docutils literal notranslate"><span class="pre">g_try_new</span></code> and check the result. For example this is valid approach for a time/space trade-off like <code class="docutils literal notranslate"><span class="pre">tlb_mmu_resize_locked</span></code> in the SoftMMU TLB code.</p> <p>If the lifetime of the allocation is within the function and there are multiple exist paths you can also improve the readability of the code by using <code class="docutils literal notranslate"><span class="pre">g_autofree</span></code> and related annotations. See <a class="reference internal" href="#autofree-ref"><span class="std std-ref">Automatic memory deallocation</span></a> for more details.</p> <p>Calling <code class="docutils literal notranslate"><span class="pre">g_malloc</span></code> with a zero size is valid and will return NULL.</p> <p>Prefer <code class="docutils literal notranslate"><span class="pre">g_new(T,</span> <span class="pre">n)</span></code> instead of <code class="docutils literal notranslate"><span class="pre">g_malloc(sizeof(T)</span> <span class="pre">*</span> <span class="pre">n)</span></code> for the following reasons:</p> <ul class="simple"> <li>It catches multiplication overflowing size_t;</li> <li>It returns T <code class="docutils literal notranslate"><span class="pre">*</span></code> instead of void <code class="docutils literal notranslate"><span class="pre">*</span></code>, letting compiler catch more type errors.</li> </ul> <p>Declarations like</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">T</span> <span class="o">*</span><span class="n">v</span> <span class="o">=</span> <span class="n">g_malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="o">*</span><span class="n">v</span><span class="p">))</span> </pre></div> </div> <p>are acceptable, though.</p> <p>Memory allocated by <code class="docutils literal notranslate"><span class="pre">qemu_memalign</span></code> or <code class="docutils literal notranslate"><span class="pre">qemu_blockalign</span></code> must be freed with <code class="docutils literal notranslate"><span class="pre">qemu_vfree</span></code>, since breaking this will cause problems on Win32.</p> </div> <div class="section" id="string-manipulation"> <h3><a class="toc-backref" href="#id23">String manipulation</a><a class="headerlink" href="#string-manipulation" title="Permalink to this headline">¶</a></h3> <p>Do not use the strncpy function. As mentioned in the man page, it does <em>not</em> guarantee a NULL-terminated buffer, which makes it extremely dangerous to use. It also zeros trailing destination bytes out to the specified length. Instead, use this similar function when possible, but note its different signature:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span> <span class="n">pstrcpy</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">dest</span><span class="p">,</span> <span class="kt">int</span> <span class="n">dest_buf_size</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">src</span><span class="p">)</span> </pre></div> </div> <p>Don’t use strcat because it can’t check for buffer overflows, but:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">char</span> <span class="o">*</span><span class="n">pstrcat</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="kt">int</span> <span class="n">buf_size</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">s</span><span class="p">)</span> </pre></div> </div> <p>The same limitation exists with sprintf and vsprintf, so use snprintf and vsnprintf.</p> <p>QEMU provides other useful string functions:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="n">strstart</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">str</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">val</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">**</span><span class="n">ptr</span><span class="p">)</span> <span class="kt">int</span> <span class="n">stristart</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">str</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">val</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">**</span><span class="n">ptr</span><span class="p">)</span> <span class="kt">int</span> <span class="n">qemu_strnlen</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">s</span><span class="p">,</span> <span class="kt">int</span> <span class="n">max_len</span><span class="p">)</span> </pre></div> </div> <p>There are also replacement character processing macros for isxyz and toxyz, so instead of e.g. isalnum you should use qemu_isalnum.</p> <p>Because of the memory management rules, you must use g_strdup/g_strndup instead of plain strdup/strndup.</p> </div> <div class="section" id="printf-style-functions"> <h3><a class="toc-backref" href="#id24">Printf-style functions</a><a class="headerlink" href="#printf-style-functions" title="Permalink to this headline">¶</a></h3> <p>Whenever you add a new printf-style function, i.e., one with a format string argument and following “…” in its prototype, be sure to use gcc’s printf attribute directive in the prototype.</p> <p>This makes it so gcc’s -Wformat and -Wformat-security options can do their jobs and cross-check format strings with the number and types of arguments.</p> </div> <div class="section" id="c-standard-implementation-defined-and-undefined-behaviors"> <h3><a class="toc-backref" href="#id25">C standard, implementation defined and undefined behaviors</a><a class="headerlink" href="#c-standard-implementation-defined-and-undefined-behaviors" title="Permalink to this headline">¶</a></h3> <p>C code in QEMU should be written to the C99 language specification. A copy of the final version of the C99 standard with corrigenda TC1, TC2, and TC3 included, formatted as a draft, can be downloaded from:</p> <blockquote> <div><a class="reference external" href="http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf">http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf</a></div></blockquote> <p>The C language specification defines regions of undefined behavior and implementation defined behavior (to give compiler authors enough leeway to produce better code). In general, code in QEMU should follow the language specification and avoid both undefined and implementation defined constructs. (“It works fine on the gcc I tested it with” is not a valid argument…) However there are a few areas where we allow ourselves to assume certain behaviors because in practice all the platforms we care about behave in the same way and writing strictly conformant code would be painful. These are:</p> <ul class="simple"> <li>you may assume that integers are 2s complement representation</li> <li>you may assume that right shift of a signed integer duplicates the sign bit (ie it is an arithmetic shift, not a logical shift)</li> </ul> <p>In addition, QEMU assumes that the compiler does not use the latitude given in C99 and C11 to treat aspects of signed ‘<<’ as undefined, as documented in the GNU Compiler Collection manual starting at version 4.0.</p> </div> <div class="section" id="automatic-memory-deallocation"> <span id="autofree-ref"></span><h3><a class="toc-backref" href="#id26">Automatic memory deallocation</a><a class="headerlink" href="#automatic-memory-deallocation" title="Permalink to this headline">¶</a></h3> <p>QEMU has a mandatory dependency either the GCC or CLang compiler. As such it has the freedom to make use of a C language extension for automatically running a cleanup function when a stack variable goes out of scope. This can be used to simplify function cleanup paths, often allowing many goto jumps to be eliminated, through automatic free’ing of memory.</p> <p>The GLib2 library provides a number of functions/macros for enabling automatic cleanup:</p> <blockquote> <div><a class="reference external" href="https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html">https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html</a></div></blockquote> <p>Most notably:</p> <ul class="simple"> <li>g_autofree - will invoke g_free() on the variable going out of scope</li> <li>g_autoptr - for structs / objects, will invoke the cleanup func created by a previous use of G_DEFINE_AUTOPTR_CLEANUP_FUNC. This is supported for most GLib data types and GObjects</li> </ul> <p>For example, instead of</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="nf">somefunc</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">ret</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="kt">char</span> <span class="o">*</span><span class="n">foo</span> <span class="o">=</span> <span class="n">g_strdup_printf</span><span class="p">(</span><span class="s">"foo%"</span><span class="p">,</span> <span class="s">"wibble"</span><span class="p">);</span> <span class="n">GList</span> <span class="o">*</span><span class="n">bar</span> <span class="o">=</span> <span class="p">.....</span> <span class="k">if</span> <span class="p">(</span><span class="n">eek</span><span class="p">)</span> <span class="p">{</span> <span class="k">goto</span> <span class="n">cleanup</span><span class="p">;</span> <span class="p">}</span> <span class="n">ret</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nl">cleanup</span><span class="p">:</span> <span class="n">g_free</span><span class="p">(</span><span class="n">foo</span><span class="p">);</span> <span class="n">g_list_free</span><span class="p">(</span><span class="n">bar</span><span class="p">);</span> <span class="k">return</span> <span class="n">ret</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p>Using g_autofree/g_autoptr enables the code to be written as:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="nf">somefunc</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">g_autofree</span> <span class="kt">char</span> <span class="o">*</span><span class="n">foo</span> <span class="o">=</span> <span class="n">g_strdup_printf</span><span class="p">(</span><span class="s">"foo%"</span><span class="p">,</span> <span class="s">"wibble"</span><span class="p">);</span> <span class="n">g_autoptr</span> <span class="p">(</span><span class="n">GList</span><span class="p">)</span> <span class="n">bar</span> <span class="o">=</span> <span class="p">.....</span> <span class="k">if</span> <span class="p">(</span><span class="n">eek</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p>While this generally results in simpler, less leak-prone code, there are still some caveats to beware of</p> <ul class="simple"> <li>Variables declared with g_auto* MUST always be initialized, otherwise the cleanup function will use uninitialized stack memory</li> <li>If a variable declared with g_auto* holds a value which must live beyond the life of the function, that value must be saved and the original variable NULL’d out. This can be simpler using g_steal_pointer</li> </ul> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">char</span> <span class="o">*</span><span class="nf">somefunc</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">g_autofree</span> <span class="kt">char</span> <span class="o">*</span><span class="n">foo</span> <span class="o">=</span> <span class="n">g_strdup_printf</span><span class="p">(</span><span class="s">"foo%"</span><span class="p">,</span> <span class="s">"wibble"</span><span class="p">);</span> <span class="n">g_autoptr</span> <span class="p">(</span><span class="n">GList</span><span class="p">)</span> <span class="n">bar</span> <span class="o">=</span> <span class="p">.....</span> <span class="k">if</span> <span class="p">(</span><span class="n">eek</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="n">g_steal_pointer</span><span class="p">(</span><span class="o">&</span><span class="n">foo</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> </div> </div> <div class="section" id="qemu-specific-idioms"> <h2><a class="toc-backref" href="#id27">QEMU Specific Idioms</a><a class="headerlink" href="#qemu-specific-idioms" title="Permalink to this headline">¶</a></h2> <div class="section" id="error-handling-and-reporting"> <h3><a class="toc-backref" href="#id28">Error handling and reporting</a><a class="headerlink" href="#error-handling-and-reporting" title="Permalink to this headline">¶</a></h3> <div class="section" id="reporting-errors-to-the-human-user"> <h4><a class="toc-backref" href="#id29">Reporting errors to the human user</a><a class="headerlink" href="#reporting-errors-to-the-human-user" title="Permalink to this headline">¶</a></h4> <p>Do not use printf(), fprintf() or monitor_printf(). Instead, use error_report() or error_vreport() from error-report.h. This ensures the error is reported in the right place (current monitor or stderr), and in a uniform format.</p> <p>Use error_printf() & friends to print additional information.</p> <p>error_report() prints the current location. In certain common cases like command line parsing, the current location is tracked automatically. To manipulate it manually, use the loc_``*``() from error-report.h.</p> </div> <div class="section" id="propagating-errors"> <h4><a class="toc-backref" href="#id30">Propagating errors</a><a class="headerlink" href="#propagating-errors" title="Permalink to this headline">¶</a></h4> <p>An error can’t always be reported to the user right where it’s detected, but often needs to be propagated up the call chain to a place that can handle it. This can be done in various ways.</p> <p>The most flexible one is Error objects. See error.h for usage information.</p> <p>Use the simplest suitable method to communicate success / failure to callers. Stick to common methods: non-negative on success / -1 on error, non-negative / -errno, non-null / null, or Error objects.</p> <p>Example: when a function returns a non-null pointer on success, and it can fail only in one way (as far as the caller is concerned), returning null on failure is just fine, and certainly simpler and a lot easier on the eyes than propagating an Error object through an Error <code class="docutils literal notranslate"><span class="pre">*````*</span></code> parameter.</p> <p>Example: when a function’s callers need to report details on failure only the function really knows, use Error <code class="docutils literal notranslate"><span class="pre">*````*</span></code>, and set suitable errors.</p> <p>Do not report an error to the user when you’re also returning an error for somebody else to handle. Leave the reporting to the place that consumes the error returned.</p> </div> <div class="section" id="handling-errors"> <h4><a class="toc-backref" href="#id31">Handling errors</a><a class="headerlink" href="#handling-errors" title="Permalink to this headline">¶</a></h4> <p>Calling exit() is fine when handling configuration errors during startup. It’s problematic during normal operation. In particular, monitor commands should never exit().</p> <p>Do not call exit() or abort() to handle an error that can be triggered by the guest (e.g., some unimplemented corner case in guest code translation or device emulation). Guests should not be able to terminate QEMU.</p> <p>Note that &error_fatal is just another way to exit(1), and &error_abort is just another way to abort().</p> </div> </div> <div class="section" id="trace-events-style"> <h3><a class="toc-backref" href="#id32">trace-events style</a><a class="headerlink" href="#trace-events-style" title="Permalink to this headline">¶</a></h3> <div class="section" id="x-prefix"> <h4><a class="toc-backref" href="#id33">0x prefix</a><a class="headerlink" href="#x-prefix" title="Permalink to this headline">¶</a></h4> <p>In trace-events files, use a ‘0x’ prefix to specify hex numbers, as in:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">some_trace</span><span class="p">(</span><span class="kt">unsigned</span> <span class="n">x</span><span class="p">,</span> <span class="kt">uint64_t</span> <span class="n">y</span><span class="p">)</span> <span class="s">"x 0x%x y 0x"</span> <span class="n">PRIx64</span> </pre></div> </div> <p>An exception is made for groups of numbers that are hexadecimal by convention and separated by the symbols ‘.’, ‘/’, ‘:’, or ‘ ‘ (such as PCI bus id):</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">another_trace</span><span class="p">(</span><span class="kt">int</span> <span class="n">cssid</span><span class="p">,</span> <span class="kt">int</span> <span class="n">ssid</span><span class="p">,</span> <span class="kt">int</span> <span class="n">dev_num</span><span class="p">)</span> <span class="s">"bus id: %x.%x.%04x"</span> </pre></div> </div> <p>However, you can use ‘0x’ for such groups if you want. Anyway, be sure that it is obvious that numbers are in hex, ex.:</p> <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">data_dump</span><span class="p">(</span><span class="kt">uint8_t</span> <span class="n">c1</span><span class="p">,</span> <span class="kt">uint8_t</span> <span class="n">c2</span><span class="p">,</span> <span class="kt">uint8_t</span> <span class="n">c3</span><span class="p">)</span> <span class="s">"bytes (in hex): %02x %02x %02x"</span> </pre></div> </div> <p>Rationale: hex numbers are hard to read in logs when there is no 0x prefix, especially when (occasionally) the representation doesn’t contain any letters and especially in one line with other decimal numbers. Number groups are allowed to not use ‘0x’ because for some things notations like %x.%x.%x are used not only in QEMU. Also dumping raw data bytes with ‘0x’ is less readable.</p> </div> <div class="section" id="printf-flag"> <h4><a class="toc-backref" href="#id34">‘#’ printf flag</a><a class="headerlink" href="#printf-flag" title="Permalink to this headline">¶</a></h4> <p>Do not use printf flag ‘#’, like ‘%#x’.</p> <p>Rationale: there are two ways to add a ‘0x’ prefix to printed number: ‘0x%…’ and ‘%#…’. For consistency the only one way should be used. Arguments for ‘0x%’ are:</p> <ul class="simple"> <li>it is more popular</li> <li>‘%#’ omits the 0x for the value 0 which makes output inconsistent</li> </ul> </div> </div> </div> </div> </div> </div> <footer> <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation"> <a href="kconfig.html" class="btn btn-neutral float-right" title="QEMU and Kconfig" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a> <a href="build-system.html" class="btn btn-neutral" title="The QEMU build system architecture" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a> </div> <hr/> <div role="contentinfo"> <p> © Copyright 2021, The QEMU Project Developers. </p> </div> Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. <!-- Empty para to force a blank line after "Built with Sphinx ..." --> <p></p> <p>This documentation is for QEMU version 6.2.0.</p> <p><a href="../about/license.html">QEMU and this manual are released under the GNU General Public License, version 2.</a></p> </footer> </div> </div> </section> </div> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT:'../', VERSION:'qemu-kvm-6.2.0-53.module+el8.10.0+2055+8eb7870b.4', LANGUAGE:'None', COLLAPSE_INDEX:false, FILE_SUFFIX:'.html', HAS_SOURCE: false, SOURCELINK_SUFFIX: '.txt' }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/js/theme.js"></script> <script type="text/javascript"> jQuery(function () { SphinxRtdTheme.Navigation.enable(true); }); </script> </body> </html>