1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<!-- Copyright (C) 1988-2021 Free Software Foundation, Inc. 4 5Permission is granted to copy, distribute and/or modify this document 6under the terms of the GNU Free Documentation License, Version 1.3 or 7any later version published by the Free Software Foundation; with the 8Invariant Sections being "Free Software" and "Free Software Needs 9Free Documentation", with the Front-Cover Texts being "A GNU Manual," 10and with the Back-Cover Texts as in (a) below. 11 12(a) The FSF's Back-Cover Text is: "You are free to copy and modify 13this GNU Manual. Buying copies from GNU Press supports the FSF in 14developing GNU and promoting software freedom." --> 15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ --> 16<head> 17<title>Debugging with GDB: Connecting</title> 18 19<meta name="description" content="Debugging with GDB: Connecting"> 20<meta name="keywords" content="Debugging with GDB: Connecting"> 21<meta name="resource-type" content="document"> 22<meta name="distribution" content="global"> 23<meta name="Generator" content="makeinfo"> 24<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 25<link href="index.html#Top" rel="start" title="Top"> 26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> 27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> 28<link href="Remote-Debugging.html#Remote-Debugging" rel="up" title="Remote Debugging"> 29<link href="File-Transfer.html#File-Transfer" rel="next" title="File Transfer"> 30<link href="Remote-Debugging.html#Remote-Debugging" rel="previous" title="Remote Debugging"> 31<style type="text/css"> 32<!-- 33a.summary-letter {text-decoration: none} 34blockquote.smallquotation {font-size: smaller} 35div.display {margin-left: 3.2em} 36div.example {margin-left: 3.2em} 37div.indentedblock {margin-left: 3.2em} 38div.lisp {margin-left: 3.2em} 39div.smalldisplay {margin-left: 3.2em} 40div.smallexample {margin-left: 3.2em} 41div.smallindentedblock {margin-left: 3.2em; font-size: smaller} 42div.smalllisp {margin-left: 3.2em} 43kbd {font-style:oblique} 44pre.display {font-family: inherit} 45pre.format {font-family: inherit} 46pre.menu-comment {font-family: serif} 47pre.menu-preformatted {font-family: serif} 48pre.smalldisplay {font-family: inherit; font-size: smaller} 49pre.smallexample {font-size: smaller} 50pre.smallformat {font-family: inherit; font-size: smaller} 51pre.smalllisp {font-size: smaller} 52span.nocodebreak {white-space:nowrap} 53span.nolinebreak {white-space:nowrap} 54span.roman {font-family:serif; font-weight:normal} 55span.sansserif {font-family:sans-serif; font-weight:normal} 56ul.no-bullet {list-style: none} 57--> 58</style> 59 60 61</head> 62 63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> 64<a name="Connecting"></a> 65<div class="header"> 66<p> 67Next: <a href="File-Transfer.html#File-Transfer" accesskey="n" rel="next">File Transfer</a>, Up: <a href="Remote-Debugging.html#Remote-Debugging" accesskey="u" rel="up">Remote Debugging</a> [<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> 68</div> 69<hr> 70<a name="Connecting-to-a-Remote-Target"></a> 71<h3 class="section">20.1 Connecting to a Remote Target</h3> 72<a name="index-remote-debugging_002c-connecting"></a> 73<a name="index-gdbserver_002c-connecting"></a> 74<a name="index-remote-debugging_002c-types-of-connections"></a> 75<a name="index-gdbserver_002c-types-of-connections"></a> 76<a name="index-gdbserver_002c-target-remote-mode"></a> 77<a name="index-gdbserver_002c-target-extended_002dremote-mode"></a> 78 79<p>This section describes how to connect to a remote target, including the 80types of connections and their differences, how to set up executable and 81symbol files on the host and target, and the commands used for 82connecting to and disconnecting from the remote target. 83</p> 84<a name="Types-of-Remote-Connections"></a> 85<h4 class="subsection">20.1.1 Types of Remote Connections</h4> 86 87<p><small>GDB</small> supports two types of remote connections, <code>target remote</code> 88mode and <code>target extended-remote</code> mode. Note that many remote targets 89support only <code>target remote</code> mode. There are several major 90differences between the two types of connections, enumerated here: 91</p> 92<dl compact="compact"> 93<dd> 94<a name="index-remote-debugging_002c-detach-and-program-exit"></a> 95</dd> 96<dt>Result of detach or program exit</dt> 97<dd><p><strong>With target remote mode:</strong> When the debugged program exits or you 98detach from it, <small>GDB</small> disconnects from the target. When using 99<code>gdbserver</code>, <code>gdbserver</code> will exit. 100</p> 101<p><strong>With target extended-remote mode:</strong> When the debugged program exits or 102you detach from it, <small>GDB</small> remains connected to the target, even 103though no program is running. You can rerun the program, attach to a 104running program, or use <code>monitor</code> commands specific to the target. 105</p> 106<p>When using <code>gdbserver</code> in this case, it does not exit unless it was 107invoked using the <samp>--once</samp> option. If the <samp>--once</samp> option 108was not used, you can ask <code>gdbserver</code> to exit using the 109<code>monitor exit</code> command (see <a href="Server.html#Monitor-Commands-for-gdbserver">Monitor Commands for gdbserver</a>). 110</p> 111</dd> 112<dt>Specifying the program to debug</dt> 113<dd><p>For both connection types you use the <code>file</code> command to specify the 114program on the host system. If you are using <code>gdbserver</code> there are 115some differences in how to specify the location of the program on the 116target. 117</p> 118<p><strong>With target remote mode:</strong> You must either specify the program to debug 119on the <code>gdbserver</code> command line or use the <samp>--attach</samp> option 120(see <a href="Server.html#Attaching-to-a-program">Attaching to a Running Program</a>). 121</p> 122<a name="index-_002d_002dmulti_002c-gdbserver-option"></a> 123<p><strong>With target extended-remote mode:</strong> You may specify the program to debug 124on the <code>gdbserver</code> command line, or you can load the program or attach 125to it using <small>GDB</small> commands after connecting to <code>gdbserver</code>. 126</p> 127<a name="g_t_002d_002dmulti-Option-in-Types-of-Remote-Connnections"></a><p>You can start <code>gdbserver</code> without supplying an initial command to run 128or process ID to attach. To do this, use the <samp>--multi</samp> command line 129option. Then you can connect using <code>target extended-remote</code> and start 130the program you want to debug (see below for details on using the 131<code>run</code> command in this scenario). Note that the conditions under which 132<code>gdbserver</code> terminates depend on how <small>GDB</small> connects to it 133(<code>target remote</code> or <code>target extended-remote</code>). The 134<samp>--multi</samp> option to <code>gdbserver</code> has no influence on that. 135</p> 136</dd> 137<dt>The <code>run</code> command</dt> 138<dd><p><strong>With target remote mode:</strong> The <code>run</code> command is not 139supported. Once a connection has been established, you can use all 140the usual <small>GDB</small> commands to examine and change data. The 141remote program is already running, so you can use commands like 142<kbd>step</kbd> and <kbd>continue</kbd>. 143</p> 144<p><strong>With target extended-remote mode:</strong> The <code>run</code> command is 145supported. The <code>run</code> command uses the value set by 146<code>set remote exec-file</code> (see <a href="Remote-Configuration.html#set-remote-exec_002dfile">set remote exec-file</a>) to select 147the program to run. Command line arguments are supported, except for 148wildcard expansion and I/O redirection (see <a href="Arguments.html#Arguments">Arguments</a>). 149</p> 150<p>If you specify the program to debug on the command line, then the 151<code>run</code> command is not required to start execution, and you can 152resume using commands like <kbd>step</kbd> and <kbd>continue</kbd> as with 153<code>target remote</code> mode. 154</p> 155<a name="Attaching-in-Types-of-Remote-Connections"></a></dd> 156<dt>Attaching</dt> 157<dd><p><strong>With target remote mode:</strong> The <small>GDB</small> command <code>attach</code> is 158not supported. To attach to a running program using <code>gdbserver</code>, you 159must use the <samp>--attach</samp> option (see <a href="Server.html#Running-gdbserver">Running gdbserver</a>). 160</p> 161<p><strong>With target extended-remote mode:</strong> To attach to a running program, 162you may use the <code>attach</code> command after the connection has been 163established. If you are using <code>gdbserver</code>, you may also invoke 164<code>gdbserver</code> using the <samp>--attach</samp> option 165(see <a href="Server.html#Running-gdbserver">Running gdbserver</a>). 166</p> 167<p>Some remote targets allow <small>GDB</small> to determine the executable file running 168in the process the debugger is attaching to. In such a case, <small>GDB</small> 169uses the value of <code>exec-file-mismatch</code> to handle a possible mismatch 170between the executable file name running in the process and the name of the 171current exec-file loaded by <small>GDB</small> (see <a href="Attach.html#set-exec_002dfile_002dmismatch">set exec-file-mismatch</a>). 172</p> 173</dd> 174</dl> 175 176<a name="Host-and-target-files"></a><a name="Host-and-Target-Files"></a> 177<h4 class="subsection">20.1.2 Host and Target Files</h4> 178<a name="index-remote-debugging_002c-symbol-files"></a> 179<a name="index-symbol-files_002c-remote-debugging"></a> 180 181<p><small>GDB</small>, running on the host, needs access to symbol and debugging 182information for your program running on the target. This requires 183access to an unstripped copy of your program, and possibly any associated 184symbol files. Note that this section applies equally to both <code>target 185remote</code> mode and <code>target extended-remote</code> mode. 186</p> 187<p>Some remote targets (see <a href="General-Query-Packets.html#qXfer-executable-filename-read">qXfer executable filename read</a>, and 188see <a href="Host-I_002fO-Packets.html#Host-I_002fO-Packets">Host I/O Packets</a>) allow <small>GDB</small> to access program files over 189the same connection used to communicate with <small>GDB</small>. With such a 190target, if the remote program is unstripped, the only command you need is 191<code>target remote</code> (or <code>target extended-remote</code>). 192</p> 193<p>If the remote program is stripped, or the target does not support remote 194program file access, start up <small>GDB</small> using the name of the local 195unstripped copy of your program as the first argument, or use the 196<code>file</code> command. Use <code>set sysroot</code> to specify the location (on 197the host) of target libraries (unless your <small>GDB</small> was compiled with 198the correct sysroot using <code>--with-sysroot</code>). Alternatively, you 199may use <code>set solib-search-path</code> to specify how <small>GDB</small> locates 200target libraries. 201</p> 202<p>The symbol file and target libraries must exactly match the executable 203and libraries on the target, with one exception: the files on the host 204system should not be stripped, even if the files on the target system 205are. Mismatched or missing files will lead to confusing results 206during debugging. On <small>GNU</small>/Linux targets, mismatched or missing 207files may also prevent <code>gdbserver</code> from debugging multi-threaded 208programs. 209</p> 210<a name="Remote-Connection-Commands"></a> 211<h4 class="subsection">20.1.3 Remote Connection Commands</h4> 212<a name="index-remote-connection-commands"></a> 213<p><small>GDB</small> can communicate with the target over a serial line, a 214local Unix domain socket, or 215over an <acronym>IP</acronym> network using <acronym>TCP</acronym> or <acronym>UDP</acronym>. In 216each case, <small>GDB</small> uses the same protocol for debugging your 217program; only the medium carrying the debugging packets varies. The 218<code>target remote</code> and <code>target extended-remote</code> commands 219establish a connection to the target. Both commands accept the same 220arguments, which indicate the medium to use: 221</p> 222<dl compact="compact"> 223<dt><code>target remote <var>serial-device</var></code></dt> 224<dt><code>target extended-remote <var>serial-device</var></code></dt> 225<dd><a name="index-serial-line_002c-target-remote"></a> 226<p>Use <var>serial-device</var> to communicate with the target. For example, 227to use a serial line connected to the device named <samp>/dev/ttyb</samp>: 228</p> 229<div class="smallexample"> 230<pre class="smallexample">target remote /dev/ttyb 231</pre></div> 232 233<p>If you’re using a serial line, you may want to give <small>GDB</small> the 234‘<samp>--baud</samp>’ option, or use the <code>set serial baud</code> command 235(see <a href="Remote-Configuration.html#Remote-Configuration">set serial baud</a>) before the 236<code>target</code> command. 237</p> 238</dd> 239<dt><code>target remote <var>local-socket</var></code></dt> 240<dt><code>target extended-remote <var>local-socket</var></code></dt> 241<dd><a name="index-local-socket_002c-target-remote"></a> 242<a name="index-Unix-domain-socket"></a> 243<p>Use <var>local-socket</var> to communicate with the target. For example, 244to use a local Unix domain socket bound to the file system entry <samp>/tmp/gdb-socket0</samp>: 245</p> 246<div class="smallexample"> 247<pre class="smallexample">target remote /tmp/gdb-socket0 248</pre></div> 249 250<p>Note that this command has the same form as the command to connect 251to a serial line. <small>GDB</small> will automatically determine which 252kind of file you have specified and will make the appropriate kind 253of connection. 254This feature is not available if the host system does not support 255Unix domain sockets. 256</p> 257</dd> 258<dt><code>target remote <code><var>host</var>:<var>port</var></code></code></dt> 259<dt><code>target remote <code>[<var>host</var>]:<var>port</var></code></code></dt> 260<dt><code>target remote <code>tcp:<var>host</var>:<var>port</var></code></code></dt> 261<dt><code>target remote <code>tcp:[<var>host</var>]:<var>port</var></code></code></dt> 262<dt><code>target remote <code>tcp4:<var>host</var>:<var>port</var></code></code></dt> 263<dt><code>target remote <code>tcp6:<var>host</var>:<var>port</var></code></code></dt> 264<dt><code>target remote <code>tcp6:[<var>host</var>]:<var>port</var></code></code></dt> 265<dt><code>target extended-remote <code><var>host</var>:<var>port</var></code></code></dt> 266<dt><code>target extended-remote <code>[<var>host</var>]:<var>port</var></code></code></dt> 267<dt><code>target extended-remote <code>tcp:<var>host</var>:<var>port</var></code></code></dt> 268<dt><code>target extended-remote <code>tcp:[<var>host</var>]:<var>port</var></code></code></dt> 269<dt><code>target extended-remote <code>tcp4:<var>host</var>:<var>port</var></code></code></dt> 270<dt><code>target extended-remote <code>tcp6:<var>host</var>:<var>port</var></code></code></dt> 271<dt><code>target extended-remote <code>tcp6:[<var>host</var>]:<var>port</var></code></code></dt> 272<dd><a name="index-TCP-port_002c-target-remote"></a> 273<p>Debug using a <acronym>TCP</acronym> connection to <var>port</var> on <var>host</var>. 274The <var>host</var> may be either a host name, a numeric <acronym>IPv4</acronym> 275address, or a numeric <acronym>IPv6</acronym> address (with or without the 276square brackets to separate the address from the port); <var>port</var> 277must be a decimal number. The <var>host</var> could be the target machine 278itself, if it is directly connected to the net, or it might be a 279terminal server which in turn has a serial line to the target. 280</p> 281<p>For example, to connect to port 2828 on a terminal server named 282<code>manyfarms</code>: 283</p> 284<div class="smallexample"> 285<pre class="smallexample">target remote manyfarms:2828 286</pre></div> 287 288<p>To connect to port 2828 on a terminal server whose address is 289<code>2001:0db8:85a3:0000:0000:8a2e:0370:7334</code>, you can either use the 290square bracket syntax: 291</p> 292<div class="smallexample"> 293<pre class="smallexample">target remote [2001:0db8:85a3:0000:0000:8a2e:0370:7334]:2828 294</pre></div> 295 296<p>or explicitly specify the <acronym>IPv6</acronym> protocol: 297</p> 298<div class="smallexample"> 299<pre class="smallexample">target remote tcp6:2001:0db8:85a3:0000:0000:8a2e:0370:7334:2828 300</pre></div> 301 302<p>This last example may be confusing to the reader, because there is no 303visible separation between the hostname and the port number. 304Therefore, we recommend the user to provide <acronym>IPv6</acronym> addresses 305using square brackets for clarity. However, it is important to 306mention that for <small>GDB</small> there is no ambiguity: the number after 307the last colon is considered to be the port number. 308</p> 309<p>If your remote target is actually running on the same machine as your 310debugger session (e.g. a simulator for your target running on the 311same host), you can omit the hostname. For example, to connect to 312port 1234 on your local machine: 313</p> 314<div class="smallexample"> 315<pre class="smallexample">target remote :1234 316</pre></div> 317 318<p>Note that the colon is still required here. 319</p> 320</dd> 321<dt><code>target remote <code>udp:<var>host</var>:<var>port</var></code></code></dt> 322<dt><code>target remote <code>udp:[<var>host</var>]:<var>port</var></code></code></dt> 323<dt><code>target remote <code>udp4:<var>host</var>:<var>port</var></code></code></dt> 324<dt><code>target remote <code>udp6:[<var>host</var>]:<var>port</var></code></code></dt> 325<dt><code>target extended-remote <code>udp:<var>host</var>:<var>port</var></code></code></dt> 326<dt><code>target extended-remote <code>udp:<var>host</var>:<var>port</var></code></code></dt> 327<dt><code>target extended-remote <code>udp:[<var>host</var>]:<var>port</var></code></code></dt> 328<dt><code>target extended-remote <code>udp4:<var>host</var>:<var>port</var></code></code></dt> 329<dt><code>target extended-remote <code>udp6:<var>host</var>:<var>port</var></code></code></dt> 330<dt><code>target extended-remote <code>udp6:[<var>host</var>]:<var>port</var></code></code></dt> 331<dd><a name="index-UDP-port_002c-target-remote"></a> 332<p>Debug using <acronym>UDP</acronym> packets to <var>port</var> on <var>host</var>. For example, to 333connect to <acronym>UDP</acronym> port 2828 on a terminal server named <code>manyfarms</code>: 334</p> 335<div class="smallexample"> 336<pre class="smallexample">target remote udp:manyfarms:2828 337</pre></div> 338 339<p>When using a <acronym>UDP</acronym> connection for remote debugging, you should 340keep in mind that the ‘U’ stands for “Unreliable”. <acronym>UDP</acronym> 341can silently drop packets on busy or unreliable networks, which will 342cause havoc with your debugging session. 343</p> 344</dd> 345<dt><code>target remote | <var>command</var></code></dt> 346<dt><code>target extended-remote | <var>command</var></code></dt> 347<dd><a name="index-pipe_002c-target-remote-to"></a> 348<p>Run <var>command</var> in the background and communicate with it using a 349pipe. The <var>command</var> is a shell command, to be parsed and expanded 350by the system’s command shell, <code>/bin/sh</code>; it should expect remote 351protocol packets on its standard input, and send replies on its 352standard output. You could use this to run a stand-alone simulator 353that speaks the remote debugging protocol, to make net connections 354using programs like <code>ssh</code>, or for other similar tricks. 355</p> 356<p>If <var>command</var> closes its standard output (perhaps by exiting), 357<small>GDB</small> will try to send it a <code>SIGTERM</code> signal. (If the 358program has already exited, this will have no effect.) 359</p> 360</dd> 361</dl> 362 363<a name="index-interrupting-remote-programs"></a> 364<a name="index-remote-programs_002c-interrupting"></a> 365<p>Whenever <small>GDB</small> is waiting for the remote program, if you type the 366interrupt character (often <kbd>Ctrl-c</kbd>), <small>GDB</small> attempts to stop the 367program. This may or may not succeed, depending in part on the hardware 368and the serial drivers the remote system uses. If you type the 369interrupt character once again, <small>GDB</small> displays this prompt: 370</p> 371<div class="smallexample"> 372<pre class="smallexample">Interrupted while waiting for the program. 373Give up (and stop debugging it)? (y or n) 374</pre></div> 375 376<p>In <code>target remote</code> mode, if you type <kbd>y</kbd>, <small>GDB</small> abandons 377the remote debugging session. (If you decide you want to try again later, 378you can use <kbd>target remote</kbd> again to connect once more.) If you type 379<kbd>n</kbd>, <small>GDB</small> goes back to waiting. 380</p> 381<p>In <code>target extended-remote</code> mode, typing <kbd>n</kbd> will leave 382<small>GDB</small> connected to the target. 383</p> 384<dl compact="compact"> 385<dd><a name="index-detach-_0028remote_0029"></a> 386</dd> 387<dt><code>detach</code></dt> 388<dd><p>When you have finished debugging the remote program, you can use the 389<code>detach</code> command to release it from <small>GDB</small> control. 390Detaching from the target normally resumes its execution, but the results 391will depend on your particular remote stub. After the <code>detach</code> 392command in <code>target remote</code> mode, <small>GDB</small> is free to connect to 393another target. In <code>target extended-remote</code> mode, <small>GDB</small> is 394still connected to the target. 395</p> 396<a name="index-disconnect"></a> 397</dd> 398<dt><code>disconnect</code></dt> 399<dd><p>The <code>disconnect</code> command closes the connection to the target, and 400the target is generally not resumed. It will wait for <small>GDB</small> 401(this instance or another one) to connect and continue debugging. After 402the <code>disconnect</code> command, <small>GDB</small> is again free to connect to 403another target. 404</p> 405<a name="index-send-command-to-remote-monitor"></a> 406<a name="index-extend-GDB-for-remote-targets"></a> 407<a name="index-add-new-commands-for-external-monitor"></a> 408<a name="index-monitor"></a> 409</dd> 410<dt><code>monitor <var>cmd</var></code></dt> 411<dd><p>This command allows you to send arbitrary commands directly to the 412remote monitor. Since <small>GDB</small> doesn’t care about the commands it 413sends like this, this command is the way to extend <small>GDB</small>—you 414can add new commands that only the external monitor will understand 415and implement. 416</p></dd> 417</dl> 418 419<hr> 420<div class="header"> 421<p> 422Next: <a href="File-Transfer.html#File-Transfer" accesskey="n" rel="next">File Transfer</a>, Up: <a href="Remote-Debugging.html#Remote-Debugging" accesskey="u" rel="up">Remote Debugging</a> [<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> 423</div> 424 425 426 427</body> 428</html> 429