<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="using-gdb-on-mysqld">
      </a>
      7.9.1.4 Debugging mysqld under gdb
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045253751296">
  </a>
  <p>
   On most systems you can also start
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   from
   <span class="command">
    <strong>
     gdb
    </strong>
   </span>
   to get more information if
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   crashes.
  </p>
  <p>
   With some older
   <span class="command">
    <strong>
     gdb
    </strong>
   </span>
   versions on Linux you
        must use
   <code class="literal">
    run --one-thread
   </code>
   if you want to be
        able to debug
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   threads. In this case,
        you can only have one thread active at a time.
  </p>
  <p>
   NPTL threads (the new thread library on Linux) may cause
        problems while running
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   under
   <span class="command">
    <strong>
     gdb
    </strong>
   </span>
   . Some symptoms are:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      hangs during startup (before it
            writes
      <code class="literal">
       ready for connections
      </code>
      ).
     </p>
    </li>
    <li class="listitem">
     <p>
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      crashes during a
      <code class="literal">
       pthread_mutex_lock()
      </code>
      or
      <code class="literal">
       pthread_mutex_unlock()
      </code>
      call.
     </p>
    </li>
   </ul>
  </div>
  <p>
   In this case, you should set the following environment variable
        in the shell before starting
   <span class="command">
    <strong>
     gdb
    </strong>
   </span>
   :
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa51494095"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal">LD_ASSUME_KERNEL<span class="token attr-value"><span class="token punctuation">=</span>2.4.1</span>
export LD_ASSUME_KERNEL</code></pre>
  </div>
  <p>
   When running
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   under
   <span class="command">
    <strong>
     gdb
    </strong>
   </span>
   , you should disable the stack trace with
   <a class="link" href="server-options.html#option_mysqld_skip-stack-trace">
    <code class="option">
     --skip-stack-trace
    </code>
   </a>
   to be able to
        catch segfaults within
   <span class="command">
    <strong>
     gdb
    </strong>
   </span>
   .
  </p>
  <a class="indexterm" name="idm46045253727712">
  </a>
  <p>
   Use the
   <a class="link" href="server-options.html#option_mysqld_gdb">
    <code class="option">
     --gdb
    </code>
   </a>
   option to
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   to install an interrupt handler for
   <code class="literal">
    SIGINT
   </code>
   (needed to stop
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   with
   <code class="literal">
    ^C
   </code>
   to set
        breakpoints) and disable stack tracing and core file handling.
  </p>
  <p>
   It is very hard to debug MySQL under
   <span class="command">
    <strong>
     gdb
    </strong>
   </span>
   if
        you do a lot of new connections the whole time as
   <span class="command">
    <strong>
     gdb
    </strong>
   </span>
   does not free the memory for old threads.
        You can avoid this problem by starting
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   with
   <a class="link" href="server-system-variables.html#sysvar_thread_cache_size">
    <code class="literal">
     thread_cache_size
    </code>
   </a>
   set to a
        value equal to
   <a class="link" href="server-system-variables.html#sysvar_max_connections">
    <code class="literal">
     max_connections
    </code>
   </a>
   + 1. In most cases just using
   <a class="link" href="server-system-variables.html#sysvar_thread_cache_size">
    <code class="option">
     --thread_cache_size=5'
    </code>
   </a>
   helps a
        lot!
  </p>
  <p>
   If you want to get a core dump on Linux if
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   dies with a SIGSEGV signal, you can
        start
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   with the
   <a class="link" href="server-options.html#option_mysqld_core-file">
    <code class="option">
     --core-file
    </code>
   </a>
   option. This core
        file can be used to make a backtrace that may help you find out
        why
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   died:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa44408125"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">$&gt; </span><span class="token command">gdb</span> mysqld core
<span class="token prompt">gdb&gt;</span><span class="token command">   backtrace</span> full
<span class="token prompt">gdb&gt;</span><span class="token command">   quit</span></code></pre>
  </div>
  <p>
   See
   <a class="xref" href="crashing.html" title="B.3.3.3 What to Do If MySQL Keeps Crashing">
    Section B.3.3.3, “What to Do If MySQL Keeps Crashing”
   </a>
   .
  </p>
  <p>
   If you are using
   <span class="command">
    <strong>
     gdb
    </strong>
   </span>
   on Linux, you should
        install a
   <code class="filename">
    .gdb
   </code>
   file, with the following
        information, in your current directory:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa70984475"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal">set print sevenbit off
handle SIGUSR1 nostop noprint
handle SIGUSR2 nostop noprint
handle SIGWAITING nostop noprint
handle SIGLWP nostop noprint
handle SIGPIPE nostop
handle SIGALRM nostop
handle SIGHUP nostop
handle SIGTERM nostop noprint</code></pre>
  </div>
  <p>
   Here is an example how to debug
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   :
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa92594841"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">$&gt; </span><span class="token command">gdb</span> /usr/local/libexec/mysqld
<span class="token prompt">gdb&gt;</span><span class="token command"> run</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
backtrace full <span class="token comment" spellcheck="true"># Do this when mysqld crashes</span></code></pre>
  </div>
  <p>
   Include the preceding output in a bug report, which you can file
        using the instructions in
   <a class="xref" href="bug-reports.html" title="1.6 How to Report Bugs or Problems">
    Section 1.6, “How to Report Bugs or Problems”
   </a>
   .
  </p>
  <p>
   If
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   hangs, you can try to use some
        system tools like
   <code class="literal">
    strace
   </code>
   or
   <code class="literal">
    /usr/proc/bin/pstack
   </code>
   to examine where
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   has hung.
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa64324284"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal">strace /tmp/log libexec/mysqld</code></pre>
  </div>
  <a class="indexterm" name="idm46045253692976">
  </a>
  <a class="indexterm" name="idm46045253691904">
  </a>
  <a class="indexterm" name="idm46045253690832">
  </a>
  <a class="indexterm" name="idm46045253689744">
  </a>
  <p>
   If you are using the Perl
   <code class="literal">
    DBI
   </code>
   interface, you
        can turn on debugging information by using the
   <code class="literal">
    trace
   </code>
   method or by setting the
   <code class="literal">
    DBI_TRACE
   </code>
   environment variable.
  </p>
 </div>
 <br/>
</div>
