To join the debugger that you are using GDB7.0 and above and run on a platform that supports reverse debugging, you can debug the program with the following commands:
Reverse-continue
The reverse runner knows that it encounters an event (such as a breakpoint, observer, exception) that can cause the program to break.
Reverse-step
Reverse-run the program to the last source line that was executed.
Reverse-stepi
Reverse running the program to the previous machine instruction
Reverse-next
The reverse runs to the last line of source code that was executed, but does not enter the function.
Reverse-nexti
Run backwards to the previous machine instruction, unless the instruction is used to return a function call, and the entire function will be executed in reverse.
Reverse-finish
The reverse runner returns to the place where the current function is called.
Set exec-direction [forward | reverse]
Set the direction of the program, you can use the usual command step and continue, etc. to perform reverse debugging commands.
The inverse of the above can also be understood to undo the effect of the statement that is run later, back to the previous state.
OK, then let's try to debug backwards.
First, confirm that your platform supports process record playback (processes records and Replay), and when the debugger enables the process record playback function, the debugger records the daemon, which is the difference between the running state of each step of the debug process and the previous run state. It is convenient to go back to the previous step when you need to undo it.
Suppose we have the following C program:
[CPP]View PlainCopyprint?
- int main (int argc, const Char *argv[])
- {
- int a = 0;
- A = 1;
- A = 2;
- return 0;
- }
Compile it and add the debug symbols:
[Python]View PlainCopy print?
- $ gcc-wall-g A.C
To start Debugging:
[Python]View PlainCopyprint?
- $ gdb a.out
Take a look at the source code:
[Python]View PlainCopy print?
- (GDB) L
- 1 int main (int argc, const char *argv[])
- 2 {
- 3 int a = 0;
- 4 A = 1;
- 5 A = 2;
- 6 return 0;
- 7}
Next, set a breakpoint on the third line:
[Python]View PlainCopyprint?
- (GDB) b 3
- Breakpoint 1 at 0x804839a:file A.C, line 3.
Run, the program will stop at the third line:
[Python]View PlainCopy print?
- (GDB) R
- Starting program:/home/cheryl/a.out
- Breakpoint 1, Main (argc=1, argv=0xbffff3e4) at A.C:3
- 3 int a = 0;
Setting a monitoring point for variable a allows us to observe:
[Python]View PlainCopyprint?
- (GDB) Watch a
- Hardware watchpoint 2:a
Start process record playback:
[Python]View PlainCopyprint?
- (GDB) record
Now each step of the debugger will record the changes for backtracking. We execute 3 consecutive statements.
[Python]View PlainCopyprint?
- (GDB) n
- 4 A = 1;
- (GDB)
- Hardware watchpoint 2:a
- Old value = 0
- New value = 1
- Main (argc=1, argv=0xbffff3e4) at A.C:5
- 5 A = 2;
- (GDB)
- Hardware watchpoint 2:a
- Old value = 1
- New value = 2
- Main (argc=1, argv=0xbffff3e4) at A.C:6
- 6 return 0;
As you can see, the value of a is changed from 0 to 1, then to 2, what if you want the program to revert back to its previous state? You can use the Reverse-next command:
[Python]View PlainCopyprint?
- (GDB) Reverse-next
- Hardware watchpoint 2:a
- Old value = 2
- New value = 1
- Main (argc=1, argv=0xbffff3e4) at A.C:5
- 5 A = 2;
- (GDB)
- Hardware watchpoint 2:a
- Old value = 1
- New value = 0
- Main (argc=1, argv=0xbffff3e4) at A.C:4
- 4 A = 1;
- (GDB)
- No more reverse-execution history.
- Main (argc=1, argv=0xbffff3e4) at A.C:3
- 3 int a = 0;
- (GDB)
So the program goes back to where we started the process of recording playback, and the value of a is returned to its original state in two steps.
If you need to turn off process record playback, you can use the record stop:
[Python]View PlainCopyprint?
- (GDB) Record stop
- Process record is stoped and all execution logs is deleted.
Reference: "Reverse debugging with GDB"---http://sourceware.org/gdb/wiki/ReverseDebug
GDB fallback debugging