blob: bcdfb59ba99ab0d78894a6cdc74bf7952afdeaa9 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2015 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Free Software" and "Free Software Needs
Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
and with the Back-Cover Texts as in (a) below.
(a) The FSF's Back-Cover Text is: "You are free to copy and modify
this GNU Manual. Buying copies from GNU Press supports the FSF in
developing GNU and promoting software freedom." -->
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Debugging with GDB: Threads</title>
<meta name="description" content="Debugging with GDB: Threads">
<meta name="keywords" content="Debugging with GDB: Threads">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Running.html#Running" rel="up" title="Running">
<link href="Forks.html#Forks" rel="next" title="Forks">
<link href="Inferiors-and-Programs.html#Inferiors-and-Programs" rel="prev" title="Inferiors and Programs">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Threads"></a>
<div class="header">
<p>
Next: <a href="Forks.html#Forks" accesskey="n" rel="next">Forks</a>, Previous: <a href="Inferiors-and-Programs.html#Inferiors-and-Programs" accesskey="p" rel="prev">Inferiors and Programs</a>, Up: <a href="Running.html#Running" accesskey="u" rel="up">Running</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Debugging-Programs-with-Multiple-Threads"></a>
<h3 class="section">4.10 Debugging Programs with Multiple Threads</h3>
<a name="index-threads-of-execution"></a>
<a name="index-multiple-threads"></a>
<a name="index-switching-threads"></a>
<p>In some operating systems, such as HP-UX and Solaris, a single program
may have more than one <em>thread</em> of execution. The precise semantics
of threads differ from one operating system to another, but in general
the threads of a single program are akin to multiple processes&mdash;except
that they share one address space (that is, they can all examine and
modify the same variables). On the other hand, each thread has its own
registers and execution stack, and perhaps private memory.
</p>
<p><small>GDB</small> provides these facilities for debugging multi-thread
programs:
</p>
<ul>
<li> automatic notification of new threads
</li><li> &lsquo;<samp>thread <var>threadno</var></samp>&rsquo;, a command to switch among threads
</li><li> &lsquo;<samp>info threads</samp>&rsquo;, a command to inquire about existing threads
</li><li> &lsquo;<samp>thread apply [<var>threadno</var>] [<var>all</var>] <var>args</var></samp>&rsquo;,
a command to apply a command to a list of threads
</li><li> thread-specific breakpoints
</li><li> &lsquo;<samp>set print thread-events</samp>&rsquo;, which controls printing of
messages on thread start and exit.
</li><li> &lsquo;<samp>set libthread-db-search-path <var>path</var></samp>&rsquo;, which lets
the user specify which <code>libthread_db</code> to use if the default choice
isn&rsquo;t compatible with the program.
</li></ul>
<blockquote>
<p><em>Warning:</em> These facilities are not yet available on every
<small>GDB</small> configuration where the operating system supports threads.
If your <small>GDB</small> does not support threads, these commands have no
effect. For example, a system without thread support shows no output
from &lsquo;<samp>info threads</samp>&rsquo;, and always rejects the <code>thread</code> command,
like this:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) info threads
(gdb) thread 1
Thread ID 1 not known. Use the &quot;info threads&quot; command to
see the IDs of currently known threads.
</pre></div>
</blockquote>
<a name="index-focus-of-debugging"></a>
<a name="index-current-thread"></a>
<p>The <small>GDB</small> thread debugging facility allows you to observe all
threads while your program runs&mdash;but whenever <small>GDB</small> takes
control, one thread in particular is always the focus of debugging.
This thread is called the <em>current thread</em>. Debugging commands show
program information from the perspective of the current thread.
</p>
<a name="index-New-systag-message"></a>
<a name="index-thread-identifier-_0028system_0029"></a>
<p>Whenever <small>GDB</small> detects a new thread in your program, it displays
the target system&rsquo;s identification for the thread with a message in the
form &lsquo;<samp>[New <var>systag</var>]</samp>&rsquo;, where <var>systag</var> is a thread identifier
whose form varies depending on the particular system. For example, on
<small>GNU</small>/Linux, you might see
</p>
<div class="smallexample">
<pre class="smallexample">[New Thread 0x41e02940 (LWP 25582)]
</pre></div>
<p>when <small>GDB</small> notices a new thread. In contrast, on an SGI system,
the <var>systag</var> is simply something like &lsquo;<samp>process 368</samp>&rsquo;, with no
further qualifier.
</p>
<a name="index-thread-number"></a>
<a name="index-thread-identifier-_0028GDB_0029"></a>
<p>For debugging purposes, <small>GDB</small> associates its own thread
number&mdash;always a single integer&mdash;with each thread in your program.
</p>
<dl compact="compact">
<dd><a name="index-info-threads"></a>
</dd>
<dt><code>info threads <span class="roman">[</span><var>id</var>&hellip;<span class="roman">]</span></code></dt>
<dd><p>Display a summary of all threads currently in your program. Optional
argument <var>id</var>&hellip; is one or more thread ids separated by spaces, and
means to print information only about the specified thread or threads.
<small>GDB</small> displays for each thread (in this order):
</p>
<ol>
<li> the thread number assigned by <small>GDB</small>
</li><li> the target system&rsquo;s thread identifier (<var>systag</var>)
</li><li> the thread&rsquo;s name, if one is known. A thread can either be named by
the user (see <code>thread name</code>, below), or, in some cases, by the
program itself.
</li><li> the current stack frame summary for that thread
</li></ol>
<p>An asterisk &lsquo;<samp>*</samp>&rsquo; to the left of the <small>GDB</small> thread number
indicates the current thread.
</p>
<p>For example,
</p></dd>
</dl>
<div class="smallexample">
<pre class="smallexample">(gdb) info threads
Id Target Id Frame
3 process 35 thread 27 0x34e5 in sigpause ()
2 process 35 thread 23 0x34e5 in sigpause ()
* 1 process 35 thread 13 main (argc=1, argv=0x7ffffff8)
at threadtest.c:68
</pre></div>
<p>On Solaris, you can display more information about user threads with a
Solaris-specific command:
</p>
<dl compact="compact">
<dt><code>maint info sol-threads</code></dt>
<dd><a name="index-maint-info-sol_002dthreads"></a>
<a name="index-thread-info-_0028Solaris_0029"></a>
<p>Display info on Solaris user threads.
</p></dd>
</dl>
<dl compact="compact">
<dd><a name="index-thread-threadno"></a>
</dd>
<dt><code>thread <var>threadno</var></code></dt>
<dd><p>Make thread number <var>threadno</var> the current thread. The command
argument <var>threadno</var> is the internal <small>GDB</small> thread number, as
shown in the first field of the &lsquo;<samp>info threads</samp>&rsquo; display.
<small>GDB</small> responds by displaying the system identifier of the thread
you selected, and its current stack frame summary:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) thread 2
[Switching to thread 2 (Thread 0xb7fdab70 (LWP 12747))]
#0 some_function (ignore=0x0) at example.c:8
8 printf (&quot;hello\n&quot;);
</pre></div>
<p>As with the &lsquo;<samp>[New &hellip;]</samp>&rsquo; message, the form of the text after
&lsquo;<samp>Switching to</samp>&rsquo; depends on your system&rsquo;s conventions for identifying
threads.
</p>
<a name="index-_0024_005fthread_002c-convenience-variable"></a>
<p>The debugger convenience variable &lsquo;<samp>$_thread</samp>&rsquo; contains the number
of the current thread. You may find this useful in writing breakpoint
conditional expressions, command scripts, and so forth. See
See <a href="Convenience-Vars.html#Convenience-Vars">Convenience Variables</a>, for general
information on convenience variables.
</p>
<a name="index-thread-apply"></a>
<a name="index-apply-command-to-several-threads"></a>
</dd>
<dt><code>thread apply [<var>threadno</var> | all [-ascending]] <var>command</var></code></dt>
<dd><p>The <code>thread apply</code> command allows you to apply the named
<var>command</var> to one or more threads. Specify the numbers of the
threads that you want affected with the command argument
<var>threadno</var>. It can be a single thread number, one of the numbers
shown in the first field of the &lsquo;<samp>info threads</samp>&rsquo; display; or it
could be a range of thread numbers, as in <code>2-4</code>. To apply
a command to all threads in descending order, type <kbd>thread apply all
<var>command</var></kbd>. To apply a command to all threads in ascending order,
type <kbd>thread apply all -ascending <var>command</var></kbd>.
</p>
<a name="index-thread-name"></a>
<a name="index-name-a-thread"></a>
</dd>
<dt><code>thread name [<var>name</var>]</code></dt>
<dd><p>This command assigns a name to the current thread. If no argument is
given, any existing user-specified name is removed. The thread name
appears in the &lsquo;<samp>info threads</samp>&rsquo; display.
</p>
<p>On some systems, such as <small>GNU</small>/Linux, <small>GDB</small> is able to
determine the name of the thread as given by the OS. On these
systems, a name specified with &lsquo;<samp>thread name</samp>&rsquo; will override the
system-give name, and removing the user-specified name will cause
<small>GDB</small> to once again display the system-specified name.
</p>
<a name="index-thread-find"></a>
<a name="index-search-for-a-thread"></a>
</dd>
<dt><code>thread find [<var>regexp</var>]</code></dt>
<dd><p>Search for and display thread ids whose name or <var>systag</var>
matches the supplied regular expression.
</p>
<p>As well as being the complement to the &lsquo;<samp>thread name</samp>&rsquo; command,
this command also allows you to identify a thread by its target
<var>systag</var>. For instance, on <small>GNU</small>/Linux, the target <var>systag</var>
is the LWP id.
</p>
<div class="smallexample">
<pre class="smallexample">(GDB) thread find 26688
Thread 4 has target id 'Thread 0x41e02940 (LWP 26688)'
(GDB) info thread 4
Id Target Id Frame
4 Thread 0x41e02940 (LWP 26688) 0x00000031ca6cd372 in select ()
</pre></div>
<a name="index-set-print-thread_002devents"></a>
<a name="index-print-messages-on-thread-start-and-exit"></a>
</dd>
<dt><code>set print thread-events</code></dt>
<dt><code>set print thread-events on</code></dt>
<dt><code>set print thread-events off</code></dt>
<dd><p>The <code>set print thread-events</code> command allows you to enable or
disable printing of messages when <small>GDB</small> notices that new threads have
started or that threads have exited. By default, these messages will
be printed if detection of these events is supported by the target.
Note that these messages cannot be disabled on all targets.
</p>
<a name="index-show-print-thread_002devents"></a>
</dd>
<dt><code>show print thread-events</code></dt>
<dd><p>Show whether messages will be printed when <small>GDB</small> detects that threads
have started and exited.
</p></dd>
</dl>
<p>See <a href="Thread-Stops.html#Thread-Stops">Stopping and Starting Multi-thread Programs</a>, for
more information about how <small>GDB</small> behaves when you stop and start
programs with multiple threads.
</p>
<p>See <a href="Set-Watchpoints.html#Set-Watchpoints">Setting Watchpoints</a>, for information about
watchpoints in programs with multiple threads.
</p>
<a name="set-libthread_002ddb_002dsearch_002dpath"></a><dl compact="compact">
<dd><a name="index-set-libthread_002ddb_002dsearch_002dpath"></a>
<a name="index-search-path-for-libthread_005fdb"></a>
</dd>
<dt><code>set libthread-db-search-path <span class="roman">[</span><var>path</var><span class="roman">]</span></code></dt>
<dd><p>If this variable is set, <var>path</var> is a colon-separated list of
directories <small>GDB</small> will use to search for <code>libthread_db</code>.
If you omit <var>path</var>, &lsquo;<samp>libthread-db-search-path</samp>&rsquo; will be reset to
its default value (<code>$sdir:$pdir</code> on <small>GNU</small>/Linux and Solaris systems).
Internally, the default value comes from the <code>LIBTHREAD_DB_SEARCH_PATH</code>
macro.
</p>
<p>On <small>GNU</small>/Linux and Solaris systems, <small>GDB</small> uses a &ldquo;helper&rdquo;
<code>libthread_db</code> library to obtain information about threads in the
inferior process. <small>GDB</small> will use &lsquo;<samp>libthread-db-search-path</samp>&rsquo;
to find <code>libthread_db</code>. <small>GDB</small> also consults first if inferior
specific thread debugging library loading is enabled
by &lsquo;<samp>set auto-load libthread-db</samp>&rsquo; (see <a href="libthread_005fdb_002eso_002e1-file.html#libthread_005fdb_002eso_002e1-file">libthread_db.so.1 file</a>).
</p>
<p>A special entry &lsquo;<samp>$sdir</samp>&rsquo; for &lsquo;<samp>libthread-db-search-path</samp>&rsquo;
refers to the default system directories that are
normally searched for loading shared libraries. The &lsquo;<samp>$sdir</samp>&rsquo; entry
is the only kind not needing to be enabled by &lsquo;<samp>set auto-load libthread-db</samp>&rsquo;
(see <a href="libthread_005fdb_002eso_002e1-file.html#libthread_005fdb_002eso_002e1-file">libthread_db.so.1 file</a>).
</p>
<p>A special entry &lsquo;<samp>$pdir</samp>&rsquo; for &lsquo;<samp>libthread-db-search-path</samp>&rsquo;
refers to the directory from which <code>libpthread</code>
was loaded in the inferior process.
</p>
<p>For any <code>libthread_db</code> library <small>GDB</small> finds in above directories,
<small>GDB</small> attempts to initialize it with the current inferior process.
If this initialization fails (which could happen because of a version
mismatch between <code>libthread_db</code> and <code>libpthread</code>), <small>GDB</small>
will unload <code>libthread_db</code>, and continue with the next directory.
If none of <code>libthread_db</code> libraries initialize successfully,
<small>GDB</small> will issue a warning and thread debugging will be disabled.
</p>
<p>Setting <code>libthread-db-search-path</code> is currently implemented
only on some platforms.
</p>
<a name="index-show-libthread_002ddb_002dsearch_002dpath"></a>
</dd>
<dt><code>show libthread-db-search-path</code></dt>
<dd><p>Display current libthread_db search path.
</p>
<a name="index-set-debug-libthread_002ddb"></a>
<a name="index-show-debug-libthread_002ddb"></a>
<a name="index-debugging-libthread_005fdb"></a>
</dd>
<dt><code>set debug libthread-db</code></dt>
<dt><code>show debug libthread-db</code></dt>
<dd><p>Turns on or off display of <code>libthread_db</code>-related events.
Use <code>1</code> to enable, <code>0</code> to disable.
</p></dd>
</dl>
<hr>
<div class="header">
<p>
Next: <a href="Forks.html#Forks" accesskey="n" rel="next">Forks</a>, Previous: <a href="Inferiors-and-Programs.html#Inferiors-and-Programs" accesskey="p" rel="prev">Inferiors and Programs</a>, Up: <a href="Running.html#Running" accesskey="u" rel="up">Running</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>