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