Reverse-Execution.html 11 KB

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