Overview.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Free Software" and "Free Software Needs
  8. Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
  9. and with the Back-Cover Texts as in (a) below.
  10. (a) The FSF's Back-Cover Text is: "You are free to copy and modify
  11. this GNU Manual. Buying copies from GNU Press supports the FSF in
  12. developing GNU and promoting software freedom." -->
  13. <!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>Debugging with GDB: Overview</title>
  16. <meta name="description" content="Debugging with GDB: Overview">
  17. <meta name="keywords" content="Debugging with GDB: Overview">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Remote-Protocol.html#Remote-Protocol" rel="up" title="Remote Protocol">
  26. <link href="Packets.html#Packets" rel="next" title="Packets">
  27. <link href="Remote-Protocol.html#Remote-Protocol" rel="previous" title="Remote Protocol">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style:oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="Overview"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Packets.html#Packets" accesskey="n" rel="next">Packets</a>, Up: <a href="Remote-Protocol.html#Remote-Protocol" accesskey="u" rel="up">Remote Protocol</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>
  62. </div>
  63. <hr>
  64. <a name="Overview-1"></a>
  65. <h3 class="section">E.1 Overview</h3>
  66. <p>There may be occasions when you need to know something about the
  67. protocol&mdash;for example, if there is only one serial port to your target
  68. machine, you might want your program to do something special if it
  69. recognizes a packet meant for <small>GDB</small>.
  70. </p>
  71. <p>In the examples below, &lsquo;<samp>-&gt;</samp>&rsquo; and &lsquo;<samp>&lt;-</samp>&rsquo; are used to indicate
  72. transmitted and received data, respectively.
  73. </p>
  74. <a name="index-protocol_002c-GDB-remote-serial"></a>
  75. <a name="index-serial-protocol_002c-GDB-remote"></a>
  76. <a name="index-remote-serial-protocol"></a>
  77. <p>All <small>GDB</small> commands and responses (other than acknowledgments
  78. and notifications, see <a href="Notification-Packets.html#Notification-Packets">Notification Packets</a>) are sent as a
  79. <var>packet</var>. A <var>packet</var> is introduced with the character
  80. &lsquo;<samp>$</samp>&rsquo;, the actual <var>packet-data</var>, and the terminating character
  81. &lsquo;<samp>#</samp>&rsquo; followed by a two-digit <var>checksum</var>:
  82. </p>
  83. <div class="smallexample">
  84. <pre class="smallexample"><code>$</code><var>packet-data</var><code>#</code><var>checksum</var>
  85. </pre></div>
  86. <a name="index-checksum_002c-for-GDB-remote"></a>
  87. <p>The two-digit <var>checksum</var> is computed as the modulo 256 sum of all
  88. characters between the leading &lsquo;<samp>$</samp>&rsquo; and the trailing &lsquo;<samp>#</samp>&rsquo; (an
  89. eight bit unsigned checksum).
  90. </p>
  91. <p>Implementors should note that prior to <small>GDB</small> 5.0 the protocol
  92. specification also included an optional two-digit <var>sequence-id</var>:
  93. </p>
  94. <div class="smallexample">
  95. <pre class="smallexample"><code>$</code><var>sequence-id</var><code>:</code><var>packet-data</var><code>#</code><var>checksum</var>
  96. </pre></div>
  97. <a name="index-sequence_002did_002c-for-GDB-remote"></a>
  98. <p>That <var>sequence-id</var> was appended to the acknowledgment. <small>GDB</small>
  99. has never output <var>sequence-id</var>s. Stubs that handle packets added
  100. since <small>GDB</small> 5.0 must not accept <var>sequence-id</var>.
  101. </p>
  102. <p>When either the host or the target machine receives a packet, the first
  103. response expected is an acknowledgment: either &lsquo;<samp>+</samp>&rsquo; (to indicate
  104. the package was received correctly) or &lsquo;<samp>-</samp>&rsquo; (to request
  105. retransmission):
  106. </p>
  107. <div class="smallexample">
  108. <pre class="smallexample">-&gt; <code>$</code><var>packet-data</var><code>#</code><var>checksum</var>
  109. &lt;- <code>+</code>
  110. </pre></div>
  111. <p>The &lsquo;<samp>+</samp>&rsquo;/&lsquo;<samp>-</samp>&rsquo; acknowledgments can be disabled
  112. once a connection is established.
  113. See <a href="Packet-Acknowledgment.html#Packet-Acknowledgment">Packet Acknowledgment</a>, for details.
  114. </p>
  115. <p>The host (<small>GDB</small>) sends <var>command</var>s, and the target (the
  116. debugging stub incorporated in your program) sends a <var>response</var>. In
  117. the case of step and continue <var>command</var>s, the response is only sent
  118. when the operation has completed, and the target has again stopped all
  119. threads in all attached processes. This is the default all-stop mode
  120. behavior, but the remote protocol also supports <small>GDB</small>&rsquo;s non-stop
  121. execution mode; see <a href="Remote-Non_002dStop.html#Remote-Non_002dStop">Remote Non-Stop</a>, for details.
  122. </p>
  123. <p><var>packet-data</var> consists of a sequence of characters with the
  124. exception of &lsquo;<samp>#</samp>&rsquo; and &lsquo;<samp>$</samp>&rsquo; (see &lsquo;<samp>X</samp>&rsquo; packet for additional
  125. exceptions).
  126. </p>
  127. <a name="index-remote-protocol_002c-field-separator"></a>
  128. <p>Fields within the packet should be separated using &lsquo;<samp>,</samp>&rsquo; &lsquo;<samp>;</samp>&rsquo; or
  129. &lsquo;<samp>:</samp>&rsquo;. Except where otherwise noted all numbers are represented in
  130. <small>HEX</small> with leading zeros suppressed.
  131. </p>
  132. <p>Implementors should note that prior to <small>GDB</small> 5.0, the character
  133. &lsquo;<samp>:</samp>&rsquo; could not appear as the third character in a packet (as it
  134. would potentially conflict with the <var>sequence-id</var>).
  135. </p>
  136. <a name="index-remote-protocol_002c-binary-data"></a>
  137. <a name="Binary-Data"></a><p>Binary data in most packets is encoded either as two hexadecimal
  138. digits per byte of binary data. This allowed the traditional remote
  139. protocol to work over connections which were only seven-bit clean.
  140. Some packets designed more recently assume an eight-bit clean
  141. connection, and use a more efficient encoding to send and receive
  142. binary data.
  143. </p>
  144. <p>The binary data representation uses <code>7d</code> (<small>ASCII</small> &lsquo;<samp>}</samp>&rsquo;)
  145. as an escape character. Any escaped byte is transmitted as the escape
  146. character followed by the original character XORed with <code>0x20</code>.
  147. For example, the byte <code>0x7d</code> would be transmitted as the two
  148. bytes <code>0x7d 0x5d</code>. The bytes <code>0x23</code> (<small>ASCII</small> &lsquo;<samp>#</samp>&rsquo;),
  149. <code>0x24</code> (<small>ASCII</small> &lsquo;<samp>$</samp>&rsquo;), and <code>0x7d</code> (<small>ASCII</small>
  150. &lsquo;<samp>}</samp>&rsquo;) must always be escaped. Responses sent by the stub
  151. must also escape <code>0x2a</code> (<small>ASCII</small> &lsquo;<samp>*</samp>&rsquo;), so that it
  152. is not interpreted as the start of a run-length encoded sequence
  153. (described next).
  154. </p>
  155. <p>Response <var>data</var> can be run-length encoded to save space.
  156. Run-length encoding replaces runs of identical characters with one
  157. instance of the repeated character, followed by a &lsquo;<samp>*</samp>&rsquo; and a
  158. repeat count. The repeat count is itself sent encoded, to avoid
  159. binary characters in <var>data</var>: a value of <var>n</var> is sent as
  160. <code><var>n</var>+29</code>. For a repeat count greater or equal to 3, this
  161. produces a printable <small>ASCII</small> character, e.g. a space (<small>ASCII</small>
  162. code 32) for a repeat count of 3. (This is because run-length
  163. encoding starts to win for counts 3 or more.) Thus, for example,
  164. &lsquo;<samp>0* </samp>&rsquo; is a run-length encoding of &ldquo;0000&rdquo;: the space character
  165. after &lsquo;<samp>*</samp>&rsquo; means repeat the leading <code>0</code> <code>32&nbsp;<span class="nolinebreak">-</span>&nbsp;29&nbsp;=&nbsp;3</code><!-- /@w --> more times.
  166. </p>
  167. <p>The printable characters &lsquo;<samp>#</samp>&rsquo; and &lsquo;<samp>$</samp>&rsquo; or with a numeric value
  168. greater than 126 must not be used. Runs of six repeats (&lsquo;<samp>#</samp>&rsquo;) or
  169. seven repeats (&lsquo;<samp>$</samp>&rsquo;) can be expanded using a repeat count of only
  170. five (&lsquo;<samp>&quot;</samp>&rsquo;). For example, &lsquo;<samp>00000000</samp>&rsquo; can be encoded as
  171. &lsquo;<samp>0*&quot;00</samp>&rsquo;.
  172. </p>
  173. <p>The error response returned for some packets includes a two character
  174. error number. That number is not well defined.
  175. </p>
  176. <a name="index-empty-response_002c-for-unsupported-packets"></a>
  177. <p>For any <var>command</var> not supported by the stub, an empty response
  178. (&lsquo;<samp>$#00</samp>&rsquo;) should be returned. That way it is possible to extend the
  179. protocol. A newer <small>GDB</small> can tell if a packet is supported based
  180. on that response.
  181. </p>
  182. <p>At a minimum, a stub is required to support the &lsquo;<samp>?</samp>&rsquo; command to
  183. tell <small>GDB</small> the reason for halting, &lsquo;<samp>g</samp>&rsquo; and &lsquo;<samp>G</samp>&rsquo;
  184. commands for register access, and the &lsquo;<samp>m</samp>&rsquo; and &lsquo;<samp>M</samp>&rsquo; commands
  185. for memory access. Stubs that only control single-threaded targets
  186. can implement run control with the &lsquo;<samp>c</samp>&rsquo; (continue) command, and if
  187. the target architecture supports hardware-assisted single-stepping,
  188. the &lsquo;<samp>s</samp>&rsquo; (step) command. Stubs that support multi-threading
  189. targets should support the &lsquo;<samp>vCont</samp>&rsquo; command. All other commands
  190. are optional.
  191. </p>
  192. <hr>
  193. <div class="header">
  194. <p>
  195. Next: <a href="Packets.html#Packets" accesskey="n" rel="next">Packets</a>, Up: <a href="Remote-Protocol.html#Remote-Protocol" accesskey="u" rel="up">Remote Protocol</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>
  196. </div>
  197. </body>
  198. </html>