1<html lang="en"> 2<head> 3<title>Server - Debugging with GDB</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Debugging with GDB"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Remote-Debugging.html#Remote-Debugging" title="Remote Debugging"> 9<link rel="prev" href="File-Transfer.html#File-Transfer" title="File Transfer"> 10<link rel="next" href="Remote-Configuration.html#Remote-Configuration" title="Remote Configuration"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988-2013 Free Software Foundation, Inc. 14 15Permission is granted to copy, distribute and/or modify this document 16under the terms of the GNU Free Documentation License, Version 1.3 or 17any later version published by the Free Software Foundation; with the 18Invariant Sections being ``Free Software'' and ``Free Software Needs 19Free Documentation'', with the Front-Cover Texts being ``A GNU Manual,'' 20and with the Back-Cover Texts as in (a) below. 21 22(a) The FSF's Back-Cover Text is: ``You are free to copy and modify 23this GNU Manual. Buying copies from GNU Press supports the FSF in 24developing GNU and promoting software freedom.'' 25--> 26<meta http-equiv="Content-Style-Type" content="text/css"> 27<style type="text/css"><!-- 28 pre.display { font-family:inherit } 29 pre.format { font-family:inherit } 30 pre.smalldisplay { font-family:inherit; font-size:smaller } 31 pre.smallformat { font-family:inherit; font-size:smaller } 32 pre.smallexample { font-size:smaller } 33 pre.smalllisp { font-size:smaller } 34 span.sc { font-variant:small-caps } 35 span.roman { font-family:serif; font-weight:normal; } 36 span.sansserif { font-family:sans-serif; font-weight:normal; } 37--></style> 38<link rel="stylesheet" type="text/css" href="../cs.css"> 39</head> 40<body> 41<div class="node"> 42<a name="Server"></a> 43<p> 44Next: <a rel="next" accesskey="n" href="Remote-Configuration.html#Remote-Configuration">Remote Configuration</a>, 45Previous: <a rel="previous" accesskey="p" href="File-Transfer.html#File-Transfer">File Transfer</a>, 46Up: <a rel="up" accesskey="u" href="Remote-Debugging.html#Remote-Debugging">Remote Debugging</a> 47<hr> 48</div> 49 50<h3 class="section">20.3 Using the <code>gdbserver</code> Program</h3> 51 52<p><a name="index-gdbserver-1194"></a><a name="index-remote-connection-without-stubs-1195"></a><code>gdbserver</code> is a control program for Unix-like systems, which 53allows you to connect your program with a remote <span class="sc">gdb</span> via 54<code>target remote</code>—but without linking in the usual debugging stub. 55 56 <p><code>gdbserver</code> is not a complete replacement for the debugging stubs, 57because it requires essentially the same operating-system facilities 58that <span class="sc">gdb</span> itself does. In fact, a system that can run 59<code>gdbserver</code> to connect to a remote <span class="sc">gdb</span> could also run 60<span class="sc">gdb</span> locally! <code>gdbserver</code> is sometimes useful nevertheless, 61because it is a much smaller program than <span class="sc">gdb</span> itself. It is 62also easier to port than all of <span class="sc">gdb</span>, so you may be able to get 63started more quickly on a new system by using <code>gdbserver</code>. 64Finally, if you develop code for real-time systems, you may find that 65the tradeoffs involved in real-time operation make it more convenient to 66do as much development work as possible on another system, for example 67by cross-compiling. You can use <code>gdbserver</code> to make a similar 68choice for debugging. 69 70 <p><span class="sc">gdb</span> and <code>gdbserver</code> communicate via either a serial line 71or a TCP connection, using the standard <span class="sc">gdb</span> remote serial 72protocol. 73 74 <blockquote> 75<em>Warning:</em> <code>gdbserver</code> does not have any built-in security. 76Do not run <code>gdbserver</code> connected to any public network; a 77<span class="sc">gdb</span> connection to <code>gdbserver</code> provides access to the 78target system with the same privileges as the user running 79<code>gdbserver</code>. 80</blockquote> 81 82<h4 class="subsection">20.3.1 Running <code>gdbserver</code></h4> 83 84<p><a name="index-arguments_002c-to-_0040code_007bgdbserver_007d-1196"></a><a name="index-g_t_0040code_007bgdbserver_007d_002c-command_002dline-arguments-1197"></a> 85Run <code>gdbserver</code> on the target system. You need a copy of the 86program you want to debug, including any libraries it requires. 87<code>gdbserver</code> does not need your program's symbol table, so you can 88strip the program if necessary to save space. <span class="sc">gdb</span> on the host 89system does all the symbol handling. 90 91 <p>To use the server, you must tell it how to communicate with <span class="sc">gdb</span>; 92the name of your program; and the arguments for your program. The usual 93syntax is: 94 95<pre class="smallexample"> target> gdbserver <var>comm</var> <var>program</var> [ <var>args</var> ... ] 96</pre> 97 <p><var>comm</var> is either a device name (to use a serial line), or a TCP 98hostname and portnumber, or <code>-</code> or <code>stdio</code> to use 99stdin/stdout of <code>gdbserver</code>. 100For example, to debug Emacs with the argument 101‘<samp><span class="samp">foo.txt</span></samp>’ and communicate with <span class="sc">gdb</span> over the serial port 102<samp><span class="file">/dev/com1</span></samp>: 103 104<pre class="smallexample"> target> gdbserver /dev/com1 emacs foo.txt 105</pre> 106 <p><code>gdbserver</code> waits passively for the host <span class="sc">gdb</span> to communicate 107with it. 108 109 <p>To use a TCP connection instead of a serial line: 110 111<pre class="smallexample"> target> gdbserver host:2345 emacs foo.txt 112</pre> 113 <p>The only difference from the previous example is the first argument, 114specifying that you are communicating with the host <span class="sc">gdb</span> via 115TCP. The ‘<samp><span class="samp">host:2345</span></samp>’ argument means that <code>gdbserver</code> is to 116expect a TCP connection from machine ‘<samp><span class="samp">host</span></samp>’ to local TCP port 2345. 117(Currently, the ‘<samp><span class="samp">host</span></samp>’ part is ignored.) You can choose any number 118you want for the port number as long as it does not conflict with any 119TCP ports already in use on the target system (for example, <code>23</code> is 120reserved for <code>telnet</code>).<a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a> You must use the same port number with the host <span class="sc">gdb</span> 121<code>target remote</code> command. 122 123 <p>The <code>stdio</code> connection is useful when starting <code>gdbserver</code> 124with ssh: 125 126<pre class="smallexample"> (gdb) target remote | ssh -T hostname gdbserver - hello 127</pre> 128 <p>The ‘<samp><span class="samp">-T</span></samp>’ option to ssh is provided because we don't need a remote pty, 129and we don't want escape-character handling. Ssh does this by default when 130a command is provided, the flag is provided to make it explicit. 131You could elide it if you want to. 132 133 <p>Programs started with stdio-connected gdbserver have <samp><span class="file">/dev/null</span></samp> for 134<code>stdin</code>, and <code>stdout</code>,<code>stderr</code> are sent back to gdb for 135display through a pipe connected to gdbserver. 136Both <code>stdout</code> and <code>stderr</code> use the same pipe. 137 138<h5 class="subsubsection">20.3.1.1 Attaching to a Running Program</h5> 139 140<p><a name="index-attach-to-a-program_002c-_0040code_007bgdbserver_007d-1198"></a><a name="index-g_t_0040option_007b_002d_002dattach_007d_002c-_0040code_007bgdbserver_007d-option-1199"></a> 141On some targets, <code>gdbserver</code> can also attach to running programs. 142This is accomplished via the <code>--attach</code> argument. The syntax is: 143 144<pre class="smallexample"> target> gdbserver --attach <var>comm</var> <var>pid</var> 145</pre> 146 <p><var>pid</var> is the process ID of a currently running process. It isn't necessary 147to point <code>gdbserver</code> at a binary for the running process. 148 149 <p><a name="index-pidof-1200"></a>You can debug processes by name instead of process ID if your target has the 150<code>pidof</code> utility: 151 152<pre class="smallexample"> target> gdbserver --attach <var>comm</var> `pidof <var>program</var>` 153</pre> 154 <p>In case more than one copy of <var>program</var> is running, or <var>program</var> 155has multiple threads, most versions of <code>pidof</code> support the 156<code>-s</code> option to only return the first process ID. 157 158<h5 class="subsubsection">20.3.1.2 Multi-Process Mode for <code>gdbserver</code></h5> 159 160<p><a name="index-g_t_0040code_007bgdbserver_007d_002c-multiple-processes-1201"></a><a name="index-multiple-processes-with-_0040code_007bgdbserver_007d-1202"></a> 161When you connect to <code>gdbserver</code> using <code>target remote</code>, 162<code>gdbserver</code> debugs the specified program only once. When the 163program exits, or you detach from it, <span class="sc">gdb</span> closes the connection 164and <code>gdbserver</code> exits. 165 166 <p>If you connect using <kbd>target extended-remote</kbd>, <code>gdbserver</code> 167enters multi-process mode. When the debugged program exits, or you 168detach from it, <span class="sc">gdb</span> stays connected to <code>gdbserver</code> even 169though no program is running. The <code>run</code> and <code>attach</code> 170commands instruct <code>gdbserver</code> to run or attach to a new program. 171The <code>run</code> command uses <code>set remote exec-file</code> (see <a href="set-remote-exec_002dfile.html#set-remote-exec_002dfile">set remote exec-file</a>) to select the program to run. Command line 172arguments are supported, except for wildcard expansion and I/O 173redirection (see <a href="Arguments.html#Arguments">Arguments</a>). 174 175 <p><a name="index-g_t_0040option_007b_002d_002dmulti_007d_002c-_0040code_007bgdbserver_007d-option-1203"></a>To start <code>gdbserver</code> without supplying an initial command to run 176or process ID to attach, use the <samp><span class="option">--multi</span></samp> command line option. 177Then you can connect using <kbd>target extended-remote</kbd> and start 178the program you want to debug. 179 180 <p>In multi-process mode <code>gdbserver</code> does not automatically exit unless you 181use the option <samp><span class="option">--once</span></samp>. You can terminate it by using 182<code>monitor exit</code> (see <a href="Monitor-Commands-for-gdbserver.html#Monitor-Commands-for-gdbserver">Monitor Commands for gdbserver</a>). Note that the 183conditions under which <code>gdbserver</code> terminates depend on how <span class="sc">gdb</span> 184connects to it (<kbd>target remote</kbd> or <kbd>target extended-remote</kbd>). The 185<samp><span class="option">--multi</span></samp> option to <code>gdbserver</code> has no influence on that. 186 187<h5 class="subsubsection">20.3.1.3 TCP port allocation lifecycle of <code>gdbserver</code></h5> 188 189<p>This section applies only when <code>gdbserver</code> is run to listen on a TCP port. 190 191 <p><code>gdbserver</code> normally terminates after all of its debugged processes have 192terminated in <kbd>target remote</kbd> mode. On the other hand, for <kbd>target 193extended-remote</kbd>, <code>gdbserver</code> stays running even with no processes left. 194<span class="sc">gdb</span> normally terminates the spawned debugged process on its exit, 195which normally also terminates <code>gdbserver</code> in the <kbd>target remote</kbd> 196mode. Therefore, when the connection drops unexpectedly, and <span class="sc">gdb</span> 197cannot ask <code>gdbserver</code> to kill its debugged processes, <code>gdbserver</code> 198stays running even in the <kbd>target remote</kbd> mode. 199 200 <p>When <code>gdbserver</code> stays running, <span class="sc">gdb</span> can connect to it again later. 201Such reconnecting is useful for features like <a href="disconnected-tracing.html#disconnected-tracing">disconnected tracing</a>. For 202completeness, at most one <span class="sc">gdb</span> can be connected at a time. 203 204 <p><a name="index-g_t_0040option_007b_002d_002donce_007d_002c-_0040code_007bgdbserver_007d-option-1204"></a>By default, <code>gdbserver</code> keeps the listening TCP port open, so that 205additional connections are possible. However, if you start <code>gdbserver</code> 206with the <samp><span class="option">--once</span></samp> option, it will stop listening for any further 207connection attempts after connecting to the first <span class="sc">gdb</span> session. This 208means no further connections to <code>gdbserver</code> will be possible after the 209first one. It also means <code>gdbserver</code> will terminate after the first 210connection with remote <span class="sc">gdb</span> has closed, even for unexpectedly closed 211connections and even in the <kbd>target extended-remote</kbd> mode. The 212<samp><span class="option">--once</span></samp> option allows reusing the same port number for connecting to 213multiple instances of <code>gdbserver</code> running on the same host, since each 214instance closes its port after the first connection. 215 216<h5 class="subsubsection">20.3.1.4 Other Command-Line Arguments for <code>gdbserver</code></h5> 217 218<p><a name="index-g_t_0040option_007b_002d_002ddebug_007d_002c-_0040code_007bgdbserver_007d-option-1205"></a>The <samp><span class="option">--debug</span></samp> option tells <code>gdbserver</code> to display extra 219status information about the debugging process. 220<a name="index-g_t_0040option_007b_002d_002dremote_002ddebug_007d_002c-_0040code_007bgdbserver_007d-option-1206"></a>The <samp><span class="option">--remote-debug</span></samp> option tells <code>gdbserver</code> to display 221remote protocol debug output. These options are intended for 222<code>gdbserver</code> development and for bug reports to the developers. 223 224 <p><a name="index-g_t_0040option_007b_002d_002dwrapper_007d_002c-_0040code_007bgdbserver_007d-option-1207"></a>The <samp><span class="option">--wrapper</span></samp> option specifies a wrapper to launch programs 225for debugging. The option should be followed by the name of the 226wrapper, then any command-line arguments to pass to the wrapper, then 227<kbd>--</kbd> indicating the end of the wrapper arguments. 228 229 <p><code>gdbserver</code> runs the specified wrapper program with a combined 230command line including the wrapper arguments, then the name of the 231program to debug, then any arguments to the program. The wrapper 232runs until it executes your program, and then <span class="sc">gdb</span> gains control. 233 234 <p>You can use any program that eventually calls <code>execve</code> with 235its arguments as a wrapper. Several standard Unix utilities do 236this, e.g. <code>env</code> and <code>nohup</code>. Any Unix shell script ending 237with <code>exec "$@"</code> will also work. 238 239 <p>For example, you can use <code>env</code> to pass an environment variable to 240the debugged program, without setting the variable in <code>gdbserver</code>'s 241environment: 242 243<pre class="smallexample"> $ gdbserver --wrapper env LD_PRELOAD=libtest.so -- :2222 ./testprog 244</pre> 245 <h4 class="subsection">20.3.2 Connecting to <code>gdbserver</code></h4> 246 247<p>Run <span class="sc">gdb</span> on the host system. 248 249 <p>First make sure you have the necessary symbol files. Load symbols for 250your application using the <code>file</code> command before you connect. Use 251<code>set sysroot</code> to locate target libraries (unless your <span class="sc">gdb</span> 252was compiled with the correct sysroot using <code>--with-sysroot</code>). 253 254 <p>The symbol file and target libraries must exactly match the executable 255and libraries on the target, with one exception: the files on the host 256system should not be stripped, even if the files on the target system 257are. Mismatched or missing files will lead to confusing results 258during debugging. On <span class="sc">gnu</span>/Linux targets, mismatched or missing 259files may also prevent <code>gdbserver</code> from debugging multi-threaded 260programs. 261 262 <p>Connect to your target (see <a href="Connecting.html#Connecting">Connecting to a Remote Target</a>). 263For TCP connections, you must start up <code>gdbserver</code> prior to using 264the <code>target remote</code> command. Otherwise you may get an error whose 265text depends on the host system, but which usually looks something like 266‘<samp><span class="samp">Connection refused</span></samp>’. Don't use the <code>load</code> 267command in <span class="sc">gdb</span> when using <code>gdbserver</code>, since the program is 268already on the target. 269 270<h4 class="subsection">20.3.3 Monitor Commands for <code>gdbserver</code></h4> 271 272<p><a name="index-monitor-commands_002c-for-_0040code_007bgdbserver_007d-1208"></a><a name="Monitor-Commands-for-gdbserver"></a>During a <span class="sc">gdb</span> session using <code>gdbserver</code>, you can use the 273<code>monitor</code> command to send special requests to <code>gdbserver</code>. 274Here are the available commands. 275 276 <dl> 277<dt><code>monitor help</code><dd>List the available monitor commands. 278 279 <br><dt><code>monitor set debug 0</code><dt><code>monitor set debug 1</code><dd>Disable or enable general debugging messages. 280 281 <br><dt><code>monitor set remote-debug 0</code><dt><code>monitor set remote-debug 1</code><dd>Disable or enable specific debugging messages associated with the remote 282protocol (see <a href="Remote-Protocol.html#Remote-Protocol">Remote Protocol</a>). 283 284 <br><dt><code>monitor set libthread-db-search-path [PATH]</code><dd><a name="index-gdbserver_002c-search-path-for-_0040code_007blibthread_005fdb_007d-1209"></a>When this command is issued, <var>path</var> is a colon-separated list of 285directories to search for <code>libthread_db</code> (see <a href="Threads.html#Threads">set libthread-db-search-path</a>). If you omit <var>path</var>, 286‘<samp><span class="samp">libthread-db-search-path</span></samp>’ will be reset to its default value. 287 288 <p>The special entry ‘<samp><span class="samp">$pdir</span></samp>’ for ‘<samp><span class="samp">libthread-db-search-path</span></samp>’ is 289not supported in <code>gdbserver</code>. 290 291 <br><dt><code>monitor exit</code><dd>Tell gdbserver to exit immediately. This command should be followed by 292<code>disconnect</code> to close the debugging session. <code>gdbserver</code> will 293detach from any attached processes and kill any processes it created. 294Use <code>monitor exit</code> to terminate <code>gdbserver</code> at the end 295of a multi-process mode debug session. 296 297 </dl> 298 299<h4 class="subsection">20.3.4 Tracepoints support in <code>gdbserver</code></h4> 300 301<p><a name="index-tracepoints-support-in-_0040code_007bgdbserver_007d-1210"></a> 302On some targets, <code>gdbserver</code> supports tracepoints, fast 303tracepoints and static tracepoints. 304 305 <p>For fast or static tracepoints to work, a special library called the 306<dfn>in-process agent</dfn> (IPA), must be loaded in the inferior process. 307This library is built and distributed as an integral part of 308<code>gdbserver</code>. In addition, support for static tracepoints 309requires building the in-process agent library with static tracepoints 310support. At present, the UST (LTTng Userspace Tracer, 311<a href="http://lttng.org/ust">http://lttng.org/ust</a>) tracing engine is supported. This support 312is automatically available if UST development headers are found in the 313standard include path when <code>gdbserver</code> is built, or if 314<code>gdbserver</code> was explicitly configured using <samp><span class="option">--with-ust</span></samp> 315to point at such headers. You can explicitly disable the support 316using <samp><span class="option">--with-ust=no</span></samp>. 317 318 <p>There are several ways to load the in-process agent in your program: 319 320 <dl> 321<dt><code>Specifying it as dependency at link time</code><dd> 322You can link your program dynamically with the in-process agent 323library. On most systems, this is accomplished by adding 324<code>-linproctrace</code> to the link command. 325 326 <br><dt><code>Using the system's preloading mechanisms</code><dd> 327You can force loading the in-process agent at startup time by using 328your system's support for preloading shared libraries. Many Unixes 329support the concept of preloading user defined libraries. In most 330cases, you do that by specifying <code>LD_PRELOAD=libinproctrace.so</code> 331in the environment. See also the description of <code>gdbserver</code>'s 332<samp><span class="option">--wrapper</span></samp> command line option. 333 334 <br><dt><code>Using </code><span class="sc">gdb</span><code> to force loading the agent at run time</code><dd> 335On some systems, you can force the inferior to load a shared library, 336by calling a dynamic loader function in the inferior that takes care 337of dynamically looking up and loading a shared library. On most Unix 338systems, the function is <code>dlopen</code>. You'll use the <code>call</code> 339command for that. For example: 340 341 <pre class="smallexample"> (gdb) call dlopen ("libinproctrace.so", ...) 342</pre> 343 <p>Note that on most Unix systems, for the <code>dlopen</code> function to be 344available, the program needs to be linked with <code>-ldl</code>. 345</dl> 346 347 <p>On systems that have a userspace dynamic loader, like most Unix 348systems, when you connect to <code>gdbserver</code> using <code>target 349remote</code>, you'll find that the program is stopped at the dynamic 350loader's entry point, and no shared library has been loaded in the 351program's address space yet, including the in-process agent. In that 352case, before being able to use any of the fast or static tracepoints 353features, you need to let the loader run and load the shared 354libraries. The simplest way to do that is to run the program to the 355main procedure. E.g., if debugging a C or C<tt>++</tt> program, start 356<code>gdbserver</code> like so: 357 358<pre class="smallexample"> $ gdbserver :9999 myprogram 359</pre> 360 <p>Start GDB and connect to <code>gdbserver</code> like so, and run to main: 361 362<pre class="smallexample"> $ gdb myprogram 363 (gdb) target remote myhost:9999 364 0x00007f215893ba60 in ?? () from /lib64/ld-linux-x86-64.so.2 365 (gdb) b main 366 (gdb) continue 367</pre> 368 <p>The in-process tracing agent library should now be loaded into the 369process; you can confirm it with the <code>info sharedlibrary</code> 370command, which will list <samp><span class="file">libinproctrace.so</span></samp> as loaded in the 371process. You are now ready to install fast tracepoints, list static 372tracepoint markers, probe static tracepoints markers, and start 373tracing. 374 375 <div class="footnote"> 376<hr> 377<h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> If you choose a port number that 378conflicts with another service, <code>gdbserver</code> prints an error message 379and exits.</p> 380 381 <hr></div> 382 383 </body></html> 384 385