You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Reverse-Execution.html 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 6.5, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <title>Reverse Execution (Debugging with GDB)</title>
  17. <meta name="description" content="Reverse Execution (Debugging with GDB)">
  18. <meta name="keywords" content="Reverse Execution (Debugging with GDB)">
  19. <meta name="resource-type" content="document">
  20. <meta name="distribution" content="global">
  21. <meta name="Generator" content="makeinfo">
  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="index.html#Top" rel="up" title="Top">
  26. <link href="Process-Record-and-Replay.html#Process-Record-and-Replay" rel="next" title="Process Record and Replay">
  27. <link href="Observer-Mode.html#Observer-Mode" rel="prev" title="Observer Mode">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.indentedblock {margin-right: 0em}
  32. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  33. blockquote.smallquotation {font-size: smaller}
  34. div.display {margin-left: 3.2em}
  35. div.example {margin-left: 3.2em}
  36. div.lisp {margin-left: 3.2em}
  37. div.smalldisplay {margin-left: 3.2em}
  38. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  50. span.roman {font-family: initial; font-weight: normal}
  51. span.sansserif {font-family: sans-serif; font-weight: normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en">
  57. <a name="Reverse-Execution"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="Process-Record-and-Replay.html#Process-Record-and-Replay" accesskey="n" rel="next">Process Record and Replay</a>, Previous: <a href="Stopping.html#Stopping" accesskey="p" rel="prev">Stopping</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</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>
  61. </div>
  62. <hr>
  63. <a name="Running-programs-backward"></a>
  64. <h2 class="chapter">6 Running programs backward</h2>
  65. <a name="index-reverse-execution"></a>
  66. <a name="index-running-programs-backward"></a>
  67. <p>When you are debugging a program, it is not unusual to realize that
  68. you have gone too far, and some event of interest has already happened.
  69. If the target environment supports it, <small>GDB</small> can allow you to
  70. &ldquo;rewind&rdquo; the program by running it backward.
  71. </p>
  72. <p>A target environment that supports reverse execution should be able
  73. to &ldquo;undo&rdquo; the changes in machine state that have taken place as the
  74. program was executing normally. Variables, registers etc. should
  75. revert to their previous values. Obviously this requires a great
  76. deal of sophistication on the part of the target environment; not
  77. all target environments can support reverse execution.
  78. </p>
  79. <p>When a program is executed in reverse, the instructions that
  80. have most recently been executed are &ldquo;un-executed&rdquo;, in reverse
  81. order. The program counter runs backward, following the previous
  82. thread of execution in reverse. As each instruction is &ldquo;un-executed&rdquo;,
  83. the values of memory and/or registers that were changed by that
  84. instruction are reverted to their previous states. After executing
  85. a piece of source code in reverse, all side effects of that code
  86. should be &ldquo;undone&rdquo;, and all variables should be returned to their
  87. prior values<a name="DOCF7" href="#FOOT7"><sup>7</sup></a>.
  88. </p>
  89. <p>On some platforms, <small>GDB</small> has built-in support for reverse
  90. execution, activated with the <code>record</code> or <code>record btrace</code>
  91. commands. See <a href="Process-Record-and-Replay.html#Process-Record-and-Replay">Process Record and Replay</a>. Some remote targets,
  92. typically full system emulators, support reverse execution directly
  93. without requiring any special command.
  94. </p>
  95. <p>If you are debugging in a target environment that supports
  96. reverse execution, <small>GDB</small> provides the following commands.
  97. </p>
  98. <dl compact="compact">
  99. <dd><a name="index-reverse_002dcontinue"></a>
  100. <a name="index-rc-_0028reverse_002dcontinue_0029"></a>
  101. </dd>
  102. <dt><code>reverse-continue <span class="roman">[</span><var>ignore-count</var><span class="roman">]</span></code></dt>
  103. <dt><code>rc <span class="roman">[</span><var>ignore-count</var><span class="roman">]</span></code></dt>
  104. <dd><p>Beginning at the point where your program last stopped, start executing
  105. in reverse. Reverse execution will stop for breakpoints and synchronous
  106. exceptions (signals), just like normal execution. Behavior of
  107. asynchronous signals depends on the target environment.
  108. </p>
  109. <a name="index-reverse_002dstep"></a>
  110. <a name="index-rs-_0028step_0029"></a>
  111. </dd>
  112. <dt><code>reverse-step <span class="roman">[</span><var>count</var><span class="roman">]</span></code></dt>
  113. <dd><p>Run the program backward until control reaches the start of a
  114. different source line; then stop it, and return control to <small>GDB</small>.
  115. </p>
  116. <p>Like the <code>step</code> command, <code>reverse-step</code> will only stop
  117. at the beginning of a source line. It &ldquo;un-executes&rdquo; the previously
  118. executed source line. If the previous source line included calls to
  119. debuggable functions, <code>reverse-step</code> will step (backward) into
  120. the called function, stopping at the beginning of the <em>last</em>
  121. statement in the called function (typically a return statement).
  122. </p>
  123. <p>Also, as with the <code>step</code> command, if non-debuggable functions are
  124. called, <code>reverse-step</code> will run thru them backward without stopping.
  125. </p>
  126. <a name="index-reverse_002dstepi"></a>
  127. <a name="index-rsi-_0028reverse_002dstepi_0029"></a>
  128. </dd>
  129. <dt><code>reverse-stepi <span class="roman">[</span><var>count</var><span class="roman">]</span></code></dt>
  130. <dd><p>Reverse-execute one machine instruction. Note that the instruction
  131. to be reverse-executed is <em>not</em> the one pointed to by the program
  132. counter, but the instruction executed prior to that one. For instance,
  133. if the last instruction was a jump, <code>reverse-stepi</code> will take you
  134. back from the destination of the jump to the jump instruction itself.
  135. </p>
  136. <a name="index-reverse_002dnext"></a>
  137. <a name="index-rn-_0028reverse_002dnext_0029"></a>
  138. </dd>
  139. <dt><code>reverse-next <span class="roman">[</span><var>count</var><span class="roman">]</span></code></dt>
  140. <dd><p>Run backward to the beginning of the previous line executed in
  141. the current (innermost) stack frame. If the line contains function
  142. calls, they will be &ldquo;un-executed&rdquo; without stopping. Starting from
  143. the first line of a function, <code>reverse-next</code> will take you back
  144. to the caller of that function, <em>before</em> the function was called,
  145. just as the normal <code>next</code> command would take you from the last
  146. line of a function back to its return to its caller
  147. <a name="DOCF8" href="#FOOT8"><sup>8</sup></a>.
  148. </p>
  149. <a name="index-reverse_002dnexti"></a>
  150. <a name="index-rni-_0028reverse_002dnexti_0029"></a>
  151. </dd>
  152. <dt><code>reverse-nexti <span class="roman">[</span><var>count</var><span class="roman">]</span></code></dt>
  153. <dd><p>Like <code>nexti</code>, <code>reverse-nexti</code> executes a single instruction
  154. in reverse, except that called functions are &ldquo;un-executed&rdquo; atomically.
  155. That is, if the previously executed instruction was a return from
  156. another function, <code>reverse-nexti</code> will continue to execute
  157. in reverse until the call to that function (from the current stack
  158. frame) is reached.
  159. </p>
  160. <a name="index-reverse_002dfinish"></a>
  161. </dd>
  162. <dt><code>reverse-finish</code></dt>
  163. <dd><p>Just as the <code>finish</code> command takes you to the point where the
  164. current function returns, <code>reverse-finish</code> takes you to the point
  165. where it was called. Instead of ending up at the end of the current
  166. function invocation, you end up at the beginning.
  167. </p>
  168. <a name="index-set-exec_002ddirection"></a>
  169. </dd>
  170. <dt><code>set exec-direction</code></dt>
  171. <dd><p>Set the direction of target execution.
  172. </p></dd>
  173. <dt><code>set exec-direction reverse</code></dt>
  174. <dd><a name="index-execute-forward-or-backward-in-time"></a>
  175. <p><small>GDB</small> will perform all execution commands in reverse, until the
  176. exec-direction mode is changed to &ldquo;forward&rdquo;. Affected commands include
  177. <code>step, stepi, next, nexti, continue, and finish</code>. The <code>return</code>
  178. command cannot be used in reverse mode.
  179. </p></dd>
  180. <dt><code>set exec-direction forward</code></dt>
  181. <dd><p><small>GDB</small> will perform all execution commands in the normal fashion.
  182. This is the default.
  183. </p></dd>
  184. </dl>
  185. <div class="footnote">
  186. <hr>
  187. <h4 class="footnotes-heading">Footnotes</h4>
  188. <h3><a name="FOOT7" href="#DOCF7">(7)</a></h3>
  189. <p>Note that some side effects are easier to undo than others. For instance,
  190. memory and registers are relatively easy, but device I/O is hard. Some
  191. targets may be able undo things like device I/O, and some may not.
  192. </p>
  193. <p>The contract between <small>GDB</small> and the reverse executing target
  194. requires only that the target do something reasonable when
  195. <small>GDB</small> tells it to execute backwards, and then report the
  196. results back to <small>GDB</small>. Whatever the target reports back to
  197. <small>GDB</small>, <small>GDB</small> will report back to the user. <small>GDB</small>
  198. assumes that the memory and registers that the target reports are in a
  199. consistent state, but <small>GDB</small> accepts whatever it is given.
  200. </p>
  201. <h3><a name="FOOT8" href="#DOCF8">(8)</a></h3>
  202. <p>Unless the code is too heavily optimized.</p>
  203. </div>
  204. <hr>
  205. <div class="header">
  206. <p>
  207. Next: <a href="Process-Record-and-Replay.html#Process-Record-and-Replay" accesskey="n" rel="next">Process Record and Replay</a>, Previous: <a href="Stopping.html#Stopping" accesskey="p" rel="prev">Stopping</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</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>
  208. </div>
  209. </body>
  210. </html>