    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Get Debugging Better!</title>
        <link>https://members.accu.org/index.php/articles/2111</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #127 - June 2015</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c350/">o127</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-350/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+350/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Get Debugging Better!</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 02 June 2015 10:31:04 +01:00 or Tue, 02 June 2015 10:31:04 +01:00</p>
<p><strong>Summary:</strong>&nbsp;The GNU debugger has several useful features you may not know. Jonathan Wakely shows us how to save time and pain with some simple tricks.</p>
<p><strong>Body:</strong>&nbsp;<p>The GNU Debugger (GDB) is a powerful tool, but if youâ€™re used to working in an IDE then using a command-line debugger can be daunting and may seem to be lacking features you take for granted in an IDE. This article has some simple tips that might help you have a more pleasant debugging experience, and might inspire you to read the documentation [<a href="#[GDB]">GDB</a>] to see what other tricks are waiting to be discovered.</p>

<p>The first tip, and maybe the most important, is to make sure youâ€™re using a recent version. Support for debugging C++ code got much better with GDB 7.0 and has continued to improve since then. If youâ€™re using anything older than GDB 7.0 you should upgrade right away, and itâ€™s probably worth upgrading anything older than a couple of releases (GDB 7.8.2 and 7.9 were both released in early 2015). If you canâ€™t get a pre-built version for your OS then compiling GDB from source is very easy, just download the tarball, unpack it, run configure (setting your preferred install directory with <code>--prefix=dir</code>) and run <strong>make</strong>.</p>

<p>One of the simplest GDB features, but one I always miss when using the venerable â€˜dbxâ€™ debugger on â€˜properâ€™ UNIX machines, is the ability to use abbreviations for commands. Any unambiguous prefix for a command will run the full command, so instead of typing <code>print foo</code> you only need <code>p foo</code> and instead of <code>break source.cc:325</code> just <code>br source.cc:325</code> (and while not strictly a prefix of the full command, <code>bt</code> will print a stack trace just like <code>backtrace</code>).</p>

<p>You can also very easily create your own commands by defining them in your personal <span class="filename">~/.gdbinit</span> file, which gets run by GDB on startup. I use the following to quit without being asked to confirm that I want to kill the process being debugged:</p>

<pre class="programlisting">
  define qquit
    set confirm off
    quit
  end
  document qquit
  Quit without asking for confirmation.
  end</pre>

<p>This allows me to use <code>qquit</code> (or just <code>qq</code>) to exit quickly. The <code>document</code> section provides the documentation that will be printed if you type <code>help qq</code> at the gdb prompt.</p>

<p>Sometimes stepping through C++ code can be very tedious if the program keeps stepping into tiny inline functions that donâ€™t do anything interesting. This is very obvious in C++11 code that makes heavy use of <code>std::move</code> and <code>std::forward</code>, both functions that do nothing except cast a variable to the right kind of reference. The solution is to tell gdb not to bother stepping into uninteresting functions (or ones that get called a lot but which you know are not the source of the problem youâ€™re debugging). Running the <code>skip</code> command with no arguments will cause the current function to be skipped over next time it is reached, instead of stepping into it. You can also use <code>skip FUNCTION</code> to cause a named function to be skipped instead of the current one, or <code>skip file FILENAME</code> to skip whole files (as with most commands, run <code>help skip</code> at the gdb prompt for more information). Unfortunately the <code>skip</code> command treats every specialization of a function template as a separate function and thereâ€™s no way to skip over all specializations of say, <code>std::move</code>, but if you skip each one as you step into it at least you know you wonâ€™t step into that particular specialization again. I define the following command in my <span class="filename">.gdbinit</span> to make this even easier:</p>

<pre class="programlisting">
  define skipfin
    dont-repeat
    skip
    finish
  end
  document skipfin
  Return from the current function and skip over
  all future calls to it.
  end</pre>

<p>This lets me use <code>skipfin</code> to mark the current function to be skipped in future and to finish running it and return to the caller. The <code>dont-repeat</code> line tells gdb that (unlike most built-in commands), hitting enter again after running <code>skipfin</code> should not run <code>skipfin</code> again, so that I donâ€™t accidentally finish running the caller and mark that to be skipped as well!</p>

<p>Another useful entry in my <span class="filename">.gdbinit</span> is <code>set history save</code>, which causes gdb to save your command history when exiting, so you can use the cursor keys to scroll through your history and easily create the same breakpoints or watchpoints as you used in an earlier debugging session.</p>

<p>The GDB feature that I most wish Iâ€™d known about sooner is the â€˜TUIâ€™ mode, which is activated by the <code>-tui</code> command-line option, or can be turned on and off in an existing debugging session with Ctrl-X Ctrl-A [<a href="#[TUI]">TUI</a>]. This splits the terminal window horizontally, with the bottom pane showing the usual prompt where you type commands and get output, and the top pane showing the source code for the function being debugged, just like your IDE would. This gives you a much more immediate view of the code than using <code>list</code> to print out chunks of it. One thing to be aware of is that the TUI mode changes the behaviour of the cursor keys, so they scroll up and down in the source code rather than through your command history. If youâ€™re familiar with them from the terminal, you can still use readline key bindings (Ctrl-P, Ctrl-N etc.) to scroll through the command history.</p>

<p>After <code>-tui</code>, the command-line option I most often use when starting gdb is <code>--args</code>. This can be used to start debugging a program with a specific set of arguments, so instead of running <code>gdb ./a.out</code> and then setting arguments for it with <code>set args a b c</code> then running it with <code>run</code>, you can start gdb as <code>gdb --args ./a.out a b c</code> and then just <code>run</code>. This is very useful when the program needs a long and complicated set of arguments, as you donâ€™t need to find them and copy &amp; paste them into gdb, just add <code>gdb --args</code> before the usual command to run the program.</p>

<p>One of the most useful features of modern version of GDB is the embedded Python interpreter. This allows you to write pretty printers for your own types (or use the ones that come with GCC for printing standard library types such as containers). Defining pretty printers for the types in your system can be very useful, and although itâ€™s not too complicated there isnâ€™t room to explain here, however the embedded Python interpreter is also very useful for running simple one-liners without leaving gdb. For example if you have a <code>time_t</code> variable containing a unix timestamp you can easily print it using Pythonâ€™s <strong>datetime</strong> module:</p>

<pre class="programlisting">
  (gdb) python import datetime
  (gdb) python print
    datetime.datetime.fromtimestamp(1425690208)
  2015-03-07 01:03:28</pre>

<p>If what you want to do isnâ€™t suitable for a one-liner you can create multiple-line blocks of Python by entering just <code>python</code> on a line on its own, and then end the block with <code>end</code>. Many of gdbâ€™s features are exposed via a python API (â€˜import gdbâ€™) [<a href="#[Python]">Python</a>] that lets you inspect variables, so you can examine their value, type, members etc.</p>

<p>None of these tips are groundbreaking, but I hope they give an idea of ways you can customise your debugging experience and define shortcuts to simplify the most repetitive tasks.</p>

<h2>References</h2>

<p class="bibliomixed"><a id="[GDB]"></a>[GDB] <a href="https://sourceware.org/gdb/onlinedocs/gdb/index.html#Top">https://sourceware.org/gdb/onlinedocs/gdb/index.html#Top</a></p>

<p class="bibliomixed"><a id="[Python]"></a>[Python] <a href="https://sourceware.org/gdb/onlinedocs/gdb/Python-API.html#Python-API">https://sourceware.org/gdb/onlinedocs/gdb/Python-API.html#Python-API</a></p>

<p class="bibliomixed">[TUI]<a id="[TUI]"></a> <a href="https://sourceware.org/gdb/onlinedocs/gdb/TUI.html">https://sourceware.org/gdb/onlinedocs/gdb/TUI.html</a></p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
